Javascript
Fri Aug 08 2025 07:42:30 GMT+0000 (Coordinated Universal Time)
Saved by @andresrivera #ampscript
/*Refrescador de pagina por seguridad*/
setTimeout(securityReload, 3600000);
function securityReload() {
location.reload();
}
/*Muestra los mensajes de error del formulario*/
function showError(inputError, subError) {
stopSending();
switch (inputError) {
case 2:
document.getElementById('documentGroup').classList.add("endesa-form__group--invalid");
switch (subError) {
case 1:
document.getElementById('documentError').innerHTML = nifErrorMessage;
break;
case 2:
document.getElementById('documentError').innerHTML = nieErrorMessage;
break;
case 3:
document.getElementById('documentError').innerHTML = cifErrorMessage;
break;
case 4:
document.getElementById('documentError').innerHTML = passaportErrorMessage;
break;
default:
document.getElementById('documentError').innerHTML = documentErrorMessage;
}
break;
default:
document.getElementById('documentGroup').classList.add("endesa-form__group--invalid");
document.getElementById('documentError').innerHTML = documentErrorMessage;
}
}
/*Oculta Mensajes de Error del formulario*/
function hiddenErrors(inputError) {
switch (inputError) {
case 2:
document.getElementById('documentGroup').classList.remove("endesa-form__group--invalid");
document.getElementById('documentError').innerHTML = "";
document.getElementById('documentSupraGroup').classList.remove("endesa-form__group--invalid");
document.getElementById('documentSupraError').innerHTML = "";
break;
default:
document.getElementById('documentGroup').classList.remove("endesa-form__group--invalid");
document.getElementById('documentError').innerHTML = "";
document.getElementById('documentSupraGroup').classList.remove("endesa-form__group--invalid");
document.getElementById('documentSupraError').innerHTML = "";
}
};
function changeDocument() {
validDocument();
validFormat();
}
/*
* Document validation
*/
function validDocument() {
var select = document.getElementById("documentType");
var opc = select.options[select.selectedIndex].value;
var userDocument = document.getElementById('document').value;
userDocument = userDocument.replace('-','');
userDocument = userDocument.replace('-','');
document.getElementById("documentHidden").value = userDocument;
switch (opc) {
case "nif":
if (!validateDNI(userDocument)) {
showError(2, 1);
lockButton();
return false
} else {
hiddenErrors(2);
return true
}
break;
case "nie":
if (!validateNIE(userDocument)) {
showError(2, 2);
lockButton();
return false
} else {
hiddenErrors(2);
return true
}
break;
case "cif":
if (!validateCIF(userDocument)) {
showError(2, 3);
lockButton();
return false
} else {
hiddenErrors(2);
return true
}
break;
case "pasaporte":
if (!validatePASSAPORT(userDocument)) {
showError(2, 4);
lockButton();
return false
} else {
hiddenErrors(2);
return true
}
break;
default:
showError(2);
lockButton();
return false
};
};
function validateDNI(dni) {
if (dni.length == 9) {
var letras = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z',
'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E', 'T'
];
var numero = dni.substring(0, 8);
var letra = dni.substring(8, 9);
letra = letra.toUpperCase();
if (numero < 0 || numero > 99999999) {
return false;
} else {
var letraCalculada = letras[numero % 23];
if (letraCalculada != letra) {
return false;
} else {
return true;
}
}
} else {
return false;
}
};
function validateNIE(nie) {
nie = nie.toUpperCase();
// Basic format test
if (!nie.match(
'((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)')) {
return false;
}
// Test NIE
//T
if (/^[T]{1}/.test(nie)) {
return (nie[8] === /^[T]{1}[A-Z0-9]{8}$/.test(nie));
}
//XYZ
if (/^[XYZ]{1}/.test(nie)) {
return (
nie[8] === "TRWAGMYFPDXBNJZSQVHLCKE".charAt(
nie.replace('X', '0')
.replace('Y', '1')
.replace('Z', '2')
.substring(0, 8) % 23));
}
return false;
}
function validateCIF(cif) {
var sum, num = [],
value, controlDigit, validar, result;
var valueCif = cif.substr(1, cif.length - 2);
var suma = 0;
value = cif.toUpperCase();
for (var i = 1; i < valueCif.length; i = i + 2) {
suma = suma + parseInt(valueCif.substr(i, 1));
}
for (var i = 0; i < 9; i++) {
num[i] = parseInt(cif.charAt(i), 10);
}
var suma2 = 0;
for (var i = 0; i < valueCif.length; i = i + 2) {
result = parseInt(valueCif.substr(i, 1)) * 2;
if (String(result).length == 1) {
suma2 = suma2 + parseInt(result);
} else {
suma2 = suma2 + parseInt(String(result).substr(0, 1)) + parseInt(String(result)
.substr(1, 1));
}
}
suma = suma + suma2;
var unidad = String(suma).substr(1, 1);
unidad = 10 - parseInt(unidad);
var primerCaracter = cif.substr(0, 1).toUpperCase();
if (primerCaracter.match(/^[FJKNPQRSUVW]$/)) {
suma += '';
controlDigit = 10 - parseInt(suma.charAt(suma.length - 1), 10);
value += controlDigit;
validar = num[8].toString() === String.fromCharCode(64 + controlDigit) || num[8]
.toString() === value.charAt(value.length - 1);
if (validar == true) return true;
if (String.fromCharCode(64 + unidad).toUpperCase() == cif.substr(cif.length - 1, 1)
.toUpperCase()) return true;
}
if (primerCaracter.match(/^[ABCDEFGHLM]$/)) {
// Se revisa que el ultimo valor coincida con el calculo
if (unidad == 10) unidad = 0;
suma += '';
controlDigit = 10 - parseInt(suma.charAt(suma.length - 1), 10);
value += controlDigit;
validar = num[8].toString() === String.fromCharCode(64 + controlDigit) || num[8]
.toString() === value.charAt(value.length - 1);
if (validar == true) return true;
if (String.fromCharCode(64 + unidad).toUpperCase() == cif.substr(cif.length - 1, 1)
.toUpperCase()) return true;
}
return false;
};
function validatePASSAPORT(passport) {
"use strict";
return passport.length > 6 && passport.length < 18;
}
function unlockButton() {
var btnSubmit = document.getElementById('btnSubmit');
btnSubmit.removeAttribute("disabled");
btnSubmit.classList.remove("endesa-form__btn--disable");
}
function lockButton() {
var btnSubmit = document.getElementById('btnSubmit');
btnSubmit.setAttribute("disabled", true);
btnSubmit.classList.add("endesa-form__btn--disable");
}
function validFormat() {
if (validDocument()) {
unlockButton();
console.log("Formulario Valido");
return true;
} else {
lockButton();
console.log("Error en el formulario");
return false;
}
}
/*Detecta si se han cambiado un check*/
function changeInput(input) {
/*Recupera todos los inputs*/
var documentInput = document.getElementById('document');
if (documentInput.value) {
validFormat();
} else {
switch (input) {
case 2:
validDocument();
console.log("Con Documento");
break;
default:
lockButton();
console.log("Sin datos");
}
}
}
function stopSending() {
var form = document.getElementById('SCForm');
unlockButton();
form.classList.add("no-spinner");
form.classList.remove("endesa-form--sending");
}
function ajaxPass() {
var ajaxResponse;
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
stopSending()
ajaxResponse = this.responseText;
ajaxResponse = String(ajaxResponse)
if (ajaxResponse.indexOf('https') >= 0) {
window.location.href = ajaxResponse
} else if (idioma == 'ES') {
document.getElementById('error-container').innerHTML = `
<div class="error-banner">
<p class="error-text">
El documento introducido no corresponde con el titular del contrato
</p>
</div>
`;
} else if (idioma == 'EN') {
document.getElementById('error-container').innerHTML = `
<div class="error-banner">
<p class="error-text">
The document submitted does not correspond to the contract holder
</p>
</div>
`;
} else if (idioma == 'CA') {
document.getElementById('error-container').innerHTML = `
<div class="error-banner">
<p class="error-text">
El document introduït no correspon amb el titular del contracte
</p>
</div>
`;
}
}
}
var paramsFormsToSent = { 'Documento': document.getElementById('document').value,
'encDocumento': encDocumento,
'encFactura': encFactura
}
console.log(paramsFormsToSent);
var ajaxUrl = "https://cloud.dev.notificaciones.endesaclientes.com/ajax-validacion-doc-aviso"
ajax.open("POST", ajaxUrl, "true");
ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.send("data=" + encodeURIComponent(JSON.stringify(paramsFormsToSent)));;
}
function visualSending() {
if (validFormat()) {
if (validFormat()) {
lockButton();
document.getElementById('SCForm').classList.add("endesa-form--sending");
document.getElementById('SCForm').classList.remove("no-spinner");
ajaxPass();
} else {
stopSending();
}
}
}



Comments