
/**************************************************************/
/**************************************************************/
/**************************************************************/

// Contador de caracteres para formularios.
function dcoTextCounter(field, countfield, maxlimit){
	if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
	else countfield.value = maxlimit - field.value.length;
}

// submitamos un formulario
function sendForm(nomObjForm){
    objForm = document.getElementById(nomObjForm);
    if (objForm){
        objForm.submit();
    }
}

function submitPasoAnterior(url) {
    window.location=url;

}

// submitamos datos de usuario
function submitFormDatosUsuario(nomObjForm){
    // Expresión regular para la validación de e-mails.
    var reg_exp = /^[a-z][\w\.-]*@[\w\.-]+\.[a-z]{2,4}$/i
    objForm = document.getElementById(nomObjForm);
    
    if (objForm){
        //Validamos el formulario
        var ok = true;
        var okMsg = '';
        if (objForm.nombre.value=="") {
            okMsg += nombreMal + "\n";
            ok = false;
        }
        if (objForm.apellidos.value=="") {
            okMsg += apellidosMal + "\n";
            ok = false;
        }
        if (objForm.email.value=="") {
            okMsg += emailMal + "\n";
            ok = false;
        } else if (!reg_exp.test( objForm.email.value )){
            okMsg += emailMal + "\n";
            ok = false;
        }
        if (objForm.email2.value=="") {
            okMsg += email2Mal + "\n";
            ok = false;
        } else if (!reg_exp.test( objForm.email2.value )){
            okMsg += email2Mal + "\n";
            ok = false;
        } else if (objForm.email.value!=objForm.email2.value) {
            okMsg += emaildiferente + "\n";
            ok = false;
        }
        if (objForm.telefono.value=="") {
            okMsg += telefonoMal + "\n";
            ok = false;
        }
        if (objForm.adultos.selectedIndex+objForm.nins.selectedIndex+objForm.bebes.selectedIndex==0) {
            okMsg += ocupacionError + "\n";
            ok = false;
        }

        var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; // is the format ok?

        if (tiene_ida  && objForm.pto1.value=="") {
            if (es_aeropuerto) okMsg += aer_origen + "\n";
            else okMsg += pto_origen + "\n";
            ok = false;
        }

        if (es_aeropuerto && tiene_ida  && objForm.pto1_num.value=="") {
            okMsg += numVuelo + "\n";
            ok = false;
        }

        if (es_aeropuerto && tiene_vuelta  && objForm.pto2_num.value=="") {
            okMsg += numVuelo + "\n";
            ok = false;
        }

        if (tiene_vuelta && objForm.pto2.value=="") {
            if (es_aeropuerto) okMsg += aer_destino + "\n";
            else okMsg += pto_destino + "\n";
            ok = false;
        }

        if ((tiene_ida && (objForm.date1.value.match(datePat) == null)) || (tiene_vuelta && (objForm.date2.value.match(datePat) == null))) {
            okMsg += fechaError + "\n";
            ok = false;
        }

        if (objForm.alojamiento.value=="") {
            okMsg += nomAloja + "\n";
            ok = false;
        }

        if (ok) {
            objForm.submit();
        } else {
            alert (okMsg);
        }

    }
}

function submitFormConfirmacionDatosReserva(paso){
    if (paso == '-1') {
        objForm = document.getElementById("anteriorDatosReserva");
        if (objForm){
            objForm.submit();
        }
    } else {
        //Debe estar marcado (o no existir, para agencias ) el checkbox "acepto condiciones"
        objForm1 = document.getElementById("tipopago");
        objForm = document.getElementById("confirmacionDatosReserva");
        if (objForm1!=null && objForm1.paycheck2 && objForm1.paycheck2.checked) { // se ha elegido pago con factura proforma
            objForm.action="mostraFactura.svr";
        }
        if (!objForm.accept || objForm.accept.checked){
            objForm.submit();
        } else {
            alert(mensajeAcepta);
        }
    }
}


function submitBonoReserva(){
     objForm = document.getElementById("bono");
     if (objForm){
         objForm.submit();
     }
}

/* funció que ens serveix per controlar el valor per defecte als camps de tipus text*/
function initField(objField,valorInicial){
    if (objField && objField.value==valorInicial){
        objField.value='';
    }
}

function marcarSelect( objSelect, valor ){
    var marcado = false;
    
    if (objSelect.options){
        for (i = 0 ; i < objSelect.options.length ; i++){
            if (objSelect.options[i].value == valor){
                objSelect.options[i].selected = true;
                marcado = true;
            }else{
                objSelect.options[i].selected = false;
            }
        }
    }
    return marcado;
}

/* función que marca un select con el valor que se aproxime más al parámetro valor */
function marcarSelectMasProximo( objSelect, valor ){
    var indice=0, dif=9999999;
    
    if (objSelect.options){
        for (i = 0 ; i < objSelect.options.length ; i++){
            var selValue = parseInt(objSelect.options[i].value,10);
            if( Math.abs( selValue - valor ) < dif ){
                dif = Math.abs( selValue - valor );
                indice = i;
            }
            objSelect.options[i].selected = false;                    
        }
    }
    objSelect.options[indice].selected = true;    
    return objSelect.options[indice].value;
}

//	MM Genéricas ----------

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/** Heredado la "end" */
function mostraTexte(url){
    open(url,"","screenX=10,screenY=150,scrollbars=yes,width=626,height=530,alwaysRaised=yes");
}

// Intercambia el valor style.display de un objeto entre "block" y "none".
function switchDisplay( id ){    
    var obj = document.getElementById( id );    
    obj.style.display = obj.style.display == "block" ? "none" : "block";
}