//variables para sacar en alertas

var exportara = "Exportará";
var segmentacion = "segmentación";
var segmentacionMay = "Segmentación";
var num_reg_exportables = 'Número máximo de registrso exportables';
var vacio = "vacío";
var Poblacion = "Población";
var Telefono = "Teléfono";
var direccion = "Dirección";
var numero = "número";
var telef = "teléfono";
var tenian = "tenían";
var estan = "están";
var aniadalos = "añádalos";
var busqueda = "búsqueda";
var direc = "dirección";
var ningun = "ningún";
var geograficos = "Geográficos";

var cadenaNumeros = "1234567890";
var errorCampoObligatorioVacio = "El campo NOMBRE_CAMPO no puede estar vacio";
var cadenaAlfabetoASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var errorSoloNumerosNIF = "El campo NOMBRE_CAMPO sólo admite números en sus 8 primeras posiciones";
var errorSoloLetrasNIF = "El campo NOMBRE_CAMPO sólo admite letras sin acentos ni signos en su última posición";
var errorValidacionNIF = "El campo NOMBRE_CAMPO no contiene un NIF válido";
var errorCaracteresNoAdmitidosCIF = "El campo NOMBRE_CAMPO no contiene un CIF válido";
var errorValidacionCIF = "El campo NOMBRE_CAMPO no contiene un CIF válido";
var errorNoAdmiteEspacios = "El campo NOMBRE_CAMPO no admite espacios";
var errorNoAlcanzaLongitudMinima = "El campo NOMBRE_CAMPO ocupa LONGITUD_CAMPO posiciones y no alcanza la longitud mínima, establecida en LONGITUD_CORRECTA posiciones";
var errorExcedeLongitudMaxima = "El campo NOMBRE_CAMPO ocupa LONGITUD_CAMPO posiciones y excede la longitud máxima, establecida en LONGITUD_CORRECTA posiciones";
var cambioLogin = "El cambio de E-Mail implica el cambio de Login.\nRecibirá un mail con el nuevo login.\nA continuación se desconectará.\nDesea continuar?";

function ayuda(popup, xx, yy) {
	var w = screen.width;
	var h = screen.height;
	// var xx = 500; yy = 300;
	var x = (w / 2) - (xx / 2);
	var y = (h / 2) - (yy / 2) - 75;
	var pp = window.open('/templates/html/Duns50M/' + popup + '.html', popup, 'resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,width=' + xx + ',height=' + yy + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + '');
	pp.focus();
}
function popUp_ayuda(popup, xx, yy) {
	var w = screen.width;
	var h = screen.height;
	// var xx = 500; yy = 300;
	var x = (w / 2) - (xx / 2);
	var y = (h / 2) - (yy / 2) - 75;
	// alert('/prod/SHOW_PAGE_NO_FRAMES/pagina/'+popup+"--"+'resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,width='+xx+',height='+yy+',screenX='+x+',screenY='+y+',left='+x+',top='+y+'');
	var pp = window.open(popup, 'ayuda', 'resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,width=' + xx + ',height=' + yy + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + '');
	// pp.focus();
}

function trim(str) {
	while ('' + str.charAt(0) == ' ')
		str = str.substring(1, str.length);
	while ('' + str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);
	return str;
}
function validarEntero(valor) {
	if (valor == 0) {
		return valor;
	} else {
		// intento convertir a entero.
		// si era un entero no le afecta, si no lo era lo intenta convertir
		valor = parseInt(valor);

		// Compruebo si es un valor numérico
		if (isNaN(valor)) {
			// entonces (no es numero) devuelvo el valor cadena vacia
			return "";
		} else {
			// En caso contrario (Si era un número) devuelvo el valor
			return valor;
		}
	}
}

function limpiarCIFNIF(obj) {
	var retorno = '';
	var i = 0;
	for (i = 0; i < obj.length; i++) {
		if ((obj.charAt(i) != ' ') && (obj.charAt(i) != '-') && (obj.charAt(i) != '.') && (obj.charAt(i) != '/')) {
			retorno += obj.charAt(i);
		}
	}
	return retorno.toUpperCase();
}

function limpiarTelefono(telefono) {
	var telf = telefono.value;
	var telefonoLimpio = "";
	for ( var i = 0; i < telf.length; i++) {
		var ch = telf.substring(i, i + 1);
		if (validarEntero(ch) != "" && ch != " ") {
			telefonoLimpio = "" + telefonoLimpio + ch;
		}
	}
	telefono.value = telefonoLimpio;
	return telefonoLimpio;
}

function comprobarNumeroTelf(telefono) {

	var telf = telefono;
	if (validarEntero(telf) == "")
		return false;
	if (telf.length < 9)
		return false;
	for ( var i = 0; i < telf.length; i++) {
		var ch = telf.substring(i, i + 1);
		if (ch < "0" || ch > "9") {
			return false;
		}
		if (i == 0 && telf.length == 9) {
			if (ch != "6" && ch != "9") {
				return false;
			}
		}
	}
	return true;
}

function comprobarEmail2(email) {
	email = email.toLowerCase();
	if (email.indexOf('@', 0) == -1 || email.indexOf('.', 0) == -1 || email.indexOf(' ', 0) != -1 || email.indexOf('/', 0) != -1 || email.indexOf(';', 0) != -1 || email.indexOf('<', 0) != -1 || email.indexOf('>', 0) != -1 || email.indexOf('*', 0) != -1 || email.indexOf('|', 0) != -1 || email.indexOf('`', 0) != -1 || email.indexOf('&', 0) != -1 || email.indexOf('$', 0) != -1 || email.indexOf('!', 0) != -1 || email.indexOf('"', 0) != -1 || email.indexOf('¡', 0) != -1 || email.indexOf('%', 0) != -1 || email.indexOf('#', 0) != -1 || email.indexOf('(', 0) != -1 || email.indexOf('(', 0) != -1 || email.indexOf('=', 0) != -1 || email.indexOf('ñ', 0) != -1 || email.indexOf('Ñ', 0) != -1 || email.indexOf('á', 0) != -1 || email.indexOf('à', 0) != -1 || email.indexOf('é', 0) != -1
			|| email.indexOf('è', 0) != -1 || email.indexOf('í', 0) != -1 || email.indexOf('ì', 0) != -1 || email.indexOf('ó', 0) != -1 || email.indexOf('ò', 0) != -1 || email.indexOf('ú', 0) != -1 || email.indexOf('ù', 0) != -1 || email.indexOf(':', 0) != -1 || email.indexOf(';', 0) != -1 || email.indexOf('..', 0) != -1 || email.indexOf('@@', 0) != -1 || email.indexOf("asdf") != -1 || email.indexOf('@', 0) == 0 || email.indexOf("xxx@xxx.xx") != -1 || email.indexOf(',', 0) != -1) {
		// alert("Email Invalido");
		return false;
	}
	if (compruebaDominioMail(email)) {
		// alert("Email Valido");
		return true;
	} else {
		// alert("no valido");
		return false;
	}
}

/**
 * comprueba que el email no sea de uno de los dominios invalidos deinidos
 */
function compruebaDominioMail(email) {
	if (email.indexOf("asdf") != -1)
		return false;

	var dominio = email.substring(email.indexOf('@'));

	var falsos = new Array();
	falsos.push("nobulk");
	falsos.push("spamfree24.info");
	falsos.push("spamfree24.net");
	falsos.push("spamfree24.com");
	falsos.push("spamfree24.org");
	falsos.push("nullbox.info");
	falsos.push("inbox2.info");
	falsos.push("gorillaswithdirtyarmpits");
	falsos.push("maileater");
	falsos.push("guerrillamail");
	falsos.push("pookmail");
	falsos.push("mailinator");
	falsos.push("jetable");
	falsos.push("spambox");
	falsos.push("temporaryinbox");
	falsos.push("bugmenot");
	falsos.push("tempinbox");
	falsos.push("spaml");
	falsos.push("spamgourmet");
	falsos.push("spambob");
	falsos.push("spamday");
	falsos.push("spammotel");
	// falsos.push("asdf");

	var i = 0;
	for (i = 0; i < falsos.length; i++) {
		if (dominio.indexOf(falsos[i]) != -1)
			return false;
	}

	return true;
}

function esCIFoNIF(obj) {
	if (obj.lenght = '10' && obj.substring(0, 1) == '0') {
		obj = obj.substring(1, obj.lenght);
	}
	if (obj.lenght = '10' && (obj.substring(1, 2) == ' ' || obj.substring(1, 2) == '-') || obj.substring(1, 2) == '.') {
		obj = obj.substring(0, 1) + obj.substring(2, obj.lenght);
	}
	return (esNIF(obj) || esCIF(obj));
}

function esNIF(obj) {
	return isNIF(obj, 'NIF', true, false, false);
}
function esCIF(obj) {
	return isCIF(obj, 'CIF', true, false, false);
}
function isNIF(obj, nom, obl, msg, foc) {
	var args = isNIF.arguments;
	var cad = getInputString(obj);
	var errors = new Array();
	if ((obl == false) && (cad == "")) {
		return processErrors(obj, msg, foc, errors);
	}
	if (obl != null) {
		errors = addErrors(checkCompulsory(cad, nom, obl), errors);
	}
	errors = addErrors(checkSpaces(cad, nom, false), errors);
	errors = addErrors(checkMinLength(cad, nom, 2), errors);
	errors = addErrors(checkMaxLength(cad, nom, 10), errors);
	if (errors.length == 0) {
		var check = true;
		var numero = cad.substring(0, cad.length - 1);
		var extran = cad.substring(0, 1);
		if ((extran == 'X') || (extran == 'x')) {
			numero = cad.substring(1, cad.length - 1);
		}
		var letra = cad.substring(cad.length - 1, cad.length);
		errors = addErrors(checkInString(numero, nom, cadenaNumeros, errorSoloNumerosNIF), errors);
		errors = addErrors(checkInString(letra, nom, cadenaAlfabetoASCII, errorSoloLetrasNIF), errors);
		if (parseInt(numero, 10) == 0) {
			check = false;
		}
		var index = parseInt(numero, 10) % 23;
		var letrasNIF = new Array('T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E', 'T');
		if (letra.toUpperCase() != letrasNIF[index]) {
			check = false;
		}
		if (!check) {
			errors[errors.length] = changeMsg(errorValidacionNIF, nom, null, null, null, null);
		}
	}
	return processErrors(obj, msg, foc, errors);
}

function isCIF(obj, nom, obl, msg, foc) {
	var args = isCIF.arguments;
	var cad = getInputString(obj);
	var errors = new Array();
	if ((obl == false) && (cad == "")) {
		return processErrors(obj, msg, foc, errors);
	}
	if (obl != null) {
		errors = addErrors(checkCompulsory(cad, nom, obl), errors);
	}
	errors = addErrors(checkSpaces(cad, nom, false), errors);
	errors = addErrors(checkMinLength(cad, nom, 9), errors);
	errors = addErrors(checkMaxLength(cad, nom, 9), errors);
	errors = addErrors(checkInString(cad, nom, cadenaNumeros + cadenaAlfabetoASCII, errorCaracteresNoAdmitidosCIF), errors);
	if (errors.length == 0) {
		var check = false;
		cad = cad.toUpperCase();
		var letrasCIF = new Array('J', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
		var sumap = 0;
		var sumai = 0;
		var p;
		var r;
		var dc;
		sumap = parseInt(cad.substring(2, 3)) + parseInt(cad.substring(4, 5)) + parseInt(cad.substring(6, 7));
		for (n = 1; n <= 8; n++) {
			p = 2 * parseInt(cad.substring(n, n + 1));
			if (p > 9) {
				sumai += parseInt((p / 10), 10) + (p % 10);
			} else {
				sumai += p;
			}
			n++;
		}
		r = sumap + sumai;
		dc = r % 10;
		dc = 10 - dc;
		if (dc == 10) {
			dc = 0;
		}
		if (!isDigit(cad.charAt(8))) {
			if (letrasCIF[dc] == cad.charAt(8).toUpperCase()) {
				check = true;
			}
		} else {
			if (dc == parseInt(cad.substring(8, 9))) {
				check = true;
			}
		}
		if (!check) {
			errors[errors.length] = changeMsg(errorValidacionCIF, nom, null, null, null, null);
		}
	}
	return processErrors(obj, msg, foc, errors);
}
function processErrors(obj, msg, foc, errors) {
	var cad = "";
	if (errors.length > 0) {
		for (n = 0; n < errors.length; n++) {
			cad += errors[n];
			if (n != errors.length - 1) {
				cad += "\n";
			}
		}
		if (msg == true) {
			alert(cad);
		}
		if ((foc == true) && (obj != null)) {
			obj.focus();
		}
		if (msg == null) {
			return errors;
		}
		return false;
	}
	if (msg == null) {
		return errors;
	}
	return true;
}
function changeMsg(descError, nom, longitud, longitudCorrecta, valor, valorCorrecto) {
	var cad = descError.replace(/NOMBRE_CAMPO/g, nom);
	if (longitud != null) {
		cad = cad.replace(/LONGITUD_CAMPO/g, longitud)
	}
	if (longitudCorrecta != null) {
		cad = cad.replace(/LONGITUD_CORRECTA/g, longitudCorrecta)
	}
	if (valor != null) {
		cad = cad.replace(/VALOR_CAMPO/g, valor)
	}
	if (valorCorrecto != null) {
		cad = cad.replace(/VALOR_CORRECTO/g, valorCorrecto)
	}
	return cad;
}

function getInputString(obj) {
	var cad = ""
	if (obj.value) {
		cad = obj.value;
	} else {
		if (obj.value == "") {
			cad = obj.value
		} else {
			cad = obj;
		}
	}
	return cad;
}

function checkInString(cad, nom, cadComparar, cadError) {
	var errors = new Array();
	var check = true;
	for (n = 0; n < cad.length; n++) {
		for (m = 0; m < cadComparar.length; m++) {
			if (cad.charAt(n) == cadComparar.charAt(m)) {
				break;
			}
		}
		if (m == cadComparar.length) {
			check = false;
			break;
		}
	}
	if (!check) {
		errors[errors.length] = changeMsg(cadError, nom, null, null, null, null);
	}
	return errors;
}
function checkCompulsory(cad, nom, obl) {
	var errors = new Array()
	if ((obl == true) && (cad == "")) {
		errors[errors.length] = changeMsg(errorCampoObligatorioVacio, nom, null, null, null, null);
	}
	return errors;
}

function checkSpaces(cad, nom, spa) {
	var errors = new Array();
	if (spa == false) {
		for (n = 0; n < cad.length; n++) {
			if (cad.charAt(n) == " ") {
				errors[errors.length] = changeMsg(errorNoAdmiteEspacios, nom, null, null, null, null);
				break;
			}
		}
	}
	return errors;
}

function checkMinLength(cad, nom, min) {
	var errors = new Array();
	if (cad.length < min) {
		errors[errors.length] = changeMsg(errorNoAlcanzaLongitudMinima, nom, cad.length, min, null, null);
	}
	return errors;
}

function checkMaxLength(cad, nom, max) {
	var errors = new Array();
	if (cad.length > max) {
		errors[errors.length] = changeMsg(errorExcedeLongitudMaxima, nom, cad.length, max, null, null);
	}
	return errors;
}
function addErrors(errorsArr, errorsTotalArr) {
	for (n = 0; n < errorsArr.length; n++) {
		errorsTotalArr[errorsTotalArr.length] = errorsArr[n];
	}
	return errorsTotalArr;
}
function isDigit(car) {
	return ((car >= "0") && (car <= "9"));
}

function info(ancla,pagina,portal) {
	var w  = screen.width;
	var h  = screen.height;
	var yy = (document.layers) ? 404 : 400;
	var xx = 610;
	var x  = (w / 2) - (xx / 2);
	var y  = (h / 2) - (yy / 2);
	y_ventana = (h / 2) - (yy / 2) - 100;
	var parametros = 'resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,width='+xx+',scrollbars=1,height=' + yy + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + '';
	var win = window.open('/templates/html/Duns50M/'+portal+'/'+pagina+'.html#' + ancla, 'windowshtml', parametros);
	win.focus();
}

function formatea(input) {
	CARACTERES = ("ñÑáéíóúàèìòùâêîôûÁÉÍÓÚÀÈÌÒÙÂÊÎÔÛçÇ?\' /");
	CARACTERES_SUSTITUIR = ("nNaeiouaeiouaeiouAEIOUAEIOUAEIOUcC++++");
	return formateaGenerico(input,CARACTERES,CARACTERES_SUSTITUIR);
}

function formateaGenerico(input,CARACTERES,CARACTERES_SUSTITUIR) {
	var cadena = "";
	var found = false;
	for (var i = 0; i < input.length; i++) {
		var chr = input.charAt(i);
		found = false;
		for (var j = 0; j < CARACTERES.length; j++) {
			if (chr == CARACTERES.charAt(j)) {
				cadena += CARACTERES_SUSTITUIR.charAt(j);
				found = true;
			}// if
		}// for j
		if (!found) {
			cadena += chr;
		}// if
	}// for i
	return cadena;
}

function poperGenerico(url,width,height,top,left){		
	var ventana=window.open(url,"newWin","scrollbars=no,status=yes,width="+ width +",height="+ height +",top="+ top +",left="+left);
}
