﻿function validNumberSpace(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
    if (keyCode == 13) {
        document.returnValue = false;
    }
    if ((keyCode < 46 || keyCode > 57) && keyCode != 32 && keyCode != 8 && keyCode != 127 && keyCode != 188 && keyCode != 190 && keyCode != 9) {
        document.returnValue = false;
        return false;
    }
    document.returnValue = true;
}
function validNumber(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
    if (keyCode == 13) {
        document.returnValue = false;
    }

    if ((keyCode < 48 || keyCode > 57) && TestForSystemKeys(keyCode) == false && keyCode != 188 && keyCode != 190) {
        document.returnValue = false;
        return false;
    }
    document.returnValue = true;
}
function validInteger(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
    if (keyCode == 13) {
        document.returnValue = false;
    }
    if ((keyCode < 48 || keyCode > 57) && TestForSystemKeys(keyCode) == false) {
        document.returnValue = false;
        return false;
    }
    document.returnValue = true;
}
function validAlphaNumber(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
    if (keyCode == 13) {
        document.returnValue = false;
    }
    if ((keyCode < 9 || keyCode > 200) && TestForSystemKeys(keyCode) == false) {
        document.returnValue = false;
        return false;
    }
    document.returnValue = true;
}
function validAlphaOnly(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
    if (keyCode == 13) {
        document.returnValue = false;
    }
    if ((keyCode < 65 || keyCode > 90) && (keyCode < 97 || keyCode > 122) && TestForSystemKeys(keyCode) == false) {
        document.returnValue = false;
        return false;
    }
    document.returnValue = true;
}
function validDate(keyStroke) {
    var keyCode = (document.layers) ? keyStroke.which : keyStroke.keyCode;
    if (keyCode == 13) {
        document.returnValue = false;
    }
    if ((keyCode < 48 || keyCode > 57) && keyCode != 191 && TestForSystemKeys(keyCode) == false) {
        document.returnValue = false;
        return false;
    }
    document.returnValue = true;
}
function TestForSystemKeys(keyCode) {
    //checks for all system keys in one place.
    window.status = '';
    switch (keyCode) {
        case 37: // right cusror
            return true;
            break;
        case 38: // left cursor
            return true;
            break;
        case 8: // delete
            return true;
            break;
        case 46: // backspace
            return true;
            break;
        case 9: //  tab
            return true;
            break;
        default:
            window.status = 'input not valid';
            return false;
            break;
    }
}


function isEmail(email) {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(email))
        return true
    else {
        return false
    }
}

function sendEmail(email) {
    window.location = 'mailto:' + email;
}

function openWebPage(url) {
    window.open(url);
}

function printPreview() {
    var browser = navigator.appName;
    if (browser == 'Microsoft Internet Explorer' || browser == 'MSIE') {
        var OLECMDID = 7;
        var PROMPT = 1; // 2 DONTPROMPTUSER 
        var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';

        document.body.insertAdjacentHTML('beforeEnd', WebBrowser); WebBrowser1.ExecWB(OLECMDID, PROMPT);
        WebBrowser1.outerHTML = "";
    } else {
        window.print();
    }
}

function isNumeric(strTest) {
    var NumericRegExp = /^\d+$/;
    var regex = new RegExp(NumericRegExp);
    return (regex.test(strTest));
}

function BlendOpacity(id, opacStart, opacEnd, millisec) {
    // Speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;


    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("SetOpacity(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("SetOpacity(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function SetOpacity(opacity, id) {
    if (document.getElementById(id) == null)
        return false;

    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

var strSoftHyphen = (navigator.userAgent.toLowerCase().indexOf("applewebkit") > -1 || document.all) ? "&shy;" : "<wbr/>"; // use soft-hyphen for IE and Opera which are known to implement it correctly

function SoftWrap(str, maxcolumns) {
    // these regular expressions need to incorporate variables (function params), so cant be static properties of the function, to save a few operations in repeated use
    var wordspaceRe = new RegExp('^\\w{1,' + maxcolumns + '}\\s+');
    var punctuationRe = new RegExp('^[!\\._\\-\\\\\,=\\*]{1,' + maxcolumns + '}');

    var wrapstr = "";
    var charCount = 0;
    while (str.length) {
        var endidx = 1;
        // shortcut if there's less remaining characters than the maxcols
        if (str.length < maxcolumns) {
            wrapstr += str;
            break;
        }
        // look ahead for space characters
        var spaceMatches = str.match(wordspaceRe);
        if (spaceMatches && spaceMatches[0]) {
            endidx = spaceMatches[0].length;
            wrapstr += str.substring(0, endidx);
            str = str.substring(endidx);
            charCount = 0; // reset 
            continue;
        } else {
            // handle markup
            if (str.charAt(0) == "<" && str.indexOf(">") > -1) {
                endidx = str.indexOf(">");
                charCount++; // count as one character
            } else if (str.charAt(0) == "&" && str.match(/^&\w+;/)) { // handle entities
                endidx = (str.indexOf(";") > -1) ? str.indexOf(";") + 1 : str.length;
                charCount++; // count as one character
            } else {
                var puncMatches = str.match(punctuationRe);
                if (puncMatches && puncMatches[0]) {
                    // handle punctuation
                    endidx = puncMatches[0].length;
                    charCount += endidx; // count as one character
                } else {
                    charCount++; // default case is just one character
                }
            }
        }
        wrapstr += str.substring(0, endidx);

        if (charCount >= maxcolumns) {
            wrapstr += strSoftHyphen;
            charCount = 0;
        }
        str = str.substring(endidx);
    }
    return wrapstr;
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}