//**********************************************************************
//****                    Formatting Functions                      ****
//**********************************************************************

// Trim String
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 tabOver(tf, nf, len)
{
	var thisform = document.getElementById(tf);
	var nextform = document.getElementById(nf);
	
	if(Trim(thisform.value).length == len)
		nextform.focus();
}

// Only allow number input
function numbersOnly(e)
{
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event)
		keynum = e.keyCode;
	else if(e.which)
		keynum = e.which;
	
	if((keynum <= 57 && keynum != 32) || (keynum >= 96 && keynum <= 105))
		return true;
	else
		return false;
}

// Check to see if string is a valid URL
function isURL(url) 
{
	var regexUrl = /^(http|ftp|https)\:\/\/\w+([\.\-]\w+)*(\.\w{2,4})+(\:\d+)*([\/\.\-\?\&amp;\%\#]\w+)*(\/?)/i;
	
	return url.match(regexUrl);
}

//Used in frmUpdateProfile.aspx
function selectCategory(f)
{
	strCategory=f.options[f.selectedIndex].value;
	if (strCategory=="0")
	{
		top.frames.location="frmUpdateProfile.aspx";
	}
}

function hideDiv(divName)
{
	var el = document.getElementById(divName);
	el.style.display = "none";
}