// This is the function that performs form verification.  It will be invoked
// from the onSubmit() event handler.  The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all
    // text and textarea elements that do not have an "optional" property
    // defined.  Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
      var e = f.elements[i];

        if(e.name.indexOf("[]")>=0 )e.optional=true;
      if (((e.type == "text") || (e.type == "textarea" || e.type=="select-one")) && !e.optional && !e.std_chars) {
        // first check if the field is empty
        if ((e.value == null) || (e.value == "") || isblank(e.value)) {
            empty_fields += "\n          " + e.name ;
            continue;
        }

      }

      // check dates
      if(e.date){
            if(!e.optional || !e.value==""  || !e.value==0){
				if (!isDate(e.value)){
			    errors += "- The field " + e.name + " is not a valid date - "+e.value;
			    errors += ".\n";
            }

				}
      }
			
			// check email
      if(e.email){
            if(!e.optional || !e.value==""  || !e.value==0){
				if (!isStdChar(e.value) || e.value.indexOf("@",0)<0){
			    errors += "- " + e.name + " is not a valid email address - "+e.value;
			    errors += ".\n";
            }

				}
      }
			

      // check email
      if(e.std_chars){

        if (!isStdChar(e.value)){
         errors += "- The field " + e.name + " is not a valid user name - "+e.value;
			    errors += ".\n";
        }
      }

      // Now check for fields that are supposed to be numeric.
      if (e.numeric || (e.min != null) || (e.max != null)) {
      var v = parseFloat(e.value);
      if (isNaN(v) ||
      ((e.min != null) && (v < e.min)) ||
      ((e.max != null) && (v > e.max))) {
	    errors += "- The field " + e.name + " must be a number";
	    if (e.min != null)
		errors += " that is greater than " + e.min;
	    if (e.max != null && e.min != null)
		errors += " and less than " + e.max;
	    else if (e.max != null)
		errors += " that is less than " + e.max;
	    errors += ".\n";
      }
     }
    }

    // Now, if there were any errors, then display the messages, and
    // return true to prevent the form from being submitted.  Otherwise
    // return false
    if (!empty_fields && !errors) return true;
  	showError(empty_fields,errors);
    return false;
}
function showError(ef,e){
    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (ef) {
        msg += "- The following required field(s) are empty: "
                + ef + "\n";
        if (e) msg += "\n";
    }
    msg += e;
    alert(msg);

}


// ************* isDate **********
function isDate(d){                 //test if d is a date
        var dd;
        var mm;
        var yy;
        var leap = 28;
    if(d.indexOf("/")>=0){
          if (d.length != 10) return false;
        	if (d.charAt(2) != "/" || d.charAt(5) != "/") return false;
        	for (var i = 0; i < d.length; i++){
               if (i != 2 && i != 5 && (d.charAt(i) < "0" || d.charAt(i) > "9")) {
               return false;
           }
        	}
        	dd = d.substring(0,2);
        	mm = d.substring(3,5);
        	yy = d.substring(6,10);
        }
        else {
        	for (var i = 0; i < d.length; i++){
							if ((d.charAt(i) < "0" || d.charAt(i) > "9")&&d.charAt(i)!="-") {
								return false;
							}
        	}
        a=d.split("-");
        dd = a[2];
        	mm =a[1];
        	yy = a[0];
        }

      if (yy < 1900 || yy > 2099 || mm < 1 || mm > 12 || dd < 1 || dd > 31) return false;
     if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && (dd > 30)) return false;
     if (mm == 2 && (4*Math.floor(yy/4) == yy)) leap = 29;
     if (mm == 2 && dd > leap) return false;
        return true;
}

//************** isInt ***********
function isInt(i){                        // test that i is an integer
        i = i + "";
        for (var j = 0; j < i.length; i++){
                                        if (i.charAt(j) < "0" || i.charAt(j) > "9") return false;
        }
        return true;
}

//***************isStdChar*******  checks for letter or hyphen, dot or @
function isStdChar(s){

    for(var j= 0;j<s.length;j++){
        c=s.charAt(j);

        if(!((c>="A"&& c<="Z")||(c>="a" && c<="z")||(c>="0"&&c<="9")||c=="."||c=="@"||c=="-"||c=="_" ))return false;
    }
    return true;
}

//************** isFloat *********
function isFloat(f){                // test that f is float
        f = f + "";
        for (var j = 0; j < f.length; j++){
                                        if ((f.charAt(j) < "0" || f.charAt(j) > "9") && f.charAt(j) != ".") return false;
        }
        if (f.indexOf(".") != -1){
                 var ii = f.substring(f.indexOf(".") + 1, f.length);
                 if (ii.indexOf(".") != -1) return false;
        }
        return true;
}

//************** isLog *************
function isLog(l, t, f) {                // tests l for correct logical value
 if (l.toUpperCase() != t.toUpperCase() && l.toUpperCase() != f.toUpperCase()) return false;
        else return true;
}
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;
}

function wordCount(s){
    //s = trim(s);
    while(s.indexOf('  ')!=-1)s=s.replace('  ',' ');
    sa=s.split(" ");
    return sa.length ;


}




