// AustralianPlays.org apMisc.js 21/2/11

// replaces apAjax.js


function SetCookie(cookieName,cookieValue,nDays)
	{ var today = new Date(); var expire = new Date(); if (nDays==null || nDays==0) nDays=1; expire.setTime(today.getTime() + 3600000*24*nDays); document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString(); }
function ReadCookie(cookieName)
	{ var theCookie=""+document.cookie; var ind=theCookie.indexOf(cookieName); if (ind==-1 || cookieName=="") return "";  var ind1=theCookie.indexOf(';',ind); if (ind1==-1) ind1=theCookie.length; return unescape(theCookie.substring(ind+cookieName.length+1,ind1)); }
// CookiesEnabled - only works if javascript is enabled, otherwise use <noscript> tag
function CookiesEnabled()
	{ SetCookie('tst','tst',1); if (ReadCookie('tst')=='') return false; else return true; }


// Set "localtime" cookie (see plugin.apWebUserLogin)
// (NEW 16/1/2012 for SharedLogin)
var today = new Date();
today.setTime(today.getTime());
SetCookie("localtime", today.toGMTString(), 1);


function toggleLibSw()
    {
    if(0==ReadCookie("aplibsw"))
        { SetCookie("aplibsw","1",1); }
    else
        { SetCookie("aplibsw","0",1); }
    setLibSw();
    }
function setLibSw()
    {
    var YOffset=(1==ReadCookie("aplibsw")) ? "-25px" : "0";
    document.getElementById("libsw").style.backgroundPosition = "0 "+YOffset;
    }

function togglePwd(Id) // new 18/1/12
    {
    // setAttribute(type) works in IE9, Firefox on Win + Safari, Chrome, Firefox on Mac
    // but not in IE7, IE8 unless we remove and re-add the DOM node! nww?
    
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1) version = parseFloat(navigator.appVersion.split("MSIE")[1]);
  
    if(version<9)
        {
        alert("Sorry, this feature is not supported by your old browser!");
        return false;
        }
    else
        {
        var box=document.getElementById(Id);
        if(box)
            {
            if("text"==box.getAttribute("type"))
                {
                box.setAttribute("type", "password");
                SetCookie("ownerhp", "1", 365);
                }
            else
                {
                box.setAttribute("type", "text");
                SetCookie("ownerhp", "0", 365);
                }
            }

        return true;
        }
    
    }
    
    
//global vars because we can't pass param to updatePage()
var actionType=""; 
var elemResult=""; 
var elemClicked;
var inProdList=false;

function setElementValue(elementId, newValue)
    {
    // Called by updatePage() and also from js inserted into cart page
    if(document.getElementById(elementId).firstChild)
        { document.getElementById(elementId).firstChild.data=newValue; }
    else // IE8?
        { document.getElementById(elementId).innerHTML=newValue; }
    }

function ajaxAdd(addWhat, addId, changeElemId, elClicked, inList)
    {
    // addWhat = cart | playlist | freescript -- + general WIP 1/3/11
    // addId   = ProductRef
    // changeElemId  = element id to set with result
    
    // inList = true for button in product list, false for button on product page (new 21/2/2011)

    actionType=addWhat; 
    elemResult=changeElemId; 
    elemClicked=elClicked;
    inProdList=inList;
    document.body.style.cursor = 'wait';
    elemClicked.style.cursor = 'wait';
    sendRequest(addWhat, addId);
    }

function ajaxCall(whatAction, whatOption, changeElemId, elClicked)
    {
    //new 1/3/2011 - a bit more general than ajaxAdd() when we don't want to actually add anything!
    ajaxAdd(whatAction, whatOption, changeElemId, elClicked, false);
    }
    
var http=createRequestObject();

function createRequestObject()
    {
    var reqobj=false;
    var browserName=navigator.appName;
    if(browserName=="Microsoft Internet Explorer") { try { reqobj=new ActiveXObject("Microsoft.XMLHTTP"); }catch(otherTypeOfMicrosoftObject) { try { reqobj=new ActiveXObject("Msxml2.XMLHTTP"); } catch(giveUp) { reqobj=false; } } }
    else { try { reqobj=new XMLHttpRequest(); } catch (NothingLeft) { reqobj=false; } }
    return reqobj;
    }

function sendRequest(Action,Value)
    {
    var url="/apAjax.php?action=" + escape(Action) + "&val=" + escape(Value);
    http.open("GET",url,true);
    http.onreadystatechange=updatePage;
    http.send(null);
    }
	
function updatePage() // sets .innerHTML for elemResult
    {
    if(4==http.readyState)
        {
        var theResult = (200==http.status) ? http.responseText : 'error ' + http.status ;
        if(elemResult) setElementValue(elemResult, theResult);
        switch(actionType)
            {
            case 'cart':
                elemClicked.src='/assets/templates/ausplays/images/incart.gif';
                elemClicked.alt='in cart';
                elemClicked.title='in cart';
                elemClicked.onclick=function() {}; // remove onclick so that href will work (new 13/2/12)
                if(inProdList) // 2/1/2011 new layout
                    { elemClicked.parentNode.style.backgroundPosition = "0 21px"; }
                else
                    { elemClicked.innerHTML='in cart'; }
                break;
            case 'playlist':
                elemClicked.src='/assets/templates/ausplays/images/onlist.gif';
                elemClicked.alt='on reading list';
                elemClicked.title='on reading list';
                elemClicked.onclick=function() {}; // remove onclick so that href will work (new 13/2/12)
                if(inProdList) // 2/1/2011 new layout
                    { elemClicked.parentNode.style.backgroundPosition = "0 21px"; }
                else
                    { elemClicked.innerHTML='on reading list'; }
                break;
            case 'freescript':
                elemClicked.src='/assets/templates/ausplays/images/isfree.gif';
                elemClicked.alt='on free list';
                elemClicked.innerHTML='on free list'; // 2/1/2011 for new layout
                break;
            case 'general': // WIP 1/3/2011 for backend fetch
                if(elemResult) document.getElementById(elemResult).innerHTML=theResult;
                // TEST 8/3/11: we've already called setElementValue to set it once! 
                // .. but this time we use innerHTML
                break;
            default:
            }

        document.body.style.cursor = 'default';
        elemClicked.style.cursor = 'default';
        }
    }

