
// THESE FUNCTIONS ARE NEEDED TO DETERMINE THE PROPERTIES OF THE WINDOW, TO CREATE A 100% div that would cover all the page.
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function $(id){ 
	return document.getElementById(id); 
}

//ajax functions

function show_loading_div(text){
  	
	var client_width = f_clientWidth();
	var client_height= f_clientHeight();
	var scroll_top   = f_scrollTop();
	var scroll_left  = f_scrollLeft();
	
	// If the text is set a TEXT saying Savimg/Deleting appears, otherwise the text won't appear (in the case of seleting another image in the photo gallery
																								  
	if(text != ""){
		$('ajax-messages').src = "js/"+text+".gif";
		$('ajax-messages').style.visibility = "visible";
	}else{
		$('ajax-messages').style.visibility = "hidden";
	}
	
	$('loading_div').style.width   	= client_width;
	$('loading_div').style.height   = client_height;
	$('loading_div').style.top		= scroll_top;
	$('loading_div').style.left   	= scroll_left;
	$('loading_div').style.paddingTop= (((client_height*2)/5)+(scroll_top))+"px";
	$('loading_div').style.display = 'block';
}


function createXMLHttpRequest( ){
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest( ); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
}

function createXMLObject(text){
	if (window.ActiveXObject)
	{
		var doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(text);
	}
	else
	{
		var parser=new DOMParser();
		var doc=parser.parseFromString(text,"text/xml");
	}		
	
	return doc.documentElement;
}

function hide_loading_div(){
	$('loading_div').style.display = 'none';
}

function ajax_doRequest(params, div_id){
	
	//AJAX ROUTINE
	var xhr = createXMLHttpRequest( );
	xhr.open("POST", "ajax_do_request.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close");

	xhr.onreadystatechange = function( ){
		//show_loading_div("loading");
		
		if (xhr.readyState==4) {
			if (xhr.status==200){
				//setTimeout('hide_loading_div()', 2000);
				if (document.getElementById){
					div_obj = document.getElementById(div_id);
					div_obj.innerHTML = xhr.responseText;
				}
			}
		}
		
	}	
	xhr.send(params);	
}
