function enviarRegistro(){
    bool = false; 
    if(document.getElementById('nombre').value == '' || document.getElementById('nombre').value == 'Escriba su nombre') {
        bool = false;
        alert('Nombre obligatorio');
        document.getElementById('nombre').focus();
    }
    else{
        bool = true;
    }
    if(bool){
        if(document.getElementById('apellidos').value == '' || document.getElementById('apellidos').value == 'Escriba sus apellidos') {
            bool = false;
            alert('Apellidos obligatorios');
            document.getElementById('apellidos').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('email').value == '' || document.getElementById('email').value == 'Escriba su dirección email') {
            bool = false;
            alert('Email obligatorio');
            document.getElementById('email').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('seguridad').value == '' || document.getElementById('seguridad').value == 'Escriba lo que ve en la imagen') {
            bool = false;
            alert('Código de verificación obligatorio');
            document.getElementById('seguridad').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        document.getElementById('frmdatos').submit();
    }   
}
function enviarContacto(){
    bool = false; 
    if(document.getElementById('nombre').value == '' || document.getElementById('nombre').value == 'Escriba su nombre') {
        bool = false;
        alert('Nombre obligatorio');
        document.getElementById('nombre').focus();
    }
    else{
        bool = true;
    }
    if(bool){
        if(document.getElementById('apellidos').value == '' || document.getElementById('apellidos').value == 'Escriba sus apellidos') {
            bool = false;
            alert('Apellidos obligatorios');
            document.getElementById('apellidos').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('email').value == '' || document.getElementById('email').value == 'Escriba su dirección email') {
            bool = false;
            alert('Email obligatorio');
            document.getElementById('email').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('comentario').value == '' || document.getElementById('comentario').value == 'Escriba su comentario, dudas, etc') {
            bool = false;
            alert('Comentario obligatorio');
            document.getElementById('comentario').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('seguridad').value == '' || document.getElementById('seguridad').value == 'Escriba lo que ve en la imagen') {
            bool = false;
            alert('Código de verificación obligatorio');
            document.getElementById('seguridad').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        document.getElementById('frmdatos').submit();
    }   
}
//para la calculadora
function replaceChars(cadena,val1,val2) {
    out = val1; // reemplazar la letra a
    add = val2; // por la letra z
    temp = "" + cadena;
    while (temp.indexOf(out)>-1) {
        pos= temp.indexOf(out);
        temp = "" + (temp.substring(0, pos) + add + 
        temp.substring((pos + out.length), temp.length));
    }
    return temp;
}
function calculocapitalc(capital,interes,duracion){
    inter = interes/(100*12);
    capitalc = capital * Math.pow((1 + inter),duracion);
    return capitalc; 
}
function enviarHipotecaDuracion(){
    capital = replaceChars(document.getElementById('capital1').value,',','.');
    interes = replaceChars(document.getElementById('interes1').value,',','.');
    cuota = replaceChars(document.getElementById('cuota1').value,',','.');
    objeto = 'mifrase1';
    if((!isNaN(capital)) && (capital!="") && (capital > 0)){
        if((!isNaN(interes)) && (interes!="") && (interes > 0)){
            if((!isNaN(cuota)) && (cuota!="") && (cuota > 0)){
                inter = interes/(100*12);
                if((cuota - capital * inter) > 0){
                    periodos = Math.ceil((Math.log(cuota) - Math.log(cuota - capital * inter)) / Math.log(1 + inter));
                    ano = periodos / 12;
                    ano = Math.round(ano*1); 
                    mes = periodos % 12;
                    document.getElementById('duracion1').value = '';
                    if(mes > 0){
                        objDaughter=document.getElementById(objeto);
                        if (objDaughter.style.display=='none'){
                            objDaughter.style.display='block';
                        }
                        else {
                            if(objDaughter.style.display=='block'){
                                objDaughter.style.display='none'; 
                            } 
                        } 
                        periodos = 12 * ano;
                        cuota  = (capital * inter * Math.pow((1 + inter),periodos)) / (Math.pow((1 + inter),periodos) - 1);
                        cuota = Math.round(cuota * 100) / 100;
                        document.getElementById('cuota1').value = cuota;
                    }
                    document.getElementById('duracion1').value = ano;
                    reembolso = calculocapitalc(capital,interes,ano);  
                    reembolso = Math.round(reembolso*100)/100; 
                    document.getElementById('reembolsar').value = reembolso;
                }
                else{
                    alert("Con la cuota introducida no puede pagar los intereses");
                    document.getElementById('cuota1').focus();;
                }
            }
        }
    }
}
function enviarHipotecaCuota(){
    importe = replaceChars(document.getElementById('importe2').value,',','.');
    gastos = replaceChars(document.getElementById('gastos2').value,',','.');
    ahorros = replaceChars(document.getElementById('ahorros2').value,',','.');
    capital = (parseInt(100) + parseInt(gastos))/100 * importe - parseInt(ahorros);
    interes = replaceChars(document.getElementById('interes2').value,',','.');
    duracion = replaceChars(document.getElementById('duracion2').value,',','.');
    cuotab = Math.round((parseInt(gastos)/100 * importe)*100)/100;
    
    
    if((!isNaN(capital)) && (capital!="") && (capital > 0)){
        if((!isNaN(interes)) && (interes!="") && (interes > 0)){
            if((!isNaN(duracion)) && (duracion!="") && (duracion > 0)){
                inter = interes/(100*12);
                periodos = 12 * duracion;
                cuota  = (capital * inter * Math.pow((1 + inter),periodos)) / (Math.pow((1 + inter),periodos) - 1);
                cuota = Math.round(cuota * 100) / 100;
                //reembolso = calculocapitalc(capital,interes,duracion);  
                //reembolso = Math.round(reembolso*100)/100;  
                document.getElementById('cuota2').value = Math.round(cuota);
                document.getElementById('cuota2a').value = Math.round(capital*100)/100;
                document.getElementById('cuota2b').value = cuotab;
                
            }
        }
    }
}
function enviarHipotecaCapital(duracion,interes,cuota){
    duracion = replaceChars(document.getElementById('duracion3').value,',','.');
    interes = replaceChars(document.getElementById('interes3').value,',','.');
    cuota = replaceChars(document.getElementById('cuota3').value,',','.');
    ahorros = replaceChars(document.getElementById('ahorros3').value,',','.');
    gastos = replaceChars(document.getElementById('gastos3').value,',','.');
    
    if((!isNaN(duracion)) && (duracion!="") && (duracion > 0)){
        if((!isNaN(interes)) && (interes!="") && (interes > 0)){
            if((!isNaN(cuota)) && (cuota!="") && (cuota > 0)){
                inter = interes/(100*12);
                periodos = 12 * duracion;
                capital = (cuota * (Math.pow((1 + inter),periodos) - 1) / (inter * Math.pow((1 + inter),periodos)));
                capital = Math.round(capital * 100) / 100;
                
                //alert(parseInt(capital) + parseInt(ahorros));
                
                //quitamos el % de hipoteca y le sumamos los ahorros
                capitalb = Math.round((parseInt(capital) + parseInt(ahorros)) / (parseInt(1) + parseInt(gastos)/100) );
                //reembolso = calculocapitalc(capital,interes,duracion); ;  
                //reembolso = Math.round(reembolso*100)/100;
                document.getElementById('capital3').value = capitalb;
            }
        }
    }
}
//fin calculadora
function cambiarCadenaAmigable(url){
    //Remplazamos caracteres especiales latinos
    var find = new Array(12);
    var resultado = '';
    //cadena.replace(/perro/g, "gato"); 
    find[0] = 'á';
    find[1] = 'é';
    find[2] = 'í';
    find[3] = 'ó';
    find[4] = 'ú';
    find[5] = 'ñ';
    find[6] = 'Á';
    find[7] = 'É';
    find[8] = 'Í';
    find[9] = 'Ó';
    find[10] = 'Ú';
    find[11] = 'Ñ';
    var repl = new Array(12);
    repl[0] = 'a';
    repl[1] = 'e';
    repl[2] = 'i';
    repl[3] = 'o';
    repl[4] = 'u';
    repl[5] = 'ñ';
    repl[6] = 'A';
    repl[7] = 'E';
    repl[8] = 'I';
    repl[9] = 'O';
    repl[10] = 'U';
    repl[11] = 'Ñ';
    for(var i=0;i<find.length;i++){
        do{
            url = url.replace(find[i],repl[i]);
        }while(url.indexOf(find[i]) >= 0);
    }
    //pasamos a minúsculas
    url = url.toLowerCase();
    //añadimos los guiones
    var find = new Array(5);
    find[0] = ' ';
    find[1] = '&';
    find[2] = '\r\n';
    find[3] = '\n';
    find[4] = '+';
    for(var i=0;i<find.length;i++){
        do{
            url = url.replace(find[i],'-');
        }while(url.indexOf(find[i]) >= 0);
    }
    //Eliminamos y Reemplazamos demás caracteres especiales
    var find = new Array(3);
    find[0] = '/[^a-z0-9\-<>]/';
    find[1] = '/[\-]+/';
    find[2] = '/<[^>]*>/';
    var repl = new Array(3);
    repl[0] = '';
    repl[1] = '-';
    repl[2] = '';
    for(var i=0;i<find.length;i++){
        do{
            url = url.replace(find[i],repl[i]);
        }while(url.indexOf(find[i]) >= 0);
    }
    //alert(url);
    return url;
}
function imprimirContenido(){
    $(function(){
        $.jPrintArea('#centro');
    });
}
function enviarFicha(){
    bool = false; 
    if(document.getElementById('nombre').value == '' || document.getElementById('nombre').value == 'Escriba su nombre') {
        bool = false;
        alert('Nombre obligatorio');
        document.getElementById('nombre').focus();
    }
    else{
        bool = true;
    }
    if(bool){
        if(document.getElementById('telf').value == '' || document.getElementById('telf').value == 'Escriba su nº de teléfono') {
            bool = false;
            alert('Teléfono obligatorio');
            document.getElementById('telf').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('email').value == '' || document.getElementById('email').value == 'Escriba su e-mail') {
            bool = false;
            alert('Email obligatorio');
            document.getElementById('email').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('comentario').value == '' || document.getElementById('comentario').value == 'Escriba su comentario, duda, etc') {
            bool = false;
            alert('Comentario obligatorio');
            document.getElementById('comentario').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        if(document.getElementById('seguridad').value == '' || document.getElementById('seguridad').value == 'Escriba lo que ve en la imagen') {
            bool = false;
            alert('Código de verificación obligatorio');
            document.getElementById('seguridad').focus();
        }
        else{
            bool = true;
        }
    }
    if(bool){
        document.getElementById('frmdatos').submit();
    }   
}
