//Abrir ventana nueva tipo popup
function newWindow(url, titulo, height, width) {
nameW='feature'
 if (navigator.appVersion.indexOf('4') != -1) {
 // Vars for centering the new window on Version 4 Browsers
 xTop = (screen.width - width)/2;
 yTop = (screen.height - height)/2;
 //width=800,height=350,scrollbars=yes,resizable=yes
 window.open(url, titulo, 'scrollbars=yes,resizable=yes,height='+height+',width='+width+',menubar=0,toolbar=0,status=0,location=0,directories=0,left=' + xTop + ',top=' + yTop + '');
 }
 else {
 window.open(url, titulo, 'scrollbars=yes,resizable=yes,height='+height+',width='+width+',menubar=0,toolbar=0,status=0,location=0,directories=0,left=150,top=200');
 }
 return false;
}

function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
// only allow 0-9 be entered, plus any values passed
// (can be in any order, and don't have to be comma, period, or hyphen)
// if all numbers allow commas, periods, hyphens or whatever,
// just hard code it here and take out the passed parameters
var checkOK = "0123456789" + comma + period + hyphen;
var checkStr = objName;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0;  i < eval(checkStr + ".value.length");  i++)
{
ch = eval(checkStr + ".value.charAt(i)");
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{	
alertsay = "Favor de capturar valores numericos"
alert(alertsay);
return (false);
}

// set the minimum and maximum
var chkVal = allNum;
var prsVal = parseInt(allNum);
if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
{
alertsay = "Favor de introducir un valor mayor o "
alertsay = alertsay + "igual a \"" + minval + "\" y menor o "
alertsay = alertsay + "igual a \"" + maxval + "\" en el campo \"" + checkStr.name + "\"."
alert(alertsay);
a =  eval(checkStr + ".focus()");
return (false);
}
}

//Ayuda a formatear n�meros en formato de moneda para cultura de am�rica
function parsecurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}

//Calcula totales en carrito de pedidos y cotizaciones, Realiza el calculo en el carrito de compras cuando se modifica la cantidad de articulos en cada registro
function CalculaTotal(IdRegistro)
{
var precio =0;
var descuentoRegistro =0;
var totalRegistro =0;
var cantidad =0;
var subtotal= 0;
var total=0;
var descuento=0;
var descuentoparcial=0;

//Obtenemos el precio, descuento y la cantidad para el registro especificado
precio=eval('document.forma_pedido.Precio' + IdRegistro + '.value'); 
descuentoRegistro= eval('document.forma_pedido.Descuento' + IdRegistro + '.value');
cantidad = eval('document.forma_pedido.Cantidad' + IdRegistro + '.value');
//Calculamos el total de el registro y lo mostramos en pantalla
totalRegistro = (precio -(precio * (descuentoRegistro /100))) * cantidad;
parcial=eval('document.forma_pedido.Totalparcial'+IdRegistro);
//alert(parcial.value);
//alert(totalRegistro);
totalRegistro=parsecurrency(totalRegistro);
//alert(totalRegistro);
parcial.value=totalRegistro ;
//Recalculamos el subtotal
for (i=1; i<= document.forma_pedido.TotalRegistros.value; i++)
{
	var totalparcial=0
	totalparcial=formateanumero(eval('document.forma_pedido.Totalparcial' + i +'.value'));
	subtotal=parseFloat(subtotal) + parseFloat(totalparcial);
}
subtotalcurrency=parsecurrency(parseFloat(subtotal));
document.forma_pedido.Subtotal.value=subtotalcurrency;
//Calculamos el total
descuento=document.forma_pedido.Descuentop.value;
descuento=(descuento/100);
descuentoparcial=parseFloat(subtotal) * descuento;
document.forma_pedido.Descuento.value=parsecurrency(descuentoparcial);
total=parseFloat(subtotal) - (parseFloat(subtotal) * descuento);
document.forma_pedido.Total.value=total;
total=parsecurrency(document.forma_pedido.Total.value);
document.forma_pedido.Total.value=total;
document.forma_pedido.hTotal.value=total;
}

//Validaci�n para checkboxes de selecci�n de categor�as en BC
function seleccionarCat(control){
	var forma = document.forms[0];
	for (i=0; i<forma.length; i++){
		var tempobj=forma.elements[i];
		if (tempobj.type != 'hidden'){
			if (tempobj.type == 'checkbox'){
				var nombrecontrol = tempobj.name;
				if (nombrecontrol.substring(0,6) == 'chkCat' ){
					if (control.checked == true) {
						tempobj.checked = true;
					}
					else 
					{
						tempobj.checked = false;
					}
				}
			}
		}	
	}
}

// Valida forma en base de conocimiento
function validateForm(form) {
	var forma = form;
	var s_arreglo_cat
	s_arreglo_cat = '';
	for (i=0; i<forma.length; i++){
		var tempobj=forma.elements[i];
		if (tempobj.type != 'hidden'){
			if (tempobj.type == 'checkbox'){
				if (tempobj.checked == true) {
					nombrecontrol = new String(tempobj.name)
					if (nombrecontrol.substring(0,6) == 'chkCat' ){
						idcat = nombrecontrol.substr(6)
						if (s_arreglo_cat != ''){
							s_arreglo_cat = s_arreglo_cat + ',' + idcat
						}
						else {
							s_arreglo_cat = idcat
						}
					}
				}
			}
		}	
	}
	forma.hdnarrcat.value = s_arreglo_cat;

  for (var e = 0; e < form.elements.length; e++) {
    var el = form.elements[e];
    if (el.type == 'text' || el.type == 'textarea' ||
        el.type == 'password' || el.type == 'file' ) { 
      if (el.value == '') {
        alert('Por favor, capture un valor v�lido');
        el.focus();
        return false;
      }
    }
    else if (el.type.indexOf('select') != -1) {
      if (el.selectedIndex == -1) {
        alert('Please select a value of the select field ');
        el.focus();
        return false;
      }
    }
    else if (el.type == 'radio') {
      var group = form[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check one of the radio buttons ');
        el.focus();
        return false;
      }
    }
    else if (el.type == 'checkbox') {
      var group = form[el.name];
      if (group.length) {
        var checked = false;
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
        if (!checked) {
          alert('Please check one of the checkboxes ');
          el.focus();
          return false;
        }
      }
    }
  }
  return true;
}