var s=navigator.userAgent.toLowerCase();
var isIE8 = s.indexOf("msie 8.0")>0
var isIE7 = s.indexOf("msie 7.0")>0
var isIE6 = (s.indexOf("msie 6.0")>0) && (!isIE8) && (!isIE7)

/***********************************************
* IFrame SSI script II- C Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/

//Input the IDs of the IFRAMES you wish to dynamically resize to match its content height:
//Separate each ID with a comma. Examples: ["myframe1", "myframe2"] or ["myframe"] or [] for none:
var iframeids=["centrale"]

//Should script hide iframe from browsers that don't support this script (non IE5+/NS6+ browsers. Recommended):
var iframehide="yes"

var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeCaller() {
	var dyniframe=new Array()
	for (i=0; i<iframeids.length; i++){
		if (document.getElementById)
			resizeIframe(iframeids[i])
		//reveal iframe for lower end browsers? (see var above):
		if ((document.all || document.getElementById) && iframehide=="no"){
			var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
			tempobj.style.display="block"
		}
	}
}

function resizeIframe(frameid){
	var currentfr=document.getElementById(frameid)
	if (currentfr && !window.opera){
		currentfr.style.display="block"
		if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
			currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
		else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
			currentfr.height = currentfr.Document.body.scrollHeight;
		if (currentfr.addEventListener)
			currentfr.addEventListener("load", readjustIframe, false)
		else if (currentfr.attachEvent){
			currentfr.detachEvent("onload", readjustIframe) // Bug fix line
			currentfr.attachEvent("onload", readjustIframe)
		}
	}
}

function readjustIframe(loadevt) {
	var crossevt=(window.event)? event : loadevt
	var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
	if (iframeroot)
		resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url){
if (parent.getElementById)
	parent.getElementById(iframeid).src=url
}

if (window.addEventListener)
	window.addEventListener("load", resizeCaller, false)
else if (window.attachEvent)
	window.attachEvent("onload", resizeCaller)
else
window.onload=resizeCaller

function verLogin(frm){
	if (frm.userid.value == "" || frm.password.value == ""){
		errorString = "Userid e password sono obbligatori";
		if (frm.userid.value == ""){
			frm.userid.focus();
		}
		else{
			frm.password.focus();
    	}
		alert(errorString); 
		return false;
	}
}

function VerificaForm(f){
	if(f.name == "frm_registra") {
		if(f.nome.value == "") {
     		f.nome.focus();
     		alert("Il campo NOME è obbligatorio");
     		return false;
    	}
		if(f.cognome.value == "") {
     		f.cognome.focus();
     		alert("Il campo COGNOME è obbligatorio");
     		return false;
    	}
		if(f.giorno.value == "0" || f.mese.value == "0" || f.anno.value == "0"){
    		if (f.giorno.value == "0"){
				alert("La DATA DI NASCITA non è completa, inserisci il GIORNO DI NASCITA");
				f.giorno.focus();
				return false;
			}
		
			if (f.mese.value == "0"){
				alert("La DATA DI NASCITA non è completa, inserisci il MESE DI NASCITA");
				f.mese.focus();
				return false;
			}
		
			if (f.anno.value == "0"){
				alert("La DATA DI NASCITA non è completa, inserisci l'ANNO DI NASCITA");
				f.anno.focus();
				return false;
			}
    	}
		if (f.cellulare.value == ""){
			alert("Il CELLULARE è obbligatorio");
			f.cellulare.focus();
			return false;
		}

		if (f.indirizzo.value == ""){
			alert("L'INDIRIZZO è obbligatorio");
			f.indirizzo.focus();
			return false;
		}
		
		if (f.cap.value == ""){
			alert("Il Codice Avviamento Postale (CAP) è obbligatorio");
			f.cap.focus();
			return false;
		}
		
		if (f.comune.value == ""){
			alert("Il COMUNE è obbligatorio");
			f.comune.focus();
			return false;
		}
		
		if (f.regolamento.value == "NO"){
			alert("E' necessario accettare esplicitamente il regolamento della community");
			f.regolamento.focus();
			return false;
		}
		
		if(f.errore.value == "SI"){
			alert("Sono presenti errori di validazione. Verificare indirizzo email e nome utente");
			return false;
		}
	}
	else if (f.name == "frm_sondaggio") {
		var numero = f.risposta.length
		for (i=0; i<numero; i++) {
			if (f.risposta[i].checked){
				var myAjax = new Ajax.Updater("div_comm_sondaggio", "ajax_comm_sondaggio_submit.asp", {
						method: "post", 
						parameters: "risposta="+encodeURIComponent(f.risposta[i].value)+"&idsondaggio="+encodeURIComponent(f.idsondaggio.value)
					});
				return false
			}
		}
		alert("Devi selezionare almeno un opzione per votare!")
		return false
	}
	else if (f.name == "frm_hits_for_you") {
		if(f.cellulare.value == ""){
			alert("Recapito telefonico obbligatorio");
			return false;
		}
	}
	else if (f.name == "frm_mod_profilo") {
		if(f.errore.value == "SI"){
			alert("Sono presenti errori di validazione. Verificare indirizzo email e nome utente");
			return false;
		}
		if (f.nome.value == ""){
			f.nome.focus();
			alert("Il campo NOME è obbligatorio");
			return false;
		}
	
		if (f.cognome.value == ""){
			f.cognome.focus();
			alert("Il campo COGNOME è obbligatorio");
			return false;
		}
	
		if (f.giorno.value == "0" && f.mese.value == "0" && f.anno.value == "0"){
			alert("La DATA DI NASCITA è obbligatoria");
			f.giorno.focus();
			return false;
		}
	
		if (f.giorno.value == "0"){
			alert("La DATA DI NASCITA non è completa, inserisci il GIORNO DI NASCITA");
			f.giorno.focus();
			return false;
		}
	
		if (f.mese.value == "0"){
			 alert("La DATA DI NASCITA non è completa, inserisci il MESE DI NASCITA");
			 f.mese.focus();
			return false;
		}
	
		if (f.anno.value == "0"){
			alert("La DATA DI NASCITA non è completa, inserisci l'ANNO DI NASCITA");
			f.anno.focus();
			return false;
		}
	
		if (f.indirizzo.value == ""){
			alert("L'INDIRIZZO è obbligatorio");
			f.indirizzo.focus();
			return false;
		}
		
		if (f.cap.value == ""){
			alert("Il Codice Avviamento Postale (CAP) è obbligatorio");
			f.cap.focus();
			return false;
		}
	
		Ctrl = f.email;
		result = false;
		space_tokens = Ctrl.value.split(" ");
		if (space_tokens.length == 1)
		{
			at_tokens = Ctrl.value.split("@");
			if (at_tokens.length == 2)
			{
					if (at_tokens[1].length != 0)
				{
					right_dot_tokens = at_tokens[1].split(".");
					if (right_dot_tokens.length >= 2)
					{
						if (right_dot_tokens[1].length != 0)
						{
							result=true;
						}
					}
				}
			}
		}
	
		if (!result){
			alert("Inserisci un'indirizzo E-MAIL valido");
			Ctrl.focus();
			return (result);
		}
		if(f.utente.value == ""){
			alert("Il NOME UTENTE è obbligatorio");
			f.utente.focus();
			return false;
		}
		if(f.password.value != ""){
			if(f.new_pass.value == ""){
				alert("Indicare la nuova password");
				f.new_pass.focus();
				return false;
			}
			else if(f.new_pass2.value == ""){
				alert("Riscrivere la nuova password");
				f.new_pass2.focus();
				return false;
			} 
			else if(f.new_pass2.value != f.new_pass.value){
				alert("Le nuove password non coincidono. Scrivere la stessa password nei campi appositi.");
				f.new_pass.value = ""
				f.new_pass2.value = ""
				f.new_pass.focus();
				return false;
			}
		}	
	}
}

function CheckEmail(Ctrl){
	result = false;
	space_tokens = Ctrl.value.split(" ");
	if(space_tokens.length == 1)
	{
		at_tokens = Ctrl.value.split("@");
   		if(at_tokens.length == 2)
		{
			if(at_tokens[1].length != 0)
			{
				right_dot_tokens = at_tokens[1].split(".");
				if(right_dot_tokens.length >= 2)
				{
					if (right_dot_tokens[1].length != 0)
					{
						result=true;
					}
				}
			}
		}
	}	
	
	CheckEmail = result
}

function ValidatorAjax(ctrl, img, f){
	var label, url
	
	if(f == "frm_registra"){
		if($(ctrl).name == "email"){
			label = "indirizzo email"
			url = "ajax_check_email.asp"
		}else if($(ctrl).name == "utente"){
			label = "nome utente"
			url = "ajax_check_utente.asp"
		}
	}
	else{
		if($(ctrl).name == "email"){
			label = "indirizzo email"
			url = "ajax_check_email_mod.asp"
		}else if($(ctrl).name == "utente"){
			label = "nome utente"
			url = "ajax_check_utente_mod.asp"
		}
	}
	if($(ctrl).value == ""){
		$(img).src = "img/icons/punto_interrogativo.png";
		$(img).title = "Inserire un " + label + " valido"
		errore.value = "SI"
	} else {
		try {
			new Ajax.Request(
				url + "?valore=" + encodeURIComponent($(ctrl).value), {
					method: "post",
					onSuccess: function(transport) {
						var retrieved_data = transport.responseText || 'no response';
							// HERE: This shows the HTML, but that is not what I need.
							if(retrieved_data == "OK"){
								$(img).src = "img/icons/registrazione_ok.png";
								$(img).title = label + " valido"
								$("errore").value = "NO"
							}else if(retrieved_data == "ESISTE"){
								$(img).src = "img/icons/registrazione_x.png";
								$(img).title = label + " gia presente"
								$("errore").value = "SI"
							}else if(retrieved_data == "NON_VALIDO"){
								$(img).src = "img/icons/punto_interrogativo.png";
								$(img).title = "Inserire un " + label + " valido"
								$("errore").value = "SI"
							}
						},
					onFailure: function() {
						//alert('Oooops');
					}
				}
			);
		} catch(e) {
			alert('problem with Ajax request');
		}
	}
}

function Ajax_LoadSubSelect(ajax_page, targetSelect, selectedValue, pars) {
  	var url = ajax_page
	var obj = $(targetSelect);
	var pars = pars
		
	var myAjax = new Ajax.Request (
		url, 
		{
			method: "post", 
			parameters: pars,
			onComplete: function showResponse(originalRequest){
				//put returned XML in the textarea
				var resp = originalRequest.responseText;
				
				obj.options.length = 0;
				if(resp != ""){
					var arrOptions = resp.split(";");
					for(i = 0; i < arrOptions.length; i++){
						if(arrOptions[i] != ""){
							var arrId = arrOptions[i].split("|");
							if(arrId[0] == selectedValue){
								obj.options[obj.options.length] = 
								new Option(arrId[1], arrId[0], false, true);
							} else {
								obj.options[obj.options.length] = 
								new Option(arrId[1], arrId[0]);
							}
						}
					}
				}
			}
		});
} 

function AjaxLoadSection(div, url, pars){
	var myAjax = new Ajax.Updater(
		div, 
		url, 
		{
			method: "post", 
			parameters: pars
		});	
}

function ShowWebCam(BaseURL){
	 // DisplayWidth specifies the displayed width of the image.
	 // You may have any number here, the effect will be horizontal 
	 // strech or shrink of the image
	 var DisplayWidth = "640";
	
	 // DisplayHeight specifies the displayed width of the image.
	 // You may have any number here, the effect will be vertical 
	 // strech or shrink of the image
	 var DisplayHeight = "480";
	
	 // This is the filepath to the video generating file inside the camera itself
	 var File = "axis-cgi/mjpg/video.cgi?resolution=640x480";
	
	 // No changes required below this point
	
	var output = "";
	if ((navigator.appName == "Microsoft Internet Explorer")&&(navigator.platform != "MacPPC")&&(navigator.platform != "Mac68k"))
	{ 
	 // If Internet Explorer for Windows then use Java Applet 
	  output =  "<APPLET ARCHIVE=AxisCamApplet.zip CODEBASE=\"";
	  output += BaseURL;
	  output += "java/\"";
	  output += " CODE=AxisCamApplet.class HEIGHT=";
	  output += DisplayHeight;
	  output += " WIDTH=";
	  output += DisplayWidth;
	  output += ">";
	  output += "<PARAM NAME=\"StreamLocation\" VALUE=\"/";
	  output += File;
	  output += "&showlength=1\">";
	  output += "</APPLET>";
	
	  document.onstop=stopit;
	  function stopit() {
	  document.applets[0].stop();
	  }
	}
	else 
	{
	  // If not IE for Windows use the browser itself to display
	  output = "<IMG SRC=\"";
	  output += BaseURL;
	  output += File;
	  output += "&dummy=garb\" HEIGHT=\"";
	  // The above dummy cgi-parameter helps some versions of NS
	  output += DisplayHeight;
	  output += "\" WIDTH=\"";
	  output += DisplayWidth;
	  output += "\" ALT=\"Moving Image Stream\">";
	} 
	document.write(output);
}