//ajax functions

var xmlHttp

function savelisting(mlsid,listtype)
{ 
var url="account_savelisting.asp?ajax=1&mlsid=" + mlsid + "&listtype=" + listtype
xmlHttp=GetXmlHttpObject(listingSaved)
xmlHttp.open("GET", url , true)
xmlHttp.send(null)
} 

function listingSaved() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
	document.getElementById('savelistingtext').innerHTML = "<span class='red bold'>Saved in Watchlist</span>"
	document.getElementById('savelistingimage').innerHTML = "<img border=0 src='/imgs/icon_watchlist_check.gif'>"
	} 
}

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
		{
		alert("This example doesn't work in Opera") 
		return 
		}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
			{
			strName="Microsoft.XMLHTTP"
			} 
		try
			{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
			} 
			catch(e)
			{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
			} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}

// the variable photoCount comes from the BuildDetail sub in propertydetail_include.asp...
// it sets the number of images for the innerHTML

var curPhoto = 1;

function changeImage(mlsid, photonum)
{
	curPhoto = photonum
	var imgSource = "/mlsphotos/" + mlsid + "_" + photonum + '.jpg';
	document.getElementById('mainimage').src=imgSource;
	document.getElementById('phototext').innerHTML=photonum + " of " + photoCount + " Photos";
}

function nextImage(mlsid)
{
	if (curPhoto == photoCount)
		curPhoto = 1;
	else
		curPhoto += 1;
	var imgSource = "/mlsphotos/" + mlsid + "_" + curPhoto + '.jpg';
	document.getElementById('mainimage').src=imgSource;
	document.getElementById('phototext').innerHTML=curPhoto + " of " + photoCount + " Photos";
}
function prevImage(mlsid)
{
	if (curPhoto == 1)
		curPhoto = photoCount;
	else
		curPhoto -= 1;
	var imgSource = "/mlsphotos/" + mlsid + "_" + curPhoto + '.jpg';
	document.getElementById('mainimage').src=imgSource;
	document.getElementById('phototext').innerHTML=curPhoto + " of " + photoCount + " Photos";
}

function CheckRegisterData()
 {
      var checkName=document.register.fullname.value;
      var checkPhone=document.register.phone.value;
      var checkEmail=document.register.email.value;
      var checkPW=document.register.pw.value;
      var checkPWConfirm=document.register.confirmpw.value;
	  
      if (! checkName)
      {
        alert("Please enter your full name.");
        document.register.fullname.focus();
        return false;
      }

      if (! checkPhone)
      {
        alert("Please enter your phone.");
        document.register.phone.focus();
        return false;
      }
	  
	  if (! checkPW)
      {
        alert("Please enter your password.");
        document.register.pw.focus();
        return false;
      }

      if (! checkEmail)
      {
        alert("Email address is required to register.");
        document.register.email.focus();
        return false;
      }

      if (! checkPWConfirm)
      {
        alert("Please enter your password twice.");
        document.register.confirmpw.focus();
        return false;
      }

      if (checkPWConfirm != checkPW)
      {
        alert("The two passwords do not match.  Please enter your password again");
        document.register.pw.value = "";
        document.register.confirmpw.value = "";
        document.register.pw.focus();
        return false;
      }

  return true;
}

function CheckSavedSearch()
 {
	  var checkSearchName=document.savedsearch.searchname.value;
		
	  if (! checkSearchName)
	  {
		alert("Please enter a name for the saved search.");
		document.savedsearch.searchname.focus();
		return false;
	  }

  return true;
}

function CheckForgotPWData()
 {
      var checkEmail=document.frm.email.value;

      if (! checkEmail)
      {
        alert("Please enter your email address.");
        document.frm.email.focus();
        return false;
      }

  return true;
}

function CheckLoginData()
 {
      var checkEmail=document.login.email.value;
      var checkPassword=document.login.pw.value;

      if (! checkEmail)
      {
        alert("Please enter your email address to login.");
        document.login.email.focus();
        return false;
      }

      if (! checkPassword)
      {
        alert("Please enter your password to login.");
        document.login.pw.focus();
        return false;
      }
  return true;
}

function CheckSendListing()
 {
	  var checkFromName=document.sendlisting.fromname.value;
	  var checkFromEmail=document.sendlisting.fromemail.value;
	  var checkToEmail=document.sendlisting.toemail.value;

	  if (! checkFromName)
	  {
		alert("From name is required.");
		document.sendlisting.fromname.focus();
		return false;
	  }

	  if (! checkFromEmail)
	  {
		alert("From email address is required.");
		document.sendlisting.fromemail.focus();
		return false;
	  }
	  
	  if (! checkToEmail)
	  {
		alert("To email address is required.");
		document.sendlisting.toemail.focus();
		return false;
	  }	  
  return true;
}


function CheckViewingRequest()
 {
	  var checkFromName=document.viewingrequest.fromname.value;
	  var checkFromEmail=document.viewingrequest.fromemail.value;

	  if (! checkFromName)
	  {
		alert("From name is required.");
		document.viewingrequest.fromname.focus();
		return false;
	  }

	  if (! checkFromEmail)
	  {
		alert("From email address is required.");
		document.viewingrequest.fromemail.focus();
		return false;
	  }
	    
  return true;
}


function confirmDelete()
{
	msg = "Are you sure you want to delete this record?";
	if (confirm(msg))
		return true;
	return false;
}

function confirmDeleteInbox()
{
	msg = "Are you sure you want to delete this property from your property inbox?";
	if (confirm(msg))
		return true;
	return false;
}
function confirmDeleteListing()
{
	msg = "Are you sure you want to delete this property from your watch list?";
	if (confirm(msg))
		return true;
	return false;
}

function confirmDeleteSearch()
{
	msg = "Are you sure you want to delete this saved search?";
	if (confirm(msg))
		return true;
	return false;
}

function setSort(sortValue)
{
	self.location.href="searchresults.asp?sort=" + sortValue;
}

function setPerPage(perPage)
{
	self.location.href="searchresults.asp?perpage=" + perPage;
}

function setAccountSort(newpage, sortValue)
{
	self.location.href= newpage + "?sort=" + sortValue;
}

function setAccountPerPage(newpage, perPage)
{
	self.location.href= newpage + "?perpage=" + perPage;
}

function opencloseDivImage(the_div, the_image)
{
	if (document.getElementById(the_div).style.display=="" || document.getElementById(the_div).style.display=="block")
	{
	document.getElementById(the_div).style.display = "none";
	document.getElementById(the_image).src = "/imgs/arrow_closed.gif";
	return
	}
	else
	{
	document.getElementById(the_div).style.display = "block";
	document.getElementById(the_image).src = "/imgs/arrow_down.gif";
	return	
	}	
}

function showDiv(the_div)
{
	 if (document.getElementById(the_div).style.display=="none")
	{
	document.getElementById(the_div).style.display = "block";return
	}
}
function hideDiv(the_div)
{
	if (document.getElementById(the_div).style.display=="" || document.getElementById(the_div).style.display=="block")
	{
	document.getElementById(the_div).style.display = "none";return
	}
}
function opencloseDiv(the_div)
{
	if (document.getElementById(the_div).style.display=="" || document.getElementById(the_div).style.display=="block")
	{
	document.getElementById(the_div).style.display = "none";return
	}
	else
	{
	document.getElementById(the_div).style.display = "block";return
	}	
}

function setSearchCat(the_div)
{
	document.getElementById('townsearch').style.display = "none";
	document.getElementById('countysearch').style.display = "none";
	//document.getElementById('zipcodesearch').style.display = "none";
	document.getElementById('schoolsearch').style.display = "none";
	document.getElementById(the_div).style.display = "block";
	document.getElementById('searchType').value = the_div;
}

function clearForm()
{
	setSearchCat('townsearch');
	document.getElementById('town1').value = '';
	document.getElementById('town1PullDownButton').innerHTML = 'Choose Town';
	document.getElementById('town2').value = '';
	document.getElementById('town2PullDownButton').innerHTML = 'Choose Town';
	document.getElementById('town3').value = '';
	document.getElementById('town3PullDownButton').innerHTML = 'Choose Town';
	document.getElementById('town4').value = '';
	document.getElementById('town4PullDownButton').innerHTML = 'Choose Town';
	document.getElementById('town5').value = '';
	document.getElementById('town5PullDownButton').innerHTML = 'Choose Town';
	document.getElementById('div_town2').style.display = "none";
	document.getElementById('div_town3').style.display = "none";
	document.getElementById('div_town4').style.display = "none";
	document.getElementById('div_town5').style.display = "none";
	document.getElementById('county').value = '';
	document.getElementById('countyPullDownButton').innerHTML = 'Choose County';
	document.getElementById('schooldistrict').value = '';
	document.getElementById('schooldistrictPullDownButton').innerHTML = 'Select School District';
	//document.getElementById('zipcode').value = '';
	document.getElementById('minprice').value = '0';
	document.getElementById('minpricePullDownButton').innerHTML = 'No Minimum';
	document.getElementById('maxprice').value = '999999999';
	document.getElementById('maxpricePullDownButton').innerHTML = 'No Maximum';
	document.getElementById('bedrooms').value = '';
	document.getElementById('bedroomsPullDownButton').innerHTML = 'Any Number';
	document.getElementById('baths').value = '';
	document.getElementById('bathsPullDownButton').innerHTML = 'Any Number';
	document.getElementById('listtype').value = '1';
	document.getElementById('listtypePullDownButton').innerHTML = 'Single-Family';
	document.getElementById('mlsnum').value = '';
	document.getElementById('keywords').value = '';
	setRes(1);
	uncheckAll(document.search.features);
}

function uncheckAll(thisfield) 
{
	if (thisfield.length)
		for (i = 0; i < thisfield.length; i++) 
			thisfield[i].checked = false; 
	else
		thisfield.checked = false;
}

function setRes(resType)
{
	if (resType == "1" || resType == "2" || resType == "3")
	{
	document.getElementById('reslabels').style.display = "";
	document.getElementById('resvalues').style.display = "";
	}
	else
	{
	document.getElementById('reslabels').style.display = "none";
	document.getElementById('resvalues').style.display = "none";
	}
}

function setHiddenFormValue(formField, formValue)
{
	document.getElementById(formField).value = formValue;
}
function setSearchCatNEW(the_div, the_link)
{
	document.getElementById('townlink').className = '';
	document.getElementById('countylink').className = '';
	//document.getElementById('zipcodelink').className = '';
	document.getElementById('schoollink').className = '';
	document.getElementById(the_link).className = 'selected';

	document.getElementById('townsearch').style.display = "none";
	document.getElementById('countysearch').style.display = "none";
	//document.getElementById('zipcodesearch').style.display = "none";
	document.getElementById('schoolsearch').style.display = "none";
	document.getElementById(the_div).style.display = "block";
	document.getElementById('searchType').value = the_div;
}

function showDetail(tblid)
{
var objTBL = document.getElementById("tbl" + tblid);
var objIMG = document.getElementById("img" + tblid);
if (objTBL.style.display=="")
	{objTBL.style.display="none";
	objIMG.innerHTML = "<a href='javascript:showDetail(" + tblid + ");'><img src='/imgs/plus.gif' border=0></a>";}
else
	{objTBL.style.display="";
	objIMG.innerHTML = "<a href='javascript:showDetail(" + tblid + ");'><img src='/imgs/minus.gif' border=0></a>";}
}
