
// funzione che ritorna il valore testuale di un elemento HTML
function inner_html(id){
	if($(id).firstChild){
		var val = $(id).firstChild.nodeValue;
		return (val.empty()?null:val.strip());
	} else {
		return 0;
	}
}

//#################################################################################################################
// FUNZIONE usata per l'anteprima di stile in embedbe
//#################################################################################################################
function update_iframe_be_from_iframe(){
	var cont_style_rules_color = parent.document.getElementById('cont_style_rules_color');
	var childrenSpanColor = new Array();
	childrenColor = cont_style_rules_color.childNodes;
	j=0;
	for(i=0;i<childrenColor.length;i++){
		if(childrenColor[i].nodeName == 'SPAN'){
			// array con gli span relativi ai colori
			childrenSpanColor[j] = childrenColor[i];
			j++;
		}
	}
	// numero di span relativi ai colori
	var num_color = childrenSpanColor.length;
	
	var cont_style_rules_font = parent.document.getElementById('cont_style_rules_font');
	var childrenSpanFont = new Array();
	childrenFont = cont_style_rules_font.childNodes;
	j=0;
	for(i=0;i<childrenFont.length;i++){
		if(childrenFont[i].nodeName == 'SPAN'){
			// array con gli span relativi ai fonts
			childrenSpanFont[j] = childrenFont[i];
			j++;
		}
	}
	// numero di span relativi ai fonts
	var num_font = childrenSpanFont.length;
	
	// stringa per il css dinamico (1 parte)
	var url_until_ebsid = "/public/css/dynamic.css.php?";
	var c = new Array();
	var url_color = "";
	// ritrovo i colori e scrivo la parte di stringa per il css dinamico relativa ai colori
	for(i=1;i<=num_color;i++){
		c[i] = parent.document.getElementById('color_cont_' + i).firstChild.nodeValue;
		c[i] = trimAll(c[i]);
		c[i] = c[i].toString();
		url_color += "&c" + i + "=" + c[i];
	}
	url_color = url_color.substr(1)
	var f = new Array();
	var url_font = "";
	// ritrovo i fonts e scrivo la parte di stringa per il css dinamico relativa ai fonts
	for(i=1;i<=num_font;i++){
		f[i] = parent.document.getElementById('font_cont_' + i).firstChild.nodeValue;
		f[i] = trimAll(f[i]);
		url_font += "&f" + i + "=" + f[i];
	}
	// url completo
	url = url_until_ebsid + url_color + url_font;
	// settaggio del nuovo href del foglio di stile
	document.getElementById('dynamic_css').href=url;
	return false;
}
// retrieves the XMLHttpRequest object
function GetXmlHttpObject()
{
	var objXMLHttp;
	try
	{
	  // Firefox, Opera 8.0+, Safari
	  objXMLHttp=new XMLHttpRequest();
	}
	catch (e)
  	{
  		// Internet Explorer
  		try
		{
		  objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	    catch (e)
		{
			try
			  {
			  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			catch (e)
			  {
			  alert("Your browser does not support AJAX!");
			  return false;
			  }
		}
  	}
	return objXMLHttp;
}

function isInt(x) {
	var y=parseInt(x,10);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
} 

function Send_Price(pid)
{
	window.location.href = 'price_request.php?prod_id='+pid;
}

function popupWindowURL(url, winname,  w, h, menu, resize, scroll) {

    var x = (screen.width-w)/2;
    var y = (screen.height-h)/3;

	if (winname == null) winname = "newWindow";
	if (w == null) w = 800;
	if (h == null) h = 600;
	if (resize == null) resize = 1;

	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";
	//alert(url+","+x+","+winname);
    cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);

	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return true;
}

function CheckUncheck_Click(fld, status)
{
	if(fld)
	{
		if(fld.length)
			for(i=0; i < fld.length; i++)
				fld[i].checked = status;
		else
			fld.checked = status;
	}
}

function Menu_ShowHide(menu, img, imgUp, imgDown)
{
	if(menu)
	{
		if(menu.style.display == 'none')
		{
			menu.style.visibility	= 'visible';
			menu.style.display		= 'block';
			img.src = imgUp;
			SetCookie(menu.id, 'open');
		}
		else
		{
			menu.style.visibility 	= 'hidden';
			menu.style.display 		= 'none';
			img.src = imgDown;
			SetCookie(menu.id, 'close');
		}
	}
}

function UploadImage_Change(obj, imgTag, defaultVal, defaultWidth)
{
	imgTag.width=120;

	if(obj.value == '')
		imgTag.src = defaultVal;
	else
	{
		imgTag.src = obj.value;
		if(defaultWidth != '')
			imgTag.width=defaultWidth;
	}
}

function SetTime()
{
	if(!document.getElementById('timeId'))	return;

	var Hours;
	var Mins;
	var Time;

	Stamp = new Date();

	Hours = Stamp.getHours();
	
	if (Hours >= 12)
		Time = " PM";
	else
		Time = " AM";
	
	if (Hours > 12)
		Hours -= 12;
	
	if (Hours == 0)
		Hours = 12;
	
	Mins = Stamp.getMinutes();

	if (Mins < 10)
		Mins = "0" + Mins;

	Sec = Stamp.getSeconds();
	if (Sec < 10)
		Sec = "0" + Sec;

	document.getElementById('timeId').innerHTML = ("&nbsp;" + Hours + ":" + Mins + ":" + Sec + Time);
}

setInterval('SetTime()',1000);

function getDate(parmDate)
{
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December");

	var curr_date = parmDate.getDate();

	var sup = "";

	if (curr_date == 1 || curr_date == 21 || curr_date ==31)
	{
	   sup = "st";
	}
	else if (curr_date == 2 || curr_date == 22)
	{
	   sup = "nd";
	}
	else if (curr_date == 3 || curr_date == 23)
	{
	   sup = "rd";
	}
	else
	{
	   sup = "th";
	}

	var curr_month 	= parmDate.getMonth();
	var curr_year 	= parmDate.getFullYear();

	return (curr_date + "<SUP>" + sup + "</SUP> " + m_names[curr_month] + " " + curr_year);
}

function GetCookie (name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) 
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);

		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function SetCookie (name, value) 
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function Change_SiteLanguage()
{
	document.frmSiteLanguage.Current_Language.value = document.frmSiteLanguage.site_languages.value;
	var loc = new String(document.location);
	var temp = new Array();
	temp = loc.split('?');
	document.frmSiteLanguage.action = temp[0];
	document.frmSiteLanguage.submit();
}

// Resolve a string identifier to an object
// ========================================
function resolveObject(s) 
{
	if (document.getElementById && document.getElementById(s)!=null) 
	{
  		return document.getElementById(s);
	}
	else if (document.all && document.all[s]!=null) 
	{
  		return document.all[s];
	}
	else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x) 
	{
  		for (var i=0; i<document.anchors.length; i++) 
		{
			if (document.anchors[i].name==s) 
			{ 
	  			return document.anchors[i]
			}
  		}
	}
}		
  
  
function getAll(o) 
{
	var fixBrowserQuirks = true;

	// If a string is passed in instead of an object ref, resolve it
    if (typeof(o)=="string") 
	{
    	
		o = resolveObject(o);
    }
    
    if (o==null) 
	{
    	return null;
    }
    
    var left = 0;
    var top = 0;
    var width = 0;
    var height = 0;
    var parentNode = null;
    var offsetParent = null;
  
    
    offsetParent = o.offsetParent;
    
	var originalObject = o;
    
	var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references
    
	while (el.parentNode!=null) 
	{
		el = el.parentNode;
    	if (el.offsetParent==null) 
		{
			
      	}
    	else 
		{
        	var considerScroll = true;
			/*
			In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already 
			take its scroll position into account. If elements further up the chain are scrollable, their 
			scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value
			which must be ignored.
			*/
        	if (fixBrowserQuirks && window.opera) 
			{
          		if (el==originalObject.parentNode || el.nodeName=="TR") 
				{
            		considerScroll = false;
          		}
        	}
        	if (considerScroll) 
			{
          		if (el.scrollTop && el.scrollTop>0) 
				{
            		top -= el.scrollTop;
          		}
          		if (el.scrollLeft && el.scrollLeft>0) 
				{
            		left -= el.scrollLeft;
          		}
        	}
      	}
      
	  // If this node is also the offsetParent, add on the offsets and reset to the new offsetParent
		if (el == offsetParent) 
		{
      		left += o.offsetLeft;
        	if (el.clientLeft && el.nodeName!="TABLE") 
			{ 
          		left += el.clientLeft;
        	}
        	
			top += o.offsetTop;
        	
			if (el.clientTop && el.nodeName!="TABLE") 
			{
          		top += el.clientTop;
        	}
        	
			o = el;
        	
			if (o.offsetParent==null) 
			{
          		if (o.offsetLeft) 
				{
            		left += o.offsetLeft;
          		}
          		if (o.offsetTop) 
				{
            		top += o.offsetTop;
          		}
        	}
        	offsetParent = o.offsetParent;
      	}
    }
    
  
    if (originalObject.offsetWidth) 
	{
    	width = originalObject.offsetWidth;
    }
	if (originalObject.offsetHeight) 
	{
      	height = originalObject.offsetHeight;
    }
    
    return height;
}  
//=======================================================================================
// funzione per recuperare lo stile da fogli di stile esterni o 
// se lo stile non e stato proprio settato
// 'el' e l'id dell'elemento di cui si vuole conoscere lo stile
// 'propCss' e la proprieta di cui si vuole conoscere il valore
// non funziona con le scorciatoie tipo border o background
function retrieveProperty(elId, propCss){
	// definisco l'elemento il cui id vale elId
	var x = document.getElementById(elId);
    // setto la var con il valore della proprieta
    var propVal = null;
    // questo per firefox
    if(document.defaultView && document.defaultView.getComputedStyle){
        // accedo allo stile
        var stile = document.defaultView.getComputedStyle(x, null);
        // se funziona il recupero
        if(stile) {
           // recupero il valore della proprieta desiderata
           propVal = stile.getPropertyValue(propCss);
		   //alert("propVal = " + propVal);
        } else {
           propVal = null;
        }
    }
    // questo invece per IE
    else if(x.currentStyle){
			propCss = propCss.replace(/\-(\w)/g, function (strMatch, p1){return p1.toUpperCase();});
        // recupero il valore della la proprieta
        propVal = x.currentStyle[propCss];
    }
	//alert("propVal = " + propVal);
    // variabile di ritorno
    return propVal;
}

function adjust_tab_menu(num_tabs){
	var tab_cont = $('tab_cont');
	var w_tab_cont = tab_cont.getWidth();
	var w_single_tab_1 = Math.floor(w_tab_cont/num_tabs);
	var tab_1 = tab_cont.childElements()[0];
	tab_1.setStyle({ 'width': w_single_tab_1 + 'px' });
	var borderWidth = 0;
	if(tab_1.hasClassName("verticalborder")){
		borderWidth += Math.ceil(parseFloat(tab_cont.childElements()[0].getStyle("borderRightWidth")));
	}
	var tab_2 = tab_cont.childElements()[1];
	if(tab_2.hasClassName("verticalborder")){
		borderWidth += Math.ceil(parseFloat(tab_cont.childElements()[1].getStyle("borderRightWidth")));
	} else if(tab_2.hasClassName("verticalborder2")){
		borderWidth += Math.ceil(parseFloat(tab_cont.childElements()[1].getStyle("borderLeftWidth")));
	}
	if(num_tabs == 2){
		var w_single_tab_2 = w_tab_cont - w_single_tab_1 - borderWidth;
		tab_2.setStyle({ 'width': w_single_tab_2 + 'px' });
	} else if(num_tabs==3){
		var tab_3 = tab_cont.childElements()[2];
		if(tab_3.hasClassName("verticalborder2")){
			borderWidth += Math.ceil(parseFloat(tab_cont.childElements()[2].getStyle("borderLeftWidth")));
		}
		var w_single_tab_2 = w_single_tab_1;
		tab_2.setStyle({ 'width': w_single_tab_2 + 'px' });
		var w_single_tab_3 = w_tab_cont - w_single_tab_1 - w_single_tab_2 - borderWidth;
		tab_3.setStyle({ 'width': w_single_tab_3 + 'px' });
	}
}
 
function adequate_width(){
	var gen_cont = $('contentScheduleGeneral');
	
	if(gen_cont){
		var tab_cont = $('tab_cont');
		var w_gen_cont = gen_cont.getWidth();
		var left_cont = $('leftColumnSchedule');
		if(left_cont){
			var w_left_cont = left_cont.getWidth();
			$('rightColumnSchedule').setStyle({ 'width': (w_gen_cont - w_left_cont-6) + 'px' });
			if(tab_cont){
				tab_cont.setStyle({ 'width': (w_gen_cont - w_left_cont-6) + 'px' });
				var num_tabs = tab_cont.childElements().length;
				adjust_tab_menu(num_tabs);
			}
		}
	}
}

// Funzione per il ridimensionamento dinamico dell'iframe secondo le esigenze
function Resize_iFrame(){
	var donotresizeme = document.getElementById("donotresizeme");
	if (donotresizeme){	
		return; 
	}
	
	if(Resize_Check() == true){
		try
		{
			var internal_div;
			var using;
/*			internal_div = document.getElementById("contentScheduleGeneral");
			var x = internal_div.style.width;
			var y = internal_div.style.height;
*/
			var y = $("contentScheduleGeneral").getHeight();
			var checkispreview = document.getElementById("checkispreview");
			var checkismcl = document.getElementById("maincontainer_login");
			var checkisreg = document.getElementById("maincontainer_reg");
/*
			if (checkispreview == null)
			{
				y = getAll("contentScheduleGeneral");
			}
*/
			// apply it to iframe document	
			myif = parent.document.getElementsByName("frameinner")[0];
			if(y){
				if (location.href.indexOf('login')==-1){
					myif.height = y;
				} else {
					if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
						myif.height = y;
					} else {
						myif.height = y-12;
					}
				}
				if(location.href.indexOf('channeltype')!=-1) {
					if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
						myif.height = y;
					} else {
						myif.height = y-20;
					}
				}else if (checkispreview){
					myif.height = y+42;
				}
			}
		}
		catch (err) {
			//alert("Error");/* so ie sucks */ 
		}
	}
}
//=============================================================================================
function Resize_Check(){
	try{
		if(parent.location.href.indexOf(CHECK_URL) == 0 && parent.location.href.indexOf(EMBED_BE_URL) == -1 ){
			// in glomera
			return true;	
		} else {
			// fuori glomera
			return false;
		}
	} catch(e) {
		// fuori glomera
		return false;
	}
}

function isEmbedBe(){
//Questa funzione serve per capire se il back-end è visualizzato nel popup di personalizzazione o no
//Se sono nel popup disabilito alcuni tasti funzionali 
	try
	{
		if(parent.location.href.indexOf(EMBED_BE_URL) == -1 )
		{
			// fuori da popup embed be
			return false;	
		}
		else
		{
			// in popup embed be
			return true;
		}
	}
	catch(e)
	{
		return false;
	}
}


function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function addGlobalStyle() {
    var head, style;
    if(Resize_Check() == false)
	{
		head = document.getElementsByTagName('head')[0];
		if (!head) { return; }
		style = document.createElement('style');
		style.type = 'text/css';
		if (style.styleSheet)
		{
			style.styleSheet.cssText = NEW_CSS;
		}
		else
		{
			style.innerHTML = NEW_CSS;
		}
		head.appendChild(style);
	}
}

function AddEventListener(element, eventType, handler, capture)
{
	if (element.addEventListener)
		element.addEventListener(eventType, handler, capture);
	else if (element.attachEvent)
		element.attachEvent("on" + eventType, handler);
}

function info_no_more_show(user_id,info_type){
	url = "/Srv/infonomoreshow/user_id/" + user_id + "/info_type/" + info_type;
	new Ajax.Request	
	(
		url,
		{
			method: 'GET',
			crossSite: false,
			onLoading: function(transport)
			{
				
			},
			onSuccess: function(transport)
			{
				//alert(transport.responseText);
			},
			onFailure: function(transport)
			{
				alert ("Net error. Please, retry.");
			}
		}
	);
}

function showTip(id,lPos){
	hiddenSpan=document.getElementById(id); 
	hiddenSpan.style.position='absolute'; 
	hiddenSpan.style.top='100%'; 
	hiddenSpan.style.left=lPos+'px'; 
	hiddenSpan.style.zIndex='100'; 
	hiddenSpan.style.visibility='visible'; 
	hiddenSpan.style.display='block';
	hiddenSpan.style.fontWeight='normal'; 
	hiddenSpan.style.width='180px'; 
}

function hideTip(id){
	hiddenSpan=document.getElementById(id); 
	hiddenSpan.style.visibility='hidden'; 
	hiddenSpan.style.display='none';
}

function login_link(lan)
{
	if(Resize_Check() == true)
	{
		parent.location = "/login/?ln="+lan;
	}
	else
	{
		location.href = "/login/indexinner?ln="+lan;
	}
}


function logout_link(lan){
	if(!isEmbedBe()){ //sono nel popup di embedbe?
		if(Resize_Check() == true){
			parent.location = "/logout/?ln="+lan;
		}
		else{
			location.href = "/logout/?ln="+lan;
		}
	}
	
}


function hide_show_elem_new(elem,arrow1,arrow2){
	//elem: 	elemento da visualizzare/nasdcondere a seconda del suo stato
	//arrow1: 	elemento che contiene l'immagine della freccia elemento aperto (opzionale)
	//arrow2: 	elemento che contiene l'immagine della freccia elemento chiuso (opzionale)
	
	var txt = document.getElementById(elem);
	var txt_arrow1 = document.getElementById(arrow1);
	var txt_arrow2 = document.getElementById(arrow2);
	
	if(txt.style.display=="none"){
		txt.style.display="";
		if(txt_arrow1)
		txt_arrow1.style.display="";
		if(txt_arrow2)
		txt_arrow2.style.display="none";
	}
	else{
		txt.style.display="none";
		if(txt_arrow1)
		txt_arrow1.style.display="none";
		if(txt_arrow2)
		txt_arrow2.style.display="";
	}
	Resize_iFrame();	
}

// per fede in multiupload
function hide_show_elem(elem){
	//elem: 	elemento da visualizzare/nasdcondere a seconda del suo stato
	//arrow1: 	elemento che contiene l'immagine della freccia elemento aperto (opzionale)
	//arrow2: 	elemento che contiene l'immagine della freccia elemento chiuso (opzionale)
	
	var txt = document.getElementById(elem);
	
	if(txt.style.display=="none"){
		txt.style.display="";
	}
	else{
		txt.style.display="none";
	}
	Resize_iFrame();	
}

function popoup_embedbe(){
	if(!isEmbedBe()){ //sono nel popup di embedbe?
		var options='resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,directories=no,width=' + 1280/*Math.min(screen.width-30,1280) */+ ',height=' + Math.floor(screen.height) + ',top=20,left=20';
		window.open('/embedbe','embedbe',options);
	}
}

function renewTrial(ch_id){	
	if(!isEmbedBe()){ //sono nel popup di embedbe?
		document.frmTrial.action="/channel/typeinnerpay/id/"+ch_id+"/renewal/1/";
		document.frmTrial.submit();
	}
}

function addslashes( str ) {
	return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}