/* 
       +-------------------------------------------------------------------------- +
       | Desarrollado por: Miharbi Hernandez                                          | 
	   |         Telefono: 0414-4284230                                            | 
	   |	        Email: gustavoocanto@gmail.com , gustavo.ocanto@hotmail.com    |
	   + ------------------------------------------------------------------------- + 
	   |            Valencia Edo. Carabobo - Venezuela	              			   |
       + ------------------------------------------------------------------------- +
*/
var version_='';
function redirect(url){
	
	location=url;	
}
function on_enter(evt,fun,tipo,val_01,val_02,val_03){
	var key = nav4 ? evt.which : evt.keyCode;	
	var t;
	if(key == 13||evt==13){ 
		switch (fun){
		   case 1: foco(val_01); break;		   
		} 		
	return false;
	}else{
		switch (tipo){
		   case 1 : return soloNum(evt);
		   case 2 : return soloLetras(evt);
		   case 3 : return val_caracter(evt);
		   case 99: t++;break;
		   default: return soloDecimal(evt,tipo);
		   //case 4: return soloDecimal(evt,val_01);
		   //default: return soloDecimal(evt,tipo);
		}  
	}
}

var nav4 = !document.all ? true : false;
function soloNum(evt){
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57)|| key==45);
}
function val_caracter(evt){
	var key = nav4 ? evt.which : evt.keyCode;
	return (key==32|| key <= 13 || (key >= 65 && key <= 90)||(key >= 97 && key <= 122)|| (key >= 40 && key <= 57) || (key>=133&&key<=235));
}
function soloLetras(evt){
	var key = nav4 ? evt.which : evt.keyCode;
	return (key==32||key <= 13 || (key >= 65 && key <= 90)||(key >= 97 && key <= 122) || key == 32 || (key >=160 && key <=164) || key ==130);
}

function soloDecimal(evt,id_campo){
	var key = nav4 ? evt.which : evt.keyCode;
	cadena=document.getElementById(id_campo).value;	
	if (checkForCharacters(cadena, ',') != -1 && key ==44 ||(cadena.length== 0 && key ==44 ||  key ==46 )||(checkForCharacters(cadena, '.') != -1 && key ==46)){ return false; }
	return (key <= 13 || (key >= 48 && key <= 57)|| key==46 || key==44);
}		
function checkForCharacters(inputString, checkString, startingIndex)
{
  if (!startingIndex) startingIndex = 0;
  return inputString.indexOf(checkString);
}

function inicio(url){
         location.href=url		
}



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;
 }

function foco(idobj){//#5
		document.getElementById(idobj).focus();
}	

function valida(){ // requerido=" label", opcional(tamanio="tamaņo")
	     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].getAttribute('requerido'))inputs[inputs.length] = inputFields[i];}
	     for (i=0;i<selectBoxes.length;i++){if(selectBoxes[i].getAttribute('requerido'))inputs[inputs.length] = selectBoxes[i];}
	     for (i=0;i<textareas.length;i++){if(textareas[i].getAttribute('requerido'))inputs[inputs.length] = textareas[i];}
	     for (i=0;i<inputs.length;i++){
		      msj=inputs[i].getAttribute('requerido')+' es requerido(a)!\n\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){	
		          foco(inputs[i].getAttribute('id'));
			      alert(msj);
			      return false;
		      }
	     }
	     return true;
}

function valida_interno(atributo){
	     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].getAttribute(atributo))inputs[inputs.length] = inputFields[i];}
	     for (i=0;i<selectBoxes.length;i++){if(selectBoxes[i].getAttribute(atributo))inputs[inputs.length] = selectBoxes[i];}
	     for (i=0;i<textareas.length;i++){if(textareas[i].getAttribute(atributo))inputs[inputs.length] = textareas[i];}
	     for (i=0;i<inputs.length;i++){
		      if (inputs[i].value==""){
				  alert(inputs[i].getAttribute(atributo)+' es requerido(a)!\n\n');
				  foco(inputs[i].getAttribute('id'));
				  return false;
			  }
	     }
	     return true;
}

function str_replace(inChar,outChar,conversionString){
         var convertedString = conversionString.split(inChar);
         convertedString = convertedString.join(outChar);
         return convertedString;
}

function sinFormato(number){
		 return str_replace(',','.',str_replace('.','',number));
}	 	   	     


function fondo(id, color, op){ //#E2E2E2
         ids     = id;
		 colores = color.split('-');
		 valor = ids.split('_');
		 if (op == '1'){
			 //changeimg('over', 'foto_b'+valor[1], 'img/detalle2.gif');  
		     document.getElementById(ids).style.backgroundColor = colores[0];
			 document.getElementById(ids).style.color = "#FFFFFF";
		 }else{			 
			 if (valor[1]%2==0){
			 	 colors = colores[0];
				 foto = "img/detalle.gif";
			 }else{
			     colors = colores[1]; 
				 foto = "img/detalle1.gif";
			 }
			 document.getElementById(ids).style.backgroundColor = colors;
			 document.getElementById(ids).style.color = "#000000";
			 //changeimg('over', 'foto_b'+valor[1], foto);
		 }
}

function fondo2(id, color, op){ //#E2E2E2
         ids     = id;
		 colores = color.split('-');
		 valor = ids.split('_');
		 if (op == '1'){
			 changeimg('over', 'foto_b'+valor[1], 'img/detalle2.gif');  
		     document.getElementById(ids).style.backgroundColor = colores[0];
			 document.getElementById(ids).style.color = "#FFFFFF";
		 }else{			 
			 if (valor[1]%2==0){
			 	 colors = colores[0];
				 foto = "img/detalle.gif";
			 }else{
			     colors = colores[1]; 
				 foto = "img/detalle1.gif";
			 }
			 document.getElementById(ids).style.backgroundColor = colors;
			 document.getElementById(ids).style.color = "#000000";
			 changeimg('over', 'foto_b'+valor[1], foto);
		 }
}

function Mayusculas(inputString)
{
  return inputString.toUpperCase();
}

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 changecolor(id, op){ //#E2E2E2
		 if (op == '1')
			 document.getElementById(id).style.color = "#FFB500";
		 else			 
			 document.getElementById(id).style.color = "#FFFFFF";
}


function show(id, modo){
	     obj=document.getElementById(id).style;
	     switch (modo){
		                case 1: obj.display="none"; break;
						case 2: obj.display="";     break;
		 }
	
}

function borrar(id, id_capa, op, url){
	     band=true;
		 if (confirm("Esta seguro de eliminar este registro ??")){
			 ir_url (url+'&id='+id, id_capa)
			 //inicio('index.php?op='+op);
		 }else{
		     band=false;	 
		 }
		 
		 return band;
}

function validaEmail(id){
		 valor  = document.getElementById(id).value;
		 correo = valor.indexOf('@');  
		 band=true;
		 if (correo != -1){
			 email = valor.split('@');
			 point = email[1].indexOf('.');
			 punto = email[1].split('.');
			 if (point != -1 && point!=''){
				 if (punto[1].length<2 || punto[1].length>5){
					 foco(id);
					 alert('El dominio de la direccion de email no es valido!\n\nEjemplo: mi_email@mi_dominio.com');
					 band=false;
				 }
			 }else{
				  foco(id);
				  alert('Falta el dominio del email!\n\nEjemplo: mi_email@mi_dominio.com');
				  band=false;
			 }
		 }else{
			    foco(id);
				alert('El email es incorrecto!\n\nEjemplo: mi_email@mi_dominio.com');
				band=false;      
		 }
		  				  
		 return band;  
}
 
function changeimg(opcion, id, strImg) {
		 if (opcion='over')
	         document.getElementById(id).src = strImg;
		 else	 
		     document.getElementById(id).src = strImg; 
}

function changeimg2(opcion, id, strImg) {
	     
		 //alert(parent.document.getElementById(id).value);
		 if (opcion='over')
	         parent.document.getElementById(id).src = strImg;
		 else	 
		     parent.document.getElementById(id).src = strImg; 
}


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";}
}

function confirma(msj){
         if (confirm(msj)) 
		     return true;
		 else
		     return false;	
}

function popup(url, ancho, largo, aux) {
         day = new Date();
         id = day.getTime();
         eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars="+aux+",location=0,statusbar=0,menubar=0,resizable=1,width="+ancho+",height="+largo+",left = 128,top = 233');");
}


function marcafila(id, valor){
		 document.getElementById(id).value  = valor;
}

function asgString(id, id_destino){
         document.getElementById(id_destino).value = document.getElementById(id).value.split('|')[1]; 
}

function showFloatDiv(id, id_div, op, id_destino, num_01, num_02){
		 document.getElementById(id_div).style.left = (findLeftObj(id)-num_01)+'px'; 
		 document.getElementById(id_div).style.top  = (findTopObj (id)+num_02)+'px'; 
		 show(id_div, 2);
		 switch (op){
		         case 1: ir_url ('includes/general/create_cbo.php?id_destino='+id_destino, id_div);	 break; 
				 case 2: ir_url ('cotizaciones/procesos/body_soli.php?sol='+id_destino, id_div);	 break;
				 case 3: ir_url ('includes/general/create_chk.php?id='+id_destino, id_div);	         break;
				 case 4: ir_url ('includes/general/create_cbo2.php?id_destino='+id_destino, id_div); break;
				 case 5: ir_url ('includes/general/create_cbo3.php?id_destino='+id_destino, id_div); break;
		 }
}

function showFloatDivChk(id, id_div, id_destino, tabla, id_ref){
		 document.getElementById(id_div).style.left = ((str_replace('px', '', document.getElementById(id_ref).style.width) / 2)+60)+'px'; 
		 document.getElementById(id_div).style.top  = (findTopObj (id)+25)+'px'; 
		 show(id_div, 2);
		 ir_url ('includes/general/create_chk.php?tabla='+tabla+'&id_destino='+id_destino, id_div);		 
}

function asignaChk(id, id_destino, vect){
	     var chk    = document.getElementById(vect).value.split('-'); // el valor de los ids de los checkbox
         var vari   = id.split('_');                                  // el id de los checkbox
         var cadena = "";
			 
		 for (j=0; j<chk.length; j++)
			  if (document.getElementById(vari[0]+'_'+chk[j]).checked==false)
			      document.getElementById(vari[0]+'_'+chk[j]).checked=false;
			  else{
			      document.getElementById(vari[0]+'_'+chk[j]).checked=true;
				  if (cadena=="")
				      cadena += document.getElementById(vari[0]+'_'+chk[j]).value;
				  else
				      cadena += '-'+document.getElementById(vari[0]+'_'+chk[j]).value;
			  }
		 document.getElementById(id_destino).value = cadena;
}


function hiddenFloatDiv(id){
		 show(id, 1);
}

function asignaValor(id_01, id_02){
		 document.getElementById(id_01).value = document.getElementById(id_02).value;
}

function validaOcultos(ids, id_hidden){
         id = ids.split('|');
         if (document.getElementById(id_hidden).style.display==""){
			 for (i=0; i<id.length; i++){
				  if (document.getElementById(id[i]).value==""){
					  alert(document.getElementById(id[i]).getAttribute('oculto')+' es requerido(a)!\n\n');
					  document.getElementById(id[i]).value="";
					  foco(id[i]);
					  return false;
				  }
			 }
		 }
		 return true;		  
}

function reportes(rpt, titulo, tipo){
		 switch (tipo){
		         case 1: popup(rpt, screen.width, screen.height, 'si'); break;	 
		 } 
}


function totalesCotizacion(id, totales, cant){ //txtTotalPrecio-txtTotalHerra-txtTotal  txtPrecioHerra_  txtPrecio_
	     var total = totales.split('-');
		 var ids   = id.split('-');
		 var sum_precio = 0;
		 var sum_herram = 0;
         for (i=0; i<cant; i++){
			  sum_precio += Number(sinFormato(document.getElementById(ids[0]+'_'+i).value)); 
			  sum_herram += Number(sinFormato(document.getElementById(ids[1]+'_'+i).value)); 
		 }
		 document.getElementById(total[1]).value = numFormat(sum_precio, 3);
		 document.getElementById(total[0]).value = numFormat(sum_herram, 3);
		 document.getElementById(total[2]).value = numFormat(Number(sinFormato(document.getElementById(total[0]).value)) + Number(sinFormato(document.getElementById(total[1]).value)), 3);
}

function sendAjaxFiles(files, divs){
	     file = files.split(','); 
		 div  = divs.split(',');
		 for (i=0; i<div.length; i++)
			  ir_url (file[i], div[i]);
}



/*----------------------------------------- Posiciones */

this.findLeftObj = function(obj) {  
var curleft = 0;  
if (obj.offsetParent) {  
   while (obj.offsetParent) {  
    curleft += obj.offsetLeft  
    obj = obj.offsetParent;  
    }  
}  
else {  
 if(obj.x) {  
   curleft += obj.x;  
  }  
}  
return(curleft);  
}  
           
this.findTopObj = function(obj) {  
var curtop = 0;  
if (obj.offsetParent) {  
    while (obj.offsetParent) {  
        curtop += obj.offsetTop  
        obj = obj.offsetParent;  
    }  
}  
else {  
  if (obj.y) {  
    curtop += obj.y;  
   }  
}  
return(curtop);  
}  
   
	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 muestraSubmenu(curobj, op){
		 if (document.getElementById(curobj)){
		     folder=document.getElementById(curobj).style;
		 }else{
			 if (!document.all||(agtbrw.indexOf('opera')!=-1)){
				 folder=curobj.nextSibling.nextSibling.style;
			 }else{
				 folder=document.all[curobj.sourceIndex+1].style;
		     }
		 }
		 
		 //alert(folder.display+' -- '+op);
		 if (folder.display == "none" && op == '1'){
			 folder.display = "";
		 }else{
			 if (folder.display == "" && op == '0') folder.display = "none";
		 }
}

function comparaClave(id_01, id_02){
	     if (document.getElementById(id_01).value==document.getElementById(id_02).value)
		     return true;
		 else{
			 alert('Las claves no cohinciden, Favor verifique!');
	         return false;
		 }
	}
function compraRapidaAdd(){
	//alert($('nuevosCampos').innerHTML);
	$('cantidad').value=($('cantidad').value*1)+1;
	var i=$('cantidad').value;
	//alert(i);
	$('nuevosCampos').innerHTML= $('nuevosCampos').innerHTML+'<input name="codigo_'+i+'" autocomplete="off" onkeyup="autosuggest(this.id,\'results_'+i+'\');" type="text" id="codigo_'+i+'" class="txt" size="8" /><input name="cant_'+i+'" type="text" id="cant_'+i+'" class="txt" size="3" maxlength="4" onKeyPress="return on_enter(event,1,1,this.id);"  /><span id="results_'+i+'" class="results" style="margin-left:50px;"></span><br/>';

}

/* 
       +-------------------------------------------------------------------------- +
       | Desarrollado por: Miharbi Hernandez                                          | 
	   |         Telefono: 0414-4284230                                            | 
	   |	        Email: gustavoocanto@gmail.com , gustavo.ocanto@hotmail.com    |
	   + ------------------------------------------------------------------------- + 
	   |            Valencia Edo. Carabobo - Venezuela	              			   |
       + ------------------------------------------------------------------------- +
*/
