// GLOBAL FUNCTION

//varibili per il rating
var timerID;
var myWidthOLD;
var myHeightOLD;
var mylightBoxObj;
var session_up=false;

var chat_users_request_obj=false;
if (window.XMLHttpRequest) // try to create XMLHttpRequest
	chat_users_request_obj = new XMLHttpRequest();
if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
	chat_users_request_obj = new ActiveXObject("Microsoft.XMLHTTP");



window.onload = function(){
	MM_preloadImages('/_images/headerMenu1_f2.jpg','/_images/headerMenu2_f2.jpg','/_images/headerMenu3_f2.jpg','/_images/headerMenu3_f2.jpg','/_images/headerMenu4_f2.jpg','/_images/headerMenu5_f2.jpg');
	caricaFlashAvvio();
	initRateAct();
//	settaAvvio();
/*	initVideoMenu();
	initUserMenu();	
	initContestVideoMenu();
*/	
}

function startloadChatUserList(){
	setInterval("loadChatUserList();",10000);
}

function loadChatUserList(){
	chat_users_request_obj.open("GET", "/_php/chatUsers.php" , true);
	chat_users_request_obj.onreadystatechange = loadChatUserListRes;
	chat_users_request_obj.send(null); 
}
function loadChatUserListRes(){
	if (chat_users_request_obj.readyState==4) {
		if (chat_users_request_obj.responseText.indexOf('invalid') == -1) { 
			document.getElementById("lista_utenti_chat").innerHTML=chat_users_request_obj.responseText;
		}
	}
}

function apriLightBox(href,tit){	
	showLightbox(href,tit);
}
function setIframeSrc(src){
	myFrame=document.getElementById("flashback");
	if(myFrame){
		myFrame.src=src;
	}
}
function showMap(lng){
	document.getElementById("homemap").style.display = "block";
	flashWriter("homemap",400,400,"/_fp/matera_sensibile.swf?lng="+lng,"transparent")
}
function hideMap(){
	document.getElementById("homemap").style.display = "none";
	document.getElementById("homemap").innerHTML="MAPPA SENSIBILE";
}


function setGeo(cnt){
	if(document.getElementById("geo")) {
		document.getElementById("geo").value = cnt;
	}
}

function popupwindow(LarghezzaCont,AltezzaCont,Pagina,nome,scrol,stat) {
	var lsBrowser = navigator.appName;
	var navInfo = navigator.userAgent;
	if (scrol == "yes") {
		if (navInfo == "Mac" && lsBrowser.indexOf("Microsoft") >= 0) {
				aw = 0;
		} else {
		aw = 17;
		}		
	} else {
	aw = 0;
	}		
    var iMyWidth;
    var iMyHeight;
    var LarghezzaPagina;
    var AltezzaPagina;
    LarghezzaPagina = screen.availWidth;
    AltezzaPagina = screen.availHeight;
    LarghezzaCont=LarghezzaCont.toUpperCase()
    AltezzaCont=AltezzaCont.toUpperCase()
				
    if (LarghezzaCont == "FULL") {
            LarghezzaWindow = screen.availWidth;
        } else {
            LarghezzaWindow = parseInt(LarghezzaCont) + aw;
        }
    if (AltezzaCont == "FULL") {
            AltezzaWindow = screen.availHeight;
        } else {
            AltezzaWindow = AltezzaCont;
        }
    iMyWidth = (LarghezzaPagina/2) - (LarghezzaWindow/2);
    iMyHeight = (AltezzaPagina/2) - (AltezzaWindow/2);
    win = window.open(Pagina,nome,"height=" + AltezzaWindow + ",width="	+ LarghezzaWindow 
	+ ",menubar=0,resizable=yes,scrollbars=" + scrol + ",status=" + stat + ",titlebar=0,toolbar=0,left="
	+ iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + "");
    win.focus();
}

function MM_preloadImages() { //v3.0
	var d=document;
	if(d.images) {
		if(!d.MM_p) {
			d.MM_p=new Array();
		}
		var j=d.MM_p.length;
		var a=arguments;
		for(var i=0; i<a.length; i++) {
			if (a[i].indexOf("#")!=0) {
				d.MM_p[j]=new Image;
				d.MM_p[j++].src=a[i];
			}
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImage2() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore2() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;width=100;
}

function showHideContest(curr,tot,id_contest){
	for(var i=0;i<tot;i++){
		if(i==curr){
			document.getElementById("contestCnt"+i).style.display="block";
			document.getElementById("contestA"+i).setAttribute("class", "menuProgrammiSel");
		}else{
			document.getElementById("contestCnt"+i).style.display="none";			
			document.getElementById("contestA"+i).setAttribute("class", "");			
		}
	}
	if(id_contest>0){
		loadAjaxContestVideoXML('/_php/postList.php?act=contest&id='+id_contest,-1,id_contest);
	}
}

function openFileUpFormVideo(){
	for(var i=0;i<9;i++){
		document.getElementById("file"+i).value="";			
		if(i>0)
			chiudiDIV("file"+i+"cnt");		
	}
	apriDIV("file0cnt");			
}

function openFileUpFormFoto(){
	for(var i=0;i<9;i++){
		document.getElementById("file"+i).value="";					
		apriDIV("file"+i+"cnt");		
	}
}

function openFileUpFormTesto(){
	for(var i=0;i<9;i++){
		document.getElementById("file"+i).value="";					
		chiudiDIV("file"+i+"cnt");		
	}
}
function aprichiudi(actdiv){
	if(document.getElementById(actdiv).style.display=="block"){
		document.getElementById(actdiv).style.display="none";		
	}else{
		document.getElementById(actdiv).style.display="block";
	}
}
function apriDIV(actdiv){
	document.getElementById(actdiv).style.display="block";
}

function chiudiDIV(actdiv){
		document.getElementById(actdiv).style.display="none";		
}


function azionaMenuProgramma(actdiv,str){
	document.getElementById("menuA0").innerHTML="<a href=\"#\" onclick=\"azionaMenuProgramma(0,'Mattina');return false;\" title=\"PALINSESTO: MATTINA\">Mattina</a>";
	document.getElementById("menuA1").innerHTML="<a href=\"#\" onclick=\"azionaMenuProgramma(1,'Pomeriggio');return false;\" title=\"PALINSESTO: POMERIGGIO\">Pomeriggio</a>";
	document.getElementById("menuA2").innerHTML="<a href=\"#\" onclick=\"azionaMenuProgramma(2,'Sera');return false;\" title=\"PALINSESTO: SERA\">Sera</a>";
	document.getElementById("menuA3").innerHTML="<a href=\"#\" onclick=\"azionaMenuProgramma(3,'Notte');return false;\" title=\"PALINSESTO: NOTTE\">Notte</a>";	
	for(var i=0;i<4;i++){
		if(i==actdiv){
			document.getElementById("blocco_"+i).style.display="block";
			document.getElementById("menuA"+i).innerHTML="<span class=\"videoMenuSel\">"+str+"</span>";
		}else{
			document.getElementById("blocco_"+i).style.display="none";	
		}
	}

}

function apriDIVMenu(actdiv){
	document.getElementById("blocco_"+actdiv).style.display="block";
	document.getElementById("menuA"+actdiv).setAttribute("class", "menuProgrammiSel");
}

function chiudiDIVMenu(actdiv){
	document.getElementById("blocco_"+actdiv).style.display="none";		 	
	document.getElementById("menuA"+actdiv).setAttribute("class", "");
}

function apriDIVMenuSearch(actdiv){
	document.getElementById("blocco_"+actdiv).style.display="block";
	if(actdiv==0){
		document.getElementById("videoChannelMenuItem0").innerHTML="<span class=\"videoMenuSel\">POST</span>";
		document.getElementById("videoChannelMenuItem1").innerHTML="<a href=\"#\" onclick=\"apriDIVMenuSearch('1');chiudiDIVMenuSearch('0'); return false;\" title=\"RICERCA AVANZATA: UTENTI\">UTENTI</a>";	
	}else{
		document.getElementById("videoChannelMenuItem0").innerHTML="<a href=\"#\" onclick=\"apriDIVMenuSearch('0');chiudiDIVMenuSearch('1'); return false;\" title=\"RICERCA AVANZATA: POST\">POST</a>";
		document.getElementById("videoChannelMenuItem1").innerHTML="<span class=\"videoMenuSel\">UTENTI</span>";	
	}
}

function chiudiDIVMenuSearch(actdiv){
	document.getElementById("blocco_"+actdiv).style.display="none";		 		
}


function checkData(){
	mf=document.register;

	tmp = mf.nick.value;
	if(tmp.length < 1)
	{	alert("Inserisci il Nickname.");
		mf.nick.focus();
		return false;
	};

	if(tmp.length > 12 || tmp.length < 4 )
	{	alert("Inserisci un Nickname compreso tra i 4 e i 12 caratteri.");
		mf.nick.focus();
		return false;
	};

// email control
	if(window.RegExp)
	{
		var rexp=new RegExp("^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.([a-zA-Z]){2,4})$");
		if(!rexp.test(mf.email.value)) 
		{
			alert("L\'e-mail deve avere questo formato: username@dominio.it \r\n\""+mf.email.value+"\" non è un indirizzo valido. Verificalo e prova di nuovo.");
			mf.email.focus();
			return false;
		};
	}else
	{
		if(!(mf.email.value.indexOf("@") > 0))
		{
			alert("L\'e-mail deve avere questo formato: username@do.ma.in \r\n'"+mf.email.value+"' non è un indirizzo valido. Verificalo e prova di nuovo.");
			mf.email.focus();
	      	return false;
		}
	}

	tmp = mf.nome.value;
	if(tmp.length < 1)
	{	alert("Inserisci il Nome");
		mf.nome.focus();
		return false;
	};


	tmp = mf.cognome.value;
	if(tmp.length < 1)
	{	alert("Inserisci il Cognome");
		mf.cognome.focus();
		return false;
	};
	

//  return true if the input date is valid, false otherwise
	day = mf.gg.value;
	month = mf.mm.value-1; //The month needs to be a 0 (zero) for Jan, up to 11 to Dec.
	year = mf.aaaa.value;

	dteDate=new Date(year,month,day);

	if (!((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear())))
	{
		alert("Inserisci una data valida.");
		mf.gg.focus();
		return false;

	};
	
	tmp = mf.citta.value;
	if(tmp.length < 1)
	{	alert("Inserisci la Citta'.");
		mf.citta.focus();
		return false;
	};	

	tmp = mf.prov;
	if(tmp.selectedIndex==0)
	{	alert("Seleziona la Provincia.");
		mf.prov.focus();
		return false;
	};	
	
	tmp = mf.telefono.value;
	if(tmp.length < 1)
	{	alert("Inserisci il telefono.");
		mf.telefono.focus();
		return false;
	};	
	
	if(!mf.sesso[0].checked && !mf.sesso[1].checked){
		alert("Specifica il Sesso.");
		mf.sesso[0].focus();
		return false;
	};
	
	if(!mf.consenso[0].checked){
		alert("Per registrarti devi aderire alle condizioni di trattamento dei dati personali.");
		mf.consenso[0].focus();
		return false;
	};
	return true;
}

function checkDataEdit(){

	mf=document.register;

	tmp = mf.passwd.value;
	tmp2 = mf.passwd2.value;
	if(tmp.length > 0){
		if(tmp.length > 12 || tmp.length < 4 ){
			alert("Inserisci una Password compresa tra i 4 e i 12 caratteri.");
			return false;
		}else{		
			if(tmp!=tmp2){
				alert("Verifica che la password fornita coincida con la password di conferma.");
				return false;
			}
		}
	}

// email control
	if(window.RegExp){
		var rexp=new RegExp("^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.([a-zA-Z]){2,4})$");
		if(!rexp.test(mf.email.value)){
			alert("L\'e-mail deve avere questo formato: username@dominio.it \r\n\""+mf.email.value+"\" non è un indirizzo valido. Verificalo e prova di nuovo.");
			mf.email.focus();
			return false;
		}
	}else{
		if(!(mf.email.value.indexOf("@") > 0)){
			alert("L\'e-mail deve avere questo formato: username@dominio.it \r\n'"+mf.email.value+"' non è un indirizzo valido. Verificalo e prova di nuovo.");
			mf.email.focus();
	      		return false;
		}
	}

	tmp = mf.nome.value;
	if(tmp.length < 1){
		alert("Inserisci il Nome");
		mf.nome.focus();
		return false;
	}


	tmp = mf.cognome.value;
	if(tmp.length < 1){
		alert("Inserisci il Cognome");
		mf.cognome.focus();
		return false;
	}
	

//  return true if the input date is valid, false otherwise
	day = mf.gg.value;
	month = mf.mm.value-1; //The month needs to be a 0 (zero) for Jan, up to 11 to Dec.
	year = mf.aaaa.value;

	dteDate=new Date(year,month,day);

	if (!((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()))){
		alert("Inserisci una data valida.");
		mf.gg.focus();
		return false;
	}
	
	tmp = mf.citta.value;
	if(tmp.length < 1){
		alert("Inserisci la Citta'.");
		mf.citta.focus();
		return false;
	}	

	/*tmp = mf.prov;
	if(tmp.selectedIndex==0){
		alert("Seleziona la Provincia.");
		mf.prov.focus();
		return false;
	}*/	
	
	if(!mf.sesso[0].checked && !mf.sesso[1].checked){
		alert("Specifica il Sesso.");
		mf.sesso[0].focus();
		return false;
	}
	
	tmp = mf.telefono.value;
	if(tmp.length < 1){
		alert("Inserisci il telefono.");
		mf.telefono.focus();
		return false;
	}	
	
	if(!mf.abbonato[0].checked && !mf.abbonato[1].checked){
		alert("Specifica se sei Abbonato.");
		mf.abbonato[0].focus();
		return false;
	}
	return true;
}


function checkPostData(){
	mf=document.post;
	tmp = mf.titolo.value;
	if(tmp.length < 1){
		alert("Inserisci il Titolo.");
		mf.titolo.focus();
		return false;
	}
/*	var tot_contest=0;
	if(document.getElementById("tot_contest")){
		tot_contest=parseInt(document.getElementById("tot_contest").value);
		for(var i=0;i<tot_contest;i++){
			tit=document.getElementById("contest_tit_"+i).innerHTML;
			if(!document.getElementById("contest_"+i+"_si").checked && !document.getElementById("contest_"+i+"_no").checked){
				alert("Scegli se far partecipare il post a "+tit+".");
				document.getElementById("contest_"+i+"_si").focus();
				return false;
			}		
		}		
	}	
*/	
/*	tmp = mf.channelSel;
	if(tmp.selectedIndex==0){
		alert("Seleziona il Canale in cui inviare il video.");
		tmp.focus();
		return false;
	}
*/	
/*	if(STag.length < 1){
		alert("Inserisci almeno una delle Tag standard.");
		return false;
	}else{
		STagStr="";
		for(i=0;i<(STag.length);i++){
			STagStr+=STag[i]+",";
		}			
		STagStr=STagStr.substr(0, (STagStr.length-1));  
		document.getElementById('stag').value=STagStr;		
	}
*/	
	tmp = mf.testo.value;
	if(tmp.length < 1){
		alert("Inserisci il Testo.");
		mf.testo.focus();
		return false;
	}	
	if(mf.tipo_file[0].checked && mf.file0.value.length>1){
		var estVideoArray = new Array("mov", "avi", "wmv", "mpg");	
		if(!validateFileName(mf.file0.value,estVideoArray)){
			mf.file0.value="";				
			mf.file0.focus();
			return false;
		}
	}
	
	if(mf.tipo_file[1].checked){
		var estPhotoArray = new  Array("gif", "jpg", "png");		
		if(mf.file0.value.length>1){
			if(!validateFileName(mf.file0.value,estPhotoArray)){
				mf.file0.value="";				
				mf.file0.focus();
				return false;
			}
		}
		if(mf.file1.value.length>1){
			if(!validateFileName(mf.file1.value,estPhotoArray)){
				mf.file1.value="";				
				mf.file1.focus();
				return false;
			}
		}
		if(mf.file2.value.length>1){		
			if(!validateFileName(mf.file2.value,estPhotoArray)){
				mf.file2.value="";
				mf.file2.focus();
				return false;
			}
		}
		if(mf.file3.value.length>1){		
			if(!validateFileName(mf.file3.value,estPhotoArray)){
				mf.file3.value="";				
				mf.file3.focus();
				return false;
			}
		}
		if(mf.file4.value.length>1){
			if(!validateFileName(mf.file4.value,estPhotoArray)){
				mf.file4.value="";				
				mf.file4.focus();
				return false;
			}	
		}
	}
	
	if(mf.tipo_file[0].checked){
		mf.file1.value="";
		mf.file2.value="";
		mf.file3.value="";
		mf.file4.value="";
	}
	
	if(!mf.consenso_1.checked || !mf.consenso_2.checked || !mf.consenso_3.checked){
		alert("Accetta tutte le condizioni legali.");
		mf.consenso_1.focus();
		return false;
	}	
	return true;
}

var STag= new Array();

function addStag(id,tname){
	if(STag.length==0){
		STag[0]=tname;
		document.getElementById(id).style.color="#EFC700";
	}else{
		allowAdd=true;
		remPos=0;
		STagStr="";
		for(i=0;i<(STag.length);i++){
			if(STag[i]==tname){
				allowAdd=false;
				remPos=i;
				break;
			}
		}
		if(allowAdd){
			STag[STag.length]=tname;
			document.getElementById(id).style.color="#EFC700";
		}else{
			STag.splice(remPos,1);
			document.getElementById(id).style.color="black";
		}
	}
}

function checkMsg(){
	if(document.posta.commento.value.length <= 1){
		alert("Inserisci il testo del commento.");
		return false;
	}else{
		return true;
	}
}

function checkOut(Obj){
	if(Obj.options[Obj.selectedIndex].value!="17"){
		document.getElementById("divtestata").style.display="none";
		document.getElementById("testata").value="";
	}else{
		document.getElementById("divtestata").style.display="block";		
	}
}

function moveTopScroll(){
	window.scrollTo(0,0);
}

var maxchar=7000
function checkLimit(obj){
	if(obj.value.length>maxchar){
		alert("Il testo non deve superare i 7.000 caratteri");
		obj.value=obj.value.substring(0,maxchar);
	}
}
function checkTextLimit(obj){
	if(obj.value.length>999){
		alert("Il testo non deve superare i 1000 caratteri");
		obj.value=obj.value.substring(0,999);
	}
}
function validateFileName(str,est) {
   var charValid = false;
   if(str.indexOf("/")!=-1 && (str.lastIndexOf("/")+1)<str.length ){
	   str=str.substr((str.lastIndexOf("/")+1));
   }
   if(str.indexOf("\\")!=-1 && (str.lastIndexOf("\\")+1)<str.length ){
	   str=str.substr((str.lastIndexOf("\\")+1));
   }
   msg_str_err="";
   if (str.length>0) {
       //valido i caratteri          
	   for (var i = 0; i<str.length; i++) {
           codice = str.charCodeAt(i);
           if (codice == 95 || codice == 45 || codice == 46 || (codice>=48 && codice<=57) || (codice>=65 && codice<=90) || (codice>=97 && codice<=122)) {
               charValid = true;
           } else {
               charValid = false;
               msg_str_err+=" - il carattere: "+str.charAt(i)+" non valido, e' possibile usare solo caratteri alfanumerici ed i simboli '.' '-' '_'\n";
               break;
           }
       }
       //valido le estensioni
       var myest = str.substr((str.length-3));
       var estValid = false;
       for (var j = 0; j<est.length; j++) {
           if (est[j] == myest.toLowerCase()) {
               estValid = true;
               break;
           }
       }
       if (!estValid) {
           charValid = false;
           msg_str_err+=" - il formato "+myest+" non e' consentito.";
       }
	   if(!charValid){
		   alert("Impossibile caricare il file:"+str+".\n"+msg_str_err);
	   }
       return charValid;
   } else {
       return charValid;
   }
}



function showDiv(id1) { //v3.0
	window.document.getElementById(id1).style.display="block";
}
function hideDiv(id1) { //v3.0
	window.document.getElementById(id1).style.display="none";
}


	function act(name){	
		if(window.document.getElementById(name).style.display!="none"){
			window.document.getElementById(name).style.display="none";
		}else{
			window.document.getElementById(name).style.display="block";
		}
	}

	function superAct(box){
		cookieStr=GetCookie("cmsTrace");
		act(box);
		box="|"+box+"|";
		if(cookieStr==null || cookieStr=="") cookieStr="";
		if(cookieStr.indexOf(box)!=-1){
		//	alert(cookieStr.indexOf(box)+"-"+(cookieStr.indexOf(box)+box.length));
			cookieNStr=cookieStr.substr(0,cookieStr.indexOf(box))+cookieStr.substr((cookieStr.indexOf(box)+box.length));
		}else{
			cookieNStr=cookieStr+box;
		}
	//	alert(cookieStr + " op "+box+"\n"+cookieNStr);
		SetCookie("cmsTrace", cookieNStr);
	}		
/* cookie function */	
	function getCookieVal(offset){
	   var endstr = document.cookie.indexOf (";", offset);
	   if (endstr == -1)
		  endstr = document.cookie.length;
		  
	   return unescape(document.cookie.substring(offset, endstr));
	}
	
	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 = (2 < argc) ? argv[2] : null;
	   var path = (3 < argc) ? argv[3] : null;
	   var domain = (4 < argc) ? argv[4] : null;
	   var secure = (5 < argc) ? argv[5] : false;
	   document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
	}
