function Check()
{	
	if(GetText("Fname").length == 0)
	{
		alert('Enter First Name');
		return;
	}
	else if(GetText("Lname").length == 0)
	{
		alert('Enter Last Name');
		return;
	}
	else if(GetText("Address").length == 0)
	{
		alert('Enter Address');
		return;
	}
	else if(GetText("City").length == 0)
	{
		alert('Enter City Name');
		return;
	}
	else if(!IsNumeric(GetText("Zip_code")))
	{
		alert('Enter a valid Zip Code');
		return;
	}
	else if(GetText("Country").length == 0)
	{
		alert('Enter Country Name');
		return;
	}
	else if(!IsNumeric(GetText("Phone")))
	{
		alert('Enter a Valid Phone Number');
		return;
	}
	else if(!validateAddress(GetText("Email")))
	{
		alert('Enter a valid Email adress');
		return;
	} 
	else if(!CheckButton("radiobutton_buy","radiobutton_sell"))
	{
		alert('Choose Buy or Sell');
		return;
	}
	else if(GetText("Memo").length == 0)
	{
		alert('Message cannot be empty');
		return;
	}
	else
	{
		var object = document.getElementById("form_contact");
		object.submit();
	}
	
}

function leftTrim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	
	return sString;
}

function GetText(ID)
{
	var object = document.getElementById(ID);
	var text = leftTrim(object.value);
	return text;
}

function CheckButton(button1,button2)
{
	var object1 = document.getElementById(button1);
	var object2 = document.getElementById(button2);
	if(object1 != null)
	{
		if(object1.checked == false && object2.checked == false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	return true;
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789- ";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
   
 function validateAddress(incoming) {
	var emailstring = incoming;
	var ampIndex = emailstring.indexOf("@");
	var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
	// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAmp.indexOf(".");
	// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + ampIndex + 1;
	// afterAmp will be portion of string from ampersand to dot
	afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
	// afterDot will be portion of string from dot to end of string
	var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
	var beforeAmp = emailstring.substring(0,(ampIndex));
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
	// index of -1 means "not found"
	if ((emailstring.indexOf("@") != "-1") &&
		(emailstring.length > 5) &&
		(afterAmp.length > 0) &&
		(beforeAmp.length > 1) &&
		(afterDot.length > 1) &&
		(email_regex.test(emailstring)) ) {
		  return true;
	} else {
		  return false;
	}
}

