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

// 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.
//				 onSubmit="	this.phone.optional=true;
//							this.email_addr.email=true;
//							this.num.numeric=true;
//							return verify(this);"
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
	var goodEmail;
	
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't 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.
	// alert("In Verify Function");
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if ((e.type == "text") || (e.type == "textarea") || (e.type == "password")) {
			// alert("Is text or text area or password");
            // first check if the field is empty; if it is, we're going to skip it
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				// however, if its not optional, we're going to complain about it
				if (!e.optional) {
					// alert("Not optional!");
                	empty_fields += "\n          " + e.name;
				}
                continue;
            } 

            // Now check for (non-null) 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 check for e-mail fields that should contain an '@' sign
			if (e.email) {
				// the next line must be all on one line with no breaks
				// goodEmail = e.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
				goodEmail = e.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/);

				if (!goodEmail){
				   errors += "The email address you entered is not valid.\n";
				} 
			} 
        }  		  
    }

	// check for password match -- this only works if there are two password fields called
	// password_1 and password_2.
	// if (f.password_1.value != f.password_2.value) {
	// 	errors += "- The passwords you've entered do not match!\n";
	//	f.password_1.value = "";
	//	f.password_2.value = "";
	// }
	
    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    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 (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}

// This is the function that verifys that someone has entered either an email or a
// fax number. It returns "Email" if email address, "Fax" if fax number, or false if 
// neither
//				 onSubmit="	return EmailOrFax(this);"
function EmailOrFax(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
	var goodEmail;
	
	// alert("In EmailOrFax");
	
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't 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.
	// alert("In Verify Function");
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if ((e.type == "text") || (e.type == "textarea")) {
			// alert("Is text or text area");
            // first check if the field is empty; if it is, we're going to skip it
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				// however, if its not optional, we're going to complain about it
				if (!e.optional) {
					// alert("Not optional!");
                	empty_fields += "\n          " + e.name;
				}
                continue;
            } 

            // Now check for telephone number
            if (e.value.match(/^\(*(\d\d\d)\)*[\s\.-]*(\d\d\d)[\s\.-]*(\d\d\d\d)(\s)*$/)) {
				//alert("Is Fax");
				return "Fax";
			} else if (
e.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi) ) {
				//alert("Is Email");
				return "Email";
			} else {
				   errors += "You must enter either a valid email address or a valid phone number.\n";
			}
        }  		  
    }

    // Now, display the error messages, and
    // return false to prevent the form from being submitted. 

    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 (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}


function verify_delete_cat()
{
    msg  = "______________________________________________\n\n";
    msg += "You are about to delete a Catalog and all its \n";
    msg += "contents. You will not be able to undo this   \n";
    msg += "action. Please click 'OK' to confirm, 'Cancel'\n";
    msg += "to cancel.\n";
    msg += "______________________________________________\n\n";
	
	return (confirm(msg));
}


function wait_alert()
{
    msg  = "______________________________________________\n\n";
    msg += "About to upload catalog data . . .  \n\n";
    msg += "This action may take from a few seconds to a few minutes\n";
    msg += "depending on the speed of your connection, and the amount\n";
    msg += "of data. \n\n";
    msg += "Click 'OK' to continue and please do not resubmit.\n";
    msg += "______________________________________________\n\n";
	
	return (alert(msg));
}

function warnOnRegister()
{
    msg  = "______________________________________________\n\n";
    msg += "Verifying your registration data . . .  \n\n";
    msg += "This action may take a few minutes.\n";
    msg += "______________________________________________\n\n";
	
	return (alert(msg));
}

