/*

// funciones para validacion de FORMS
// Deberia haber una funcion para cada tipo de campo.
*/
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}

 
		document.onkeypress = stopRKey;

function nif(dni) {
  numero = dni.substr(0,dni.length-1);
  let = dni.substr(dni.length-1,1);
  numero = numero % 23;
  letra='TRWAGMYFPDXBNJZSQVHLCKET';
  letra=letra.substring(numero,numero+1);
  if (letra!=let) 
    return false;
    else return true;
}

function validateEmail(email){
	var regEx = /^[\w\.\+-]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/;
	if (!regEx.test(email)) {
		return false;
	} 
return true;
}

function validateNumber(valor){
  	var regEx = /^[0-9]+[\.]?[0-9]*$/;
	if (!regEx.test(valor)) {
		return false;
	} 
	return true;

}
function formato_numero(numero, decimales, separador_decimal, separador_miles){ // v2007-08-06
   numero=numero.toString().replace('.', '');
    numero=numero.replace(',', '.');
    numero=parseFloat(numero);

    if(isNaN(numero)){
        return "";
    }
	  //numero=numero.replace(",", ".");
    if(decimales!==undefined){
        // Redondeamos
        numero=numero.toFixed(decimales);
        
    }

    // Convertimos el punto en separador_decimal
    numero=numero.toString().replace(".", separador_decimal!==undefined ? separador_decimal : ",");

    if(separador_miles){
        // Añadimos los separadores de miles
     var miles=new RegExp("(-?[0-9]+)([0-9]{3})");
  //      var miles=new RegExp("^(\d{1}\.)?(\d+\.?)+(,\d{2})?$");
        while(miles.test(numero)) {
            numero=numero.replace(miles, "$1" + separador_miles + "$2");
        }
    }

    return numero;
}
function validateNumber_float(valor,idx){

		dix = document.getElementById(idx);
		
		dix.value =  formato_numero(dix.value,2,",",".");
	
	
}
function float_to_sql(valor){
	numero  = valor.toString().replace(".",'');
	numero = numero.replace(",",'.');
	return numero;
}

function validate_percent(valor,idx){
	dix = document.getElementById(idx);
	numero = valor.toString().replace(",",".");
	numero = parseFloat(valor);
	
//numero = valor;	
//	aux = "0,00";
	aux = "";
	if (!isNaN(numero)) aux = ""; 
	if (numero > 100) aux = "100,00";

	
	if (numero > -1 & numero < 101 & !isNaN(numero) ){
		numero = numero.toString().replace(",",'.');
		aux = formato_numero(numero,2,",",".");
	}
	
	dix.value = aux;
	
}

/*

CUSTOM JS FUNCTIONS

*/

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 obrirPopUp(url, alt, ample, scrollok){
var altura=screen.height;
var amp=screen.width;
		xW= (screen.width - ample)/2;
		yW= (altura - alt)/2;
		esquema=window.open(""+url+"" ,"esquema","left="+xW+", top="+yW+",width="+ample+", height="+alt+",resizable=0,status=0,statusbar=0,toolbar=0,location=0,scrollbars="+scrollok+",menubar=0");
		esquema.focus();

}

function DeleteRegistro(div_id,id_registro,cat,tabla){

	
	if(confirm('ATENCION: Estas seguro? (Si hay otros registros que dependen de este, este no se eliminara.) ') != false){
	
	ajax=objetoAjax();
	//url = 'lib/moduls/consulta.php?year='+comboyear+'&city='+combocity;
	url = 'scripts/exec_delete.php?cat='+cat+'&rid='+id_registro+'&tabla='+tabla;
	
	ajax.open("GET", url);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
//			divResultado.innerHTML = ajax.responseText
			aux = 0;
			aux  = ajax.responseText;
 document.getElementById(div_id).style.display = 'none';
			
		}
	}
	ajax.send(null);

	
	}
}

function show_add_option_box(field){
	id = "combo_"+field;
	id2 = "div_"+field;
	div = document.getElementById(id);
	div2 = document.getElementById(id2);	
	
	div.style.display='none';
	div2.style.display='block';
}

function close_add_option_box(field){
	id = "combo_"+field;
	id2 = "div_"+field;
	div = document.getElementById(id);
	div2 = document.getElementById(id2);	
	

	div2.style.display='none';
	div.style.display='block';

}

function add_new_option_to_combo(field){

	id = "combo_"+field;
	id2 = "div_"+field;
	div = document.getElementById(id);
	div2 = document.getElementById(id2);	
	
	new_option = document.getElementById('new_'+field).value;
	

	div2.style.display='none';
	div.innerHTML = 'Loading ...';
	div.style.display='block';
//	div.style.display='block';
	
	
	
	
	ajax=objetoAjax();
	//url = 'lib/moduls/consulta.php?year='+comboyear+'&city='+combocity;
	url = 'scripts/exec_add_option_combo.php?field='+field+'&val='+new_option;
	//http://www.intersalus.com/admin/index.php?rid=&area=test&tabla=ref_Areas&cat=9&op=exec_add&rid=
	ajax.open("GET", url);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
//			divResultado.innerHTML = ajax.responseText
			div.innerHTML = ajax.responseText;			
		}
	}
	ajax.send(null);

	document.getElementById('new_'+field).value = "";
	


}
		
