
function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp; 
}

function Sobre(id)
{
	document.getElementById(id).style.background="#add8f6";
}
	

function valida(valor,id)
{
	if (valor=="") 
		{
		document.getElementById(id+"Failed").style.display="block";
		document.getElementById(id+"Failed").style.font="12px Verdana";	
		document.getElementById(id+"Failed").style.color="#4682B4";
		document.getElementById(id).style.background="#FAFAD2";
		}
		
	else
	    {
		document.getElementById(id+"Failed").style.display="none";
		document.getElementById(id).style.border="1px solid #CCCCCC";
		document.getElementById(id).style.background="#ffffff";
		}
}
		
function IngresaRegistro()
{
		
//  ------------------- Valida el Nombre -------------------------	
	
	Nombre = document.getElementById("txt_Nombre").value;
	if (Nombre=="")
	{
     valida(Nombre,"txt_Nombre");
	 document.getElementById("txt_Nombre").focus();
	 return;  
	}
	
//  ------------------- Valida el Rut -------------------------	
	
	Rut = document.getElementById("txt_Rut").value;
	if (Rut=="")
	{
     valida(Rut,"txt_Rut");
	 document.getElementById("txt_Rut").focus();
	 return;  
	}

//  ------------------- Valida el Email -------------------------	
	
	Email = document.getElementById("txt_email").value;
	if (Email=="")
	{
     valida(Email,"txt_email");
	 document.getElementById("txt_email").focus();
	 return;  
	}
	
//  ------------------- Valida el Contraseña -------------------------	
	Contrasena = document.getElementById("txt_Contrasena").value;
	if (Contrasena=="")
	{
     valida(Contrasena,"txt_Contrasena");
	 document.getElementById("txt_Contrasena").focus();
	 return;  
	}
	
//  ------------------- Valida el Contraseña2 -------------------------	
	
	Contrasena2 = document.getElementById("txt_Contrasena2").value;
	if (Contrasena2=="")
	{
     valida(Contrasena2,"txt_Contrasena2");
	 document.getElementById("txt_Contrasena2").focus();
	 return;  
	}
	
//  ------------------- Valida las 2 contraseñas -------------------------	
	
	if (Contrasena2!=Contrasena)
	{
		id="txt_Contrasena2";
		
		document.getElementById(id+"Failed").style.display="block";
		document.getElementById(id+"Failed").innerHTML = "Error las contrasenas son distintas";
		document.getElementById(id+"Failed").style.font="12px Verdana";	
		document.getElementById(id+"Failed").style.color="#4682B4";
		document.getElementById(id).style.background="#FAFAD2";
		
		document.getElementById("txt_Contrasena").focus();
		return; 
	}
//  ------------------- Valida Direccion -------------------------	
	
	Direccion = document.getElementById("txt_direccion").value;
	if (Direccion=="")
	{
     valida(Direccion,"txt_direccion");
	 document.getElementById("txt_direccion").focus();
	 return;  
	}
	
//  ------------------- Valida Ciudad -------------------------	
	
	Ciudad = document.getElementById("txt_ciudad").value;
	if (Ciudad=="")
	{
     valida(Ciudad,"txt_ciudad");
	 document.getElementById("txt_ciudad").focus();
	 return;  
	}	
	
//  ------------------- Valida Telefono -------------------------	
	
	Fono = document.getElementById("txt_telefono").value;
	if (Fono=="")
	{
     valida(Fono,"txt_telefono");
	 document.getElementById("txt_telefono").focus();
	 return;  
	}	
	
//  ------------------- Valida Lista de Opciones -------------------------	
	
	//alert(document.getElementById("select_sector").value);
	

	Sector =document.getElementById("select_sector").value;
	if (Sector==0)
	{
	valida(Sector,"select_sector");
	
	document.getElementById("select_sectorFailed").style.display="block";
	document.getElementById("select_sectorFailed").style.font="12px Verdana";	
	document.getElementById("select_sectorFailed").style.color="#4682B4";
	document.getElementById("select_sector").focus();
	return;  
	}
	else
	{
	document.getElementById("select_sectorFailed").style.display="none";	
	}
	
	// ----- Variables Opcionales -------------------
	
	Empresa = document.getElementById("txt_empresa").value;
	Departamento = document.getElementById("txt_Departa").value;
	Cargo = document.getElementById("txt_cargo").value;
	Fax = document.getElementById("txt_fax").value;
	Web = document.getElementById("txt_sitioweb").value;
	
	//------------------------------------------------------
	contenedor=document.getElementById("label_informacion");
	
	ajax=nuevoAjax();

	var aleatorio=Math.random();
	
	
	ajax.open("GET","RegistroClientesSql.php?Nombre="+Nombre+"&Rut="+Rut+"&Email="+Email+"&Contrasena="+Contrasena+"&Direccion="+Direccion+"&Ciudad="+Ciudad+"&Fono="+Fono+"&Sector="+Sector+"&Departamento="+Departamento+"&Cargo="+Cargo+"&Fax="+Fax+"&Web="+Web+"&Sector="+Sector+"&Empresa="+Empresa+"&nocache="+aleatorio,true);
	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==1)
			{
				// Mientras carga elimino la opcion "Selecciona Opcion..." y pongo una que dice "Cargando..."
				contenedor.innerHTML="Validando Informacion ...";
				//selectDestino.appendChild(nuevaOpcion); selectDestino.disabled=true;	
			}		
		if (ajax.readyState==4) 
			{
						if (ajax.responseText=='OK')
						   {
							  message = document.getElementById("formulario");
  							// muestra el error o bien oculta el error
  							message.className = "hidden";
							contenedor.innerHTML=" <br /> Usted ha sido registrado en nuestra Base de datos, en breve recibirá un Email para terminar el proceso de inscripción";
						   }
						   
						 if (ajax.responseText=='ERROR: EL EMAIL ES INVALIDO')
						   {
							  //message = document.getElementById("formulario");
  							// muestra el error o bien oculta el error
  							//message.className = "hidden";
							contenedor.innerHTML=" <br /> ERROR: EL EMAIL ES INVALIDO O ESTA MAL ESCRITO";
						   }
						 if (ajax.responseText=='ERROR: EL EMAIL YA ESTA  INGRESADO')
						   {
							  //message = document.getElementById("formulario");
  							// muestra el error o bien oculta el error
  							//message.className = "hidden";
							contenedor.innerHTML=" <br /> ERROR: EL EMAIL YA SE ENCUENTRA AGREGADO EN NUESTRA BASE DE DATOS";
						   }
						
						
	 		}
	}
ajax.send(null);
}

function Loginear()
{
	Username=document.getElementById("textUsername").value;
	Password=document.getElementById("textPassword").value;
		
	//-------------- Valida Username -----------------------
	if (Username=="")
	{
     document.getElementById("textUsername").focus();
	 return;  
	}
	
	//-------------- Valida Username -----------------------
	if (Password=="")
	{
     document.getElementById("textPassword").focus();
	 return;  
	}
	//window.print();
	ajax=nuevoAjax();
	var aleatorio=Math.random();
	contenedor=document.getElementById("TextUsuarioInvalido");
	//ajax.open("GET","listaAtributos.php?nocache="+aleatorio, true);
	ajax.open("GET","loginea.php?Username="+Username+"&Password="+Password+"&nocache="+aleatorio,true);
	ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==1)
				{
				contenedor.innerHTML='Validando Informacion ..';
				}
			if (ajax.readyState==4)
				{
			     if (ajax.responseText=='Error:  El Usuario no ha completado el proceso de registro')
				 	{
						contenedor.innerHTML=ajax.responseText;
						 //return; 
					}
				 if (ajax.responseText=='ERROR: Usuario Invalido o contrasena incorrecta')
				 	{
						contenedor.innerHTML="Usuario Invalido o Contraseña incorrecta";
						
						//ajax.responseText;
						// return; 
					}			    
			    if (ajax.responseText=='OK')
					{
						window.location += "232";
						//alert(window.location);
						window.location.reload();	
					}
				} 
		}
		ajax.send(null);
	}
	
	

	
function Loginear2()
{
	Username=document.getElementById("textUsername2").value;
	Password=document.getElementById("textPassword2").value;
		
	//-------------- Valida Username -----------------------
	if (Username=="")
	{
     document.getElementById("textUsername2").focus();
	 return;  
	}
	
	//-------------- Valida Username -----------------------
	if (Password=="")
	{
     document.getElementById("textPassword2").focus();
	 return;  
	}
	//window.print();
	ajax=nuevoAjax();
	var aleatorio=Math.random();
	contenedor=document.getElementById("TextUsuarioInvalido");
	//ajax.open("GET","listaAtributos.php?nocache="+aleatorio, true);
	ajax.open("GET","loginea.php?Username="+Username+"&Password="+Password+"&nocache="+aleatorio,true);
	ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==1)
				{
				contenedor.innerHTML='Validando Informacion ..';
				}
			if (ajax.readyState==4)
				{
			     if (ajax.responseText=='Error:  El Usuario no ha completado el proceso de registro')
				 	{
						contenedor.innerHTML=ajax.responseText;
						 //return; 
					}
				 if (ajax.responseText=='ERROR: Usuario Invalido o contrasena incorrecta')
				 	{
						contenedor.innerHTML="Usuario Invalido o Contraseña incorrecta";
						
						//ajax.responseText;
						// return; 
					}			    
			    if (ajax.responseText=='OK')
					{
						window.location += "232";
						//alert(window.location);
						window.location.reload();	
					}
				} 
		}
		ajax.send(null);
	}
	
	
	function Loginear3()
{
	Username=document.getElementById("textUsername2").value;
	Password=document.getElementById("textPassword2").value;
		
	//-------------- Valida Username -----------------------
	if (Username=="")
	{
     document.getElementById("textUsername2").focus();
	 return;  
	}
	
	//-------------- Valida Username -----------------------
	if (Password=="")
	{
     document.getElementById("textPassword2").focus();
	 return;  
	}
	//window.print();
	ajax=nuevoAjax();
	var aleatorio=Math.random();
	contenedor=document.getElementById("TextUsuarioInvalido");
	//ajax.open("GET","listaAtributos.php?nocache="+aleatorio, true);
	ajax.open("GET","loginea.php?Username="+Username+"&Password="+Password+"&nocache="+aleatorio,true);
	ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==1)
				{
				contenedor.innerHTML='Validando Informacion ..';
				}
			if (ajax.readyState==4)
				{
			     if (ajax.responseText=='Error:  El Usuario no ha completado el proceso de registro')
				 	{
						contenedor.innerHTML=ajax.responseText;
						 //return; 
					}
				 if (ajax.responseText=='ERROR: Usuario Invalido o contrasena incorrecta')
				 	{
						contenedor.innerHTML="Usuario Invalido o Contraseña incorrecta";
						
						//ajax.responseText;
						// return; 
					}			    
			    if (ajax.responseText=='OK')
					{
						//window.location += "232";
						//alert(window.location);
						//window.location.reload();	
						history.go(-1);
					}
				} 
		}
		ajax.send(null);
	}
	
	
	
	
	
	
function Buscar()
{
	TextABuscar=document.getElementById("TextABuscar").value;
	
		
	//-------------- Valida texto -----------------------
	if (TextABuscar=="")
	{
     document.getElementById("TextABuscar").focus();
	 alert("INGRESE TEXTO");
	 return;  
	}
	
	
	
}



function setFocus()    
{
  document.getElementById("txt_Nombre").focus();
}


function ModificarPrecio()
{
	
	PPrecioSolicitado=document.getElementById("PrecioSolicitado").value;
	
	MMotivo=document.getElementById("Motivo").value;
	
	//-------------------- hidden -----------------------------------
	CCodProductoMod=document.getElementById("CodProductoMod").value;
	DDescProductoMod=document.getElementById("DescProductoMod").value;
	NNombreSolitante=document.getElementById("NombreSolitante").value;
	PPrecioActual=document.getElementById("PrecioActual").value;
	//-------------- Valida Username -----------------------
	if (PPrecioSolicitado=="")
	{
     document.getElementById("PrecioSolicitado").focus();
	 alert("INGRESE PRECIO");
	 return;  
	}
	
	//-------------- Valida Username -----------------------
	if (MMotivo=="")
	{
     document.getElementById("Motivo").focus();
	 alert("INGRESE MOTIVO");
	 return;  
	}
	
	ajax=nuevoAjax();
	var aleatorio=Math.random();
	contenedor=document.getElementById("Mensaje");
	
	
	ajax.open("GET","SolicitudPrecio.php?PrecioSolicitado="+PPrecioSolicitado+"&Motivo="+MMotivo+"&CodProductoMod="+CCodProductoMod+"&DescProductoMod="+DDescProductoMod+"&NombreSolitante="+NNombreSolitante+"&PrecioActual="+PPrecioActual+"&nocache="+aleatorio,true);
	ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==1)
				{
				contenedor.innerHTML='Validando Informacion .....';
				}
			if (ajax.readyState==4)
				{
			        
			    if (ajax.responseText=='OK')
					{
						contenedor.innerHTML="SOLICITUD NOTIFICADA";
						//window.location += "232";
						//alert("SOLICITUD ENVIADA");
						document.getElementById("TablaPrecio").className = "hidden";
						//window.location.reload();	
						//.display="none";
					}
				} 
		}
		ajax.send(null);
	}
