// JavaScript Document

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=args[i+1]; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' debe contener un e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' debe ser numérico.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' debe contener un número entre '+min+' y '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es obligatorio.\n'; }
    } if(errors){alert('Han ocurrido los siguientes errores:\n'+errors)}else{setTimeout('document.getElementById(\'confirmationButton\').disabled=1;',500);};
  document.MM_returnValue = (errors == '');
}


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 validateExsc() {
	var adlt=document.getElementById("Adults").value;
	var chld=document.getElementById("Children").value;
	if(adlt==0 && chld==0) {
		alert('Debe introducir al menos un Adulto o un Niño para continuar!');
	} else {
		document.frsv01.action="entradas.php";
		document.frsv01.submit();
	}
}


function removeProduct(ptoken, spui) {
	document.forms['remove'].PTOKEN.value=ptoken;
	document.forms['remove'].SPUI.value=spui;
	document.forms['remove'].REMOVE.value='remove';
	document.forms['remove'].submit();
}

function removeAll(ptoken) {
	document.forms['remove'].PTOKEN.value=ptoken;
	document.forms['remove'].REMOVE.value='all';
	document.forms['remove'].submit();
}

function clearValues() {
	document.forms['remove'].PTOKEN.value='';
	document.forms['remove'].SPUI.value='';
	document.forms['remove'].REMOVE.value='';
}

function bookEntrada(tCode,mCode,num) {
	var desde=document.getElementById("cfrm_date_"+num).value;
	var days=document.getElementById("days_"+num).value;		
	var y_in=desde.substring(0,4);
	var m_in=desde.substring(4,6);
	var d_in=desde.substring(6);	



//	var date_in=new Date(y_in,(m_in*1),(d_in*1));

	var date_in=new Date();
	date_in.setFullYear(y_in);
	date_in.setMonth((m_in*1)-1);
	date_in.setDate((d_in*1));

	var n_date="";
	if((days*1)>1) {
		n_date=new Date(date_in.getTime() + ((days-1)*1)*24*60*60*1000);	
	} else {
		n_date=new Date(date_in.getTime());	
	}
	
	
	
	var nm=(num.indexOf("_")==-1?'0':num.substring(num.indexOf("_")+1));

	var hasta=n_date.getFullYear()+""+((n_date.getMonth()+1)<=9?"0"+(n_date.getMonth()+1):(n_date.getMonth()+1))+""+(n_date.getDate()<=9?"0"+n_date.getDate():n_date.getDate());		
	document.location.href="step-3.php?tCode="+tCode+"&mCode="+mCode+"&desde="+desde+"&hasta="+hasta+"&num="+nm;
}


function showDetails(a,b,c) {
	document.location.href="entrada-details.php?avToken="+a+"&tCode="+b+"&tName="+c;
}

function DoOnLoad(){
	window.focus();
	if(typeof(roomChanged)!="undefined")roomChanged(12)
}
function MM_openBrWindow(theURL,winName,features){
  window.open(theURL,winName,features);
  window.focus();
}

function openWindowApointment (url,wdth,hght) {
	window.open(url,'flag','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+wdth+',height='+hght+',top=100,left='+((screen.availWidth - 500) /2));
}

function showImage(theURL){
	var p_image=document.getElementById("PIMAGE");
	p_image.src=theURL;
}


function showChildren(value){

	switch(value) {
		case '1':
			document.getElementById("child01").style.display="block";
			document.getElementById("child02").style.display="none";
			document.getElementById("child03").style.display="none";
			break;
		case '2':
			document.getElementById("child01").style.display="block";
			document.getElementById("child02").style.display="block";
			document.getElementById("child03").style.display="none";
			break;
		case '3':
			document.getElementById("child01").style.display="block";
			document.getElementById("child02").style.display="block";
			document.getElementById("child03").style.display="block";
			break;
		default :
			document.getElementById("child01").style.display="none";
			document.getElementById("child02").style.display="none";
			document.getElementById("child03").style.display="none";
	}
}

function buttonStatus(status){
	var r=MM_findObj('confirmationButton')
	r.disabled=(status==false)
}

function switchPayment(rb){
try {
	var l=MM_findObj('creditcard'),m=MM_findObj('code'),n=MM_findObj('transfer'),f=document.forms['form1']
	if(rb.value=='transfer') {
		l.style.visibility='hidden'
		l.style.display='none'
		n.style.visibility='visible'
		n.style.display='block'
		f.onsubmit=function(){
			MM_validateForm('FirstName','Nombre','R','LastName','Apellidos','R','e_Mail','EMail','RisEmail','phone','Teléfono','R',
			'Address','Dirección 1','R','City','Ciudad','R','Zip','Código Postal','R','DNI','DNI o Pasaporte','R')
			return document.MM_returnValue
		}
	} else if(rb.value=='parts') {
		l.style.visibility='visible'
		l.style.display='block'
		n.style.visibility='hidden'
		n.style.display='none'
		f.onsubmit=function(){
		MM_validateForm('FirstName','Nombre','R','LastName','Apellidos','R','e_Mail','EMail','RisEmail','phone','Teléfono','R','Address','Dirección','R','City','Ciudad','R','Zip','Código Postal','R','DNI','DNI o Pasaporte','R','card_Holder','Titular de la Tarjeta','R','ccNumber','Número de Tarjeta','R','cvv2','Código de Seguridad','R')
			return document.MM_returnValue
		}
	} else {
		l.style.visibility='hidden'
		l.style.display='none'
		n.style.visibility='hidden'
		n.style.display='none'
		m.style.visibility='hidden'
		m.style.display='none'

		f.onsubmit=function(){
				MM_validateForm('FirstName','Nombre','R','LastName','Apellidos','R','e_Mail','EMail','RisEmail','phone','Teléfono','R','Address','Dirección','R','City','Ciudad','R','Zip','Código Postal','R','DNI','DNI o Pasaporte','R')
			return document.MM_returnValue;
		}
	}
}catch(errr){}
}

function changeDuration(valor,num) {
	try {
	var y_in=valor.substring(0,4);
	var m_in=valor.substring(4,6);
	var d_in=valor.substring(6);	
	var d_out=document.getElementById("day_out").value;
	var m_out=document.getElementById("month_out").value;
	var y_out=document.getElementById("year_out").value;
	var days_sel=document.getElementById("days_"+num);

	var date_in=new Date();
	date_in.setDate(d_in);
	date_in.setMonth(m_in);
	date_in.setFullYear(y_in);
	
	var date_out=new Date();
	date_out.setDate(d_out);
	date_out.setMonth(m_out);
	date_out.setFullYear(y_out);
	
	var one_day=1000*60*60*24;
	var dist=Math.ceil((date_out.getTime()-date_in.getTime())/(one_day));
	
	if(days_sel.options.length>1) {  // Si nomes hi ha un dia que ja no faci res més.
   		days_sel.options.length = 0;
  	 	var contador= 0;
		for (var i = 0; i <= dist; i++) {
			days_sel.options[contador] = new Option((i+1),(i+1));
			contador ++;
		}	
	}	
	}catch(err){}
}

function changeDays(num,val) {
	var valor=document.getElementById("cfrm_date_"+num).value;
	var days_to=document.getElementById("cto_date_"+num).value;	
	var y_in=valor.substring(0,4);
	var m_in=valor.substring(4,6);
	var d_in=valor.substring(6);	

	var date_in=new Date();
	date_in.setFullYear(y_in);
	date_in.setMonth(m_in);
	date_in.setDate((d_in*1));
	
	var n_date=new Date(date_in.getTime() + (val*1)*24*60*60*1000);	
	
	days_to=n_date.getFullYear()+""+(n_date.getMonth()<9?"0"+n_date.getMonth():n_date.getMonth())+""+(n_date.getDate()<9?"0"+n_date.getDate():n_date.getDate());

}


function changeLanguage(lang) {
	document.getElementById("language").value=lang;
	document.forms["frsv01"].submit();
}