bErrs = false;
function clearAllErrors(){
	if (document.getElementById){
		document.getElementById("err_user").innerHTML='';
		document.getElementById("err_tc").innerHTML='';
		document.getElementById("err_gen").innerHTML='';
		document.getElementById("err_email").innerHTML='';
		checkUsername();
	}
}
function showInfo(sDivName, sContent){
	bErrs = true
	if (document.getElementById){
		document.getElementById(sDivName).innerHTML=sContent ;
	}
	else
	{
	alert(sContent);
	}
}

function validateForm() {
	bErrs = false
	clearAllErrors();
	x = document.frmReg;
	if (!emailCheck(x.primaryemail.value)){
		showInfo('err_email','You have not provided a valid email address.')
	}
	if (x.username.value.length < 5){
		showInfo('err_user','Your username must be atleast 5 characters long');
	}

	if (x.tc.checked == false){
		showInfo('err_tc','You must agree to the Terms & Conditions.')
	}
	if (bErrs == false) {
	x.submit();
	}
	else
	{
		showInfo('err_gen','Please correct the errors above.')
	}
}

function alertMailSecurity()
{
	var f = document.frmReg;
	var boo = f.emailme.checked;
	if (boo) {
		if(!confirm('IMPORTANT\n\nWe have gone to great lengths to protect your data.\n\nWe highly recommend that you do not have your log in details\nemailed to you because email is a very unsecure medium.\n\nWe advise that you choose login details that you can remember easily\nor that if you write them down you keep them in a safe place.\n\nClick OK if you\'re sure you wish to have your login details emailed to you.')){
			f.emailme.checked = false;
		}
	}
}
function createAjaxObj(){
	var httprequest=false
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest=new XMLHttpRequest()
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml')
		}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
	catch (e){
		try{
			httprequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e){}
		}
	}
		return httprequest
	}
	
	var ajaxpack=new Object()
	ajaxpack.basedomain="http://"+window.location.hostname
	ajaxpack.ajaxobj=createAjaxObj()
	ajaxpack.filetype="txt"
	ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.
	
	ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
		ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
		if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
		if (this.ajaxobj){
		this.filetype=filetype
		this.ajaxobj.onreadystatechange=callbackfunc
		this.ajaxobj.open('GET', url+"?"+parameters, true)
		this.ajaxobj.send(null)
	}
}

function cancelRequest(){
	ajaxpack.ajaxobj.abort();
}
	
	var ajaxpack=new Object()
	ajaxpack.basedomain="http://"+window.location.hostname
	ajaxpack.ajaxobj=createAjaxObj()
	ajaxpack.filetype="txt"
	ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.
	
	ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
		ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
		if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
		if (this.ajaxobj){
		this.filetype=filetype
		this.ajaxobj.onreadystatechange=callbackfunc
		this.ajaxobj.open('GET', url+"?"+parameters, true)
		this.ajaxobj.send(null)
	}
}
ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (this.ajaxobj){
this.filetype=filetype
this.ajaxobj.onreadystatechange = callbackfunc;
this.ajaxobj.open('POST', url, true);
this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.ajaxobj.setRequestHeader("Content-length", parameters.length);
this.ajaxobj.setRequestHeader("Connection", "close");
this.ajaxobj.send(parameters);
}
}
function executeAjax(s,q){
	ajaxpack.getAjaxRequest(s, q, executeAjax_cb, "txt")
}

function executeAjax_cb(){
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
			if (myfiletype=="txt")	eval(myajax.responseText);
		}
	}
	
}
function checkUsername(){
	var username = document.getElementById('username').value
	errDiv = document.getElementById("err_user")
	if(username.length >= 5){
		executeAjax('starttrial.asp','do=checkusername&u=' + username)
	}else{
		errDiv.innerHTML='Your username must be at least 5 characters long';
		document.getElementById('userreq').style.visibility="visible";
		document.getElementById('usertick').style.visibility="hidden";
	}
	
}
