// htmlhead.js

// Generates content urls, for usage by other js functions
function buildContentURL(key) {

	if (requestType == 'statictml' || requestType == 'null') {
		return "";
	}


	if (mediaKeyMode == 1) {
		if (key != null) {
			return WGPPath + "/" + myLayout + "/" + key + "." + myMedium;
		}
		else {
			return WGPPath + "/" + myLayout + "." + myMedium;
		}
	}
	else {
		if (key != null) {
			return WGPPath + "/" + myMedium + "/" + myLayout + "/" + key;
		}
		else {
			return WGPPath + "/" + myMedium + "/" + myLayout;
		}
	}
}

// Loads content of the given key
function loadContent(key) {

	location.href = buildContentURL(key);

	
}

function callAction(actionLink) {

	if (actionLink.indexOf('/') == -1) {
		location.href = buildContentURL(myContentKey) + "?$action=" + actionLink;
	}
	else {
		var divider = actionLink.indexOf('/');
		var formID = actionLink.substring(0, divider);
		var realActionLink = actionLink.substring(divider + 1);
		var form = document.forms[formID];
		if( !form || !form.$formaction){
			alert("Error while calling action. TMLForm \"" + formID + "\" not found.");
			return false;
		}
		form.$formaction.value = realActionLink;
		form.submit();
	}
	return true;
}

function callAjaxAction(action, id, graydiv, callback) {
	
	if (action.indexOf('/') == -1) {
		// retrieve ajaxInfo object
		var ajaxInfo = eval("$ajaxInfo_" + id);
		// build url
		var url = buildContentURL(myContentKey) + "?$action=" + action;
		// do post
		ajaxPost(url, id, ajaxInfo, '', graydiv, callback);
	}
	else {
		var divider = action.indexOf('/');
		var formID = action.substring(0, divider);
		var realAction = action.substring(divider + 1);
		var form = document.forms[formID];
		if( !form || !form.$formaction){
			alert("Error while calling action. TMLForm \"" + formID + "\" not found.");
			return false;
		}
		form.$formaction.value = realAction;
		form.$ajaxcallid.value = id;		
		form.target = "$ajaxIFrame_" + id;
	    form.action = WGAContextPath + "/ajaxform";				
		form.submit();
	}	
}

function ajaxFormCallback(action, id, sessionkey) {
		// retrieve ajaxInfo object
		var ajaxInfo = eval("$ajaxInfo_" + id);
		// build url
		var url = buildContentURL(myContentKey) + "?$action=" + action;
		// do post
		ajaxPost(url, id, ajaxInfo, sessionkey);
}

function ajaxUpdate(str, id){	
    var divTag = document.getElementById("$ajaxDiv_" + id);
    if (divTag==null)
    	return;
    // IE does not recognize the first script tag by getElementsByTagName
    // if there is no html:tag with content in front
    // so add a none displayed span-tag in front of the pasted code
	divTag.innerHTML = "<div id='$ajaxContentDiv_" + id + "'><span style=\"display:none\">ajax</span>" + str + "</div>";
	
    // evaluate pasted javascript
    var scripts = divTag.getElementsByTagName("script");
    var heads = document.getElementsByTagName("head");
    for (var i = 0; i < scripts.length; i++) {
    	var script = scripts[i].text;
		var script_element = document.createElement("script");
		// first append as child of head
		// Opera execute scripts twice if the next to lines are changed in order
		heads[0].appendChild(script_element);	    	    				
		script_element.text = script;
	}
}

function ajaxPost(strURL, id, ajaxInfo, sessionkey, graydiv, callback) {
    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var divTag = document.getElementById("$ajaxContentDiv_" + id);
    if(divTag!=null){
    	if(graydiv==undefined || graydiv){
	    	//divTag.innerHTML="<h1>Loading ...</h1>";
	    	// for mozilla:
			divTag.style.opacity=0.2;
			// for IE:
			divTag.style.width=divTag.offsetWidth;		// IE needs a "layout-attribute" like width
	   	    divTag.style.filter="alpha(opacity:20)";

		   	// capture all mouse events until div is reloaded.
		   	if(document.all){
				divTag.setCapture(true);		// IE: capture all mouse events
			}
			else if (divTag.addEventListener) {
				divTag.addEventListener("click", function (event) {				 
							event.stopPropagation();
						}, true);
				divTag.addEventListener("mouseover", function (event) {				 
							event.stopPropagation();
						}, true);
			}			
	   	}
    }
    else alert("no content div found");
    
    // build absolute URL for post - HTMLUnit tests do not support referencial URLs
	var host = location.host;
	var protocol = location.protocol;
	var absoluteURL = protocol + "//" + host + strURL
    xmlHttpReq.open('POST', absoluteURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  

    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
        	if(callback==undefined){
	            ajaxUpdate(xmlHttpReq.responseText, id);
	            try{
	            	window.ajaxCallback(id);
	            } catch(e){}
	     	}
	       	else callback(xmlHttpReq);
        }
    }
    xmlHttpReq.send("$ajaxInfo=" + ajaxInfo + "&$ajaxformkey=" + sessionkey);
}

