/*
This script simply tests that required fields have had data entered withn them.  It does not validate the data within those fields.
Call is in this format:

 checkForm('formname','fieldname.text,Field Label,1||fieldname.radio,Field Label,1||fieldname.select,Field Label,1');

That's a "||"-delimited list for each field; within each of those, a ","-delimited list for fieldname (with a ".select" appended if it's a <select>, a ".radio" appended for <input type="radio">, a ".email" for email-address validation, and finally, ".text" for <input type="text"> fields), label name for error message, and a "1" if you can focus() on the field--otherwise pass a 0, the script will not attempt to auto-focus on the field after the error.

:: KKZ 07-Mar-02 :: kkz@foureyes.com ::
:: Updated KKZ 05-Jun-02 :: Added code for simple <select> and <input type="Radio"> validation ::
:: Updated KKZ 28-Aug-02 :: Added code for numeric validation (isNaN) ::
*/ 

function checkForm(formname,fldlist) {
  var formObj = document.forms[formname];
  var fields = fldlist.split("||");
  for(i=0;i<fields.length;i++) {
    var fieldUp = fields[i].split(",");
    var thisField = fieldUp[0].split(".");
    switch(thisField[1]) {
      case "select":
	      if(formObj[thisField[0]].options[formObj[thisField[0]].selectedIndex].value == "") {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
	      break;
      case "email":
	      if(!(/^[^@%*<> ]+@[^@%*<> ]{1,255}\.[^@%*<> ]{2,5}/.test(trim(formObj[thisField[0]].value)))) {
		   alert("The " + fieldUp[1] + " is an invalid email address.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
	      break;
      case "radio":
	      var isIt = false;
	      for(j=0;j<formObj[thisField[0]].length;j++) {
	        if(formObj[thisField[0]][j].checked) {
	          isIt=true;
	        }
	      }
	      if(!(isIt)) {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]][0].focus();
	        }
	        return false;
	      }
	      break;
      case "checkbox":
	      var isIt = false;
	      if(formObj[thisField[0]].checked) {
	        isIt=true;
	      }
	      if(!(isIt)) {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
	      break;
      case "numeric":
	      if(isNaN(formObj[thisField[0]].value)) {
	        alert("The " + fieldUp[1] + " must be numeric.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
				break;
      default: // AKA "text"
	      if(formObj[thisField[0]].value == "") {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
				break;
    }
  }
  // Everything's OK?  Then submit the form!!!
  return true;
  //frmSubmit(formname);  This was not working correctly in certain Netscape, Opera, Mac browsers
}

/*
This script is for validating the length of <textarea> fields.  Could probably be used with <input type="text"> fields if desired.
Call is in this format:
  <textarea ... onKeyUp="checkLength(256,'Meta Description',this)">

The first parameter is the max. number of characters, the second is the title the JS pop-up shows, and the last parameter refers to the object-name for the field itself.  In 99.9% of cases you should refer to it as "this" (no quotes, like shown above).

:: KKZ 15-Apr-02 ::
*/
function checkLength(size,title,field) {
  if(field.value.length>size) {
    alert("The " + title + " can be a maximum of " + size + " characters long.");
    field.value=field.value.substring(0,size)
  }
}

/*
This script allows for a generic submittal to be executed on any form.
The variable "pageSubmitted" keeps multiple form submittals at bay!

:: KKZ 06-Mar-02 ::
*/
var pageSubmitted = false;

function frmSubmit(formname) {
  if(!(pageSubmitted)) {
    pageSubmitted = true;
    document.forms[formname].submit();
  }
}
/* 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){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


function writeCookie(name, value, hours)

{

  var expire = "";

  if(hours != null)

  {

    expire = new Date((new Date()).getTime() + hours * 3600000);

    expire = "; expires=" + expire.toGMTString();

  }

  document.cookie = name + "=" + escape(value) + expire;

}

// Example:

// alert( readCookie("myCookie") );

function readCookie(name)

{

  var cookieValue = "";

  var search = name + "=";

  if(document.cookie.length > 0)

  { 

    offset = document.cookie.indexOf(search);

    if (offset != -1)

    { 

      offset += search.length;

      end = document.cookie.indexOf(";", offset);

      if (end == -1) end = document.cookie.length;

      cookieValue = unescape(document.cookie.substring(offset, end))

    }

  }

  return cookieValue;

}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validateAmount(amount){
	if(amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
		return true;
	}else{
		alert('You must enter a valid donation.');
		amount.focus();
		return false;
	}
}
