//////////////////////////////////////////// Layout and text size ////////////////////////////////////////////
//These functions were added by Paul 5/12/2008 so that the text size and layout use their own cookies
function ChangeLayoutType (layout) {ChangePageProperty ('layout', layout);}
function ChangeTextSize (size) {ChangePageProperty ('text', size);}
function ChangePageProperty (property, value) {
	if (!document.getElementsByTagName) return; //can't do this
	var bodytags = document.getElementsByTagName ('body'); //get the body tag
	var bodyclass = bodytags[0].className;
	var pos1 = bodyclass.indexOf (property); //look for the word 'text' or 'layout'
	var pos2 = Math.min (bodyclass.indexOf (' ', pos1), bodyclass.length); //look for the next space
	var newclass = bodyclass.substr (0, pos1) + property + value + bodyclass.substr (pos2); //replace the text in the class
	bodytags[0].className = newclass; //save to the body tag
	var today = new Date(); var expires = new Date (today.getTime() + 100 * 86400); //get when it expires
	var newcookie = 'page' + property + '=' + value + ';expires=' + expires.toGMTString() + '; path=/'; //create the cookie to store this
	document.cookie = newcookie; //set this new cookie
	//alert ('Body class is now: ' + newclass + '\nNew cookie: ' + newcookie + '\nCookie is now: ' + document.cookie);
}

//////////////////////////////////////////// Checkform function which we can use ////////////////////////////////////////////
//This function should be passed a list of arguments. The first is the form, then pass in alternating fields,
//type of validation (notblank is the default, email, number), and text if validation fails (defaults to the field name).
//Example use: <form onsubmit="return PaulCheckForm(this,'your_name','','','your_email','email','','feedback','','');" ...
function PaulCheckForm (theform) {
	var s = ''; //this is the error string
	var a = PaulCheckForm.arguments;
	for (var i=1; i<a.length; i+=3) { //loop through the arguments
		var el = theform[a[i]]; //get the element
		var check = a[i+1] ? a[i+1] : 'notblank'; //type of check
		var field = a[i+2] ? a[i+2] : (a[i].substr(0, 1).toUpperCase() + a[i].substr(1).replace (/_/, ' ')); //the default field text
		var error = ''; //to store the error
		if (!el) {s += field + ' does not exist'; break;} //this field does not exist
		var value = ''; //the value for the form element
		//Now get the value of the element based on the type of the element
    		if (el.type == 'text' || el.type == 'password' || el.type == 'hidden' || el.type == 'textarea') value = el.value;
		else if (el.type == 'select-one') value = el.options[el.selectedIndex].value;
		//else if (el.type == 'checkbox') //this, select-multiple and radio should be implemented later
		else {s += field + ' is of type ' + el.type + ' which this check cannot handle'; break;} //unknown type
		//Now perform the check
		if (check == 'notblank' && (value.length == 0 || value.indexOf ('Enter your ') == 0)) error = 'must be filled in';
		else if (check == 'email' && (value.indexOf('@')<1 || value.lastIndexOf('.')<3 || value.length<5)) error = 'must be a valid email address';
		else if (check == 'number' && isNaN (value)) error = 'must be a number';
		if (error) s += field + ' ' + error + '.\n';
	}
	if (s) alert ('Please correct the following errors and resubmit:\n' + s);
	return s ? false : true;
}
