﻿// Validation Scripts
// Developed by Chris Richards 2006

function check_email( id ) {
    //Get the Element
    $elm = $( "#"+id );
    
    //Now check it with Regexp
    reg = /^\w+([0-9\.+-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
    
    //Is it Valid?
    if( reg.test( $elm.val() ) ) {
        //Mark it as OK
        $elm.addClass( "ok" ).removeClass( "error" );
        return true;
    } else {
        //Red for the goof-up
        $elm.addClass( "error" ).removeClass( "ok" );
        //Tell themm about it
        alert( "The E-mail you entered is not valid" );
        return false;
    }
    
}
        
function check_passwords( $id1, $id2 ) {
    //Get the two passwords
    $pass1 = $( "#" + $id1 );
    $pass2 = $( "#" + $id2 );
    
    //Are they the same?
    if( $pass1.val() == $pass2.val() ) {
        $pass1.addClass( "ok" ).removeClass( "error" );
        $pass2.addClass( "ok" ).removeClass( "error" );
        
        //Are they strong enough?
        if( TestStrength( $pass1.val() ) >= required_password_level ) {
            return true;
        } else {
            $pass1.addClass( "error" ).removeClass( "ok" );
            $pass2.addClass( "error" ).removeClass( "ok" );
            alert( "Password is too weak" );
            return false;
        }
    } else {
        //Oops 
        $pass1.addClass( "error" ).removeClass( "ok" );
        $pass2.addClass( "error" ).removeClass( "ok" );
        alert( "Oops Your Passwords Don't Match" );
        return false;
    }
}

//This will check to make sure they filled out everything nessary to update their informaiton
function check_update(){
    //Inputs that must exist
    var ok = true;
    $("#form input").each(function(){
        if( $(this).val() == "" ) {
            $(this).addClass("error");
            //Not ok
            ok = false;
        }
    });
    
    if( ok == true ) {
        return true;
    }else {
        return false;
    }
}

function TestStrength(val){
    //No Blanks
    if( val == "" ) { return -1; }
    //Test the passwords strength
    val					= val.replace(/(^\s+)|(\s+$)/g, "");
    var cNbr = cCap = cMin = cPct = cSpe = 1;
    var len				= val.length;
    for(var c = 0; c < len; c++){
	    var char		= val.charCodeAt(c);
	    if(char < 128){ if(char > 47 && char < 58){ cNbr += 1; }else if(char > 64 && char < 91){ cCap += 1; }else if(char > 96 && char < 123){ cMin += 1; }else{ cPct += 2;} }else{ cSpe += 3; }
    }
    var lPsw			= (cNbr * cCap * cMin * cPct * cSpe);
    lPsw				= Math.round(Math.log((lPsw * lPsw)));
	
    //Return our findings
    if( lPsw < 5 ) {
        return 0;   //Low
    } else if( lPsw < 10 ) {
        return 1;   //Normal
    } else if( lPsw >= 10 ){
        return 2;   //Strong
    } else {
        return -1;  //Error
    }
}
