	function moveToPos(objid, x, y) {
		document.getElementById(objid).style.visibility = 'visible';
		document.getElementById(objid).style.left = x+'px';	
		document.getElementById(objid).style.top = y+'px';	
	}
	
	function hideObj(obj) {
		moveToPos(obj, -1000, -1000);
		document.getElementById(obj).style.visibility = 'hidden';
		return true;
	}

	function showTemplate(subject, anch, lang) {
		var obj = getPosition(anch);
		var w = 480; 
		var h = 300;
		var x = obj.x;
		var y = obj.y;
		if ( ((screen.width/2) + w) >= screen.width ) {
			x = screen.width - (w+100);
		}
		
		var theHandle = document.getElementById("handle");
		var theRoot   = document.getElementById("root");
		y = y - h;
		y = (y <= 0 ) ? 10 : y;
		Drag.init(theHandle, theRoot);
		moveToPos("root", x, y)
		if (document.getElementById('ajaxctnt')) {
			document.getElementById('ajaxctnt').innerHTML = '<br/><blockquote><br/><img src="pics/ajaxwait.gif" border="0" hspace="0" vspace="0" /></blockquote>';
		}
		var query = 'language='+lang+'&exec=idx&subject='+subject;
		getAjax('ajaxctnt', 'agent.ajx.php', query, "processAjaxResponse");
		return true;
	}
	
	function showCalDetail(subject, anch, lang, calid) {
		var obj = window.center();//getPosition(anch);
		var w = 540; 
		var h = 300;
		var x = obj.x - (w/2);
		var y = obj.y - (h/2);
		var theHandle = document.getElementById("handle");
		var theRoot   = document.getElementById("root");
		y = (y <= 0 ) ? 10 : y;
		Drag.init(theHandle, theRoot);
		moveToPos("root", x, y)
		if (document.getElementById('ajaxctnt')) {
			document.getElementById('ajaxctnt').innerHTML = '<br/><blockquote><br/><img src="pics/ajaxwait.gif" border="0" hspace="0" vspace="0" /></blockquote>';
		}
		var query = 'language='+lang+'&exec=showdet&subject='+subject+'&cid='+calid;
		getAjax('ajaxctnt', 'agent.ajx.php', query, "processAjaxResponse");
		return true;
	}

	function showCalendar(subject, anch, lang, date) {
		/*
		var obj = getPosition(anch);
		var w = 480; 
		var h = 300;
		var x = obj.x;
		var y = obj.y;
		date = (typeof date == "undefined" || date == null ) ? '' : date;
		
		var theHandle = document.getElementById("handle");
		var theRoot   = document.getElementById("root");
		y = y - h;
		Drag.init(theHandle, theRoot);
		moveToPos("root", x, y)
		if (document.getElementById('ajaxctnt')) {
			document.getElementById('ajaxctnt').innerHTML = '<br/><blockquote><br/><img src="pics/ajaxwait.gif" border="0" hspace="0" vspace="0" /></blockquote>';
		}
		var query = 'language='+lang+'&exec=showcal&subject='+subject+'&date='+date;
		getAjax('ajaxctnt', 'agent.ajx.php', query, "processAjaxResponse");
		*/
		open('../agent.ajx.php?query', '', 'width=600,height=400');
		return true;
	}
	
	function nl2br(text){
		var re_nlchar = '';
		text = escape(text);
		if(text.indexOf('%0D%0A') > -1){
			re_nlchar = /%0D%0A/g ;
		}else if(text.indexOf('%0A') > -1){
			re_nlchar = /%0A/g ;
		}else if(text.indexOf('%0D') > -1){
			re_nlchar = /%0D/g ;
		}else if(text.indexOf('%0D') > -1){
			re_nlchar = /%0D/g ;
		}else if(text.indexOf('%\\n') > -1){
			re_nlchar = /\\n/g ;
		}
		if (re_nlchar.length>1) {
			return unescape( text.replace(re_nlchar,'<br />') );
		}
		return unescape( text );
	}
	
	function ajxSendMessage(formId, lang) {
		if (!genericValidate(formId)) {
			return false;
		}
		var obj = document.getElementById(formId);
		var subject = obj['mf_subject'].value;
		var userEmail = obj['mf_email'].value;
		var comment = obj['mf_comment'].value;
		comment = nl2br(comment);
		var query = 'language='+lang+'&exec=send';
		if (document.getElementById('mailresult')) {
			document.getElementById('mailresult').innerHTML = '<br/><blockquote><br/><img src="pics/ajaxwait.gif" border="0" hspace="0" vspace="0" /></blockquote>';
		}
		query += '&subject='+subject+'&usermail=' + userEmail + '&comment=' + comment;
		getAjax('ajaxctnt', 'agent.ajx.php', query, "processAjaxResponse");
		return false;
	}
	
	function setData(divId, msg) {
		alert(msg);
		if (document.getElementById(divId)) {
			document.getElementById(divId).value = msg;
		}
	}
	
    function processAjaxResponse() {
       if (http_request.readyState == 4) {
          if (http_request.status == 200) {
             var xmldoc = http_request.responseText;
             showResponse(xmldoc);
          } else {
             alert('There was a problem with the request.');
          }
       }
    }
   
    function showResponse(data) {
	    if (document.getElementById(targetDiv)) {
	 	    var cntDiv = document.getElementById(targetDiv);
	 	    dats = parseResponse(data);
		    cntDiv.innerHTML = urlDecode(dats[0]);
		    for (var i=1; i<dats.length; i++) {
		    	parts = dats[i].split('=');
		    	// this is a html div to fill
		    	if (parts[0].indexOf('html')>-1) {
		    		id = parts[0].substring(5);
		    		document.getElementById(id).innerHTML = urlDecode(parts[1]);
		    		continue;
		    	}
		    	// this is an input field to set the value
	    		document.getElementById(parts[0]).value = urlDecode(parts[1]);
		    }
	    }
    }
    function parseResponse(data) {
    	if (data.indexOf('||@||')>-1) {
   			return data.split('||@||');
    	}
   		return new Array(data); 
    }

window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return{x:_x,y:_y};
}

window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}


