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 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 setAttributValue(id, attribute, value)
{
	eval("document.getElementById('"+id+"')."+attribute+" = '" + value + "'");
}
function getStyle(el, style) {

   if(!document.getElementById) return;
   
     var value = el.style[toCamelCase(style)];
    if(!value)
        if((navigator.userAgent.toLowerCase().indexOf('chrome') > -1) || (navigator.userAgent.toLowerCase().indexOf('safari') > -1))
		{
            value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
		}
		else
		{
			if(style == 'height')
			{
				value = $(el).getHeight();
			}
			else
			{
            	value = $(el).getWidth();
			}
		}
     return value;
}
function setStyle(objId, style, value) {
    document.getElementById(objId).style[style] = value;
}
function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)   
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ?
       oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}

function increaseWidth(addToWidth, whichDiv){
    var theDiv = document.getElementById(whichDiv);
    var currWidth = parseInt(getStyle(theDiv, "width"));
    var newWidth = currWidth + parseInt(addToWidth);
    setStyle(whichDiv, "width", newWidth + "px");
}
function previewXls(url, form)
{
	form.action = url;
	form.target = '_blank';
	form.submit();
}

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();
	var width  = getStyle(container, 'width');
	width	   = width.toString().replace(/px/g, '');
	var height = getStyle(container, 'height');
	height	   = height.toString().replace(/px/g, '');
	container.style.left = ((size[0] - width)/2 ) + "px";
	container.style.top  = ((((size[1] - height)/2) + spos[1]) - (document.documentElement.scrollTop)) + "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();
	ElementStateChanged (resp, object );
	showOverlay();
	centerWindow( object )	
	showWindow(object);
	setOverlayHeight();
}

function closeFormLayer ( object )
{
	toggleFlashObjects();
	hideOverlay();
	hideWindow(object);
}

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="http://www.revisterovirtual.com/images/loading.gif" style="margin:0 auto" />';
	}
}
function toggleFlashObjects()
{
	var elements = document.getElementsByTagName("embed");
	var length   = elements.length;
	for(i=0;i<length;i++)
	{
		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);
	}
}
function submitForm(formId)
{
	document.getElementById(formId).submit();
}
function toggleObject(objectId)
{
	var target = document.getElementById(objectId);
	if(target.style.display == 'none')
		showWindow(objectId);
	else
		hideWindow(objectId);
}
document.observe("dom:loaded", function() { 
	$$('div.mag-div').invoke("observe", "mouseover", function() {
		$(this).setStyle({
		  backgroundColor: '#82B32A',
		  cursor: 'Pointer'
		});
		$(this).getElementsBySelector('a').each(function (item) {
			item.setStyle({
			textDecoration: 'none'	
			});						
		});
		$(this).getElementsBySelector('[class="mag-name"]').each(function (item) { 
			item.setStyle({
			color: '#FFF'
			});
		});
		$(this).getElementsBySelector('[class="mag-date"]').each(function (item) { 
			item.setStyle({
			color: '#333333'
			});
		});
		$(this).getElementsBySelector('[class="mag-suscribirse"]').each(function (item) { 
			item.setStyle({
			color: '#000'
			});
		});
	});
	$$('div.mag-div').invoke("observe", "mouseout", function() {
		$(this).setStyle({
		  backgroundColor: '#FFF',
		  cursor: 'Pointer'
		});
		$(this).getElementsBySelector('a').each(function (item) {
			item.setStyle({
			textDecoration: 'none'	
			});						
		});		
		$(this).getElementsBySelector('[class="mag-name"]').each(function (item) { 
			item.setStyle({
			color: '#9BA442'
			});
		});
		$(this).getElementsBySelector('[class="mag-date"]').each(function (item) { 
			item.setStyle({
			color: '#666666'
			});
		});
		$(this).getElementsBySelector('[class="mag-suscribirse"]').each(function (item) { 
			item.setStyle({
			color: '#376C1C'
			});
		});
	});
});
var zwindow = "";
/*function getScrollPosition()
{
	var ScrollTop = document.body.scrollTop;
 
	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return(ScrollTop);
}*/
function openwin(number, name)
{
	var size = getWindowSize();
	/*var top  = ((size[1] - (size[1]-60)) / 2);
	var left = ((size[0] - 990) / 2);
	size[0] = 900;
	*/
	scrollTo(0,0);
	var position = getScrollPosition();
	while(position[1] != 0)
	{
		position = getScrollPosition();
		doNothing();
	}
	var top = 0;
	var left = 0;
	size[1] = size[1] + 62;
	size[0] = size[0] - 19;

	zwindow=dhtmlwindow.open("id", "iframe", "http://viewer.zmags.com/publication/"+number, name, "width="+size[0]+"px,height="+(size[1]-97)+"px,resize=1,scrolling=1,center=0,top="+top+",left="+left, "");
}
function showInfo(id)
{
	document.getElementById('info'+id).style.display = '';
}
function hideInfo(id)
{
	document.getElementById('info'+id).style.display = 'none';
}
function setEmbedHeight()
{
	var size = getWindowSize();
	document.getElementById('object').style.width = size[0] + "px";
	document.getElementById('object').style.height = size[1] + "px";
	document.getElementById('embed').style.width = size[0] + "px";
	document.getElementById('embed').style.height = size[1] + "px";	
}