/* -*- mode: java -*- */
/*****************************************
 * Copyright (c) 2008, Emmett M. Pate, Jr.
 * emmett@epate.com
 *
 */


function validateLogin() {
 if (document.login.email.value=='') {
  alert('Please Enter your email address.');
  return false;
 }
 if (document.login.password.value=='') {
  alert('Please Enter your password.');
  return false;
 }
}

function setupForm() {
 var searchSettings = getCookie("reinftp-ps");
 fillForm(searchSettings);
 setSchools(document.ps.city);
 setPrices(document.ps.pt);
 // these have to be done *after* the property type and prices are set
 getValue(searchSettings, "min", document.ps.min, "select");
 getValue(searchSettings, "max", document.ps.max, "select");
}

function getCookie(name) {
 var start = document.cookie.indexOf(name+"=");
 var len = start+name.length+1;
 if ((!start) && (name != document.cookie.substring(0,name.length))) return '';
 if (start == -1) return '';
 var end = document.cookie.indexOf(";",len);
 if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
 }

function fillForm(str) {
 getValue(str, "city", document.ps.city, "select");
 getValue(str, "hs", document.ps.hs, "select");
 getValue(str, "pt", document.ps.pt, "select");
 getValue(str, "nbh", document.ps.nbh, "text");
 getValue(str, "zip", document.ps.zip, "text");
 getValue(str, "br", document.ps.br, "select");
 getValue(str, "ba", document.ps.ba, "select");
 getValue(str, "sqft", document.ps.sqft, "select");
 getValue(str, "acre", document.ps.acre, "select");
 getValue(str, "remarks", document.ps.remarks, "text");
 getValue(str, "mls", document.ps.mls, "text");
}

function getValue(string,elementName,object,elementType) {
    var startPos = string.indexOf(elementName + "&");
    if (startPos > -1) {
	startPos = startPos + elementName.length + 1;
	var endPos = string.indexOf("&",startPos);
	if (endPos == -1) endPos = string.length;
	
	var elementValue = unescape(string.substring(startPos,endPos));

	if (elementType == "text")     object.value = elementValue;
	if (elementType == "password") object.value = elementValue;
	if (elementType == "select") {
	    for (var i=0; i<object.length; i++) {
		if (object[i].value == elementValue) object.selectedIndex = i;
	    }
	}
	if (elementType == "checkbox") object.checked = onCheck(elementValue);
	if (elementType == "radio")    object[elementValue].checked = true;
    }
}

function getCookieValue(string,elementName) {
 var startPos = string.indexOf(elementName + "&")
 if (startPos > -1) {
  startPos = startPos + elementName.length + 1;
  var endPos = string.indexOf("&",startPos);
  if (endPos == -1) endPos = string.length;
  return unescape(string.substring(startPos,endPos));
 }
}

function setSchools(t) {
 //alert(t.value);
 //alert(document.ps.city.value);

 // enable all schools if City='Any' or '%'
 var hs = document.ps.hs;

 hs.options.length = 0;
 hs.options[ hs.options.length ] = new Option('Any', '%');
 for (i=0; i<schools.length-1; i++)
 {
  if (t.value == schools[i][0] || t.value == "%")
  {
   hs.options[ hs.options.length ] = new Option(schools[i][2], schools[i][1]);
  }
 }
}

function setPrices(t) {
    // alert(t.value);

    var min = document.ps.min;
    var max = document.ps.max;
    
    min.options.length = max.options.length = 0;
    
    if (t.value == "RNT")
	{
	    mins = new Array(0,500,750,1000,1250,1500);
	    maxs = new Array(750,1000,1250,1500);
	    document.ps.sold.disabled = true;
	}
    else
	{
	    mins = new Array(0,25000,50000,75000,100000,125000,150000,175000,200000,225000,250000,275000,300000,325000,350000,375000,400000,450000,500000,600000,700000,800000,900000,1000000);
	    maxs = new Array(25000,50000,75000,100000,125000,150000,175000,200000,225000,250000,275000,300000,325000,350000,375000,400000,450000,500000,600000,700000,800000,900000,1000000,1200000);
	    document.ps.sold.disabled = false;
	}
    
    for (v in mins)
	{
	    min.options[ min.options.length ] = new Option(mins[v], mins[v]);
	}
    for (v in maxs)
	{
	    max.options[ max.options.length ] = new Option(maxs[v], maxs[v]);
	}
    max.options[ max.options.length ] = new Option("No Limit...", 99999999);
    max.options[ max.options.length - 1 ].selected = true;
}
