cuenta=0;
function validar(fx){
	email_str=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;

	for (i = 0; i < fx.elements.length; i++) {
		if ((fx.elements[i].type == "text" || fx.elements[i].type == "password" || fx.elements[i].type == "hidden" || fx.elements[i].type == "textarea") && 
			(fx.elements[i].name!= 'login_m' && fx.elements[i].name!= 'pass_m' && fx.elements[i].name!= 'pagina' && fx.elements[i].name != 'email' && fx.elements[i].name != 'telefono' && fx.elements[i].name != 'codigo_tel' &&fx.elements[i].name != 'fax' && fx.elements[i].name != 's_nob_f' && fx.elements[i].name != 's_ape_f' && fx.elements[i].name != 'cod_nom' && fx.elements[i].name != 'id_func' && fx.elements[i].name != 'direccion' && fx.elements[i].name != 'latitud'  && fx.elements[i].name != 'longitud'  && fx.elements[i].name != 'cedula_lider'  && fx.elements[i].name != 'lider'   && fx.elements[i].name != 'longitud'  && fx.elements[i].name != 'area' && fx.elements[i].name != 'nro_integ') &&
			(fx.elements[i].value == "" || 
			fx.elements[i].value == "null" || 
			fx.elements[i].value.indexOf("\"", 0) > -1 || 
			fx.elements[i].value.indexOf("=", 0) > -1 || 
			fx.elements[i].value.indexOf("\'", 0) > -1)) {
				alert("Por favor completa todos los campos del formulario\nVerifique que no contenga caracteres especiales");
				
				if(fx.elements[i].type != "hidden"){
					fx.elements[i].focus();
				}
				return false;
				break;
			}

			if(fx.elements[i].name == "email" && fx.elements[i].value!=""){
				if(!email_str.test(fx.elements[i].value)) {
					alert("El formato del campo email no es valido");
					return false;
					break;
				}
			}
			if(fx.elements[i].value=="x999" && fx.elements[i].name!="tipoabrae" && fx.elements[i].name!="abrae" && fx.elements[i].name!="rio"&& fx.elements[i].name!="parroquia_p"&& fx.elements[i].name!="municipio_p"){
				if(!email_str.test(fx.elements[i].value)) {
					alert("Por favor completa todos los campos del formulario\nVerifique las listas dependientes");
					return false;
					break;
				}
			}
		}
		if (cuenta == 0){
			cuenta++;
			return true;
		}else{
			alert("Por favor espera la respuesta de tu peticion!");
			return false;
		}
	}

function currencyFormat(fld, milSep, decSep, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true; // Enter
	if (whichCode == 8) return true; // Enter
	if (whichCode == 46) return true; // Enter
	if (whichCode == 0) return true; // Enter
    key = String.fromCharCode(whichCode); // Get key value from key code
    if (strCheck.indexOf(key) == -1) return false; // Not a valid key
    len = fld.value.length;
    for(i = 0; i < len; i++)
     if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
    aux = '';
    for(; i < len; i++)
     if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) fld.value = '';
    if (len == 1) fld.value = '0'+ decSep + '0' + aux;
    if (len == 2) fld.value = '0'+ decSep + aux;
    if (len > 2) {
     aux2 = '';
     for (j = 0, i = len - 3; i >= 0; i--) {
      if (j == 3) {
       aux2 += milSep;
       j = 0;
      }
      aux2 += aux.charAt(i);
      j++;
     }
     fld.value = '';
     len2 = aux2.length;
     for (i = len2 - 1; i >= 0; i--)
      fld.value += aux2.charAt(i);
     fld.value += decSep + aux.substr(len - 2, len);
    }
    return false;
}



function formato_moneda(fld, milSep, decSep,e) {
	var bandera=0;
	var num_dec=0;
	var comas=0;
	var comas2=0;
	var campo='';
	var aux_entero=0;
	var parte_entera='';
	var num_max_dec=2;
    var sep_dec = ',';
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789,';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    key = String.fromCharCode(whichCode); 
	if (whichCode == 13) return true; 						// Enter
	if (whichCode == 8)	return true;		// Enter
	if (whichCode == 46) key=',';						// Enter 
	if (whichCode == 0) return true; 					// Consigue el valor del codigo de tecla...
    if (strCheck.indexOf(key) == -1){ 
	return false; 	// no es una tecla valida	
	}	
	for(i=0;i<fld.value.length;i++){
		if(fld.value.charAt(i)=='.')
			comas2=comas2+1;
		}
	if(comas2>0){		
	campo_split=fld.value.split('.');
		for(i=0;i<=comas2;i++){
			campo+=campo_split[i];
		}
	}else
		campo=fld.value;
			
	if(sep_dec.indexOf(key)!= -1){
		if(campo.indexOf(key)!= -1){
			return false;
			}
	}		
	//tamaño del campo 
	if(campo.length>15)
		return false;
		
	if((campo+key).indexOf(',')!= -1)
		bandera=1;
	
	cadena=campo.split(',');
	
	if(bandera!=1)
		cadena[0]+=key;
	
	//calcular numero de comas
	for(i=(cadena[0].length)/3;i>1;i--)
		comas=comas+1;

	if(cadena[0].length%3==0)
		aux_entero=3;
	else
		aux_entero=(cadena[0].length%3);
				
	if(bandera!=1){
		for(i=0;i<(aux_entero);i++){
			parte_entera+=cadena[0].charAt(i);
		}
		if(cadena[0].length>3)
			parte_entera+='.';
			
		for(i=aux_entero,j=1;i<(cadena[0].length);i++,j++){
			parte_entera+=cadena[0].charAt(i);
			if(j%3==0&& cadena[0].length-1!=i){
				parte_entera+='.';
				}
			}
		
		campo_final=parte_entera;
		}
	else{
	for(i=0;i<(aux_entero);i++){
			parte_entera+=cadena[0].charAt(i);
		}
		if(cadena[0].length>3)
			parte_entera+='.';
			
		for(i=aux_entero,j=1;i<(cadena[0].length);i++,j++){
			parte_entera+=cadena[0].charAt(i);
			if(j%3==0&& cadena[0].length-1!=i){
				parte_entera+='.';
				}
			}
		if(cadena[1]!=undefined)
			len2=cadena[1].length;
		else len2=0;
		
		for(i=0;i<len2;i++){
				num_dec++;
			}
		if(num_dec>=num_max_dec)
			return false;
		if(cadena[1]!=undefined)
			campo_final=(parte_entera+','+cadena[1]+key);
		else
			campo_final=(parte_entera+',');
	}
	
	fld.value=campo_final;		
	return false;
}


function solo_numeros(fld,e) {
    var aux = aux2 = '';
	var i = j = 0;
	
   	var strCheck = '0123456789';
    var whichCode = (window.Event) ? e.which : e.keyCode;

    if (whichCode == 13) return true; // Enter
	if (whichCode == 8) return true; // Enter
	if (whichCode == 46) return true; // Enter
    key = String.fromCharCode(whichCode); // Get key value from key code
    if (strCheck.indexOf(key) == -1) return false; // Not a valid key
	fld.value += aux2.charAt(i);
}
var fld_old="0";
var aux_old=0;
var aux_old2=0;

function formato_campo(fld,e,t) {
	
    var aux = aux2 = '';
	var i = j = 0;

	if(t==1)
    	var strCheck = '0123456789';
	if(t==2)
    	var strCheck = 'AaBbCcDdEeFfGgHhIiJjKkLlÑñNnMmOoPpQqRrSsTtUuVvWwXxYyZzáÁéÉíÍóÓúÚ ';
	if(t==3)
    	var strCheck = '0123456789-ext';
	if(t==4)
    	var strCheck = '0123456789';
	
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true; // Enter
	if (whichCode == 8) return true; // Enter
	if (whichCode == 0) return true; // Enter
	if(t==4){
		if(fld.value==""){
			aux_old=0;
			aux_old2=0;
		}
		if(fld_old=="0")
			fld_old=fld;
		else{
			if(fld_old==fld){
				if (whichCode == 46 && aux_old==0){
				aux_old=1;
				return true;
				}
				if (whichCode == 45 && aux_old2==0 && fld.value==""){
				aux_old2=1;
				return true;
				}
			}else{
				fld_old=fld;
				aux_old=0;
				aux_old2=0;
				if (whichCode == 46 && aux_old==0){
				aux_old=1;
				return true;
				}
				if (whichCode == 45 && aux_old2==0 && fld.value==""){
				aux_old2=1;
				return true;
				}
			}
		}
	}
    key = String.fromCharCode(whichCode); // Get key value from key code
    if (strCheck.indexOf(key) == -1) return false; // Not a valid key
	fld.value += aux2.charAt(i);
} 

function ventanaPopUp (pagina,ancho,alto,scroll_b){
	var opciones=("toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars="+scroll_b+", resizable=no, width="+ancho+", height="+alto+"");
	var w=window.open(pagina,"ventana",opciones);
}
function ventanaPopUp2(pagina,ancho,alto,scroll_b){
	var opciones=("toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars="+scroll_b+", resizable=no, width="+ancho+", height="+alto+"");
	var w=window.open(pagina,"ventana2",opciones);
}



function esVacioPermiso(fecha_solicitud,nombre_solicitante,cedula_solicitante,actividad,estado,municipio,tipo_permiso,superficie,vigencia,fecha_respuesta,estatus){

	if(fecha_solicitud=="" ||fecha_solicitud=="--" || tipo_permiso=="" || fecha_respuesta==""|| estatus=="")
		return true;
	else
		return false;
}

function esVacioDeposito(nombre_depositante,ci_pasaporte,banco,n_deposito,n_cuenta,fecha_deposito,total_pagado){

	if(nombre_depositante=="" || ci_pasaporte=="" || banco=="" || n_deposito=="" || n_cuenta==""|| fecha_deposito==""||fecha_deposito=="--"|| total_pagado=="")
		return true;
	else
		return false;
}

function fechaDeposito(fecha){
        f_s = fecha;
        f_s = f_s.split("-");
        n_f_s = f_s[2]+"-"+f_s[1]+"-"+f_s[0];


    d = new Date();
        hoy = d.getFullYear()+"-"+d.getMonth()+1+"-"+d.getDate();
    return (hoy < n_f_s);
}

/*retorna true si es puro numero*/
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
 ////////////
  ////////////
   ////////////
    ////////////
     ////////////
      ////////////
 
//el fume de chequeadr mail
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}
////////////////
////////////////
////////////////
////////////////
////////////////
////////////////   
   
   
/*retorna true si es vacio*/
function isEmpty(text){
return text=="";
}


function isEmptyUbicationSelection(e,m,p){
    if(e.value==0)return true;
    
    if(e.value!=0 && m.value == 0) return true;
    
    if(e.value!=0 && m.value!=0 && p.value==0)return true;
    
    if(e.value==0||m.value==0 || p.value==0)return true;
    
    return false;
}

function custom_to_html_encoding(texto){
texto=texto.replace(/%a/g, "");
texto=texto.replace(/%e/g, "");
texto=texto.replace(/%i/g, "");
texto=texto.replace(/%o/g, "");
texto=texto.replace(/%u/g, "");
texto=texto.replace(/%A/g, "");
texto=texto.replace(/%E/g, "");
texto=texto.replace(/%I/g, "");
texto=texto.replace(/%O/g, "");
texto=texto.replace(/%U/g, "");
texto=texto.replace(/%n/g, "");
texto=texto.replace(/%N/g, "");
return texto;
}

function busca(ref,array,patron){
    //borro todo el select
    longitud = ref.length;
    for(var i=0;i<longitud;i++){
        ref.options[0]= null;
    }
    for(i=0;i<array.length;i++){
        str = array[i].nombre;
        //hago una busqueda en array: los que se parezcan a str;
        if(str.match(patron,"i")!=null){
            ref.options.add(new Option(array[i].nombre,array[i].id));
        }

    }
}

function muestra_todo(ref,array){
    //borro todo el select
    longitud = ref.length;
    for(var i=0;i<longitud;i++){
        ref.options[0]= null;
    }
    for(i=0;i<array.length;i++){
        str = array[i].nombre;
        ref.options.add(new Option(array[i].nombre,array[i].id));
    }
}

function submit_to_new_window(forma,ancho,alto,scroll_b)
{
	var opciones=("toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars="+scroll_b+", resizable=no, width="+ancho+", height="+alto+"");
	window.open("","ventana",opciones);
	forma.target = "ventana";
	return true;
	//forma.submit;
}



