function HandleAJAXError(e, clientID) {
    //alert(e.message);
}

function Body_OnLoad() {
    checkLowRes();
    searchOnBlur();
    if (window.Page_OnLoad != null) { window.Page_OnLoad(); }
}

function checkLowRes() {
    if (screen.width <= 1024 && screen.height <= 768 && (getCookie('lowres') == null || getCookie('lowres') == '')) {
        setCookie('lowres', true, 7);
        window.location.reload();
    }
}

function comboBox_ClientValidate(cb, args) {
    args.IsValid = false;
    var text = cb.get_text();
    if (text.length < 1) {
        args.IsValid = false;
    }
    else {
        var node = cb.findItemByText(text);
        if (node) {
            var value = node.get_value();
            args.IsValid = (value.length > 0);
        }
    }
}

function searchOnFocus() {
    var q = document.getElementById('q');
    var segments = document.location.pathname.split("/");
    if (segments.length < 3 || segments[2] != 'search') {
        q.value = '';
    }
}

function searchOnBlur() {
    var q = document.getElementById('q');
    var segments = document.location.pathname.split("/");
    if (segments.length < 3 || segments[2] != 'search') {
        q.value = q_default;
    }
    else {
        q.value = decodeURI(getQuerystringParameter('q'));
    }
}

function searchSubmit() {
    var q = document.getElementById('q');
    var lang = document.location.pathname.split("/")[1];
    if (q != null && q.value != '' && q.value != q_default) {
        var searchUrl = '/' + lang + '/search?q=' + encodeURI(q.value) + '&cx=006379224448280766962%3Axrhrp2eoswc&cof=FORID%3A9&ie=UTF-8';
        window.location.href = searchUrl;
        window.event.returnValue = false; // causes onclick event to fire in IE 
    }
}

// helper functions
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function catchEnterKey(event, action) {
    if (document.all) {
        if (event.keyCode == 13) {
            event.returnValue = false;
            event.cancel = true;
            action();
        }
    }
    else if (document.getElementById) {
        if (event.which == 13) {
            event.stopPropagation();
            event.preventDefault();
            action();
        }
    }
    else if (document.layers) {
        if (event.which == 13) {
            event.returnValue = false;
            event.cancel = true;
            action();
        }
    }
}

function getQuerystringParameter(parameter) {
    // get the current URL
    var url = window.location.toString();
    // get the parameters
    url.match(/\?(.+)$/);
    var params = RegExp.$1;
    // split up the query string and store in an associative array
    var params = params.split("&");
    for(var i=0; i<params.length; i++) {
 	    var tmp = params[i].split("=");
 	    if (tmp[0] == parameter) return tmp[1];
    }
    return null;
}
