/*This function loops through the required fields on the form and tests that the values entered
in them appear to be valid.  If no invalid fields are identified it returns true, if not it returns false
and returns the keyboard focus to the first offending field*/
function compulsoryFields(){
	compulsoryFieldsArray = new Array();
	var labels=document.getElementsByTagName('label');
	j = 0;
	for(var i = 0; i < labels.length; i++){
		labelClass = labels[i].className; // get label class
		tagId = labels[i].htmlFor; //get id of input tag from label
		if(labelClass == "compulsory"){
			compulsoryFieldsArray[j] = tagId;
			j++;
		}
	}
}

function checkFields(total, formId){
	for(i = 0; i < compulsoryFieldsArray.length; i++){
		currField = document.getElementById(formId).elements[compulsoryFieldsArray[i]];
		if(currField && (currField.type == "text" || currField.type == "textarea")){
			if(isBlank(currField.value)){
				//alert(currField.name + " is a required field\n Please enter a value.");
				alert("Some details that are compulsory are missing\n Please enter the required information.\nThe first missing field will now be highlighted");
				currField.focus();
				if(currField.className == "emptyField"){
					currField.className = "missingField";
				}
			return false;
			}
			else{
				currField.className = "emptyField";
			}
		}
	}
	if(total == 'none'){
		document.getElementById(formId).submit();
	}
	else{
		if(confirm("Your card is about to be debited\nfor £" + findDot(total) + "\nDo you wish to continue?")){
			document.getElementById(formId).submit();
		}
	}
}

function isBlank(yourString){
	for(var i = 0; i < yourString.length; i++){
		var currChar = yourString.charAt(i);
		if(currChar != " "){
			return false;
		}
	}
	return true;
}

function findDot(dot){
	dot = new String(Math.round(dot * 100) / 100);
	if (dot.indexOf('.')==-1){
		return(dot + ".00")
	}//if no decimal place add ".00"
	else if (dot.indexOf('.')==(dot.length)-2){
		return(dot + "0")      //if one decimal place add "0"
	}
	//if for any reason there are more than 2 characters after the '.' they will be striped off
	else if (dot.indexOf('.')>(dot.length)-2);{
	x=dot.indexOf('.');
	}
	x = x + 3;
	dot = dot.substring(0,x);
	return(dot);
}
