var objAjax = new Ajax();
var places = new Array();


function $(id)
{
	return document.getElementById(id);
} // $


function is_empty(str)
{
	return ( str==null || str==0 || str=="" || str.length<1 );
} // is_empty


/*
*	populates the select options
*
*	@param		array optArray
*	@param		Select object obj
*	@param		boolean clear
*
*/
function populateSelects(optArray,obj,clear)
{
  var objSelect = document.getElementById(obj);
  var totalObjSelect = objSelect.options.length;
  var subOptionArray = new Array();
  var totalSubOption = 0;
  var resultArray = new Array();

  // firt of all, we have to clear the select option if its necessary
  if (clear == true)
  {
	  for (var i=totalObjSelect-1 ; i>=0 ; i--)
  		objSelect.options[i] = null;
  }

  // then we proced to add the options
  // NOTE: we have to spli the given optArray, because we get something like: 0=>1|1=>2|2=>3...
  subOptionArray = optArray.split('|');
  totalSubOption = subOptionArray.length;

  for( var i=0; i<totalSubOption;i++ )
  {
	var optNew = document.createElement('option');
	resultArray = subOptionArray[i].split('=>');
	optNew.value = resultArray[0];
	optNew.text = resultArray[1];

	// trying to add the option, depending of the internet brower
	try
	{
		objSelect.add(optNew, null); // standard way, doesn't work in IE
	}
	catch(ex)
	{
		objSelect.add(optNew); // for IE only
	}
  } // for
} // populateSelectss


function check_form_response()
{
	document.getElementById("submit").removeAttribute('disabled');
	var response = parseInt(objAjax.getResponseText());

	if (response==1)
	{
		alert("Email Successfully Sent. Thank You...!");
		document.getElementById("firstname").value = "";
		document.getElementById("contactinformation").value = "";
		document.getElementById("subject").value = "";
		document.getElementById("request").value = "";
		document.getElementById("code").value = "";		
	}
	else if ( response==2 )
	{
		alert("The Code is Incorrect, Please type it Again.");	
		document.getElementById("code").value = "";
		document.getElementById("code").focus();
	}
	else if( response==0 )
	{
		alert("Error Sending Email, Please Try Again");
	}
} // check_form_response




function check_form()
{
	// Lets set the variables and evaluate it
	var firstName = document.getElementById("firstname").value;
	var email = document.getElementById("contactinformation").value;
	var country = document.getElementById("country").value;
	var subject = document.getElementById("subject").value;
	var request = document.getElementById("request").value;
	var code = document.getElementById("code").value;
	var error = false;
	var msgError = "Please check this: \n\n";
	

	// Lets see if everething its fine!
	if ( firstName=="" )
	{
		error = true;
		msgError +="- You must write your first name, to send the request... \n";
	} // if
		
	
	if ( !isEmailAddress(email) )
	{
		error = true;
		msgError +="- You must write a valid email, to send the request... \n";
	} // if

	if ( subject=="" )
	{
		error = true;
		msgError +="- You must write a subject, to send the request... \n";
	} // if


	if ( request=="" )
	{
		error = true;
		msgError +="- You must write a description, to send the request... \n";
	} // if

	if ( code=="" )
	{
		error = true;
		msgError +="- You must write a code, to send the request... \n";
	} // if	
	
	if ( !error )
	{
		document.getElementById("submit").setAttribute('disabled', true); 
		objAjax.setURL("/contact_us_send_email");
		objAjax.setParameter("firstName", firstName);
		objAjax.setParameter("email", email);
		objAjax.setParameter("country", country);
		objAjax.setParameter("subject", subject);
		objAjax.setParameter("request", request);
		objAjax.setParameter("code", code);				
		objAjax.setComplete( check_form_response );
		objAjax.execute();
	}
	else
	{
		alert(msgError);
	} // if
} // check_form




function isEmailAddress(email)
{
var s = email;
var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
if (s.length == 0 ) return true;
   if (filter.test(s))
      return true;
   else
     return false;
}




function listPlace_response()
{
	var result = objAjax.getResponseText();
	result = result.split("#");

	if ( !is_empty(result[1]) )
	{
		places[result[0]] = result[1];
		populateSelects(result[1],"placeList",true);
	}
} // listPlace_response




function listPlace()
{
	var id_state = $('province').value;
	if ( is_empty(places[id_state]) )
	{

		objAjax.setURL('/actionBringPlace');
		objAjax.setParameter("id_state",id_state);
		objAjax.setComplete( listPlace_response );
		objAjax.execute();
	}
	else
		populateSelects(places[id_state],"placeList",true);
} // listPlace


function goTo(server_name)
{
	// Lets bring the variables that wee need :D
	var state = $('province').value;
	var place = $('placeList').value;
	var subCategory = $('subCategory').value;
	var price = $('price').value;
	var area = $('area').value;
	var bedroom = $('bedroom').value;
	var URL = "http://www.propertiesjaco.com/test_listing";
	var entro_state = false;
	var entro_place = false;
	var entro_subcategory = false;
	var entro_price = false;
	var entro_area = false;
	
	if ( !is_empty(state) )
	{
		if ( !is_empty(place) )
		{
			URL = URL+"/"+state+"/"+place;
			entro_place = true;
		}	
		else	
			URL = URL+"/"+state;	
		entro_state = true;		
	}
	
	if ( !is_empty(subCategory) )
	{
		entro_subcategory = true;
		if ( !entro_state )
			URL = URL+"/costa_rica/"+subCategory;
		else if ( entro_place )
			URL = URL+"/"+subCategory;	
		else
			URL = URL+"/View/"+subCategory;		
	}	


	if ( !is_empty(price) )
	{
		entro_price = true;
		if ( !entro_subcategory && !entro_state )
			URL = URL+"/costa_rica/price/"+price;	
		else
			URL = URL+"/price/"+price;		
	}	

	if ( !is_empty(area) )
	{
		entro_area = true;
		if ( !entro_price && !entro_subcategory && !entro_state )
			URL = URL+"/costa_rica/area/"+area;	
		else
			URL = URL+"/area/"+area;		
	}	

	if ( !is_empty(bedroom) )
	{
		if ( !entro_price && !entro_subcategory && !entro_area && !entro_state)
			URL = URL+"/costa_rica/bedrooms/"+bedroom;
		else
			URL = URL+"/bedrooms/"+bedroom;
	}				
	
	window.location = URL;
} // goTo
