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


////////
////////


function submitConfig(control,ventanaOrigen){
//alert(control);
	    var ventMod=parent.document.getElementById(ventanaOrigen);
	    
	    
	    if(control=="registro"){
	   
	    var frame=parent.document.getElementById(control+"_frame");
	    //frame.src="http://201.122.47.6:8120";
	    frame.src="./tienda/registro.jsp?contrl=regisDone";
	  
	    
	    }else{
	   
	       var frame=parent.document.getElementById(control+"_frame");
	       frame.src="usuarioConfig.midiat?operacion=mostrarCatalogo"+control;
	       ventMod.style.display="none";
	       }
	  
	    
}





function validacionUsuario(ventana, form, accion1, width, height){
	function OnSubmit(obj){
		form.submit();
	  }
	//alert(accion1);
	if(accion1=="insertarUsuario" ||  accion1=="modificarUsuario" ){
		
		

	    if(form.usuario.value==""){
	      alert("Usuario vacio!");
	    }else if(form.password.value==""){
	      alert("Contraseña vacio!");
	    }else if(form.rePassTxt.value==""){
	      alert("reContraseña vacio!");
	    }else if(document.getElementById("rol").value=="-1"){
	      alert("Rol vacio!");
	    }else if(document.getElementById("almacen").value=="-1"){
	      alert("Almacen vacio!");
	    }else if(document.getElementById("nombre").value==""){
	      alert("Nombre vacio!");
	    }else if(document.getElementById("apellidoP").value==""){
	      alert("Apellido vacio!");
	    }else if(document.getElementById("password").value!=document.getElementById("rePassTxt").value){
	      alert("Contraseña Incorrecta!");
	    }else if(document.getElementById("email").value==""){
	      alert("Correo vacio!");
	    }
	    else{

	    form.accion.value=accion1;
		form.submit();
	    
	    }

	    } else if(accion1="registrarUsuario"){
	    	
	    	if(form.usuario==null){
				 if(document.getElementById("nombre").value=="" || document.getElementById("nombre").value=="--Nombre--" ){
				      alert("Proporciona un nombre");
				    }else if(document.getElementById("email").value=="" || document.getElementById("email").value=="---Correo---" || document.getElementById("email").style.background=="#f7c8c4" ){
					      alert("Proporciona una direccion de correo valida");
				    }
			else{
				       
					    form.accion.value=accion1;
						//form.submit();
						var ventanaObjII=parent.document.getElementById("paredOpacaDiv");
						var ventanaObj=parent.document.getElementById("ventanaRegistro");
					       new Effect.Fade(ventanaObjII, { duration:0.5, from:0.7, to:0, transition: Effect.Transitions.linear});
					       new Effect.Fade(ventanaObj, { duration:0.5, from:1, to:0, transition: Effect.Transitions.linear, afterFinish: OnSubmit });
					    }
	    }
	  
	}
}


function validaRoles(form,accion1,idLink1){
	var roles = document.getElementsByName("perms");
	var bandera = "false";
	if(roles){
		for(i = 0; i < roles.length; i++){
			if(roles[i].checked){
				bandera = "true";
				break;
			}
		}
	}
	if(bandera == "false"){
		document.getElementById("errorRoles").style.visibility = "visible";
		document.getElementById("errorRoles").style.display = "block";
	} else {
	
	form.accion.value=accion1;
	form.idLink.value=idLink1;
	form.submit();

		//validaVentana('ventanaCatRol',430,280);
		//submitVentana(form,accion,idLink,null);
	}
}

function checkPass(obj1,obj2){
  //alert("satan te invade");
  
  var passTxt=document.getElementById(obj1);
  var rePassTxt=document.getElementById(obj2);
  
  
  if(passTxt.value==rePassTxt.value){
     // alert("oK!");
      //passTxt.style.background="#dcebd2";
      rePassTxt.style.background="#e3f1d9";
  }else{
    rePassTxt.style.background="#f7c8c4";
  
  }

}

function passFlush(obj1,obj2){
     var passTxt=document.getElementById(obj1);
     var rePassTxt=document.getElementById(obj2);
     
     if(passTxt.value!=rePassTxt.value){
     
     passTxt.value="";
      rePassTxt.value="";
     
     }

}


function validarCorreo(obj1){
   var cad=document.getElementById(obj1).value;
   var mailTxt=document.getElementById(obj1);
   
   //mailTxt.style.background="#f7c8c4";
 
   
   
   var patronEmail=/(^[0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$/;

   if (patronEmail.test(cad)){
	   mailTxt.style.background="#e3f1d9";
     // document.getElementById(obj2).innerHTML="oK";
   }else{ 
  // document.getElementById(obj2).innerHTML="X";
	   mailTxt.style.background="#f7c8c4";
   
   }
   

}

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




function procesaTipoProd(){
 if(http_request.readyState == READY_STATE_COMPLETE) {
      if (http_request.status == 200) {
 
         document.getElementById("edoSelect").innerHTML="";
             //var temas=eval("("+obj.respuestaTexto()+")");
             var temas=eval("("+http_request.responseText+")");
            // alert(http_request.responseText);
            // var temas = http_request.responseTEXT;
             //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);
             }
         }
          }
      }
     }    

