var whitespace = " \t\n\r"

function Validate(theForm)
{
	var errMesg = "";
	var diplayMesg = "";


	// Check all the Required Information fields.
	var Q = ""; // this block determines lifespan of Q
	if (isWhitespace(theForm.name.value))
	{
		Q += "  Name\n";
	}

	if (isWhitespace(theForm.email.value))
	{
		Q += "  Email \n";
	}
	else if(echeck(theForm.email.value))
	{
	   errMesg += "Invalid Email Address\n";
	}	
	
	else if(!isCharsInBag( theForm.email.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-"))
	{
	  errMesg += "Email Address contains Invalid Characters\n";
	}
	if (isWhitespace(theForm.organization.value))
	{
		Q += "  Organization\n";
	}
	if (theForm.phone.value!='')
	{
		 if(!isCharsInBag(theForm.phone.value,"0123456789- "))
		{
			errMesg += "Telephone contains Invalid Characters\n";
		}
	}
	if (isWhitespace(theForm.city.value))
	{
		Q += "  City\n";
	}
	if (theForm.country.value==0)
	{
		Q += "  Country\n";
	}
	if (theForm.country.value!=0)
	{
		if (theForm.country.value!=1 && theForm.otherstate.value=="")
		{	
			Q += "  Other State\n";
		}
		
		if (theForm.country.value==1 && theForm.state.value=="")			
		{
			Q += "  State\n";
		}
	}
	if (isWhitespace(theForm.zip.value))
	{
		Q += "  Zip Code\n";
	}
	if (isWhitespace(theForm.code.value))
	{
		Q += "  Security Code\n";
	}

	if ( Q.length > 0 )
	{
		diplayMesg = "Please provide Valid values for\n" + Q ;
	}

	if (errMesg == "" && diplayMesg == "")
	{
		return true;
	}
	else
	{
		if(diplayMesg!="")
		{
			alert(diplayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
}

function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}
function isWhitespace (s)
{  var i;
	// Is s empty?
	if (isEmpty(s)) return true;

	 // Search through string's characters one by one
	 // until we find a non-whitespace character.
	 // When we do, return false; if we don't, return true.
	 for (i = 0; i < s.length; i++)
	 {   
		 // Check that current character isn't whitespace.
		 var c = s.charAt(i);
 
		 if (whitespace.indexOf(c) == -1) return false;
	 }
 
	 // All characters are whitespace.
	 return true;
}
function BuildStr(s, s1)
{
	  if (s.length> 0) s = s + "\n";
	  s = s + s1;
	  return s;
}

function isCharsInBag (s, bag)
{  
  var i;
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {   
	  // Check that current character isn't whitespace.
	  var c = s.charAt(i);
	  if (bag.indexOf(c) == -1) return false;
  }
  return true;
}


function echeck(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return true;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return true;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return true;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return true;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return true;
	 }

	 return false;					
}


function enableState(country) {
	if(country == 1) {
		document.getElementById('otherstate').disabled = true;
		document.getElementById('state').disabled = false;		
	}
	else {
		document.getElementById('otherstate').disabled = false;
		document.getElementById('state').disabled = true;		
	}
}