/*****************************************************************************\

  SOPHIAKNOWS JAVASCRIPT VALIDATE

    CONTENTS:  Validator Subroutines
    AUTHOR:    Tony Pisarra
    CREATED:   2001-03-14
    MODIFIED:  2006-05-05
    COPYRIGHT: SophiaKnows.com

    This library collects client-side validation scripts that check for
    compliance with several common data formats as well as the presence
    contents in required fields. The library validator routines can be
    used to conduct both intermediate checks, as the user enters data, as
    well as a final validation when the user submits the form.

    Functionality requires a DOM enabled browser but will permit the normal
    form submit behavior for primitive and JavaScript-disabled browsers


	UPDATE (May 05 2006): Workaround added for IE6. Requires modification
	to HTML files and control/warning node id/names.
	
	
	The following revised convention is used when creating form-control 
	warning-node pairs (i.e. '_w' is appended to the control-name to
	create the warning-node id):
	
        <input name=example />
	
        <div id=example_w> </div>

    	<input name=example2 />
	
        <div id=example2_w> </div>
	
	
	

\*****************************************************************************/
/*
    VALIDATORS

    1. IsEmail(c,r)    -- is a valid address
    2. IsPhonenumber(c,r)    -- is a valid 10 digit phone number
    3. IsZip(c,r)    -- is a valid 5 or 9 digit ZIP code
    4. IsPlaintext(c,r) -- allow only numbers and english letters
    5. IsInteger(c,r)    -- is an integer
    6. IsNotHtml(c,r)    -- does not include markup
    7. IsSecure(c,r)    -- is a secure password
    8. IsEqual(c,m,r)    -- matches value of specified field
    9. IsSelected(c) -- a valid option is selected
   10. IsRequired(c) -- a required field has contents

    ARGUMENT KEY

    c = control-reference
    r = required (0|1)
    m = matched control reference


*/

// WARNING MESSAGE
var warning="There are one or more errors or omitted fields in the form. Please correct to continue";

// CHECK VALIDATION: REVIEW ERRORS BEFORE SUBMIT    
// Edit and Move to Head of Document containing form

function Validate(f) {
    if(!document.getElementById) { return true;}
    var errors=0;
    errors+=IsPlaintext(f.username,true);
    errors+=IsSecure(f.password,true);
    errors+=IsEqual(f.confirm,f.password,true);
    errors+=IsEmail(f.email,true);
//    errors+=IsPhonenumber(f.phone,false);
    errors+=IsRequired(f.address1);
    errors+=IsPlaintext(f.city,1);
    errors+=IsSelected(f.state);
    errors+=IsZip(f.zip,1);
    if(errors>0) alert (warning);
    return ((errors)? false : true);
    }


// VALIDATORS:

// VALIDATORS: IS EMAIL
function IsEmail(c,r) {
    if(r && IsRequired(c)) {return 1;}
    if (!c.value 
        || c.value.search(/^[\w-\.]+@[\w-\.]+\.\w+$/)>-1) {
        unflag_error(c," Unrecognized Format");
        return 0;
        } else {
            flag_error(c," Unrecognized Format");
            return 1;
            }
    }


// VALIDATORS: IS PHONENUMBER        
function IsPhonenumber(c,r) {
  return 0;
    if(r && IsRequired(c)) {return 1;}
    if (!c.value 
        || c.value.search(/\(?\d{3}\)?[ \.-]?\d{3}[ \.-]\d{4}/)>-1) {
        unflag_error(c," Unrecognized Format");
        return 0;
        } else {
            flag_error(c," Unrecognized Format");
            return 1;
            }
    }

// VALIDATORS: IS ZIP
function IsZip(c,r) {
    if(r && IsRequired(c)) {return 1;}
    if (!c.value 
        || c.value.search(/^\d\d\d\d\d(-\d\d\d\d)?$/)>-1) {
        unflag_error(c," Unrecognized Format");
        return 0;
        } else {
            flag_error(c," Unrecognized Format");
            return 1;
            }
    }

// VALIDATORS: IS PLAINTEXT
function IsPlaintext(c,r) {
    if(r && IsRequired(c)) {return 1;}
    if (c.value.search(/[^\w]/)>-1) {
        flag_error(c," Letters and numbers only");
        return 1;
        } else {
            unflag_error(c," Letters and numbers only");
            return 0;
            }
    }

// VALIDATORS: IS INTEGER
function IsInteger(c,r) {
    if(r && IsRequired(c)) {return 1;}
    if (c.value.search(/[^\d]/)>-1) {
        flag_error(c," Must be integer");
        return 1;
        } else {
            unflag_error(c," Must be integer");
            return 0;
            }
    }

// VALIDATORS: IS NOT HTML
function IsNotHtml(c,r) {
    if(r && IsRequired(c)) {return 1;}
    if (c.value.search(/<[^>]+>/)>-1) {
        flag_error(c," Markup not permitted");
        return 1;
        } else {
            unflag_error(c," Markup not permitted");
            return 0;
            }
    }

// VALIDATORS: IS SECURE
function IsSecure(c,r) {
    if(r && IsRequired(c)) {return 1;}
    if(c.value.search(/[A-Z]/)>-1 
        && c.value.search(/[\d]/)>-1
        && c.value.length>3) {
        unflag_error(c," Must include at least 4 characters, including 1 number and 1 capital");
        return 0;
        } else {
            flag_error(c," Must include at least 4 characters, including 1 number and 1 capital");
            return 1;
            }
    }

// VALIDATORS: IS EQUAL
function IsEqual(c,m,r) {
    if(r && IsRequired(c)) {return 1;}
    if(!c.value || c.value == m.value) {
        unflag_error(c," Doesn't match "+ m.name);
        return 0;
        } else {
            flag_error(c," Doesn't match "+ m.name);
            return 1;
            }
    }


// VALIDATORS: IS SELECTED
function IsSelected(c,r) {
    if(c.selectedIndex>0) {
        unflag_error(c," Required");
        return 0;
        } else {
            flag_error(c," Required");
            return 1;
            }
    }

// VALIDATORS: IS REQUIRED
function IsRequired(c) {
    if(c.value) { 
        unflag_error(c," Required");
        return 0;
        } else { 
            flag_error(c," Required");
            return 1;
            }
    }

// FLAG/UNFLAG ERRORS

// FLAG ERROR
function flag_error(field,msg) {
    if(document.getElementById) {
        var td=document.getElementById(field.name+'_w')
        td.className="alert";
        if(td.firstChild.nodeValue.indexOf(msg)<0) {
            td.firstChild.nodeValue+=msg;
            }
        }
    }

// UNFLAG ERROR
function unflag_error(field,msg) {
    if(document.getElementById) {
        var td=document.getElementById(field.name+'_w')
        td.className="alert";
        if(td.firstChild.nodeValue) td.firstChild.nodeValue=td.firstChild.nodeValue.replace(msg,"");
        }
    }
