// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}  // End function

// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
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.lastIndexOf(dot) == (lstr-1)){
	    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					
}
// A utility function that returns true if a string contains a non-positive integer number,
// commas aren't allowed.
function fieldNotInteger(e) {

    var istrValidChars = "0123456789";
    var istrChar;
    var istrNxtChar;
    var iisNumeric;
    var ibadStrEnd;
    var idblChar;
    var idecmCount;

    iisNumeric = true;
    ibadStrEnd = false;
    idblChar = false;
    idecmCount = 0;
    for(var m = 0; m < e.value.length && iisNumeric; m++) {
        if (e.value.charAt(0) === "," ||  e.value.charAt(e.value.length-1) === ",") ibadStrEnd = true;
        if (!ibadStrEnd) {
            istrChar = e.value.charAt(m);
            if (istrChar === ".") idecmCount++;
            if (idecmCount < 2 && m != e.value.length-1) {
       	        istrNxtChar = e.value.charAt(m+1);
                if ((istrChar === "," && istrNxtChar === ",") ||
                    (istrChar === "," && istrNxtChar === ".") ||
                    (istrChar === "." && istrNxtChar === ",") ||
                    (istrChar === "." && istrNxtChar === ".")) idblChar = true;
            }
        }
        if (istrValidChars.indexOf(istrChar) == -1 || ibadStrEnd || idecmCount > 1 || idblChar) {
            iisNumeric = false;
            return true;
        }
    }   //  End for loop

    return false;

}  // End function


// A utility function that returns true if a string contains an invalid decimal number
function fieldNotDecimal(e) {

    var strValidChars = "0123456789,.-";
    var strChar;
    var strNxtChar;
    var isNumeric;
    var badStrEnd;
    var dblChar;
    var decmCount;

    isNumeric = true;
    badStrEnd = false;
    dblChar = false;
    decmCount = 0;
    for(var j = 0; j < e.value.length && isNumeric; j++) {
        if (e.value.charAt(0) === "," ||  e.value.charAt(e.value.length-1) === ",") badStrEnd = true;
        if (!badStrEnd) {
            strChar = e.value.charAt(j);
            if (strChar === ".") decmCount++;
            if (decmCount < 2 && j != e.value.length-1) {
       	        strNxtChar = e.value.charAt(j+1);
                if ((strChar === "," && strNxtChar === ",") ||
                    (strChar === "," && strNxtChar === ".") ||
                    (strChar === "." && strNxtChar === ",") ||
                    (strChar === "." && strNxtChar === ".")) dblChar = true;
            }
        }
        if (strValidChars.indexOf(strChar) == -1 || badStrEnd || decmCount > 1 || dblChar) {
            isNumeric = false;
            return true;
        }
    }   //  End for loop

    return false;

}  // End function

function Form1_Validator(theForm)
{

  var msg_all;
  var msg_txt = "";
  var msg = new Array();
  var errors = "";

  if (!theForm.Consultant[0].checked && !theForm.Consultant[1].checked)
  {
   theForm.Consultant[0].focus();
   errors = "yes";
   msg[13] = "yes";
  }

// Note:  The USER3 form field is a select multiple. Hence it must be named USER3[] so it can be
// referred to as an array in PHP. However, the [] on the element name causes problems in Javascript
// so you must refer to the element by name, i.e. use it's numerical form element ID instead -- see below.
//
//  NOT THIS ---> if (theForm.USER3.selectedIndex == 0)
//  NOT THIS --->  theForm.USER3.focus();
//  
//  BUT THIS --->
//
  if (theForm.elements[24].selectedIndex == 0)
  {
    theForm.elements[24].focus();
    errors = "yes";
	msg[12] = "yes";
  } 

  if (!echeck(theForm.x_email.value))
  {
    theForm.x_email.focus();
    errors = "yes";
	msg[11] = "yes";
  }

  if ((theForm.x_zip.value == null) || (theForm.x_zip.value == "") || isblank(theForm.x_zip.value))
  {
    theForm.x_zip.focus();
    errors = "yes";
	msg[10] = "yes";
  }
  
  lastselectopt = theForm.x_country.options.length - 1;

  if (((theForm.x_country.selectedIndex != lastselectopt) && (theForm.x_country.selectedIndex != 0)) && ((theForm.USER2.value != null) && (theForm.USER2.value != "") & !isblank(theForm.USER2.value)))
  {
    theForm.USER2.focus();
    errors = "yes";
	msg[9] = "yes";
  } 

  if (theForm.x_country.selectedIndex == lastselectopt && ((theForm.USER2.value == null) || (theForm.USER2.value == "") || isblank(theForm.USER2.value)))
  {
    theForm.USER2.focus();
    errors = "yes";
	msg[8] = "yes";
  } 

  if (theForm.x_country.selectedIndex == 0)
  {
    theForm.x_country.focus();
    errors = "yes";
	msg[7] = "yes";
  } 
   
  lastselectopt = theForm.x_state.options.length - 1;
  
  if (((theForm.x_state.selectedIndex != lastselectopt) && (theForm.x_state.selectedIndex !=0)) && ((theForm.USER1.value != null) && (theForm.USER1.value != "") & !isblank(theForm.USER1.value)))
  {
    theForm.USER2.focus();
    errors = "yes";
	msg[6] = "yes";
  } 

  if (theForm.x_state.selectedIndex == lastselectopt && ((theForm.USER1.value == null) || (theForm.USER1.value == "") || isblank(theForm.USER1.value)))
  {
    theForm.USER1.focus();
    errors = "yes";
	msg[5] = "yes";
  } 

  if (theForm.x_state.selectedIndex == 0)
  {
    theForm.x_state.focus();
    errors = "yes";
	msg[4] = "yes";
  } 
   
  if ((theForm.x_city.value == null) || (theForm.x_city.value == "") || isblank(theForm.x_city.value))
  {
    theForm.x_city.focus();
    errors = "yes";
	msg[3] = "yes";
  }
  
  if ((theForm.x_address.value == null) || (theForm.x_address.value == "") || isblank(theForm.x_address.value))
  {
    theForm.x_address.focus();
    errors = "yes";
	msg[2] = "yes";
  }

 if ((theForm.x_last_name.value == null) || (theForm.x_last_name.value == "") || isblank(theForm.x_last_name.value))
  {
    theForm.x_last_name.focus();
    errors = "yes";
	msg[1] = "yes";
  }

 if ((theForm.x_first_name.value == null) || (theForm.x_first_name.value == "") || isblank(theForm.x_first_name.value))
  {
    theForm.x_first_name.focus();
    errors = "yes";
	msg[0] = "yes";
  }
        
 if (errors == "yes")
  {
    if (msg[0] == "yes")
	  msg_txt += "\n-- The \"First Name\" field is required. Please complete the \"First Name\" field.\n";
    if (msg[1] == "yes")
	  msg_txt += "\n-- The \"Last Name\" field is required. Please complete the \"Last Name\" field.\n";
    if (msg[2] == "yes")
	  msg_txt += "\n-- The \"Street Address\" field is required. Please complete the \"Street Adress\" field.\n";
    if (msg[3] == "yes")
	  msg_txt += "\n-- The \"City\" field is required. Please complete the \"City\" field.\n";
    if (msg[4] == "yes")
	  msg_txt += "\n-- The \"State or Province\" option is not a valid selection.  Please choose another \"State or Province\" option.\n";
    if (msg[5] == "yes")
	  msg_txt += "\n-- Please complete the \"specify if Other\" field since \"Other\" is selected for the \"State or Province\" option.\n";
    if (msg[6] == "yes")
	  msg_txt += "\n-- Please clear the \"specify if Other\" field since a \"State or Province\" option is selected.\n";
    if (msg[7] == "yes")
	  msg_txt += "\n-- The \"Country\" option is not a valid selection.  Please choose another \"Country\" option.\n";
    if (msg[8] == "yes")
	  msg_txt += "\n-- Please complete the \"specify if Other\" field since \"Other country\" is selected for the \"Country\" option.\n";
    if (msg[9] == "yes")
	  msg_txt += "\n-- Please clear the \"specify if Other\" field since a \"Country\" option is selected.\n";
    if (msg[10] == "yes")
	  msg_txt += "\n-- The \"Zip / Postal Code\" field is required. Please complete the \"Zip / Postal Code\" field.\n";
    if (msg[11] == "yes")
	  msg_txt += "\n-- The \"Email\" field must contain a valid email address.\n";
    if (msg[12] == "yes")
	  msg_txt += "\n-- The \"Select Your Area(s) of Expertise\" option is not a valid selection.  Please choose another \"Expertise\" option.\n";
    if (msg[13] == "yes")
	  msg_txt += "\n-- One of the \"Listing Type\" selection fields must checked.\n";

    msg_all  = "______________________________________________________\n\n"
    msg_all += "The form was not submitted because of the following error(s).\n";
    msg_all += "Please correct these error(s) and click continue.\n";
    msg_all += "______________________________________________________\n\n"
    
	msg_all += msg_txt;
	alert(msg_all);
	return (false);	  
  }
  else
    return (true);
}
