//input (fieldname, keystroke, dec) dec is optional if decimals are allowed
function numbersonly(myfield, e, dec) { 
	var key; 
	var keychar; 
	if (window.event) 
		key = window.event.keyCode; 
	else if (e) 
		key = e.which; 
	else 
		return true; 

	keychar = String.fromCharCode(key); 
	// control keys 
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) 
		return true; 
	// numbers 
	else if ((("0123456789").indexOf(keychar) > -1)) 
		return true; 
	// decimal point jump 
	else if (dec && (keychar == ".")) { 
		myfield.form.elements[dec].focus(); 
		return false; 
	} 
	else 
		return false; 
} 
// 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 maxlength_fields = "";
    var errors = "";

    // 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.
    // Then, check for fields that have exceeded thier max char size. 
    // 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.elements.length; i++) {
        var e = f.elements[i];
        if ((e.type == "text") || (e.type == "textarea")) {
                // first check if the field is empty
                if ( e.required ) {
                    if ((e.value == null) || (e.value == "")) {
                    empty_fields += "\n          " + e.name;
                    continue;
                    }
                }

                // Now check for fields that have exceeded thier max char size.
                if (e.maxlength > 0) {
                    if (e.value.length > e.maxlength) {
                        var chars_over = e.value.length - e.maxlength
                        maxlength_fields += "\n          " + e.name + " : " + chars_over + " characters over.";
                        continue;
                    }
                }

            // 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))) && (e.value != "")) {
                    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";
                }
            }

            if (e.date) {
                var err = Date_Validator( e.value );
                if ( err != "" ) {
                    errors += "\n- The field " + e.name;
                    errors += " must be a valid";
                    errors += " date.\n";
                    errors += "          " + err;
                    errors += ".\n";
                }
            }
        }
    }
	

    // Now, if there were any errors, then display the messages, and
    // return false to prevent the form from being submitted. Otherwise
    // return true.

    if (!empty_fields && !maxlength_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";
    }

    if (maxlength_fields) {
        msg += "- The following field(s) have exceeded their max size:" 
                + maxlength_fields + "\n";
        if (errors) msg += "\n";
    }

    msg += errors;
    alert(msg);
    return false;
}

function checkBlank(ctrl,ctrlName)
{
	if(ctrl.value.length == 0)
	{
		alert(ctrlName + ' cannot be blank');
		ctrl.focus();
		return false;
	}
	return true;
}

function validateNL()
{
	if(!checkBlank(document.frmnewsletter.fname, 'First Name'))
	{
		return false;
	}
	
	if(!checkBlank(document.frmnewsletter.email, 'Email'))
	{
		return false;
	}
	else if(document.frmnewsletter.email.value.indexOf("@") == -1 || document.frmnewsletter.email.value.indexOf(".") == -1)
	{
		alert('Invalid email address');
		document.frmnewsletter.email.focus();
		return false;
	}
	
	return true;
}

function validateRF()
{
	if(!checkBlank(document.frmrf.fname, 'First Name'))
	{
		return false;
	}
	
	if(!checkBlank(document.frmrf.email, 'Email'))
	{
		return false;
	}
	else if(document.frmrf.email.value.indexOf("@") == -1 || document.frmrf.email.value.indexOf(".") == -1)
	{
		alert('Invalid email address');
		document.frmrf.email.focus();
		return false;
	}
	
	return true;
}
