// The domain we reside in (include trailing slash on URL)

//var domain = 'http://acidburn.cofa.unsw.edu.au/~sambauers/ADT/';
//var domain = 'http://adt-test.library.unsw.edu.au/';
//var domain = 'http://adt-beta.library.unsw.edu.au/';
var domain = 'http://adt.caul.edu.au/';



/* CART FUNCTIONS */
// Mostly copied from metacart.js in hotmeta

// Default timeout is 2 days.
var m_timeoutDays = 2;

/**
 * Retrieve value of named cookie
 */
function getCookie (name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var idx = 0;
    
    while (idx < clen) {
        var j = idx + alen;
        if (document.cookie.substring(idx, j) == arg) {
            return getCookieVal(j);
        }
        idx = document.cookie.indexOf(" ", idx) + 1;
        if (idx == 0) {
            break;
        }
    }
    return null;
}

/**
 * Extract cookie value from separator index offset
 */
function getCookieVal (offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

/**
 * Sets/creates a named cookie.  Optional args (in sequence) are:
 * expiration date, path, domain, secure.  Naturally the preceeding
 * args must be present, but may be 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 path = "/";
    var cdomain = (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)) +
                      ((cdomain == null) ? "" : ("; domain=" + cdomain)) +
                      ((secure == true) ? "; secure" : "");
}

/**
 * Purges the named cookie
 */
function deleteCookie ()
{
    var name = "metacart";
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);  // This cookie is history
    document.cookie = name + "=\"\"; path=/; expires=" + exp.toGMTString();
    viewCart();
}

/**
 * Adds the passed HotMeta record ID to the MetaCart cookie if it
 * is not already there.
 * The cookie timeout is reset to now + m_timeout (days)
 */
function addToCookie (recId)
{
    var period = m_timeoutDays * 24 * 60 * 60 * 1000;
    var expireDate = new Date();
    expireDate.setTime(expireDate.valueOf() + period);
    
    var cartVal = getCookie("metacart");
    if (cartVal == null) {
        cartVal = recId;
    } else {
        var ids = cartVal.split(",");
        var found = false;
        for (var idx = 0; idx < ids.length; idx++) {
            if (ids[idx] == recId) {
                found = true;
                break;
            }
        }
        if (!found) {
            cartVal += "," + recId;
        }
    }
    setCookie("metacart", cartVal, expireDate);
    updateCount();
}

/**
 * Removes the passed HotMeta record ID from the MetaCart cookie if present.
 * The cookie timeout is reset to now + 24 hours unless the value is
 * empty in which case it is deleted. If a change was made, we force a
 * refresh of the cart page.
 */
function removeFromCookie (recId)
{
    var period = m_timeoutDays * 24 * 60 * 60 * 1000;
    var expireDate = new Date();
    expireDate.setTime(expireDate.valueOf() + period);
    
    var cartVal = getCookie("metacart");
    if (cartVal != null) {
        var ids = cartVal.split(",");
        var idx = 0;
        var tmp = "";
        while (idx < ids.length) {
            if (ids[idx] != recId) {
                tmp += ("," + ids[idx]);
            }
            ++idx;
        }
        if (tmp.length == 0) {
            deleteCookie("metacart");
        } else {
            cartVal = tmp.substring(1, tmp.length);
            setCookie("metacart", cartVal, expireDate);
        }
        viewCart();
    }
}

/**
 * Update cart count display.  This method assumes there may be two INPUT
 * text objects on the page with the ids "CartCntTop" and "CartCntBottom".
 */
function updateCount ()
{
    var cnt = 0;
    var cartVal = getCookie("metacart");
    if (cartVal != null) {
        var ids = cartVal.split(",");
        cnt = ids.length;
    }
    
    var navCnt = document.getElementById('CartCntNav');
    if (navCnt) {
        navCnt.innerHTML = cnt;
    }
    
    var topCnt = document.getElementById('CartCntTop');
    if (topCnt) {
        topCnt.innerHTML = cnt;
    }
    
    var botCnt = document.getElementById('CartCntBottom');
    if (botCnt) {
        botCnt.innerHTML = cnt;
    }
}

/**
 * Fire a query to view the cart content
 */
function viewCart ()
{
    //var regex = /&ranking=\w*/;
    //var res = regex.exec(location.toString());
    //var ranking = (res != null) ? res[0] : "";
    window.location = domain + 'homesearch/cart/';
}



/* OTHER FUNCTIONS */

/**
 * Pops up the site info window
 */
function siteInfo ()
{
    window.open (domain + 'siteinfo/','adtSiteinfo','height=340,width=300,left=40,top=40,toolbar=no,status=no,statusbar=no,menubar=no,location=no,resizable=no,scrollbars=yes,directories=no');
    return false;
}

/**
 * Just checks if the search form on the start page is blank
 */
function checkBlank (formObj)
{
    if (formObj.val0.value) {
        return true;
    } else {
        alert ('You have not typed in your query.');
        formObj.val0.focus();
        return false;
    }
}
