function isDate(str) 
{ 
	// anno a 4 cifre !!!
	spz = str.split("."); 
	dat = new Date(spz[2], spz[1]-1, spz[0]); 
	if	( 
		dat.getFullYear() == parseFloat(spz[2]) &&
		dat.getMonth() == (parseFloat(spz[1])-1) &&
		dat.getDate() == parseFloat(spz[0]) 
		) {
			return dat;
	}
	return false;
}
function CheckForm()
{
	var da=isDate($("#CheckIn").val())	
	var a=isDate($("#CheckOut").val())	
	var rate=$("#rate").val()
	var adulti=$("#Adults").val()
	var stanze=$("#NumRooms").val()
	var soggiorno=$("#soggiorno").val()
	var diff=(a-da)/86400000;
	if(rate=='')
	{	
		alert("Devi selezionare una tariffa")
	}
	else if(soggiorno>0 && diff<=(soggiorno-1))
	{	
		alert("Il soggiorno minimo per questa tariffa e' di "+soggiorno+" giorni!")
	}
	else if(adulti=='')
	{	
		alert("Devi selezionare il numero di adulti")
	}
	else if(stanze=='')
	{	
		alert("Devi selezionare il numero di stanze")
	}
	else
		document.Search.submit();
}
$(document).ready( function(){
	var w=$(window).width();
	if(w<1263)
	{
		$('#sfondocenter').css('width','1000');
		$('.container').css('width','1000');
		$('#offerte').css('width','1000');
		$('.header').css({'marginLeft':'70px'});
		$('.offerte').css({'marginLeft':'0px'});
		$('.offerte').css({'paddingLeft':'50px'});
		$('.home').css({'marginLeft':'70px'});
		$('.menu').css({'marginLeft':'80px'});
		$('.contenuti').css('width','1000');
		$('.contenuti').css({'marginLeft':'50px'});
		
	}
	$.ajax({
		   type: "GET",
		   url: dir+'/ajax/sfondo.php',
		   data: 'w='+escape(w),
		   success: function(msg){
			   $("#message").html(msg);
           }
	});
	Offerte()
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
	
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value
	
		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="'+dir+'/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
	
		//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
	
		//Apply Margin to Popup
		$('#' + popID).css({
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
	
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
	
		return false;
	});
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
		});
		return false;
	});
})
/* Offerte in home page */
function Offerte()
{	
	$.ajax({
		beforeSend: function() {
			$("#offerte div").fadeOut('fast'); 
			$("#offerte").html('');
			$("#offerte div").fadeIn('slow'); 
		},
		type: "GET",
		url: dir+'/ajax/offerte.php',
		success: function(msg){
			  $("#offerte").html(msg);
        }
	});
}
/* Iscriviti alla newsletter */
function ShowNewsletter()
{	
	var email=$('#mail_newsletter').val();
	$('#DivTrasparenza').css('display','');
	$.ajax({
		type: "GET",
		url: dir+'/ajax/newsletter.php',
		data: 'email='+escape(email),
		success: function(msg){
			$("#message").html(msg);
        }
	});
}
function IscrivitiNewsletter()
{	
	var email=$('#mail_news').val();
	var nome=$('#nome_news').val();
	var cognome=$('#cognome_news').val();
	var captcha=$('#captcha_news').val();
	var consenso=$('#consenso_news').val();
	$('#DivTrasparenza').css('display','');
	$.ajax({
		beforeSend: function(){
     		$('#response').html('<img src='+dir+'/images/loading.gif />');
   		},
		type: "GET",
		url: dir+'/ajax/iscrivinewsletter.php',
		data: 'email='+escape(email)+'&nome='+escape(nome)+'&cognome='+escape(cognome)+'&captcha='+escape(captcha)+'&consenso='+escape(consenso),
		success: function(msg){
			$("#response").html(msg);
        }
	});
}
/* Contattaci */
function Contatta()
{	
	var email=$('#email').val();
	var nome=$('#nome').val();
	var oggetto=$('#oggetto').val();
	var testo=$('#testo').val();
	var code=$('#code').val();
	var consenso=$('#consenso').val();
	//document.getElementById().style.display='';
	$('#DivTrasparenza').css('display','');
	$.ajax({
		   type: "GET",
		   url: dir+'/ajax/contatti.php',
		   data: 'email='+escape(email)+'&nome='+escape(nome)+'&oggetto='+escape(oggetto)+'&testo='+escape(testo)+'&consenso='+escape(consenso)+'&code='+escape(code),
		   success: function(msg){
			   $("#message").html(msg);
           }
	});
}
/* Ricerca pacchetti Header */
function Ricerca()
{	
	var arrivo=$('#arrivo').val();
	var partenza=$('#partenza').val();
	if(arrivo==partenza)
		alert("La data di partenza deve essere maggiore della data di arrivo!")
	else
	{
		$.ajax({
			beforeSend: function(){
				$('#loadingricerca').html('<img src='+dir+'/images/loading.gif />');
			},
			type: "GET",
			url: dir+'/ajax/ricerca.php',
			data: 'arrivo='+escape(arrivo)+'&partenza='+escape(partenza),
			success: function(msg){
				$("#message").html(msg); 
			}
		});
	}
}
/* Ricerca pacchetti */
function CercaPacchetti()
{	
	var trattamento=$('#trattamento').val();
	var arrivo=$('#arrivo_f').val();
	var partenza=$('#partenza_f').val();
	$.ajax({
		beforeSend: function(){
			$('#pacchetti').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/risultati.php',
		data: 'arrivo='+escape(arrivo)+'&partenza='+escape(partenza)+'&trattamento='+escape(trattamento),
		success: function(msg){
			$("#pacchetti").html(msg);
		}
	});
}
/* Aggiungi al carrello () */
function Politiche(pid)
{	
	var stile = "top=10, left=10, width=450, height=600, status=no, menubar=no, toolbar=no scrollbars=yes";
	window.open(dir+'/popup.php?pid='+escape(pid), "", stile);
}
/* Aggiungi al carrello () */
function InCarrello(id_tipo_camera,code,arrivo,partenza,notti,trattamento,tipo,i,codice,ex)
{	
	if(tipo=='pacchetto')
	{
		var adulti=$('#adulti_'+i).val();
		if(ex==1)
		code=$('#code_'+i).val();
	}
	else
		var adulti='';
	$.ajax({
		beforeSend: function(){
   			$('#respcarrello').css('display','');
			$('#respcarrello').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/incarrello.php',
		data: 'id_tipo_camera='+escape(id_tipo_camera)+'&code='+escape(code)+'&arrivo='+escape(arrivo)+'&partenza='+escape(partenza)+'&notti='+escape(notti)+'&trattamento='+escape(trattamento)+'&tipo='+escape(tipo)+'&adulti='+escape(adulti)+'&codice='+escape(codice),
		success: function(msg){
			$("#message").html(msg);
		},
		complete: function(){
			$('#respcarrello').css('display','none');
		}
	});
}
/* Elimina dal carrello () */
function DelCarrello(id,i)
{	
	$.ajax({
		beforeSend: function(){
   			$('#r_'+i).css('display','');
			$('#r_'+i).html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/delcarrello.php',
		data: 'id='+escape(id),
		success: function(msg){
			$("#message").html(msg);
		},
		complete: function(){
			$('#r_'+i).css('display','none');
		}
	});
}
function Aggiorna(campo,i,id_carrello)
{	
	var valore=$('#'+campo+'_'+i).val();
	$.ajax({
		beforeSend: function(){
   			$('#r_'+i).css('display','');
			$('#r_'+i).html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/aggiorna.php',
		data: 'id_carrello='+escape(id_carrello)+'&campo='+escape(campo)+'&valore='+escape(valore)+'&i='+escape(i),
		success: function(msg){
			$('#resp_'+i).html(msg);
		},
		complete: function(){
			$('#r_'+i).css('display','none');
		}
	});
}
function RefAD(val,i)
{
	if(val==1 || val==2)
	{
		$('#ch3_'+i).attr('checked',false);
		$('#ch4_'+i).attr('checked',false);
	}
	if(val==3)
	{
		$('#ch3_'+i).attr('checked',true);
		$('#ch4_'+i).attr('checked',false);
	}
	if(val==4)
	{
		$('#ch3_'+i).attr('checked',true);
		$('#ch4_'+i).attr('checked',true);
	}
}
function CheckNum(val,i,nome,valold)
{
	var adulti=$('#adulti_'+i).val();
	var b26=$('#bambini_2_6_'+i).val();
	var b712=$('#bambini_7_12_'+i).val();
	var gen=Number(adulti)+Number(b26)+Number(b712);
	if(gen>4)
	{
		alert("Attenzione! Il numero massimo di ospiti per questa camera e' di 4 persone.");
		if(nome=='bambini_2_6')
			var t=b26;
		else
			var t=b712;
		$('#'+nome+'_'+i).val(valold);
		return false;
	}
	else	
		return true;
}
/* Controllo Disponibilità */

function CheckDisp()
{	
	var arrive=$('#arrive').val();
	var codice=$('#codice').val();
	$.ajax({
		beforeSend: function() {
			$('#result_dispo').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/checkDisp.php',
		data: 'arrive='+escape(arrive)+'&codice='+escape(codice),
		success: function(msg){
			  $("#result_dispo").html(msg);
        }
	});
}
/* prenotazioni nel carrello */
function Prenotazioni(i)
{	
	$.ajax({
		beforeSend: function(){
			$('#prenotazioni').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/prenotazioni.php',
		success: function(msg){
			$("#prenotazioni").html(msg);
		}
	});
}
function Completa()
{	
	$.ajax({
		beforeSend: function(){
			$('#prenotazioni').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/completa.php',
		success: function(msg){
			$("#prenotazioni").html(msg);
		}
	});
}
function RegistraDati()
{	
	var val='';
	var name='';
	var obbl='';
	$('#prenotazioni input, textarea,select').each( function() {
		val+= $(this).val()+'|';
		name+= $(this).attr("id")+'|';
		obbl+= $(this).attr("class")+'|';
	});
	$.ajax({
		beforeSend: function(){
			$('#response').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/registradati.php',
		data: 'val='+escape(val)+'&name='+escape(name)+'&obbl='+escape(obbl),
		success: function(msg){
			$("#response").html(msg);
		}
	});
}
function Login()
{	
	var user=$('#nomeutente').val();
	var psw=$('#passwordutente').val();
	$.ajax({
		beforeSend: function(){
			$('#login').html('<img src='+dir+'/images/load.gif />');
		},
		type: "GET",
		url: dir+'/ajax/login.php',
		data: 'user='+escape(user)+'&psw='+escape(psw),
		success: function(msg){
			$("#login").html(msg);
		}
	});
}
function PageOp(pag)
{
	$('#pag').val(pag);
	RicercaRisorse();
}
/* Prenota Pacchetti */

function Prenota(codice)
{	
	$('#DivLoading').css('display','');
	$.ajax({
		beforeSend: function(){
			$('#mex').html('<img src='+dir+'/images/loadingrotate.gif />');
		},
		type: "GET",
		url: dir+'/ajax/prenota.php',
		data: 'codice='+escape(codice),
		success: function(msg){
			 $('#DivTrasparenza').css('display',''); $("#message").html(msg); 
		}
	});
}
//Funzione per prelevare codice Javascript all'interno delle pagine AJAX

function mostra(div)
{	
	var stile=document.getElementById(div).style.display
	if(stile=='')
		document.getElementById(div).style.display='none'
	else	
		document.getElementById(div).style.display=''
}
function svuota(div)
{
	$('#'+div).val('');
	$('#'+div).html('');
}
function change(loc)
{
	$('#image_large').attr('src',dir+'/file/large/'+loc)
}
function torna(loc)
{
	document.location.href = linku+"/"+loc;
}
function nascondi(id) {
    document.getElementById(id).style.display='none';
}
function svuota(id) {
	$('#'+id).html('')
}
function blink() {
  $('.blink').each(function() {
    $(this).css('visibility', $(this).css('visibility') != 'hidden' ? 'hidden' : 'visible');
  });
}

$(document).ready(function() {
  setInterval("blink()", 500);
});


function fielderror(div) {
    $('#'+div).css('background', '#ffdbdb');
}

function fieldok(div) {
    $('#'+div).css('background', '#e9ffdb');
};


function Assign(divassign) {
	var val=$('#'+divassign).val();
	if(val==1)
		$('#'+divassign).val('');
	else
		$('#'+divassign).val('1');
}
function Captcha()
{
	$.ajax({
		   type: "GET",
		   url: dir+'/ajax/captcha.php',
		   success: function(msg){
			   $("#captcha").html(msg);
           }
	});
}
