// JavaScript Document
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function PonerRojo(iD, NombreCampo){
var nombre = document.getElementById(iD);
nombre.style.borderColor="red";
nombre.style.borderStyle="solid";
nombre.style.borderWidth="1";
if (NombreCampo=="Email"){
  if (!nombre.value) alert(""+NombreCampo+"");
  else alert("Direccion de Email invalida");
} else if (NombreCampo=="Sexo" || NombreCampo=="Pais") {
  alert("Debe elegir una opcion del campo de seleccion "+NombreCampo);
} else {
  alert(""+NombreCampo+"");
}
nombre.focus();
}

function PonerRojoNOJS(iD, NombreCampo){
var nombre = document.getElementById(iD);
nombre.style.borderColor="red";
nombre.style.borderStyle="solid";
nombre.style.borderWidth="1";
nombre.focus();
}

function Normalizar(iD){
// var nombre = document.getElementById(iD);
// var nombre = iD;
iD.style.borderColor="";
iD.style.borderStyle="";
iD.style.borderWidth="";
}
//
//
// Validador del Contacto
//
//
function Validar_contacto(){
  var nombre = document.getElementById("idnombre");
  var email = document.getElementById("idemail");
  var comentarios = document.getElementById("idcomentarios");

Normalizar(nombre);
Normalizar(email);
Normalizar(comentarios);
  if (!nombre.value){
    PonerRojo('idnombre','Nombre');
    return false;
  }
  if (!isEmail(email.value)) {
    PonerRojo('idemail','email');
    return false;
  }
  if (!comentarios.value){
    PonerRojo('idcomentarios','Comentario');
    return false;
  }
}

//
//
// Validador de la consulta
//
//
function Validar_consulta(){
  var nombre = document.getElementById("idnombre");
  var email = document.getElementById("idemail");
  var comentarios = document.getElementById("idcomentarios");

Normalizar(nombre);
Normalizar(email);
Normalizar(comentarios);
  if (!nombre.value){
    PonerRojo('idnombre','El campo Nombre es Obligatorio');
    return false;
  }
  if (!isEmail(email.value)) {
    PonerRojo('idemail','El campo email es Obligatorio');
    return false;
  }
  if (!comentarios.value){
    PonerRojo('idcomentarios','El campo Comentario es Obligatorio');
    return false;
  }
}
//
//
// Validador de la recomendar
//
//
function Validar_recomendar(){
  var nombre = document.getElementById("idnombre");
  var email = document.getElementById("idemail");
  var amigo = document.getElementById("idamigo");
  var emaila = document.getElementById("idemaila");

Normalizar(nombre);
Normalizar(email);
Normalizar(amigo);
Normalizar(emaila);
  if (!nombre.value){
    PonerRojo('idnombre','El campo Nombre es Obligatorio');
    return false;
  }
  if (!isEmail(email.value)) {
    PonerRojo('idemail','El campo email es Obligatorio');
    return false;
  }
    if (!amigo.value){
    PonerRojo('idamigo','El campo Nombre de tu amigo es Obligatorio');
    return false;
  }
  if (!isEmail(emaila.value)) {
    PonerRojo('idemaila','El campo email es Obligatorio');
    return false;
  }
}
//
//
// Validador de aviso gratis y Politicas
//
//
function Validar_politicas(){
  var politicas = document.getElementById("idpoliticas");
Normalizar(politicas);
  if (!politicas.checked){
    PonerRojo('idpoliticas','Para Continuar debe aceptar los Términos y Condiciones');
    return false;
  }
}

//
// Calculo de Peso y de Pliegos
//
//
function pliegos(){
	document.getElementById('idk').checked=0;
	var pliegos = document.getElementById("idcalculo").value;
}
function kilos(){
	document.getElementById('idpleg').checked=0;
	var kilos = document.getElementById("idcalculo").value;
}

var kkk=" Kg";
var ppp=" Pliegos";

function calc(){
	var ggg = document.getElementById("idggg");
    var aaa = document.getElementById("idaaa");
    var lll = document.getElementById("idlll");
	
	if (!ggg.value){
    alert('Todos los campos son Obligatorios');
    return false;
   }
   if (!aaa.value){
    alert('Todos los campos son Obligatorios');
    return false;
   }
   if (!lll.value){
    alert('Todos los campos son Obligatorios');
    return false;
   }
   	var ggg = document.getElementById("idggg").value;
    var aaa = document.getElementById("idaaa").value/1000;
    var lll = document.getElementById("idlll").value/1000;
	
	if (document.getElementById('idpleg').checked) {
	var pliegos = document.getElementById("idcalculo").value;
	total=(ggg*aaa*lll*pliegos)/1000;
	document.getElementById("idtotal").value = total.toFixed(2)+kkk;
	}
	if (document.getElementById('idk').checked) {
	var kilos = document.getElementById("idcalculo").value;
	total=(kilos*1000)/(ggg*aaa*lll);
	document.getElementById("idtotal").value = total.toFixed(2)+ppp;
	}
}
//
//
// Validador de suscriptor
//
//
function Validar_suscribir(){
  var nombre = document.getElementById("idnombre");
  var email = document.getElementById("idemail");

Normalizar(nombre);
Normalizar(email);
  if (!nombre.value){
    PonerRojo('idnombre','El campo Nombre es Obligatorio');
    return false;
  }
  if (!isEmail(email.value)) {
    PonerRojo('idemail','El campo email es Obligatorio');
    return false;
  }
}
//
//
// Validador de cambio de contraseña
//
//
function Validar_cambioclave(){
  var claveactual = document.getElementById("idclaveactual");
  var clavenueva = document.getElementById("idclavenueva");
  var clavenueva1 = document.getElementById("idclavenueva1");

Normalizar(claveactual);
Normalizar(clavenueva);
Normalizar(clavenueva1);
  if (!claveactual.value){
    PonerRojo('idclaveactual','No ha completado el casillero de Contraseña Actual');
    return false;
  }
  if (!clavenueva.value){
    PonerRojo('idclavenueva','No ha completado el casillero de Contraseña Nueva');
    return false;
  }
  if (!clavenueva1.value){
    PonerRojo('idclavenueva1','No ha completado el casillero de Rescritura de Contraseña');
    return false;
  }
    if (clavenueva.value!=clavenueva1.value){
    PonerRojo('idclavenueva1','La Contraseña ingresada no es igual a la ingresada en Nueva Contraseña');
    return false;
  }
}
//
//
// Validador de Pedidos en Promoción
//
//
function Validar_ppromo(){
  var cantidad = document.getElementById("idcantidad");
  var validacalcular = document.getElementById("idvalidacalcular");
Normalizar(cantidad);
  if (!cantidad.value){
    PonerRojo('idcantidad','Debe ingresar una Cantidad a Pedir');
    return false;
  }
  if (!validacalcular.value){
    alert("Debe Calcular el valor antes de Proceder");
    return false;
  }
}
//
// Redondea a entero
//
function Redondea (amount)
{ var val = parseFloat(amount);
if (isNaN(val)) { return "0"; }
if (val <= 0) { return "0"; } else { val= Math.round(val*1)/1}
val = (val == Math.floor(val)) ? val + '' : ((val*10 == Math.floor(val*10)) ? val + '0' : val);
return val;
}
//
// Calculo de Valor en Promociones
//
//
function calcpromocion(){
	var cantidad = document.getElementById("idcantidad");
	if (!cantidad.value){
	document.getElementById("idvalidacalcular").value = "";
    alert('Debe ingresar una Cantidad a Pedir');
    return false;
	}
   val1 = document.getElementById("idcantidad").value * 1;
   val2 = document.getElementById("idcantmax").value * 1;
   //val3 = val1 * 1;
   //val4 = val2 * 1;
   // if (idcantidad.value > idcantmax.value){
   if (val1 > val2){
	document.getElementById("idvalidacalcular").value = "";
    alert('La cantidad ingresada es mayor a la disponible para este producto');
	return false;
   }
   	var calc = document.getElementById("idvalor").value;
	var cantsel = document.getElementById("idcantidad").value;
	total=Redondea ((cantsel*calc));
	moneda="$ ";
	document.getElementById("idtotal").value = moneda + total;
	document.getElementById("idvalidacalcular").value = "1";
}
//
// Calculo de Valor en Promociones KGR
//
//
function calcpromocion1(){
	var cantidad = document.getElementById("idcantidad");
	if (!cantidad.value){
	document.getElementById("idvalidacalcular").value = "";
    alert('Debe ingresar una Cantidad a Pedir');
    return false;
	}
   val1 = document.getElementById("idcantidad").value * 1;
   val2 = document.getElementById("idcantmax").value * 1;
   if (!document.getElementById('idcorte').checked)
   		{
		    if (val1 < 100){
			document.getElementById("idvalidacalcular").value = "";
			alert('La cantidad mínima a pedir es de 100 KGR');
			return false;
		}}
		if (document.getElementById('idcorte').checked) {
			document.getElementById("idvalidacalcular").value = "";
			if (val1 < 250){
			alert('La cantidad mínima a pedir con Corte es de 250 KGR');
			return false;
		}}
   //val3 = val1 * 1;
   //val4 = val2 * 1;
   // if (idcantidad.value > idcantmax.value){
   if (val1 > val2){
    alert('La cantidad ingresada es mayor a la disponible para este producto');
	document.getElementById("idvalidacalcular").value = "";
    return false;
   }
   	var calc = document.getElementById("idvalor").value;
	var cantsel = document.getElementById("idcantidad").value;
	total=Redondea ((cantsel*calc));
	moneda="$ ";
	document.getElementById("idtotal").value = moneda + total;
	document.getElementById("idvalidacalcular").value = "1";
	
}
//
//
// Pedidos Valida
// 
// 
function Validar_ppconfirma(){
  var confirmar = document.getElementById("idconfirmar");
  Normalizar(confirmar);
  if (!confirmar.checked){
    alert ("Debe Confirmar que los datos son correctos para preceder a realizar su pedido");
    return false;
  }
}
//
// Funcion habilitar Con Corte y Gillotina
//
function Con_corte(){
	if (document.getElementById('idlargo').value=="") {
	document.getElementById('idcorte').checked="";
	}	
	else
	{
		val9 = document.getElementById("idlargo").value * 1;
		if (val9 < 100){
				document.getElementById("idlargo").value = "";
				alert('El Largo debe ser expresado en milímetros');
				return false;
			}
	document.getElementById('idcorte').checked="checked";
	}
}
function Guillo(){
	if (document.getElementById('iddescg').value=="") {
	document.getElementById('idguillotina').checked="";
	}	
	else
	{
	document.getElementById('idguillotina').checked="checked";
	}
}
function Con_corte1(){
	if (document.getElementById('idlargo').value=="") {
		document.getElementById('idcorte').checked=""
		alert("Debe ingresar una medida de corte");
		} else {
		document.getElementById('idcorte').checked="checked";
		}
}
function Guillo1(){
	if (document.getElementById('iddescg').value=="") {
		document.getElementById('idguillotina').checked=""
		alert("Debe ingresar una Descripción de Guillotinado");
		} else {
		document.getElementById('idguillotina').checked="checked";
		}
}
//
//
//
// Validador de la consulta
//
//
function Validar_alta(){
  var nombre = document.getElementById("idnombre");
  var cuit = document.getElementById("idcuit");
  var telefono = document.getElementById("idtelefono");
  var email = document.getElementById("idemail");
  var clave = document.getElementById("idclave");
  var clave1 = document.getElementById("idclave1");

Normalizar(nombre);
Normalizar(cuit);
Normalizar(telefono);
Normalizar(email);
Normalizar(clave);
Normalizar(clave1);
  if (!nombre.value){
    PonerRojo('idnombre','El campo Razón Social es Obligatorio');
    return false;
  }
  if (!cuit.value){
    PonerRojo('idcuit','El campo CUIT / CUIL es Obligatorio');
    return false;
  }
  if (!telefono.value){
    PonerRojo('idtelefono','El campo Teléfono es Obligatorio');
    return false;
  }
  if (!isEmail(email.value)) {
    PonerRojo('idemail','El campo email es Obligatorio');
    return false;
  }
  if (!clave.value){
    PonerRojo('idclave','El campo Clave es Obligatorio');
    return false;
  }
  if (clave1.value!=clave.value){
    PonerRojo('idclave1','La Verificación de la clave no coincide con la clave');
    return false;
  }
}
//
// Buscar Productos Pide Ancho
//
function Buscar_productos(){
  var ancho = document.getElementById("idancho");

Normalizar(ancho);
  if (!ancho.value){
    PonerRojo('idancho','Para realizar su búsqueda debe ingresar un ancho en milímetros');
    return false;
  }
}