// JavaScript Document
function EnviarForm2(x){
	x.submit();
}

function EnviarForm(x){
	// Variables
	var total_unidades = 0;

	// Verifica nombre
	if(!x.nombre.value){
		x.nombre.focus();
		alert(msn[2]);
		return false;
	}
	
	// Verifica apellido
	//if(!x.apellido.value){
	//	x.apellido.focus();
	//	alert(msn[12]);
	//	return false;
	//}
	
	// Verifica direccion
	if(!x.direccion.value){
		x.direccion.focus();
		alert(msn[3]);
		return false;
	}
	// Verifica pais
	if(!x.pais.value){
		x.pais.focus();
		alert(msn[6]);
		return false;
	}
	// Verifica ciudad
	if(!x.ciudad.value){
		x.ciudad.focus();
		alert(msn[5]);
		return false;
	}
	// Verifica codigo postal
	if(!x.cp.value){
		x.cp.focus();
		alert(msn[4]);
		return false;
	}
	// Verifica email vacio
	if(!x.email.value){
		x.email.focus();
		alert(msn[7]);
		return false;
	}
	// Verifica email valido
	if(!verifica_email(x.email.value)){
		x.email.focus();
		return false;
	}
	// Verifica telefono
		if(!x.telefono.value){
		x.telefono.focus();
		alert(msn[8]);
		return false;
	}
	// Verifica si fue selecionado algún producto
	for(aux = 0; aux < producto.length; aux++){
		var unidades = parseFloat(document.getElementById("unidad["+aux+"]").value);
		total_unidades = total_unidades + unidades;
	}
	if(total_unidades ==0){
		alert(msn[9]);
		return false;
	}
	x.submit();
}

function verifica_email(x){
	var s = x;

	var filter=/^[A-Za-z]+(\.?)[A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

    if (filter.test(s)){
		return true;
	}else{
		alert(msn[1]);
		return false;
	}
}

function LimpiarForm(x){
	if(confirm(msn[0])){
		// Limpia form
		x.reset();
		// Limpia total
		document.getElementById("total").innerHTML = "0";
	}
}

function total(){
	var total = 0;
	var subtotal = 0;
	for(aux = 0; aux < producto.length; aux++){
		// Recupera la cuantidad de cada producto
		var unidades = parseInt(document.getElementById("unidad["+aux+"]").value, 10);
		if (!isNaN(unidades)){	
			if(unidades < 0){
				document.getElementById("unidad["+aux+"]").value = 0;
				unidades = 0;
				alert(msn[11]);
			}
			document.getElementById("unidad["+aux+"]").value = unidades;
			subtotal = Math.round(unidades*precio[aux]*100)/100
			total = Math.round((total + subtotal)*100)/100; // Calcula el nuevo subtotal
			
			// Escribe los valores nuevos
			//document.getElementById("subtotal["+aux+"]").innerHTML = subtotal; 
		}else{
			document.getElementById("unidad["+aux+"]").value = 0;
			alert(msn[10]);
			return false;
		}
	}
	document.getElementById("total").innerHTML = total;
}

function CommaFormatted(amount)
{
	var delimiter = "."; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}
// end of function CommaFormatted()