// JavaScript Document
function nuevoAjax()
{ 
 var xmlhttp=false; 
 try 
 { 
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
 }
 catch(e)
 { 
  try
  { 
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
  } 
  catch(E) { xmlhttp=false; }
 }
 if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 
 
 return xmlhttp; 
}

function carga_localidad(seccion){
ajax = nuevoAjax();
var indice = document.getElementById("provincia").options[document.getElementById("provincia").selectedIndex].value;

strPos = "id="+indice+"&seccion="+seccion;
ajax.open("POST", "http://www.mercatour.com/inc/ajax.php?func=1",true);
ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajax.onreadystatechange=function() {
	if (ajax.readyState==1){
		document.getElementById("celda_localidad").innerHTML="<select class='selbuscador' disabled><option value=''>Cargando...</option></select>";
	} 
	if (ajax.readyState==4){
			document.getElementById("celda_localidad").innerHTML=ajax.responseText;
	}
}
ajax.send(strPos);
return;		
}


function carga_localidad_all(){
ajax = nuevoAjax();
var indice = document.getElementById("provincia").options[document.getElementById("provincia").selectedIndex].value;

strPos = "id="+indice;
ajax.open("POST", "http://www.mercatour.com/inc/ajax.php?func=2",true);
ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajax.onreadystatechange=function() {
	if (ajax.readyState==1){
		document.getElementById("celda_localidad").innerHTML="<select disabled><option value=''>Cargando...</option></select>";
	} 
	if (ajax.readyState==4){
			document.getElementById("celda_localidad").innerHTML=ajax.responseText;
	}
}
ajax.send(strPos);
return;		
}

function enviaruta(){
   var origen = document.getElementById("dirorigen").value;
   var destino = document.getElementById("dirdestino").value
   if(origen =="" || destino ==""){
   		alert("Debe introducir origen y destino");
   }else{
   		origen = escape(origen);
		destino = escape(destino);
		var dirmapagoogle="http://maps.google.es/maps?f=d&saddr="+origen+"&daddr="+destino;
		window.location = dirmapagoogle;	
   } 
}

function vercalendario(capa){
	var capa2 = "calentrada";
	if(capa == "calentrada"){
		capa2 = "calsalida";
	}
	document.getElementById(capa2).style.display = "none";
	if(document.getElementById(capa).style.display == "block"){
		document.getElementById(capa).style.display = "none";
	}else{
		document.getElementById(capa).style.display = "block";
	}
}

function Comparar_Fecha(Obj1,Obj2){
String1 = Obj1;
String2 = Obj2;
// Si los dias y los meses llegan con un valor menor que 10 
// Se concatena un 0 a cada valor dentro del string 
if (String1.substring(1,2)=="/") {
String1="0"+String1
}
if (String1.substring(4,5)=="/"){
String1=String1.substring(0,3)+"0"+String1.substring(3,9)
}

if (String2.substring(1,2)=="/") {
String2="0"+String2
}
if (String2.substring(4,5)=="/"){
String2=String2.substring(0,3)+"0"+String2.substring(3,9)
}

dia1=String1.substring(0,2);
mes1=String1.substring(3,5);
anyo1=String1.substring(6,10);
dia2=String2.substring(0,2);
mes2=String2.substring(3,5);
anyo2=String2.substring(6,10);


if (dia1 == "08") // parseInt("08") == 10 base octogonal
dia1 = "8";
if (dia1 == '09') // parseInt("09") == 11 base octogonal
dia1 = "9";
if (mes1 == "08") // parseInt("08") == 10 base octogonal
mes1 = "8";
if (mes1 == "09") // parseInt("09") == 11 base octogonal
mes1 = "9";
if (dia2 == "08") // parseInt("08") == 10 base octogonal
dia2 = "8";
if (dia2 == '09') // parseInt("09") == 11 base octogonal
dia2 = "9";
if (mes2 == "08") // parseInt("08") == 10 base octogonal
mes2 = "8";
if (mes2 == "09") // parseInt("09") == 11 base octogonal
mes2 = "9";

dia1=parseInt(dia1);
dia2=parseInt(dia2);
mes1=parseInt(mes1);
mes2=parseInt(mes2);
anyo1=parseInt(anyo1);
anyo2=parseInt(anyo2);

if (anyo1>anyo2)
{
return false;
}

if ((anyo1==anyo2) && (mes1>mes2))
{
return false;
}
if ((anyo1==anyo2) && (mes1==mes2) && (dia1>=dia2))
{
return false;
} 

return true;
}

function emailvalidation(email){
	apos=email.indexOf("@");
	dotpos=email.lastIndexOf(".");
	lastpos=email.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)		
		return false;
	else 
		return true;
}

function validareserva(){
	if(document.getElementById("nombre").value == ""){
		alert("Debe introducir un nombre");
		return false;
	} 
	
	if(!emailvalidation(document.getElementById("email").value)){
		alert("Debe introducir un email valido");
		return false;
	} 
	
	if(document.getElementById("telefono").value == ""){
		alert("Debe introducir un telefono");
		return false;
	} 
	
	if(document.getElementById("adultos").value == ""){
		alert("Debe introducir el número de adultos");
		return false;
	}else{
		if(isNaN(document.getElementById("adultos").value))	{
			alert("El campo adultos debe ser un numero");
			return false;			
		}
	}
	
	if(document.getElementById("ninos").value != ""){
		if(isNaN(document.getElementById("ninos").value))	{
			alert("El campo niños debe ser un numero");
			return false;			
		}	
	}
	
	if(document.getElementById("fentrada").value == "dd/mm/aaaa" || document.getElementById("fentrada").value == ""){
		alert("Debe introducir una fecha de entrada");
		return false;
	}
	if(document.getElementById("fsalida").value == "dd/mm/aaaa" || document.getElementById("fsalida").value == ""){
		alert("Debe introducir una fecha de salida");
		return false;
	}
	
	if (!Comparar_Fecha(document.getElementById("fentrada").value, document.getElementById("fsalida").value)){
		alert("La fecha de salida debe ser mayor que la fecha de entrada.");
		return false;
	}	
}

function vercalendar(capa){
	if(document.getElementById(capa).style.display == "block"){
		document.getElementById(capa).style.display = "none";
	}else{
		document.getElementById(capa).style.display = "block";
	}
}

function selstar(puntos,idtipo,titulo) {
	marca = puntos * 16;
	selrate = "selrate"+idtipo;
	valora = "valora"+idtipo;
	txtvalora = "txtvalora"+idtipo;
	document.getElementById(selrate).style.width = marca+"px";
	document.getElementById(valora).value = puntos;
	document.getElementById(txtvalora).innerHTML = titulo;
}

function validacomentario(){
	for(i=1;i<=10;i++){
		var campo = "valora"+i;
		if(document.getElementById(campo).value ==""){
			alert("Debe valorar todas las secciones");
			return false;
		}
	}
	
	if(document.getElementById("nombre").value == ""){
		alert("Debe introducir un nombre");
		return false;
	} 
	
	
	if(!emailvalidation(document.getElementById("email").value)){
		alert("Debe introducir un email valido");
		return false;
	} 	
		
	if(document.getElementById("valoraentrada").value == "dd/mm/aaaa" || document.getElementById("valoraentrada").value == ""){
		alert("Debe introducir una fecha de entrada");
		return false;
	}

}

function verocupacion(id){
	var url = "http://www.mercatour.com/calocupacion.php?idAlojamiento="+id;
	window.open(url,"ocupacion","width=610,height=200,scrollbars=NO");
	
}

function validaalta(form){
	error=false;
	txterror="Corriga los siguientes campos obligatorios:\n";
	var flag=false;
	
	for(var i=0;i<form.Ptipo.length;i++){
		if(form.Ptipo.item(i).checked){
			flag = true;
		}
	}
	
	if (!flag){
		error=true;
		txterror+='* Debe seleccionar si es Particular o Agencia\n';
	}
	
	if(form.PNombre.value==""){
		error=true;
		txterror+="* Nombre del propietario o empresa\n";
	}
	if(form.PDNI.value==""){
		error=true;
		txterror+="* DNI / CIF\n";
	}
	if(form.PDireccion.value==""){
		error=true;
		txterror+="* Direccion\n";
	}
	if(form.PCpostal.value==""){
		error=true;
		txterror+="* Codigo Postal\n";
	}
	if(form.localidad.value=="0"){
		error=true;
		txterror+="* Localidad\n";
	}
	if(form.PTfno1.value==""){
		error=true;
		txterror+="* Telefono\n";
	}
	if(form.PEmail.value==""){
		error=true;
		txterror+="* Email\n";
	}else{
		if(!emailvalidation(form.PEmail.value)){
			error=true;
			txterror+="* Introduzca un Email valido\n";	
		}
	}
	
	if(form.password.value==""){
		error=true;
		txterror+="* Contraseña\n";
	}else{
		if(form.password.value.length < 5){
			error=true;
			txterror+='* El campo contraseña debe tener 5 caracteres como minimo\n';	
		}				
	}
	if(form.password2.value==""){
		error=true;
		txterror+="* Repita contraseña\n";
	}
	if(form.password.value!=form.password2.value){
		error=true;
		txterror+="* Las contraseñas no coinciden\n";
	}
	
	if(!form.notalegal.checked){
		error=true;
		txterror+="* Debe aceptar la nota legal\n";		
	}

	if(error){
		alert(txterror);
		return false;
	}else{
		return true;	
	}
}

function guardaboletin(){
	if(document.getElementById("emailboletin").value=="" || !emailvalidation(document.getElementById("emailboletin").value)){
		alert("Debe introducir un email valido");
		return;
	}
	datos = "email="+document.getElementById("emailboletin").value;
	ajax = nuevoAjax();
	ajax.open("POST","inc/guardaboletin.php",true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.onreadystatechange=function(){
		if (ajax.readyState==1){
			document.getElementById("boletin").innerHTML="<div align='center'><br /><br /><strong>Enviando datos...</strong></div>";
		} 
		if (ajax.readyState==4){
			document.getElementById("boletin").innerHTML=ajax.responseText;
		}
	}
	ajax.send(datos);
	return;		
}









