function isEmpty(s) {
	return s == null || s == "undefined" || s == "";
}

function isNotEmpty(s) {
	return !isEmpty(s);
}

function getCookie(name)
{
var i,cname,cvalue,cookiez=document.cookie.split(";");
for (i=0;i<cookiez.length;i++)
{
  cname = cookiez[i].substr(0,cookiez[i].indexOf("="));
  cvalue = cookiez[i].substr(cookiez[i].indexOf("=")+1);
  cname = cname.replace(/^\s+|\s+$/g,"");
  if (cname==name)
    {
    return unescape(cvalue);
    }
  }
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function closeDetail(elementId) {
	document.getElementById(elementId + '_Open').style.display = 'none';
	document.getElementById(elementId + '_Closed').style.display = 'block';
}

function openDetail(elementId) {
	document.getElementById(elementId + '_Open').style.display = 'block';
	document.getElementById(elementId + '_Closed').style.display = 'none';
}

function removeDivWithoutLink(element) {
	var aElements = element.getElementsByTagName('a');
	if (aElements.length == 0) {
		element.parentNode.removeChild(element);
	}
}

//'xmlhttp' request object, do not access directly, use getXmlHttp instead
var xmlhttp = null;

function getXmlHttp() {	
	if (xmlhttp) {
    return xmlhttp;
    }
	
    if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (ex) {
        try {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (ex) {
        }
    }
    }
    return xmlhttp;
}


function sendRequest(/* String */contextPath, /* String */user,
        /* String */ pass) {
    //Warning: there is a copy of this method in /libs/cq/ui/widgets/source/User.js#login
    //changing this method means also changing User.js#login until a unique
    //and central method is implemented
    //current method is the master
    
    var xmlhttp = getXmlHttp();
    if (!xmlhttp) {
    return;
    }

    if (xmlhttp.readyState < 4) {
    xmlhttp.abort();
    }

    // send the authentication request
    xmlhttp.open('GET', contextPath + "?sling:authRequestLogin=1", false, user, pass);
    xmlhttp.send('');
    
    // check result against 403/FORBIDDEN sent by the server
    // if the credentials are wrong (other status codes like
    // 200/OK, 404/NOT FOUND or even 500/INTERNAL SERVER ERROR
    // should be considered as login success)
    return xmlhttp.status != 403;
}


function loginuser() {
    var contextPath = document.forms['login'].contextPath.value;
    var user = document.forms['login'].usr.value;
    var pass = document.forms['login'].pwd.value;
    var resource = document.forms['login'].resource.value;
    
    // if no user is given, avoid login request
    if (!user) {
        return false;
    }

    // send user/id password to check and persist
    if (sendRequest(contextPath, user, pass)) {

        var u = resource;
        if (window.location.hash) {
            u = u + window.location.hash;
        }
        document.location = u;

    } else {

        sendRequest(contextPath, '__failed_login_user__', 'null');

    }

    return false;
}
