//
// Creates new window from link
// Parameters:
// Link - link to show in window
// Effect:
// Opens a window with name "Additional" and shows needed link in it.
//
function LinkWindow(linkURL)
{
	if (window.event)
	{
		event.cancelBubble = true;
		event.returnValue = false;
	}
	var width = Math.ceil(screen.width/2);
	var height = Math.ceil(screen.height*0.6);
	if (width < 600) width = 600;
	if (height < 400) height = 400;
	var left = Math.ceil(screen.width/4);
	var top = Math.ceil(screen.height*0.1);
	var oTarget = window.open(linkURL, "Additional", "height="+height+",width="+width+",scrollbars=1,left="+left+",top="+top);
	oTarget.focus();
	return false;
}

function CheckAll(elm, name)
{
	for (i = 0; i < elm.form.elements.length; i++)
	{
		if (elm.form.elements[i].type == "checkbox" && elm.form.elements[i].name == name)
		{
			elm.form.elements[i].checked = elm.checked;
		}
	}
}

var savedClass = null;

function HighlightTR(elm)
{
	savedClass = elm.className;
	elm.className = "selected";
}

function UnhighlightTR(elm)
{
	if (savedClass != null)
		elm.className = savedClass;
}

function ChangeLanguage(languageCode)
{
	document.cookie =  "LanguageCode = " + escape(languageCode) + "; expires=Mon, 01-Jan-2010 00:00:00 GMT; path=/";
	document.location.reload();
}

function CreateGet(elem, url)
{
	document.location.href=url+'?'+elem.name+'='+elem.value;
}

function SendEmail(elem)
{
	if ( confirm( 'Are you sure send email?' ) )
	{
		elem.Send.value = "Yes";
	}
}
function SendEmailDel(elem)
{
	if ( confirm( 'Are you sure send email?' ) )
	{
		elem.href += "&Send=Yes";
	}
}

function ChangeLanguage(languageCode)
{
    document.cookie =  "LanguageCode = " + escape(languageCode) + "; expires=Mon, 01-Jan-2020 00:00:00 GMT; path=/";
    document.location.reload();
}

/**
 * ajax.js
 *
 * This file contains a number of functions to work with AJAX requests
 *
 * <b>Variables</b>
 *
 * <ul>
 *   <li>http (object) = null<br><br>AJAX handler through getHttp()<br> </li>
 * </ul>
 *
 * @package Jscripts
 * @author Webline BVBA <info@webline.be>
 * @copyright Webline BVBA
 * @link http://www.webline.be/  www.webline.be
 * @todo further document the functions
 * @uses getHttp()
 */

var http = null;

/**
 * Add slashes and conversions needed
 *
 * @param string The string you want to alter
 * @access private
 * @return string
 * @todo possibly use encode/decodeURI as alternative
 */
function addslashes(str) {

    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0").replace(/\n/g, '<br>');
    
} // end function

/**
 * Get AJAX request object
 *
 * @access private
 * @return object
 */
function getHttp() {

	var req; 

	if(window.XMLHttpRequest) {
	
		req = new XMLHttpRequest();
		
	}else{
	
		if(window.ActiveXObject) {

			req = new ActiveXObject("Microsoft.XMLHTTP");

		}else{

			alert("Problem creating the XMLHttpRequest object");

		} // end if
		
	} // end if

	return req;

} // end function

/**
 * Execute an AJAX request and send the response to the given function
 *
 * @param string Name of the function that needs to be called when the AJAX handler receives a response
 * @param string URL you want to call for this AJAX request
 * @param string If async is true, a new thread will be started, otherwise the script will wait until the request is done and returns a value if needed.
 * @access public
 * @return void
 */
function ajaxRequest(myFunction,url,async) {
	
	var response;
		
	if(async != false) {

		async = true;

	}else{

		async = false;

	} // end if
	
	var http;
	http = getHttp();
	http.abort();
	
	http.open("GET",url + "?ajax=" + new Date().getTime(), async);
	
	if(async != false) {
		
		http.onreadystatechange=function() {
	
			if(http.readyState == 4  && http.status == 200) {
				
				response = addslashes(http.responseText);
				eval(myFunction+"('" + response  + "')");
				
			} // end if
			
		} // end function
		
		http.send(null);
		
	}else{
	
		http.send(null);
		response = addslashes(http.responseText);	
		eval(myFunction+"('" + response  + "')");
	
	} // end if

} // end function

/*
If async is true, a new thread will be started.
If async is false, the script will wait until the request is done and returns a value if needed.
*/
function ajaxGetRequest(myFunction,url,parameters,async) {
	
	var response;
		
	if(async != false) {

		async = true;

	}else{

		async = false;

	} // end if
	
	var http;
	http = getHttp();
	http.abort();
	
	if(parameters != "") {

		parameters += "&" + "dummy=" + new Date().getTime();

	}else{

		parameters += "dummy=" + new Date().getTime();

	} // end if
	
	http.open("GET",url + "?" + parameters, async);
	
	if(async != false) {
		
		http.onreadystatechange=function() {
	
			if(http.readyState == 4  && http.status == 200) {
				
				response = addslashes(encodeURI(http.responseText));
				eval(myFunction+"('" + response  + "')");
				
			} // end if
			
		} // end function
		
		http.send(null);
		
	}else{
	
		http.send(null);
		response = addslashes(http.responseText);	
		eval(myFunction+"('" + response  + "')");
	
	} // end if

} // end function

function cancelPostRequest() {

	http.abort();

} // end function

/*
If async is true, a new thread will be started.
If async is false, the script will wait until the request is done and returns a value if needed.
*/
function ajaxPostRequest(myFunction,url,parameters,async,cancelLast) {
	
	if(cancelLast!=null && cancelLast!=false) {
		
		var cancelLast=true;
		
	}else{
	
		var cancelLast=false;
	
	} // end if
	
	var response;
		
	if(async != false) {

		async = true;

	}else{

		async = false;

	} // end if
	
	if(cancelLast) {
	
		cancelPostRequest();
	
	}else{
	
		eval('var http = getHttp();');
	
	} // end if
	
	http.open('POST', url,async);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.setRequestHeader("Connection", "close");
	
	if(async != false) {
		
		http.onreadystatechange=function() {
	
			if(http.readyState == 4 && http.status == 200) {

				response = /*addslashes(*/http.responseText/*)*/;
				eval(myFunction+'("' + encodeURI(response)  + '")');
				
			} // end if
			
		} // end function
		
		http.send(parameters);
		
	}else{

		http.send(parameters);
		response = addslashes(http.responseText);	

		if(myFunction) return eval(myFunction+'("' + response  + '")');
		
		return http.responseText;
		
	} // end if

} // end function

// public function
function setEventHandler(obj,myEvent,myFunction) {

	if(BrowserDetect.browser == "Explorer"){

		obj.onpropertychange = function(){
			
			var prop = event.propertyName;
			
			if(prop=="value"||prop=="checked"){
			
				eval(myFunction + "()");
			
			}// end if
				
		}// end function
		
	}else{
	
		obj.addEventListener(myEvent,eval(myFunction),false);
	
	}// end if

} // end function

http = getHttp();
