
function addLoadEvent(loadAction) {
    if (window.addEventListener) {
        window.addEventListener("load", loadAction, false);
    } else if (window.attachEvent) {
        try {
            window.detachEvent("onload", loadAction);
        } catch(e) {}
        window.attachEvent("onload", loadAction);
    } else {
        window.onload = loadAction;
    }
}

function trim(text) {
    return text.replace(/^\s*/, "").replace(/\s*$/, "");
}

/**
 * Sets the value of the specified cookie
 *
 * @param name Name of the desired cookie
 * @param value a string containing value of specified cookie
 * @param date expiration date of the cookie
 */
function setCookie(name, value, date) {
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + "; path=/";
}

/**
 * Gets the value of the specified cookie.
 *
 * @param name  Name of the desired cookie.
 * Returns a string containing value of specified cookie, or null if cookie does not exist.
 */
function getCookie(name) {
   var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) {
            return null;
        }
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function addCSSClass(element, newClass) {
    var classValues = element.className;
    if (classValues.indexOf(newClass) != -1){
        return;
    }
    if (classValues && classValues.length > 0) {
        classValues += " " + newClass;
    } else {
        classValues = newClass;
    }
    element.className = classValues;
    return classValues;
}

function removeCSSClass(element, className){
    var classValues = element.className;
    if (classValues && classValues.indexOf(className) != -1) {
        classValues = classValues.substring(0, classValues.indexOf(className));
    }
    element.className = classValues;
    return classValues;
}

function getElementsByClassName(className, tagName) {
    var elementsByClass = new Array();
    var re = new RegExp('\\b' + className + '\\b');
    var elements = document.getElementsByTagName(tagName);
    for (var i = 0; i < elements.length; i++) {
        if (re.test(elements[i].className)) {
            elementsByClass.push(elements[i]);
        }
    }
    return elementsByClass;
}

function isIe6() {
    var browser = navigator.appName;
    var version = parseFloat(navigator.appVersion);
    if ((browser == "Microsoft Internet Explorer" || browser == "MSIE") && version <= 6) {
        return true;
    }
    return false;
}

function isFireFox() {
    return navigator.userAgent.indexOf("Firefox") != -1;
}

/*
 * Add style button to all the buttons, reset, and submit input fields
 * as the attribute selector fails on IE version 6,
 */
function fixIE6ButtonStyles() {
/*    if (isIe6()) {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i] && inputs[i].type) {
                var type = inputs[i].type;
                if (type == "button" || type == "reset" || type == "submit") {
                    inputs[i].style.border = "none";
                    addCSSClass(inputs[i], "button");
                    inputs[i].style.border = "";
                }
            }
        }
    }
*/
}

function ajaxCall(url, timeout) {
    var xmlHttpReq;
    var responseReturned;
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    responseReturned = false;
    xmlHttpReq.open('GET', url, true);
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
            responseReturned = true;
        }
    }
    xmlHttpReq.send(null);
    currentTime = new Date().getTime;
    while ((new Date().getTime() - currentTime) < timeout && !responseReturned) {}
}

// @author Hazem Saleh, function to attach any event to any component for all browsers
function attachEventToComponent(component, eventName, eventHandler) { 
    // bind the event with the component.
    if (component.addEventListener) {
        component.addEventListener(eventName, eventHandler, false);
    } else if (component.attachEvent) {
        try {
            component.detachEvent("on" + eventName, eventHandler);
        } catch(e) {}
        component.attachEvent("on" + eventName, eventHandler);
    }
}

function formIsBusy(formObj){
	// classes are a space separated list
    var arrList = formObj.className.split(' ');
    for ( var i = 0; i < arrList.length; i++ )
    {
    	// if the submmting class found, then form is busy submmting
		if ( arrList[i].toLowerCase() == "submitting".toLowerCase() )
		{
            return true;
        }
     }
     return false;
}

function setBusyForm(formObj){
	formObj.className += "submitting";
}
