//----------------------------------------------     Browser & Div      -------------------------------------------
function checkBrowser()
{
	
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.agent=navigator.userAgent;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	//this.saf=(this.ver.indexOf("WebKit")>-1 && this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5 || this.saf)
	return this

	/*
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
	*/
}

bw=new checkBrowser()


function ShowOrHide(div,nest)
{
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	
	if (obj.display=='inline')
	{
		 obj.display='none';
	}
	else 
	{
		obj.display='inline';
	}	
}

function Show(div,nest)
{
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.display='inline'
}

function Hide(div,nest)
{
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.display='none'
}

//----------------------------------------------     Varie     -------------------------------------------
function paginazione(pag) {
	document.getElementById("pag").value = pag;
	document.paginazione.submit();
}

function pwdGenerator()
{
	var var1=String.fromCharCode(97+Math.floor(Math.random()*26));
	var var2=String.fromCharCode(97+Math.floor(Math.random()*26));
	var var3=String.fromCharCode(97+Math.floor(Math.random()*26))
	var var4=10+Math.floor(Math.random()*89);
	var var5=10+Math.floor(Math.random()*89);
	var var6=100+Math.floor(Math.random()*99);
	
	return (var1+var2+var4+var3+var5+var6);

}

function WWopen(what,wx,wy)
{
	window.open(what,'windows','width='+wx+',height='+wy+',status=no,scrollbars=yes,location=no,toolbar=no,resizable=yes');
}


//----------------------------------------------     Controllo Input      -------------------------------------------
function confermaElimina(modulo,azione,div){
	var mod = document.forms[modulo];
	var fine = confirm(Messaggi[4]);
	if (fine){
		RunCMD(div,azione,mod,6);
	}
}

function SettaDati(pulsante){
	var nomeComposito = pulsante.name;
	var valore = pulsante.value;
	arrNome = nomeComposito.split("_");
	
	document.getElementById("casella").value = valore;
	document.getElementById("composito").value = nomeComposito;
	
	Show('Val','');
	document.getElementById("casella").focus();
}

function chkSndSub(modulo,azione){
var mod = document.forms[modulo];
var fine = true;
	for (i=0; i<mod.length; i++){
		var tmpObj = mod.elements[i];
		if (tmpObj.id){
			if (tmpObj.value == ''){
				ShowMessage (tmpObj.name+" : "+Messaggi[3]);
				fine = false;
				return;
			}
		}
	}
	if (fine){
		document.forms[modulo].action = azione;
		document.forms[modulo].submit();
	}
}

function chkSndAjax(modulo,azione,div){
var mod = document.forms[modulo];
var fine = true;
	for (i=0; i<mod.length; i++){
		var tmpObj = mod.elements[i];
		if (tmpObj.id){
			if (tmpObj.value == ''){
				ShowMessage (tmpObj.name+" : "+Messaggi[3]);
				fine = false;
				return;
			}
		}
	}
	if (fine){
		RunCMD(div,azione,mod,6);
	}
}

function checklenSubmit(data,max)
{
	var	dax=data.value.substring(0,max-1);
	if (data.value.length>max) 
	{
		data.value=dax
		ShowMessage(Messaggi[0]); //"Raggiunto numero massimo di caratteri
	}
	else
	{
		document.form1.submit();
	}
}

function checklen(data,max)
{
	var	dax=data.value.substring(0,max-1);
	if (data.value.length>max) 
	{
		data.value=dax
		ShowMessage(Messaggi[0]); //"Raggiunto numero massimo di caratteri
	}
}


function check (field, kind)
{
	var e = 0,i,schar,found,Rchars,a='',appfield;
	switch (kind){
		case 0:Rchars = "abcdefghilmnopqrstuvzwxykj.@_-1234567890";break;	//email
   		case 1:Rchars = "abcdefghilmnopqrstuvzwxykj ";break;             	//string
   		case 2:Rchars = "1234567890";break;                              	//number
   		case 3:Rchars = "abcdefghilmnopqrstuvzwxykj1234567890,. ";break; 	//address
   		case 4:Rchars = "abcdefghilmnopqrstuvzwxykj1234567890";break;    	//Utente
   		case 5:Rchars = "1234567890,";break;                              	//decimale
		case 6:Rchars = "1234567890,-+";break;                              //algebrico
		case 7:Rchars = "1234567890-";break;								//data
		case 8:Rchars = "1234567890:.";break;								//ora
		default: break;
	}
		
	appfield=field.value.toLowerCase();
	for (i=0;i<field.value.length;i++)	{
		schar =appfield.charAt(i);
		found=Rchars.indexOf(schar);
		if (found==-1)
		{
			e=1;
			a.substr(0,i);
			field.focus();
			break;
			break;
		}
		else
		{
			a += schar;
		}
	}
	
	if (e==1){
		field.value=a;
		field.focus();
		ShowMessage(Messaggi[2]+" "+Rchars);
		return(false);
	}
	return(true);
}

function checkemail (field)
{
	var e=false,apos,dotpos,lastpos;
	e=check(field, 0);
	if (field.value!="" || e==true){
		apos=field.value.indexOf("@");
		dotpos=field.value.lastIndexOf(".");
		lastpos=field.value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) e=false;
	}
	if (e==false && field.value!=""){
		ShowMessage (Messaggi[1]);  //indirizzo email inserito non valido
        field.focus();
	}
	return(e)
}
//----------------------------------------------     Ajax      -------------------------------------------

function sendCMD(scrtp,data)
{	
	var myAjax = new Ajax.Request(scrtp,{method: 'post',parameters: data,onComplete: sResponse});
}

function sResponse(ajobj) 
{

	  if (ajobj.readyState == 4) 
	  {
			
			if(ajobj.status == 200)
			{
				 ElabResults(ajobj.responseText);
			} 
			else 
			{
				ShowMessage("ERRORE:" + ajobj.status)
			}
	  }
	  
}


function ShowMessage(msg,yy)
{
		if (yy==null){document.getElementById('msg').style.height='100px';} else {document.getElementById('msg').style.height=yy+'px';}
		document.getElementById('msg').innerHTML="<BR><div align=center>"+msg+"<BR><BR><input class='bottone' type='button' name='Chiudi' value='Chiudi' onclick=Hide('msg'); ></div>"
		Show("msg");
	
}

function ShowMessageLang(idmsg,yy)
{
		var mms;
		mms=MessageLang(idmsg);
		
		if (yy==null){document.getElementById('msg').style.height='100px';} else {document.getElementById('msg').style.height=yy+'px';}
		document.getElementById('msg').innerHTML="<BR><div align=center>"+mms+"<BR><BR><input class='bottone' type='button' name='Chiudi' value='Chiudi' onclick=Hide('msg'); ></div>"
		Show("msg");
	
}

function RunCMD(ContDiv,cmd,cmdata,postAct,param1,param2,param3)
{
	var bckDiv='';
	
	if (cmdata){datafrom=Form.serialize(cmdata)}
	
 	if (document.getElementById(ContDiv))
	 {
	  bckDiv=document.getElementById(ContDiv).innerHTML;
	  document.getElementById(ContDiv).innerHTML='<img src="../img/loading2.gif" alt="Loading.." align="absmiddle"/>';
	 }
	
	if (cmd)
	{
		new Ajax.Request(cmd,
		{
			method: 'post',parameters: datafrom,onSuccess: function(results)
			{
				switch (postAct)
				{
					case 0: //sostituisce img senza href
						//alert("pausa");
						document.getElementById(ContDiv).innerHTML='<img src="'+param1+'" align="absmiddle" />';
						break;
					case 1://swap div
						//alert("pausa");
						Hide(ContDiv);
						Show(param1); 	 
						break;
					case 2://Ri visualizza bottone e lo disabilita
						//alert(results.responseText);
						document.getElementById(ContDiv).innerHTML=bckDiv;
						document.getElementById(param1).disabled=true;
						break;
					case 4://Sostituisce con param1
						//alert("pausa");
						document.getElementById(ContDiv).innerHTML=param1;
						break;
					case 5://Sostituisce con responseText
						//alert("pausa");
						document.getElementById(ContDiv).innerHTML=results.responseText;
						break;
					case 6://carica pagina al posto di div da response text
						//alert("pausa");
						window.location.href = results.responseText;
						break;
				}	
			}
		,onFailure: ShowMessage
		});
	}
}