onerror=handleErr
function handleErr(msg,url,l)
{
	txt="There was an error on this page.\n\n"
	txt+="Error: " + msg + "\n"
	txt+="URL: " + url + "\n"
	txt+="Line: " + l + "\n\n"
	txt+="Click OK to continue.\n\n"
	alert(txt);
	return false;
}
function getEl(id)
{
	return document.getElementById(id);
}
// Removes leading whitespaces
function LTrim( value ) 
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}
// Removes ending whitespaces
function RTrim( value )
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim ( value ) 
{
	return LTrim(RTrim(value));
	
}
function validEmail(src)
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(src);
}
function rand (min, max) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman
    // %          note 1: See the commented out code below for a version which will work with our experimental (though probably unnecessary) srand() function)
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
    
    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;
    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
    
    /*
    // See note above for an explanation of the following alternative code
    
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: srand
    // %          note 1: This is a very possibly imperfect adaptation from the PHP source code
    var rand_seed, ctx, PHP_RAND_MAX=2147483647; // 0x7fffffff
 
    if (!this.php_js || this.php_js.rand_seed === undefined) {
        this.srand();
    }
    rand_seed = this.php_js.rand_seed;
 
    var argc = arguments.length;
    if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
 
    var do_rand = function (ctx) {
        return ((ctx * 1103515245 + 12345) % (PHP_RAND_MAX + 1));
    };
 
    var php_rand = function (ctxArg) { // php_rand_r
        this.php_js.rand_seed = do_rand(ctxArg);
        return parseInt(this.php_js.rand_seed, 10);
    };
 
    var number = php_rand(rand_seed);
 
    if (argc === 2) {
        number = min + parseInt(parseFloat(parseFloat(max) - min + 1.0) * (number/(PHP_RAND_MAX + 1.0)), 10);
    }
    return number;
    */
}
function reload_captcha(img)
{
	img.src="/_server.php?task=captcha_display&"+rand(1,100000);
}
function validateRegistration(obj)
{
	var message='';
	var flag=true;
	var form=getEl(obj);
	//
	if(trim(form.last_name.value)=='')
	{
		flag=false;
		message+="* Surname is empty.\n";
	}
	if(form.id!='clubForm')
	{
		if(trim(form.first_name.value)=='')
		{
		flag=false;
		message+='* First Name is empty.\n';
		}
	}
	if(!validEmail(trim(form.email_address.value)))
	{
		flag=false;
		message+='* Email Address is invalid.\n';
	}
	else if(form.userID.value=='')
	{
		// Check via ajax whether the email address exists in the system
		var ajax=new Ajax();
		ajax.async=false;
		var json=eval('('+ajax.doGet('_server.php?task=check_email&email='+trim(form.email_address.value),null,'text')+')');
		if(!json.status)
		{
			flag=false;
			message+='* That Email address is already registered.\n';
		}
	}
	
	if(form.id!='publicForm'&&trim(form.password.value)!='')
	{
		if(trim(form.password.value)!=trim(form.password_confirm.value))
		{
			flag=false;
			message+='* Passwords must match.\n';
		}
	}
	else if(form.id!='publicForm'&&form.userID.value=='')
	{
		flag=false;
		message+='* Password is empty and must match \'Confirm Password\'.\n';
	}
	if(form.id!='publicForm'&&trim(form.phone.value)=='')
	{
		flag=false;
		message+='* Phone number is empty.\n';
	}
	switch(form.id)
	{
		case 'coachForm':
		{
			// dob, phone
			if(form.dob_day.value==''||form.dob_month.value==''||form.dob_year.value=='')
			{
				flag=false;
				message+='* Date of Birth is empty.\n';
			}
			break;
		}
		case 'playerForm':
		{
			// dob, phone and preferred positions
			if(form.dob_day.value==''||form.dob_month.value==''||form.dob_year.value=='')
			{
				flag=false;
				message+='* Date of Birth is empty.\n';
			}
			var select_flag=false;
			var select=getEl('positions_preferred');
			for(var i=0;i<select.options.length;i++)
			{
				if(select.options[i].selected)
				{
					select_flag=true;
				}
			}
			if(!select_flag)
			{
				flag=false;
				message+='* No Preferred Positions selected.\n';
			}
			break;
		}
	}
	if(form.id!='publicForm'){
	if(!form.accept_terms.checked)
	{
		flag=false;
		message+='* You must accept the terms and conditions before proceeding.\n';
	}
	}
	if(form.usercode=='')
	{
		flag=false;
		message+='* Please enter the Captcha Code.\n';
	}
	else {
		var ajax=new Ajax();
		ajax.async=false;
		var json=eval('('+ajax.doGet('_server.php?task=captcha_validate&usercode='+trim(form.usercode.value),null,'text')+')');
		if(!json)
		{
			flag=false;
			message+='* Please enter the correct CAPTCHA Code.\n';
		}
	}
	if(!flag)
	{
		message='The following errors were found:\n'+message;
		alert(message);
	}
	return flag;
}