function populatePackages(s, c, p)
{
	var state = getObject(s).value;
  var county = getObject(c).value;
  var package_list = getObject(p);
  var datafile = "/populate/packages.php?state="+state+"&county="+county;
  
	//Clear the county box out
  for(i=package_list.options.length-1; i>-1; i--)
	  package_list.options[i] = null;
	
  package_list.options[0] = new Option("       Loading...       ", "");
  package_list.disabled = 'disabled';  
  
  if(document.all)
  {
    objXmlP = new ActiveXObject("Microsoft.XMLHTTP");
    objXmlP.open("GET", datafile, true);
    objXmlP.onreadystatechange=function()
    {
    	if (objXmlP.readyState==4)
      {
      	packages = objXmlP.responseText;
      	populatePackagesAction(packages, package_list);
  		}
    }
    objXmlP.send(null);
  }
  else
  {
  	if(document.getElementById)
    {
    	objXmlP = new XMLHttpRequest();
    	objXmlP.open("GET", datafile, false);
    	objXmlP.send(null);
      
      packages = objXml.responseText;
     	populatePackagesAction(packages, package_list);
    }
  }	
}

function populatePackagesAction(packages, package_list)
{
	package_list.disabled = false;
	// repopulate
  package_list.options[0] = new Option("--Select Package--","");
  if(packages.length > 0)
  {
	 	packages_array = packages.split("|");
   	for(i=0; i<packages_array.length; i++)
     	if(packages_array[i])
      {
      	p = packages_array[i].split("~~");
   			package_list.options[i+1] = new Option("$"+p[2]+" - "+p[1],p[0]);
      }
       
  }
}

function populateTransactionType(p, t)
{
  var p = p.value;
  var tt_list = getObject(t);
  var datafile = "/populate/package_item.php?package="+p+"&item=type";

	//Clear the county box out
  for(i=tt_list.options.length-1; i>-1; i--)
	  tt_list.options[i] = null;
	
  tt_list.options[0] = new Option("       Loading...       ", "");
  tt_list.disabled = 'disabled';  
  
  if(document.all)
  {
    objXmlTT = new ActiveXObject("Microsoft.XMLHTTP");
    objXmlTT.open("GET", datafile, true);
    objXmlTT.onreadystatechange=function()
    {
    	if (objXmlTT.readyState==4)
      {
      	tt = objXmlTT.responseText;
      	populateTransactionTypeAction(tt, tt_list);
  		}
    }
    objXmlTT.send(null);
  }
  else
  {
  	if(document.getElementById)
    {
    	objXmlTT = new XMLHttpRequest();
    	objXmlTT.open("GET", datafile, false);
    	objXmlTT.send(null);
      
      tt = objXmlTT.responseText;
     	populateTransactionTypeAction(tt, tt_list);
    }
  }	
}

function populateTransactionTypeAction(tt, tt_list)
{
	tt_list.disabled = false;
	// repopulate
  if(tt == 'sale')
		tt_list.options[0] = new Option("For Sale","sale");
  else if(tt == 'rent')
		tt_list.options[0] = new Option("For Rent","rent");
  else
  {
	  tt_list.options[0] = new Option("--Select Transaction Type--","");
    tt_list.options[1] = new Option("For Sale","sale");
    tt_list.options[2] = new Option("For Rent","rent");
  }
}

function populatePropertyType(p, property_type)
{
  var p = p.value;
  var property_type_list = getObject(property_type);
  
  var datafile = "/populate/package_item.php?package="+p+"&item=zoning";
  
	//Clear the county box out
  for(i=property_type_list.options.length-1; i>-1; i--)
	  property_type_list.options[i] = null;
	
  property_type_list.options[0] = new Option("       Loading...       ", "");
  property_type_list.disabled = 'disabled';  
  
  if(document.all)
  {
    objXmlPT = new ActiveXObject("Microsoft.XMLHTTP");
    objXmlPT.open("GET", datafile, true);
    objXmlPT.onreadystatechange=function()
    {
    	if (objXmlPT.readyState==4)
      {
      	property_type = objXmlPT.responseText;
      	populatePropertyTypeAction(property_type, property_type_list);
  		}
    }
    objXmlPT.send(null);
  }
  else
  {
  	if(document.getElementById)
    {
    	objXmlPT = new XMLHttpRequest();
    	objXmlPT.open("GET", datafile, false);
    	objXmlPT.send(null);
      
      property_type = objXml.responseText;
     	populatePropertyTypeAction(property_type, property_type_list);
    }
  }	
}

function populatePropertyTypeAction(property_type, property_type_list)
{
	property_type_list.disabled = false;
	// repopulate
  if(property_type == 'res')
		property_type_list.options[0] = new Option("Residental","res");
  else if(property_type == 'rent')
		property_type_list.options[0] = new Option("Commercial","com");
  else
  {
	  property_type_list.options[0] = new Option("--Select Property Type--","");
    property_type_list.options[1] = new Option("Residential","res");
    property_type_list.options[2] = new Option("Commercial","com");
  }
}

function populatePropertyCategory()
{
	var t = getObject('transaction_type');
  var p = getObject('property_type');

	var t_type = t.value;
  var p_type = p.value;
  
	var mls_category = getObject('mls_category');
  if(mls_category)
  {
  	// clear out the mls_category select box
  	for(j=mls_category.options.length-1; j>-1; j--)
		  	mls_category.options[j] = null;
    
  	if((t_type == 'sale' || t_type == 'rent') && (p_type == 'res' || p_type == 'com'))
	  {
    	mls_category.options[0] = new Option("Please Choose", "");
    	j=1;
    	for(var formid in form_types)
      {
      	if((form_types[formid]['type'] == 'both' || form_types[formid]['type'] == t_type) && (form_types[formid]['zoning'] == 'both' || form_types[formid]['zoning'] == p_type))
      	{
       		mls_category.options[j] = new Option(form_types[formid]['name'], formid);
          j++;
				}
      }
  	}
    else if(t_type == 'sale' || t_type == 'rent')
    	mls_category.options[0] = new Option("Choose Property Type First", "");
    else
  		mls_category.options[0] = new Option("Choose Transaction Type First", "");
  }
}

function populatePropertyTypeList()
{
	var type_sale = getObject("type_sale");
  var type_rent = getObject("type_rent");
  var type_both = getObject("type_both");
  
  var zoning_res = getObject("zoning_res");
  var zoning_com = getObject("zoning_com");
  var zoning_both = getObject("zoning_both");
  
  var property_types = getObject("property_types");
  
  var type_checked;
  var zoning_checked;
  
  // populate the type that's checked
  if(type_sale.checked)
  	type_checked = "sale";
  else if(type_rent.checked)
  	type_checked = "rent";
  else if(type_both.checked)
  	type_checked = "both";
  
  // populate the zoning that's checked
  if(zoning_com.checked)
  	zoning_checked = "com";
  else if(zoning_res.checked)
  	zoning_checked = "res";
  else if(zoning_both.checked)
  	zoning_checked = "both";
  
  // clear out the current option list
  for(j=property_types.options.length-1; j>-1; j--)
  	property_types.options[j] = null;
  
  j=0;
  // filter out the list based on the type and zoning checked
  //	if one is not checked, don't filter on that value
  //	if both is checked or form is 'both', include it
  for(i in property_form_types)
  {
  	if((!type_checked || type_checked == 'both' || property_form_types[i]['type'] == 'both' || property_form_types[i]['type'] == type_checked) && (!zoning_checked || zoning_checked == 'both' || property_form_types[i]['zoning'] == 'both' || property_form_types[i]['zoning'] == zoning_checked))
    {
    	// add the new option to the list
    	property_types.options[j] = new Option(property_form_types[i]['name'], i);
      // update the 'selected' property with the value from the stored variable
      property_types.options[j].selected = property_form_types[i]['selected'];
      j++;
    }
  }
}

function addPropertyType(formid)
{
	var form = getObject('property_types_'+formid);
  var list = getObject('available_property_types');
  var error = "";
  for(var k=0; k<list.length; k++)
  	if(list.options[k].selected)
    {
    	if((form_type[formid] == 'both' || form_type[formid] == form_type_type[list.options[k].value] || form_type_type[list.options[k].value] == 'both') && (form_zoning[formid] == 'both' || form_zoning[formid] == form_type_zoning[list.options[k].value] || form_type_zoning[list.options[k].value] == 'both'))
      {
      	if(notInList(list.options[k].value, form))
	    	 	form.options[form.options.length] = new Option(list.options[k].text, list.options[k].value);
        list.options[k] = null;
        // we removed one from list, so we have to decrement the counter
        k--;
      }
      else
      {
      	error += "The property type '"+list.options[k].text+"' is for ";
        if(form_type_zoning[list.options[k].value] == 'res')
        	error += "Residential ";
        else if(form_type_zoning[list.options[k].value] == 'com')
        	error += "Commercial ";
      	if(form_type_type[list.options[k].value] == 'sale')
        	error += "Sales ";
        else if(form_type_type[list.options[k].value] == 'rent')
        	error += "Rentals ";
        error += "Only\n";
    	}
    }
      
  setSize(form,list);
  sortList(form);
  selectNone(form,list);

  if(error != "")
  {
  	error += "\nThe form you selected is for ";
    if(form_zoning[formid] == 'res')
    	error += "Residential ";
    else if(form_zoning[formid] == 'com')
    	error += "Commercial ";
  	if(form_type[formid] == 'sale')
    	error += "Sales ";
    else if(form_type[formid] == 'rent')
    	error += "Rentals ";
    error += "Only\n\n";
  	alert("The following errors were encountered:\n\n"+error);
  }
}

function delPropertyType(formid)
{
	var form = getObject('property_types_'+formid);
  var list = getObject('available_property_types');
  
  for(var k=0; k<form.length; k++)
  	if(form.options[k].selected)
    {
    	if(notInList(form.options[k].value, list))
	  	 	list.options[list.options.length] = new Option(form.options[k].text, form.options[k].value);
      form.options[k] = null;
      // we removed one from form, so we have to decrement the counter
      k--;
    }

  setSize(form,list);
  sortList(list);
  selectNone(form,list);
}

function sortList(box)
{
  var order = new Array();

  for(var i=0; i<box.options.length; i++)
	  order[i] = form_type_order[box.options[i].value];
    
  order.sort(compareNum);
  
  for(var i=0; i<order.length; i++)
  {
  	box.options[i].value = form_type_value[order[i]];
    box.options[i].text = form_type_name[order[i]];
  }
}

function notInList(value, l)
{
	for(var x=0; x<l.length; x++)
  	if(l.options[x].value == value)
    	return false;
  return true;
}

function compareNum(a,b)
{
	return a-b
}

function setSize(list1,list2)
{
	list1.size = getSize(list1);
	list2.size = getSize(list2);
}

function selectNone(list1,list2)
{
	list1.selectedIndex = -1;
	list2.selectedIndex = -1;
}

function getSize(list)
{
	var len = list.childNodes.length;
	var nsLen = 0;
	//nodeType returns 1 for elements
	for(i=0; i<len; i++)
		if(list.childNodes.item(i).nodeType==1)
			nsLen++;
      
	return nsLen ? nsLen : 1;
}

function selectAllPropertyTypes()
{
  for(var i in form_id)
  {
  	var form = getObject("property_types_"+form_id[i]);
    for(j=0; j<form.options.length; j++)
    	form.options[j].selected = true;
  }
}

function updateSelectedPropertyTypeList()
{
	var property_types = getObject("property_types");
  // updates the 'selected variable with the status of each option
  for(i=0; i<property_types.options.length; i++)
  {
   	property_form_types[property_types.options[i].value]['selected'] = property_types.options[i].selected;
  }
}

function populateCounties(s, c, status, force, showCounties)
{
	var state = s.options[s.selectedIndex].value;
	var box = document.getElementById(c);
  populateSelect(box, state, "/populate/counties.php?state="+state+"&status="+status, force, showCounties);
}

function populateBrokenDownCounties(s, c)
{
	var state = s.options[s.selectedIndex].value;
	var box = document.getElementById(c);
  populateSelect(box, state, "/populate/counties.php?state="+state+"&status=brokendown", "no", "yes");
}

function populateUserCounties(s, c, username)
{
	var state = s.options[s.selectedIndex].value;
	var box = document.getElementById(c);
	populateSelect(box, state, "/populate/counties.php?state="+state+"&status=user&username="+username, "yes", "yes");
}

function populateContactCounties(s, c, username)
{
	var state = s.options[s.selectedIndex].value;
	var box = document.getElementById(c);
	populateSelect(box, state, "/populate/counties.php?state="+state+"&status=contact&username="+username, "yes", "yes");
}

function populateSelect(box, state, datafile, forceCounty, showCounties)
{
	//Clear the county box out
  for(i=box.options.length-1; i>-1; i--)
	  box.options[i] = null;
	
  box.options[0] = new Option("       Loading...       ", "");
  box.disabled = 'disabled';  
  
  if(document.all)
  {
    objXml = new ActiveXObject("Microsoft.XMLHTTP");
    objXml.open("GET", datafile, true);
    objXml.onreadystatechange=function()
    {
    	if (objXml.readyState==4)
      {
      	states = objXml.responseText;
      	populateSelectAction(box, state, states, forceCounty, showCounties);
  		}
    }
    objXml.send(null);
  }
  else
  {
  	if (document.getElementById)
    {
    	objXml = new XMLHttpRequest();
    	objXml.open("GET",datafile,false);
    	objXml.send(null);
      
      states = objXml.responseText;
     	populateSelectAction(box, state, states, forceCounty, showCounties);
    }
  }
}

function populateSelectAction(box, state, states, forceCounty, showCounties)
{
	box.disabled = false;
	// repopulate
	if(forceCounty == "yes" || state == '')
		o = "--Select County--";
	else
		o = "--Entire State--";
	
  box.options[0] = new Option(o,"");
  if(states != "none")
  {
	 	state_array = states.split("|");
    if(showCounties == "yes")
    	for(i=0; i<state_array.length; i++)
      	if(state_array[i])
     			box.options[i+1] = new Option(state_array[i],state_array[i]);
       
  }
}

var allowSearchNoCity = true;
function doSearchState()
{
	var sBox = document.getElementById('state');
	var state = sBox.options[sBox.selectedIndex].value;
	var cBox = document.getElementById('county');
  var cityBox = document.getElementById('city');
  
  cBox.style.display = 'none';
  cityBox.style.display = 'none';

 	// search by state only
 	if(st[state] == 'state')
   	return true;
  // populate county box
  else if(st[state] == 'county' || st[state] == 'city')
  	populateSearchCounty();
    
  // populate city box
  else if(st[state] == 'nocounty')
  	populateSearchCity('state');
}

function doSearchCounty()
{
	var sBox = document.getElementById('state');
	var state = sBox.options[sBox.selectedIndex].value;
	var cBox = document.getElementById('county');
  var cityBox = document.getElementById('city');
  
  // search by county only
  if(st[state] == 'county')
  	return true;
    
  // populate city box
  else if(st[state] == 'city')
  	populateSearchCity();
}

function populateSearchCounty()
{
	var sBox = document.getElementById('state');
	var state = sBox.options[sBox.selectedIndex].value;
	var cBox = document.getElementById('county');
  var cityBox = document.getElementById('city');

  //Clear the county box out
  for(i=cBox.options.length-1; i>-1; i--)
	  cBox.options[i] = null;
	
	// repopulate
	if(state == '')
  {
		o = "--Select County--";
    cBox.style.display = 'none';
    cityBox.style.display = 'none';
  }
	else
  {
		cBox.style.display = 'block';
    cityBox.style.display = 'none';
		
		if(es[state])
		{
			o = "--Entire State--";
			allowSearchNoCity = true;
    }
		else
		{
			o = "--Select County--";
			allowSearchNoCity = false;
		}
    cBox.options[0] = new Option(o,"");
   	var i = 1;
   	for(var a in ls[state])
   	{
   		cBox.options[i] = new Option(a+" County",a);
   		i++;
   	}
    if(i < 3)
    	cBox.style.display = 'none';
  }
}

function populateSearchCity()
{
	var sBox = document.getElementById('state');
  var state = sBox.options[sBox.selectedIndex].value;
	var cBox = document.getElementById('county');
  var county = cBox.options[cBox.selectedIndex].value;
  var cityBox = document.getElementById('city');
  
  //Clear the city box out
  for(i=cBox.options.length-1; i>-1; i--)
	  cityBox.options[i] = null;
	
	// repopulate
	if(ec[state][county])
	{
		o = "--Entire "+(st[state] == 'nocounty' ? "State" : "County" )+"--";
		allowSearchNoCity = true;
	}
	else
	{
		o = "--Choose City--";
		allowSearchNoCity = false;
	}
  cityBox.style.display = 'block';
  
  cityBox.options[0] = new Option(o,"");
 	var i = 1;
  var variable = st[state] == 'nocounty' ? lc[state] : ls[state][county];
 	for(var a in variable)
 	{
 		cityBox.options[i] = new Option(a,a);
 		i++;
 	}
  if(i < 3)
  	cityBox.style.display = 'none';
}
