//Motorola Vignette Development Team - 2008

function getXmlHttp() {
	try {
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
	  	}
	}
	return null;
}

//Improvements in the clear cache on management stage 
function clearMgmtCache(url, vgnextoid) {
	xmlHttp = getXmlHttp();
	document.getElementById(vgnextoid).innerHTML='Clearing cache...';	
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function()
	{
	    if (xmlHttp.readyState == 4) {	    	
	    	window.location.reload(); 
	    }
	}
  	xmlHttp.send(null);
}

// Clears Akamai cache by invoking a servlet, passing the URL and the logged user's e-mail
function clearAkamaiCache(url, vgnextoid) {
	xmlHttp = getXmlHttp();
	
	var oldInnerHtml = document.getElementById(vgnextoid).innerHTML;
	document.getElementById(vgnextoid).innerHTML='Clearing cache...';	

	// Testing whether the user is logged in or not
    xmlHttp.open("POST", "/AppConsole" ,false);
    xmlHttp.send(null);

    var responseText = xmlHttp.responseText;

    if(responseText.indexOf("j_username") >= 0){
    	// User is not logged in. Popping up the login screen
		alert("You're currently not logged in. Please log in and try again.");
		window.open("/AppConsole/common/login.jsp");
    } else {
		var regex = new RegExp(/<span class="vign-listTitle">Task Inbox : (\w*)<\/span>/);
		if (regex.exec(responseText) == null) {
			alert("Unable to identify logged user");
		} else {
			xmlHttp.open("GET", "/gfext/secure/akamai/servlet/clearAkamaiCache?url=" + escape(url) + 
					"&email=" + escape(RegExp.$1) + "@motorola.com", true);
			xmlHttp.onreadystatechange = function()
			{
			    if (xmlHttp.readyState == 4) {
			    	window.alert(xmlHttp.responseXML.getElementsByTagName("msg").item(0).firstChild.data);
			    	document.getElementById(vgnextoid).innerHTML = oldInnerHtml;
			    }
			}
			xmlHttp.send(null);
		}
    }
    document.getElementById(vgnextoid).innerHTML = oldInnerHtml;
    return true;
}

// Fixes the ICE menu, repositioning it dynamically
function fixICEMenu() {
	var iceMenu = document.getElementById('menu');
	iceMenu.style.top=(iceMenu.offsetHeight - 7)*-1 + 'px';	
}
