// JavaScript Document
function isblank(s) {
   for(var i = 0; i < s.value.length; i++) {
      var c = s.value.charAt(i);
      if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
   }
   return true;
}  
function checkContact(obj) {
	if (isblank(obj.name))
	{
		alert("Please enter your name.");
		obj.name.focus();
		return false;
	}
	if (!isblank(obj.email))
	{
		var valid = true;	// Initially set valid to true
		var atcount = 0;
		var email = obj.email.value;
		var email_length = obj.email.value.length;
		
		for (index=0; index<email_length; index++)
		{
			if ((email.substring(index,index+1) == ":") || (email.substring(index,index+1) == ",") || (email.substring(index,index+1) == "'") || (email.substring(index,index+1) == " ") )
			{
				valid = false;
			}
		}
		
		for (i=0; i<email_length; i++)
		{
			if (email.substring(i,i+1) == "@")
			{
				++atcount;
				if (email.substring(i-1,i) == ".")
					valid = false;
				if (email.substring(i+1,i+2) == ".")
					valid = false;
			}
		}
			if (email.substring(email_length-1,email_length) == "@")
			valid = false;
			if (email.substring(email_length-1,email_length) == ".")
			valid = false;
			if (email.substring(0,1) == "@")
			valid = false;
			if (email.substring(0,1) == ".")
			valid = false;
			if (obj.email.value.indexOf(".") == -1)
			valid = false;
			if (atcount > 1 || atcount == 0)
			valid = false;
		
		if (!valid) 
		{
			alert("Please include a proper email address (Generally in the form 'username@hostname.domain').");
			obj.email.focus();
			return false;
		}
	}
	if (isblank(obj.message))
	{
		alert("Please write a message.");
		obj.message.focus();
		return false;
	}
	return true;
}
function checkRequestProperty(obj) {
	if (isblank(obj.name))
	{
		alert("Please enter your name.");
		obj.name.focus();
		return false;
	}
	if ( isblank(obj.contact_number) && isblank(obj.email) )
	{
		alert("Please enter contact number or email.");
		obj.contact_number.focus();
		return false;
	}
	if (!isblank(obj.email))
	{
		var valid = true;	// Initially set valid to true
		var atcount = 0;
		var email = obj.email.value;
		var email_length = obj.email.value.length;
		
		for (index=0; index<email_length; index++)
		{
			if ((email.substring(index,index+1) == ":") || (email.substring(index,index+1) == ",") || (email.substring(index,index+1) == "'") || (email.substring(index,index+1) == " ") )
			{
				valid = false;
			}
		}
		
		for (i=0; i<email_length; i++)
		{
			if (email.substring(i,i+1) == "@")
			{
				++atcount;
				if (email.substring(i-1,i) == ".")
					valid = false;
				if (email.substring(i+1,i+2) == ".")
					valid = false;
			}
		}
			if (email.substring(email_length-1,email_length) == "@")
			valid = false;
			if (email.substring(email_length-1,email_length) == ".")
			valid = false;
			if (email.substring(0,1) == "@")
			valid = false;
			if (email.substring(0,1) == ".")
			valid = false;
			if (obj.email.value.indexOf(".") == -1)
			valid = false;
			if (atcount > 1 || atcount == 0)
			valid = false;
		
		if (!valid) 
		{
			alert("Please include a proper email address (Generally in the form 'username@hostname.domain').");
			obj.email.focus();
			return false;
		}
	}
	
	var locations = document.getElementById("locations"); 
	var locinput = locations.getElementsByTagName("input");
	var locempty=false; 
	for (var i = 2; i < locinput.length; i++) { 
		if(locinput[i].checked){
		locempty=true;
		}
	}
	
	var types = document.getElementById("types"); 
	var typeinput = types.getElementsByTagName("input");
	var proempty=false; 
	for (var i = 2; i < typeinput.length; i++) { 
		if(typeinput[i].checked){
		proempty=true;
		}
	}
	
	if(locempty==false && proempty==false && obj.pricerange.selectedIndex==0){
		alert("Please choose a preferred location, property type or price range");	
		return false;
	}
	
	if (obj.other.checked)
	{
		if (isblank(obj.other_requirement))
			{
			alert("Please enter other requirement.");
			obj.other_requirement.focus();
			return false;
			}
	}
	if(!obj.swimming_pool.checked && !obj.garden.checked && !obj.garage.checked && !obj.own_roof.checked && !obj.other.checked){
	   obj.features.value = 'None';
	}else{
	  obj.features.value = 'y';
	}
	return true;
}
function checkSubmitProperty(obj) {
	if (isblank(obj.name))
	{
		alert("Please enter your name.");
		obj.name.focus();
		return false;
	}
	if ( isblank(obj.contact_number) && isblank(obj.email) )
	{
		alert("Please enter contact number or email.");
		obj.contact_number.focus();
		return false;
	}
	if (!isblank(obj.email))
	{
		var valid = true;	// Initially set valid to true
		var atcount = 0;
		var email = obj.email.value;
		var email_length = obj.email.value.length;
		
		for (index=0; index<email_length; index++)
		{
			if ((email.substring(index,index+1) == ":") || (email.substring(index,index+1) == ",") || (email.substring(index,index+1) == "'") || (email.substring(index,index+1) == " ") )
			{
				valid = false;
			}
		}
		
		for (i=0; i<email_length; i++)
		{
			if (email.substring(i,i+1) == "@")
			{
				++atcount;
				if (email.substring(i-1,i) == ".")
					valid = false;
				if (email.substring(i+1,i+2) == ".")
					valid = false;
			}
		}
			if (email.substring(email_length-1,email_length) == "@")
			valid = false;
			if (email.substring(email_length-1,email_length) == ".")
			valid = false;
			if (email.substring(0,1) == "@")
			valid = false;
			if (email.substring(0,1) == ".")
			valid = false;
			if (obj.email.value.indexOf(".") == -1)
			valid = false;
			if (atcount > 1 || atcount == 0)
			valid = false;
		
		if (!valid) 
		{
			alert("Please include a proper email address (Generally in the form 'username@hostname.domain').");
			obj.email.focus();
			return false;
		}
	}
	if (isblank(obj.location))
	{
		alert("Please enter the property location.");
		obj.location.focus();
		return false;
	}
	if (isblank(obj.asking_price))
	{
		alert("Please enter asking price.");
		obj.asking_price.focus();
		return false;
	}
	if (obj.other.checked)
	{
		if (isblank(obj.other_features))
			{
			alert("Please enter other features.");
			obj.other_features.focus();
			return false;
			}
	}
	if(!obj.swimming_pool.checked && !obj.garden.checked && !obj.garage.checked && !obj.own_roof.checked && !obj.other.checked){
	   obj.features.value = 'None';
	}else{
	  obj.features.value = 'y';
	}
	return true;
}