// JavaScript Document for apartment search in new Triangle.com homepage widget

/*******************************
* Search JS from Apartments.com *
* link to rent.aspx may need to be updated when template is ready
********************************/

// Determine whether to do a zipcode search or city/state
//     zipcode overrides city/state
function deterLoad()
{

theCity = window.document.qs.city.value;
theState = window.document.qs.state.value;
//window.document.qs.state.options[window.document.qs.state.selectedIndex].value; //if use drop-down list
theZip = window.document.qs.zip.value;
theZip = escape(theZip);



  if (theZip.length > 0)
  {  
       return loadZS();
  }
  else if ( theState != "")
  {

       return loadQS();
  }
  else
  {
    alert("Please enter a \"city and state\" or \"zipcode\".");
    return false;
  }
}


function loadQS(w)
{
	var theCity;
	var theState;
	var radius;
	var bool;
	
	theCity = window.document.qs.city.value;
	//theState = window.document.qs.state.options[window.document.qs.state.selectedIndex].value; // if use state drop-down list
	theState = window.document.qs.state.value;
	radius = window.document.qs.rad.options[window.document.qs.rad.selectedIndex].value;
		if(radius == '5' || radius == '10' || radius == '20') {
			if(theCity == ""){
			alert("Please enter a City");
			document.qs.city.focus();
			return false;
			}
		}
	bool = validateForm(theState);
	if (bool == "n")
	{
	  return false;
	}
	else
	{
	
	theCity = escape(theCity);
	
	  var qString;
	  if(document.qs.rad.value == '5' || document.qs.rad.value == '10' || document.qs.rad.value == '20'){
			qString = "http://www.apartments.com/Rent.aspx?page=rent&stype=city&city=" + theCity + "&state=" + theState + "&rad=" + radius;
		}
	  else{
			qString = "http://www.apartments.com/Rent.aspx?stype=city&city=" + theCity + "&state=" + theState;
		}
		window.document.location = qString;
	  return false;
}

}
function validateForm(st)
{
	var daState = st;
	
	if (daState == "")
	{
		alert("Please Select a State from the menu")
		return("n");
	}
	
	else
	{
		return("y");
	}
}
//NxB
function loadZS()
{
	var theZip;
	var bool;

	theZip = document.qs.zip.value;
	
	theZip = escape(theZip);

	if(theZip.length < 5)
	{
		alert("Please enter one/multiple 5 digit zip code/s seperated by commas");
		return false;
	}
	
	var qString;
	if(document.qs.rad.value == '5' || document.qs.rad.value == '10' || document.qs.rad.value == '20'){
      qString  = "http://www.apartments.com/Rent.aspx?page=rent&stype=zip&zip=" + theZip + "&rad=" + document.qs.rad.value;
	}
	else{
	  qString = "http://www.apartments.com/Rent.aspx?stype=zip&zip=" + theZip;
	  }
	window.document.location = qString;
	return false;
}

function ValidateZip(WebSearchFrm)
{
   if(WebSearchFrm.txtZipCode.value == "") 
   { 
      alert('Missing ZIP information. Please enter a ZIP code to post a listing') 
      WebSearchFrm.txtZipCode.focus(); 
      return false; 
   } 
 
 
   if (!IsNumeric(WebSearchFrm.txtZipCode.value)) 
   { 
      alert('Please enter only numbers in the Zip Code') 
      WebSearchFrm.txtZipCode.focus(); 
      return false; 
      } 
 
 return true;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}