﻿function trim(inputString) {
// **** Remove leading and trailing spaces ****

	//Make sure input is string, otherwise return input
	if (typeof inputString != "string") {
		return inputString;
	}

	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ") {
	//Check for leading spaces and remove
	retValue = retValue.substring(1, retValue.length);
	ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") {
	//Check for trailing spaces and remove
	retValue = retValue.substring(0, retValue.length-1);
	ch = retValue.substring(retValue.length-1, retValue.length);
	}

	return retValue;
}


function isEmail (s)
{
    if (typeof s != "string") {
	return false;
	}
    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    //don't even bother if string is less than 4 chars ("a@b.c".length=5)
    if (!sLength > 4){
	return false;
	}

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    {
	i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

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;
   
   }

function contact_check(){

	allOk=true;
	// Name Validation 
	if (trim(document.contact_form.name.value).length == 0 && allOk) 
	{
		alert("Please Enter your name");
		document.contact_form.name.focus();
		allOk = false;
		return allOk;
	}
	
	// **** Email validation ****
	if (trim(document.contact_form.email.value).length == 0 && allOk)
	{
		alert("Please Enter your email");
		document.contact_form.email.focus();
		allOk = false;
		return allOk;
	} else {
		if (!isEmail(document.contact_form.email.value)) {
			alert("Please check your email address");
			document.contact_form.email.focus();
			allOk = false;
			return allOk;			
		}		
	}
	
	if (trim(document.contact_form.tel.value).length == 0 && allOk) 
	{
		alert("Please enter your contact number");
		document.contact_form.tel.focus();
		allOk = false;
		return allOk;
	}else {
		if (!IsNumeric(document.contact_form.tel.value) || trim(document.contact_form.tel.value).length < 8 )
		{
			alert("Please check your contact number");
			document.contact_form.tel.focus();
			allOk = false;
			return allOk;
		}
	}
		
	
	if (allOk){
      		return true;
	}
	
}


function contact_submit()
{
	if (contact_check())
	{
		document.contact_form.action = "thankyou.php";
		document.contact_form.submit();
	}
	
}

