// JavaScript Document

//remove the white sapces from the beginning & end of the string
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//This is the JS function to validate whether the input is not a empty, 
//if the input is non empty then it will passed to further process else the required input will be ask to the end user
function fieldBlank(fieldName,msg)
{	
	if(trim(fieldName.value)=="")
	{
		alert(msg);
		fieldName.focus();
		return false;
	}
	else
	{
		return true;
	}	
}




//This is the JS function to validate whether the input is a valid email format, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validEmail(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var email = fieldName.value;
		var matcharray = email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z]+)*\.[A-Za-z]+$/) 
		if(matcharray == null)
		{	
			alert(msg);
			fieldName.select();
			return false;
		}
		else 
		{
			return true;
		}
	}
	return true;		
}

//This is the JS function to validate whether the input is a valid alphabetic characters, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validAlpha(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var reAlpha = /^[a-zA-Z ]+$/;	
		if(reAlpha.test(fieldName.value)==true)
		{
			return true;
		}
		else
		{
			alert(msg);
			fieldName.select();
			return false;
		}
	}
	return true;	
}

//This is the JS function to validate whether the input is a valid alphanumeric characters, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validAlphaNumeric(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var reAlphanumeric = /^[a-zA-Z0-9_ ]+$/;
		if(reAlphanumeric.test(fieldName.value)==true)
		{
			return true;
		}
		else
		{
			alert(msg);
			fieldName.select();
			return false;
		}	
	}
	return true;
}

//This is the JS function to validate whether the input is a valid Phone Number or Fax Number, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validPhone(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var reAlphanumeric = /^[0-9_\-)( ]+$/;
		if(reAlphanumeric.test(fieldName.value)==true)
		{
			return true;
		}
		else
		{
			alert(msg);
			fieldName.select();
			return false;
		}	
	}
	return true;
}



//This is the JS function to compare the two fields input, 
//if the result is valid it will be passed to further process else the required input will be ask to the end user
function compareFields(fieldName1,fieldName2,cmp,msg)
{

	if(trim(fieldName1.value)!="" && trim(fieldName1.value)!="")
	{
			
		if(cmp=="=")
		{
			if(fieldName1.value==fieldName2.value)
			{				
				return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp==">")
		{
			if(fieldName1.value>fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp=="<")
		{
			if(fieldName1.value<fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp=="<=")
		{
			if(fieldName1.value<=fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp==">=")
		{
			if(fieldName1.value>=fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}	
		
			
	}
	return true;	
}

function validateRequest()
{
	var name = document.getElementById('name1');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	if(!fieldBlank(name,'Please enter the name'))
	{
		return false;	
	}
	if(!validAlpha(name,'Please enter the valid name'))
	{
		return false;	
	}
	if(!fieldBlank(email,'Please enter the email addresss'))
	{
		return false;	
	}
	if(!validEmail(email,'Please enter the valid email addresss'))
	{
		return false;	
	}
	if(!fieldBlank(phone,'Please enter the phone number'))
	{
		return false;	
	}
	if(!validPhone(phone,'Please enter the valid phone number'))
	{
		return false;	
	}
	return true;
}

function validateContact()
{
	var name = document.getElementById('name1');
	var email = document.getElementById('email');
	var confirm_email = document.getElementById('confirm_email');
	var company = document.getElementById('company');
	var phone = document.getElementById('phone');
	if(!fieldBlank(name,'Please enter the name'))
	{
		return false;	
	}
	if(!validAlpha(name,'Please enter the valid name'))
	{
		return false;	
	}
	if(trim(company.value) != '')
	{
		if(!validAlphaNumeric(company,'Please enter the valid company name'))
		{
			return false;	
		}
	}
	if(!fieldBlank(email,'Please enter the email addresss'))
	{
		return false;	
	}
	if(!validEmail(email,'Please enter the valid email addresss'))
	{
		return false;	
	}
	if(!fieldBlank(confirm_email,'Please enter the confirm email addresss'))
	{
		return false;	
	}
	if(!validEmail(confirm_email,'Please enter the valid confirm email addresss'))
	{
		return false;	
	}
	if(!compareFields(email,confirm_email,'=','Please check the email address & confirm email address'))
	{
		return false;	
	}
	if(!fieldBlank(phone,'Please enter the phone number'))
	{
		return false;	
	}
	if(!validPhone(phone,'Please enter the valid phone number'))
	{
		return false;	
	}
	
	return true;
}

