var formSubmitted = false;
function Validate(theForm)
{
	
	var errMesg = "";
	var displayMesg = "";
	// Check all the Required Information fields.
	
	//alert('test');
	var Q = ""; // this block determines lifespan of Q
	var invalid = " "; // Invalid character is a space
	
	if (isWhitespace(theForm.first_name.value))
	{
		Q += "  First Name\n";
	}
	if (isWhitespace(theForm.last_name.value))
	{
		Q += "  Last 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.username.value))
	{
		Q += "  Username \n";
	}
	if (isWhitespace(theForm.password.value))
	{
		Q += "  Password \n";
	}
	if (isWhitespace(theForm.retype_password.value))
	{
		Q += "  Retype Password \n";
	}
	if(theForm.password.value.length < 6)
	{
		errMesg += "Password required minimum 6 characters\n";
	}
	if (theForm.password.value.indexOf(invalid) > -1) {
		errMesg += "Sorry, spaces are not allowed in Password.\n";
	}	
	if(theForm.password.value != theForm.retype_password.value)
	{
		errMesg += "Retype Password not match\n";
	}
	if (isWhitespace(theForm.code.value))
	{
		Q += "  Security Code \n";
	}

	
	if ( Q.length > 0 )
	{
		displayMesg = "Please provide Valid values for\n" + Q ;
		//errMesg += "Please provide Valid values for\n" + Q ;
	
	}
	
	if (errMesg == "" && displayMesg == "")
	{
		return true;
	}
	else
	{
		if(displayMesg!="")
		{
			alert(displayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
}


function EditValidate(theForm)
{
	
	var errMesg = "";
	var displayMesg = "";
	// Check all the Required Information fields.
	
	//alert('test');
	var Q = ""; // this block determines lifespan of Q
	var invalid = " "; // Invalid character is a space
	
	if (isWhitespace(theForm.first_name.value))
	{
		Q += "  First Name\n";
	}
	if (isWhitespace(theForm.last_name.value))
	{
		Q += "  Last 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.username.value))
	{
		Q += "  Username \n";
	}
	if (isWhitespace(theForm.password.value))
	{
		Q += "  Password \n";
	}
	if (isWhitespace(theForm.retype_password.value))
	{
		Q += "  Retype Password \n";
	}
	if(theForm.password.value.length < 6)
	{
		errMesg += "Password required minimum 6 characters\n";
	}
	if (theForm.password.value.indexOf(invalid) > -1) {
		errMesg += "Sorry, spaces are not allowed in Password.\n";
	}	
	if(theForm.password.value != theForm.retype_password.value)
	{
		errMesg += "Retype Password not match\n";
	}

	
	if ( Q.length > 0 )
	{
		displayMesg = "Please provide Valid values for\n" + Q ;
		//errMesg += "Please provide Valid values for\n" + Q ;
	
	}
	
	if (errMesg == "" && displayMesg == "")
	{
		return true;
	}
	else
	{
		if(displayMesg!="")
		{
			alert(displayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
}

function ForgotPassValidate(theForm)
{
	
	var errMesg = "";
	var displayMesg = "";
	// Check all the Required Information fields.
	
	//alert('test');
	var Q = ""; // this block determines lifespan of Q
	
	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 ( Q.length > 0 )
	{
		displayMesg = "Please provide Valid values for\n" + Q ;
		//errMesg += "Please provide Valid values for\n" + Q ;
	}
	
	if (errMesg == "" && displayMesg == "")
	{
		return true;
	}
	else
	{
		if(displayMesg!="")
		{
			alert(displayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
}

function openWin(url)
{
	var j=window.open(url,"jj","width=800,height=600,scrollbars=1")
}

var whitespace = " \t\n\r"
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 numeralsOnly1(evt)
{
    
	evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Enter numerals only in Quantity field.");
        return false;
	}
    return true;
}







