var arrCheckerFields = new Array()
var sCheckerForm
var sCheckerAction
var sCheckerButton


function initFormChecker(sForm,sAction,sButton){
	sCheckerForm = sForm
	sCheckerAction = sAction
	sCheckerButton = sButton
	
	var oButton = getObject(sCheckerButton)
	oButton.onclick = function(){checkFields()}
}

function addCheckerField(sFieldName,sReadoutName,sType,bCanBeBlank,sConfirmationFieldName){

	var oField = new Object()
	oField.FieldName = sFieldName
	oField.ReadoutName = sReadoutName
	oField.Type = sType
	oField.CanBeBlank = bCanBeBlank
	oField.ConfirmationFieldName = sConfirmationFieldName
	
	arrCheckerFields.push(oField)
	
	
	var oReadout = getObject(oField.ReadoutName)
	oReadout.style.display = 'none'
	
	var oInput = getObject(sFieldName)
	oInput.onkeypress = function(event){
												return (getEvent(event).keyCode != 13)												
									   }
}

function checkFields(){
	var bFormOK = true
	
	//loop through the fields we have
	for (var i = 0;i<arrCheckerFields.length;i++){
	
	var oField = arrCheckerFields[i];
	var oInputBox = getObject(oField.FieldName);
	
	var bFieldOK = true
	
	var sReadout = ''
	
		//can it be blank?
		if (oField.CanBeBlank == false){
			if (oInputBox.value == ''){
				sReadout += ('This field can not be empty, ')
				bFieldOK = false
			}
		}
		
		
		//is it a currency field?
		if (oField.Type == 'Currency'){
			if (isCurrency(oInputBox.value) == false){
				sReadout += ('This field must be valid currency, ')
				bFieldOK = false
			}
		}
		
		//is it a integer field?
		if (oField.Type == 'Integer'){
			if (isNumeric(oInputBox.value) == false){
				sReadout += ('This field must be a number, ')
				bFieldOK = false
			}
		}
		
		
		//is it a date field?
		if (oField.Type == 'Date'){
			if (isDate(oInputBox.value) == false){
				if (oInputBox.value != ''){
					sReadout += ('This field must be a valid date, ')
					bFieldOK = false
				}
			}
		}
		
		
		//is it an auth code field
		if (oField.Type == 'AuthCode'){
			if (oInputBox.value != sAuthCode){
				if (oInputBox.value != ""){
					sReadout += ('This field must be a valid auth code, ')
					bFieldOK = false
				}
			}
		}
		
		
		
		
		//is it a "must tick" checkbox?
		if (oField.Type == 'MustTick'){
			if (!oInputBox.checked){
				sReadout += ('This field must be ticked, ')
				bFieldOK = false
			}
		}
		
		
		
		
		//is it an email field?
		if (oField.Type == 'Email'){
			if (isEmail(oInputBox.value) == false){
				if (oInputBox.value != ''){
					sReadout += ('This field must be a valid email address, ')
					bFieldOK = false
				}
			}
		}
		
		//is it a confirmed field?
		if (oField.ConfirmationFieldName != undefined){
			var oConfirmationInputBox = getObject(oField.ConfirmationFieldName)
				if (oInputBox.value != oConfirmationInputBox.value){
					sReadout += formatForReadOut(oField.ConfirmationFieldName) + ' does not match ' + formatForReadOut(oField.FieldName)
					bFieldOK = false
				}
		}
		
		
		var oReadout = getObject(oField.ReadoutName)
		
		if (sReadout != ''){
			sReadout = Left(sReadout,sReadout.length - 2)
			oReadout.innerHTML = sReadout
			oReadout.style.display = 'block'
		}else{
			oReadout.innerHTML = ''
			oReadout.style.display = 'none'
		}
		
		if (!bFieldOK){bFormOK = false}
	
	}
	
	if (bFormOK){
		var oButton = getObject(sCheckerButton)
		oButton.disabled = true
		
		var oForm = getObject(sCheckerForm)
		oForm.action = sCheckerAction	
		oForm.submit()
	}
	
}


function formatForReadOut(sText){
	var sResult = sText
	
	sResult = sResult.replace(/_/g,' ')
	
	return sResult
}


























function Left(str, len){
	return str.slice(0,len)
}
function Right(str, len){
	return str.slice(len,str.length)
}

function Mid(str,start,len){
	return str.slice(start,start+len)
}

function formatCurrency(sText){
	
	var lPounds = Math.floor(sText)
	var lPence = Math.round((sText - lPounds) * 100)
	
	lPence = lPence+''
	
	if (lPence.length == 1){
		lPence = '0' + lPence
	}
	
	return '£' + formatNumber(lPounds) + "." + lPence
}

function formatNumber(sText){
	
	sText = sText + ''
	
	var sResult = ""
	var lCount = 0
	
	for (var i = sText.length-1;i>=0;i--){
			
		if (lCount == 3){
			sResult = "," + sResult
			lCount = 0
		}
		sResult = sText.charAt(i) + sResult
		lCount++
	}
	
	return sResult
}

function isCurrency(sText){
   var ValidChars = "0123456789.£";
   var isValid=true;
   var Char;

 
   for (i = 0; i < sText.length && isValid == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         isValid = false;
         }
      }

	return isValid;
   
}


function isNumeric(sText){
   var ValidChars = "0123456789";
   var isValid=true;
   var Char;

 
   for (i = 0; i < sText.length && isValid == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         isValid = false;
         }
      }

	return isValid;
   
}






function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2,4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
return false;
}

day = matchArray[1]; // p@rse date into variables
month = matchArray[3];
year = matchArray[5];

if (day < 1 || day > 31) {
return false;
}

if (month < 1 || month > 12) { // check month range
return false;
}


if ((month==4 || month==6 || month==9 || month==11) && day==31) {
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
return false;
}
}
return true; // date is valid
}


function isTime(sTime){

var sSplit = sTime.split(":")

if (sSplit.length != 2){
	return false;
}

var hours = sSplit[0]
var minutes = sSplit[1]

if (isNumeric(hours) == false){
	return false;	
}
if (isNumeric(minutes) == false){
	return false;	
}

if (hours == ""){
	return false;
}

if (minutes == ""){
	return false;
}

if (hours > 23){
	return false;
}

if (minutes > 59){
	return false;
}

return true;


}

function isEmail(sEmail){
	if (sEmail.indexOf("@") == -1 || sEmail.indexOf(".") == -1){
		return false;
	}else{
		return true;
	}
}