﻿/*
Shejoy Cherian : 12th March 2009

Validations.js : Common validations javascript file. This file should contain common functions that all product
sites can use, instead of duplicating the same logic in different places.
    
This file should grow on demand and there should be an update log in Sharepoint so developers
are aware of what logic it contains.

/* Validation Functions */


var oAccents = {
    //accented characters cleanup routine
    A: /[ÁÀÂÄǍĂĀÅĄẮẰẴẲẠẬẶÃ]/g,
    a: /[áàâäǎăāåąắằẵẳạậặã]/g,
    AE: /[ÆǼ]/g,
    ae: /[æǽ]/g,
    OE: /[Œ]/g,
    oe: /[œ]/g,
    B: /[Ḃ]/g,
    b: /[ḃ]/g,
    C: /[ĆĊÇ]/g,
    c: /[ćċç]/g,
    D: /[ḊḌ]/g,
    d: /[ḋḍ]/g,
    E: /[ÉÈĖÊËĚĔĒĘỆ]/g,
    e: /[éèėêëěĕēęệ]/g,
    F: /[Ḟ]/g,
    f: /[ḟ]/g,
    G: /[ǴĠĞ]/g,
    g: /[ǵġğ]/g,
    I: /[ÍÌÎÏǏĬĪĮ]/g,
    i: /[íìîïǐĭīį]/g,
    K: /[ḰĶ]/g,
    k: /[ḱķ]/g,
    L: /[ĹĻḶ]/g,
    l: /[ĺļḷ]/g,
    M: /[ḾṀ]/g,
    m: /[ḿṁ]/g,
    N: /[ŃŅÑ]/g,
    n: /[ńņñ]/g,
    O: /[ÓÒÔÖǑŎŌǪŐǾỌỘỢØ]/g,
    o: /[óòôöǒŏōǫőǿọộợø]/g,
    P: /[ṔṖ]/g,
    p: /[ṕṗ]/g,
    R: /[ŔṚ]/g,
    r: /[ŕṛ]/g,
    S: /[ŚṠŞṢß]/g,
    s: /[śṡşṣß]/g,
    T: /[ṪṬ]/g,
    t: /[ṫṭ]/g,
    U: /[ÚÙÛÜǓŬŪŮŲŰǕỤỰ]/g,
    u: /[úùûüǔŭūůųűǖụự]/g,
    W: /[ẂẄ]/g,
    w: /[ẃẅ]/g,
    Y: /[ÝỲỴȲ]/g,
    y: /[ýỳỵȳ]/g,
    Z: /[ŹŻẒ]/g,
    z: /[źżẓ]/g
};

// Checks for invalid charecters
// Returns False if validation Fails

function ValidateTextFields(fieldId){
    var field = document.getElementById(fieldId);
    var pattern = /[0123456789!@#$%\^&*()_=+[\]{}\\|;:"/<>`~\xc2\xaa\xc2\xa3\xc2\xa2\xc2\xb0\xc2\xa4\xc2\xa6\xc2\xa5\xc2\xbb\xc2\xbc\xc3\x90\xc2\xad\xc3\x9e\xc2\xb1\xc2\xb2\xc2\xb3\xc2\xb7\xc2\xa8\xc2\xb9\xc2\xab\xc2\xac\xc2\xb8\xc2\xb6\xc2\xa9\xc3\xb0\xc2\xbd\xc2\xba\xc2\xb5\xc3\xbc\xc3\xbe\xc3\x97]/i;
    var retFlag, field, defaultVal, val;
    val = field.value || false;
    // if field have invalid chars, return true
    if(field != null && field.value.match(pattern) != null){
        return false;
    }
    // all is well
    return true;
}


function RemoveAccentedChars(str){
    //var oAccents = oAccents;
	for(prop in oAccents){
	    str = str.replace(oAccents[prop], prop);
	}
	return str;
}


function ValidateEmailFields(fieldId){
    var field = document.getElementById(fieldId);
    var pattern = /^[a-z0-9_\-\.\+]+@[a-z0-9]+[a-z0-9_\-\.]*\.[a-z]{2,}$/i;
     val = field.value || false;
    if(field.value.match(pattern) != null)
    {
        return true;
    }
    else{
        return false;
    }
}


function ValidateTextFieldsWithArgs(args,e){
    //var field = document.getElementById(fieldId);
 
    var pattern = /[$%\^&*()_=+[\]{}|;"<>`~\xc2\xaa\xc2\xa3\xc2\xa2\xc2\xb0\xc2\xa4\xc2\xa6\xc2\xa5\xc2\xbb\xc2\xbc\xc3\x90\xc2\xad\xc3\x9e\xc2\xb1\xc2\xb2\xc2\xb3\xc2\xb7\xc2\xa8\xc2\xb9\xc2\xab\xc2\xac\xc2\xb8\xc2\xb6\xc2\xa9\xc3\xb0\xc2\xbd\xc2\xba\xc2\xb5\xc3\xbc\xc3\xbe\xc3\x97]/i;
    var retFlag, field, defaultVal, val;
    val = e || false;
       // if field have invalid chars, return true
        if(e.Value != null && RemoveAccentedChars(e.Value).match(pattern) != null){
            e.IsValid=false;    
    }   
}

function ValidatePhoneFieldsWithArgs(args,e){
    //var field = document.getElementById(fieldId);
    
    var pattern = /[!$%\^&*()_=+[\]{}\\|;:"/<>`~\xc2\xaa\xc2\xa3\xc2\xa2\xc2\xb0\xc2\xa4\xc2\xa6\xc2\xa5\xc2\xbb\xc2\xbc\xc3\x90\xc2\xad\xc3\x9e\xc2\xb1\xc2\xb2\xc2\xb3\xc2\xb7\xc2\xa8\xc2\xb9\xc2\xab\xc2\xac\xc2\xb8\xc2\xb6\xc2\xa9\xc3\xb0\xc2\xbd\xc2\xba\xc2\xb5\xc3\xbc\xc3\xbe\xc3\x97]/i;
    var retFlag, field, defaultVal, val;
    val = e || false;
       // if field have invalid chars, return true
        if(e.Value !== null && RemoveAccentedChars(e.Value).match(pattern) !== null){
            e.IsValid=false;    
    }   
}


function ValidateEmptyString(args,e){
//debugger;
    //args.parentElement.parentElement.className = 'normalColor';
    args.parentNode.parentNode.className = 'normalColor';
    if(ValidatorTrim(e.Value) == ""){
        e.IsValid=false;
        //parent is TD and parent's parent is TR, make TR red is there is an error
        //args.parentElement.parentElement.className = 'error';
        args.parentNode.parentNode.className = 'error';
        return;
    }
}

function ValidateEmptyList(args,e){
    args.parentNode.parentNode.className = 'normalColor';
    if(e.Value == -1){
        e.IsValid=false;
        //parent is TD and parent's parent is TR, make TR red is there is an error
        args.parentNode.parentNode.className = 'error';
        args.parentNode.className = 'normalColor';
        return;
    }
}


function ValidateEmptyRadioList(args,e){
//debugger;
    args.parentNode.parentNode.className = 'normalColor';
    if(e.Value == ""){
        e.IsValid=false;
        //parent is TD and parent's parent is TR, make TR red is there is an error
        args.parentNode.parentNode.className = 'error';
        args.parentNode.className = 'normalColor';
        return;
    }
}


function ValidateEmailFieldsWithArgs(args,e){
    //var field = document.getElementById(fieldId);
    var pattern = /^[a-z0-9_\-\.\+]+@[a-z0-9]+[a-z0-9_\-\.]*\.[a-z]{2,}$/i;
     val = e || false;
    if(e.Value.match(pattern) != null)
    {   
        e.IsValid=true;
    }
}

function DateValidatorOnChange(obj,val){
//debugger;
    controltovalidateIDs = val.controlstovalidate.split(',');
    initialValues = val.initialvalue.split(',');
    if(obj.selectedIndex == 0 )
    {
        val.style.display = "inline";
        if(val.customClientFunction != "")
        {
            EvalFunction(val.customClientFunction,controltovalidateIDs[0],false)
        }
    }
    else{
        for(var controltovalidateIDIndex=0;controltovalidateIDIndex < controltovalidateIDs.length;controltovalidateIDIndex++) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if(initialValues.length>1)
                {
                    init = initialValues[controltovalidateIDIndex];
                }
                if (ValidatorTrim(ValidatorGetValue(controlID)) == init) {
                    return;
                } 
            }
            val.style.display = "none";
            if(val.customClientFunction != ""){
                        EvalFunction(val.customClientFunction,controltovalidateIDs[0],true)
                    }
    }
}


function EvalFunction(clientFunction,args,isValid)
{
    //alert("[DEBUG - REMOVE THIS ALERT]\n\ncf: " + clientFunction + " args: " + args + " valid: " + isValid);

    var funcCall = clientFunction + "('" + args + "'," + isValid + ");";
        eval(funcCall);
}

// this is using for multifield validation
function MultipleFieldsValidatorEvaluateIsValid(val) { 
//debugger;
    controltovalidateIDs = val.controlstovalidate.split(',');
    initialValues = val.initialvalue.split(',');
    var init = val.initialvalue;
    if(val.customClientFunction != "")
    {
        EvalFunction(val.customClientFunction,controltovalidateIDs[0],true)
    }
    switch (val.condition) {
        case 'OR':
            for(var controltovalidateIDIndex = 0;controltovalidateIDIndex < controltovalidateIDs.length;controltovalidateIDIndex++) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if(initialValues.length>1)
                {
                    init = initialValues[controltovalidateIDIndex];
                }                
                if (ValidatorTrim(ValidatorGetValue(controlID)) != init) {
                    val.style.display = "none";
                    return true;
                } 
            }
            val.style.display = "inline";
            if(val.customClientFunction != ""){
                EvalFunction(val.customClientFunction,controltovalidateIDs[0],false)
            }
            return false;
        break;
        case 'XOR':
            for(var controltovalidateIDIndex =0;controltovalidateIDIndex < controltovalidateIDs.length;controltovalidateIDIndex++ ) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if(initialValues.length>1)
                {
                    init = initialValues[controltovalidateIDIndex];
                }
                if (controltovalidateIDIndex == '0') {
                    var previousResult = !(ValidatorTrim(ValidatorGetValue(controlID)) == init);
                    continue;
                }
                var currentResult = !(ValidatorTrim(ValidatorGetValue(controlID)) == init);
                if (currentResult != previousResult) {
                    val.style.display = "none";
                    return true;
                }
                previousResult != currentResult;
            }
            val.style.display = "inline";
            if(val.customClientFunction != ""){
                EvalFunction(val.customClientFunction,controltovalidateIDs[0],false)
            }
            return false;
        break;
        case 'AND':
            for(var controltovalidateIDIndex=0;controltovalidateIDIndex < controltovalidateIDs.length;controltovalidateIDIndex++) {
                var controlID = controltovalidateIDs[controltovalidateIDIndex];
                if(initialValues.length>1)
                {
                    init = initialValues[controltovalidateIDIndex];
                }
                if (ValidatorTrim(ValidatorGetValue(controlID)) == init) {
                    val.style.display = "inline";
                    if(val.customClientFunction != ""){
                        EvalFunction(val.customClientFunction,controltovalidateIDs[0],false)
                    }
                    return false;
                } 
            }
            val.style.display = "none";
            return true;
        break;
    }
    return false;
}

function ErrorDisplay(args,isValid){
//debugger;
    var args = document.getElementById(args);
    args.parentNode.parentNode.className = '';
    if(!isValid){
        args.parentNode.parentNode.className = 'error';
        args.parentNode.className = 'normalColor';
        return;
    }
}

function DateValidatorIsValidDate(val) {
//debugger;
var dayval = ValidatorTrim(ValidatorGetValue(val.day));
var monval = ValidatorTrim(ValidatorGetValue(val.month));
var yrval = ValidatorTrim(ValidatorGetValue(val.year));
//var req = false;
//var isValid = false;

//if(val.required!=null)
//{
//    req = val.required;
//}
val.style.display = "none";
if(dayval!=val.dayinitialvalue && monval != val.monthinitialvalue && yrval != val.yearinitialvalue)
    {
        var DateVal = monval + "/" + dayval+ "/" + yrval;
        var dt = new Date(DateVal);

        if (dt.getDate() != dayval) {
            val.style.display = "inline";
            return (false);
        }
        else if (dt.getMonth() != monval-1) {
            //this is for the purpose JavaScript starts the month from 0
            val.style.display = "inline";
            return (false);
        }
        else if (dt.getFullYear() != yrval) {
            val.style.display = "inline";
            return (false);
        }
        
        val.style.display = "none";
        return (true);
    }
    return true;
//if(val.customClientFunction != "")

//    var clientFunction = val.customClientFunction;
//    var argObj = val.day;
//    var argisValid = true;
//    var funcCall = clientFunction + "('" + argObj + "'," + argisValid + ");";
//    eval(funcCall);
//}
// if(dayval==val.dayinitialvalue && monval == val.monthinitialvalue && yrval == val.yearinitialvalue)
//    {
//        if(req)
//        {
//            val.style.display = "none";
//            if(val.customClientFunction != "")
//            {
//                var clientFunction = val.customClientFunction;
//                var argObj = val.day;
//                var argisValid = false;
//                var funcCall = clientFunction + "('" + argObj + "'," + argisValid + ");";
//                eval(funcCall);
//            }
//            return false;
//         }
//         else
//         {
//            val.style.display = "none";
//            return true;
//         }   
//    }
//    else if(dayval!=val.dayinitialvalue && monval != val.monthinitialvalue && yrval != val.yearinitialvalue)
//    {
//        var DateVal = monval + "/" + dayval+ "/" + yrval;
//        var dt = new Date(DateVal);

//        if (dt.getDate() != dayval) {
//            val.style.display = "inline";
//            return (false);
//        }
//        else if (dt.getMonth() != monval-1) {
//            //this is for the purpose JavaScript starts the month from 0
//            val.style.display = "inline";
//            return (false);
//        }
//        else if (dt.getFullYear() != yrval) {
//            val.style.display = "inline";
//            return (false);
//        }
//        
//        val.style.display = "none";
//        return (true);
//    }
//    else
//    {
//        if(req)
//        {
//            val.style.display = "none";
//            if(val.customClientFunction != "")
//            {
//                var clientFunction = val.customClientFunction;
//                var argObj = val.day;
//                var argisValid = false;
//                var funcCall = clientFunction + "('" + argObj + "'," + argisValid + ");";
//                eval(funcCall);
//            }
//            return false;
//         }
//         else
//         {
//            val.style.display = "inline";
//            return false;
//         }   
//    }
}

//For Price Quotation
function ValidateEmptyListPC(args,e){
//debugger;
    //args.parentNode.className = 'normalColor';
    if(e.Value == -1){
        e.IsValid=false;
        //args.parentNode.className = 'error';
        //args.parentNode.className = 'normalColor';
        return;
    }
}

function scrollToTop()
    {
//    alert(Page_IsValid);
//        if(Page_IsValid)
//        {
//        alert(document.body.scrollTop);
            scroll(0,0);
//        }
    }
    
    
    function ClickOnce(myButton,span){
// Client side validation
if (typeof(Page_ClientValidate) == 'function') {
if (Page_ClientValidate() == false)
{ 

return false; 
}
}

// disable the button
myButton.style.display='none';
var cli=document.getElementById(span);
if(cli)
{
cli.innerHTML = myButton.innerHTML;
cli.style.display='block';
}
return true;
}

