//ajax call invoking
	function ajaxRequest(url, handler)
	{
		xmlHttp = GetXmlHttpObject(handler);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
//ajax call initialisation
	function GetXmlHttpObject(handler)
	{
		var objXmlHttp = null;
		try {
			objXmlHttp = new XMLHttpRequest();
			objXmlHttp.onreadystatechange = handler;
			return objXmlHttp;
		} catch(e) {
			try {
				objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				objXmlHttp.onreadystatechange = handler; 
				return objXmlHttp;
			} catch (e) { 
				try {
					objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					objXmlHttp.onreadystatechange = handler; 
					return objXmlHttp;
				} catch (e) {
					return false;
				}
			}
		}
		return false;
	}

// ajax call end

// registration page ajax start
	function sendRegvalues()
	{
		if(registrationVal())
		{
		   var vow='';
		   for (var i=0; i < document.forms.registrationFrm.notify.length; i++)
		   {
			   if (document.forms.registrationFrm.notify[i].checked)
			   {
			     var rad_val = document.forms.registrationFrm.notify[i].value;
			   }
		   }
		 if(document.forms.registrationFrm.vowsupport!=undefined){
			 if(document.forms.registrationFrm.vowsupport.checked){
				 vow = document.forms.registrationFrm.vowsupport.value;
			 }
		 }
		url = "ajax.php?reg=1&firstName=" + document.forms.registrationFrm.firstName.value + "&lastName=" + document.forms.registrationFrm.lastName.value + "&address=" + document.forms.registrationFrm.address.value + "&city=" + document.forms.registrationFrm.city.value + "&state=" + document.forms.registrationFrm.state.value + "&zipcode=" + document.forms.registrationFrm.zipcode.value + "&email=" + document.forms.registrationFrm.email.value + "&passwd=" + document.forms.registrationFrm.passwd.value+"&phoneno="+document.forms.registrationFrm.phonenum.value+"&notify="+rad_val+"&vow="+vow;
			ajaxRequest(url, insertContentHandler);
		}
	}
	
	function insertContentHandler()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			if(res == '1')
				document.getElementById('errid').style.display = "block";
			else if(res == '2')
			{
				document.getElementById('RegistrationOpen').style.display = "none";
				document.getElementById('RegistrationConfirmation').style.display = "block";
			}
			else if(res == '3')
			{
				document.getElementById('registerLayer').style.visibility = "hidden";
				document.getElementById('upgradediv').style.visibility = "visible";
			}
			return false
		}
            return false;
	}
// registration ajax end

// login ajax start

	function checkemailaddress()
	{
		if(loginvalidation())
		{
			if(document.forms.regform.setcookie.checked)
				 setcookie	=	1
			else
				 setcookie	=	0
			
			var pw = document.forms.regform.password.value;
			pw = hex_md5(pw);
			url = "ajax.php?login=1&email=" + document.forms.regform.regemail.value + "&password=" +pw+ "&setcookie="+setcookie;
			ajaxRequest(url, loginalert);
			
		}
	}
	function loginalert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText;	
			if(res==0 || res == "")				
				document.getElementById('erroid').style.display="block"
			else
			{
				if(res==2)
					location.href="Admin/dashboard.php";
				else
					location.reload(true);
			}
		}
	}
// login ajax ends

// forgot password start

	function checkforgetvalidation()
	{
		if(forgetvalidation())
		{
			url = "ajax.php?forgetemail=" + document.forms.forgetform.forgetemail.value;
			ajaxRequest(url, forgetalert);
		}
	}
	function forgetalert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			var val	=	res.trim();	
			if(val=='0')
			{
				document.getElementById('errodisp').style.display="block"
				document.getElementById('sucssid').style.display="none"
			}
			else
			{
				document.getElementById('sucssid').style.display="block"
				document.getElementById('errodisp').style.display="none"
			}
		}
	}
	
// forgot password end

// address ajax start

	function noaddress()
	{
		url	= "ajax.php?noaddress=1";
		ajaxRequest(url, addressalert);
	}
	
	function sendAddressvalues()
	{
		if(addressVal())
		{
			url	= "ajax.php?addaddr=1&address=" + document.forms.addressfrm.address.value + "&city=" + document.forms.addressfrm.city.value + "&state=" + document.forms.addressfrm.state.value + "&zipcode=" + document.forms.addressfrm.zipcode.value;
			ajaxRequest(url, addressalert);
		}
	}
	
	function addressalert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{
				res = xmlHttp.responseText;	
				if(res == '1'){
					document.getElementById('addressLayer').style.display = "none";
				}
			}
	}
// address ajax end

// saved search start
	function handelsearchdata()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			var res = xmlHttp.responseText;	
		    document.getElementById('details').innerHTML= res;
			document.getElementById('details').style.display="block";
			
		}
	}
		
	function searchdata(linkid,searchId,count)
	{ 
		
		for(i=1;i<=count;i++)
		{
			var linki = 'sname'+i;
			document.getElementById(linki).className="";
		
		}
		var linkidv = 'sname'+linkid;
		document.getElementById(linkidv).className="selectlink";
		url = "ajax.php?searchId="+searchId;
		ajaxRequest(url, handelsearchdata);
	}
// saved search end

// to save search start
	function handelsavesearch()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			var res = xmlHttp.responseText.trim();
			if(res=='1')	
			{
				document.getElementById('savedmsg').style.display="block";
			}
		}
	}
	function savesearch()
	{
		if(document.forms.searchform.searchname.value!='' && document.forms.searchform.searchname.value!='Enter Search Name')
		{
			url = "ajax.php?sname="+document.forms.searchform.searchname.value;
			ajaxRequest(url, handelsavesearch);
		}
		else
		{
			alert('Enter search name')
			document.forms.searchform.searchname.focus();
		}
	}
// save search end

// add to favourites start
	function favoritecatalert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			document.getElementById('favreloadimage'+res).style.display="none";
			document.getElementById('afterfavmainpart'+res).style.display="block";
		}
	}
		
	function addfavourite(listingid,searchtype,favid1)
	{ 
		document.getElementById('favmainpart'+favid1).style.display="none";
		document.getElementById('favreloadimage'+favid1).style.display="block";
		url = "ajax.php?favid="+listingid+"&type="+searchtype+"&id="+favid1;
		ajaxRequest(url, favoritecatalert);
		
	}
// add to favourites end

// add to trip start
	var divvalue;
	function handeladdtrip()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			if(res == 1 || res == 0)
			{
	    		var addtripdiv = 'tripadded'+divvalue
				var tripload = 'triploadimage'+divvalue;
				document.getElementById(tripload).style.display="none";
				document.getElementById(addtripdiv).style.display="block";
			}
		}
	}
		
	function addtotrip(listId,listType,divid)
	{ 	
		divvalue = divid;
		var addtripdiv = 'addtrip'+divid;
		var tripload = 'triploadimage'+divid;
		document.getElementById(addtripdiv).style.display="none";
		document.getElementById(tripload).style.display="block";
		url = "ajax.php?listid="+listId+"&type="+listType;
		ajaxRequest(url, handeladdtrip);
	}
// add to trip end

// share start
	function sendSharevalues()
	{
		if(ShareVal())
		{
			linkString = document.forms.shareFrm.link.value
			replacedString = linkString.replace(/&/g, "$")
			url = "ajax.php?friendEmail="+document.forms.shareFrm.friendEmail.value+"&name="+document.forms.shareFrm.nam.value+"&myEmail="+ document.forms.shareFrm.myEmail.value+"&link="+replacedString+"&message="+document.forms.shareFrm.message.value;
			ajaxRequest(url, shareAlert);
		}
	}
	function shareAlert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			if(res == 1)
			{
				document.getElementById('ShareErrid').style.display = "block";
			}
			else
			{
				document.getElementById('ShareSuccessId').style.display = "block";
				document.getElementById('ShareButtonId').style.display  = "none";
			}
			return false
		}
            return false
	}
// share end

// search map start
	function searchmap()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			 //ne = -1;
			 dr=0;
			searchLocationsNear();
		}
	}

	function searchmapsessionset(val)
	{ 
		var searchgetstring;
		var formval;
		searchgetstring ="";
		formval         = document.forms.searchform;
		
		
		// search type
		if(formval.searchtype.value!='')
		{
			searchgetstring+="?searchtype="+escape(formval.searchtype.value);
		}
		
		//open house
		if(val=='1')
		{
			searchgetstring+="&open_house=1";
		}
			
		//listing number
		if(formval.ListingNumber.value!='')
		{
			searchgetstring+="&ListingNumber="+escape(formval.ListingNumber.value);
		}
		//listing price start
		if(formval.listing_pricemin.value!='' && formval.listing_pricemin.value!='No min')
		{
			searchgetstring+="&listing_pricemin="+escape(formval.listing_pricemin.value);
		}
		
		if(formval.listing_pricemax.value!='' && formval.listing_pricemax.value!='No Max')
		{
			searchgetstring+="&listing_pricemax="+escape(formval.listing_pricemax.value);
		}
		//listing price end	
		
		//location start
		if(formval.searchvalue.value!='Address, City, State or Zip')
		{
			searchgetstring+="&searchvalue="+escape(formval.searchvalue.value);
		}
		
		//location end
		
		//bedroom start
		if(formval.bedroom.value!='' && formval.bedroom.value!='No min')
		{
			searchgetstring+="&bedroom="+escape(formval.bedroom.value);
		}
		
		if(formval.bedroomMax.value!='' && formval.bedroomMax.value!='No max')
		{
			searchgetstring+="&bedroomMax="+escape(formval.bedroomMax.value);
		}
		//bedroom end
		
		//bathroom start
		if(formval.bath.value!='' && formval.bath.value!='No min')
		{
			searchgetstring+="&bath="+escape(formval.bath.value);
		}
		
		if(formval.bedroomMax.value!='' && formval.bedroomMax.value!='No max')
		{
			searchgetstring+="&bedroomMax="+escape(formval.bedroomMax.value);
		}
		//bathroom end
		
		//square feet start
		if(formval.squarefeet.value!='' && formval.squarefeet.value!='No min')
		{
			searchgetstring+="&squarefeet="+escape(formval.squarefeet.value);
		}
		
		if(formval.sqfeetMax.value!='' && formval.sqfeetMax.value!='No max')
		{
			searchgetstring+="&sqfeetMax="+escape(formval.sqfeetMax.value);
		}
		//square feet end
		
		//year start
		if(formval.ystart.value!='' && formval.ystart.value!='No min')
		{
			searchgetstring+="&ystart="+escape(formval.ystart.value);
		}
		
		if(formval.yend.value!='' && formval.yend.value!='No max')
		{
			searchgetstring+="&yend="+escape(formval.yend.value);
		}
		//year end
			
		//garage start
		if(formval.garage.value!='' && formval.garage.value!='No min')
		{
			searchgetstring+="&garage="+escape(formval.garage.value);
		}
		
		//garage end
		
		//acres
		if(formval.acres.value!='' && formval.acres.value!='No min')
		{
			searchgetstring+="&acres="+escape(formval.acres.value);
		}
		
		if(formval.acresmax.value!='' && formval.acresmax.value!='No max')
		{
			searchgetstring+="&acresmax="+escape(formval.acresmax.value);
		}
		
		//property type
		if(formval.property_type.value!='' && formval.property_type.value!='-99')
		{
			searchgetstring+="&property_type="+escape(formval.property_type.value);
		}
		//farm type
		if(formval.farm_type.value!='' && formval.farm_type.value!='-99')
		{
			searchgetstring+="&farm_type="+escape(formval.farm_type.value);
		}
		//business type
		if(formval.business_type.value!='' && formval.business_type.value!='-99')
		{
			searchgetstring+="&business_type="+escape(formval.business_type.value);
		}
		//no of units
		if(formval.nunits.value!='' && formval.nunits.value!='-99')
		{
			searchgetstring+="&nunits="+escape(formval.nunits.value);
		}
		
		//Elementary School
		if(formval.eschool.value!='')
		{
			var ElementarySchoolconcatenate="";
			nechk = formval.eschool;
			totallength = formval.eschool.length;
			countElementarySchool=0;
			for(i=0;i<totallength;i++)
			{
				
				if(nechk[i].value!='' &&(nechk[i].checked==true))	
				{
					ElementarySchoolconcatenate +="&ElemSchool"+i+"="+escape(nechk[i].value);
					countElementarySchool++;
				}
			}
			if(countElementarySchool!=0)
			ElementarySchoolconcatenate +="&countElementarySchool="+countElementarySchool;
			searchgetstring+=ElementarySchoolconcatenate;
		}
		
		//Middle School
		if(formval.mschool.value!='')
		{
			var MiddleSchoolconcatenate="";
			nechk = formval.mschool;
			totalMiddleSchoollength = formval.mschool.length;
			countMiddleSchool=0;
			for(i=0;i<totalMiddleSchoollength;i++)
			{
				if(nechk[i].value!='' &&(nechk[i].checked==true))	
				{
					MiddleSchoolconcatenate +="&MdlSchool"+i+"="+escape(nechk[i].value);
					countMiddleSchool++;
				}
			}
			if(countMiddleSchool!=0)
			MiddleSchoolconcatenate +="&countMiddleSchool="+countMiddleSchool;
			searchgetstring+=MiddleSchoolconcatenate;
		}
		
		//High School
		if(formval.hschool.value!='')
		{
			var HighSchoolconcatenate="";
			nechk = formval.hschool;
			totalHighSchoollength = formval.hschool.length;
			countHighSchool=0;
			for(i=0;i<totalHighSchoollength;i++)
			{
				if(nechk[i].value!='' &&(nechk[i].checked==true))	
				{
					HighSchoolconcatenate +="&SrHighSchool"+i+"="+escape(nechk[i].value);
					countHighSchool++;
				}
			}
			if(countHighSchool!=0)
			HighSchoolconcatenate +="&countHighSchool="+countHighSchool;
			searchgetstring+=HighSchoolconcatenate;
		}
		
		//home type
		if(formval.HouseStyle.value!='')
		{
			var HomeTypeconcatenate="";
			nechk = formval.HouseStyle;
			totalHomeTypelength = formval.HouseStyle.length;
			countHousestyle =0;
			for(i=0;i<totalHomeTypelength;i++)
			{
				if(nechk[i].value!='' &&(nechk[i].checked==true))	
				{
					HomeTypeconcatenate +="&HouseStyle"+i+"="+escape(nechk[i].value);
					countHousestyle++;
				}
			}
			if(countHousestyle!=0)
			HomeTypeconcatenate +="&countHousestyle="+countHousestyle;
			searchgetstring+=HomeTypeconcatenate;
		}
		url = "ajax_searchmap.php"+searchgetstring;
		ajaxRequest(url, searchmap);
	}
// search map  end

//Request tour start
function requestTouralert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			if(res == 2)
			{
				
				document.getElementById('requestTourSuccessId').style.display = "block";
				document.getElementById('requestTourButtonId').style.display  = "none";
				document.getElementById('requestTourErrorId').style.display   = "none";
			}
			else
			{
				document.getElementById('requestTourErrorId').style.display   = "block";
				document.getElementById('requestTourSuccessId').style.display = "none";
			}
			return false
		}
            return false
	}
function sendRequestTour()
	{
		if(requestTourVal())
		{
	url="ajax.php?requestTourName="+document.forms.requestTourFrm.nam.value+"&requestTourEmail="+document.forms.requestTourFrm.myEmail.value+"&lid="+document.forms.requestTourFrm.listing.value+"&requestTourPropertyType="+document.forms.requestTourFrm.propertytype.value+"&code="+document.forms.requestTourFrm.reg_code.value+"&requestMessage="+document.forms.requestTourFrm.message.value;
		ajaxRequest(url, requestTouralert);
		}
	}



	function requestTouralertWithlogin()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			if(res == 2)
			{
				document.getElementById('requestTourLayer').style.visibility ="visible";
			}
			return false
		}
            return false
	}
	
	function sendRequestTourWithlogin()
	{
	url="ajax.php?&lid="+document.forms.requestTourFrm.listing.value+"&requestTourPropertyType="+document.forms.requestTourFrm.propertytype.value+"&requestLogin=1";
		ajaxRequest(url, requestTouralertWithlogin);
	}
//Request tour end

// Comparison for house js start
   function CompareHouseEachOtherAlert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			window.location.href = 'compareHouse.php';
			return false
		}
            return false
	}
	
	function CompareHouseEachOther()
	{
		var CheckFlag          = "false";
		var CompareField       = document.forms.CompareHouseForm.CompareHouse;
		var CompareHouseLength = document.forms.CompareHouseForm.CompareHouse.length;
		var Inc                = 0;
		var CompareHouseString ='';
		var TotalCompare       = 0;		
		for(i=0;i<CompareHouseLength;i++)
		{
			if((CompareField[i].checked==true))	
			{
				 CheckFlag = "true";
				 Inc++;
			}
		}
		
		if(CheckFlag == "false")
		{	
			alert('Please select property');
			return false;
		}
		
		if(Inc < 2)
		{
			 alert('Please select atleast two property');
			 return false;
		}	
		if(Inc >4)
		{
			 alert('Dont select more than 4 for Compare House Feature');
			 return false;
		}
		
		for(j=0;j<CompareHouseLength;j++)
		{
			if(CompareField[j].value!='' && (CompareField[j].checked==true))	
			{
				CompareHouseString +="&CompareHouse"+TotalCompare+"="+escape(CompareField[j].value);
				TotalCompare++;
			}
		}
		url="ajax.php?TotalCompare="+TotalCompare+CompareHouseString;
		ajaxRequest(url, CompareHouseEachOtherAlert);
                return null
   }
// Comparison for house js end


//Mortgage-Calculator js start
   function Mortgagecalculatoralert()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			res = xmlHttp.responseText.trim();
			if(res=='1')
			{		
				document.getElementById('mortgageerror').style.display = "block";
			
			}
			else
			{		
				document.getElementById('mortgageerror').style.display = "none";		
				document.getElementById('amountmortage').innerHTML = res;
			}
			return false
		}
            return false
	}
	
	function Mortgagecalculator()
	{
		var formString =  document.forms.information;;
	url="ajax.php?sale_price="+escape(formString.sale_price.value)+"&annual_interest_percent="+escape(formString.annual_interest_percent.value)+"&year_term="+escape(formString.year_term.value)+"&down_percent="+escape(formString.down_percent.value);
	 ajaxRequest(url, Mortgagecalculatoralert);
   }
// Mortgage-Calculator js end
// send sms
function sendSmsvalues()
{
	if(SmsVal())
	{
	url="ajax.php?smsphoneno="+escape(document.forms.smsFrm.phonenum.value)+"&listingid="+document.forms.smsFrm.listingid.value+"&listtype="+document.forms.smsFrm.listtype.value+"&fromNumber="+escape(document.forms.smsFrm.fromNumber.value)+"&FromName="+escape(document.forms.smsFrm.FromName.value)+"&clients="+escape(document.forms.smsFrm.clients.value);
	  	ajaxRequest(url, sendSmsalert);
	}
}
function sendSmsalert()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		res = xmlHttp.responseText.trim();
		if(res == 1)
		{
			document.getElementById('SmsSuccessId').style.display = "block";
			document.getElementById('SmsButtonId').style.display = "none";
			
		}
		return false
	}
    return false;
}
	
// send sms end 

// Agent sms start
function sendAgentSms()
{
	if(AgentSms())
	{
	
	url="ajax.php?agentSms="+escape(document.forms.agentSmsFrm.phonenumber.value)+"&message="+escape(document.forms.agentSmsFrm.smsmessage.value)+"&code="+escape(document.forms.agentSmsFrm.reg_code.value)+"&userName="+escape(document.forms.agentSmsFrm.userName.value)+"&userEmail="+escape(document.forms.agentSmsFrm.userEmail.value);
  	ajaxRequest(url, sendagentSmsalert);
	}
}
function sendagentSmsalert()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		res = xmlHttp.responseText.trim();
		if(res == 1)
		{
			document.getElementById('agentSmsSuccessId').style.display = "block";
			document.getElementById('agentSmsButtonId').style.display  = "none";
		}
		else 
		{
			document.getElementById('agentSmsErrorId').style.display = "block";
		}
		return false
	}
    return false
}
// Agent sms end


// Agent sms start
function sendagentsmsvalues()
{
	if(adminphoneVal())
	{
		url="ajax.php?agentmobile="+escape(document.forms.agentphoneFrm.phoneno.value);
  		ajaxRequest(url, sendagentsmsvaluesalert);
	}
}
function sendagentsmsvaluesalert()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		res = xmlHttp.responseText.trim();
		if(res == 1)
		{
			location.href="agentphone.php";
		}
		return false
	}
    return false
}
// Agent sms end

// check trip #

function checkTrip()
{ 	
	url = "ajax.php?numFavs=1";
	ajaxRequest(url, handlecheckTrip);
}

function handlecheckTrip()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		res = xmlHttp.responseText.trim();
		if(res == 1)
		{
			window.location='openhouse.php'; 
		}
		else if(res ==0){
			alert('You must add at least two houses to your trip!');	
		}
	}
}

function upgradeAccount()
{
	url = "ajax.php?upgradenow=1";
	ajaxRequest(url, handleupgradeAccount);
}

function handleupgradeAccount()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		res = xmlHttp.responseText.trim();
		if(res == 1)
		{
			document.getElementById('messagecontent').style.display = "block";
			document.getElementById('maincontent').style.display = "none";
		}
		
	}

}
function upgradeAccountreg()
{
	url = "ajax.php?registervow=1&email=" + document.forms.registrationFrm.email.value;
	ajaxRequest(url, insertvowHandler);
}
function insertvowHandler()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		res = xmlHttp.responseText.trim();
		if(res == '2')
		{
			document.getElementById('upgradediv').style.visibility = "hidden";
			document.getElementById('registerLayer').style.visibility = "visible";
			document.getElementById('RegistrationOpen').style.display = "none";
			document.getElementById('RegistrationConfirmation').style.display = "block";
			document.getElementById('vowverification').style.display = "block";
		}
	}
}