/****************************************************************************************
*** CALENDARIO v1.7 - 05-05-2011													  ***
****************************************************************************************/
lingua="IT";

GiornoDellaSettimana = new Array("Dom","Lun","Mar","Mer","Gio","Ven","Sab");
NomeDelMese = new Array("Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");

GiornoDellaSettimanaEN = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
NomeDelMeseEN = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
//--------------------------------------------------------------------------------------------
function FixYear(anno){
	if(anno<200)return anno+1900;
	else return anno;
}
//--------------------------------------------------------------------------------------------
function WeekDay(anno, mese, giorno){
	var data1=new Date(anno,mese,giorno);
	if(lingua=="IT") return GiornoDellaSettimana[data1.getDay()];
	else return GiornoDellaSettimanaEN[data1.getDay()];
}
//--------------------------------------------------------------------------------------------
function nomeMese(mese){
	if(lingua=="IT") return NomeDelMese[mese];
	else return NomeDelMeseEN[mese];
}
//--------------------------------------------------------------------------------------------

function DaysInMonth(WhichMonth, WhichYear)
{
	var DaysInMonth = 31;
	if (WhichMonth == 3 || WhichMonth == 5 || WhichMonth == 8 || WhichMonth == 10) DaysInMonth = 30;
	if (WhichMonth == 1)  {
  		if ((WhichYear % 4 == 0 && WhichYear % 100 != 0) || WhichYear % 400 == 0 || WhichYear == 0) DaysInMonth = 29;
		else DaysInMonth = 28;
	}
	return DaysInMonth;
}
//--------------------------------------------------------------------------------------------
function numMese(NomeMese)
{
		 if (NomeMese=="Gennaio") return 0;
	else if (NomeMese=="Febbraio") return 1;
	else if (NomeMese=="Marzo") return 2;
	else if (NomeMese=="Aprile") return 3;
	else if (NomeMese=="Maggio") return 4;
	else if (NomeMese=="Giugno") return 5;
	else if (NomeMese=="Luglio") return 6;
	else if (NomeMese=="Agosto") return 7;
	else if (NomeMese=="Settembre") return 8;
	else if (NomeMese=="Ottobre") return 9;
	else if (NomeMese=="Novembre") return 10;
	else if (NomeMese=="Dicembre") return 11;
	else if (NomeMese=="January") return 0;
	else if (NomeMese=="February") return 1;
	else if (NomeMese=="March") return 2;
	else if (NomeMese=="April") return 3;
	else if (NomeMese=="May") return 4;
	else if (NomeMese=="June") return 5;
	else if (NomeMese=="July") return 6;
	else if (NomeMese=="August") return 7;
	else if (NomeMese=="September") return 8;
	else if (NomeMese=="October") return 9;
	else if (NomeMese=="November") return 10;
	else if (NomeMese=="December") return 11;
}
//--------------------------------------------------------------------------------------------

function ChangeCheckinMese(){

	var now=new Date();
	if (document.getElementById('sm').options.length<5){

		document.getElementById("sm").options.length=0;
		nUltimoMese=12;
		nUltimoAnno=FixYear(now.getYear())+1;
	
		fine=nUltimoAnno*100+nUltimoMese;
		document.getElementById("em").options.length=0;
		nMese=now.getMonth();
		Anno=FixYear(now.getYear());
		
		//Fix ultimo del mese (successivo alla modifica per prenotazioni sucecssive ad aggi) del #30 giugno 2009#
		if(DaysInMonth(nMese,FixYear(now.getYear()))<(now.getDate()+1)){
			nMese++;
			if (nMese==12){
				nMese=0;
				Anno++;
			}
		}
		//----------------------------------------------------------------------------------------------------

		
		i=Anno*100+nMese;
		while (i<fine){
			var anOption = document.createElement("option");
			anOption.value=(nMese+1)+"."+Anno;
			anOption.text = nomeMese(nMese)+" '"+Anno.toString().substring(2);
			document.getElementById("sm").options.add(anOption);
			nMese++;
	
			if (nMese==12){
				nMese=0;
				Anno++;
			}
			
			i=Anno*100+nMese;
		}
	}

	if(!document.getElementById('sd'))return false;
	
	//Se � selezionato un giorno lo segna
	if (document.getElementById('sd').options.length>0){
		GiornoSel=document.getElementById('sd').options[document.getElementById('sd').selectedIndex].text;
		nGiornoSel=parseInt(GiornoSel.substring(GiornoSel.length-2));
	} else nGiornoSel=1;
	document.getElementById('sd').options.length=0;


	//Trova il mese e l'anno selezionato
	NomeMese=document.getElementById('sm').options[document.getElementById('sm').selectedIndex].text;
	Anno=2000+parseInt(NomeMese.substring(NomeMese.length-2,NomeMese.length),10);
	nMese=numMese(NomeMese.substring(0,NomeMese.length-4));

	//Individua il primo giorno disponibile

	dagiorno=1;
	//Se � selezionato il mese corrente setta oggi come primo gg disp
	if (parseInt(now.getMonth())==parseInt(nMese) && parseInt(FixYear(now.getYear()))==parseInt(Anno))dagiorno=now.getDate();

	if(nGiornoSel==31 && Anno==FixYear(now.getYear())+1) nGiornoSel=30;
	
	//Crea tutti i giorni
	for (i=dagiorno; i<=DaysInMonth(nMese,Anno); i++)
	{
 	    var anOption = document.createElement("option");
		anOption.value = i;
		anOption.text = WeekDay(Anno,nMese,i) +" "+i;
		if (i==nGiornoSel) {
				anOption.selected=true;
				anOption.defaultSelected=true;
		}
		document.getElementById("sd").options.add(anOption);
	}

	CaricaCheckout();
}

//--------------------------------------------------------------------------------------------

function ChangeCheckinGiorno(){

	SelNomeMese=document.getElementById('sm').options[document.getElementById('sm').selectedIndex].text;

	var now=new Date();
	if (
		document.getElementById('sd').selectedIndex==document.getElementById('sd').length-1 &&
		document.getElementById('sm').selectedIndex==document.getElementById('sm').length-1)
	{
		document.getElementById('sd').selectedIndex=document.getElementById('sd').selectedIndex-1;
	}

	//Trova checkin
	checkin=new Date(
		parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
		parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
		parseInt(document.getElementById('sd').options[document.getElementById('sd').selectedIndex].value)
	);

	
	//Trova checkout
	checkout=new Date(
		parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
		parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
		parseInt(document.getElementById('sd').options[document.getElementById('sd').selectedIndex].value)+1
	);

	//Carica i mesi nel checkout dal checkout alla fine
		nUltimoMese=12;
		nUltimoAnno=FixYear(now.getYear())+1;
		fine=nUltimoAnno*100+nUltimoMese;


		nMese=checkout.getMonth();
		Anno=FixYear(checkout.getYear());
		i=Anno*100+nMese;

	
		document.getElementById("em").options.length=0;
		while (i<fine){
			var anOption = document.createElement("option");
			anOption.value=(nMese+1)+"."+Anno;
			anOption.text = nomeMese(nMese)+" '"+Anno.toString().substring(2);
			document.getElementById("em").options.add(anOption);
			nMese++;
	
			if (nMese==12){
				nMese=0;
				Anno++;
			}
			
			i=Anno*100+nMese;
		}
	//Carica i giorni nel checkout dal checkout alla fine
	CaricaCheckout();
}
function ChangeCheckoutMese(){
	now=new Date();
	//Carica i giorni nel checkout dal checkout alla fine

	SelNomeMese=document.getElementById('sm').options[document.getElementById('sm').selectedIndex].text;
	checkin=new Date(
		parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
		parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
		parseInt(document.getElementById('sd').options[document.getElementById('sd').selectedIndex].value)
	);
	SelNomeMese=document.getElementById('em').options[document.getElementById('em').selectedIndex].text;	
	checkout=new Date(
		parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
		parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
		parseInt(document.getElementById('ed').options[document.getElementById('ed').selectedIndex].value)
	);

	//Se esiste il giorno del mese selezionato (es. 30/02 d� errore)
	if(checkout.getMonth()!==parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4)))){
		checkout=new Date(
			parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
			parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
			1
		);
	}
	nMese=checkout.getMonth();
	Anno=FixYear(checkout.getYear());
	document.getElementById("ed").options.length=0;
	inizio=1;
	if(checkout.getMonth()==checkin.getMonth())inizio=checkin.getDate()+1;
	for (i=inizio; i<=DaysInMonth(checkout.getMonth(),FixYear(checkout.getYear())); i++)
	{
 	    var anOption = document.createElement("option");
		anOption.value = i;
		anOption.text = WeekDay(Anno,nMese,i) +" "+i;
		if (i==checkout.getDate()) {
				anOption.selected=true;
				anOption.defaultSelected=true;
		}
		document.getElementById("ed").options.add(anOption);
	}
};
//---------------------------------------------------------------------------------------
function CaricaCheckout(){
	SelNomeMese=document.getElementById('sm').options[document.getElementById('sm').selectedIndex].text;

	var now=new Date();
	if (
		document.getElementById('sd').selectedIndex==document.getElementById('sd').length-1 &&
		document.getElementById('sm').selectedIndex==document.getElementById('sm').length-1)
	{
		document.getElementById('sd').selectedIndex=document.getElementById('sd').selectedIndex-1;
	}

	//Trova checkin
	checkin=new Date(
		parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
		parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
		parseInt(document.getElementById('sd').options[document.getElementById('sd').selectedIndex].value)
	);


	
	//Trova prossimo checkout
	checkout=new Date(
		parseInt(SelNomeMese.substring(SelNomeMese.length-2),10)+2000,
		parseInt(numMese(SelNomeMese.substring(0,SelNomeMese.length-4))),
		parseInt(document.getElementById('sd').options[document.getElementById('sd').selectedIndex].value)+1
	);

	//Carica i mesi nel checkout dal checkout alla fine
	
		nUltimoMese=12;
		nUltimoAnno=FixYear(now.getYear())+1;
		fine=nUltimoAnno*100+nUltimoMese;
		document.getElementById("em").options.length=0;
		nMese=checkout.getMonth();
		Anno=FixYear(checkout.getYear());
		i=Anno*100+nMese;
		while (i<fine){
			var anOption = document.createElement("option");
			anOption.value=(nMese+1)+"."+Anno;
			anOption.text = nomeMese(nMese)+" '"+Anno.toString().substring(2);
			document.getElementById("em").options.add(anOption);
			nMese++;
	
			if (nMese==12){
				nMese=0;
				Anno++;
			}
			
			i=Anno*100+nMese;
		}
		
	//Carica i giorni nel checkout dal checkout alla fine
	Anno=FixYear(checkout.getYear());
	nMese=checkout.getMonth();
	document.getElementById("ed").options.length=0;
	for (i=checkout.getDate(); i<=DaysInMonth(checkout.getMonth(),FixYear(checkout.getYear())); i++)
	{
 	    var anOption = document.createElement("option");
		anOption.value = i;
		anOption.text = WeekDay(Anno,nMese,i) +" "+i;
		if (i==checkout.getDate()) {
				anOption.selected=true;
				anOption.defaultSelected=true;
		}
		document.getElementById("ed").options.add(anOption);

	}
}
//################################################################################################################

function ControlloForm1(Form){
	nPosti=0;
	errore=false;
	num=0;
	
	for (i=0; i < Form.elements.length; i++) {
		ele = Form.elements[i];
		if(ele.name && ele.name.substr(0,6)=="stanza" && parseInt(ele.value)>0) nPosti+=parseInt(ele.value);
	}

	if (nPosti==0) {
		if (lingua=="IT") alert('Selezionare almeno una camera.');
		else alert('Select a room please.');
		return false;
	}
	else return true;
}

//--------------------------------------------------------------------------------------------
function trim(stringa) 
{
	while (stringa.substring(0,1) == ' ')
	{
		stringa = stringa.substring(1, stringa.length);
	}
	while (stringa.substring(stringa.length-1, stringa.length) == ' ')
	{
		stringa = stringa.substring(0,stringa.length-1);
	}
	if (stringa=="") return "0";
	return stringa;
}
//--------------------------------------------------------------------------------------------
function ControllaFormato(stringa)
{
	errore=false;
	if (stringa.length==0)errore=true;

	for (i=0;i<stringa.length;i++){
		car=stringa.substring(i,i+1);

		if (car!=="0" && car!=="1" && car!=="2" && car!=="3" && car!=="4" && car!=="5" && car!=="6" && car!="7" && car!=="8" && car!=="9"){
			errore=true;
		}
	}	
	return !errore;
}
//--------------------------------------------------------------------------------------------
function Ingrandisci(img){
	document.getElementById('ingrandimento').src=img;
	return false;
};
//--------------------------------------------------------------------------------------------
function selIn(giorno){
	var $j=giorno.split("-");

	ob=document.getElementById('sm');
	for (i=0; i<ob.length;i++) {
		if(ob.options[i].value==parseInt($j[1])+"."+$j[2]) ob.selectedIndex=i;
	}
	ChangeCheckinMese();
	ob=document.getElementById('sd');
	for (i=0; i<ob.length;i++) {
		if(ob.options[i].value==parseInt($j[0])) ob.selectedIndex=i;
	}
	CaricaCheckout();
}
//--------------------------------------------------------------------------------------------
function selOut(giorno){
	var $j=giorno.split("-");

	ob=document.getElementById('em');
	for (i=0; i<ob.length;i++) {
		if(ob.options[i].value==parseInt($j[1])+"."+$j[2]) ob.selectedIndex=i;
	}
	ChangeCheckoutMese();
	ob=document.getElementById('ed');
	for (i=0; i<ob.length;i++) {
		if(ob.options[i].value==parseInt($j[0])) ob.selectedIndex=i;
	}
}
//--------------------------------------------------------------------------------------------
function controllonum(){
	$v=window.event.keyCode;
	if(!$v || $v==0 || $v==8 || $v==9 || $v==13 || $v==27) return null;
	$k=String.fromCharCode($v);
	$charset="0123456789";
	if (($charset).indexOf($k) > -1) return true; else return false;
}
//--------------------------------------------------------------------------------------------

/*#####################################################################################
## STRINGHE																			 ##
#####################################################################################*/
function ContaChar(variabile,char){
	variabile=variabile.match('/['+char+']/g');
	if(!variabile) return 0; else return variabile.length;
}
//-------------------------------------------------------------------------------------
function ContaNumeri(variabile){
	variabile=variabile.match(/[0-9]/g);
	if(!variabile) return 0; else return variabile.length;
}
//-------------------------------------------------------------------------------------
function ContaMinuscole(variabile){
	variabile=variabile.match(/[a-z]/g);
	if(!variabile) return 0; else return variabile.length;
}
//-------------------------------------------------------------------------------------
function ContaMaiuscole(variabile){
	variabile=variabile.match(/[A-Z]/g);
	if(!variabile) return 0; else return variabile.length;
}
//-------------------------------------------------------------------------------------
function trim(stringa){
	while (stringa.substring(0,1) == ' ') stringa = stringa.substring(1, stringa.length);
	while (stringa.substring(stringa.length-1, stringa.length) == ' ') stringa = stringa.substring(0,stringa.length-1);
	return stringa;
}

/*#####################################################################################
## CONTROLLI																		 ##
#####################################################################################*/
function check($nomecampo){
	var $msg="";
	for ($nomeform in forms){
		if(forms[$nomeform][$nomecampo]){
			if(forms[$nomeform][$nomecampo]["etichetta"]) $etichetta=forms[$nomeform][$nomecampo]["etichetta"]; else $etichetta="";
			if(forms[$nomeform][$nomecampo]["opzioni"]  ) $opz      =forms[$nomeform][$nomecampo]["opzioni"];   else $opz="";
			if(forms[$nomeform][$nomecampo]["min_len"]  ) $min      =parseInt(forms[$nomeform][$nomecampo]["min_len"]);   else $min=0;
			if(forms[$nomeform][$nomecampo]["max_len"]  ) $max      =parseInt(forms[$nomeform][$nomecampo]["max_len"]);   else $max=0;
			if(forms[$nomeform][$nomecampo]["charset"]  ) $charset  =forms[$nomeform][$nomecampo]["charset"];   else $charset="";

			document.getElementById($nomecampo).value=trim(document.getElementById($nomecampo).value);
			if($opz=="lower") document.getElementById($nomecampo).value=document.getElementById($nomecampo).value.toLowerCase();
			if($opz=="upper") document.getElementById($nomecampo).value=document.getElementById($nomecampo).value.toUpperCase();

			if(document.getElementById($nomecampo)) $valore=document.getElementById($nomecampo).value;
			else $valore="";
			
			if(!$msg && $min>0 && $min==$max && $valore.length!==$min){
				if(lingua!=="EN") $msg="Il campo "+$etichetta+" deve contenere "+$min+" caratteri.";
				else $msg="Field "+$etichetta+" must contain "+$min+" characters.";
			}
			if(!$msg && $min>0 && $valore.length<$min){
					if(lingua!=="EN") $msg="Il campo "+$etichetta+" deve contenere almeno "+$min+" caratteri.";
					else $msg="Field "+$etichetta+" must contain at least "+$min+" characters.";
			}
			if(!$msg && $max>0 && $valore.length>$max){
				if(lingua!=="EN") $msg="Il campo "+$etichetta+" pu� contenere al massimo "+$max+" caratteri.";
				else $msg="Field "+$etichetta+" can contain a maximum of "+$min+" characters.";
			}

			if(!$msg && $charset){
				$charset=$charset.replace("09","0123456789");
				$charset=$charset.replace("az","abcdefghijklmnopqrstuvwxyz");
				$charset=$charset.replace("AZ","ABCDEFGHIJKLMNOPQRSTUVWXYZ");

				for(i=0;i<$valore.length;i++){
					$k=$valore.charAt(i);
					if (($charset).indexOf($k) < 0){
						if(lingua!=="EN") $msg="Il campo "+$etichetta+" contiene caratteri non consentiti. [ => "+$k+"]";
						else $msg="Field "+$etichetta+" contains characters not allowed. [ => "+$k+"]";
					}
				}
			}
		}
	}
	return $msg;
};
//-------------------------------------------------------------------------------------
function addControlliKeys($nomeform){
	for ($input in forms[$nomeform]){
		if(document.getElementById($input))document.getElementById($input).onkeypress=function(){return controllokey(this);}
		if(document.getElementById($input))document.getElementById($input).onchange = function(){return controllochange(this);}
		if(forms[$nomeform][$input]['max_len'] && document.getElementById($input)) document.getElementById($input).setAttribute('maxLength', parseInt(forms[$nomeform][$input]['max_len']));
	};
};
//-------------------------------------------------------------------------------------
function controllochange($obj){
	for ($nomeform in forms){
		for ($input in forms[$nomeform]){
			if($input==$obj.name){
				if(forms[$nomeform][$input]['opzioni'])$opz=forms[$nomeform][$input]['opzioni']; else $opz="";
				if($opz=="lower") document.getElementById($input).value=document.getElementById($input).value.toLowerCase();
				if($opz=="upper") document.getElementById($input).value=document.getElementById($input).value.toUpperCase();
			}
		}
	}
}
//-------------------------------------------------------------------------------------
function controllokey($obj){
	for ($nomeform in forms){
		for ($input in forms[$nomeform]){
			if($input==$obj.name){
				if(forms[$nomeform][$input]['opzioni'])$opz=forms[$nomeform][$input]['opzioni']; else $opz="";
				if(forms[$nomeform][$input]['charset'])$charset=forms[$nomeform][$input]['charset']; else $charset="";
			}
		}
	}
	$v=window.event.keyCode;
	if(!$v || $v==0 || $v==8 || $v==9 || $v==13 || $v==27) return null;
	if($opz=="upper"){
		if ($v>=97 && $v<=122) {window.event.keyCode=$v-32; $v-=32;}
	}
	if($opz=="lower"){
		if ($v>=65 && $v<=90) {window.event.keyCode=$v+32; $v+=32;}
	}
	if(!$charset) return true;
	$k=String.fromCharCode($v);
	$charset=$charset.replace("09","0123456789");
	$charset=$charset.replace("az","abcdefghijklmnopqrstuvwxyz");
	$charset=$charset.replace("AZ","ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	if (($charset).indexOf($k) > -1) return true; else return false;
}
//-------------------------------------------------------------------------------------
function controlla_form($nomeform){
	$errore=false;
	for ($input in forms[$nomeform]){
		if(!$errore){
			$errore=check($input);
			if($errore) $input_focus=$input;
		}
	}
	return $errore;
}
//-------------------------------------------------------------------------------------
forms=new Array();
$input_focus=null;
