function LTrim( value ) 
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) 
{
	return LTrim(RTrim(value));
}

function isValidEmail(strEmail,type)
{
	if(strEmail.length<5)
	{
		return false;
	}
  validRegExp = /^[\w\.-]+@[a-z,A-Z,0-9-]+[\.]{1}[a-zA-Z]{2,}[[\.]?[a-zA-Z]{0,2}[[\.]?[a-zA-Z]{0,2}$/i;
   // search txtEmail text for regular exp matches
   strEmail = trim(strEmail);
    if (strEmail.search(validRegExp) == -1) 
	{
      return false;
    } 
	//chkExistMail(strEmail, type);
	return true; 
}

//validating address
function  isValidAddress(addr){
	//validRegExp = /^[0-9a-zA-Z\s\,./-]+$/;
	validRegExp = /^[0-9a-zA-Z \s,.\!\@\#\$\%\^\&\*\(\)\?\_\'\"/-]+$/;
    if (addr.match(validRegExp) == null) {
      return false;
    } 
    return true;
}
//Validating Name
function isValidName(val) {
	 validRegExp = /^[a-zA-Z \.\-]+$/i;
      val = trim(val);
	  if(val.length == 1)
	  return false;
    if (val.search(validRegExp) == -1) {
      return false;
    } 
    return true; 	
}

function isValidCity(val)
{
	val = trim(val);
	if(val.length <= 1)
	return false;
    return true; 

}

//Validating Comment
function  isValidComment(commt){
	validRegExp = /^[0-9a-zA-Z \s,.\!\@\#\$\%\^\&\*\(\)\?\_\'\"/-]+$/;
    if (commt.match(validRegExp) == null) {
      return false;
    } 
    return true;
}

//Validating Phone
function isValidPhone(val) {
	validRegExp = /^[0-9- \(\)\+]+$/;
   // search txtEmail text for regular exp matches
   	val = trim(val);
	if(val.length <= 6)
	{
		return false;
	}
    if (val.match(validRegExp) == null) {
      return false;
    }	
	return true;
}

if (window.XMLHttpRequest)
{
   // If IE7, Mozilla, Safari, etc: Use native object
	var xmlhttp = new XMLHttpRequest();

}
else
{
	if (window.ActiveXObject)
	{
		// ...otherwise, use the ActiveX control for IE5.x and IE6
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

}

function validate()				
{
	document.getElementById('name_error2').innerHTML = '';
	document.getElementById('name_error3').innerHTML = '';	
	document.getElementById('email_error').innerHTML = '';
		
	var FirstName = document.getElementById('txtFirstName').value;
	var LastName = document.getElementById('txtLastName').value;
	var Email = document.getElementById('txtEmail').value;
	
	if(FirstName == '')
	{
		document.getElementById('name_error2').innerHTML ="First Name is mandatory";
		document.getElementById('txtFirstName').focus();
		return false;
	}
	if(!isValidName(FirstName)){
		document.getElementById('name_error2').innerHTML =" Invalid First Name";
		document.getElementById('txtFirstName').focus();
		return false;
	}

	if(LastName == '')
	{
		document.getElementById('name_error3').innerHTML ="Last Name is mandatory";
		document.getElementById('txtLastName').focus();
		return false;
	}
	if(!isValidName(LastName)){
		document.getElementById('name_error3').innerHTML =" Invalid Last Name";
		document.getElementById('txtLastName').focus();
		return false;
	}
	
	/*if(!isValidPhone(Phone)){
		document.getElementById('errormessage').innerHTML =" Invalid Phone No";
		document.getElementById('phone').focus();
		return false;
	}	*/
	
	
	if(Email == '')
	{
		document.getElementById('email_error').innerHTML ="Email is mandatory";
		document.getElementById('txtEmail').focus();
		return false;
	}
	if(!isValidEmail(Email)){
		document.getElementById('email_error').innerHTML =" Invalid Email";
		document.getElementById('txtEmail').focus();
		return false;
	}
	
	return true;
}


function httpnewsletter_validate(){
	if (xmlhttp.readyState != 4){
		document.getElementById('spinner').innerHTML = "<img src='images/busy.gif' alt='Loading...'>";
	}
	else{
		mytext = xmlhttp.responseText;
		//alert(mytext);
		//document.getElementById('spinner').innerHTML = mytext;
		if(mytext == 'success'){
			window.location="thankyou.html"; }		
		else {
			document.getElementById('spinner').innerHTML = "";
			document.getElementById("errormessage").innerHTML = mytext;
			//document.getElementById('fname').focus();
		}
	}
}
