﻿// JScript File
// http://www.askthecssguy.com/2007/05/validation_hints_for_your_form_1.html

var formHasChanged = false;

function checkEmail(obj) {
    //check into Reg Ex for this.
    // RexEx Source : http://www.devarticles.com/c/a/JavaScript/Regular-expressions-in-JavaScript/8/ 
    var emailExp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/
    var par = obj.parentNode;
    if (emailExp.test(obj.value)) {
        par.className = "statusGood";
        return;
    }
    par.className = "statusUnknown";
}

function showHint(obj) {
    // make sure if DDL shrinkList is called first.
    var chds = obj.parentNode.getElementsByTagName("SPAN");
    var myWid = getDistance(obj);
    for (i = 0; i < chds.length; i++) {
        if (chds[i].className == "hint" || chds[i].className == "hintSmall") {
            var hBlk = chds[i];
            hBlk.style.display = "block";
            hBlk.className = "hint";
            // move over so it appears to the right of the text box.
            //alert(hBlk.id);
            hBlk.style.left = (myWid + 35) + "px";
            hBlk.style.zIndex = "300";
        }
    }
}

function getDistance(obj) {
    var lbls = obj.parentNode.getElementsByTagName("label");
    // get width of referring opbject.
    var retWid = getObjWidth(obj);
    for (li = 0; li < lbls.length; li++) {
        // add width of labels.
        retWid += getObjWidth(lbls[li]);
    }
    return retWid;
}

function getObjWidth(lobj) {
    var w = lobj.clientWidth;
    if (lobj.iWidth) w = lobj.iWidth;
    if (!w) w = 200;
    return w + 20;
}

function hideHint(obj) {
    /// Make sure if DDL, expandList is call first.
    //chkFunction(obj);
    var chds = obj.parentNode.getElementsByTagName("span");
    var par = obj.parentNode;
    var myDist = getDistance(obj);
    for (i = 0; i < chds.length; i++) {
        if (chds[i].className == "hint" || chds[i].className == "hintSmall") {
            var hBlk = chds[i];
            if (par.className == "statusGood") {
                hBlk.style.display = "none";
            } else {
                hBlk.className = "hintSmall";
                //var startI = parseInt(hBlk.style.left);
                //if (!startI) startI = 0;
                //hBlk.style.left = (startI  - 35) + "px";
                hBlk.style.left = (myDist) + "px";
                hBlk.style.zIndex = "";
            }
        }
    }
    //alert(par.className);
}

function shrinkList(obj) {
    //obj.style.width = getOtherBoxWidth(obj) + "px";
}

function getOtherBoxWidth(obj) {
    var othBoxs = document.getElementsByTagName("select");
    var oldWid = "auto";
    for (ib = 0; ib < othBoxs.length; ib++) {
        var iBox = othBoxs[ib];
        if (iBox.id != obj.id) {
            //if (iBox.style.width)
            oldWid = getObjWidth(iBox);
        }
    }
    return oldWid;

}

function expandList(obj) {
    try {
        obj.style.width = "auto";
    } catch (e) {
    }

}


function checkText(obj) {
    var par = obj.parentNode;
    par.className = "statusGood";
    if (obj.value.length < 1) {
        par.className = "statusUnknown";
    }
    markFormChanged()
}
function checkSelection(obj, dropDownFilter) {
    var par = obj.parentNode;
    par.className = "statusGood";
    if (obj.value < 0) {
        par.className = "statusUnknown";
    }
    if (dropDownFilter) {
        if (obj.options[obj.selectedIndex].text.match(dropDownFilter)) {
            par.className = "statusGood";
        } else {
            par.className = "statusBad";
        }
    }
    markFormChanged()
}

function checkPassword(obj) {
    var par = obj.parentNode;
    par.className = "statusGood";
    if (obj.value.length < 1) {
        par.className = "statusUnknown";
    }
    markFormChanged()
}


function checkNumber(obj) {
    var par = obj.parentNode;
    markFormChanged()
    var numExp = /^[0-9]{1,5}$/
    if (obj.value == '') {
        par.className = "statusUnknown";
        return;
    }
    if (numExp.test(obj.value)) {
        if (obj.value > 65555) {
            par.className = "statusBad";
            return;
        }
        par.className = "statusGood";
        return;
    }
    par.className = "statusBad";
}

function checkAllBoxes(obj) {
    var arr = document.getElementsByTagName("input");
    var toCheck = false;
    var newTxt = "Select All";
    toCheck = (obj.innerHTML == "Select All");
    if (toCheck) {
        newTxt = "Unselect All";
    }
    for (i = 0; i < arr.length; i++) {
        if (arr[i].type == "checkbox") {
            arr[i].checked = toCheck;
            markMyLine(arr[i]);
        }
    }
    obj.innerHTML = newTxt;
}

function markMyLine(obj) {
    var par = obj.parentNode.parentNode;
    if (par.nodeName.toLowerCase() == "tr") return;
    var txt = "pageNode";
    if (obj.checked) {
        txt = "pageNodeSelected";
    }
    par.setAttribute("class", txt);
    par.setAttribute("className", txt);
    //par.style.border = "solid 1px #F00"
}

function countChars(txtToCount, spanToShow, count) {
    var txt = document.getElementById(txtToCount);
    var lbl = document.getElementById(spanToShow);
    var charCount = txt.value.length;
    lbl.innerHTML = "used: " + charCount + " max: " + count;
    if (charCount > count) {
        lbl.innerHTML += " exceeded: " + (charCount - count);
        lbl.setAttribute("class", "charCountError");
        lbl.setAttribute("className", "charCountError");
    } else {
        lbl.setAttribute("class", "charCount");
        lbl.setAttribute("className", "charCount");
    }
    lbl.innerHTML += " (characters)";
    markFormChanged()
    return;
}

function getValidElements() {
    return "style";
}
function markFormChanged(obj) {
    if (!(obj === undefined)){
        obj.setAttribute("class", "modified");
        obj.setAttribute("className", "modified");
        var par = obj.parentNode;
        if (par) {
            var lbls = par.getElementsByTagName("label");
            if (lbls) {
                var lbl = lbls[0]
                lbl.setAttribute("class", "modified");
                lbl.setAttribute("className", "modified");
            }
        }
    }
    formHasChanged = true;
}
function getStyleSheets() {
    var hd = document.getElementsByTagName("head")[0];
    var styls = hd.getElementsByTagName("link");
    var stylsheets = "";
    for (sID = 0; sID < styls.length; sID++) {
        var hReffer = styls[sID].href;
        if (hReffer != "undefined" || hReffer != null)
            stylsheets += hReffer + ",";
    }
    return stylsheets.substr(0, stylsheets.length - 1);
}
