/* $Id: global.js 20590 2008-03-19 17:57:56Z nmew $ */
/* global javascript functions for the corp webapp */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/* Login Form */
function clearDefaultValue(input) {
    // if this is the default password field (textfield that has a value 'Password'),
    // hide it and deplay the real password field
    if(input.id == "passwordtxt" && input.type == "text") {
        document.getElementById("passwordtxt").style.display = "none";
        document.getElementById("password").style.display = "inline";
	    document.getElementById("password").focus();
    } // if this is the login field and it has a default value, clear the default value
    else {
        if(input.id == "username" && input.value == "Email Address") {
            input.value = "";
        }
    }
}

function addDefaultValue(input) {
    // if this is the real password field and it is empty, make it the
    // default password field (a text field w/ value of 'Password')
    if(input.id == "password" && input.value.trim() == "") {
        document.getElementById("passwordtxt").style.display = "inline";
        document.getElementById("password").style.display = "none";
    } // if this is the login field and it is empty, add a default value
    else {
        if(input.id == "username" && input.value.trim() == "") {
            input.value = "Email Address";
        }
    }
}

/* Functions to check if there are items in the store */
function loadCartContent() { //get json from Nanocart.do using ajax
    new Ajax.Request("/store/Nanocart.do?time=" + new Date().getTime(), {
        method: 'get',
        onSuccess: function(transport) {
            // Get JSON values
            var jsonRaw = transport.responseText;
            // Eval JSON response into variable
            var cart = eval("(" + jsonRaw + ")");
            /*alert(cart.itemcount);*/
            writeCartContent(cart.itemcount);
        }
    });
}

function writeCartContent(itemCount) {
    if (itemCount > 0) {
        $("nanocart").className += " full";
        var details = itemCount + " Item";
        if (itemCount > 1) {
            details += "s";
        }
        $("nanocartDetails").innerHTML = details;
        //$("nanocartImage").alt = details;
        //$("nanocartImage").title = details;
        $("nanocartImage").style.display = "block";
        $("nanocartImage").className += " translucent";
    } else {
        $("nanocart").innerHTML = " ";
    }
}

