

/**
 * Global Variable Declarations
 *
 *
 **/
var selEvent; //the selected Event ID (EID)
var oOption;
var raceOrOall=null;
var clubOrOpen=null;
var lastClubOrOpen=null;
var lastEvent=null;
var lastEventIndex=null;
var lastClass=null;
var lastClassIndex=null;
var lastDate=null;
var lastDateIndex=null;
var isNS, isIE


if (navigator.appName == "Netscape")
	isNS=true;
if (navigator.appVersion.indexOf("MSIE") != -1)
	isIE=true;


clubImg= new Image();
clubImg.src= "../images/clubCaption.gif";
openImg= new Image();
openImg.src= "../images/openCaption.gif";
raceImg= new Image();
raceImg.src= "../images/raceCaption.gif";
overallImg= new Image();
overallImg.src= "../images/overallCaption.gif";

function popPage(asppage, X1, Y1)
{
	var winX=screen.width;
  var winY=screen.height;
	if (!X1) X1=400;
	if (!Y1) Y1=250;
	
	var url;
	var infoWnd;
	url = asppage ;
	windowparam="toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=no,scrollbars=no,width="+X1+",height="+Y1+",top="+(winY-Y1-20)/2+",left="+(winX-X1)/2;

	infoWnd = window.open(url,'info',windowparam);
	
	if (!infoWnd.opener)
		infoWnd.opener = self;

	if (infoWnd.focus != null)
		infoWnd.focus();	
}

function getSavedOptions()
{
	
var raceOrOall=getCookie("lastRaceOrOverall");
//default if not right in cookie
if (raceOrOall!='race' ||raceOrOall!='oall')
	raceOrOall='race';
	
lastClubOrOpen=getCookie("lastClubOrOpen");
//alert(lastClubOrOpen);
if (lastClubOrOpen=='open')
	{		
		clubOrOpen='open';
		document.thisForm.btnEvent.value="Show Club Events";
		document.images['clubCaption'].src=openImg.src;
		//flipBlocks('openCaption','clubCaption');
	}
	else  (lastClubOrOpen=='club')
	{
	
		clubOrOpen='club'
		document.thisForm.btnEvent.value="Show Open Events";
		//flipBlocks('openCaption','clubCaption');
		document.images['clubCaption'].src=clubImg.src;
	}

	
lastEvent=getCookie("lastEvent");
lastClass=getCookie("lastClass");
lastDate=getCookie("lastDate");

fillSelEvents();
//getResult();

}

function bodyOnload(){
/**
*	Fill the selectEvent select box with Club Events
*/
	//alert("about to run!");
	//getSavedOptions();

	//raceOrOall='race';	
	//clubOrOpen='open';
	//alert("raceorall="+raceOrall);
	//alert("clubOrOpen="+clubOrOpen);
	raceOrall = getCookie("lastRaceOrOverall");
	clubOrOpen = getCookie("lastClubOrOpen");
	
	if(getCookie("lastClubOrOpen")=='open'){
		clubOrOpen='open';
		document.thisForm.btnEvent.value="Show Club Events";
		document.images['clubCaption'].src=openImg.src;
	}else{
		clubOrOpen='club';
		document.thisForm.btnEvent.value="Show Open Events";
		document.images['clubCaption'].src=clubImg.src;
	}
	
	lastEvent=getCookie("lastEvent");
	lastClass=getCookie("lastClass");
	
	if (getCookie("lastRaceOrOverall")=="oall")
	{
		raceOrOall='oall';
		document.thisForm.btnOverall.value="Show Race Dates";
		document.images['dateCaption'].src=overallImg.src;
	}
	else
	{
		raceOrOall='race';
		document.thisForm.btnOverall.value="Show Overall Date";
		document.images['dateCaption'].src=raceImg.src;
	}
	
	lastDate = getCookie("lastDate");
		
	fillSelEvents();
	
	//getSavedOptions();
	//	onClubOrOpenClick();
	//	onSelectRaceOrOall();
}



function onClubOrOpenClick()
{
	if (document.thisForm.btnEvent.value=="Show Open Events")
	{
		clubOrOpen='open';
		document.thisForm.btnEvent.value="Show Club Events";
		document.images['clubCaption'].src=openImg.src;
			
	}
	else
	{
		clubOrOpen='club';
		document.thisForm.btnEvent.value="Show Open Events";
		document.images['clubCaption'].src=clubImg.src;
		
	}
	
	fillSelEvents();
}

function fillSelEvents()
{
/**
 *		This function is called by the CLUB or OPEN button.
 *		It removes the OPTIONs currently in the selectEvent SELECT box and
 *		adds the list of races corresponding to the button pressed.
 */

	

	
	//	document.thisForm.selectEvent.selectedIndex=0;
		
		//remove the current select box items
		with (document.thisForm.selectEvent)
		{
			for (var i = options.length; i > 0; i--)
			{
				options[i]=null;
				length--;
			}
			
			
				length += 1;
				options[0] = new Option('Select Event');
				options[0].value = 'null'; 
		}
		document.thisForm.selectEvent.length=1;
		

		//add the newly selected items
		for (i=0;i<EventsArray.length;i++){
			
			if (EventsArray[i][1]==clubOrOpen)
			{
				document.thisForm.selectEvent.length += 1;
				document.thisForm.selectEvent.options[document.thisForm.selectEvent.length-1] = new Option(EventsArray[i][2]);
				document.thisForm.selectEvent.options[document.thisForm.selectEvent.length-1].value = EventsArray[i][0];  

			}	
		}
		
		sortSelectArray(document.thisForm.selectEvent.options,"text");
		
		for(i=0;i<document.thisForm.selectEvent.length;i++)
		{
			if(document.thisForm.selectEvent.options[i].value ==lastEvent)
			{
				lastEventIndex=i;
				break;
			}
		}
		
		//if the last used event is in the select box pick it otherwise pick none.	
		if (lastEventIndex!=null)
		{
		if (lastEventIndex>0)
			document.thisForm.selectEvent.selectedIndex=lastEventIndex;
		else
			document.thisForm.selectEvent.selectedIndex=0;
		
			onSelectEvent();
		}
		else
			document.thisForm.selectEvent.selectedIndex=0;
		

		
}

function onSelectEvent()
{
/**
 *		This function is called by changing the selected Event.
 *		It removes the OPTIONs currently in the selectDate SELECT box and
 *		adds the list of date corresponding to the selected value in selectEvent.
 */

//Set up the date select 


		with (document.thisForm.selectDate)
		{
			for (var i = options.length; i > 0; i--)
			{
				options[i]=null;
				length--;
			}

				length += 1;
				options[0] = new Option('Select Date');
				options[0].value = 'null';  

		}
		
		//add the newly selected items
		with(document.thisForm.selectEvent)
		{
		//EID of event
		if(options.selectedIndex>0)
		 selEvent=options[options.selectedIndex].value;
		 else
		  selEvent=options[1].value;
		}

		for (i=0;i<ScheduleArray.length;i++){
						
			if (ScheduleArray[i][1]== selEvent && ScheduleArray[i][2]==raceOrOall)
			{
				
				document.thisForm.selectDate.length += 1;
				document.thisForm.selectDate.options[document.thisForm.selectDate.length-1] = new Option(ScheduleArray[i][3]);
				document.thisForm.selectDate.options[document.thisForm.selectDate.length-1].value = ScheduleArray[i][0];  
				//try to keep the same date if its an option	
					
				if(ScheduleArray[i][0] ==lastDate)
					lastDateIndex=document.thisForm.selectDate.length-1;
					
			}	
		}
		
		
		//if the last used date is in the select box pick it otherwise pick none.	
		if (lastDateIndex!=null)
			document.thisForm.selectDate.selectedIndex=lastDateIndex;
		else
			document.thisForm.selectDate.selectedIndex=0;
		
		//if there is only one option in the box then select it				
		//with(document.thisForm.selectDate)
		//		if (length==2)
		//		{
					//VIN Change was 1
		//			selectedIndex=0;
		//		}
		
		// xxxxxxxxxxxxxxx  reintroduced by jd 17/7/02 
		with(document.thisForm.selectDate)
				if (length==2)
				{
					//VIN was 1 - changed to 0
					// xxxxxxxxxxxxxxxx  vin set at 0 - changed back to 1 jd 17/7/02
					selectedIndex=1;
				}

		
//next set up the classes select box
		
		//remove the current select box items
		with (document.thisForm.selectClass)
		{
			for (var i = options.length; i > 0; i--)
			{
			
				options[i]=null;
				length--;
			}
				length += 1;
				options[0] = new Option('Select Class');
				options[0].value = 'null'; 
		}
		
		for (i=0;i<ClassArray.length;i++)
		{				
			if (ClassArray[i][0]== selEvent)
			{
				document.thisForm.selectClass.length += 1;
				document.thisForm.selectClass.options[document.thisForm.selectClass.length-1] = new Option(ClassArray[i][1]);
				document.thisForm.selectClass.options[document.thisForm.selectClass.length-1].value = ClassArray[i][1];  
				//try to keep the same date if its an option
				if(ClassArray[i][1] ==lastClass)
					lastClassIndex=document.thisForm.selectClass.length-1;
			}	
		}		
		
		//if the last used date is in the select box pick it otherwise pick none.	
		if (lastClassIndex!=null)
			document.thisForm.selectClass.selectedIndex=lastClassIndex;
		else
			document.thisForm.selectClass.selectedIndex=0;
		
		//if there is only one option in the box then select it				
				if (document.thisForm.selectClass.length==2)
					with(document.thisForm.selectClass)
							if (length==2)
							{
								//VIN was 1 - changed to 0
								// xxxxxxxxxxxxxxxx  vin set at 0 - changed back to 1 jd 17/7/02
								selectedIndex=1;	
							}
}

function onSelectRaceOrOall()
{
/**
 *		This function is called by clicking the race / overall button.
 *		It modifies the list of dates in the date select box to show race dates or overall
 *		dates.
 */

	lastClass=document.thisForm.selectClass.value;
	lastDate=document.thisForm.selectDate.value;

		
	if (document.thisForm.btnOverall.value=="Show Overall Date")
	{
		raceOrOall='oall';
		document.thisForm.btnOverall.value="Show Race Dates";
		document.images['dateCaption'].src=overallImg.src;
	}
	else
	{
		raceOrOall='race';
		document.thisForm.btnOverall.value="Show Overall Date";
		document.images['dateCaption'].src=raceImg.src;
	}
	
		onSelectEvent();

}

function getResult()
{
/**
 *		This function is called by pressing the Get Result button.
 *		It builds the SQL statement to send to the server.
 *		
 */
 // set error = true if any select box has a null entry
 var error=false;

	 //construct a url with the correct values in the querystring and pass open it in
	 //the results window.
	href="resultsTable.asp?SID="+
	document.thisForm.selectDate.options[document.thisForm.selectDate.selectedIndex].value+
"&Class="+escape(document.thisForm.selectClass.options[document.thisForm.selectClass.selectedIndex].value)+
"&EventName="+escape(document.thisForm.selectEvent.options[document.thisForm.selectEvent.selectedIndex].text)+
"&Date="+escape(document.thisForm.selectDate.options[document.thisForm.selectDate.selectedIndex].text);
//+"&raceOrOall="+escape(raceOrOall)+"&clubOrOpen="+escape(clubOrOpen)+"&lastRace="+escape(lastRace)+"&lastDate="+escape(lastDate);

	//debug:show requested url:
	//alert(href);
	if (document.thisForm.selectEvent.value=='null')
	{
		error=true;
		alert("Please select a Event");
	}
	if (document.thisForm.selectClass.value=='null')
	{
		error=true;
		alert("Please select a Class");
	}
	if (document.thisForm.selectDate.value=='null')
	{
		error=true;
		alert("Please select a Date");
	}	
	if(error==false)
	{
		//set cookie
		//alert(clubOrOpen);
			setCookie("lastClass",document.thisForm.selectClass.value);
			setCookie("lastDate",document.thisForm.selectDate.value);
			setCookie("lastEvent",document.thisForm.selectEvent.value);
			setCookie("lastClubOrOpen",clubOrOpen);
			setCookie("lastRaceOrOverall",raceOrOall);
		//url is ok so get results
		//parent.resultsTable.location.href=href;
		window.location.href=href;
	}
}


function sortSelectArray(arrayName, type)
{
/**
 *		This function sorts an array.
 *		You must specify whether to do a text, number or date sort
 *		
 *		@param arrayName the select object to be sorted
 */
 
if (type=="text"){

var Imax = arrayName.length;
   for (var i=1;i<(Imax-1); i++)
	{
    for (var j=i+1; j<Imax; j++)
		{
		if (arrayName[j].text < arrayName[i].text) 
			{
			var dummyi = new Option(arrayName[i].text);
			dummyi.value=arrayName[i].value;
			var dummyj = new Option(arrayName[j].text);
			dummyj.value=arrayName[j].value;
			arrayName[i] = dummyj;           
			arrayName[j] = dummyi;
			}
		}
	}
}//end text type

else if (type=="numbers"){
var Imax = arrayName.length;
   for (var i=0;i<(Imax-1); i++)
	{
    for (var j=i+1; j<Imax; j++)
		{
		if (Number(arrayName[j].text) < Number(arrayName[i]).text) 
			{
			var dummy =  arrayName[i];
			arrayName[i] = arrayName[j];           
			arrayName[j] = dummy;      
			}
		}
	}
}//end type numbers

else if (type=="date"){

var Imax = arrayName.length;
   for (var i=0;i<(Imax-1); i++)
	{
    for (var j=i+1; j<Imax; j++)
		{
		if (Date.parse(arrayName[j].text) < Date.parse(arrayName[i]).text) 
			{
			var dummy =  arrayName[i];
			arrayName[i] = arrayName[j];           
			arrayName[j] = dummy;      
			}
		}
	}
}//end type date
	
}//end sort function


function setCookie(name, value) 
{
  
//alert ('setCookie:'+name+':'+value);
	var today = new Date()
	var expires = new Date()
	expires.setTime(today.getTime() + 1000*60*60*24*365)

   document.cookie = name + "=" + escape(value)
   + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
}

function getCookie(Name) 
{
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         //alert('getCookie:'+Name+': '+unescape(document.cookie.substring(offset, end)));
         return unescape(document.cookie.substring(offset, end))
         
      } 
   }
}

