﻿// -------------------------------------------------------------------------------------------------
// START: GLOBALS

var _siteAvailable = true;
var _postRender = null; //_commonPostRender;
var _customElementName = "LOC";
var _UID = null;
var _UN = null;
var _LoginPrompt = null;
var _AlertWin = null;
var _AlertLace = null;
var _AlertMsg = null;
var _AlertButton = null;
var _LenCrit = 5;
var _sectionLoaded = false;
var _ival_Loading, _loaderOff = true, _loading_item = null, loading_prog;

// END: GLOBALS
// -------------------------------------------------------------------------------------------------
//
// START: Check site availability

var _delayTimer;

function checkSiteIntegrity()
{
    Comm.sendCommand("<CMD name='Handshake'/>", cb_checkSiteIntegrity, "Connecting to site content...");
    _delayTimer = setTimeout("na_checkSiteIntegrity()",10000);
}

function cb_checkSiteIntegrity(args)
{
    if(args!="Connected" && args!="Conn-BUY" && args!="Conn-PEN") na_checkSiteIntegrity();
    else {
        document.body.innerHTML="<p>Site up, loading sections ...</p>";
        clearTimeout(_delayTimer);
        
        document.body.innerHTML="";
        showNavigator();
        renderContent();        
        
        if(initPG!="") {
            ///loadSpecificPGSC(initPG,initSC);
            selectDistinctMainOption(initPG);
            //fetchItemsForSection(initPG);
            initPG = "";                    
        }            
        else {
            
            switch(args) {
                case "Connected": ;
                    selectDistinctMainOption("NEWS");
                    //fetchItemsForSection("HOME");
                    break;
                case "Conn-BUY": ;
                    //selectDistinctMainOption("BUY-A");
                    //alert(_LinkBuyA);
                    selectMainMenuOption("BUY-A");
                    getPurchaseConfirmation();
                    break;
                case "Conn-PEN": ;
                    //selectDistinctMainOption("BUY-P");
                    selectMainMenuOption("BUY-P");
                    getPurchaseConfirmation();
                    break;
                default: ;
                    selectDistinctMainOption("NEWS");
                    break;
            } 
        }
        
        setFooter();
		getStats();
    }        
}

function loadSpecificPGSC(iPG, iSC)
{
    if(document.readyState==4) {
        gotoPage(iPG, iSC);
        initPG="";
    }
    else {
        setTimeout("loadSpecificPGSC('"+iPG+"','"+iSC+"');", 40);
    }
}

function na_checkSiteIntegrity()
{
    _siteAvailable = false;
    document.body.innerHTML = "<p>Site is currently unavailable!</p>";
}

//  END: Check site availability
// -------------------------------------------------------------------------------------------------
//
// START: ITEM FETCHER

function fetchItemsForSection(section)
{
    _sectionLoaded = false;
    Comm.sendCommand("<CMD name='GetSection'><PARAM name='name'>"+section+"</PARAM></CMD>", cb_fetchItemsForSection, "Loading "+section+" section items...");
}

function cb_fetchItemsForSection(args)
{
    var XmlDoc = args;
    var Items, i, Item;
    
    clearContent();
        
    try {
        Items = XmlDoc.selectNodes("RET/ITEM");
    } catch(e0) {
        alert("Failed to load content: "+e0.message+"\n\n"+args); 
        return;
    }
    
    //alert(Items.length+args.selectSingleNode("CALLID").xml);
    
    for(i=0;i<Items.length;i++) {
        Item = Items[i];
        addItem(Item.selectSingleNode("REF").text, Item.selectSingleNode("TITLE").text, Item.selectSingleNode("CONTENT").text, Item.selectSingleNode("DATE").text, Item.selectSingleNode("SIGN").text, Item.selectSingleNode("SHOWDATE").text, Item.selectSingleNode("SHOWTITLE").text);
        
    }
    
    XmlDoc = null;
    _loaderOff = true;
    
    try { _postRender(); } catch(e1) {}
    
    try { CollectGarbage(); } catch(e2) {}
    
    if(initSC!="") {
        document.location=String(document.location).split("#")[0]+"#"+initSC;
    }
    
    _sectionLoaded = true;
}

function clearContent()
{
    var i, n = _contentArea.childNodes.length;
    
    try {
        for(i=0;i<n;i++) {
            _contentArea.childNodes.remove(0, true);
        }
    } catch(e0) {}
        
    _contentArea.innerHTML = "";
    
    try { CollectGarbage(); } catch(e1) {}
        
}

function addItem(ref, title, content, date, signature, showDate, showTitle)
{
    var item, reflabel, titleD, dateD, refD, signatureD, contentD, headerD, bottomD, goUpS;
    var linkColl, i, link;
    
    item = document.createElement("DIV");
    
    try {
        reflabel = document.createElement("<A name='"+ref+"'>");
    } catch (e) {
        reflabel = document.createElement("A");
        reflabel.name = ref;
    }        
    reflabel.descr = title;
    item.appendChild(reflabel);
    
    headerD = document.createElement("DIV");
    headerD.className = "itemHeaderDiv";
    
    dateD = document.createElement("SPAN");
    dateD.className = "itemDateDiv";
    
    if(showDate=="False") {
        dateD.innerHTML = "";
        dateD.style.display="none";
    } else {
        dateD.innerHTML = date;
    }
    headerD.appendChild(dateD);
    
    if(showTitle=="True") {
        titleD = document.createElement("SPAN");
        titleD.className = "itemTitleDiv";
        titleD.innerHTML = title;
        headerD.appendChild(titleD);
        
        item.appendChild(headerD);
    }
    
    signatureD = document.createElement("DIV");
    signatureD.className = "itemSignatureDiv";
    signatureD.innerHTML = signature; //+"By Vlad";
    item.appendChild(signatureD);
    
    contentD = document.createElement("DIV");
    contentD.className = "itemContentDiv";
    contentD.innerHTML = content;
        
    item.appendChild(contentD);
    
    if(showTitle=="True") {
        bottomD = document.createElement("DIV");
        bottomD.className = "itemNavDiv";
        
        goUpS = document.createElement("SPAN");
        goUpS.onclick = function() { try { _pageTopRef.scrollIntoView(); } catch(e) {} _pageTopRef.focus(); } 
        goUpS.innerHTML = "<img src='visuals/images/goup.gif'>&nbsp;Top of Page";
        bottomD.appendChild(goUpS);
        
        item.appendChild(bottomD);
    }
        
    
        
    _contentArea.appendChild(item);
    
    try { _postRender(); } catch(e) {}
    try { _commonPostRender(); } catch(e1) {}
}

// END: ITEM FETCHER
// -------------------------------------------------------------------------------------------------
//
// START : NAVIGATOR


function showLoader()
{
    if(_loading_item==null) {
        _loading_item = document.createElement("DIV");
        _loading_item.className = "loading";
        document.body.appendChild(_loading_item);
        //_loader_prog = document.createElement("DIV");
        
    }
    _loaderOff = false;
    _ival_Loading = setInterval("checkIfLoaded();", 50);
    _loading_item.style.display="block";
}

function checkIfLoaded()
{
    if(_loaderOff) {
        _loading_item.style.display="none";
        clearInterval(_ival_Loading);
    }
}
function gotoPage(section, item)
{
    
    showLoader();
        
    if(item) {
        _postRender = function() { 
            var i, coll = selectElementsByTagName("A");
            for(i=0;i<coll.length;i++) { if(coll[i].name == item) { coll[i].scrollIntoView(); coll[i].focus(); } _loaderOff = true; break; }
        }
        //_postRender +=_commonPostRender;
        //alert(_postRender);
        //_commonPostRender();
    } else { _loaderOff = true; _postRender = null; } 
    
    selectDistinctMainOption(section);
    // scroll to item
    
    //alert(section+" / "+item);
    try { event.cancelBubble = true; event.returnValue = false; } catch(e) {}
    return false;
}

function _commonPostRender()
{
    var j, tmp, coll = document.getElementsByTagName(_customElementName);
    //alert(coll.length);
    
    for(j=0;j<coll.length;j++) {
        //tmpEl = coll[j];
        //tmpEl.href = "#";
        //tmp = coll[j].href.split("/");
        tmpEl = document.createElement("SPAN");
        tmpEl.page = coll[j].attributes.getNamedItem("page").value; //tmp[tmp.length-2]; 
        tmpEl.ref = coll[j].attributes.getNamedItem("ref").value;
        tmpEl.appendChild(document.createTextNode(coll[j].attributes.getNamedItem("label").value+" ")); //;
        tmpEl.className = "link_off";
        //tmpEl.ref = tmp[tmp.length-1]; 
        tmpEl.onmouseover = function() { this.className = "link_on"; }
        tmpEl.onmouseout = function() { this.className = "link_off"; }
        tmpEl.onclick=function() { gotoPage(this.page, this.ref); return false; }
        //alert(coll[j].parentNode);
        //coll[j].replaceNode(tmpEl, coll[j]);
        coll[j].parentNode.replaceChild(tmpEl, coll[j]);
    }
        
}

// END: NAVIGATOR
// -------------------------------------------------------------------------------------------------
//
// START: LOGIN


function loginCustomer(username, password)
{
    //showLoginPrompt();
    document.getElementById("_loginStatus").innerHTML = "Logging in...";
    Comm.sendCommand("<CMD name='Login'><PARAM name='username'>"+username+"</PARAM><PARAM name='password'>"+password+"</PARAM></CMD>", cb_loginCustomer, "Logging in... ");
}

function cb_loginCustomer(args)
{
    var ret; 
    
    try {
        ret = args.selectSingleNode("RET/LOGIN");
    } catch(e0) {
        alert("Internal ERR#1002: Failed to verify login!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    if(ret.attributes.getNamedItem("SUCCESS").value=="1") {
        document.getElementById("_loginTab").style.visibility="hidden";
        setTimeout('closeLoginPrompt();',3000);
        document.getElementById("_loginStatus").innerHTML = "Welcome "+ret.selectSingleNode("USERNAME").text+"!";
        
		updateUserIndicator();
		_loaderOff = true;
			
    } else {
        document.getElementById("_loginTab").style.visibility="visible";
        document.getElementById("_loginStatus").innerHTML = "Wrong username or password.";
        _loaderOff = true;
    }
    
}

function showLoginPrompt()
{

    var Ltab, Lrow, Lcell, Ldiv, Lerr;
    var LIuser, LIpass;
    var Lsubmit;
    
    if(_LoginPrompt!=null) { 
        _LoginPrompt.style.display="block"; 
        _LoginPrompt.UN.focus();
        try { _LoginPrompt.UN.setActive(); } catch(e) {}
        return true; 
    }
    
    Ldiv = document.createElement("DIV");
    Ldiv.className = "LoginPromptHolder";
    Ldiv.id="_loginHolder";
    
    document.body.appendChild(Ldiv);
    _LoginPrompt = Ldiv;
    
    Ltab = document.createElement("TABLE");
    Ltab.style.width="100%";
    Ltab.id="_loginTab";
    Ltab.border=0;
    Ltab.cellspacing = 0;
    Ltab.cellpadding=2;
    
    Lrow = Ltab.insertRow(0);
    Lcell = Lrow.insertCell(0);
    Lcell.align="right";
    Lcell.innerHTML = "Username:";
    Lcell.style.width="75px";
    
    Lcell = Lrow.insertCell(1);
    LIuser = document.createElement("INPUT");
    LIuser.type = "text";
    LIuser.id = "_loginUN";
    LIuser.style.width="100px";
    LIuser.onkeydown = function() { try { if(event.keyCode==13) this.submitter.onclick(); } catch(e) {} }
    Lcell.appendChild(LIuser);
    
    Lrow = Ltab.insertRow(1);
    Lcell = Lrow.insertCell(0);
    Lcell.align="right";
    Lcell.innerHTML = "Password:";
    
    Lcell = Lrow.insertCell(1);
    LIpass= document.createElement("INPUT");
    LIpass.style.width="100px";
    LIpass.type = "password";
    LIpass.onkeydown = function() { try { if(event.keyCode==13) this.submitter.onclick(); } catch(e) {} }
    LIpass.id = "_loginPW";
    Lcell.appendChild(LIpass);
    
    Lrow = Ltab.insertRow(2);
    Lcell = Lrow.insertCell(0);
    Lcell.colSpan=2;
    Lcell.align="center";
    Lcell.style.padding="4px";
    Lcell.style.paddingTop="8px";
    
    Lsubmit = document.createElement("BUTTON");
    Lsubmit.innerHTML="Login";
    Lsubmit.className = "LoginButton";
    Lsubmit.UN = LIuser;
    Lsubmit.PW = LIpass;
    Lsubmit.onclick = function() { loginCustomer(this.UN.value, this.PW.value); }
    
    Lsep= document.createElement("SPAN");
    Lsep.innerHTML="&nbsp;";
    
    
    Lcancel = document.createElement("BUTTON");
    Lcancel.innerHTML="Cancel";
    Lcancel.className = "LoginButtonCancel";
    Lcancel.onclick = function() { _loaderOff = true; closeLoginPrompt(); }
    
    LIuser.submitter = Lsubmit;
    LIpass.submitter = Lsubmit;
    
    Lcell.appendChild(Lsubmit);
    Lcell.appendChild(Lsep);
    Lcell.appendChild(Lcancel);
    
    Lerr = document.createElement("DIV");
    Lerr.className = "LoginError";
    Lerr.id="_loginStatus";
    Lsubmit.status = Lerr;
    
    Ldiv.appendChild(Ltab);
    Ldiv.appendChild(Lerr);
    
    _LoginPrompt.UN = LIuser;
    _LoginPrompt.PW = LIpass;
    _LoginPrompt.ST = Lerr;
    
    LIuser.focus();
    try { LIuser.setActive(); } catch(e) {}
    
}

function closeLoginPrompt()
{
    _LoginPrompt.style.display="none";
    document.getElementById("_loginTab").style.visibility="visible";
    _LoginPrompt.UN.value="";
    _LoginPrompt.PW.value="";
    _LoginPrompt.ST.innerHTML = "";
    
}
    

// END: LOGIN
// -------------------------------------------------------------------------------------------------
//
// START: UPDATE USER INDICATOR

function updateUserIndicator()
{
    Comm.sendCommand("<CMD name='IsLoggedIn'></CMD>", cb_updateUserIndicator, "Verifying login...");
}

function cb_updateUserIndicator(args)
{
    var ret; 
    
    try {
        ret = args.selectSingleNode("RET/ISLOGGEDIN");
    } catch(e0) {
        alert("Internal ERR#1003: Failed to verify login!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    if(ret.attributes.getNamedItem("STATE").value=="1") {
        _UID = ret.selectSingleNode("UID").text;
        _UN = ret.selectSingleNode("UNAME").text;
        document.getElementById("REGISTER").style.display="none";
        document.getElementById("LOGIN").style.display="none";
        document.getElementById("LOGOFF").style.display="block";
        _UserIndic.style.display="block";
        _UserIndic.innerHTML = _UN;
    } else {
        _UID = null;
        _UN = null;
        document.getElementById("REGISTER").style.display="block";
        document.getElementById("LOGIN").style.display="block";
        document.getElementById("LOGOFF").style.display="none";
        _UserIndic.style.display="none";
        _UserIndic.innerHTML = "";
    }
    
}

// END: UPDATE USER INDICATOR
// -------------------------------------------------------------------------------------------------
//
// START: LOGOFF

function logoff()
{
    Comm.sendCommand("<CMD name='Logoff'></CMD>", cb_Logoff, "Logging out...");
}

function cb_Logoff(args)
{
    _UID = null;
    _UD = null;
    
    document.getElementById("REGISTER").style.display="block";
    document.getElementById("LOGIN").style.display="block";
    document.getElementById("LOGOFF").style.display="none";
    _UserIndic.style.display="none";
    _UserIndic.innerHTML = "";
}

// END: LOGOFF
// -------------------------------------------------------------------------------------------------
//
// START: STATS

function getStats()
{
    Comm.sendCommand("<CMD name='GetStats'></CMD>", cb_GetStats, "Getting stats...");
}

function cb_GetStats(args)
{
    var _StatWin;
    var ret; 
    
    _StatWin = document.createElement("DIV");
    _StatWin.className = "StatWin";
 
    try {
        ret = args.selectSingleNode("RET/STATS");
    } catch(e0) {
        alert("Internal ERR#1004: Failed to get stats!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    _StatWin.innerHTML = "Unique hits: <b>"+ret.selectSingleNode("UNIQUEHITS").text+"</b><br>Total hits: <b>"+ret.selectSingleNode("TOTALHITS").text+"</b>";
        
    document.body.appendChild(_StatWin);
}

// END: STATS
// -------------------------------------------------------------------------------------------------
//
// START: REGISTER

function disableReg()
{
    document.getElementById("regButton").disabled = true;
}

function enableReg()
{
    document.getElementById("regButton").disabled = false;
}


function initRegister()
{
    if(document.getElementById("regButton")==null) { setTimeout("initRegister()",10); return; }
    getCountries();
    
    document.getElementById("regButton").onclick = function() {
        if(validateRegData()) { registerNewCustomer(); }
    }
}

function getCountries()
{
    disableReg();
    Comm.sendCommand("<CMD name='GetCountries'></CMD>", cb_GetCountries, "Getting country list...");
}

function cb_GetCountries(args)
{
    var clist = document.getElementById("ddlCountries");
    var ret, i, opt; 
    
    try {
        ret = args.selectNodes("RET/COUNTRY");
    } catch(e0) {
        alert("Internal ERR#1005: Failed to get country list!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    for(i=0;i<ret.length;i++) {
        opt = document.createElement("OPTION");
        clist.options.add(opt,i);
        
        opt.text = ret[i].attributes.getNamedItem("Name").value;
        opt.value = ret[i].attributes.getNamedItem("ID").value;
        opt.hasStates = ret[i].attributes.getNamedItem("States").value;
        
    }
    
    clist.onchange = function() {
        disableReg();
        getStates(this.value);
    }   
    
    getStates(clist.value);
}

function getStates(IDCountry)
{
    Comm.sendCommand("<CMD name='GetStates'><PARAM name='IDCOUNTRY'>"+IDCountry+"</PARAM></CMD>", cb_GetStates, "Getting states list...");
}

function cb_GetStates(args)
{
    var slist = document.getElementById("ddlStates");
    var ret, i, opt, n; 
    
    try {
        ret = args.selectNodes("RET/STATE");
    } catch(e0) {
        alert("Internal ERR#1006: Failed to get state list!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    n=slist.options.length;
 
    for(i=0;i<n;i++) { slist.options.remove(0); }
    
    if(ret.length>0) {
        for(i=0;i<ret.length;i++) {
            opt = document.createElement("OPTION");
            slist.options.add(opt,i);
            
            opt.innerText = ret[i].attributes.getNamedItem("Name").value;
            opt.value = ret[i].attributes.getNamedItem("ID").value;
        }
        slist.parentNode.parentNode.style.display="block";
    } else {
        opt = document.createElement("OPTION");
        slist.options.add(opt,i);
        
        opt.innerText = "N/A";
        opt.value = 0;
        slist.parentNode.parentNode.style.display="none";
    }
    
    enableReg();
}

function validateRegData()
{
    if(getev("tbFName").length<2) { alertErr("Please provide a proper first name"); return false; }
    if(getev("tbLName").length<2) { alertErr("Please provide a proper last name"); return false; }
    if(!checkEmailAddr(getev("tbEmail"))) { alertErr("Please provide a proper email address"); return false; }
    if(getev("tbCity").length<2) { alertErr("Please specify your city"); return false; }
    
    if(getev("tbUsername").length<_LenCrit) { alertErr("Please provide a proper username<br>("+_LenCrit+" characters min)"); return false; }
    if(getev("tbPassword").length<_LenCrit) { alertErr("Please provide a proper password<br>("+_LenCrit+" characters min)"); return false; }
    if(getev("tbPassword")!=getev("tbPasswordConfirm")) { alertErr("Passwords do not match"); return false; }
    if(getev("tbPassword")==getev("tbUsername")) { alertErr("Username and password cannot be the same"); return false; }
    if(getev("tbPassReminder").length<2) { alertErr("Please provide a proper password reminder"); return false; }
    
    return true;
}

function gete(id)
{
    return document.getElementById(id);
}

function getev(id)
{
    return escape(document.getElementById(id).value);
}

function checkEmailAddr(val)
{
    //Advanced Email Check credit-
    //By JavaScript Kit (http://www.javascriptkit.com)
    //Over 200+ free scripts here!
    //*** Adjusted by SpindleScape for internal usage

    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(val)) return true;
    else return false;

    return false;
}


function registerNewCustomer()
{
    var cmdstr = "";
    
    disableReg();
    alertErr("Registering your account<br>Please wait...", "REG");
    
    try {
        cmdstr += "<CMD name='Register'>";
        cmdstr += "<PARAM name='SAL'>"+getev("ddlSalutation")+"</PARAM>";
        cmdstr += "<PARAM name='FNAME'>"+getev("tbFName")+"</PARAM>";
        cmdstr += "<PARAM name='LNAME'>"+getev("tbLName")+"</PARAM>";
        cmdstr += "<PARAM name='EMAIL'>"+getev("tbEmail")+"</PARAM>";
        cmdstr += "<PARAM name='IDCOUNTRY'>"+getev("ddlCountries")+"</PARAM>";
        cmdstr += "<PARAM name='IDSTATE'>"+getev("ddlStates")+"</PARAM>";
        cmdstr += "<PARAM name='CITY'>"+getev("tbCity")+"</PARAM>";
        cmdstr += "<PARAM name='POSTCODE'>"+getev("tbPostCode")+"</PARAM>";
        cmdstr += "<PARAM name='ADDRESS'>"+getev("tbAddress")+"</PARAM>";
        cmdstr += "<PARAM name='PHONE'>"+getev("tbPhone")+"</PARAM>";
        cmdstr += "<PARAM name='USERNAME'>"+getev("tbUsername")+"</PARAM>";
        cmdstr += "<PARAM name='PASSWORD'>"+getev("tbPassword")+"</PARAM>";
        cmdstr += "<PARAM name='REMINDER'>"+getev("tbPassReminder")+"</PARAM>";
        cmdstr += "<PARAM name='NEWS'>"+(gete("cbNews").checked ? "1" : "0")+"</PARAM>";
        cmdstr += "</CMD>";
        
        
        Comm.sendCommand(cmdstr, cb_RegisterNewCustomer, "Registering customer...");
    } catch(e) {
        closeAlert();
        alertErr("Could not process your request<br><br>Please verify your data and try again<br>"+e.message);
        enableReg();
    }
}

function cb_RegisterNewCustomer(args)
{
    var ret; 
    
    try {
        ret = args.selectSingleNode("RET/REG");
    } catch(e0) {
        alert("Internal ERR#1007: Failed to register a customer!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    closeAlert();
    if(parseInt(ret.attributes.getNamedItem("STATUS").value)==0) {
        alertErr(ret.attributes.getNamedItem("MSG").value+"<br><br>Please verify your details and try again");
        enableReg();
    } else {
        updateUserIndicator();
        gotoPage("NEWS");
        alertErr("You have been successfully registered", "REGOK");
        setTimeout("regLogin();",3000);
    }        
}

function regLogin()
{
    closeAlert();
}


function alertErr(str, mode)
{
    var collSelect, i;
    
    if(_AlertWin==null) {
        _AlertWin = document.createElement("DIV");
        _AlertWin.className = "AlertWin";
        
        _AlertLace = document.createElement("DIV");
        _AlertLace.className = "AlertLace";
        _AlertLace.onclick = function() { try { event.cancelBubble = true; event.returnValue = false; } catch(e) {}  return false; }
        
        _AlertLace.style.left = _contentArea.offsetLeft;    
        _AlertLace.style.width = _contentArea.offsetWidth;
        _AlertLace.style.top = _contentArea.offsetTop;
        _AlertLace.style.height= _contentArea.offsetHeight;
        _AlertLace.innerHTML="&nbsp;";
        
        var _AlertBHolder = document.createElement("DIV");
        _AlertBHolder.className="AlertBHolder";
        
        _AlertButton = document.createElement("BUTTON");
        _AlertButton.lace = _AlertLace;
        _AlertButton.holder = _AlertWin;
        _AlertButton.innerHTML="OK";
        _AlertButton.className = "AlertButton";
        _AlertButton.onclick = function() {
            var i;
            for(i=0;i<this.selects.length;i++) {
                this.selects[i].style.display="inline";
            }
            this.holder.style.display="none";
            this.lace.style.display="none";
        }
        
        _AlertMsg = document.createElement("DIV");
        _AlertMsg.className = "AlertMsg";
        
        _AlertWin.appendChild(_AlertMsg);      
        _AlertBHolder.appendChild(_AlertButton);      
        _AlertWin.appendChild(_AlertBHolder);      
        _AlertLace.appendChild(_AlertWin);
        document.body.appendChild(_AlertLace);
        
    }
    
    collSelect = document.getElementsByTagName("SELECT");
        
    for(i=0;i<collSelect.length;i++) {
        collSelect[i].style.display="none";
    }
    
    if(!mode || mode == "") { _AlertButton.style.display="inline"; } else { _AlertButton.style.display="none"; }
    
    _AlertButton.selects = collSelect;
    
    _AlertMsg.innerHTML = str;
    _AlertLace.style.display="block";
    _AlertWin.style.display="block";            
}

function closeAlert()
{
    if(_AlertWin) {
        _AlertWin.style.display="none";
        _AlertLace.style.display="none";
    }        
}

// -------------------------------------------------------------------------------------------------

function getPurchaseConfirmation()
{
    Comm.sendCommand("<CMD name='GetPurchaseInfo'></CMD>", cb_getPurchaseInfo, "Getting purchase info..."); 
}

var _pobj = new Object();

function cb_getPurchaseInfo(args)
{
    var r = args.selectSingleNode("RET");
    
    _pobj.productId = r.selectSingleNode("PID").text;
    _pobj.orderNr = r.selectSingleNode("OID").text;
    _pobj.amount = r.selectSingleNode("AMN").text;
    _pobj.productName =r.selectSingleNode("PNM").text;
    
    placePurchaseData();
}

function placePurchaseData()
{    
    var el = document.getElementById("PurchaseHolder");
 
    if(el==null) { setTimeout("placePurchaseData();", 50); return; }    
    
    var subel = document.createElement("CENTER");
    
    var ctab = document.createElement("TABLE");
    ctab.cellPadding=4;
    ctab.cellSpacing=0;
    ctab.border=0;
    ctab.style.backgroundColor = "#ffffff";
    ctab.style.border="1px outset #eeeeee";
    ctab.style.width="250px";
    ctab.style.padding="10px;";
    
    var crow = ctab.insertRow(0);
    var ccel = crow.insertCell(0);
    ccel.style.width="50px";
    ccel.innerHTML = "Product:";
    ccel.style.textAlign="right";
    ccel = crow.insertCell(1);
    ccel.style.color="black";
    ccel.innerHTML = "<b>"+_pobj.productName+"</b>";
    ccel.style.textAlign="left";
    
    crow = ctab.insertRow(1);
    ccel = crow.insertCell(0);
    ccel.innerHTML = "Order ID:";
    ccel.style.textAlign="right";
    ccel = crow.insertCell(1);
    ccel.style.color="black";
    ccel.innerHTML = "<b>"+_pobj.orderNr+"</b>";
    ccel.style.textAlign="left";
    
    crow = ctab.insertRow(2);
    ccel = crow.insertCell(0);
    ccel.innerHTML = "Amount:";
    ccel.style.textAlign="right";
    ccel = crow.insertCell(1);
    ccel.style.color="black";
    ccel.innerHTML = "<b>"+"$"+_pobj.amount+"</b>";
    ccel.style.textAlign="left";
    
    crow = ctab.insertRow(3);
    ccel = crow.insertCell(0);
    ccel.colSpan=2;
    ccel.style.textAlign="center";
    
    var dlb = document.createElement("BUTTON");
    dlb.className = "btDownload";
    dlb.pid = _pobj.productId;
    dlb.onclick = function() {
        DownloadProductB(this.pid);
    }
    ccel.appendChild(dlb);
    
    subel.appendChild(ctab);
    
    el.appendChild(subel);
    
}

// SEF
  
function cb_isLoggedInForSEF(args)
{
    var ret; 
    
    try {
        ret = args.selectSingleNode("RET/ISLOGGEDIN");
    } catch(e0) {
        alert("Internal ERR#10C3: Failed to verify login!\n"+e0.message+"\n\n"+args); 
        return;
    }
    
    if(ret.attributes.getNamedItem("STATE").value=="1") {
        fetchItemsForSection("SEF");
    } else {
        fetchItemsForSection("SEF-LOGIN");
    }
}