var URL;
var IMG;
var URL_CAT;
var URL_CAST;

function trim(str) 
{
  return str.replace(/^\s+|\s+$/g,"");
}
function getWindowHeight() 
{
    if (document.all)
    {
        return top.document.body.offsetHeight; 
    } 
    else 
    { 
        return top.window.innerHeight; 
    } 
} 

function getWindowWidth() 
{ 
    if (document.all) 
    { 
        return top.document.body.offsetWidth; 
    } 
    else 
    { 
        return top.window.innerWidth; 
    } 
}

var resizeBg = function() 
{
	var bgimg = document.getElementById('bgimg');
  var w = getWindowWidth();
  var h = getWindowHeight();
  if (bgimg.width !== w) {
  	bgimg.parentNode.style.left = '0';
    bgimg.parentNode.style.top = '0';
    bgimg.height = (w / bgimg.width) * bgimg.height;
    bgimg.width = w;
  }
  if (bgimg.height < h) {
    bgimg.width = (h / bgimg.height) * bgimg.width;
    bgimg.height = h;
    bgimg.parentNode.style.left = '-' + ((bgimg.width - w) / 2) + 'px';		
  }
  else if (bgimg.height > h) 
  {
		bgimg.parentNode.style.top = '-' + ((bgimg.height - h) / 2) + 'px';
  } 
}       
function detectURL_QUERY()
{
	URL_QUERY=document.location.href;
	if (URL_QUERY.indexOf('cast') > 0)
	{
		re=/cast/g;
		URL_CAST=URL_QUERY;
		URL_CAT=URL_QUERY.replace(re,'cat');		
	}
	else
	{
		re=/cat/g;
		URL_CAT=URL_QUERY;
		URL_CAST=URL_QUERY.replace(re,'cast');				
	}
}
function detectURL()
{
	query=document.location.href.substring(document.location.href.indexOf("?")+1);
	URL =query.substring(query.indexOf("URL=")+4);
} 
function swapIdioma()
{
	if (document.idioma.lang.value=="CAT")
	{
		document.location=URL_CAT;
	}
	else
	{
		document.location=URL_CAST;
	}
}

function sumari(contenidor,mostrador,marcador,texte_expandir,texte_contrareu,num_caracs)
{
	sumari_length=num_caracs;
	sumari_index=0;
	var texte_sumari="";
	if (document.getElementById(contenidor).getAttribute("type")=="html")
	{
		texte_sumari=sumarize(document.getElementById(contenidor));
	}
	else if (document.getElementById(contenidor).getAttribute("type")=="gallery")
	{
		texte_sumari=sumarize(document.getElementById(contenidor).getElementsByTagName("description")[0]);
	}
	if(texte_sumari.length>sumari_length)
	{
		texte_sumari=texte_sumari.substring(0,sumari_length);
	}
	sumari_index=0;	
	texte_expandir=escape(texte_expandir);
	texte_contrareu=escape(texte_contrareu);
	document.getElementById(mostrador).innerHTML='';
	document.write(texte_sumari+" <a class='llegir' href='#"+marcador+"' onClick=\"unsumarize('"+contenidor+"','"+mostrador+"','"+marcador+"','"+texte_expandir+"','"+texte_contrareu+"','"+num_caracs+"')\">"+unescape(texte_expandir)+"</a>");
	
}
function sumariNoWrite(contenidor,mostrador,marcador,texte_expandir,texte_contrareu,num_caracs)
{
	sumari_length=num_caracs;
	sumari_index=0;
	var texte_sumari="";
	if (document.getElementById(contenidor).getAttribute("type")=="html")
	{
		texte_sumari=sumarize(document.getElementById(contenidor));
	}
	else if (document.getElementById(contenidor).getAttribute("type")=="gallery")
	{		
		texte_sumari=sumarize(document.getElementById(contenidor).getElementsByTagName("description")[0]);
	}	
	if(texte_sumari.length>sumari_length)
	{
		texte_sumari=texte_sumari.substring(0,sumari_length);
	}
	sumari_index=0;	
	return texte_sumari+" <a class='llegir' href='#"+marcador+"' onClick=\"unsumarize('"+contenidor+"','"+mostrador+"','"+marcador+"','"+texte_expandir+"','"+texte_contrareu+"','"+num_caracs+"')\">"+unescape(texte_expandir)+"</a>";	
}
function unsumarize(contenidor,mostrador,marcador,texte_expandir,texte_contrareu,num_caracs)
{	
	document.getElementById(mostrador).innerHTML=document.getElementById(contenidor).innerHTML.replace(/LYTEBAG/g,"lytebox["+contenidor+"]").replace(/LYTEGRA/g,"lyteframe["+contenidor+"]")+" <a class='llegir' href='#"+marcador+"' onClick=\"resumarize('"+contenidor+"','"+mostrador+"','"+marcador+"','"+texte_expandir+"','"+texte_contrareu+"','"+num_caracs+"')\">"+unescape(texte_contrareu)+"</a>";		
	myLytebox = new LyteBox();
}
function resumarize(contenidor,mostrador,marcador,texte_expandir,texte_contrareu,num_caracs)
{	
	document.getElementById(mostrador).innerHTML=sumariNoWrite(contenidor,mostrador,marcador,texte_expandir,texte_contrareu,num_caracs);		
}
function sumarize(obj) 
{	
	// return the data of obj if its a text node	
	if (obj.nodeType == 3)
	{
		return obj.nodeValue;
	}	
	if (obj.nodeType == 1)
	{
		if(obj.childNodes.length==0)
		{
			//return obj.nextSibling.nodeValue;
			return obj.nodeValue;
		}		
	}
	var txt = new Array(),i=0;
	// loop over the children of obj and recursively pass them back to this function
	while(obj.childNodes[i]) 
	{
		if (sumari_index<sumari_length)
		{
			txt[txt.length] = sumarize(obj.childNodes[i]);
			i++;
			sumari_index=sumari_index+txt.join("").length;
		}		
		else
		{
			break;
		}
	}
  // return the array as a string
  return txt.join("");
}
function distributeLineImagesH(node)
{
	alert(document.getElementById(document.getElementById(node).parentNode.id).style.width);
}
function go(numberPage)
{
	document.getElementById("enma_feedrender_formPost").action=document.location;
	document.getElementById("enma_feedrender_formPost").actualPage.value=numberPage;
	document.getElementById("enma_feedrender_formPost").submit();
}
function showPageSubDomain(subdomain,urlita)
{
	  urlcrida=subdomain+'/recursos/jsp/seturl.jsp?domain='+subdomain+'&URL='+urlita;
		window.frames['wrkarea'].document.location=urlcrida;
}
function showMapSubDomain(subdomain,map,lang,kind)
{
	  urlcrida=subdomain+'/recursos/jsp/setmap.jsp?domain='+subdomain+'&map='+map+'&lang='+lang+'&kind='+kind;
		window.frames['wrkarea'].document.location=urlcrida;
}
function renderGallery(formName,contenidorName,titolName,target)
{	
	urlita=document.getElementById(formName).action;
	var idContenidor;
	var idTitol;
	var correctionPixels=0;
	if (navigator.userAgent.indexOf("MSIE")!=-1)
	{
		idContenidor = document.getElementById(target).parentNode.id;
		correctionPixels=16;
  }
	else
	{
		idContenidor = document.getElementById(target).parentNode.id;
	}	
	idTitol = idContenidor.replace(contenidorName,titolName);	
	document.getElementById(formName).gallery_containerSize.value=document.getElementById(idTitol).offsetWidth+correctionPixels;
	//processAjaxPost(formName,target);		
	var myHTMLRequest = new Request.HTML(
	  {
	  	url:urlita,	  
	  	headers:{'accept-charset':'ISO-8859-1'},
	  	update:$(target)	  	
		}).post($(formName));
}
function renderGalleryId(formName,contenidorName,titolName,target)
{	
	urlita=document.getElementById(formName).action;
	var idContenidor;
	var idTitol;
	var correctionPixels=0;
	if (navigator.userAgent.indexOf("MSIE")!=-1)
	{
		idContenidor = document.getElementById(target).parentNode.id;
		correctionPixels=16;
  }
	else
	{
		idContenidor = document.getElementById(target).parentNode.id;
	}	
	idTitol = idContenidor.replace(contenidorName,titolName);	
	document.getElementById(formName).gallery_containerSize.value=document.getElementById(idTitol).offsetWidth+correctionPixels;
	//processAjaxPost(formName,target);		
	var myHTMLRequest = new Request.HTML(
	  {
	  	url:urlita,	  
	  	async:false,
	  	update:$(target),
	  	headers:{'accept-charset':'ISO-8859-1'}
		}).post($(formName));
	document.getElementById(target).innerHTML=document.getElementById(target).innerHTML.replace(/LYTEBAG/g,"lytebox[new]").replace(/LYTEGRA/g,"lyteframe[new]");
	myLytebox = new LyteBox();
}
// VARS TIME

var Div		= "";
var TargetDate	=	0;
var DisplayFormat	= "";
var EndTimeStr="";
var CountActive	= true;
var DisplayStr="";
var idTimeOut;
function Calcage(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}
function resetWindow()
{
	window.top.location="actions/admin/logout_timeoutsession.jsp";
}
function CountBack(secs)
{
  //this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  //this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  DisplayStr = DisplayFormat.replace(/%%M%%/g,		Calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g,		Calcage(secs,1,60));
	if(secs>0 && CountActive)
	{
  	document.getElementById(Div).innerHTML = DisplayStr;
  	idTimeOut=setTimeout("CountBack(" + (secs-1) + ")", 990);
  }
  else
	{
		document.getElementById(Div).innerHTML = EndTimeStr;
		CountActive=false;
		resetWindow();
	}
}
function start()
{	
	CountActive=false;
	clearTimeout(idTimeOut);
	var dthen	= new Date((new Date()).getTime()+TargetDate*1000);
  var dnow	= new Date();
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	CountActive=true;
	CountBack(gsecs);
}
function processURL(target,urlita)
{	
	window.target.document.location=urlita;
	start();
}
// AJAX FUNCTIONS

function processAjaxNoReturn(URL) 
{			
	if (window.XMLHttpRequest) 
	{ // Non-IE browsers
		req_processAjaxNoReturn = new XMLHttpRequest();
		req_processAjaxNoReturn.onreadystatechange = targetDivNoReturn;
		try 
		{
			req_processAjaxNoReturn.open("GET", URL, true);
		} 
		catch (e) 
		{
			alert(e);
		}
		req_processAjaxNoReturn.send(null);
	}
	else if (window.ActiveXObject) 
	{ // IE
		req_processAjaxNoReturn = new ActiveXObject("Microsoft.XMLHTTP");
		if (req_processAjaxNoReturn)
		{
			req_processAjaxNoReturn.onreadystatechange = targetDivNoReturn;
			req_processAjaxNoReturn.open("GET", URL, true);
			req_processAjaxNoReturn.send();
		}
	}
	return false;
}
function processAjaxNoReturn(URL) 
{			
	if (window.XMLHttpRequest) 
	{ // Non-IE browsers
		req_processAjaxNoReturn = new XMLHttpRequest();
		req_processAjaxNoReturn.onreadystatechange = targetDivNoReturn;
		try 
		{
			req_processAjaxNoReturn.open("GET", URL, true);
		} 
		catch (e) 
		{
			alert(e);
		}
		req_processAjaxNoReturn.send(null);
	}
	else if (window.ActiveXObject) 
	{ // IE
		req_processAjaxNoReturn = new ActiveXObject("Microsoft.XMLHTTP");
		if (req_processAjaxNoReturn)
		{
			req_processAjaxNoReturn.onreadystatechange = targetDivNoReturn;
			req_processAjaxNoReturn.open("GET", URL, true);
			req_processAjaxNoReturn.send();
		}
	}
	return false;
}
function targetDivNoReturn() 
{
	if (req_processAjaxNoReturn.readyState == 4) 
	{ // Complete
		if (req_processAjaxNoReturn.status == 200) 
		{ // OK response
		  // That's ok.
		  start();
		}
		else 
		{
			alert("Problem: " + req_processAjaxNoReturn.statusText);
		}
	}
}
function processAjaxPost(formName,target)
{
	try 
	{
		urlAction = document.getElementById(formName).action;
		if (window.XMLHttpRequest) 
		{ // Non-IE browsers
			req_processAjaxTextResult = new XMLHttpRequest();
			req_processAjaxTextResult.onreadystatechange = function() { targetTextResult(target) };
			try 
			{
				req_processAjaxTextResult.open("POST", urlAction, true);
				req_processAjaxTextResult.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				req_processAjaxTextResult.send(getParams(formName));	
			} 
			catch (e) 
			{
				alert(e);
			}
		}
		else if (window.ActiveXObject) 
		{ // IE
			req_processAjaxTextResult = new ActiveXObject("Microsoft.XMLHTTP");
			if (req_processAjaxTextResult)
			{
				req_processAjaxTextResult.onreadystatechange = function() { targetTextResult(target) };
				req_processAjaxTextResult.open("POST", urlAction, true);
				req_processAjaxTextResult.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				req_processAjaxTextResult.send(getParams(formName));						
			}
		}
		return;	
	}
	catch(excepcio)
	{
		alert(excepcio);
	}
}
function targetTextResult(node)
{
	if (req_processAjaxTextResult.readyState == 4) 
	{ // Complete
		if (req_processAjaxTextResult.status == 200) 
		{ 
			var div = document.createElement('div');
		  var text = document.createTextNode(req_processAjaxTextResult.responseText);
		  div.appendChild(text);			
			document.getElementById(node).innerHTML=div.innerHTML;
			//alert(document.getElementById(node).innerHTML);
		}
		else 
		{
			alert("Problem: " + req_processAjaxTextResult.statusText);
		}
	}	
}
function getParams(form)
{
	var query="";
	for(i=0; i<document.forms[form].elements.length; i++)
	{
		if (document.forms[form].elements[i].type != "button" && document.forms[form].elements[i].type != "submit" && document.forms[form].elements[i].type != "reset")
		{
			if(query!="")
			{
				if (document.forms[form].elements[i].type != "checkbox")
				{				
					query+="&"+document.forms[form].elements[i].name +"="+ escape(document.forms[form].elements[i].value);
				}
				else if (document.forms[form].elements[i].type == "checkbox" && document.forms[form].elements[i].checked)
				{
					query+="&"+document.forms[form].elements[i].name +"="+ escape(document.forms[form].elements[i].value);
				}
			}
			else
			{
				
				if (document.forms[form].elements[i].type != "checkbox")
				{				
					query+=document.forms[form].elements[i].name +"="+ escape(document.forms[form].elements[i].value);
				}
				else if (document.forms[form].elements[i].type == "checkbox" && document.forms[form].elements[i].checked)
				{
					query+=document.forms[form].elements[i].name +"="+ escape(document.forms[form].elements[i].value);
				}				
			}
		}
	}
	return query;
}
function f_open_window_max( aURL, aWinName )
{
   var wOpen;
   var sOptions;

   sOptions = 'status=yes,menubar=yes,scrollbars=yes,resizable=yes,toolbar=yes,location=yes';
   sOptions = sOptions + ',width=' + (screen.availWidth - 10).toString();
   sOptions = sOptions + ',height=' + (screen.availHeight - 122).toString();
   sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';

   wOpen = window.open( '', aWinName, sOptions );
   wOpen.location = aURL;
   wOpen.focus();
   wOpen.moveTo( 0, 0 );
   wOpen.resizeTo( screen.availWidth, screen.availHeight );
   return wOpen;
}
function showLink(linkDiv,nom,messageText1,messageText2,textLink1)
{
	linkDiv.innerHTML='<img onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'normal\'" onclick="closeLink(linkDiv)" src="/recursos/imatges/close.gif" style="float:right">'+messageText1+':<br/><input onclick="this.focus();this.select();" id="linkText1" name="linkText1" type="text" size="50" value="'+textLink1+'"/><br/>'+messageText2+':<br/><input id="linkText2" name="linkText2" onclick="this.focus();this.select()" size="50" type="text" value="\<a href=\''+textLink1+'\' target=\'_new\'\>'+nom+'\</a\>"/>';	
	linkDiv.style.visibility='visible';
	document.getElementById('linkText1').focus();
	document.getElementById('linkText1').select();
}
function closeLink(linkDiv)
{	
	linkDiv.style.visibility='hidden';	
	linkDiv.innerHTML='';
}
function checkCondition(condicio,buto)
{
	if (document.getElementById(condicio).checked)
	{
		document.getElementById(buto).disabled=false;		
	}
	else
	{
		document.getElementById(buto).disabled=true;
	}
}
