/*
Check out this page for the drop-down menu:
	http://www.milonic.com/mfa/2004-February/003643.html

The cose for the javascript:
<script	language=JavaScript>
	if(ns4)_d.write("<scr"+"ipt language=JavaScript src=/mmenuns4.js><\/scr"+"ipt>");		
	  else _d.write("<scr"+"ipt language=JavaScript src=/mmenudom.js><\/scr"+"ipt>"); 
	</script>

*/




/*
 * Form validation
 */
var errMsg = "";


/*
 *  Code compliments of http://www.virtualpromote.com/tools/javascript-regex/
 */
// Validator Object
var valid = new Object();

// REGEX Elements

// matches zip codes
valid.zipCode = /\d{5}(-\d{4})?/;

// matches $17.23 or $14,281,545.45 or ...
valid.Currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;

// matches 5:04 or 12:34 but not 75:83
valid.Time = /^([1-9]|1[0-2]):[0-5]\d$/;

//matches email
valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

// matches phone ###-###-####
valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;

// International Phone Number
valid.phoneNumberInternational = /^\d(\d|-){7,20}/;

// IP Address
valid.ipAddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;

// Date xx/xx/xxxx
valid.Date = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;

// State Abbreviation
valid.State = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;

// Social Security Number
valid.SSN = /^\d{3}\-\d{2}\-\d{4}$/;

// Username
valid.Username = /^[a-zA-Z]+([a-zA-Z0-9])*$/;

// Password
valid.Password = /^[a-zA-Z0-9]+([a-zA-Z0-9\-])*$/;



function resetForm(theForm) {

    var elArr = theForm.elements;

    for(var i = 0; i < elArr.length; i++) {
	
	   // Check to see if the current field is required and non-empty
	   if (elArr[i].type != "submit" && elArr[i].type != "reset" && elArr[i].type != "hidden")
           document.all(elArr[i].name + "_err").innerHTML = " ";  
	}
	errMsg = "";
	
	return true;            
}

function validateForm(theForm) {

    var elArr = theForm.elements;
	var error_count = 0;

    for(var i = 0; i < elArr.length; i++) {
	   // Clear any flagged input fields
	   if (elArr[i].type != "submit" && elArr[i].type != "reset" && elArr[i].type != "hidden")
	       if (document.all(elArr[i].name + "_err")) 
               document.all(elArr[i].name + "_err").innerHTML = " ";                

	   var ReqField = elArr[i].required;
		   
	   if ((elArr[i].name).indexOf('Password') >= 0) {
		   var sDivBlock = "Password";
	   } else if ((elArr[i].name).indexOf('Address') >= 0) {
		   var sDivBlock = "Address";
	   } else
		   var sDivBlock = elArr[i].name;

	   // Check to see if the current field is required and non-empty
	   if ((typeof elArr[i].required != 'undefined') && (Trim(elArr[i].value).length == 0)) {
	             //alert(elArr[i].name + " is a required field."); 
				 document.all(elArr[i].name + "_err").innerHTML = "<a href=\"javascript:void(0);\" onClick=\"ShowValidationHelp('" + sDivBlock + "_Info');\">i</a>";
/*
	             elArr[i].select();
	             elArr[i].focus();
*/
				 errMsg += elArr[i].name + " is a required field.\n";
				 error_count++;
	   } else if (Trim(elArr[i].value != "") || (typeof elArr[i].required != 'undefined')) {

	       with(elArr[i]) { 
	
	          var v = elArr[i].validator; 
	
	          if(!v) continue; 
	
	          var thePat = valid[v]; 
//		document.write("Regular expression: " + thePat + "<br>value: " + value + "</p>");
	
	          var gotIt = thePat.exec(value); 
	
	          if(! gotIt){
				 document.all(elArr[i].name + "_err").innerHTML = "<a href=\"javascript:void(0);\" onClick=\"ShowValidationHelp('" + sDivBlock + "_Info');\">i</a>";
				 errMsg += name + ": failure to match " + v + " to " + value + "\n";
				 error_count++;
			  /*
	             alert(name + ": failure to match " + v + " to " + value);                  
	             elArr[i].select();
	             elArr[i].focus(); 
	             return false;
				 */
	          }
	       }
	   } /* The input value is not blank */
    }

	if (error_count == 0)
    return true;
else
             return false;
}

/*
 *   Compliments of http://www.quirksmode.org/js/mailcheck.html
 */
function Check_eMail()
{
	var x = document.forms[0].email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) alert('YES! Correct email address');
	else alert('NO! Incorrect email address');
}


/*
 *   Compliments of Steve Collymore
 */
// Utility function to trim spaces from both ends of a string
var lastDivBlock = "";
var HeaderBlock = "ValTitle";

function ShowValidationHelp(divBlock) {
    if (Trim(divBlock) != "") {
		document.all('ErrorMsg').innerHTML = document.all(divBlock).innerHTML;
	}
}

function ShowValidationHelp_Orig(divBlock) {
    if (lastDivBlock != "") {
		document.all(lastDivBlock).style.display = "none";
		document.all(HeaderBlock).style.display = 'none';
	}

    if (Trim(divBlock) != "") {
		document.all(HeaderBlock).style.display = 'block';
		document.all(divBlock).style.display = 'block';
		lastDivBlock = divBlock;
	}
}

// Expecting the form object for the Passwords
function AuthenticatePassword(Passwd, Verification) {
	//alert("Verification: " + Verification.name + "\nPassword: " + Passwd.value + "\nRe-entered Password: " + Verification.value + "\n");
	if (Passwd.value != Verification.value) {
		document.all(Passwd.name + "_err").innerHTML = "X";
		document.all(Verification.name + "_err").innerHTML = "X";
		return false;
	}
	return true;
}

function Trim(inString) {
  var retVal = "";
  var start = 0;
  
  if (inString == '\0') {
// alert("inString: " + inString);
     return retVal;
  }
  
  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
    ++start;
  }
  var end = inString.length;
  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
    --end;
  }
  if (start < end) {
     retVal = inString.substring(start, end);
  } else {
     // The entire string contains the blank character
  }
  return retVal;
}

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;
}

var submitted = 0;
function LockButton (form,element) {
   if ( ! submitted ) { 
       form.elements[element].disabled=true;
       submitted = 1;
//       form.submit;
//alert('You registration form is being submitted.\n\nThank you for taking the time to register!');
   }
   else { alert("Form already submitted, please be patient"); }
}


