/*
<-----------=[ F.I.L.E  I.N.F.O ]=----------->

-Language: PHP 5.2.5
-Database: None
-Web Server: Apache 2.2.4
-IDE: NetBeans IDE Early Access for PHP
-OS: Windows 2000 SP4

-Author: Bijin Babu
-Filename: form_validation.php
-Description: Provides the form validation functionality to the quote module
-Created On: 30-Sep-2008
-Last Modified On: 06-Oct-2008

<-----------------=[ E.N.D ]=----------------->
 */

// flags for the mandatory entries...

var isValid = new Array();

isValid[0] = false;
isValid[1] = false;
isValid[2] = false;
isValid[3] = false;
isValid[4] = false;
isValid[5] = false;
isValid[6] = false;
isValid[7] = false;


function validate(e)
{
    var val = e.value;
    switch (e.id)
    {
        case "user_name":
            var result = isEmpty(val);
            document.getElementById(1).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(1).src="images/false.gif"',100);
                isValid[0] = false;
                return;
            }
            else
            {
                setTimeout('document.getElementById(1).src="images/true.gif"',100);
                isValid[0] = true;
                //e.style.background="#00CCFF";
            }
			
            break;
        case "country":
            result = isEmpty(val);
            document.getElementById(2).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(2).src="images/false.gif"',100);
                isValid[1] = false;
            }
            else
            {
                setTimeout('document.getElementById(2).src="images/true.gif"',100);
                isValid[1] = true;
            }
			
            break;
        case "organization":
            result = isEmpty(val);
            document.getElementById(3).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(3).src="images/false.gif"',100);
                isValid[2] = false;
            }
            else
            {
                setTimeout('document.getElementById(3).src="images/true.gif"',100);
                isValid[2] = true;
            }
			
            break;
                 
        case "city":
            result = isEmpty(val);
            document.getElementById(4).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(4).src="images/false.gif"',100);
                isValid[3] = false;
            }
            else
            {
                setTimeout('document.getElementById(4).src="images/true.gif"',100);
                isValid[3] = true;
            }
			
            break;
			
        case "email":
            result = isEmail(val);
            document.getElementById(5).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(5).src="images/true.gif"',100);
                isValid[4] = true;
            }
            else
            {
                setTimeout('document.getElementById(5).src="images/false.gif"',100);
                isValid[4] = false;
            }
			
            break;
                       
        case "state":
            result = isEmpty(val);
            document.getElementById(6).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(6).src="images/false.gif"',100);
                isValid[5] = false;
            }
            else
            {
                setTimeout('document.getElementById(6).src="images/true.gif"',100);
                isValid[5] = true;
                //e.style.background="#00CCFF";
            }
			
            break;
           
        case "street_address":
            result = isEmpty(val);
            document.getElementById(7).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(7).src="images/false.gif"',100);
                isValid[6] = false;
            }
            else
            {
                setTimeout('document.getElementById(7).src="images/true.gif"',100);
                isValid[6] = true;
                //e.style.background="#00CCFF";
            }
			
            break;
			
        case "zip_code":
            result = isNumeric(val);
            document.getElementById(8).src="images/loading.gif";
            if(result=="1")
            {
                setTimeout('document.getElementById(8).src="images/true.gif"',100);
                isValid[7] = true;
                //e.style.borderColor="#00FF00 #0000FF";
            }
            else
            {
                setTimeout('document.getElementById(8).src="images/false.gif"',100);
                isValid[7] = false;
            }
            break;
    }	
}

// checking wheather all the mandatory fields are filled before form submission...
function doSubmit()
{
    var i=0;
    
    for(i=1;i<=isValid.length;i++)
    {
        if(isValid[i]==false)
        {
            alert("Please fill the mandatory field(s) before submitting");
            return false;
        }
    }
    document.frm_quote.submit();
    return true;
}
                                

// returns true if the string is empty
function isEmpty(str){
    return (str == null) || (str.length == 0);
}

// returns true if the string is a valid email
function isEmail(str){
    if(isEmpty(str)) return false;
    var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
    return re.test(str);
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
    var re = /[^a-zA-Z]/g
    if (re.test(str)) return false;
    return true;
}

// returns true if the string only contains characters 0-9
function isNumeric(str){
    var re = /[\D]/g
    if (re.test(str)) return false;
    return true;
}

// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
    var re = /[^a-zA-Z0-9]/g
    if (re.test(str)) return false;
    return true;
}

// returns true if the string's length equals "len"
function isLength(str, len){
    return str.length == len;
}

// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
    return (str.length >= min)&&(str.length <= max);
}

// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
    var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
    return re.test(str);
}

function isMobileNumber(str){
    var re = /^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}98(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$/
    return re.test(str);
}

//Most Concise RegExp for matching Decimal IPs. If nothing else, it'll make your code easier to read. (And I know that \d?\d is \d{1,2} but that's 2 extra characters.) --Update: darkone noticed 8 characters could be shaved down. I've edited it to reflect this
function isIPAddress(str){
    var re = /\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b/
    return re.test(str);
}

/*Description  	
This will grep for a valid MAC address , with colons seperating octets. It will ignore strings too short or long, or with invalid characters. It will accept mixed case hexadecimal. Use extended grep.
Matches 	
01:23:45:67:89:ab | 01:23:45:67:89:AB | fE:dC:bA:98:76:54
Non-Matches 	
01:23:45:67:89:ab:cd | 01:23:45:67:89:Az | 01:23:45:56:*/
function isMacAddress(str){
    var re = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/
    return re.test(str);
}

//Checks for a valid windows file name (Must be used with the case-insensitive option Checks that the file has at lease one char, does not contain any invalid characters and does not have a reserved word as a file name. &quot;If you see a &amp;quot; in the regex replace it with a &quot; character&quot;
//Matches: test.txt | test.jpg.txt | a&b c.bmp Non-Matches: CON | .pdf | test:2.pdf



// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
    var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
    if (!re.test(str)) return false;
    var result = str.match(re);
    var m = parseInt(result[1]);
    var d = parseInt(result[2]);
    var y = parseInt(result[3]);
    if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
    if(m == 2){
        var days = ((y % 4) == 0) ? 29 : 28;
    }else if(m == 4 || m == 6 || m == 9 || m == 11){
        var days = 30;
    }else{
        var days = 31;
    }
    return (d >= 1 && d <= days);
}

// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
    return str1 == str2;
}

// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
    var re = /[\S]/g
    if (re.test(str)) return false;
    return true;
}

// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
    if (replacement == null) replacement = '';
    var result = str;
    var re = /\s/g
    if(str.search(re) != -1){
        result = str.replace(re, replacement);
    }
    return result;
}
