﻿//------------------------------------
//
//@Author	Jim Sheridan - Infotekka - http://www.infotekka.com
//@Date		23 March, 2009
//@Version	Release 2
//@Known_Issues	Email addresses do not correctly check for dot syntax in domain
//
//------------------------------------

/* 
tk_text
tk_posint
tk_anynum
tk_currency
tk_email
tk_match([id of object to match])
tk_dropdown([value that is not valid])etc)
tk_email
tk_chars([integer number of minimum characters])
tk_phone([delimeter to be inserted after validation])
*/

function tk_fSetFuncs() { //use a special class on the form to find text fields that will use this validation
    var objInputs = document.getElementsByTagName("input");
    for (x = 0; x <= objInputs.length - 1; x++) {
        var strClass = objInputs[x].getAttribute("class");
        if (strClass == null) { strClass = objInputs[x].getAttribute("className"); } //IE
        try {
            switch (objInputs[x].getAttribute("type")) {
                case "text":case "password":
                    if (strClass.toLowerCase().match("tk_text")) {
                        objInputs[x].onblur = function() { return tk_fValidate("text", this); }
                    } else if (strClass.toLowerCase().match("tk_posint")) {
                        objInputs[x].onblur = function() { return tk_fValidate("posint", this); }
                    } else if (strClass.toLowerCase().match("tk_anynum")) {
                        objInputs[x].onblur = function() { return tk_fValidate("anynum", this); }
                    } else if (strClass.toLowerCase().match("tk_currency")) {
                        objInputs[x].value = tk_fSetDecimal(objInputs[x].value)
                        objInputs[x].onblur = function() { return tk_fValidate("currency", this); }
                    } else if (strClass.toLowerCase().match("tk_email")) {
                        objInputs[x].onblur = function() { return tk_fValidate("email", this); }
                    } else if (strClass.toLowerCase().match("tk_match")) {
                        objInputs[x].onblur = function() { return tk_fValidate("match", this); }
                    } else if (strClass.toLowerCase().match("tk_dropdown")) {
                        objInputs[x].onblur = function() { return tk_fValidate("dropdown", this); }
                    } else if (strClass.toLowerCase().match("tk_chars")) {
                        objInputs[x].onblur = function() { return tk_fValidate("chars", this); }
                        objInputs[x].onkeyup = function() { tk_fValidate("chars", this); }
                    } else if (strClass.toLowerCase().match("tk_phone")) {
                        objInputs[x].onblur = function() { return tk_fValidate("phone", this); }
                    }
                    break;
                case "submit":
                    if (strClass.toLowerCase().match("tk_submit")) {
                        objInputs[x].onclick = function() {
                            var blnVal = new Boolean(true);
                            var objInputs = document.getElementsByTagName("input");
                            for (x = 0; x <= objInputs.length - 1; x++) { if (objInputs[x].getAttribute("type") == "text" || objInputs[x].getAttribute("type") == "password") { try { if (objInputs[x].onblur() == false) { blnVal = false; } } catch (e) { } } }
                            objInputs = document.getElementsByTagName("select");
                            for (x = 0; x <= objInputs.length - 1; x++) { try { if (objInputs[x].onblur() == false) { blnVal = false; } } catch (e) { } }
                            this.focus();
                            return blnVal;
                        }
                        objInputs[x].disabled = false;
                    }
                    break;
            }
        } catch (e) {}
    }
    objInputs = document.getElementsByTagName("select");
    for (x = 0; x <= objInputs.length - 1; x++) {
        var strClass = objInputs[x].getAttribute("class");
        if (strClass == null) { strClass = objInputs[x].getAttribute("className"); } //IE
        try {
            if (strClass.toLowerCase().match("tk_dropdown")) {
                objInputs[x].onblur = function() { return tk_fValidate("dropdown", this); }
            }
        } catch (e) {}
    }
}

function tk_fSetDecimal(obj) { //Take a value and round it up to two decimal places then display as currency
    obj = (Math.round(obj * 100) / 100);
    if (Math.floor(obj) == 0) {
        obj = (obj * 1);
    }
    if (obj == Math.floor(obj)) {
        obj = (obj * 1) + ".00";
    }
    else if (obj * 10 == Math.floor(obj * 10)) {
        obj = (obj * 1) + "0";
    }
    if (isNaN(obj)) {
        obj = "0.00";
    }
    return obj;
}

function tk_fTrim(strTrim) { return strTrim.replace(/^\s+/, "").replace(/\s+$/, ""); }

function tk_fValidate(strType, objVal) {
    blnRet = true;
    var strMatCls = objVal.getAttribute("class");
    if (strMatCls == null) { strMatCls = objVal.getAttribute("className"); } //IE
    try {
        var objTest = objVal.parentNode.childNodes;
        for (t = 0; t < objTest.length; t++) {
            if (objTest[t].id == "tkInvalid") {
                objVal.parentNode.removeChild(objTest[t])
                break;
            }
        }
    } catch (e) { }
    switch (strType) {
        case "text":
            if (objVal.value == null || objVal.value == "") {
                tk_fAddAlert(objVal, "Field cannot be empty.");
                blnRet = false;
            }
            break;
        case "posint":
            if (isNaN(objVal.value) || objVal.value == null || objVal.value == "" || objVal.value < 0) {
                tk_fAddAlert(objVal, "Field must be a non-negative integer value.");
                blnRet = false;
            } else {
                objVal.value = Math.round(objVal.value * 1);
            }
            break;
        case "anynum":
            if (isNaN(objVal.value) || objVal.value == null || objVal.value == "") {
                tk_fAddAlert(objVal, "Field must be a number.");
                blnRet = false;
            }
            break;
        case "currency":
            objVal.value = tk_fSetDecimal(objVal.value);
            break;
        case "email":  //TODO: This needs work
            var rxEmail = new RegExp("[a-z, 0-9]@[a-z, 0-9]\.[a-z, 0-9]", "i");
            if (!rxEmail.test(objVal.value) || objVal.value == "") {
                tk_fAddAlert(objVal, "Field must be a valid email address.");
                blnRet = false;
            }
            break;
        case "match":
            if (objVal.value != document.getElementById(strMatCls.split("(")[1].split(")")[0]).value) {
                tk_fAddAlert(objVal, "Fields do not match.");
                blnRet = false;
            }
            break;
        case "dropdown":
            if (objVal.value == strMatCls.split("(")[1].split(")")[0] || objVal.value == "") {
                tk_fAddAlert(objVal, "Please make a selection.");
                blnRet = false;
            }
            break;
        case "chars":
            if (tk_fTrim(objVal.value).length < strMatCls.split("(")[1].split(")")[0]) {
                tk_fAddAlert(objVal, "Please enter at least " + strMatCls.split("(")[1].split(")")[0] + " characters");
                blnRet = false;
            }
            break;
        case "phone":
            if (tk_fTrim(objVal.value) == "") {
                tk_fAddAlert(objVal, "Please enter a phone number");
                blnRet = false;
                break;
            }
            var rxPhone = new RegExp("[0-9]", "g");
            var strRX = new String("");
            var strPhone = new String("");
            do {
                strRX = rxPhone.exec(objVal.value);
                if (strRX == null) { if (strPhone.length < 10) { strPhone = "noMatch"; } } else { strPhone += strRX; }
            } while (strRX != null);
            if (!isNaN(strPhone)) { if (strPhone.length >= 11 && strPhone.slice(0, 1) == "1") { strPhone = strPhone.slice(1, 12); } else { strPhone = strPhone.slice(0, 11); } }
            if (strPhone == "noMatch") {
                tk_fAddAlert(objVal, "Please enter a valid 10 digit phone number");
            } else {
                try {
                    var d = new String(strMatCls.split("(")[1].split(")")[0]);
                } catch (e) {
                    d = "";
                }
                objVal.value = strPhone.slice(0, 3) + d + strPhone.slice(3, 6) + d + strPhone.slice(6, 10);
            }
            break;
    }
    return blnRet;
}

function tk_fAddAlert(objVal, strMessage) {
    var spAlert = document.createElement("span");
    spAlert.setAttribute("id", "tkInvalid");
    spAlert.setAttribute("class", "tkInvalid");
    spAlert.setAttribute("title", strMessage);
    spAlert.innerHTML = "&nbsp;*";
    //spAlert.innerHTML = '<span title="'+strMessage+'">&nbsp;*</span>';
    //spAlert.innerHTML = "&nbsp;* " + strMessage;
    objVal.parentNode.appendChild(spAlert);
}

function tk_addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') { window.onload = func; }
    else {
        window.onload = function() {
            try {
                if (oldonload) { oldonload(); }
                func();
            } catch (e) { }
        }
    }
}

tk_addLoadEvent(tk_fSetFuncs);
