﻿// ************************************************************************
//IM_GenScript
//Created:	04/05/2001
//Author:	T. Gardner
//Last Update:	3/14/2008
// ************************************************************************

function GenScr() {
    this.adjTopNN = adjTopNN;
    this.rollImg = rollImg;
    this.setStatus = setStatus;
    this.trimL = trimL;
    this.hideObject = hideObject;
    this.showObject = showObject;
    this.isVisible = isVisible;
}

var GS = new GenScr();

function adjTopNN(objDoc) {
    if (objDoc.layers) objDoc.layers[0].top = -1.5;
}

function rollImg(obj, way) {
    var re = /\$\d+\b/;
    var sSrc = obj.name;
    sSrc = sSrc.replace(re, '');

    if (way == 'in') {
        sSrc = sSrc + 'R';
    }
    sSrc = 'images/' + sSrc + '.gif';
    obj.src = sSrc;
}

function setStatus(msg) {
    status = msg;
    return true;
}

function trimL(newStr) {
    for (var n = 0; i <= newStr.length; n++) {
        if (newStr.charCodeAt(n) == 32) {
            n++;
            break;
        }
    }
    return newStr.substr(n);
}


function isVisible(oDoc, sID, oRef) {
    //get a reference as above ...
    if (!oRef) {
        var oRef = getRefToObj(oDoc, sID);
        if (!oRef) {
            alert('Nothing works in this browser');
            return; //don't go any further
        }
    }
    //now we have a reference to it
    if (oRef.style) {
        //DOM & proprietary DOM
        if (oRef.style.visibility == 'visible') {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        //layers syntax
        if (oRef.visibility == 'show') {
            return true;
        }
        else {
            return false;
        }
    }
}

function showObject(oDoc, sID, oRef) {
    //get a reference as above ...
    if (!oRef) {
        var oRef = getRefToObj(oDoc, sID);
        if (!oRef) {
            alert('Nothing works in this browser');
            return; //don't go any further
        }
    }
    //now we have a reference to it
    if (oRef.style) {
        //DOM & proprietary DOM
        oRef.style.visibility = 'visible';
    }
    else {
        //layers syntax
        oRef.visibility = 'show';
    }
}

function hideObject(oDoc, sID, oRef) {
    //get a reference as above ...
    if (!oRef) {
        oRef = getRefToObj(oDoc, sID);
        if (!oRef) {
            alert('Nothing works in this browser');
            return; //don't go any further
        }
    }
    //now we have a reference to it
    if (oRef.style) {
        //DOM & proprietary DOM
        oRef.style.visibility = 'hidden';
    }
    else {
        //layers syntax
        oRef.visibility = 'hide';
    }
}

function getRefToObj(oDoc, objID) {
    if (document.getElementById) {
        var objNew = oDoc.getElementById(objID);
        return objNew
    }
    if (document.all) {
        return oDoc.all[objID];
    }

    if (!oDoc) { oDoc = document; }

    if (document.layers) {
        if (oDoc.layers[objID]) {
            return oDoc.layers[objID];
        }
        else {
            //repeatedly run through all child layers
            for (var x = 0, y; !y && x < oDoc.layers.length; x++) {
                //on success, return that layer, else return nothing
                y = getRefToObj(objID, oDoc.layers[x].document);
            }
            return y;
        }
    }
    return false;
}

function getNodeAbove(oNode, sName) {
    var sParent = oNode.parentNode.nodeName;
    var oRetVal;

    if (sParent == sName) {
        oRetVal = oNode.parentNode; // return the node
    }
    else {
        oRetVal = getNodeAbove(oNode.parentNode, sName); // iterate
    }
    return oRetVal;
}

function getAttrVal(ref, attrName) {
    var Attrs = ref.attributes;

    var sRetVal;
    for (var i = Attrs.length - 1; i >= 0; i--) {
        if (Attrs[i].name == attrName) {
            sRetVal = Attrs[i].value; break;
        }
    }
    return sRetVal;
}


function changeCase(obj, sType) {
    if (sType == "U") {
        obj.value = obj.value.toUpperCase();
    }
    else if (sType == "L") {
        obj.value = obj.value.toLowerCase();
    }
}

function inputErr(obj, sMsg) {
    alert(sMsg);
    if (obj.type == "text" || obj.type == "password") {
        obj.select();
        obj.focus();
    }
    return false;
}

function isAlpha(value) {
    var iChar;
    value = value.toString();
    for (var i = 0; i < value.length; i++) {

        iChar = value.charCodeAt(i);
        if (iChar >= 65 && iChar <= 90) {
            return true;
        }
        else if (iChar >= 97 && iChar <= 122) {
            return true;
        }
        else {
            return false;
        }
    }
}

function REinput() {
    this.Zip = chkZip;
    this.Email = chkEmail;
}

var RE = new REinput()

function chkEmail(value) {
    var re = /^\w+([\.\-]\w+)*\@\w+([\.\-\S]\w+)*\.\w+$/;
    var re2 = /^.*@[^_]*$/;
    var re3 = /^[\S]*$/;

    if ((!re.test(value)) || (!re2.test(value)) || (!re3.test(value))) {
        return "Invalid E-mail format";
    }
    else {
        return "";
    }
}

function chkZip(value) {
    var zipVal;
    var zipPlus;

    var re = /\d{5,5}/;
    var rePlus = /\-\d{4,4}/;

    zipVal = value.substr(0, 5);
    zipPlus = value.substr(5);

    if ((zipPlus.length > 0) && (!rePlus.test(zipPlus))) {
        return "Invalid Zip Plus 4 Code format";
    }
    else if (!re.test(zipVal)) {
        return "Invalid Zip Code format";
    }
    else {
        return "";
    }
}

function isCC(s, ccType) {
    var vCC = /^4\d{14,15}$/;
    var mcCC = /^5[1-5]\d{14}$/;
    var aeCC = /^3[47]\d{13}$/;
    var dCC = /^6011\d{12}$/;
    var dcCC = /^(30[0-5]|360|380)\d{11}$/;
    var re;

    s = s.replace(/\s/g, '');

    if (ccType == "v") {
        re = vCC;
    }
    else if (ccType == "mc") {
        re = mcCC;
    }
    else if (ccType == "ae") {
        re = aeCC;
    }
    else if (ccType == "d") {
        re = dCC;
    }

    if (re.test(s)) {
        return isLUHN(s);
    }
    else {
        return false;
    }
}

function isLUHN(s) {
    var x = '';
    var iLen = s.length;
    var t = 0;

    for (i = 0; i <= iLen - 1; i++) {
        if ((iLen - i) % 2) x += s.charAt(i); else x += '' + s.charAt(i) * 2;
    }
    for (var i = 0; i <= (x.length - 1); i++) {
        t += x.charAt(i) - 0;
    }
    return !(t % 10);
}

function isNumber(value) {
    var sChar;
    value = value.toString();
    for (var i = 0; i < value.length; i++) {
        sChar = value.charAt(i);
        if (sChar < "0" || sChar > "9") {
            return false;
        }
    }
    return true;
}

function sQ(str) {
    sReturn = String.fromCharCode(39) + str + String.fromCharCode(39);
    return sReturn;
}


var objAcctWin;
var objLogWin;

function closeLogIn() {
    if (objLogWin) {
        if (fNN) {
            if (objLogWin.name != undefined) objLogWin.close();
        }
        if (fIE) {
            objLogWin.close();
        }
    }
}

function logInMsg(idx, FullName) {
    if (idx == 1) {
        var sMsg = 'Welcome, ' + FullName + '!';
    }
    else if (idx == 2) {
        var sMsg = 'Your log-in data has been sent to your e-mail.';
    }
    else if (idx == 3) {
        var sMsg = 'Your account data has been updated.';
    }
    else if (idx == 4) {
        var sMsg = FullName + ' has successfully logged out.';
    }
    else if (idx == 5) {
        var sMsg = 'Your brochure requests will be processed.';
    }
    else if (idx == 6) {
        var sMsg = 'Thank you for joining Insight Media!\n';
        sMsg += 'Please remember to keep your password in a safe place.';
    }
    else if (idx == 7) {
        var sMsg = 'No changes have been made to your account data.';
    }
    else if (idx == 8) {
        var sMsg = 'You did not complete your entries!\n';
        sMsg += 'An account has not been established for you.';
    }
    alert(sMsg);
}

function navInfoBroch(pageSending) {
    var sLoc = 'IMInfoBroch.asp?page=' + pageSending;
    window.top.location = sLoc;
}


function setLogIn(logIdx) {
    var sLoc;
    // 0: logout; 1: log-in; 2: edit account; 4: Checkout setLogIn
    sLoc = 'processors/IM_ProcessLogIn.asp?p=setLogIn&logIdx=' + logIdx.toString();
    window.top.location = sLoc;
}

function setLogInSec(logIdx) {
    var sLoc;
    // 0: logout; 1: log-in; 2: edit account; 4: Checkout setLogIn
    sLoc = '../processors/IM_ProcessLogIn.asp?p=setLogIn&logIdx=' + logIdx.toString();
    window.top.location = sLoc;
}


function setLogInBAK(logIdx, secLoc) {
    var sLoc;
    // 0: logout; 1: log-in; 2: MyAccount; 4: Checkout login
    if (logIdx == 0) {
        sLoc = secLoc + 'IM_ProcessLogIn.asp?p=setLogIn&logIdx=0';
    }
    else {
        sLoc = secLoc + 'IMAcctLogIn.asp?logIdx=' + logIdx.toString();
    }
    window.top.location = sLoc;
}


function changeVw(idx, mainLoc) {
    if (idx == 1) {
        sMsg = 'The Log-In name you have entered is valid only for International Sales!\nYou will be redirected in a moment.';
        sLoc = mainLoc + 'IMGroupDispl.asp?Init=S&View=I';
    }
    else if (idx == 2) {
        sMsg = 'The Log-In name you have entered is valid only for domestic sales in the United States!\nYou will be redirected in a moment.';
        sLoc = mainLoc + 'IMGroupDispl.asp?Init=S&View=U';
    }

    alert(sMsg);
    window.top.location = sLoc;

}

function navEditAddr(SSL, param) {
    var sLoc = SSL + 'IMEditAcct.asp?LogIdx=9';

    if (typeof param == "object") {
        param.txtURL.value = window.top.location.href
        param.action = sLoc;
        param.submit();
    }
    else {
        window.top.location = sLoc;
    }
}

function enterKey(e, objFrm, objParam) {
    var iKey
    if (window.event) {
        iKey = window.event.keyCode;
    }
    else if (e) {
        iKey = e.charCode;
        if (iKey == 0 || iKey == undefined) iKey = e.which;
    }

    if (iKey == 13) {
        searchCat(objFrm, objParam);
        return false;
    }
    else {
        return true;
    }
}

function searchCat(objFrm, objParam) {
    var searchWord = objFrm.txtSearch.value;

    if (searchWord == '') {
        var sMsg = 'Enter at least one word to search by.';
        alert(sMsg);
    }
    else {
        if (objParam.name == objFrm.name) {
            objFrm.action = 'IMListDispl.asp?method=post'
            objFrm.submit();
        }
        else {
            var searchType = getRadioVal(objFrm.SearchType);
            var sSearchVal = escape(objFrm.txtSearch.value);

            var sLoc = 'IMGetProdData.asp?mode=S&SearchWord=' + sSearchVal + '&SearchType=' + searchType;

            var oFrame = getRefToObj(objParam, 'iFrameDispl');
            oFrame.src = sLoc
        }
    }
}

function orderItemTEST(x, y) {
    alert('OK: ' + y);
}


function orderItem(oDoc, btnName) {
    var sSaveType = btnName.slice(0, 1);
    var sItem = btnName.slice(2);
    var iQty = 1;

    var sHiddenID = 'h_' + sItem;

    var oHidden = getRefToObj(oDoc, sHiddenID);
    var sData = oHidden.value;

    var iChar1 = sData.indexOf(';@');
    var iChar2 = sData.indexOf(';$');
    var iChar3 = sData.indexOf(';~');

    var sTitle = escape(sData.slice(0, iChar1));
    var sMedia = sData.slice(iChar1 + 2, iChar2);


    if (iChar3 >= 0) {
        var sPrice = sData.slice(iChar2 + 1, iChar3);
    }
    else {
        var sPrice = sData.slice(iChar2 + 1);
    }

    var sLocation = 'processors/IM_SaveItem.asp?SaveType=' + sSaveType + '&ItemNo=' + sItem + '&Qty=' + iQty;
    sLocation += '&Title=' + sTitle + '&Media=' + sMedia + '&Price=' + sPrice;

    //	alert(sLocation);

    var oParent = window.parent.document;
    var oMsgHdr = getRefToObj(oParent, 'iFrameMsg');

    oMsgHdr.src = sLocation;

}


function getRadioVal(objRadio) {
    if (!objRadio) return "";

    var radioLen = objRadio.length;
    if (radioLen == 1)
        if (objRadio.checked)
            return objRadio.value;
        else
            return "";
    for (var i = 0; i < radioLen; i++) {
        if (objRadio[i].checked) {
            return objRadio[i].value;
        }
    }
    return "";
}



