// JavaScript Document for Permacultura México

function RevisaForma(frm){
	var bandera = true;
	for(i=0; i<frm.elements.length; i++){
		if(frm.elements[i].type == 'text'){
			if(frm.elements[i].value == ''){
				alert('Falta llenar el campo: '+ frm.elements[i].id);
				bandera = false;
			}
		}
	}
	return bandera;
}

function MM_jumpMenu(targ,selObj,restore){
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore){ 
		selObj.selectedIndex = 0;
	}
}

function Submit(frm, op, selObj){
	op.value = selObj.options[selObj.selectedIndex].value;
	frm.submit();
}

function ChangeDisplay(str){
	document.getElementById("mainImage").src = str;
}

function mostrardiv() {
	div = document.getElementById('FirmasDIV');
	div.style.display = 'block';
}
function cerrar() {
	div = document.getElementById('FirmasDIV');
	div.style.display='none';
}

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
<!-- Un Click
var usuario_escribir_envio = false;
var msj_un_click_suficiente = 'Un click es suficiente, gracias.';
	// Alta Forma
function usuario_alta_click(){
	if(usuario_escribir_envio == false){
		usuario_escribir_envio = true;
		//form.submit();
	} else {
		alert(msj_un_click_suficiente);
	}
}
//-->


function calcula_totales() {
	var Descuento = document.form1.Descuento.value;
	var Precio = document.form1.Precio.value;
	var Cantidad = document.form1.Cantidad.value;
	PrecioN = Precio * Cantidad;
	PrecioDesc = Precio - Descuento;
	Totaldesc = Descuento * Cantidad;
	Subtotal = PrecioDesc * Cantidad;
	document.form1.Subtotal.value = "$ " + roundToPennies(PrecioN, true) + " MX";
	document.form1.Total.value = "$ " + roundToPennies(Subtotal, true) + " MX";
	document.form1.Descuento_Total.value = "$ " +  roundToPennies(Totaldesc, true) + " MX";
	
	
	//--
	document.form1.Subtotal1.value = roundToPennies(PrecioN, true);
	document.form1.Total1.value = roundToPennies(Subtotal, true);
	document.form1.Descuento_Total1.value = roundToPennies(Totaldesc, true);
	document.form1.Cantidad1.value = Cantidad;
	
}

function roundToPennies(n, coma)
{
   pennies = n * 100;
   pennies = Math.round(pennies);
   strPennies = "" + pennies;
   len = strPennies.length;
   first = strPennies.substring(0, len - 2) + ".";
   last = strPennies.substring(len - 2, len);
   if(first == ".")
   {
      first = "0."
   }
   if(last.length == 1)
   {
      last += "0";
   }
   if(first.length >4 && coma == true)
   {
      len = first.length;
      s = first;
      first = s.substring(0, len-4) + "," + s.substring(len-4, len);
   }
   return first + last;
}

function selecciona_value(objInput) { 

    var valor_input = objInput.value; 
    var longitud = valor_input.length; 

    if (objInput.setSelectionRange) { 
        objInput.focus(); 
        objInput.setSelectionRange (0, longitud); 
    } 
    else if (objInput.createTextRange) { 
        var range = objInput.createTextRange() ; 
        range.collapse(true); 
        range.moveEnd('character', longitud); 
        range.moveStart('character', 0); 
        range.select(); 
    } 
} 

function invisible(str){
	document.getElementById(str).style.visibility = 'hidden';
}
function visible(str){
	document.getElementById(str).style.visibility = 'visible';
}
function show(str){
	document.getElementById(str).style.display = 'block';
}
function hide(str){
	document.getElementById(str).style.display = 'none';
}

