// Richard Hunt. Target Training International, Scottsdale AZ. 2003-2004 

function submitTest() {
	// check to make sure that all required fields are filled out before submitting.
	var pass = 0; 
	/* check radio buttons.
	for(x = 0; x < 4; x++) {
		if(document.signUp.rd1[x].checked){ pass++; break; }
	}
	for(x = 0; x < 8; x++) {
		if(document.signUp.rd2[x].checked){ pass++; break; }
	}
	for(x = 0; x < 5; x++) {
		if(document.signUp.rd3[x].checked){ pass++; break; }
	}
	for(x = 0; x < 7; x++) {
		if(document.signUp.rd4[x].checked){ pass++; break; }
	}
	for(x = 0; x < 3; x++) {
		if(document.signUp.rd6[x].checked){ pass++; break; }
	}*/
	// check text fields.
	if(Trim(document.signUp.fname.value) == "" || Trim(document.signUp.lname.value) == "" ||
	   Trim(document.signUp.comp.value) == "" ||  Trim(document.signUp.state.value) == "" ||
	   Trim(document.signUp.phone.value) == "" || Trim(document.signUp.email.value) == "")
	{
		messageWarm();	
	}
	else {
		if(echeck(document.signUp.email.value) == true) {
			document.signUp.submit();		
		}
		else {
			messageWarm();	
		}
	}	
}

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 false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}


// Highlight required fields on form:
function messageWarm() {
	document.getElementById("x1").style.backgroundColor = "#A6BAFC";	
	document.getElementById("x2").style.backgroundColor = "#A6BAFC";	
	document.getElementById("x3").style.backgroundColor = "#A6BAFC";	
	document.getElementById("x4").style.backgroundColor = "#A6BAFC";	
	document.getElementById("x5").style.backgroundColor = "#A6BAFC";	
	document.getElementById("x6").style.backgroundColor = "#A6BAFC";	
	setTimeout('messageCool()', 3000);
}

// Reset the required fields to normal:
function messageCool() {
	document.getElementById("x1").style.backgroundColor = "#D9E4F1";	
	document.getElementById("x2").style.backgroundColor = "#D9E4F1";	
	document.getElementById("x3").style.backgroundColor = "#D9E4F1";	
	document.getElementById("x4").style.backgroundColor = "#D9E4F1";	
	document.getElementById("x5").style.backgroundColor = "#D9E4F1";	
	document.getElementById("x6").style.backgroundColor = "#D9E4F1"; 
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

