// Trim String
var xmlHttp;

function Trim(inString) 
{
  inString = inString.replace( /^\s+/g, "" );	// strip leading
  return inString.replace( /\s+$/g, "" );		// strip trailing
}

// Invalid E-mail
function InvalidEmail(email)
{
	var atsign = email.indexOf("@");
	var period = email.lastIndexOf(".");
	
	if(atsign == -1)
		return true;
	else if(atsign == 0 || atsign == email.length)
		return true;
	
	if(period == -1)
		return true;
	else if(period == 0 || period == email.length)
		return true;
	else if(email.charAt(period-1) == '@')
		return true;
	else if(period < atsign)
		return true;
		
	return false;
}

// Invalid Zip code
function InvalidZip(zip)
{
	numberzip = Trim(zip).replace("-","");
	
	if(!isInteger(numberzip))
		return true;
		
	if(zip.indexOf("-") == -1)
	{
		if(Trim(zip).length != 5)
			return true;	
	}
	else
	{	
		var zipstart = Trim(zip).substring(0,zip.indexOf("-"));
		var zipend = Trim(zip).substring(zip.indexOf("-")+1,Trim(zip).length);
		
		if(zipstart.length != 5 || zipend.length != 4)
			return true;
	}

	return false;
}

// Invalid Phone No.
function InvalidPhone(phone)
{
	var fmtphone = Trim(phone).replace(/ /g,"");
	fmtphone = fmtphone.replace(/-/g,"");
	fmtphone = fmtphone.replace("(","");
	fmtphone = fmtphone.replace(")","");
	
	if(!isInteger(fmtphone))
		return true;

	// 9999999
	if(fmtphone.length == 7)
	{
		if(phone.indexOf("-") != -1)
		{
			if(Trim(phone).charAt(3) != '-')
				return true;
			else if(!isInteger(Trim(phone).replace("-","")))
				return true;
		}	
	}
	// 1112223333
	else if(fmtphone.length == 10)
	{
		if(phone.indexOf("(") != -1)
		{
			if(Trim(phone).charAt(0) != '(' && Trim(phone).charAt(4) != ')')
				return true;
			if(Trim(phone).charAt(Trim(phone).length-5) != '-')
				return true;		
		}
		else if(phone.indexOf("-") != -1)
		{
			// (111) 222-3333 or 111-222-3333 or 111 222-3333
			if(!(Trim(phone).charAt(3) == '-' || Trim(phone).charAt(3) == ' ') ||
			 Trim(phone).charAt(Trim(phone).length-5) != '-')
				return true;	
		}
	}
	// 18001112222 
	else if(fmtphone.length == 11)
	{
		var nospace = phone.replace(/ /g,"");
		
		if(phone.indexOf("(") != -1)
		{
			if(nospace.charAt(1) != '(' && nospace.charAt(4) != ')')
				return true;
			else if(nospace.charAt(nospace.length-5) != '-')
				return true;		
		}
		else if(phone.indexOf("-") != -1)
		{		
			if(!(Trim(phone).charAt(1) == '-' || Trim(phone).charAt(1) == ' ') || 
		 	!(Trim(phone).charAt(5) == '-' || Trim(phone).charAt(5) == ' ') ||
			nospace.charAt(nospace.length-5) != '-')
				return true;
		}
	}
	else
		return true;

	return false;
}

// Check to see if string is all numeric
function isInteger(num)
{
	var numbers = "0123456789";
	var i = 0;
	
	for(i=0;i<num.length;i++)
	{
		if(numbers.indexOf(num.charAt(i)) == -1)
			return false;	
	}
	
	return true;
}

function isNumeric(num){
  var numbers = "0123456789.";
	var i = 0;
	
	for(i=0;i<num.length;i++)
	{
		if(numbers.indexOf(num.charAt(i)) == -1)
			return false;	
	}
	
	return true;
}


function CheckFormValue(fname,spanname)
{
	valid = true;
	var sFieldName = Trim(document.getElementById(fname).value);
	if(sFieldName.length == 0 || sFieldName == null)
	{
		document.getElementById(spanname).style.display="inline";
		valid = false;
	}
	else
		document.getElementById(spanname).style.display="none";
	return 	valid;
}

function CheckSelectValue(fname,spanname, selvalue)
{
	valid = true;
	var sFieldName = document.getElementById(fname).value;
	if(sFieldName > selvalue)
		document.getElementById(spanname).style.display="none";
	else
	{
		document.getElementById(spanname).style.display="inline";
		valid = false;
	}
	
		
	return 	valid;
}


function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)objXMLHttp=new XMLHttpRequest()
	else if (window.ActiveXObject)	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	return objXMLHttp
}