/*  Required validation script for the Crunch & Sip website "Submit your application" form	*/

/*
	Copyright 2005, Pretzel Logic 
	http://www.pretzel.com.au 
	Author: Alex Ramsey 
	Document created on: Friday, 4 March 2005 
	Document modified on:   by:  
*/

function isEmpty(thisForm, fieldPosition, Message1){
	if (thisForm[fieldPosition].value == ""){
		alert(Message1 + thisForm[fieldPosition].title + " field");
		thisForm[fieldPosition].focus();
		return true;
	}
	else{
		return false;
	}
}


//function submitForm(thisForm) -- Tests if checkboxes have been selected
//Requires -- validate.js


function submitForm(thisForm)
{
	var Message1 = "Please check that you have filled out the "
	var Message2 = "You have to select at least 3 checkboxes "
	var Message3 = "You have to select the "
	var Message4 = "The form cannot be submitted as no checkboxes have been selected. \nPlease check the boxes that apply to your school"
	var numCheckboxes = 8;
	var numCheckedFields = 0;
	var numTextBoxes = 2;
	var numTextBoxes2 = 4;
	var numTextBoxes3 = 5;
	var countChecked = 0;
	
	
	
	// Loop through the form
	for (var counter = 0; counter < thisForm.length; counter++){
		
		
		if (thisForm[counter].type == "checkbox"){
			if (thisForm[counter].checked){
				
				countChecked = countChecked + 1;
			} 
			if (thisForm[counter].id == "1") {
				if (thisForm[counter].checked){
					if ((thisForm[counter + 1].id == "2") && (thisForm[counter + 1].value == "") ) {
						alert(Message1 + thisForm[counter + 1].title + " field");
						thisForm[counter + 1].focus();
						return false;
						break
					}
				}	
			}
			if (thisForm[counter].id == "3") {
				if (thisForm[counter].checked){
					for (var counter2 = 1; counter2 < numCheckboxes + 1; counter2++){			
						if ((thisForm[counter + counter2].id == "4") && (thisForm[counter + counter2].checked)) {
							numCheckedFields = numCheckedFields + 1
						}
					}
					if (numCheckedFields < 3) {
						alert(Message2)
						thisForm[counter].focus();
						return false;
						break
					}
				}	
			}
			if (thisForm[counter].id == "5") {
				if (thisForm[counter].checked){
					for (var counter3 = 1; counter3 < numTextBoxes + 1; counter3++){
						if ((thisForm[counter + counter3].id == "6") && (thisForm[counter + counter3].value == "")) {
							alert(Message1 + thisForm[counter + counter3].title + " field");
							thisForm[counter + counter3].focus();
							return false;
							break
						}
					}
				}
			}
			if (thisForm[counter].id == "7") {
				if (thisForm[counter].checked){
					for (var counter4 = 1; counter4 < numTextBoxes2 + 1; counter4++){
						if ((thisForm[counter + counter4].id == "8") && (thisForm[counter + counter4].value == "")) {
							alert(Message1 + thisForm[counter + counter4].title + " field");
							thisForm[counter + counter4].focus();
							return false;
							break
						}
					}
				}
			}
			
		}
		//Test for the empty contact details
		else if (thisForm[counter].type == "text"){
			switch (thisForm[counter].name){
				case "txt_ContactName" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				case "txt_SchoolName" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				case "txt_SchoolPostalAddress" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				case "txt_SchoolStreetAddress" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				/*case "txt_Telephone" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				case "txt_Facsimile" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				case "txt_Website" : if (isEmpty(thisForm,counter,Message1)){return false; break}
				case "txt_Email" : if (isEmpty(thisForm,counter,Message1)){return false; break}*/
			}
		}
	}
	
	
	
	
	
	
	if (countChecked==0){
		alert (Message4)
		return false
	}
}


//function setFocus(thisForm,chkField,txtField) -- Set focus to the txtField if chkField is checked
function setFocus(thisForm,chkField,txtField){
	if (thisForm[chkField].checked){
		thisForm[txtField].focus()
		thisForm[txtField].select
	}
	else{
		
		thisForm[txtField].value = "";
		thisForm[txtField].blur();
	}
	return false
}

//function setFocusFile(thisForm,chkField,txtField,divID) -- Set focus to the file upload txtField if chkField is checked
function setFocusFile(thisForm,chkField,txtField,divID){
	if (thisForm[chkField].checked){
		thisForm[txtField].focus()
		thisForm[txtField].select
	}
	else{
		document.getElementById(divID).innerHTML = document.getElementById(divID).innerHTML;
		thisForm[txtField].blur();
	}
	return false
}

//function clearFocusGroup(thisForm,chkField,numFields,fieldType) -- Clears the text fields or checkboxes associated with the chkField
function clearFocusGroup(thisForm,chkField,numFields,fieldType){
	for (var counter1=0; counter1 < thisForm.length; counter1++){
		if (thisForm[counter1].name == chkField){
			if (!(thisForm[counter1].checked)){
				for (var counter2=1; counter2 < numFields + 1; counter2++){
					if (fieldType == "checkbox"){
						thisForm[counter1 + counter2].checked = false;
						thisForm[counter1 + counter2].blur();
					}
					if (fieldType == "text"){
						thisForm[counter1 + counter2].checked = false;
						thisForm[counter1 + counter2].value = "";
						thisForm[counter1 + counter2].blur();
					}
				}
				return false
			}
		}
	}
}

//function calcPercent(thisForm,firstField,secField,PercentField) -- calculates an interger percent value
function calcPercent(thisForm,firstField,secField,PercentField){
	if ((isNum(thisForm[secField].value)) && (isNum(thisForm[firstField].value))){
		if ( Math.ceil(thisForm[secField].value) <= Math.ceil(thisForm[firstField].value) ){
			thisForm[PercentField].value  = ((thisForm[secField].value / thisForm[firstField].value) * 100);	
			thisForm[PercentField].value = Math.ceil(thisForm[PercentField].value) + "%"
		}
		else{
			alert("Incorrect values entered")
			thisForm[firstField].value = ""
			thisForm[secField].value = ""
			thisForm[PercentField].value = ""
		}
	}
	else{
		if (!(isNum(thisForm[secField].value))){
			thisForm[secField].value = ""
		}
		if (!(isNum(thisForm[firstField].value))){
			thisForm[firstField].value = ""
		}
	}
}

//function isInteger(thisForm,thisField,msg) -- Checks if an integer value has been entered
function isInteger(thisForm,thisField,msg){
	if ((isNum(thisForm[thisField].value)) || (thisForm[thisField].value == "")){
		return true
	}
	else
		alert(msg)
		thisForm[thisField].value = ""
		thisForm[thisField].focus()
		return false
}

function invalidEmail(thisForm,thisfield){
	// check to see if the email's valid
	if (!validEmail(thisForm[thisfield].value)) {
		alert("Invalid email address")
		thisForm.txt_Email.focus()
		thisForm.txt_Email.select()
		return false
	}
}