
function updte()
{
	suffix = document.accountinfo.suffix.value;
	firstname = document.accountinfo.firstname.value;
	lastname = document.accountinfo.lastname.value;
	cell = document.accountinfo.cell.value;
	homephone = document.accountinfo.homephone.value;
	workphone = document.accountinfo.workphone.value;
	address = document.accountinfo.address.value;
	zip = document.accountinfo.zip.value;
	city = document.accountinfo.city.value;
	state = document.accountinfo.state.value;
//	dob = document.accountinfo.dob.value;
	country=document.accountinfo.country.value;
	//email=document.accountinfo.email.value;
	

	
	var why = '';

	if (suffix == "")
	{
  		 why += "Please, choose a title!\r\n";
	}

	if (firstname == "")
	{
  		 why += "Please, enter a firstname!\r\n";
	}
	
	if (lastname == "")
	{
		why += "Please, enter a lastname!\r\n";
	}
	
	if (cell == "")
	{
  		 why += "Please, choose a cell phone!\r\n";
	}

	if (homephone == "")
	{
  		 why += "Please, enter a home phone!\r\n";
	}
	
	if (workphone == "")
	{
		why += "Please, enter a work phone!\r\n";
	}
	
	if (address == "")
	{
		why += "Please, enter an address!\r\n";
	}
	
	if (zip == "")
	{
  		 why += "Please, enter a zipcode!\r\n";
	}
	
	if (city == "")
	{
		why += "Please, enter a city!\r\n";
	}
	
	if (state == "")
	{
		why += "Please, enter a state!\r\n";
	}
	
	
	/*if (dob == "")
	{
		why += "Please, enter a date of birth!\r\n";
	}
*/

	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
	
		if (confirm("Are you sure you want to update your profile?")) { 
			var input = document.createElement("input");
			input.setAttribute("type", "hidden");
			input.setAttribute("name", "update");
			input.setAttribute("value", "update");
			document.getElementById("accountinfo").appendChild(input);
			document.accountinfo.submit();
		}
	}

}


function passcheck()
{  
	
		newpass = document.passinfo.newpass.value;
		confirmpass = document.passinfo.confirmpass.value;
		oldpass = document.passinfo.oldpass.value;
	
		var input = document.createElement("input");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "update");
		input.setAttribute("value", "pass");
		document.getElementById("passinfo").appendChild(input);

		var why = '';
	 
		if ((newpass == "") || (confirmpass == "") || (oldpass == "")) {
			alert ("Please fill every field!\r\n");
 		}
 		else {
	
			if ((newpass != confirmpass)|| (newpass == ""))  {
				why += "The new password and the confirmation one don't match!\r\n";
			}


			if (why != "") 
			{
   				alert(why); 	   
			} 
			else {
				if (confirm("Are you sure you want to update your information?")) {
					document.passinfo.submit();
				}
			}
 		}
}




function Delete_book(m)
{
	if (confirm("Are you sure you want to delete this apartement booking?")) {
		if (confirm("Careful, this booking information will be deleted!")) { 
			var input = document.createElement("input");
			input.setAttribute("type", "hidden");
			input.setAttribute("name", "update");
			input.setAttribute("value", "deletebook");
			document.getElementById("book").appendChild(input);
			//alert("Successfully deleted");
			document.book.submit();
		}
	}
}

function Edit_book()
{

	client = document.book.client.value;
	startdate = document.book.startdate.value;
	enddate = document.book.enddate.value;
	
	var why = '';

	if (client == "")
	{
  		 why += "Please, choose a client!\r\n";
	}
	
	if (startdate == "")
	{
		why += "Please, enter a start date!\r\n";
	}
	
	if (enddate == "")
	{
		why += "Please, enter an end date!\r\n";
	}
	
	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
		var input = document.createElement("input");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "update");
		input.setAttribute("value", "editbook");
		document.getElementById("book").appendChild(input);
		document.book.submit();
	}

}


function sendpass()
{
	document.formpass.submit();
}

function feedupdate()
{
	document.feedinfo.submit();
}


function Delete_price(m)
{
	if (confirm("Are you sure you want to delete this apartement price?")) {
		if (confirm("Careful, this price information will be deleted!")) { 
			var input = document.createElement("input");
			input.setAttribute("type", "hidden");
			input.setAttribute("name", "update");
			input.setAttribute("value", "deleteprice");
			document.getElementById("price").appendChild(input);
			alert("Successfully deleted");
			document.price.submit();
		}
	}
}

function Edit_price()
{
	length1 = document.price.length1.value;
	type_name = document.price.type_name.value;
	rate = document.price.rate.value;
	startdate = document.price.startdate.value;
	enddate = document.price.enddate.value;
	
	var why = '';

	if (length1 == "")
	{
  		 why += "Please, enter a length!\r\n";
	}

	if (rate == "")
	{
  		 why += "Please, enter a rate!\r\n";
	}
	
	if (startdate == "")
	{
		why += "Please, enter a start date!\r\n";
	}
	
	if (enddate == "")
	{
		why += "Please, enter an end date!\r\n";
	}
	
	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
		var input = document.createElement("input");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "update");
		input.setAttribute("value", "editprice");
		document.getElementById("price").appendChild(input);
		document.price.submit();
	}

}

function Add_price(){
	
	length1 = document.priceadd.length1.value;
	type_name = document.priceadd.type_name.value;
	rate = document.priceadd.rate.value;
	startdate = document.priceadd.startdate.value;
	enddate = document.priceadd.enddate.value;
	
	var why = '';
	
	if (length1 == "")
	{
  		 why += "Please, enter a length!\r\n";
	}
	
	if (rate == "")
	{
  		 why += "Please, enter a rate!\r\n";
	}
	
	if (startdate == "")
	{
		why += "Please, enter a start date!\r\n";
	}
	
	if (enddate == "")
	{
		why += "Please, enter an end date!\r\n";
	}
	
	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
		document.priceadd.submit();
	}
}

function Add_available(){
	
	client = document.available.client.value;
	startdate = document.available.startdate.value;
	enddate = document.available.enddate.value;
	
	var why = '';
	
	if (startdate == "")
	{
		why += "Please, enter a start date!\r\n";
	}
	
	if (enddate == "")
	{
		why += "Please, enter an end date!\r\n";
	}
	
	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
		document.available.submit();
	}
}

var xmlHttp8

function apt_add_dropdown(str)
{ 
xmlHttp8=GetXmlHttpObject()
if (xmlHttp8==null)
{
 alert ("Browser does not support HTTP Request")
 return
}
var url="apt_add1.php"
url=url+"?id_borough="+str
url=url+"&sid="+Math.random()
xmlHttp8.onreadystatechange=stateChanged8 
xmlHttp8.open("GET",url,true)
xmlHttp8.send(null)
}

function stateChanged8() 
{ 
if (xmlHttp8.readyState==4 || xmlHttp8.readyState=="complete")
 { 
 document.getElementById("dropdown_apt_add").innerHTML=xmlHttp8.responseText 
 }  
} 

var xmlHttp9

function apt_add_dropdown2(str)
{ 
xmlHttp9=GetXmlHttpObject()
if (xmlHttp9==null)
{
 alert ("Browser does not support HTTP Request")
 return
}
var url="apt_add2.php"
url=url+"?id_zone="+str
url=url+"&sid="+Math.random()
xmlHttp9.onreadystatechange=stateChanged9 
xmlHttp9.open("GET",url,true)
xmlHttp9.send(null)
}

function stateChanged9() 
{ 
if (xmlHttp9.readyState==4 || xmlHttp9.readyState=="complete")
 { 
 document.getElementById("dropdown_apt_add2").innerHTML=xmlHttp9.responseText 
 }  
} 

var xmlHttp7

function apt_dropdown(str)
{ 
xmlHttp7=GetXmlHttpObject()
if (xmlHttp7==null)
{
 alert ("Browser does not support HTTP Request")
 return
}
var url="apt_add_drop.php"
url=url+"?id_building="+str
url=url+"&sid="+Math.random()
xmlHttp7.onreadystatechange=stateChanged7 
xmlHttp7.open("GET",url,true)
xmlHttp7.send(null)
}

function stateChanged7() 
{ 
if (xmlHttp7.readyState==4 || xmlHttp7.readyState=="complete")
 { 
 document.getElementById("dropdown_apt_add").innerHTML=xmlHttp7.responseText 
 }  
} 


var xmlHttp

function searchapt(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="apt_get.php"
url=url+"?id_zone="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 }  
} 

function stateChanged3() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("List_owner").innerHTML=xmlHttp.responseText 
 }  
}

function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
   var obj = document.layers ? document.layers[szDivID] :
   document.getElementById ?  document.getElementById(szDivID).style :
   document.all[szDivID].style;
   obj.visibility = document.layers ? (iState ? "show" : "hide") :
   (iState ? "visible" : "hidden");
}

function formstep1(permission)
{  
 if (permission  == 0){
	alert ("You are not allowed to create an entry.");	
 }
 else{

	borough = document.apt_create.borough.value;
	zone = document.apt_create.zone.value;
	building = document.apt_create.building.value;
	owner = document.apt_create.owner.value;
	apt_number = document.apt_create.apt_number.value;
	user = document.apt_create.user.value;
	nb_room = document.apt_create.nb_room.value;
	baseprice = document.apt_create.baseprice.value;
	
	tempo_check = 0;
	criteria ='';
	for (i=0;i<document.apt_create.display.length;i++) {
		if (document.apt_create.display[i].checked) {
			criteria = document.apt_create.display[i].value;
			tempo_check ++;
		}
	}
	if (tempo_check == 0) {
		criteria ='';
	}

	var why = '';
	
	if (criteria == 0) {
		why += "Please, choose if you want to display or not!\r\n";
	}
		
	if (borough == 0)
	{
		why += "Please, choose a borough!\r\n";
	}
	
	if (zone == 0)
	{
		why += "Please, choose a zone!\r\n";
	}
	
	if (building == 0)
	{
		why += "Please, choose a building!\r\n";
	}

	if (owner == 0)
	{
		why += "Please, choose an owner!\r\n";
	}
	
	if (nb_room == "")
	{
		why += "Please, how many rooms there are!\r\n";
	}
	
	if (apt_number == "")
	{
		why += "Please, enter an apartment number!\r\n";
	}
	
	if (user == 0)
	{
  		 why += "Please, choose a user!\r\n";
	}
	
	if (isNaN(apt_number))
	{
  		 why += "Apartment number must be a number!\r\n";
	}
	
	if (isNaN(nb_room))
	{
  		 why += "Number of room must be a number!\r\n";
	}
	
	if (baseprice == 0) {
		why += "Please, enter a base price!\r\n";
	}
	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
		var input = document.createElement("input");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "check");
		input.setAttribute("value", "add");
		document.getElementById("apt_create").appendChild(input);
		alert("Apartment created");
		document.apt_create.submit();
	}
 }
	
}



var xmlHttp2

function step2(str)
{ 
xmlHttp2=GetXmlHttpObject()
if (xmlHttp2==null)
{
 alert ("Browser does not support HTTP Request")
 return
}
var url="apt_update.php"
url=url+"?borough="+str
url=url+"&sid="+Math.random()
xmlHttp2.onreadystatechange=stateChanged2 
xmlHttp2.open("GET",url,true)
xmlHttp2.send(null)
}

function stateChanged2() 
{ 
if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
 { 
 document.getElementById("step2").innerHTML=xmlHttp2.responseText 
 }  
} 

function Delete(){
	
 if (confirm("Are you sure you want to delete the apartement information?")) {
	if (confirm("Careful, the entire apartement information will be deleted!")) { 
		var input = document.createElement("input");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", "update");
		input.setAttribute("value", "delete");
		alert("Successfully Deleted");
		document.getElementById("apt_update").appendChild(input);
		document.apt_update.submit();
	}
 }
	
}


function search3()
{  
	tempo_check = 0;
	for (i=0;i<document.apt_search.apt_choice.length;i++) {
		if (document.apt_search.apt_choice[i].checked) {
		criteria = document.apt_search.apt_choice[i].value;
		tempo_check ++;
		}
	}
	if (tempo_check == 0) {
		criteria ='';
	}
	boroughid = document.apt_search.boroughid.value;
	buildid = document.apt_search.buildid.value;
	buildadd = document.apt_search.buildadd.value;
	aptid = document.apt_search.aptid.value;
	aptadd = document.apt_search.aptadd.value;
	ownerid = document.apt_search.ownerid.value;
	ownerdate = document.apt_search.ownerdate.value;
	clientfirstname = document.apt_search.clientfirstname.value;
	clientlastname = document.apt_search.clientlastname.value;
	clientemail = document.apt_search.clientemail.value;
	zoneid = '';
	
	tempo1 = 0;
	tempo2 = 0;
	tempo3 = 0;
	tempo4 = 0;
	tempo5 = 0;
	
	var why = '';
	if (criteria == ''){
		why += "Choose a criteria!\r\n";
	}
	
	if (criteria == 'zone'){
		if (boroughid == '0') {
			why += "Choose a borough!\r\n";
		}
		else{
			zoneid = document.apt_search.zoneid.value;
			if ( (zoneid == 'No value') && (tempo1 == 0)){
				tempo1 = 1;
				why += "Choose a zone!\r\n";
			}
			else {
			}
		}

	}
	
	if (criteria == 'building'){
		if ( (buildid == '') && (buildadd == '') && (tempo2 == 0)){
			tempo2 = 1;
			why += "Enter a building ID and/or an address!\r\n";
		}
	}
	
	if (criteria == 'apartment'){
		if ( (aptid == '') && (aptadd == '') && (tempo3 == 0)){
			tempo3 = 1;
			why += "Enter an apartment code and/or an appartment address!\r\n";
		}
	}
	
	if (criteria == 'owner'){
		if ( (ownerid == '') && (ownerdate == '')  && (tempo4 == 0)){
			tempo4 = 1;
			why += "Enter a owner ID and/or a owner date!\r\n";
		}
		else{	
			/*if (ownerdate != ''){
			  retour = Validatedate('owner');
			  if (retour == false ){
				  why += "The date format should be : mm-dd-yyyy \r\n";
			  }
			}*/
		}
	}
	
	if (criteria == 'client'){
		if ( (clientfirstname == '') && (clientlastname == '') && (clientemail == '') && (tempo5 == 0)){
			tempo5 = 1;
			why += "Enter a client first name and/or last name or an email address!\r\n";
		}
	}
		
	if (why != "") 
	{
   		alert(why); 	   
	} 
	else {
		step3(aptid+'&boroughid='+boroughid+'&buildid='+buildid+'&aptid='+aptid+'&aptadd='+aptadd+'&ownerid='+ownerid+'&ownerdate='+ownerdate+'&clientfirstname='+clientfirstname+'&clientlastname='+clientlastname+'&clientemail='+clientemail+'&criteria='+criteria+'&zoneid='+zoneid+'&buildadd='+buildadd);
	}
}

var xmlHttp33

function step3(str)
{ 
xmlHttp33=GetXmlHttpObject()
if (xmlHttp33==null)
{
 alert ("Browser does not support HTTP Request")
 return
}
var url="apt_filter.php"
url=url+"?aptid="+str
url=url+"&sid="+Math.random()
xmlHttp33.onreadystatechange=stateChanged33 
xmlHttp33.open("GET",url,true)
xmlHttp33.send(null)
}

function stateChanged33() 
{ 
if (xmlHttp33.readyState==4 || xmlHttp33.readyState=="complete")
 { 
 document.getElementById("search3").innerHTML=xmlHttp33.responseText 
 }  
}

var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm-dd-yyyy");
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function Validatedate(type){
	if (type == "owner"){
		var dt=document.apt_search.ownerdate
		if (isDate(dt.value)==false){
			dt.focus()
			return false
		}
    	return true		
	}
	else {
		return true
	}
}


