window.addEvent('domready', function() {
    Alert = new AlertBox(); // alert, info, error, confirm, prompt
});
var focoActual="";

var version_='';
//quita menu contextual
function clickIE() {if (document.all) {;return false;}}
function clickNS(e) {if (document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

//document.oncontextmenu=new Function("return false");


	function numFormat(numero,dec)//da formato de moneda a un doble. 1er parametro es el numero, y el 2do es el decimal que se quiere
	{
		aux = String(numero);
		if (aux.indexOf(',')!=-1){
		    numero = redondear(Number(str_replace(',','.',str_replace('.','',String(numero)))),dec);
		}else{
			numero = redondear(Number(String(numero)),dec);
		}
		
		var num = numero, signo=3, expr;
		var cad = ""+numero;
		var ceros = "", pos, pdec, i;
		for (i=0; i < dec; i++)
			ceros += '0';
		pos = cad.indexOf('.')
		if (pos < 0)
			cad = cad+"."+ceros;
		else
		{
			pdec = cad.length - pos -1;
			if (pdec <= dec)
			{
				for (i=0; i< (dec-pdec); i++)
				cad += '0';
			}
			else
			{

				num = new String(num);
				aux_num=num.split('.');				
				num=aux_num[0]+'.'+aux_num[1][0]+aux_num[1][1];
				cad = new String(num);
			}
		}
		pos = cad.indexOf('.')
		if (pos < 0) pos = cad.lentgh
		if (cad.substr(0,1)=='-' || cad.substr(0,1) == '+')
		signo = 4;
		if (pos > signo)
		do{
		expr = /([+-]?\d)(\d{3}[\.\,]\d*)/
		cad.match(expr)
		cad=cad.replace(expr, RegExp.$1+','+RegExp.$2)
		}
		while (cad.indexOf(',') > signo)
		if (dec<0) cad = redondear(cad.replace(/\./,''),dec);
		//alert(cad);
		cad=str_replace("undefined","",str_replace(",",".",cad.split('.')[0])+","+cad.split('.')[1]);
		return cad;
	}
	


function redondear(cantidad, decimales) {
		var cantidad = parseFloat(cantidad);
		var decimales = parseFloat(decimales);
		decimales = (!decimales ? 2 : decimales);
		return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
	} 
	
function isset(variable_name) {
    try {		 
         if (typeof(eval(document.getElementById(variable_name))) != 'undefined')
         if (eval(document.getElementById(variable_name)) != null)
         return true;
     } catch(e) { }
    return false;
 } 
 
 window.addEvent('domready', function() {
    Alert = new AlertBox(); // alert, info, error, confirm, prompt
});
var focoActual="";
function foco(){

	idobj=foco.arguments[0];
	if(foco.arguments[1]==1){
		error=true;
	}else{ error=false;	}
	
  if(focoActual==idobj&&!error){return;}
  
  if(isset(idobj)){
	focoActual=idobj;  
	var destino =     document.getElementById(idobj);  
	var inputFields = document.getElementsByTagName('INPUT'); 
	var selectBoxes = document.getElementsByTagName('SELECT'); 
	var textareas =   document.getElementsByTagName('TEXTAREA');
	var inputs = new Array();
	for(i=0;i<inputFields.length;i++){
		if(inputFields[i].type!='button'&&
           inputFields[i].type!='submit'&&
		   inputFields[i].type!='reset'&&
		   inputFields[i].type!='checkbox'){
		   inputs[inputs.length] = inputFields[i];
			}
	}
	for(i=0;i<selectBoxes.length;i++){inputs[inputs.length] = selectBoxes[i];}
	for(i=0;i<textareas.length;i++){inputs[inputs.length] = textareas[i];}
	for(i=0;i<inputs.length;i++){ 
		
			inputs[i].onfocus=function(){foco(this.id);};			
			inputs[i].style.backgroundImage="";
			inputs[i].style.backgroundColor = '#FFFFFF';
			inputs[i].style.border = '1px solid #cccccc';

		
		if(inputs[i].type!='hidden'){
			//inputs[i].value=Mayusculas(inputs[i].value);
			
			if(inputs[i].getAttribute('tipo')=='moneda'){ 
				inputs[i].value =numFormat(inputs[i].value,2);
			}
		}
	}
		   if(destino.type!='button'&&destino.type!='submit'&&destino!='reset'){
		     
			 	
			    if(error){

					destino.style.backgroundColor = '#FEE2DA';
					destino.style.border= '1px solid #990000';
					
				}else{
					destino.style.backgroundColor = '#E7EAF1';
					destino.style.border = '1px solid #000000';
				}
			}
		   if(isNumberFloat(idobj) && destino.value.indexOf(',')!=-1){ 
		   		destino.value=str2Number(idobj);
				if(destino.value==0){
					destino.value='';
				}
				//destino.select();
			}
		   destino.focus();
	 
	}else{ setTimeout('foco("'+idobj+'","'+error+'")',10); }	   
}




function isNumberFloat(idString)
{
  return (!isNaN(parseFloat(str2Number(idString)))) ? true : false;
}


function str2Number(numero){

	if (valor(numero).indexOf(',')!=-1){
		
		return Number(str_replace(',','.',str_replace('.','',valor(numero))));
		    
	}else{
		
		return Number(valor(numero));
	}
		
	 
}	


function valor(id){
	return document.getElementById(id).value;
}	
function asigna(id_destino,valor){
	document.getElementById(id_destino).value=valor;
}	
//////////////////////////menu expan////////////
function expandit(curobj){
	if(document.getElementById(curobj)){
	  folder=document.getElementById(curobj).style;
	  }else{
		if(ns6==1||(agtbrw.indexOf('opera')!=-1)){
			folder=curobj.nextSibling.nextSibling.style;
		}else{
			folder=document.all[curobj.sourceIndex+1].style;
		}
	   }
	if (folder.display=="none"){
		folder.display="";
	}else{
		folder.display="none";}
}

////////////////////fin menu expan////////////////////
function Mayusculas(inputString)
{
  return inputString.toUpperCase();
}






function soloNum(evt){
	
	var key = (document.all)?evt.keyCode:evt.which;
	return (key <= 13 || (key >= 48 && key <= 57));
}
function val_caracter(evt){
	var key = (document.all)?evt.keyCode:evt.which;
	return (key!=45&&key!=39);
}
function soloLetras(evt){
	var key = (document.all)?evt.keyCode:evt.which;
	return (key==32||key <= 13 || (key >= 65 && key <= 90)||(key >= 97 && key <= 122) || key == 32);
}
function letrasNumeros(evt){
	var key = (document.all)?evt.keyCode:evt.which;
	return (key==32||key <= 13 || (key >= 65 && key <= 90)||(key >= 97 && key <= 122) || key == 32) ||(key <= 13 || (key >= 48 && key <= 57) && (key >= 165 && key <= 166));
}

function soloDecimal(evt,id_campo){
	var key = (document.all)?evt.keyCode:evt.which;
	cadena=document.getElementById(id_campo).value;	
	if ((cadena.length== 0 || checkForCharacters(cadena, '.') != -1 )&& key ==46 ){ return false; }
	return (key <= 13 || (key >= 48 && key <= 57)|| key==46);
}



function checkForCharacters(inputString, checkString, startingIndex)
{
  if (!startingIndex) startingIndex = 0;
  return inputString.indexOf(checkString);
}

function valida(form){ // requerido=" label", opcional(tamanio="tamaño")  

	var inputFields = $(form).getElementsByTagName('INPUT'); 
	var selectBoxes = $(form).getElementsByTagName('SELECT'); 
	var textareas = $(form).getElementsByTagName('TEXTAREA');
	var inputs = new Array();
	for(i=0;i<inputFields.length;i++){if(inputFields[i].getAttribute('requerido')&&!inputFields[i].disabled)inputs[inputs.length] = inputFields[i];}
	for(i=0;i<textareas.length;i++){if(textareas[i].getAttribute('requerido')&&!textareas[i].disabled)inputs[inputs.length] = textareas[i];}
	for(i=0;i<selectBoxes.length;i++){if(selectBoxes[i].getAttribute('requerido')&&!selectBoxes[i].disabled)inputs[inputs.length] = selectBoxes[i];}
	
	for(i=0;i<inputs.length;i++){
		//alert(inputs[i].id);
		msj=inputs[i].getAttribute('requerido')+' es requerid@!\n';
		if(inputs[i].getAttribute('tamanio')){
			tamanio=inputs[i].getAttribute('tamanio');
			msj+='y debe contener al menos \''+ tamanio+'\' caracteres';
		}else tamanio=1;
		if(inputs[i].value.length<tamanio||(inputs[i].value=='0,00'&&inputs[i].getAttribute('tipo')=='moneda')){	
			
			focoActual=inputs[i].id;
		    Alert.alert(msj);		
			foco(inputs[i].id,1);
			return false;
		}else{
			if(inputs[i].getAttribute('mail')=='1'){
				
				if(!validaEmail(inputs[i].id))return false;
				
				
			}
			
			if(inputs[i].id=='contrasena'&&isset('re_contrasena')&&(valor('contrasena')!=valor('re_contrasena'))){
					focoActual=inputs[i].id;
					Alert.alert('Contraseñas no coinciden!');		
					foco(inputs[i].id,1);
					return false;
			}
			
		}
	}
	return true;
}
function validaEmail(id){
		 val  = valor(id);
		 correo = val.indexOf('@');  
		 band=true;
		 if (correo != -1){
			 email = val.split('@');
			 point = email[1].indexOf('.');
			 punto = email[1].split('.');
			 if (point != -1 && point!=''){
				 if (punto[1].length<2 || punto[1].length>5){
					 focoActual=id;
					 Alert.alert('El dominio de la direccion de email no es valido!<br/><br/>Ejemplo: mi_email@mi_dominio.com');
					 foco(id,1);
					 band=false;
				 }
			 }else{
				  focoActual=id;
				  Alert.alert('Falta el dominio del email!<br/><br/>Ejemplo: mi_email@mi_dominio.com');
				  foco(id,1);
				  band=false;
			 }
		 }else{
			    focoActual=id;
				Alert.alert('El email es incorrecto!<br/><br/>Ejemplo: mi_email@mi_dominio.com');
				foco(id,1);
				band=false;      
		 }
		  				  
		 return band;  
}


function str_replace(inChar,outChar,conversionString)
{
  var convertedString = conversionString.split(inChar);
  convertedString = convertedString.join(outChar);
  return convertedString;
}

 

function desmarcar_todo(idObjet) {
	if(idObjet){
  		for(i=0; ele=document.getElementById(idObjet).elements[i]; i++) 
  		ele.checked=false;
	}else{
		ids=String(document.getElementById('ids').value).split('-');
		for(i=0;i<ids.length;i++){
			document.getElementById(ids[i]).checked=false;
		}
	}
} 

function marcar_todo(idObjet) {	
	if(idObjet){
  		for(i=0; ele=document.getElementById(idObjet).elements[i]; i++) 
  		ele.checked=true;
	}else{
		
		ids=String(document.getElementById('ids').value).split('-');		
		for(i=0;i<ids.length;i++){ 
			document.getElementById(ids[i]).checked=true;
		}
	}
} 

function marcar_hasta(idObjet) {

	ids=String(document.getElementById('ids').value).split('-');
	for(i=0;i<ids.length;i++){
		document.getElementById(ids[i]).checked=true;
		if(ids[i]==idObjet) break;
	}
	i++;
	for(;i<ids.length;i++){
		document.getElementById(ids[i]).checked=false;
	}

} 

function validar_marcado(idObjet) {
 if(idObjet){	
  for(i=0; ele=document.getElementById(idObjet).elements[i]; i++)
    if (ele.type=='checkbox')
      if (ele.checked)
        return true;
 }else{
	 if(document.getElementById('ids').value!=''){
		ids=String(document.getElementById('ids').value).split('-');
		for(i=0;i<ids.length;i++){
			if(document.getElementById(ids[i]).checked)return true;
		}
	 }
 
 }
  return false;
} 


function redirect(URLStr){ location = URLStr; }


function IsNumeric(valor) 
{   var log=valor.length; var sw="S"; 
	for (x=0; x<log; x++){ 
		v1=valor.substr(x,1); 
		v2 = parseInt(v1); 
		if (isNaN(v2)) { sw= "N";} 
	} 
	if (sw=="S") {return true;} else {return false; } 
} 
var primerslap=false; 
var segundoslap=false; 

function formateafecha(fecha) 
{ 	var long = fecha.length; 
	var dia; var mes; var ano;
	anio_actual=new Date();
	anio_actual=anio_actual+' ';
	anio_actual=anio_actual.split(' ')[3];

	if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
		if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
		else { fecha=""; primerslap=false;} 
	} 
	else 
	{ dia=fecha.substr(0,1); 
	if (IsNumeric(dia)==false) 
	{fecha="";} 
	if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
	} 
	if ((long>=5) && (segundoslap==false)) 
	{ mes=fecha.substr(3,2); 
	if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
	else { fecha=fecha.substr(0,3); segundoslap=false;} 
	} 
	else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
	if (long>=7) 
	{ ano=fecha.substr(6,4); 
	if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
	else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>anio_actual)) { fecha=fecha.substr(0,6); } } } 
	} 
	
	if (long>=10) 
	{ 
	fecha=fecha.substr(0,10); 
	dia=fecha.substr(0,2); 
	mes=fecha.substr(3,2); 
	ano=fecha.substr(6,4); 
	// Año no viciesto y es febrero y el dia es mayor a 28 
	if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
	} 
	return (fecha); 
}

function valida_anio(id){//#14
	var anio=document.getElementById(id).value;
	var long = anio.length;
	anio_actual=new Date();
	anio_actual=anio_actual+' ';
	anio_actual=anio_actual.split(' ')[3];
	if ((long<3)||(anio==0) || (anio<1900) || (anio>anio_actual+2)) { 
		document.getElementById(id).value='';
		alert('Debe Introducir un Año Valido');
		return false;
	} 
	return true;
}
function validarlista(lista,entrada){
	band2=true;
	 for (var no=0;no<document.getElementById(lista).options.length;no++){
		if(document.getElementById(lista).options[no].value== entrada){
				band2=false;break; 
		}
	 }
	 return band2
}
function agregaItemSelect(ToBox,text,value,valida){     
	if (valida&&!validarlista(ToBox,value)){alert('Registro ya Agregado');return;}
	document.getElementById(ToBox).options[document.getElementById(ToBox).options.length] = new Option(text,value); 

}

function eliminaItemSelect(FromBox,value){	
	var tmpToBox = document.createElement('select');
	for (var no=0;no<document.getElementById(FromBox).options.length;no++){
		if(document.getElementById(FromBox).options[no].value!=value){
		  tmpToBox.options[tmpToBox.options.length] = new Option(document.getElementById(FromBox).options[no].text,document.getElementById(FromBox).options[no].value); 
		}
	 }
	 document.getElementById(FromBox).options.length=0;
	 for (var no=0;no<tmpToBox.options.length;no++){
		  document.getElementById(FromBox).options[document.getElementById(FromBox).options.length] = new Option(tmpToBox.options[no].text,tmpToBox.options[no].value); 
	 }		 
}
function seleccionaItem(val,lista,separador,campo){
	lista=document.getElementById(lista);
	for (var no=0;no<lista.options.length;no++){
		if(checkForCharacters(str_replace('.','',lista.options[no].value.split(separador)[campo]),valor(val)) != -1){
		  lista.options[no].selected=true;
		  document.getElementById(val+'Msj').innerHTML='';
		  return;
		}
	 }
	 document.getElementById(val+'Msj').innerHTML='Código errado o no existe!';
}
function Inner(id, val){
	if(!val)val='';
	document.getElementById(id).innerHTML=val;
}

function bloquea(){
	for (var no=0;no<bloquea.arguments.length;no++){
		if (isset(bloquea.arguments[no])){
			if (document.getElementById(bloquea.arguments[no]).disabled)
				 document.getElementById(bloquea.arguments[no]).disabled=false;
			else
				 document.getElementById(bloquea.arguments[no]).disabled=true;
		}
	}
}



function corta_cadena(cadena,tamananio){
	band=true;
	i=0;
	salida='';
	while(true){		
		if((i>=tamananio&&cadena[i]==' ')||(i==cadena.length)){
			return salida;
			}
		salida+=cadena[i++];
	}
}

function ltrim(s) { 
    return s.replace(/^\s+/, ""); 
} 
 
function rtrim(s) { 
    return s.replace(/\s+$/, "")+" "; 
} 
 
function trim(s) { 
    return rtrim(ltrim(s)); 
}

function Abrir(url)
{
	ventana=window.open(url,url, "scrollbars=yes,toolbar=no,menubar=no,status=no,alwaysRaised=yes,directories =no,location=no,z-lock=yes,top=yes,fullscreen=yes");
	if(navigator.appName!='Netscape'){  
		this.close();
	}else{
		ventana.moveTo(0,0);
		if (document.all) {
			ventana.resizeTo(screen.availWidth,screen.availHeight);
		}
		else if (document.layers||document.getElementById) {
		if (ventana.outerHeight<screen.availHeight||ventana.outerWidth<screen.availWidth){
			ventana.outerHeight = screen.availHeight;
			ventana.outerWidth = screen.availWidth;
		}
		}
	}
	ventana.focus();
}

function popup(url, ancho, largo, aux) {
         day = new Date();
         id = day.getTime();
		 x = (screen.width-ancho)/2;
		 y = (screen.height-largo)/2;
         eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars="+aux+",location=0,statusbar=0,menubar=0,resizable=0,width="+ancho+",height="+largo+",left = "+x+",top = "+y+"');");
}

