


function TrimString(str)
{
	var stringInput = new String(str)//variable to store input string 
	startPosition=0;	              //variable to store the postion where the first Non Blank character comes
	stringLength = stringInput.length;	//stores string length
	endPosition = stringLength-1;			//stores the position where the last non blank character comes
		
	while(startPosition<stringLength)	//starts finding first non blank character from begining of the string
	{
		if(stringInput.substr(startPosition,1)!=' ' && stringInput.charCodeAt(startPosition)!=13 && stringInput.charCodeAt(startPosition)!=10)	
		{
			break;
		}
		startPosition=startPosition+1;	//if non blank character is found then the position is stored and loop breaks
	}
		
		
	while(endPosition>=startPosition)
	{
		if(stringInput.substr(endPosition,1)!=' ' && stringInput.charCodeAt(endPosition)!=13 && stringInput.charCodeAt(endPosition)!=10)
		{
			break;
		}
		endPosition=endPosition-1;
	}
	var stringReturn = stringInput.substring(startPosition,endPosition+1);	//gets the string between 2 postions
	
	return stringReturn;
}


function isPhone(strPhone)
{
	// following line define a Regular Expression for validating Phone Numbers
	//A regular expression to match phone numbers, allowing for an international dialing code at the start and hyphenation and spaces that are sometimes entered.  
	//Matches: [(+44)(0)20-12341234], [02012341234], [+44 (0) 1234-1234]   [ More Details]  
	//Non-Matches: [(44+)020-12341234], [12341234(+020)] [ Test Expression] 
	
	
	var objPhoneRegExp = /^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/; 
	var flag = strPhone.match(objPhoneRegExp); //validate Phone
	
	if(flag==null)
	   {	//if not valid Phone Number
		return false;
		}
	else
	   {	
		return true;
		}
}


function IsBlank(objTextbox)
{
	if(TrimString(objTextbox.value) == "")
		{
			return true;
		}
	return false;
}

function ValidateEmail(strEmail)
{
	try
	{
		var objRegExp =/^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/;
		
		var flag=strEmail.match(objRegExp);//validate Email Address.		
		if(flag==null)	//if not valid Email address.
			return false;
		else
			return true;
	}
	// block of code to catch the exception raised, if any
	catch (exception){}
}


      function CheckNumeric()
		{
			//get the ascii value of the key pressed by the user			
			var key = window.event.keyCode;			
			//was the key pressed a numeric character
			if( key > 47 && key < 58)
			{
				return;  //if so, do nothing
			}
			else
			{
				window.event.returnValue = null; //discard character
			}
		
		}
		


function ValidateForm()
{
	if(ValidateAll())
	{
		return true;
	}
	else
	{
		return false;
	}
}

function ValidateAll()
{
    try
    {
    var objName=document.getElementById('MailFromName');
    var objFirmName=document.getElementById('Address');
    var objMailingAddress= document.getElementById('Address2');
    var objCity= document.getElementById('City');
    var objState= document.getElementById('State');
    var objphone1=document.getElementById('Country');
    var objphone2=document.getElementById('Country').value;
    var objMailFromAddress1=document.getElementById('MailFromAddress');
    var objMailFromAddress=document.getElementById('MailFromAddress').value;
    
    if(IsBlank(objName))
    {  
        alert('Please fill Name.')
		document.getElementById('MailFromName').focus();
		return false;
    }	
    
    else if(IsBlank(objFirmName))
    {
        alert('Please fill Firm Name.')
	    document.getElementById('Address').focus();
	    return false;
    }	
	
	else if(IsBlank(objMailingAddress))
    {
       alert('Please fill Principal Mailing Address.')
	   document.getElementById('Address2').focus();
	   return false;
    }	
	
	else if(IsBlank(objCity))
    {
        alert('Please fill City.')
		document.getElementById('City').focus();
		return false;
    }	
    
    
    else if(IsBlank(objState))
    {
       alert('Please fill State.')
		document.getElementById('State').focus();
		return false;
    }	
	
	else if(IsBlank(objphone1))
	{
		alert('Please fill Phone No..')
		document.getElementById('Country').focus();
		return false;
	}
	
	else if(IsBlank(objMailFromAddress1))
	{
		alert('Please fill Email.')
		document.getElementById('MailFromAddress').focus();
		return false;
	}
	
	else if(!isPhone(objphone2))
	{
		alert('Please fill Correct Phone No..')
		document.getElementById('Country').focus();
		return false;
	}
	
	else if(!ValidateEmail(objMailFromAddress))
	{
		alert('Please fill Correct Email .')
		document.getElementById('MailFromAddress').focus();
		return false;
	}
	
	
	else
	return true;
	}catch(Err)
	{
	    alert(Err);
	}
	
	
}






