function init()
{
	$("#payfororder").ajaxStart($.blockUI);
	$("#payfororder").ajaxStop($.unblockUI);
	$("#payfororder form").ajaxForm({
		beforeSubmit: validateForm,
		success: handlePayment,
		datatype: ($.browser.msie) ? "text" : "xml"
	});

	$("#cc_cardnumber").blur(
		function() {
			var num = $(this).val();
			checkValidCC(num);
		}
	);
}

function abortOrder(ocid)
{
	//var ordernumber = $("#ordernumber").val();
	var email = $("#email").val();
	if( confirm("Aborting the order will cancel it. Are you sure you want to abort this order?") ) {
		$.post("cancelorder.asp", { ordernumber: ocid, email: email }, function(data) {
			//window.setTimeout( function() { alert("Your order has been cancelled. You will now be redirected to the Flemington Shop home page."); location.href = "index.asp"; }, 500);
			if (data = "succeeded") {
				window.setTimeout( function() { alert("Your order has been cancelled. You will now be redirected to the Flemington Shop home page."); location.href = "index.asp"; }, 500);
			} else {
				alert("Your order was not cancelled. Please contact <span style='color: blue; text-decoration: underline;'>flemington@tpf.com.au</span>.");
			}
		});
	}
}

function isBlank(str)
{
	return( String(str).replace(/\s/g, "") === "" );
}

function validateForm()
{
	if( isBlank( $("#cc_cardholdername").val() ) ) {
		alert("You must enter the card holder's name");
		$("#cc_cardholdername").focus();
	} else if( isBlank( $("#cc_cardnumber").val() ) ) {
		alert("You must enter your credit card number");
		$("#cc_cardnumber").focus();
	} else if( $("#cc_cardtype").val() == "none" ) {
		alert("You must enter the card type");
		$("#cc_cardtype").focus();
	} else if( $("#cc_mmexp").val() == "none" ) {
		alert("You must choose the expiry month");
		$("#cc_mmexp").focus();
	} else if( $("#cc_yyexp").val() == "none" ) {
		alert("You must choose the expiry year");
		$("#cc_yyexp").focus();
	} else
		return true;
	return false;
}

function handlePayment(xmlDoc)
{
	var xml = parseXMLDocResult(xmlDoc);
	var ordernumber = $("input[name='ordernumber']").val();
	var email = $("input[name='email']").val();
	var status = $("status", xml).text();
	if( status == "success" ) {
		location.href = "paymentapproved.asp?ordernumber=" + ordernumber + "&email=" + email;
	} else {
		paymentFailed(xml);
	}
}

function paymentFailed(xml)
{
	var reason = $("reason", xml).text();
	$("#failure").html("Unfortunately we were unable to process your credit card because:<br> " + reason);
}

function isValidCreditCardNumber(number) {
  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  var number= number.replace(/\D/g, '');

  if( isNaN(number) || Number(number) <= 0 )
	return false;

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (var i = 0; i < number_length; i++) {
	var digit=number.charAt(i);
	// Multiply alternate digits by two
	if (i % 2 == parity) {
	  digit=digit * 2;
	  // If the sum is two digits, add them together (in effect)
	  if (digit > 9) {
		digit=digit - 9;
	  }
	}
	// Total up the digits
	total = total + parseInt(digit, 10);
  }

  // If the total mod 10 equals 0, the number is valid
  return (total % 10 === 0);
}

function checkValidCC(cc)
{
	if( ! isValidCreditCardNumber(cc) ) {
		$("#failure").html("The credit card number may be incorrect.");
	} else 
		$("#failure").html("");
}

$(document).ready(init);