//====================== Validation Script
/*
1		--		required
2		--		numeric
4		--		phone
8		--		zip
16		--		email
32		--		state
64		--		time
128
256
512
*/


//**********  valditaion below ********************************
function validate(frm) {
/* *************************************************************
* set some variables that will be checked later         *
***************************************************************/
var msg = "";                 // an output message
var missing = "";             // for missing required fields
var invNum = "";              // for invalid numeric fields
var outOfRange = "";          // less than min or more than max
var invZIP = "";              // for invalid zip codes
var invPhone = "";            // for invalid phone numbers
var invState = "";            // for invalid state fields
var invEmail = "";			  // for invalid email fields
var invTime = "";			  // for invalid time fields
var invSelection = "";    // if user didn't make a selection
  
/* *************************************************************
* The main validation function, calls other sub-functions      *
***************************************************************/

//alert("in validate form");
if (frm.rbInterest)
{
	var bChecked = false;
	for (var j=0;j<frm.rbInterest.length ;j++)
	{
		if  (frm.rbInterest[j].checked){
			bChecked = true
		}
	}	
	if (!bChecked){
		alert("Please make a selection");
		return false;
	}
}

//	alert("after  !bchecked");
//	alert("missing.length =" + missing.length);


for(i=0; i<frm.elements.length; i++){ // loop through form elements
	 // alert("in for loop for time " + i);
    var el = frm.elements[i];
    //alert(el.name);
    //alert(el.opts);
	
		
	if(el.opts & 1){             // if element has required property
                                 // test to see if field is empty
		//alert("in required");
		if(isEmpty(el)){
		 switch (el.name) {
		 case "_1_FN":
			missing += "\n   - First Name is a required field";
			break;
		 case "FN":
			missing += "\n   - First Name is a required field";
			break;
		 case "_2_LN":
			missing += "\n   - Last Name is a required field";
			break;
		 case "LN":
			missing += "\n   - Last Name is a required field";
			break;
		 case "selType":
			missing += "\n   - Please Select a Complaint Type";
			break;
		 case "accType":
			missing += "\n   - Please Select an Account Type";
			break;
		 case "email":
			missing += "\n   - E-mail is a required field";
			break;
		 case "add1":
			missing += "\n   - Street Address is a required field";
			break;
		 case "city":
			missing += "\n   - City is a required field";
			break;
		 case "county":
			missing += "\n   - County is a required field";
			break;
		 case "txtPostDate":
			missing += "\n   - Post Date is requird";
			break;
		 case "DaText":
			missing += "\n   - Details are required";
			break;
		 case "txtTitle":
			missing += "\n   - Title is Required";
			break;
		 case "phone":
			missing += "\n   - Daytime phone is a required field";
			break;
		 case "probDesc":
			missing += "\n   - Please supply a problem description";
			break;
		 case "oldPWD":
			missing += "\n   - Please supply old password";
			break;
		 case "pwd1":
			missing += "\n   - Please supply new password";
			break;
		 case "pwd2":
			missing += "\n   - Please confirm new password";
			break;
		 case "pwdMatch":
			missing += "\n   - Passwords don't match";
			break;
		 case "txtSubject":
			missing += "\n   - Subject is required";
			break;
		 case "txtStartDate":
			missing += "\n   - Start Date is required";
			break;
		 case "txtStartTime":
			missing += "\n   - Start Time is required";
			break;
		 case "txtEndDate":
			missing += "\n   - End Date is required";
			break;
		 case "txtEndTime":
			missing += "\n   - End time is required";
			break;
		 case "txtRSVP":
			missing += "\n   - Please supply an email address for the RSVP";
			break;
		 default:
			missing += "\n   - " + el.name + " is a required field";
		 } //close switch
      } //close isEmpty
    } //close if required
    if(el.opts & 2) {
	 	//alert("checking if numeric for:" + el.name);
		//alert("the value is" + el.value);
      if(!isNumeric(el)) {
        invNum += "\n   - " + el.value + " is not a number";
      }
    }

	 if(el.isInt) {
      if(isFloat(el)) {
        invNum += "\n   - " + el.value + " decimal numbers not allowed";
      }
    }
	
    if(el.minVal || el.minVal == 0) {
		//alert("in minVal is=" +el.minVal);
      if(parseFloat(el.value) < el.minVal) {
        outOfRange +=  "\n   - " + el.name + " must be larger than " + el.minVal + ", you entered " + el.value;
      }
    }
    if(el.maxVal) {
      if(parseFloat(el.value) >= el.maxVal) {
        outOfRange +=  "\n   - " + el.name + " must be smaller than " + el.maxVal + ", you entered " + el.value
      }
    }
    if(el.opts & 8 && el.value.length !=0) {
      if(invalidZIP(el)) {
        invZIP += "\n  - " + el.value + " is an invalid ZIP code";
	  }
    }
    if(el.opts & 4 && el.value.length !=0) {
      if(invalidPhone(el)) {
        invPhone += "\n  - " + el.value + " is not a valid phone number";
      }
    }
    if(el.opts & 32 && el.value.length != 0){
      if(invalidState(el)){
        invState += "\n  - " + el.value + " is not a valid two-letter state abbreviation";
      }
    }
	if(el.opts & 16){
		if (validateEMail(el)){
		invEmail += "\n - " + el.value + " is not a valid email address.";
		}
	}
	if(el.opts & 64){
		if (invalidTime(el)){
		invTime += "\n - " + el.value + " is not a valid Time.";
		}
	}
	if(el.chosen) {
		//alert("in rbInterest check");
		if (noRBSelected(el))	{
			invSelection += "\n - No selection made.";
		}
	}

	if(el.posNum)
	{
		if (isNegative(el))
		{
			invNum += "\n - No negative numbers allowed";
		}
	}

  } //close the for loop

	//alert("out of for loop");
  // build output message

	
//alert("missing.length= " + missing.length);
//alert("outOfRange.length " + outOfRange.length);

 if(missing.length !=0 || invNum.length != 0 || outOfRange.length != 0 || invZIP.length != 0 || invPhone.length != 0 || invState.length != 0 || invEmail.length != 0 || invTime.length !=0)
{
		 // alert("in the if statement for errors");
    if(missing.length !=0) {
      msg += "\n\nThe following required fields are missing:";
      msg += missing;
     }
    if(invNum.length !=0)  {
      msg += "\n\nYou entered incorrect numeric data in these fields:";
      msg += invNum;
     }
    if(outOfRange.length !=0) {
      msg += "\n\nYou entered out-of-range data in these fields:";
      msg += outOfRange;
     }
    if(invZIP.length !=0) {
      msg += "\n\nYou entered an incorrect ZIP code in these fields:";
      msg += invZIP;
     }
    if(invPhone.length !=0){
      msg += "\n\nYou entered an incorrect phone number in these fields:";
      msg += invPhone;
     }
    if(invState.length !=0){
      msg += "\n\nYou entered an incorrect state abbreviation";
      msg += invState;
     }
	if (invEmail.length !=0){
		msg +="\n\nYou entered an incorrect Email address";
		msg += invEmail;
	}
	if (invTime.length !=0){
		msg +="\n\nYou entered an incorrect Time";
		msg += invTime;
	}
  // alert("after if statements");

    errMsg(msg);           // call the output function
    msg = ""; missing = ""; invNum = ""; invZIP = ""; invPhone = ""; invState = "" ; outOfRange = ""; invTime = ""; // reset all our variables
    invEmail =""; invSelection="";
	return false;
	}
	else {
	//	alert("about to return true");
    return true;
	}
}

/* *************************************************************
* Sub-functions follow from here to end of file                *
* All sub-functions return true if field is of invalid         *
* format and false if they are valid entries                   *
***************************************************************/
function isEmpty(field)
  {
  var str = field.value;

  //alert("the value is: " + field.value);

  if(str == "") 
    {
    return true;
    }
  else
    {
    for(j=0; j<str.length; j++)
      {
      if(str.charAt(j) != " ") return false;
      }
    }
  return true;
  }

function invalidTime(field)
{
	var timeRegExp = /[0-9].:[0-9]./;
	var str = field.value;
	if(timeRegExp.test(str))
		return false;
	else
		return true;
}

function isNumeric(field)
  {
  var errCount = 0;
  var numdecs = 0;              // number of decimal points
  var nummins = 0;              // number of minus signs
  for(j=0;j<field.value.length;j++)
    {
    c = field.value.charAt(j);        // short hand notation for character at position j
    if((c >= 0 && c <= 9) || (c=="." || c=="-"))
      {
      if(c==".") numdecs++;     // count the number of decimal points
      if(c=="-") nummins++;     // count the number of minus signs
      }
    else
      {
      errCount++;               // if it's none of those, increment error counter
      }
    }
  // error if count is non-zero or there are more than one decimal point or minus sign
  if(errCount > 0 || numdecs > 1 || nummins > 1)
    {
    return false;
    }
  return true;
  }

function stripNonDigits(str)
  {
  newStr = "";
  for(j=0; j<str.length; j++)
    {
    c = str.charAt(j);
    if(c >= "0" && c <= "9")
      {
      newStr += c;
      }
    }
  return newStr;
  }

function invalidZIP(field)
  {
  newStr = stripNonDigits(field.value);
  if(newStr.length == 5 || newStr.length== 9)
    {
    return false;
    }
  return true;
  }

function invalidPhone(field)
  {
  newStr = stripNonDigits(field.value);
  if(newStr.length == 10)
    {
    return false;
    }
  return true;
  }

function invalidState(field)
  {
  var STATES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/LA/ME/MD/MA/MI/MN/MS/MO/MT/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/TN/TX/UT/VT/VA/WA/WV/WI/WY";
  var newStr = field.value.toUpperCase();
  if(STATES.indexOf(newStr) == -1 || newStr.indexOf("/") != -1 || newStr.length != 2)
    {
    return true;
    }
  return false;
  }

  function validateEMail(field) {
	var	email = field.value;
  var atsignPos = email.indexOf("@", 0)     // check for @
  if (atsignPos == -1)	
    {
    // alert("Enter a valid email address with an @, please!")
    return true;
    }
  if (email.indexOf(".", atsignPos) == -1)  // check for . after @	
    {
    // alert("Enter a valid email domain after the @, please!")
    return true;
    }
  return false;
  }

	function noRBSelected(field) {
	
		for (var i=0;i<frm.rbInterest.length ;i++){
	
			if (frm.rbInterest[i].checked){
				return false;
			}
			return true;
		}
	}
	
	function isNegative(field)
	{
		var n = field.value;
		var posNeg = n.indexOf("-");
		if (posNeg == -1)
		{
			return false;
		}
		return true;
	}

	function isFloat(field)
	{
		var n = field.value;
		var pos = n.indexOf(".");
		if (pos == -1)
		{
			return false;
		}
		return true;
	}


function errMsg(msg)
  {
  var theMsg = "You entered some incorrect values into the form. ";
  theMsg += "Please correct your entries then re-submit the form.\n";
  theMsg += "____________________________________________________________________";
  theMsg += msg;
  theMsg += "\n____________________________________________________________________\n";
  alert(theMsg);
  }

function testRef(){
//	document.layers["HomeMenu"].innerText="myTest";
//  alert("test ref " + document.layers["HomeMenu"].innerText); //getRef("HomeMenu").innerText);
}
//  alert("bn.js loaded in full");
