<!--
	// Dewsbury Road Runners
	// Javascript extension library 	
	
	var lngSelectedTab = 1;
	
	//given the path, work out the new title image source and set it
	function UpdateTitle(strPath) {
	
		//fix the title element
		var elTitleImage = document.getElementById('titleImage');			

		//extract the page
		var lngDot1 = strPath.lastIndexOf('.php');
		var lngDot2 = strPath.indexOf('param1=');
		var strPage = strPath.substring(lngDot2 + 7, lngDot1);
		
		//alert(strPage);
		
		//now alter the page string for some cases
		switch (strPage.toLowerCase()) {
		
			case 'gallerypic' : case 'story' : case 'resultsquery' : case '.php?action=1&param1=' :
				strPage = '';
				break;
			case 'bobgraham' :
				strPage = 'News';
				break;
			case 'agallery' :
				strPage = 'Gallery';
				break;	
			case 'forum_view_post' : case 'forum_new_post' : case 'forum_store_new_post' :
				strPage = 'Forum';
				break;
			case 'headtohead' :
				strPage = 'Results';
				break;					
			case 'profile' : case "clubprofile" :
				strPage = 'Profiles';
				break;		
			case 'guestbooksigner' :
				strPage = 'Guestbook';
				break;
			case 'ranking_member_calculation' : case 'ranking_calculation' : case 'rankingsadjusted' : case 'ratingsforraces' : case 'rankingsracetimepredictor' : case 'clubrankings' : case 'clubrankingsupdate' : case 'rankingsoverall' :
				strPage = 'Rankings';
				break;
			case '10ksearch' : case '10ksearchquery' : case '10kentrantlist' :
				strPage = '10k';
				break;					
			case 'galleryeditor' :
				strPage = 'Gallery';
				break;
			case 'raceeditor' : case 'eventeditor' : case 'eventeditor2' : case 'stageeditor' : case 'resulteditor' : case 'resultboard' :
				strPage = 'Events';
				break;
			case 'membereditor' : case 'reports' : case 'clubeditor' : case 'aliaseditor' : case 'clubmembereditor' : case 'clubresult' : case 'attentions' : case 'clubresulteditor' : case 'memberduplicates' : case 'membertruncations' :
				strPage = 'Admin';
				break;	
			
		}
		
		//alert(strPage);
		strPage = strPage.substring(0,1).toUpperCase() + strPage.substring(1, strPage.length);
		
		//set the image source if we need to
		if (strPage != '') { elTitleImage.src = '../Images/title' + strPage + '.gif'; }
	
	}
	
	//function to flash color of an element 
	function flashtext( strElementID ) {

       var textcolours = Array( '#FFFFFF', '#EEEEEE', '#DDDDDD', '#CCCCCC', '#BBBBBB', '#AAAAAA' );
       var colour = Math.round( textcolours.length * Math.random() );

       if (document.getElementById( strElementID )) {
            document.getElementById( strElementID ).style.color = textcolours[ colour ];
       } 

    } 
	
	function setOpacityIE(element, opacity) {
		document.getElementById(element).style.filter = "alpha(opacity=" + opacity + ")";	
	}
	
	function setOpacityFirefox(element, opacity) {
		document.getElementById(element).opacity = opacity;
	}
	
	var fadecounter;
		
	//function which will fade out an element
	function fadeIE(element, opacity) {
		
		opacity = opacity - 10;
		
		//set new opacity
		setOpacityIE(element, opacity);
		//alert(opacity);
		
		//if we have more steps to go, set timer
		if (opacity > 0) {
			fadecounter = setTimeout('fadeIE(\'' + element + '\',' + opacity + ')', 2);
		}
		//otherwise end
		else {
		}
		
	}
	
	//function which will brighten an element
	function brightenIE(element, opacity) {
		
		opacity = opacity + 10;
		
		//set new opacity
		setOpacityIE(element, opacity);
		//alert(opacity);
		
		//if we have more steps to go, set timer
		if (opacity < 60) {
			fadecounter = setTimeout('brightenIE(\'' + element + '\',' + opacity + ')', 2);
		}
		//otherwise end
		else {
			document.getElementById(element).style.filter = "";
		}
		
	}
	
	function showTab(lngTab) {
				
		//change tab display
		document.getElementById('TabTable' + lngSelectedTab).className = 'TabNoSelect';
		document.getElementById('TabTable' + lngSelectedTab).style.backgroundColor = 'RGB(180,180,180)';
		//hide current tab info	
		document.getElementById('Tab' + lngSelectedTab).style.display = 'none';
				
		//record new tab
		lngSelectedTab = lngTab;
		
		//change tab display
		document.getElementById('TabTable' + lngSelectedTab).className = 'TabSelect';
		document.getElementById('TabTable' + lngSelectedTab).style.backgroundColor = 'RGB(255,255,255)';
		
		//show new tab info
		document.getElementById('Tab' + lngSelectedTab).style.display = '';
	
	}
	
	function highlightTab(lngTab, blSwitch) {
	
		if (lngTab != lngSelectedTab) {
			if (blSwitch) {
				document.getElementById('TabTable' + lngTab).style.backgroundColor = 'RGB(210,210,210)';
			}		 
			else {
				document.getElementById('TabTable' + lngTab).style.backgroundColor = 'RGB(180,180,180)';
			}		
		}	
	
	}
	
	//Main javascript update function
	function AddNewFormRow(elSelect) {
		
		//get the row number
		lngRow = elSelect.id.substring(12, elSelect.id.length) * 1;
		
		//get the TBODY node
		var elTBODY = document.getElementById('resultsTable').getElementsByTagName('TBODY')[0];
		
		//get the number of rows
		var lngRows = elTBODY.childNodes.length;
		
		//If we are not looking at the last row then exit
		if (lngRow != lngRows - 1) { return null; }
		
		//make a copy of the last row
		var elRow = elTBODY.childNodes[lngRows - 1];
		var elNewRow = elRow.cloneNode(true);
		
		//make a list of all the table form elements
		arrInputElements = elNewRow.getElementsByTagName('input');
		arrSelectElements = elNewRow.getElementsByTagName('select');
		//cycle thru them changing the id
		for (var i=0; i < arrInputElements.length; i++) {
			var strNewID = arrInputElements[i].id.replace(lngRow + '', lngRows + '');
			arrInputElements[i].id = strNewID;
			arrInputElements[i].name = strNewID;			
		}
		for (var i=0; i < arrSelectElements.length; i++) {
			var strNewID = arrSelectElements[i].id.replace(lngRow + '', lngRows + '');
			arrSelectElements[i].id = strNewID;
			arrSelectElements[i].name = strNewID;				
		}
		
		//update vet category
		var elDiv = elNewRow.getElementsByTagName('DIV')[0];
		elDiv.id = 'VetCategoryString' + lngRows;
		elDiv.innerHTML = "M";
		//alert(elDiv.id);
			
		//now insert it into the table
		//alert(elNewRow.innerHTML);
		elTBODY.appendChild(elNewRow);
		
		//Set vet category 2 to default
		document.getElementById('SecondVetCategory' + lngRows).value = 17;
		
	}
	
	//functions to calculate vet category details
	function getCat(age, sex, blString) {

		if (sex != 2) {
			if (age < 35) {
				cat = 17; strCat ="M";
			}		
			else if (age < 40) {
				cat = 18; strCat ="M35";
			}
			else if (age<75) {
				cat = Math.floor(age/5)+1; strCat = "M" + (Math.floor(age/5)*5) ;
			} 
			else {
				cat = 15; strCat ="M70";
			}
		}
		else {
			if (age < 35) {
				cat = 16; strCat ="F";
			}
			else if (age<75) {
				cat = Math.floor(age/5)-6; strCat = "F" + (Math.floor(age/5)*5) ;
			} 
			else {
				cat = 8; strCat ="F70";
			}
		}
	
		if (blString) { return strCat; } else { return cat; }
		
	}
	
	function getCatString(age, sex) {
		return getCat(age, sex, true);
	}
	
	//Update Vet Categories function
	function UpdateVetCategory(elCombi, strEventDate) {
	
		//if we have no event date, try and get it from the form details
		if (strEventDate == '') {
			lngDay = document.getElementById('Day').value;
			lngMonth = document.getElementById('Month').value;
			lngYear = document.getElementById('Year').value;
			strDay = (lngDay > 9)?''+lngDay:'0'+lngDay;
			strMonth = (lngMonth > 9)?''+lngMonth:'0'+lngMonth;
			
			strEventDate = strDay + strMonth + lngYear;
		}
	
		var strID = elCombi.id;
		var lngDOB = strID.lastIndexOf('DOB');
		var lngRow = strID.substring(lngDOB + 3, strID.length)
		//alert(lngRow);
		
		//first get the selected value
		var strValue = document.getElementById('MemberSexDOB' + lngRow).value;
		
		//get the gender and DOB
		var lngAsterisk = strValue.lastIndexOf('*');
		var strGender = strValue.substring(lngAsterisk + 1, lngAsterisk + 2);
		var strDOB = strValue.substring(lngAsterisk + 2, strValue.length);
		
		//calculate member age at event date				
		var lngDOBDay = strDOB.substring(0,2);
		var lngDOBMonth = strDOB.substring(2,4);
		var lngDOBYear = strDOB.substring(4,8);
		var lngEventDay = strEventDate.substring(0,2);
		var lngEventMonth = strEventDate.substring(2,4);
		var lngEventYear = strEventDate.substring(4,8);

		//calculate age
		var lngAge = lngEventYear - lngDOBYear;
		
		if (lngDOBMonth > lngEventMonth) { lngAge--; }
		else {
			if (lngDOBMonth == lngEventMonth) {
				if (lngDOBDay > lngEventDay) { lngAge--; }
			}	
		}
		
		//if the age is silly, make it arbitrarily low
		if (lngAge > 150) { lngAge = 10; }
		
		//Now get the category name and number
		var lngCat = getCat(lngAge, strGender);
		var strCat = getCatString(lngAge, strGender);
		
		//set the options on the form
		document.getElementById('VetCategoryString' + lngRow).innerHTML = strCat;
		document.getElementById('VetCategory' + lngRow).value = lngCat;
		document.getElementById('SecondVetCategory' + lngRow).value = lngCat;
		
	}
		
	//***************
	//AJAX FUNCTIONS
	//***************
	
	//function which sets up a ajax post for form data
	function xmlhttpPostFormData(WebServicePath, Destination, strForm, Nav, strJavascript) {
	
		xmlhttpPost(WebServicePath, Destination, Nav, PostData(strForm), strJavascript);
	
	}
	
	//function which sets up a ajax post and callback function
	function xmlhttpPost(WebServicePath, Destination, Nav, strPost, strJavascript) {
        
        var xmlHttpReq = false;
        var self = this;
        
        //if no form data to be posted, set to default
        if (!strPost) { strPost = ''; }
        
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
        }           
		
		//bit of a hack, only force a navigation and progress bar if Destination is contentarea i.e. a full AJAX page request
		if (Destination == "contentarea") {
			
			//set space to show progress timer
			injectHtml(Destination, "<br /><br /><br /><br /><br /><br /><div align='center'><img src='../Images/progress.gif' /></div>");
		 
			//if no inpage navigation required, set to default
			if (Nav) { Nav = '#' + Nav; } else { Nav = '#'; }
        
		}
		//otherwise no timer transition, no navigation
		else {
			Nav = "";
		}
		
        //configure the request
        self.xmlHttpReq.open('POST', WebServicePath, true);   
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                
        //set the callback, if there is a destination
        if (Destination) {
			self.xmlHttpReq.onreadystatechange = function() {
				if (self.xmlHttpReq.readyState == 4) {
				
					//update the title image
					UpdateTitle(WebServicePath);
					//alert(self.xmlHttpReq.responseText);
					injectHtml(Destination, self.xmlHttpReq.responseText);
					//brighten page
					//brightenIE('contentarea', 0);					
					//navigate to top of page or appropriate section
					if (Nav != "") { document.location=Nav; }
					
					//find any script tags in the destination element
					elScript = document.getElementById(Destination).getElementsByTagName('script');
					//execute them
					if (elScript.length >= 0) {
						for(var i=0;i<elScript.length;i++) {
							//alert("SCRIPT IS:" + elScript[i].text);
							eval(elScript[i].text);
						}
					}
					
					//run any custom javascript if we have any
					//if (strJavascript) { eval(strJavascript); }   
					
				}
			}
		}
		else {
			self.xmlHttpReq.onreadystatechange = function() {return;}
		}	
		
		//and make it
		self.xmlHttpReq.setRequestHeader('Content-length', strPost.length);
		self.xmlHttpReq.setRequestHeader('Connection', 'close');			
        self.xmlHttpReq.send(strPost);           
        
        //fade out original
        //fadeIE('contentarea', 60);
		
    }
	
	//principle callback function
	//inject strHtml into the HTML element with ID strID	    
	function injectHtml(strID, strHtml) {
	
		var element = document.getElementById(strID);
		
		element.innerHTML = strHtml;		
				
	}
	
	function clearElement(strID) {
	
		var element = document.getElementById(strID);
		
		element.innerHTML = '';		
				
	}	
			
	//function which returns any post data for a form as a string if it exists
	function PostData(strForm) {
	
		//initialise response string
		var strData = '';
		
		//get form element
		var frmThis = document.getElementById(strForm);		
		
		for (i=0; i < frmThis.length; i++) {
			if (frmThis.elements[i].type == "text") {
				//Handle Textbox's
				strData = strData + frmThis.elements[i].name + "=" + encodeURIComponent(frmThis.elements[i].value) + "&";
			}
			else if (frmThis.elements[i].type == "textarea") {
				//Handle textareas
				strData = strData + frmThis.elements[i].name + "=" + encodeURIComponent(frmThis.elements[i].value) + "&";
			}
			else if (frmThis.elements[i].type == "checkbox") {
				//Handle checkbox's
				strData = strData + frmThis.elements[i].name + "=" + frmThis.elements[i].checked + "&";
			}
			else if (frmThis.elements[i].type == "radio") {
				//Handle Radio buttons
				if (frmThis.elements[i].checked==true) {
					strData = strData + frmThis.elements[i].name + "=" + frmThis.elements[i].value + "&";
				}
			}
			else {
				//finally, this should theoretically be a select box.
				strData = strData + frmThis.elements[i].name + "=" + encodeURIComponent(frmThis.elements[i].value) + "&";
			}
		}		
		
		//alert('POST DATA=' + strData);
		
		return strData;
	
	}	
					
//-->