var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
var http_request = getXMLHTTPRequest();

function getXMLHTTPRequest() {
   if (window.XMLHttpRequest) {
       return new XMLHttpRequest();
    }else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}


function ejecutaAjax(url,accion,funcion){
   url=url+accion;
      if(http_request) {
	     http_request.open('GET', url, true);
	     http_request.send(null);
	     http_request.onreadystatechange = funcion;
      }
} 


var anchoTotal=screen.availWidth;
var altoTotal=screen.availHeight;

function contraerDiv(nombreDiv){

	var divObj = document.getElementById(nombreDiv);
	if(divObj.style.display=="none"){
		divObj.style.display="block";
	}else{
		divObj.style.display="none";
	}
} 

function muestraOcultaFrame(nombreDiv,url){
  var divObj = document.getElementById(nombreDiv+"_div");
  var frameObj = document.getElementById(nombreDiv+"_frame");

  
	if(divObj.style.display=="none"){
		divObj.style.display="block";
	}else{
		divObj.style.display="none";
	}
}

function cargaFramesInicial(url0,url1){

     var frameList=document.getElementsByTagName("IFRAME");
     var res=frameList[0].src
     alert(frameList.length);
     
   //  for(i=0;i==frameList.length;i++){
   
         frameList[0].src=url0;
         frameList[1].src=url1;
          
         // setTimeout("document.location.href='http://www.google.es';",5000);
        // cargaFramesInicial(url0,url1);
         
     
    // }

}


var transicionBackground = null;
var transicionColor = null;
function seleccionarRenglon(indice) {
	var renglon = document.getElementById("id"+indice);
	transicionBackground = renglon.style.backgroundColor;
	transicionColor = renglon.style.color;
	renglon.style.backgroundColor = "#daa25a";
	renglon.style.color = "#FFFFFF";
}

function deseleccionarRenglon(indice) {
    var renglon = document.getElementById("id"+indice);
	renglon.style.backgroundColor = transicionBackground;
	renglon.style.color = transicionColor;
}

function cambiarRollover(categoria,id) {  
	fotoCatego = document.getElementById(id);
	if (categoria=="ins"){
		fotoCatego.src = "imagenes/botones/insertarOver.png";
	}else if (categoria=="insBorrar") {
		fotoCatego.src = "imagenes/botones/insertarUp.png";
	}else if (categoria=="bor") {
		fotoCatego.src = "imagenes/botones/borrarOver.png";
	}else if (categoria=="borBorrar") {
		fotoCatego.src = "imagenes/botones/borrarUp.png";
	}else if (categoria=="AceptarOver") {
		fotoCatego.src = "imagenes/botones/btnAceptarOver.png";
	}else if (categoria=="AceptarOut") {
		fotoCatego.src = "imagenes/botones/btnAceptarUp.png";
	}else if (categoria=="catalogoOver") {
		fotoCatego.src = "imagenes/botones/catalogoOver.png";
	}else if (categoria=="catalogoOut") {
		fotoCatego.src = "imagenes/botones/catalogoUp.png";
	}else if (categoria=="posOver") {
		fotoCatego.src = "imagenes/botones/posOver.png";
	}else if (categoria=="posOut") {
		fotoCatego.src = "imagenes/botones/posUp.png";
	}else if (categoria=="usuariosOver") {
		fotoCatego.src = "imagenes/botones/usuariosOver.png";
	}else if (categoria=="usuariosOut") {
		fotoCatego.src = "imagenes/botones/usuariosUp.png";
	}else if (categoria=="inventarioOver") {
		fotoCatego.src = "imagenes/botones/inventarioOver.png";
	}else if (categoria=="inventarioOut") {
		fotoCatego.src = "imagenes/botones/inventarioUp.png";
	}else if (categoria=="ventasOver") {
		fotoCatego.src = "imagenes/botones/ventasOver.png";
	}else if (categoria=="ventasOut") {
		fotoCatego.src = "imagenes/botones/ventasUp.png";
	}else if (categoria=="devoOver") {
		fotoCatego.src = "imagenes/botones/devoOver.png";
	}else if (categoria=="devoOut") {
		fotoCatego.src = "imagenes/botones/devoUp.png";
	}else if (categoria=="modificarOver") {
		fotoCatego.src = "imagenes/botones/modificarOver.png";
	}else if (categoria=="modificarOut") {
		fotoCatego.src = "imagenes/botones/modificarUp.png";
	}							
}//EndFUNCTION
            
function quitarCombo(form){
	  var combo = document.getElementById("comboAlmacen");
      if (combo) {
      	if(combo.options){
      		form.comboAlmacen.style.visibility="hidden";
      	}
      }
      var mes = document.getElementById("comboMes");
      if(mes){
      	if(mes.options){
     		form.comboMes.style.visibility="hidden";
     	}
     }
}

function habilitaComboMes(form){
	var combo = document.getElementById("comboAlmacen");
      if (combo) {
      	if(combo.options){
      		if(combo.selectedIndex != -1){
      			var mes = document.getElementById("comboMes");
      			if(mes){
	     			form.comboMes.disabled="";
    	 		}
      		}
      	}
    } 
}

function verificarCheck(form, permisos){
	var valorCheck = document.getElementsByName("perms");
	if (permisos && valorCheck) {
			for (i = 0; i < valorCheck.length; i++) {
				for (j = 0; j < permisos.length; j++) {
					if (valorCheck[i].value == permisos[j]) {
						form.perms[i].checked="checked";
						break;
					}
				}
			}
	}
}

function checkEnter(form,event) { 	
	var code = 0;
	if (event && event.which) {
		code = event.which;
	} else if (window.event && window.event.keyCode) {
		code = window.event.keyCode;
	}
	if (code==13)
		submitForm(form,'autentificaUsuario','');
}

function telcaNumerica(obj1){
	   $('.'+obj1).bind('keypress', function(e) {
		   var code = (e.keyCode ? e.keyCode : e.which); 
		   
			if(code >= 48 && code <= 57 || code==8)
			  	return true;
		  	else
				return false;
		     });
}

function AceptaSoloTeclaNumerica(event){

	if (!event) var event = window.event;
	if(event.keyCode >= 48 && event.keyCode <= 57)
	  	event.returnValue = true;
  	else
		event.returnValue = false;
}

function eliminaCatalogo(form){
	var check = document.getElementsByName("checks");
	if(check){
		var ids = "";
		for(i = 0; i < check.length; i++){
			if(check[i].checked){
				ids += check[i].value + ",";
			}
		}
		ids = ids.substring(0,ids.length-1);
		if(ids == ""){
			alert("No has seleccionado ningun registro...");
		}else{
			if(confirm("Estas seguro que deseas eliminar los registros seleccionados?")){
				form.accion.value = "eliminarRegistros";
				form.idLink.value = ids;
				form.submit();
				
			}
		}
	}
}

function obtenCalendario(field){
 Calendar.setup({
					        inputField     :    field,
					        ifFormat       :    "%d/%m/%Y",
					        button         :    field,
					        align          :    "Br", 
					        weekNumbers	   :	false,
					        showOthers     :    true,
					        singleClick    :    true
					    });
}


function mostrarDiv(div1,div2){
       var divMostrar = document.getElementById(div1);
       var divOcultar = document.getElementById(div2);
       document.body.style.cursor="pointer";
       with(divMostrar.style){
			display="block";
			}
	   with(divOcultar.style){
			display="none";
			}

}

function ocultarDiv(){
       var divOcultar = document.getElementById(div);
       with(divOcultar.style){
			display="none";
			}
}

function teclaSumarRestar(idMerc,ctrl){
         // alert(ctrl);
          document.onkeydown = checkKeycode;	
       function checkKeycode(e) {
             var keycode;
             if (window.event) keycode = window.event.keyCode;
             else if (e) keycode = e.which;
             
             //restar(189,109="-")
             if (keycode == 189 || keycode == 109){
              eliminaExistencia(idMerc,ctrl);
          
             }
             //restar(187,107="+")
             else if (keycode == 187 || keycode == 107){
              agregaExistencia(idMerc,ctrl);
           
             }
             }
}

var Capa1 = document.getElementById("divAscensor");
            
function mostrarCarrito(div){
       var carritoDiv = document.getElementById(div);
       with(carritoDiv.style){
			visibility="visible";
			display="block";
   }
}


var muestraOculta=0;
function desplazarPrueba(div){
     var divObj=document.getElementById(div);
   
     if(muestraOculta==0){
      if(divObj.style.position=="absolute"){
        divObj.style.left="1px";
       divObj.style.top="-17px";
       divObj.style.position="relative";
        }else{
       divObj.style.left="-170px";
       divObj.style.top="95px";
       divObj.style.position="absolute";
       muestraOculta=1;
       
      }
     }else if(muestraOculta==1){
        
       divObj.style.left="1px";
       divObj.style.top="-17px";
       divObj.style.position="relative";
       muestraOculta=0;
     }
}

function desacoplarPanel(div){
      var divObj=document.getElementById(div);
       divObj.style.left="1px";
       divObj.style.top="113px";
       divObj.style.position="absolute";
      
}



function agregarCategoSubmit(form,obj1,obj2,accion){
   
     var res=document.forms[0].categoTxt.length;
     var valorDesc=document.getElementById(obj1).value;
     var descText=document.getElementById("descTxt").value;
     //var categoValue=document.forms[0].categoTxt[0].value;
    // alert(res);
    var listaVacios=new Array();
    
   if(accion=="modificarCatalogo"){
       var html1="";
   }else if(accion=="insertarCatalogo"){
       var html1=descText+",";
   }
   
   
   if(res==null){
    res=1;
    if(document.forms[0].categoTxt.value==""){
       html1+="sinCategos";
       alert(html1);
       form.accion.value=accion;
	   form.cadena.value=html1;
	   form.submit();
    }else{
    html1+=document.forms[0].categoTxt.value;
    alert(html1);
    form.accion.value=accion;
	form.cadena.value=html1;
	form.submit();	
    }
    }else{
    
    //alert(res);
    
     var iter=0;
     for(i=0;i<res;i++){
    // if(!document.forms[0].categoTxt[i].value==""){
       //concatenar
       html1+=document.forms[0].categoTxt[i].value + ",";
        if(document.forms[0].categoTxt[i].value==""){
        iter++;
        
        }
     
       
     }
     
     
     if(iter==res){
       html1="sinCategos,";
       }
     alert(html1.substring(0, html1.length-1));
     form.accion.value=accion;
	 form.cadena.value=html1.substring(0, html1.length-1);
	 form.submit();	
     }
   
 
}

function cropFrame(obj1,obj2){
	
	var ras=document.getElementById(obj1);
    
  
    ras.height=obj2;
	
	
}

function agregarCategoTxt(categoActuales,form,div){
   var res=document.forms[0].categoTxt.length;
   //var valor=document.forms[0].categoTxt[0].value;
   var resultDiv=document.getElementById(div);
   var lista=new Array();
 
 
 textohtml="<table>";
 
  if(res==null){
  res=1;
  lista[0]=document.forms[0].categoTxt.value;
  //alert(lista[0]);
  }else{
  
   
   var i=0;
   for(i=0;i<res;i++){
     lista[i]=document.forms[0].categoTxt[i].value;
   
   }
   
   }
 
   i=0;
   for(i=0;i<res+1;i++){
   
     textohtml+="<tr>";
    
     if(lista[i]==null){
         textohtml+="<td width='20px' align='right'>";
         textohtml+=i+1+"</td>";
         textohtml+=" <td> <input type='text' id='categoTxt' size='13' value=''/> </td>";
     
     }else{
         textohtml+="<td width='20px' align='right'>";
         textohtml+=i+1+"</td>";
         textohtml+=" <td> <input type='text' id='categoTxt' size='13' value='"+lista[i]+"'/> </td>";
       
     }
   
     textohtml+="</tr>";
    
   }
   textohtml+="</table>";
   resultDiv.innerHTML=textohtml;   

}

function eliminaCategoTxt(form,div){
   var res=document.forms[0].categoTxt.length;
   var valor=document.forms[0].categoTxt[0].value;
   var resultDiv=document.getElementById(div);
   
  if(res!=1){
   var lista=new Array();
   textohtml="<table>";
   var i=0;
   for(i=0;i<res;i++){
   lista[i]=document.forms[0].categoTxt[i].value;
   }
  
   i=0;
   for(i=0;i<res-1;i++){
   
     textohtml+="<tr>";
     textohtml+="<td>";
     if(lista[i]==null){
         textohtml+="<td width='20px' align='right'>";
         textohtml+=i+1+"</td>";
         textohtml+=" <td> <input type='text' id='categoTxt' size='13' value=''/> </td>";
        
     
     }else{
         textohtml+="<td width='20px' align='right'>";
         textohtml+=i+1+"</td>";
         textohtml+=" <td> <input type='text' id='categoTxt' size='13' value='"+lista[i]+"'/> </td>";
         
     }
     textohtml+="</td>";
     textohtml+="</tr>";
    
   }
   textohtml+="</table>";
   resultDiv.innerHTML=textohtml;   
   }
}

function textVacio(obj1,obj2){
     var res=document.forms[0].categoTxt.length;
     
  if(res==null){
      res=1;
  
      if(document.forms[0].categoTxt.value==""){
          document.forms[0].categoTxt.value=obj2;
          }
     
     }else{
     
     if(document.forms[0].categoTxt[obj1].value==""){
        document.forms[0].categoTxt[obj1].value=obj2;
     }
     }

}

function cargarPass(){
  var usuarioText=document.getElementById("usuarioTxt");
  var passText=document.getElementById("passTxt");
  
  usuarioText.value="xgress";
  passText.value="cxggcxgg";

}

var obj;
function cargaEstados(){
   var paisSel=parent.document.getElementById("paisSelect");
   var edoSel=parent.document.getElementById("edoSelect");
   alert(paisSel.value);
   
   obj=new ObjetoAJAX();
   obj.enviar("UsuarioServlet?accion=cargaEstados&pais="+paisSel.value,"GET","procesaTipoProd", null); 




function procesaTipoProd(){
         //obj.respuestaTexto === JSON {id:"ID_TIPO_PRODUCTO", nombre:"DESCRIPCION"}
         document.getElementById("edoSelect").innerHTML="";
             var temas=eval("("+obj.respuestaTexto()+")");
             //crea la opci�n de selecci�n inicial
             var opcionInicio=document.createElement("option");
             var textoOpcion=document.createTextNode("-seleccione");
             opcionInicio.appendChild(textoOpcion);
             var lista=document.getElementById("edoSelect");
             lista.appendChild(opcionInicio);
             //genera el resto de opciones de la lista a partir 
             //de los datos recibidos en la respuesta
             for(var elm = 0;elm < temas.length;elm++){
                    var item=document.createElement("option");
                    item.setAttribute("value",temas[elm].estado);
                    var texto=document.createTextNode(temas[elm].estado);
                    item.appendChild(texto);
                    document.getElementById("edoSelect").appendChild(item);
             }
         }
         }
