ccType = '';
ccNumberOkay = 0;
ccNumberChecked = '';

function StripNonDigits(number) {
var Re = /\d+/g;
if(Re.lastIndex > 1) { Re.lastIndex = 0; }
var Array = Re.exec(number);
if(Re.lastIndex < 1) { return 'X'; }
var ss = Array.join();
while(Re.lastIndex > 0 && Re.lastIndex < number.length) {
	Array = Re.exec(number);
	if(Array) { ss += Array.join(); }
	}
return ss;
} // StripNonDigits()

function CheckccExpiry() {
	currDate = new Date();
	currMonth = currDate.getMonth()+1;
		if (currMonth < 10) currMonth = "0"+currMonth;
	currYear = (currDate.getYear())%100;
     		if (currYear < 10) currYear = "0"+currYear;
	expMonth = (document.application.ccExpMonth.options[document.application.ccExpMonth.selectedIndex].value);
	expYear = (document.application.ccExpYear.options[document.application.ccExpYear.selectedIndex].value);
	if (currYear > expYear) {
			return false;
		}
	if (currYear == expYear) {
		if (currMonth > expMonth) {
			return false;
		}
	}
return 1;
} 

function GetType(number) {
var len = number.length;
var Re = /^5[1-5]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		var Array = Re.exec(number);
			if(Array && len == 16) { return 'MasterCard'; }
Re = /^4/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 13 || len == 16)) { return 'Visa'; }
Re = /^5610/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 16) { return 'BankCard'; }
return '';
} // GetType()


function Reverse(number) {
var n = '';
for(i = number.length; i >= 0; i--) { n += number.substr(i,1); }
return n;
} // Reverse()


function AddedTogether(number) {
var n = 0;
for(i = 0; i < number.length; i++) {
	var s = number.substr(i,1);
	var si = parseInt(s,10);
	if(i % 2 > 0) {
		var ii = si * 2;
		if(ii < 10) { n += ii; }
		else {
			var ss = ' ' + ii;
			for(xi = 1; xi < ss.length; xi++) {
				var xs = ss.substr(xi,1);
				var xsi = parseInt(xs,10);
				n += xsi;
				} // for
			} // else
		} // if
	else { n += si; }
	} // for
return n;
} // AddedTogether()


function Mod10(n) {
var reversed = Reverse(n);
var total = AddedTogether(reversed);
if(total % 10 > 0) { return 0; }
return 1;
} // Mod10()

function Validate(n) {
ccNumberChecked = StripNonDigits(n);
ccTypeTested = GetType(ccNumberChecked);
ccNumberOkay = Mod10(ccNumberChecked);
ccExpiryOkay = CheckccExpiry();
if	(ccTypeTested.length < 2 && ccNumberOkay == 0) { missinginfo += "\n     -  Credit card number is invalid";}
else if	(ccNumberOkay == 0) { missinginfo += "\n     -  Credit card number is invalid";}
else if	(ccTypeTested.length < 2) { missinginfo += "\n     -  Credit card type is invalid. We accept Visa, MasterCard and BankCard";}
else if	(ccExpiryOkay != 1) { missinginfo += "\n     -  Credit card has expired";}
else  	{document.application.ccNumber.value=ccNumberChecked;}
return false;
}

function ValidateForPrecharge(n) {
ccNumberChecked = StripNonDigits(n);
ccTypeTested = GetType(ccNumberChecked);
ccNumberOkay = Mod10(ccNumberChecked);
ccExpiryOkay = CheckccExpiry();
if	(ccTypeTested.length < 2 && ccNumberOkay == 0) {
		return false;
	}
else if	(ccNumberOkay == 0) {
		return false;
	}
else if	(ccTypeTested.length < 2) {
		return false;
	}
else if	(ccExpiryOkay != 1) {
		return false;
	}
else {
	return true;
}
}
//------------------------------------------

function checkFields() {
document.application.submit.value="Submitting, please wait...";
document.application.submit.disabled=true;

missinginfo = "";

if (document.application.title.options[document.application.title.selectedIndex].value == "") {
missinginfo += "\n     -  Title";
}
if (document.application.firstname.value == "") {
missinginfo += "\n     -  First Name";
}
if (document.application.lastname.value == "") {
missinginfo += "\n     -  Last Name";
}
if ((document.application.birthdate_day.options[document.application.birthdate_day.selectedIndex].value == "") ||
(document.application.birthdate_month.options[document.application.birthdate_month.selectedIndex].value == "") ||
(document.application.birthdate_year.options[document.application.birthdate_year.selectedIndex].value == "")) {
missinginfo += "\n     -  Birth Date";
}
if (document.application.address1.value == "") {
missinginfo += "\n     -  Address";
}
if (document.application.suburb.value == "") {
missinginfo += "\n     -  Suburb";
}
if (document.application.state.value == "") {
missinginfo += "\n     -  State";
}
if (document.application.country.value == "") {
missinginfo += "\n     -  Country";
}
if (document.application.postcode.value == "") {
missinginfo += "\n     -  Postcode";
}
if (document.application.phonenumber.value == "") {
missinginfo += "\n     -  Home Phone";
}
if ((document.application.Email.value == "") || 
(document.application.Email.value == "@.") ||
(document.application.Email.value.indexOf('@') == -1) || 
(document.application.Email.value.indexOf('.') == -1)) {
missinginfo += "\n     -  e-Mail Address";
}
if ((!document.application.elements.idtype[0].checked) &&
(!document.application.elements.idtype[1].checked) &&
(!document.application.elements.idtype[2].checked)) {
missinginfo += "\n     -  Identification Type";
}
if ((document.application.elements.idtype[2].checked) && (document.application.otherid.value == "")) {
missinginfo += "\n     -  Please specify Other Identification Type";
}
if (document.application.idnumber.value == "") {
missinginfo += "\n     -  Identification Number";
}

if ((document.application.ccNumber.value != "") || (document.application.ccname.value != "") || (document.application.ccExpMonth.options[document.application.ccExpMonth.selectedIndex].value != "") || (document.application.ccExpYear.options[document.application.ccExpYear.selectedIndex].value != "")) {
	if (document.application.ccNumber.value == "") {
		missinginfo += "\n     -  Credit Card Number";
	}
	if ((document.application.ccExpMonth.options[document.application.ccExpMonth.selectedIndex].value == "") || (document.application.ccExpYear.options[document.application.ccExpYear.selectedIndex].value == "")) {
		missinginfo += "\n     -  Credit Card Expiry";
	}
	if (document.application.ccname.value == "") {
		missinginfo += "\n     -  Name On Credit Card";
	}
}

if ((document.application.ccNumber.value != "") && (document.application.ccExpMonth.options[document.application.ccExpMonth.selectedIndex].value != "") && (document.application.ccExpYear.options[document.application.ccExpYear.selectedIndex].value != "") && (document.application.ccname.value != "")) {
	Validate(document.application.ccNumber.value);
}

if ((document.application.elements.ccprecharge.checked) && (!ValidateForPrecharge(document.application.ccNumber.value))) {
	missinginfo += "\n     -  Pre-charge selected but credit card details missing or invalid";
}

if (document.application.store_location.options[document.application.store_location.selectedIndex].value == "") {
missinginfo += "\n     -  Store Location";
}

if (document.application.pin.value != "") {
  var PINval=document.application.pin.value;
  var PINlen=PINval.length;
  var PINexp = new RegExp(/^\d+$/);
  var PINtest=PINexp.test(PINval);
  if (!(PINlen == 4)) {missinginfo += "\n     -  Master PIN must be 4 digits";}
  if (!PINtest) {missinginfo += "\n     -  Master PIN must consist of numbers only";}
}
else missinginfo += "\n     -  Master PIN";
	
if (document.application.pin_minors.value != "") {
  var PINval=document.application.pin_minors.value;
  var PINlen=PINval.length;
  var PINexp = new RegExp(/^\d+$/);
  var PINtest=PINexp.test(PINval);
  if (!(PINlen == 4)) {missinginfo += "\n     -  Minors PIN must be 4 digits";}
  if (!PINtest) {missinginfo += "\n     -  Minors PIN must consist of numbers only";}
  if (PINval == document.application.pin.value) {missinginfo += "\n     -  Minors PIN may not be the same as Master PIN";}
}

if (!document.application.elements.agreement.checked) {
missinginfo += "\n     -  Agreement of Terms and Conditions";
}

if (missinginfo != "") {
missinginfo ="These fields have not been correctly filled:\n" +
missinginfo + "\n\nPlease fill these fields and submit again.";
alert(missinginfo);
document.application.submit.disabled=false;
document.application.submit.value="Submit";
return false;
}
else return true;
}