function validade_form () {

var els = $$('.buyinput');
var req='';

var errors = 0;
var errors_str = '';

for (var i=0;i<els.length;i++) {

	if (els[i].name == "ccnumber" && valid_cc(els[i].value) == false) {
		errors++;
		errors_str += ' <img src="images/arrow.png" align="top"> The credit card number is invalid.<br />';
		els[i].style.border = "2px solid #fbb";
	}
	else if (els[i].name == "email" && valid_email(els[i].value) == false) {
		errors++;
		errors_str += ' <img src="images/arrow.png" align="top"> The e-mail entered is invalid.<br />';
		els[i].style.border = "2px solid #fbb";
	}
	else if (els[i].name != "address2" && els[i].value == "") {
		errors++;
		els[i].style.border = "2px solid #fbb";
	}
	else if (els[i].name == "zip" && els[i].value.length != 5) {
		errors++;
		errors_str += ' <img src="images/arrow.png" align="top"> The zip code is invalid.<br />';
		els[i].style.border = "2px solid #fbb";
	}
	else if (els[i].name == "phone" && els[i].value.length < 10) {
		errors++;
		errors_str += ' <img src="images/arrow.png" align="top"> The phone number entered is invalid.<br />';
		els[i].style.border = "2px solid #fbb";
	}
	else if (els[i].name == "cccvv2" && (els[i].value.length < 3 || els[i].value.length > 4)) {
		errors++;
		errors_str += ' <img src="images/arrow.png" align="top"> The CVV2 code entered is invalid.<br />';
		els[i].style.border = "2px solid #fbb";
	}
	else {
	els[i].style.border = "2px solid #aad";
	req += els[i].name + '=' + escape(els[i].value) + '&';
	}

}

req += "r="+Math.random();

if (errors > 0) {

	$('form_errors').update('<img src="images/arrow.png" align="top"> All fields must me filled in.<br />' + errors_str);
	$('form_errors').show();
	return false;
 
 }

 $('form_errors').hide();
 
	$('cc_form').update("<br /><center>We are processing your request, please don't leave this page...</center><br />");
	new Ajax.Request('/includes/paypaldirect.driver.php', {
		parameters: req + '&Qty=' + $F('Qty'),
		onComplete: function(t) { location = t.responseText; },
		onFailure: function(t) { location = '/error.php'; }
	});

}

function valid_cc (n) {
	var sum=0;
	var tmp;
	cc = n.toString();
	if ((cc.length != 16) && (cc.length != 15) && (cc.length != 13)) return false;
	for (var i=1;i<=cc.length;i+=1) {
		tmp = parseInt(cc.charAt(cc.length-i));
		tmp *= (i % 2 == 0) ? 2 : 1;
		tmp -= (tmp > 9) ? 9 : 0;
		sum += tmp;	
	}
	return (sum % 10 == 0) ? true : false;
}

function valid_email (e) {
	return (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(e));
}

function paybypaypal() {
	$('paypal_form').update("<br /><center>You will be redirected to PayPal in a few seconds...</center><br />");
	location = '/includes/paypalexpress.driver.php?Qty=' + $F('Qty');
}

//only allow numbers to be typed
function validateKeyPress(evt, obj) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if(charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	return true;
}

function setPrice() {
	if(isNaN($F('Qty'))) $('Qty').value = '1';
	else if($F('Qty') > 25) $('Qty').value = '25';
	
	if($F('Qty') <= 3) var Ship = 4.8;
	else if($F('Qty') >= 4 && $F('Qty') <= 15) var Ship = 10;
	else if($F('Qty') >= 16 && $F('Qty') <= 25) var Ship = 15;
	
	var Price = (4.85 * $F('Qty')) + Ship;
	$('Shipping').innerHTML = Ship.toFixed(2);
	$('Total').innerHTML = Price.toFixed(2);
}

function validateBlur() {
	if($F('Qty') == 0) $('Qty').value = '1';
	setPrice();
}
