// JavaScript Document

function redireccionar(url){
	 document.frm.action=url;
	 document.frm.submit();
}

function get_array(name)
{var qstr = "";
 var el = document.getElementsByName(name); 	
	for(j=0; j < el.length; j++)
	{qstr += escape(el[j].value)+';';
	}
	qstr = qstr.substring(0,qstr.length - 1);

	return qstr;
}

function check_for_num(obj,val){
	if(val=="" || isNaN(val) || val < 1 ){
	  alert("Ingresar un valor apropiado");
	  obj.value=1;
	}
}

function onlyDigit(e,obj){
        var tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==8 || tecla==0) return true;
        patron = /[0123456789]/;
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function onlyNumber(e,obj){
        var tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==8 || tecla==0) return true;
        patron = /[.0123456789]/;
	te = String.fromCharCode(tecla);
	return patron.test(te);
}

function isEmailAddress(name){
var s = document.getElementById(name).value;
var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
if (s.length == 0 ) return true;
if (filter.test(s))
return true;
else
document.getElementById(name).focus();
return false;
}

function validar_dis(){

  var error="";
  
  if(document.getElementById("empresa").value==""){
   error+="Ingresar Empresa\n";   
  }
  if(document.getElementById("ruc").value==""){
   error+="Ingresar RUC\n";   
  }
  if(document.getElementById("email").value==""){
   error+="Ingresar Email de empresa\n";   
  }else{
	 if(!isEmailAddress('email')) error+="Ingrese una dirección de correo válida";
  }
 
  if(error!=""){
   alert(error);
  }else{
   document.frm.action="datos_dist.php";
   document.frm.submit();
  }
 }

 function validar_login(){
	var user = document.getElementById("user").value;
	var pass = document.getElementById("pass").value;

	if(user==""){
		alert("Ingresar Usuario");
		document.getElementById("user").focus();
	}else{
	   if(pass==""){
		   alert("Ingresar Password");
		   document.getElementById("pass").focus();
	   }else{
		   document.frmLogin.action="login.php";
		   document.frmLogin.submit();
	   }
	}
 }

 function validar_cont(){

  var error="";

  if(document.getElementById("nombrecont").value==""){
   error+="Ingresar Nombre\n";   
  }
  if(document.getElementById("emailcont").value==""){
   error+="Ingresar Email\n";   
  }else{
	if(!isEmailAddress('email')) error+="Ingrese una dirección de correo válida";
  }
  if(document.getElementById("temacont").value==""){
   error+="Ingresar Comentario\n";   
  }

  if(error!=""){
   alert(error);
  }else{
   document.frm.action="datos.php";
   document.frm.submit();
  } 
 }
 
 function limpiar_menu(div){
	 document.getElementById(div).innerHTML = '';
 }

 function validar_prod(){	
	 document.frmBus.action='producto.php';
	 document.frmBus.submit();	
 }
 
 function validar_prodcot(){	
	 document.frm.action='cotizar.php';
	 document.frm.submit();	
 }

 function colocar_producto(id,name,price,stock,fecha){
	if(stock=='') stock = 0;
	if(stock>0){
     if(stock>=document.getElementById("cantidad_" + id).value){
	  document.getElementById("id_prod").value = id;
	  document.getElementById("name_prod").value = name;
	  document.getElementById("price_prod").value = price;
	  document.getElementById("stock_prod").value = stock;
	  document.getElementById("fecha_prod").value = fecha;
	  document.getElementById("cant_prod").value = document.getElementById("cantidad_" + id).value;
	  //document.frm.action = "carrito/mete_producto.php";
	  document.frm.action = "cotizar.php";
	  document.frm.submit();
	 }else{
	  alert("La cantidad es mayor que el stock actual"); 
	 }
	}else{
	  alert("No hay stock para este producto");
	}
 }
 
 function buscar_producto(){
	if(document.getElementById("id_prod").value==""){
     alert("Ingresar Item");
	 document.getElementById("id_prod").focus();
	}else{
	 document.frm.action = "cotizar.php";
	 document.frm.submit();
	}
 }
 
 function buscar_prod(){
	var cat = document.getElementById("cat").value;
	var subcat = document.getElementById("subcat").value;
	var fab = document.getElementById("fabrica").value;
	
	document.location.href = 'producto.php?cat_id=' + cat + '&subcat_id=' + subcat + '&fabrica=' + fab;
}
 
 function validar_loginadm(){
	if(document.getElementById("user").value==""){
	 alert("Ingresar Usuario");
	 document.getElementById("user").focus();
	}else{
	 if(document.getElementById("pass").value==""){
	  alert("Ingresar Contraseña");
	  document.getElementById("pass").focus();
	 }else{
	   document.frmLogin.submit(); 
	 }
	}
 }

 function validar_contra(){
 if(document.frm.contrasena.value==""){
	 alert("Ingresar contraseña");
	 document.frm.contrasena.focus();
 }else{
   if(document.frm.contrasena.value!=document.frm.contrasena_nueva.value){
	   alert("Contraseña Incorrecta");
	   document.frm.contrasena_nueva.focus();
   }else{
	   document.frm.action="graba_contra.php";
	   document.frm.submit();
   }
 } 
}

function validar_usu(){
 var codigo=document.frm.codigo.value;

 if(document.frm.nombre.value==""){
	alert("Ingrese Nombre");
	document.frm.nombre.focus();
 }else{
 if(document.frm.user.value==""){
	alert("Ingrese Usuario");
	document.frm.user.focus();
 }else{
 if(document.frm.pass.value==""){
	 alert("Ingresar contraseña");
	 document.frm.pass.focus();
 }else{
   if(document.frm.pass.value!=document.frm.pass1.value){
	   alert("Contraseña Incorrecta");
	   document.pass1.focus();
   }else{
	   document.frm.action="grabar.php?tabla=1&codigo=" + codigo;
	   document.frm.submit();
   }
 } 
 }
 }
}

function validar_user(){
 var codigo=document.frm.codigo.value;

 if(document.frm.nombre.value==""){
	alert("Ingrese Nombre");
	document.frm.nombre.focus();
 }else{
  if(document.frm.ruc.value==""){
	alert("Ingrese Ruc");
	document.frm.ruc.focus();
  }else{
   if(document.frm.pass.value==""){
	 alert("Ingresar contraseña");
	 document.frm.pass.focus();
   }else{
    if(document.frm.pass.value!=document.frm.pass1.value){
	   alert("Contraseña Incorrecta");
	   document.pass1.focus();
    }else{
	   document.frm.action="grabar.php?tabla=4&codigo=" + codigo;
	   document.frm.submit();
    }
   } 
  }
 }
}

function validar_not(){
 var codigo=document.frm.codigo.value;
 
 tinyMCE.triggerSave();
 tinyMCE.triggerSave();	
 
 if(document.frm.titulo.value==""){
	alert("Ingrese Titulo");
	document.frm.titulo.focus();
 }else{
 if(document.frm.fecha.value==""){
	alert("Ingrese Fecha");
	document.frm.fecha.focus();
 }else{
 if(document.getElementById("descripcion").value==""){
	 alert("Ingresar Descripcion");
	 document.getElementById("descripcion").focus();
 }else{  
	   document.frm.action="grabar.php?tabla=2&codigo=" + codigo;
	   document.frm.submit();
 } 
 }
 }
}

function eliminar(codigo,tabla){
  if(confirm('Desea borrar el registro?')) location.href="borrar.php?codigo=" + codigo + "&tabla=" + tabla;
}

function buscar_coti(){
	var cat = document.getElementById("cat").value;
	var subcat = document.getElementById("subcat").value;
	
	document.location.href = 'shopping.php?cat_id=' + cat + '&subcat_id=' + subcat;
}

function descargar(form,url,img){
	form.action = url;
	form.f.value = img;
	form.submit();
}
