function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


// ---------------------------------------------------------------------------
// remove all spaces from a string
function removeSpaces(string) {

	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];

	return tstring;
}

// ---------------------------------------------------------------------
// set the error colours around the field
function set_error(field, flength) { 
	document.getElementById(field).style.width = flength;
	document.getElementById(field).style.borderStyle = "solid";
	document.getElementById(field).style.borderColor = "red";
	document.getElementById(field).style.borderWidth = "1px";
	document.getElementById(field).style.backgroundColor = "#F0C9C1";
	return;

}

// ---------------------------------------------------------------------
// clear the error colours around the field
function clear_error(field, flength) {
	document.getElementById(field).style.width = flength;
	document.getElementById(field).style.borderStyle = "none";
	document.getElementById(field).style.backgroundColor = "#FFFFF";
	return;
}


// ---------------------------------------------------------------------
// check for valid email syntax
function emailcheck(email) {

	var str = document.getElementById(email).value;

	var error_flag=false;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1){
	   error_flag=true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   error_flag=true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   error_flag=true;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	   error_flag=true;
	}

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   error_flag=true;
	}

	 if (str.indexOf(dot,(lat+2))==-1){
	   error_flag=true;
	}
	
	 if (str.indexOf(" ")!=-1){
	   error_flag=true;
	}

	if(error_flag) {
		
		//flag the field and pass back false
		set_error(email+"_div");
		return false; 

	} else {

		// clear any errors and pass back true;
		clear_error(email+"_div");
		return true;
}

}

// -------------------------------------------------------------------------------------
// check for values in mandatory fields

function mandatory_fields(myArray){

	var error_flag=false;
	var arLen=myArray.length;

	for ( var i=0, len=arLen; i<len; ++i ){ 

			var myTextField = removeSpaces(document.getElementById(myArray[i]).value);

			// determine the width of the field so we can set the red error box the correct size
			switch(myArray[i])
				{
				case "send_postal":
				case "send_phone":
				case "send_email":
					flength = "257px";
					break;
				default:
					flength = "357px";
				}

			if(myTextField == "") { // set the error

				set_error(myArray[i]+"_div",flength);
				error_flag = true;

			} else { // reset the error field 
					
				clear_error(myArray[i]+"_div",flength);
			}
	}

	if(error_flag) {return false; }
		else {return true;}

}

// -------------------------------------------------------------------------------------
// validate donation form

function validate_form(checkphone) { 

	var myArray = new Array();

	// add the form fields to the array that are MANDATORY.  don't change the order!
	myArray[0] = "send_fname";
	myArray[1] = "send_address";
	myArray[2] = "send_city";
	myArray[3] = "send_province";
	myArray[4] = "send_postal";
	myArray[5] = "send_phone";
	myArray[6] = "send_email";
	myArray[7] = "send_lname";

	// check for values in mandatory fields
	if (!mandatory_fields(myArray)){ 
		alert("There was a problem with the data you submitted.  Please recheck that the mandatory fields are completed.");
		document.getElementById("form_err_msg").innerHTML = "Please complete all required fields."
		return false;
	}
	
	// now check for valid email address
	if (!emailcheck(myArray[6])){
		alert("There was a problem with the data you submitted.  Please recheck your email address.");
		document.getElementById("form_err_msg").innerHTML = "Invalid email address."
		return false;
	}

	
	// now check for in honour of field
	if (document.getElementById('inhonourcheckbox').checked == true){

		if (removeSpaces(document.getElementById('inhonourname').value)==""){

			set_error("inhonourname_div","357px");
			alert("You must provide a value for the 'In Honour Of' field.");
			return false;

		}
	}



	// if nothing fails then pass back true
	return true;
}

function validate_ticket_form() { 

	var myArray = new Array();

	// add the form fields to the array that are MANDATORY.  don't change the order!
	myArray[0] = "send_fname";
	myArray[1] = "send_address";
	myArray[2] = "send_city";
	myArray[3] = "send_province";
	myArray[4] = "send_postal";
	myArray[5] = "send_phone";
	myArray[6] = "send_email";
	myArray[7] = "send_lname";

	// check for values in mandatory fields
	if (!mandatory_fields(myArray)){ 
		alert("There was a problem with the data you submitted.  Please recheck that the mandatory fields are completed.");
		document.getElementById("form_err_msg").innerHTML = "Please complete all required fields."
		return false;
	}
	
	// now check for valid email address
	if (!emailcheck(myArray[6])){
		alert("There was a problem with the data you submitted.  Please recheck your email address.");
		document.getElementById("form_err_msg").innerHTML = "Invalid email address."
		return false;
	}


	// if nothing fails then pass back true
	return true;
}
