function isNumeric(form_value) 
{ 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
} 

var jsProduct = function(id,eur,gbp,usd,aud,name) {
	this.id = id;
	this.eur = eur;
	this.gbp = gbp;
	this.usd = usd;
	this.aud = aud;
	this.name = name;
};

function showhideDiv(class1,class2,class3) {
	if (document.getElementById) {
	     document.getElementById(class1).style.background = '#efedb9';
	     document.getElementById(class2).style.background = 'transparent';
	     document.getElementById(class3).style.background = 'transparent';
	}		
}
var selectedCurrency = '';
var selectedProduct = '';
var subtotal, quantity, currencysign;

var loadProduct = function() {	
	
	$("input[name='productname']").change(function(){
		displayProductName();		
		updatePrice(selectedCurrency, selectedProduct);
	});
	
	
	$("input[name='currency'], #quantity").change(function(){
		updateProduct();
		updatePrice(selectedCurrency, selectedProduct);	
	});		
	$("#quantity").keyup(function(){
		updateProduct();
		updatePrice(selectedCurrency, selectedProduct);	
	});
	
	/* this only applies if the links buy now and send invoice are on the order page (1st page)
	$("form#frmBuynow .buy-now a").click(function(e) {
		e.preventDefault();	
		$("form#frmBuynow #action").val("buy");
		$("form#frmBuynow").submit();
	});
	
	$("form#frmBuynow #invoice-btn a").click(function(e) {
		e.preventDefault();	
		$("form#frmBuynow #action").val("invoice");
		$("form#frmBuynow").submit();
	});
	 */
	/* this only applies if the links buy now and send invoice are on the customer page (2nd page) 
	$("form#frmCustomer .buy-now a").click(function(e) {
		e.preventDefault();	
		$("form#frmCustomer #action").val("paynow");
		$("form#frmCustomer").submit();
	});
	
	$("form#frmCustomer #invoice-btn a").click(function(e) {
		e.preventDefault();	
		$("form#frmCustomer #action").val("sendinvoice");
		$("form#frmCustomer").submit();
	});
	*/
	/* this only applies if the links buy now and send invoice are on the confirmation page  */
	$("form#checkout .buy-now a").click(function(e) {
		e.preventDefault();	
		$("form#checkout #action").val("paynow");
		$("form#checkout .buy-now, form#checkout #invoice-btn").hide();
		$("form#checkout").submit();
	});
	
	$("form#checkout #invoice-btn a").click(function(e) {
		e.preventDefault();	
		$("form#checkout #action").val("sendinvoice");
		$("form#checkout .buy-now, form#checkout #invoice-btn").hide();
		$("form#checkout").submit();
	});
	
	/* continue button */
	$("form#frmBuynow #continue p.continue a").click(function(e) {
		e.preventDefault();	
		$("form#frmBuynow #action").val("continue");
		$("form#frmBuynow").submit();
	});	
	
	$("form#frmCustomer #continue p.continue a").click(function(e) {
		e.preventDefault();	
		$("form#frmCustomer #action").val("continueconfirm");
		$("form#frmCustomer").submit();
	});
	
	
	displayProductName();		
	updatePrice(selectedCurrency, selectedProduct);
};

var updateProduct = function () {
	if ($("input.currencyinput:checked").length)
		selectedCurrency = $("input.currencyinput:checked").val();
	else
		selectedCurrency = '';
	
	if ($("input[@name='productname']:checked").length)
		eval("selectedProduct = jsProduct_" + $("input[@name='productname']:checked").attr("id"));
	else 
		selectedProduct = '';	
};

function validateProductForm() {	
	return ($("input[@name='productname']:checked").length > 0 && selectedCurrency != '' && $("#quantity").val() != 0) ;		
}

var updatePrice = function (currency, jsProduct) {	
	
	subtotal = 0;
	quantity = 0;
	currencysign = '';
	
	if (currency != '') {
		$(".productprice").addClass('hidden');
		$(".productprice."+currency).removeClass('hidden');
	}
	
	if (validateProductForm()) {
		
		subtotal = jsProduct[currency];		
		quantity = $("#quantity").val();
		if (!isNumeric(quantity))
			quantity = 0;
		switch (currency) {
			case 'eur':
				currencysign = '&euro;';
				break;
			case 'gbp':
				currencysign = '&pound;';
				break;
			default:
				currencysign = '$';
		}
	}
	
	$("#val-subtotal").html(subtotal).format({format:"#,###.00", locale:"us"}).prepend(currencysign);
	$("#val-total").html(subtotal*quantity).format({format:"#,###.00", locale:"us"}).prepend(currencysign);	
};


var displayProductName = function() {
	// update product name
	updateProduct();
	if (selectedProduct != '')
	$("#selected-product-name").html(selectedProduct.name);	
};

function validateOnSubmit()  {
	
	if ($("input[@name='productname']:checked").length < 1) {
		alert("Please select a product");
		return false;
	}	
	else if ($("input.currencyinput:checked").length < 1) {
		alert("Please select a currency");
		return false;
	}
	else if ($("#quantity").val() < 10 || $("#quantity").val() >= 10000) {
		alert("Number of students must be an integer number between 10 and 10000");
		return false;
	}
	else if (!isNumeric($("#quantity").val())) {
		alert("Number of students must be an integer number between 10 and 10000");
		return false;
	}
	return true;
}