function showSearchOptions(type, id, optionContainer, fieldId)
{
	SimpleAJAXCall ( 'index.php?display_options/'+id+'/'+type+'/'+fieldId, ElementStateChanged, 'GET', optionContainer )
}
function showCriterio(number)
{
	SimpleAJAXCall ( 'index.php?user_criteria/'+number, ElementStateChanged, 'GET', 'newCriteria'+number )
}
function previewXls(url, form)
{
	form.action = url;
	form.target = '_blank';
	form.submit();
}
function getScrollPosition()
{
	var hscroll = (document.all ? document.scrollLeft : window.pageXOffset);
	var vscroll = (document.all ? document.scrollTop : window.pageYOffset);
	if(typeof hscroll == "undefined")
	{
		hscroll = document.documentElement.scrollLeft;
        vscroll = document.documentElement.scrollTop;
	}
	return [hscroll, vscroll];
}
function getPageSize()
{
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	return [pageWidth,pageHeight];
}
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
  }
	return [myWidth,myHeight];  
}
function centerWindow( object )
{
	container = document.getElementById(object);
	var size = getWindowSize();
	var spos = getScrollPosition();
	container.style.left = ((size[0] - container.style.width.replace(/px/g, ''))/2)+'px';
	container.style.top  = (((size[1] - container.style.height.replace(/px/g, ''))/2) + spos[1])+'px';
}
function setOverlayHeight() 
{
	var div = document.getElementById('TB_overlay');
	var pageSize = getPageSize();
	div.style.height = pageSize[1] + "px";
}
function showOverlay ( )
{
	document.getElementById('TB_overlay').style.display = '';
}
function showWindow(windowId)
{
	document.getElementById(windowId).style.display = '';
}
function hideOverlay()
{
	document.getElementById('TB_overlay').style.display = 'none';	
}
function hideWindow(windowId)
{
	document.getElementById(windowId).style.display = 'none';
}
function loadFormLayer ( resp, object )
{
	toggleFlashObjects('close');
	ElementStateChanged (resp, object );
	showOverlay();
	centerWindow( object )
	showWindow(object);
	setOverlayHeight();
}
function closeFormLayer ( object )
{
	toggleFlashObjects('open');
	hideOverlay();
	hideWindow(object);
	document.getElementById(object).innerHTML = '';
}
function customFormLayer( in_url, in_callback , in_method , in_container_id, in_style_array, in_center)
{
	container = document.getElementById(in_container_id);
	for(var key in in_style_array)
	{
		if(key != 'toJSONString')
		{	
			eval("container.style." + key + " = '" + in_style_array[key] + "'");
		}
	}
	SimpleAJAXCall(in_url, in_callback, in_method, in_container_id);
}
function verifyText(textareaObject, text)
{
	
	if(textareaObject.value == text)
		textareaObject.value = '';
	
	return true;
}
function cleanInnerHTML(response, elementId)
{
	document.getElementById(elementId).innerHTML = '';
}
function doNothing()
{
	void(0);
}
function displayLoading(elementId)
{
	
	if((typeof document.getElementById(elementId) != 'undefined') && (elementId != ''))
	{
		if(elementId == 'window2')
		{
			showOverlay();
			showWindow(elementId);
		}
		document.getElementById(elementId).innerHTML = '<img src="images/loading.gif" style="margin:0 auto" />';
	}
}
function toggleFlashObjects(action)
{
	var elements = document.getElementsByTagName("embed");
	var length   = elements.length;
	for(i=0;i<length;i++)
	{
		if(action == 'close')
		{
			elements[i].style.display = 'none';
		}
		else if(action == 'open')
		{
			elements[i].style.display = ''
		}
		else
		{
			if(elements[i].style.display == '')
			{
				elements[i].style.display = 'none';
			}
			else
			{
				elements[i].style.display = ''
			}			
		}
	}
}
function switchState(principalObj, secondaryObjId)
{
	principalObj.style.display = 'none';
	document.getElementById(secondaryObjId).style.display = '';
	document.getElementById(secondaryObjId).focus();
}
function saveStates(form, object, enter)
{
	var elements = form.elements;
	for(i=0; i<elements.length; i++)
	{
		if(((elements[i].type == 'text') && (elements[i] != object)) || (enter))
		{
			if(elements[i].title != '')
			{
				document.getElementById('span'+elements[i].title).innerHTML = elements[i].value;
				switchState(elements[i], 'span'+elements[i].title);
				if((enter) && (elements[i] == object))
					SimpleAJAXCall ( 'index.php?country.controller/update/'+elements[i].title+'/'+object.value, doNothing, 'GET', '' );
				
			}
		}
		else
		{
			if(elements[i].type == 'text')
			{
				SimpleAJAXCall ( 'index.php?country.controller/update/'+elements[i].title+'/'+elements[i].value, doNothing, 'GET', '' );
			}
		}
	}
}
function checkEnter(e, form, object){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		saveStates(form, object, true);
	}
}
//DHTML HISTORY
window.onload = function() {
		dhtmlHistory.initialize();
		dhtmlHistory.addListener(historyChange);
}
  /** Our callback to receive history change
	  events. */
  function historyChange(newLocation, 
						 historyData) {
	ElementStateChanged(historyData.value1[0], historyData.value1[1]);
  }
  
 
  /** Converts pointy brackets into their
		 HTML escape code equivalents so
		 we can print out XML. */
	 function prettyPrintXml(content) {
		if (content == null)
		   return null;
		content = 
			content.replace(/</g, "&lt;");
		content = 
			content.replace(/>/g, "&gt;<br>");
		return content;
	 }