jQuery(document).ready(function($)
{
    $(":input").click(function(){
        if ($(this).val() == 'Ingrese' || $(this).val() == 'Area Code' || $(this).val() == 'Enter' || $(this).val() == 'Lada' ) {
               $(this).val("");
           }
    });
    

});
function abrirMenuSuites(valor)
{
    var ajax = objetoAjax();
    var divSuite = document.getElementById('div_suite');
    ajax.open('GET','respuestas_ajax.php?action=get_suite&torre='+valor,true);
    ajax.onreadystatechange=function (){
        if( ajax.readyState==4 ) {
            divSuite.innerHTML = ajax.responseText;
        }
    }
    ajax.send('');
}

function objetoAjax(){
	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 validarCamposObligatorios()
{
    var errores = '';
    $.each($(".obligatorio,.obligatorio2"),function(){
        if ( this.value != '' ) {
            switch(this.value) {
                case 'Ingrese':
                    errores += 'El campo '+quitaCorchetes(this.name)+' es obligatorio \n';
                    break;
                case '-1':
                    errores += 'El campo '+quitaCorchetes(this.name)+' es obligatorio \n';
                    break;
                case 'Lada':
                    errores += 'El campo '+quitaCorchetes(this.name)+' es obligatorio \n';
                    break;
                default:
                    break;
            }
            if ( quitaCorchetes(this.name) == 'total' ) {
               if ( ! ((this.value / this.value) == 1) ) {
                   errores += 'El campo '+quitaCorchetes(this.name)+' debe de ser numerico \n';
               }
            }
        } else {
            errores += 'El campo '+quitaCorchetes(this.name)+' es obligatorio \n';
        }
    });
    if ( errores != '' ) {
        return errores;
    } else {
        return 'los campos son correctos';
    }
}
function quitaCorchetes(campo)
{   
    return campo.substring(5, campo.length-1);
}
function validarTodosLosCampos()
{
    var errores = '';
    if ( ! validarEmail($("#email").val()) ){
        errores += "El E-mail es invalido\n";
    }
    // validar telefono
    if ( ! isPalabraInicial($('#telefono').val()) && !esEntero($('#telefono').val())  ){
        errores += "El Telefono es invalido \n";
    } else {
        if ( $("#telefono").val().length > 8 ){
            errores += "El Tel\xE9fono es invalido \n";
        }
    }

    // validar telefono de la factura
    if( ! isPalabraInicial($("#telefono2").val()) && ! isPalabraInicial($("#campoLlave").val() ) ){
        if ( !(esEntero($("#telefono2").val()) && $("#telefono2").val().length < 9)  ){
             errores += "El Telefono de factura es invalido \n";
        }
    }

    //validar lada
    if ( ! isPalabraInicial($('#lada').val()) && !esEntero($('#lada').val())  ){
        errores += "La Lada es invalida \n";
    } else {
        if ( $("#lada").val().length > 3 ){
            errores += "La lada es invalida \n";
        }
    }

    // validar lada de factura
    if( ! isPalabraInicial($("#lada2").val()) && ! isPalabraInicial($("#campoLlave").val() ) ){
        if ( !(esEntero($("#lada2").val()) && $("#lada2").val().length < 4)  ){
             errores += "La lada de factura es invalido \n";
        }
    }

    // validar codigo postal
    if ( !esEntero($("#cp").val()) || $("#cp").val().length!=5  ) {
        errores += "El C\xf3digo postal es invalido\n";
    }
    // validar codigo postal de factura
    if ( $("#cp2").val() != 'Ingrese' && $("#cp2").val() !='Enter' && !isPalabraInicial($("#campoLlave").val())  ) {
        if ( !esEntero($("#cp2").val()) || $("#cp2").val().length!=5 ){
            errores += "El C\xf3digo postal de la factura es invalido\n";
        }
    }
    if ( $("#campoLlave").val() != 'Ingrese' &&  $("#campoLlave").val() != "" ) {
        $.each($(".dinamico"),function(){
            this.className = 'obligatorio2';
        });
    } else {
        $.each($(".obligatorio2"),function(){
            this.className = 'dinamico';
        });
    }
    var resultadoObligatorio = validarCamposObligatorios();
    if ( ! (resultadoObligatorio == 'los campos son correctos') ) {
        errores += resultadoObligatorio;
    }
    if ( errores == '' ){
        return true;
    } else {
        alert(errores);
        return false;
    }
}
function isPalabraInicial(palabra)
{
    
    switch(palabra){
        case 'Ingrese':return true;break;
        case 'Enter'   :return true;break;
        case 'Lada'    :return true;break;
        case '':return true;
        default:return false;break;
    }
}
function validarCamposTarjeta()
{
    
    var errores = '';

    if ( ! isFechaValida($("#expira_mes").val()+''+$("#expira_year").val() ) ) {
        errores += 'La fecha es incorrecta o esta vacia \n';
    }
    
    // validar numero de tarjeta
    var err_tarjeta=0;
    if ( !esEntero($("#numero_tarjeta_1").val()) || $("#numero_tarjeta_1").val().length !=4  ){
        err_tarjeta = 1;
    }
    if ( !esEntero($("#numero_tarjeta_2").val()) || $("#numero_tarjeta_2").val().length !=4  ){
        err_tarjeta = 1;
    }
    if ( !esEntero($("#numero_tarjeta_3").val()) || $("#numero_tarjeta_3").val().length !=4  ){
        err_tarjeta = 1;
    }
    if ( !esEntero($("#numero_tarjeta_4").val()) || $("#numero_tarjeta_4").val().length !=4  ){
        err_tarjeta = 1;
    }
    if ( err_tarjeta==1){
        errores += "El numero de tarjeta es invalido \n";
    }

    if ( !($("#tipo_tarjeta").val() == 'visa' || $("#tipo_tarjeta").val() == 'master_card' ) ) {
        errores += 'El tipo de tarjeta es invalido \n';
    }
    if ( $("#titular_tarjeta").val() =='Ingrese' || $("#titular_tarjeta").val() =='' ) {
        errores += 'El titular de la tarjeta es obligatorio \n';
    }
    if ( $("#codigo_validacion").val() =='Ingrese' || $("#codigo_validacion").val() =='' || $("#codigo_validacion").val().length != 3 ) {
        errores += 'El codigo de validacion es obligatorio \n';
    } else {
        if(! esEntero($("#codigo_validacion").val())){
            errores += 'El codigo de validacion es invalido \n';
        }
    }
    if ( errores == '' ) {
        return true;
    } else {
        alert(errores);
       return false;
    }
    
}

function pasaSiguiente(actual, next, longitud)
  {
      siguiente = document.getElementById(next);
     if((actual.value.length + 1) == longitud){
         siguiente.focus();
     }

  }
function isFechaValida(fecha)
{
    
    if ( fecha.length<4 ) {
        
        return false;
    }
    var mes = fecha.substring(0,2);
    var year = fecha.substring(3,2);
    
    if (  isEntero(mes) && isEntero(year) ) {
        if ( (mes < 12 && mes > 0) && (year > 0 && year < 99 ) ) {
            return true;
        }else {
            return false;
        }
    } else {
        return false;
    }
}
function validarEmail(valor) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
        return true;
    } else {
        return false;
    }
}
function isEntero(valor){
    if (!isNaN(valor)) {
        return true;
    } else {
       return false;
    }
}
function esEntero(valor)
{
    
    if ( valor/valor == 1 ) {
        return true;
    } else {
        return false;
    }
}
function checkDate(month, day, year)
{
    var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if (!day || !month || !year) {
        return false;
    }
    if (year/4 == parseInt(year/4)) {
        monthLength[1] = 29;
    }
    if (month < 1 || month > 12) {
    return false;
    }
    if (day > monthLength[month-1]) {
        return false;
    }
    return true;
}
function copiarDatos()
{
   $("#direccion2").val($("#direccion").val());
   $("#colonia2").val($("#colonia").val());
   $("#ciudad2").val($("#ciudad").val());
   $("#estado2").val($("#estado").val());
   $("#pais2").val($("#pais").val());
   $("#cp2").val($("#cp").val());
   $("#lada2").val($("#lada").val());
   $("#telefono2").val($("#telefono").val());
}
