function onLoad(){} // error prevention

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function validateForm(theLink, msg){

    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (typeof(window.opera) != 'undefined') {
        return true;
    }
	
	mandatoryArray=Array(
		document.mainform.bill_name.value.length,
		document.mainform.bill_addr_1.value.length,
		document.mainform.bill_city.value.length,
		document.mainform.bill_country.value.length,
		document.mainform.bill_email.value.length);


	emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/

	is_confirmed=true;

	if (!emailRe.test(document.mainform.bill_email.value)){
		alert(document.mainform.bill_email.value + " doesnt look like a valid email");
		is_confirmed=false;
	}else{
		
		for (i=0;i<mandatoryArray.length; i++){
			if (mandatoryArray[i]==0){
				alert("you need to fill in all the fields with a * next to them");
				i=mandatoryArray.length;
				is_confirmed=false;
			}
		}
	}

	if (is_confirmed==true){
		if (document.mainform.terms.checked!=true){
			alert("you need to confirm that you agree with the terms and conditions");
			is_confirmed=false;
		}
	}
		
	return is_confirmed;
} // end of the 'confirmLink()' function



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function twoLists(word,url){
	
	ajaxGeneric('list2',url + "&drawList=2&searchStr=" + word);
	setTimeout("ajaxGeneric('list3','" + url + "&drawList=3&searchStr=" + word +  "')",500);

}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function predict(val,lim,resultSpace,sqlID,listlen,urlStr){

	if(val.length>lim || lim==0){

		outnode=document.getElementById(resultSpace);
		outnode.innerHTML='<img src="images/admin/wait.gif" alt="loading" />';

		url='lib/ajax/predictiveText.php?val=' + val + '&sqlID=' + sqlID + '&listlen=' + listlen + '&urlStr=' + urlStr;
		ajaxGeneric(resultSpace,url);
	}

	obj=document.getElementsByName(resultSpace + 'Link');

	for (i=0;i<obj.length ; i++){
		obj[i].style.backgroundColor="";
	}
}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function competitionStore(str){

	arr=str.split("&");			

	var choice=Array();

	for (i=0;i<arr.length; i++ ){
		choice[i]=arr[i].split('=')
		//		obj=document.getElementById(objStub + n[1]);	// the form ID is starts from 1, the list id from 0
//		obj.value=i+1;

	}
	
	if (i>9){
		Effect.Fade('instructions'); 
		Effect.Appear('loginForm',{from: 0.0, to: 0.90}); 
		Effect.Appear('loginArrow'); 
	}

}

// clicks below a certain point
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function chkBelowHere(id,formName){
	
	var frm=document.getElementById(formName);
	var fromHere='';

	for (i=0;i<frm.elements.length ;i++ ){
		if (frm.elements[i].type=='checkbox'){
			if (frm.elements[i].name=='chk_'+ id){
				fromHere='y';
			}
			if (fromHere!=''){
				frm.elements[i].checked=true;
			}else{
				frm.elements[i].checked=false;
			}
		}

		if (id<0){
			frm.elements[i].checked=false;
		}
		
	}
}

// filters by a certain value - there needs to be a hidden element craftily named so it's basically a prefix in front of the checkbox
// eg chk_123 leads to a name filter field called name_chk_123
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function filterClicks(formName,hiddenPrefix,val){
	
	var frm=document.getElementById(formName);
	var fromHere='';

	for (i=0;i<frm.elements.length ;i++ ){
		if (frm.elements[i].type=='checkbox'){
			if (frm.elements[i].checked=true){
			
				 hidden=document.getElementById(hiddenPrefix + frm.elements[i].name);
		
				if (hidden.value!=val){
					frm.elements[i].checked=false;
				}
			}
		}
	}
}




// changes the values of a sortable list
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function sortListSubmit(str,objStub){
	
	// there has got to be a way of passing an array directly. Don't know what it is though.

	arr=str.split("&");			

	for (i=0;i<arr.length; i++ ){
		n=arr[i].split('=')
		obj=document.getElementById(objStub + n[1]);	// the form ID is starts from 1, the list id from 0
		obj.value=i+1;

	}
}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function flipMenus(show,hide1,hide2){
	
	flip(show + 'Div');
	flip(show + 'Up');	
	flip(show + 'Down');

	obj=document.getElementById(show + 'Div');

	if (obj.style.display!='none'){

		var obj1=document.getElementById(hide1 + 'Div');
		var obj2=document.getElementById(hide1 + 'Up');
		var obj3=document.getElementById(hide1 + 'Down');

		obj1.style.display='none';
		obj2.style.display='none';
		obj3.style.display='block';

		var obj1=document.getElementById(hide2 + 'Div');
		var obj2=document.getElementById(hide2 + 'Up');
	  	var obj3=document.getElementById(hide2 + 'Down');

        if (obj1 != null){
			obj1.style.display='none';
			obj2.style.display='none';
			obj3.style.display='block';
		}
	}
}


// buttons float at side of screen
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function floatBtn(divName){
		var obj=document.getElementById(divName);
		if (obj!=null){
			obj.style.top=document.body.scrollTop+15;
		}
}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function putDate(){
	var now = new Date() ;
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') ;
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December') ;
	var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

	today =  days[now.getDay()] + " " +
				  date + " " +
				   months[now.getMonth()] + ", " +
					(y2k(now.getYear()));
	document.write(today);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function y2k(number) { return (number < 1000) ? number + 1900 : number; }


//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function flip(divName){
	var obj=document.getElementById(divName);

	if (obj.style.display=='none')	{
		obj.style.display='block';
	}else{
		obj.style.display='none';
	}
}



//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function keyCap() { //v1.0
	if (navigator.appName != 'Netscape') {
		if (window.event.keyCode == 13){
			loginform.submit();
		}
	}
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function showDiv(divName){

	var obj=document.getElementById(divName);
		obj.style.visibility='visible';
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------
function confirmLink(theLink, msg)
{
    // Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm( msg);

	return is_confirmed;
} // end of the 'confirmLink()' function


// for tree
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function showMenu( divNum,minusGif, plusGif,nodeChr){

    if (getIdProperty( nodeChr + divNum, "display") != "block" )
    {
        setIdProperty(nodeChr + divNum, "display", "block");
        document.images[nodeChr+ "img" + divNum].src = minusGif;
		document.cookie= nodeChr + "_" + divNum + '=y';
    }
    else
    {
        setIdProperty(nodeChr + divNum, "display", "none");
        document.images[nodeChr + "img" + divNum].src = plusGif;
		document.cookie=nodeChr + "_" + divNum + "=''; expires=Sun, 09-Nov-97 01:00:00";

    }
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function getIdProperty( id, property ){

        var styleObject = document.getElementById( id );
        if (styleObject != null){
            styleObject = styleObject.style;
            if (styleObject[property]) {
                return styleObject[ property ];
            }
        }
        styleObject = getStyleBySelector( "#" + id );
        return (styleObject != null) ?
            styleObject[property] :
            null;

}

// turns on all the hidden edit buttons.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function switchAllButtons(){	 
	

	for (i=0;i<document.images.length; i++){
		if (document.images[i].id.length>0){
			obj=document.getElementById(document.images[i].id);
			if (obj.style.display=='none')	{
				obj.style.display='block';
				document.cookie="btnsOn'=y'";
			}else{
				obj.style.display='none';
				document.cookie="btnsOn'=n'";
			}
		}
	}
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function setIdProperty( id, property, value ){

        var styleObject = document.getElementById( id );
        if (styleObject != null){
            styleObject = styleObject.style;
            styleObject[ property ] = value;
       }
  }

// ajax stuff - based on Richard's code.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------

function xhtmlresultcallback(responseArray,resultParams) {
	//alert(responseArray['errstring']);

	ctlip=document.getElementById(resultParams['inputFieldId']);
	if(responseArray['result']=='OK') {
		ctlip.style.color='green';	
	} else {
	ctlip.style.color='red';	

	outnode=document.getElementById(resultParams['resultSpaceId']);

	offset=responseArray['errbyte'];
	outnode.innerHTML=responseArray['report']+'<br />'+responseArray['errstring']+"<br /> <a href=\"javascript:highlightError('htmltemplate','"+offset+"')\">Attempt to highlight</a> (won't scroll!)<br /> To find the error, highlight it, hold down SHIFT, and press right-arrow<p><a href=\"javascript:auto_html_to_xhtml('" + resultParams['inputFieldId']  + "')\">Convert to (dirty) XHTML for manual fix</a> (may not change simple templates)</p>";	
	}
}


//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function highlightError(ctl,offset) {
	selectRange(ctlip,offset-1,offset);
	ctlip.focus();
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function selectRange(ctl,from,to) {
	if( ctl.setSelectionRange ) {
		ctl.setSelectionRange(from,to);
	} 
	else if( ctl.createTextRange ) {
		range = ctl.createTextRange();
		range.collapse(true);
		range.moveEnd('character',to);
		range.moveStart('character',from);
		range.select();
	}
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function auto_html_to_xhtml(ctlid) {

	ctl=document.getElementById(ctlid);
	html=ctl.value;

	xhtml=html.replace(/<br>/g,'<br />');

	xhtml=xhtml.replace(/\n\s*\n/g,"\n");
	xhtml=xhtml.replace(/(<img\s[^>]+[" ])>/g,"$1 />");
	xhtml=xhtml.replace(/(<meta\s[^>]+[" ])>/g,"$1 />");
	ctl.value=xhtml;
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function xmlhttpPost(ajaxURL, queryString, resultFunc, resultParams) {

	var xmlHttpReq = false;

	
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
		xmlHttpReq.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open('POST', ajaxURL, true);
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			eval(resultFunc + '(xmlHttpReq.responseText, resultParams );');
		}
	}
	xmlHttpReq.send(queryString);
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function ajaxValidateSingleInput(inputFieldId,resultSpaceId,ajaxURL,extraResponseHandler) {
// alert('VSA');

	inputField=document.getElementById(inputFieldId);
	queryString=(inputField.name+'='+escape(inputField.value));

	resultParams= new Array();
	resultParams['inputFieldId']=inputFieldId;
	resultParams['resultSpaceId']=resultSpaceId;
	resultParams['ajaxURL']=ajaxURL;
	resultParams['extraResponseHandler']=extraResponseHandler;

	xmlhttpPost(ajaxURL,queryString,'ajaxValidateSingleInputResponse',resultParams);	

}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function ajaxValidateSingleInputResponse(rawResponse,resultParams) {
	var outstr='';
// alert(rawResponse);
//document.write(rawResponse);
	responseArray= new Array();
	responseLines=rawResponse.split("\n");
	numResponseLines=responseLines.length;
	for(i=0;i<numResponseLines;i++) {
		responseParts=responseLines[i].split("|");
		responseArray[responseParts[0]]=responseParts[1];
	}

	if(resultParams['resultSpaceId'].length) {
		resultSpace=document.getElementById(resultParams['resultSpaceId']);
		resultSpace.innerHTML=outstr+responseArray['report'];
	}
	if(resultParams['extraResponseHandler']) {
		eval(resultParams['extraResponseHandler'] + '(responseArray,resultParams);');
	}
}










//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function ajaxShowProcessList(resultSpaceId,ajaxURL){

	

	resultParams= new Array();
	resultParams['resultSpaceId']=resultSpaceId;
	resultParams['ajaxURL']=ajaxURL;

	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
		xmlHttpReq.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open('POST', ajaxURL, true);
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			eval('stringToDiv(xmlHttpReq.responseText, resultParams );');
		}
	}
		xmlHttpReq.send('');	// we're not actually sending any params atm
	
	setTimeout("ajaxShowProcessList('" + resultSpaceId + "', '" + ajaxURL+  "')",1000);

}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function stringToDiv(str,resultParams) {

	outnode=document.getElementById(resultParams['resultSpaceId']);
	outnode.innerHTML=str;
}


//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function ajaxGeneric(resultSpaceId,ajaxURL){

	resultParams= new Array();
	resultParams['resultSpaceId']=resultSpaceId;
	resultParams['ajaxURL']=ajaxURL;

	outnode=document.getElementById(resultSpaceId);
	outnode.innerHTML='<img src="images/admin/wait.gif" alt="loading" />';


agent=navigator.userAgent.toLowerCase();
var isIE7=(agent.indexOf('msie 7') != -1)?1:0;
var isIE8=(agent.indexOf('msie 8') != -1)?1:0;


	// Mozilla/Safari
	if (window.XMLHttpRequest && isIE7!=1  && isIE8!=1) {

		xmlHttpReq = new XMLHttpRequest();
		xmlHttpReq.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open('POST', ajaxURL, true);
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			eval('stringToDiv(xmlHttpReq.responseText, resultParams );');
		}
	}
	xmlHttpReq.send('');	
}


//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function unitTester(resultSpaceId,ajaxURL,idCSV){

	var ids=idCSV.split(',');

	for (i=0; i<ids.length; i++){
		url=ajaxURL + '&testID=' + ids[i];
		ajaxGeneric(resultSpaceId,url);
	}

}






//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function ajaxTextPopulate(ajaxURL,fieldCsv){



	var fields=fieldCsv.split(',');


	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
		xmlHttpReq.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open('POST', ajaxURL, true);
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {

			var values=xmlHttpReq.responseText.split("||");

//	outnode=document.getElementById('ajax');
//	outnode.innerHTML=xmlHttpReq.responseText;

			for (i=0;i<fields.length;i++ )	{

				obj=document.getElementById(fields[i]);
				obj.value=values[i];
			}
		}
	}
	xmlHttpReq.send('');	
}




// ticks / unticks various groups of workouts.
//-----------------------------------------------------------------------------------------------------------------------------------------
function tickAll(type,formName,tag){

	
	var len=tag.length;
	var obj=document.getElementById(formName);

	for (i=0;i<obj.elements.length ; i++){

		if (typeof(obj.elements[i].name)!='undefined'){


			if (type=='untick'){
				if (obj.elements[i].name.substr(0,len)==tag){
					obj.elements[i].checked=false;
				}
			}

			if (type=='tickAll'){
				if (obj.elements[i].name.substr(0,len)==tag){
					obj.elements[i].checked=true;
				}
			}


			if (type=='tickFlip'){
				if (obj.elements[i].name.substr(0,len)==tag){
					if (obj.elements[i].checked==true){
						obj.elements[i].checked=false;
					}else{
						obj.elements[i].checked=true;
					}
				}
			}
		}
		
	}
}