/* formval.js */
var TYPEINTEGER         = 1;        // Integer 'min' defined
var TYPEDATE            = 2;        // Date value ("mm/dd/yyyy")
var TYPEDOUBLE          = 3;        // Double 'min' defined
var TYPETEXT            = 4;        // Text or TextArea
var TYPENUMBERRANGE     = 5;        // Integer or Double with a 'min' and 'max' defined
var TYPESELECT          = 6;        // Integer (Select Box value) 'min' defined
var TYPEZIPCODE         = 7;        // Text, exactly 5 digits, between '00000' and '99999'
var TYPECHECKBOX        = 8;        // Boolean
var TYPEPHONEAREACODE   = 9;        // Phone Number Area Code
var TYPEPHONEEXCHANGE   = 10;       // Phone Number Exchange
var TYPEPHONEEXTENSION  = 11;       // Phone Number Extension
var TYPEZIPEXT          = 12;       // Text, exactly 4 digits, between '0000' and '9999'
var TYPEEMAIL			= 13;		// Text, valid email address
 

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 isAllDigits(s,exactlen) 
    {
    if (s.length != exactlen) return false;
    for(var i = 0; i < s.length; i++)
        {
        var c = s.charAt(i);
        if ( c < '0' || c > '9') 
            return false;
        }
    return true;
    }

function ValidateDate(e) 
    {
    var errors = "";              
    var s = e.value;
    if (isblank(s) && CheckOptional(e) == "")
        return errors;

    // "mm/dd/yy" or "mm/dd/yyyy" format
    var msg = "- The field " + e.labelstr + " contains an invalid date.\n";
    var s = e.value;
    var a = s.split("/");
    var mm = parseInt(a[0]);
    var dd = parseInt(a[1]);
    var yy = parseInt(a[2]);
	var err = 1;

    if (!(isNaN(mm) || isNaN(dd) || isNaN(yy))) 
        {
        if ((yy > -1 && yy < 100) || (yy > 1899 && yy < 2100))
            err = 0;

        if (err) return (msg);
        err = 1;
        if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)
            {
            if (dd <= 31 && dd > 0) { err = 0; }
            }
        else if (mm == 2)
            {
            if (dd <= 28 && dd > 0) { err = 0; }
            }
        else if (mm == 4 || mm == 6 || mm == 9 || mm == 11)
            {
            if (dd <= 30 && dd > 0) { err = 0; }
            }
        }
    if (err) return(msg);
    else return("");
    }
    
        
function CheckOptional(e)
    {                
    var str = "";
    if (!parseInt(e.optional))
        {
        // first check if the field is empty
        if ((e.value == null) || (e.value == "") || isblank(e.value) || e.value.length == 0)
            {
            str += "\n           " + e.labelstr;
            }
        }
    return str;
    }


function ValidateNumberRange(e)
    {
    var errors = "";              
    var s = e.value;
    if (isblank(s) && CheckOptional(e) == "")
        return errors;
    if ((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.labelstr + " 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";                    
            }
        }
    return errors;
    }

function ValidateNumberMin(e)
    {
    var errors = "";              
    var s = e.value;
    if (isblank(s) && CheckOptional(e) == "")
        return errors;
        
    if (e.min != null) 
        {
        var v = parseFloat(e.value);
        if (isNaN(v) || ((e.min != null) && (v < e.min)))
            {
            errors += "- The field " + e.labelstr + " must be a number";
            if (e.min != null) 
                errors += " that is greater than " + e.min;
            errors += ".\n";                    
            }
        }
    return errors;
    }
    

function ValidateZipCode(e)
    {
	//alert("ValidateZipCode");
    var errors = "";              
    var s = e.value;

	//alert(CheckOptional(e))
    if (isblank(s) && CheckOptional(e).length != 0)
        return errors;

    if (!isAllDigits(s,5))
        {
        errors += "- The field " + e.labelstr + " must be exactly 5 digits";
        errors += " between 00000 and 99999.\n";
        }
    return errors;
    }

function ValidateZipCodeExt(e)
    {
    var errors = "";              
    var s = e.value;

    if (isblank(s) && CheckOptional(e) == "")
        return errors;

    if (!isAllDigits(s,4))
        {
        errors += "- The field " + e.labelstr + " must be exactly 5 digits";
        errors += " between 0000 and 9999.\n";
        }
    return errors;
    }

function ValidateDigits(e,exactlen)
    {
	//alert("ValidateDigits")
    var errors = "";              
    var s = e.value;

    if (isblank(s) && CheckOptional(e) == "")
        return errors;
    
    if (!isAllDigits(s,exactlen))
        {
        errors += "- The field " + e.labelstr + " must be exactly " + exactlen + " digits.\n";
        }
    return errors;
    }



function ValidateEmail(e)
    {
	// alert(e.value)
    var errors = "";              
    var s = e.value;
    var emailRegExp;
    
    if (isblank(s) && CheckOptional(e) == "")
        return errors;
    
	var emailRegExp = new RegExp("[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
	
	if (!emailRegExp.test(e.value))
		{
        errors += "- The field " + e.labelstr + " contains an invalid email address.\n";
        }
    return errors;
    }
    
function valid(f) {
    var msg;
    var empty_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.  Also, if any of these elements have a "min" or "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 empty_field = "";
        var err = "";
        var e = f.elements[i];
        if (e.type == "button") continue;
        switch (parseInt(e.fldtype))
            {
            
            case TYPEEMAIL:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateEmail(e);
					}
                break;

            case TYPEZIPEXT:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateZipCodeExt(e);
					}
                break;
            case TYPESELECT:
                empty_field = CheckOptional(e);
                break;
            case TYPEINTEGER:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateNumberMin(e);
					}
                break;
            case TYPEDATE:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err += ValidateDate(e);
					}
                break;
            case TYPEDOUBLE:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateNumberMin(e);
					}
                break;
            case TYPETEXT:
                empty_field = CheckOptional(e);
                break;
            case TYPEZIPCODE:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateZipCode(e);
					}
                break;
            case TYPEPHONEAREACODE:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateDigits(e,3);
					}
                break;
            case TYPEPHONEEXCHANGE:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateDigits(e,3);
					}
                break;
            case TYPEPHONEEXTENSION:
                empty_field = CheckOptional(e);
				if (e.value.length > 0)
					{
	                err = ValidateDigits(e,4);
					}
                break;
            }
        if (empty_field || err)
            {
            e.style.backgroundColor="orange";
            empty_fields += empty_field;
            errors += err;
            }
        else
			{
			;
            e.style.backgroundColor="white";
			}
        }
    /* Now, if there were any errors, display the messages, and 
       return fals to prevent the form from being submitted.
       Otherwise return true.
    */
    if (!empty_fields && !errors) return true;
/*
    msg = "Errors were detected. Erroneous input has been highlighted.\n";
    msg += "\nWould you like to see specific errors?\n";
    msg += "\nClick OK to see specific errors. Click Cancel to Return To Form.\n\n";
    msg += "_______________________________________________\n";
    msg += "This form validated by The Ultimate Form Validation Tool.\n";
    msg += "Copyright 2001, Gary Janecek.\n";

    var seemessages = window.confirm(msg);
    if (seemessages)
        {
        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;
	    msg += "_______________________________________________\n";
	    msg += "This form validated by The Ultimate Form Validation Tool.\n";
	    msg += "Copyright 2001, Gary Janecek.\n";
        alert(msg);
        }
*/
        msg = "";
        if (empty_fields)
            {
            msg += "- The following required field(s) are empty:" + empty_fields + "\n";
            if (errors) msg += "\n";
            }
        msg += errors;
	    msg += "\n\n_______________________________________________\n";
        msg += "The form was not submitted because of the error(s) above.\n";
	    msg += "Erroneous input has been highlighted.\n";
        msg += "Please correct these error(s) and re-submit.\n";
	    msg += "_______________________________________________\n";
	    msg += "This form validated by The Ultimate Form Validation Tool.\n";
	    msg += "Copyright 2001, Gary Janecek.\n";
        alert(msg);
    return false;
    }
        



