var ns4 = document.layers;
var ie4 = document.all;
var ns6 = document.getElementById && !document.all;

function goto(url) {
    document.location.href = url;
}

function URLJump(formname, url) {
    this.document[formname].action = url;
    this.document[formname].submit();
}

function invalidChars(testie) {
    var invalid = /[<>"'|]/;
    return invalid.test(testie);
}

function validEmail(testie) {
    var valid = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return (valid.test(testie));
}

function validPassword(testie) {
    var valid = /\w{4,16}/;
    return valid.test(testie);
}

function isPhone(testie, canBeEmpty) {
    var valid = (canBeEmpty) ? /^[0-9-+().]*$/ : /^[0-9-+().]+$/;
    return valid.test(testie);
}

function isNumber(testie, canBeEmpty, isDecimal) {
    var valid = '';
    if (canBeEmpty) {
        if (isDecimal) {
            valid = /^[0-9.]*$/
        } else {
            valid = /^[0-9]*$/
        }
    } else {
        if (isDecimal) {
            valid = /^[0-9.]+$/
        } else {
            valid = /^[0-9]+$/
        }
    }
    return valid.test(testie);
}

function showError(msg, showIcon) {
    if (ns4) {
        alert(msg);
    } else {
        if (showIcon) {
            document.getElementById('erroricon').innerHTML = '<img src="/images/warning.gif" alt="" hspace="3" vspace="2" border="0" />';
        }
        document.getElementById('errormsg').innerHTML = msg;
        document.getElementById('errormsg').style.display = "";
    }
}

function openWin(url, name, width, height) {
    window.open(url, name, 'height=' + height + ',width=' + width + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=auto,resizable=yes');
}

function openFull(pic, name) {
    window.open('zoom.php?pic=' + pic, 'Zoom', 'height=' + screen.height + ',width=' + screen.width + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=auto,resizable=yes');
}

/* Set Session Cookie */
function openBrWindow(url, name, width, height) {
    if (GetCookie('popup') != '1') {
        openWin(url, name, width, height);
        SetCookie('popup', '1')
    }
}

/* global cookies functions */
function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;

    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;

        if (i == 0) break;
    }
    return null;
}

function SetCookie(name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;

    document.cookie = name + "=" + escape(value) +

                      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                      ((path == null) ? "" : ("; path=" + path)) +
                      ((domain == null) ? "" : ("; domain=" + domain)) +
                      ((secure) ? "; secure" : "");
}

function DeleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

/* SEARCH */
function findProduct(lang) {
    var obj = document.getElementById('search_str');
    if ((obj) && (obj.value != '')) {
        document.getElementById("searchResults").src = "./include/products_search.php?lang=" + lang + "&q=" + obj.value;
    } else {
        document.getElementById("results").innerHTML = document.getElementById("txtNoMatchingProducts").value;
    }
}
