
function resultsPageLoad(){
    fillYear();
    if (document.cookie && document.cookie != "")
    {
        readCookie();
    }
    
    
    //alert("PAGE LOAD");
}

function loadDefaultResults(){
    fillYear();
    if (document.cookie && document.cookie != "")
    {
        readCookie();
    } else {
        document.drop_list.Year.selectedIndex = 1;
        SelectSubCatWeek();
        document.drop_list.Week.selectedIndex = 1;
    }
    var yearSelectedIndex = document.drop_list.Year.selectedIndex;
    var weekSelectedIndex = document.drop_list.Week.selectedIndex;
    var weekSelected = document.drop_list.Week.options[weekSelectedIndex].value;
    
    document.drop_list.hdn_firstWeek.value = weekSelected;
    //alert ("Week Selected: " + weekSelected);
}

function fillYear(){ 
 // this function is used to fill the Year list on load
    addOption(document.drop_list.Year, "2009", "2009", "");
    addOption(document.drop_list.Year, "2008", "2008", "");
 //   addOption(document.drop_list.Year, "2007", "2007", "");
 //   addOption(document.drop_list.Year, "2006", "2006", "");
 //   addOption(document.drop_list.Year, "2005", "2005", "");
}


function SelectSubCatWeek(){
    // ON selection of Year this function will work

    // 1st Division 1st XI //
    removeAllOptions(document.drop_list.Week);
    addOption(document.drop_list.Week, "", "- Select Week -", "");
    //
    
    // Match dates for 2009. Most recent match at the top.
    if(document.drop_list.Year.value == '2009'){
	addOption(document.drop_list.Week,"050909", "Sat 05th Sep");
	addOption(document.drop_list.Week,"290809", "Sat 29th Aug");
	addOption(document.drop_list.Week,"220809", "Sat 22nd Aug");
	addOption(document.drop_list.Week,"150809", "Sat 15th Aug");
	addOption(document.drop_list.Week,"080809", "Sat 08th Aug");
	addOption(document.drop_list.Week,"010809", "Sat 01st Aug");
        addOption(document.drop_list.Week,"250709", "Sat 25th Jul");
        addOption(document.drop_list.Week,"180709", "Sat 18th Jul");
        addOption(document.drop_list.Week,"110709", "Sat 11th Jul");
        addOption(document.drop_list.Week,"040709", "Sat 04th Jul");
        addOption(document.drop_list.Week,"270609", "Sat 27th Jun");
        addOption(document.drop_list.Week,"200609", "Sat 20th Jun");
        addOption(document.drop_list.Week,"130609", "Sat 13th Jun");
        addOption(document.drop_list.Week,"060609", "Sat 06th Jun"); 
        addOption(document.drop_list.Week,"300509", "Sat 30th May");
        addOption(document.drop_list.Week,"230509", "Sat 23rd May");
        addOption(document.drop_list.Week,"160509", "Sat 16th May");
        addOption(document.drop_list.Week,"090509", "Sat 09th May");
        addOption(document.drop_list.Week,"020509", "Sat 02nd May");
    }
    
    // Match dates for 2008
    if(document.drop_list.Year.value == '2008'){
        addOption(document.drop_list.Week,"060908", "Sat 06th Sept");
        addOption(document.drop_list.Week,"300808", "Sat 30th Aug");
        addOption(document.drop_list.Week,"230808", "Sat 23rd Aug");
        addOption(document.drop_list.Week,"160808", "Sat 16th Aug");
        addOption(document.drop_list.Week,"090808", "Sat 09th Aug");
        addOption(document.drop_list.Week,"020808", "Sat 02nd Aug");
        addOption(document.drop_list.Week,"190708", "Sat 19th Jul");
        addOption(document.drop_list.Week,"120708", "Sat 12th Jul");
    //    addOption(document.drop_list.Week,"050708", "Sat 05th Jul");
    //    addOption(document.drop_list.Week,"280608", "Sat 28th Jun");
    //    addOption(document.drop_list.Week,"210608", "Sat 21st Jun");
    //    addOption(document.drop_list.Week,"140608", "Sat 14th Jun");
    //    addOption(document.drop_list.Week,"070608", "Sat 07th Jun");
    //    addOption(document.drop_list.Week,"310508", "Sat 31st May");
    //    addOption(document.drop_list.Week,"240508", "Sat 24th May");
    //    addOption(document.drop_list.Week,"170508", "Sat 17th May");
    //    addOption(document.drop_list.Week,"100508", "Sat 10th May");
    //    addOption(document.drop_list.Week,"030508", "Sat 03rd May");
    }
    // Match dates for 2007
    if(document.drop_list.Year.value == '2007'){
        addOption(document.drop_list.Week,"080907", "Sat 08th Sept");
        addOption(document.drop_list.Week,"010907", "Sat 01st Sept");
        addOption(document.drop_list.Week,"250807", "Sat 25th Aug");
        addOption(document.drop_list.Week,"180807", "Sat 18th Aug");
        addOption(document.drop_list.Week,"110807", "Sat 11th Aug");
        addOption(document.drop_list.Week,"040807", "Sat 04th Aug");
        addOption(document.drop_list.Week,"280707", "Sat 28th Jul");
        addOption(document.drop_list.Week,"210707", "Sat 21st Jul");
        addOption(document.drop_list.Week,"140707", "Sat 14th Jul");
        addOption(document.drop_list.Week,"070707", "Sat 07th Jul");
        addOption(document.drop_list.Week,"300607", "Sat 30th Jun");
        addOption(document.drop_list.Week,"240607", "Sun 24th Jun");
        addOption(document.drop_list.Week,"230607", "Sat 23rd Jun");
        addOption(document.drop_list.Week,"170607", "Sun 17th Jun");
        addOption(document.drop_list.Week,"160607", "Sat 16th Jun");
        addOption(document.drop_list.Week,"100607", "Sun 10th Jun");
        addOption(document.drop_list.Week,"090607", "Sat 09th Jun");
        addOption(document.drop_list.Week,"030607", "Sun 03rd Jun");
        addOption(document.drop_list.Week,"020607", "Sat 02nd Jun");
        addOption(document.drop_list.Week,"270507", "Sun 27th May");
        addOption(document.drop_list.Week,"260507", "Sat 26th May");
        addOption(document.drop_list.Week,"200507", "Sun 20th May");
        addOption(document.drop_list.Week,"190507", "Sat 19th May");
        addOption(document.drop_list.Week,"130507", "Sun 13th May");
        addOption(document.drop_list.Week,"120507", "Sat 12th May");
        addOption(document.drop_list.Week,"060507", "Sun 06th May");
        addOption(document.drop_list.Week,"050507", "Sat 05th May");
        addOption(document.drop_list.Week,"290407", "Sun 29th Apr");
        addOption(document.drop_list.Week,"280407", "Sat 28th Apr");
        addOption(document.drop_list.Week,"220407", "Sun 22nd Apr");
        addOption(document.drop_list.Week,"210407", "Sat 21st Apr");
    }
    // Match dates for 2006
    if(document.drop_list.Year.value == '2006'){
        addOption(document.drop_list.Week,"020906", "Sat 02nd Sept");
        addOption(document.drop_list.Week,"260806", "Sat 26th Aug");
        addOption(document.drop_list.Week,"190806", "Sat 19th Aug");
        addOption(document.drop_list.Week,"120806", "Sat 12th Aug");
        addOption(document.drop_list.Week,"050806", "Sat 05th Aug");
        addOption(document.drop_list.Week,"290806", "Sat 29th Jul");
        addOption(document.drop_list.Week,"220706", "Sat 22nd Jul");
        addOption(document.drop_list.Week,"150706", "Sat 15th Jul");
        addOption(document.drop_list.Week,"080706", "Sat 08th Jul");
        addOption(document.drop_list.Week,"010706", "Sat 01st Jul");
        addOption(document.drop_list.Week,"240606", "Sat 24th Jun");
        addOption(document.drop_list.Week,"170606", "Sat 17th Jun");
        addOption(document.drop_list.Week,"100606", "Sat 10th Jun");
        addOption(document.drop_list.Week,"030606", "Sat 03rd Jun");
        addOption(document.drop_list.Week,"270506", "Sat 27th May");
        addOption(document.drop_list.Week,"200506", "Sat 20th May");
        addOption(document.drop_list.Week,"130506", "Sat 13th May");
        addOption(document.drop_list.Week,"060506", "Sat 06th May");
        addOption(document.drop_list.Week,"290406", "Sat 29th Apr");
    }
    // Match dates for 2005
    if(document.drop_list.Year.value == '2005'){
        addOption(document.drop_list.Week,"030905", "Sat 03rd Sept");
        addOption(document.drop_list.Week,"270805", "Sat 27th Aug");
        addOption(document.drop_list.Week,"200805", "Sat 20th Aug");
        addOption(document.drop_list.Week,"130805", "Sat 13th Aug");
        addOption(document.drop_list.Week,"060805", "Sat 06th Aug");
        addOption(document.drop_list.Week,"300705", "Sat 30th Jul");
        addOption(document.drop_list.Week,"230705", "Sat 23rd Jul");
        addOption(document.drop_list.Week,"160705", "Sat 16th Jul");
        addOption(document.drop_list.Week,"090705", "Sat 09th Jul");
        addOption(document.drop_list.Week,"020705", "Sat 02nd Jul");
        addOption(document.drop_list.Week,"250605", "Sat 25th Jun");
        addOption(document.drop_list.Week,"180605", "Sat 18th Jun");
        addOption(document.drop_list.Week,"110605", "Sat 11th Jun");
        addOption(document.drop_list.Week,"040605", "Sat 04th Jun");
        addOption(document.drop_list.Week,"280505", "Sat 28th May");
        addOption(document.drop_list.Week,"210505", "Sat 21st May");
        addOption(document.drop_list.Week,"140505", "Sat 14th May");
        addOption(document.drop_list.Week,"070505", "Sat 07th May");
    }
}


function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}


function showDiv1_1_XI()
{
    var yearSelected = document.drop_list.Year.selectedIndex;
    var weekSelected = document.drop_list.Week.selectedIndex
    var dateSelected = "1ST_DIV_1ST_XI_" + document.drop_list.Week.options[weekSelected].value;
    var divSelected = "1_1";
    var url_add = 'results.htm?year=' + document.drop_list.Year.options[yearSelected].value + '&match=' + dateSelected + '&div=' + divSelected;
    // Set the cookie
    setCookie();
    // Redirect the result page
    window.location.href = url_add;
}
function showDiv1_2_XI()
{
    var yearSelected = document.drop_list.Year.selectedIndex;
    var weekSelected = document.drop_list.Week.selectedIndex
    var dateSelected = "1ST_DIV_2ND_XI_" + document.drop_list.Week.options[weekSelected].value;
    var divSelected = "1_2";
    var url_add = 'results.htm?year=' + document.drop_list.Year.options[yearSelected].value + '&match=' + dateSelected + '&div=' + divSelected;
    // Set the cookie
    setCookie();
    // Redirect the result page
    window.location.href = url_add;
}
function showDiv2_1_XI()
{
    var yearSelected = document.drop_list.Year.selectedIndex;
    var weekSelected = document.drop_list.Week.selectedIndex
    var dateSelected = "2ND_DIV_1ST_XI_" + document.drop_list.Week.options[weekSelected].value;
    var divSelected = "2_1";
    var url_add = 'results.htm?year=' + document.drop_list.Year.options[yearSelected].value + '&match=' + dateSelected + '&div=' + divSelected;
    // Set the cookie
    setCookie();
    // Redirect the result page
    window.location.href = url_add;
}
function showDiv2_2_XI()
{
    var yearSelected = document.drop_list.Year.selectedIndex;
    var weekSelected = document.drop_list.Week.selectedIndex
    var dateSelected = "2ND_DIV_2ND_XI_" + document.drop_list.Week.options[weekSelected].value;
    var divSelected = "2_2";
    var url_add = 'results.htm?year=' + document.drop_list.Year.options[yearSelected].value + '&match=' + dateSelected + '&div=' + divSelected;
    // Set the cookie
    setCookie();
    // Redirect the result page
    window.location.href = url_add;
}



function querySt(variable)
{
    query = window.location.search.substring(1);
    var vars = query.split('&');
    for (i=0;i<vars.length;i++)
    {
        var pair = vars[i].split('=');
        if (pair[0] == variable)
        {
            return pair[1];
        }
    }
}
//var YearToShow = querySt('year');
//var MatchToShow = querySt('match');


//
function readCookie()
// This function reads the cookie and sets the index of the year droplist to whatever's in the cookie
{
    var cookie_stuff = unescape(document.cookie);
    var crumbs = cookie_stuff.split('_xXx_');
    //document.getElementById("txt_target").innerHTML = cookie_stuff;
    document.drop_list.Year.selectedIndex = crumbs[0];
    SelectSubCatWeek();
    document.drop_list.Week.selectedIndex = crumbs[1];
    
    //alert (crumbs[0] + " - " + crumbs[1] + " - " + crumbs[2] + " - " + crumbs[3] + " - " + crumbs[4]);
    //alert (cookie_stuff);
}
//
function setCookie()
{
    // The following lines are to kill (or delete) any existing cookie data before creating a new one
    var kill_time = new Date('January 1, 1970');
    var kill_string = "stub;expires=" + kill_time.toGMTString();
    document.cookie = kill_string;
    //
    // The following line takes a note of the selected year index and writes it in a cookie
    var yearViewed = document.drop_list.Year.selectedIndex;
    var weekViewed = document.drop_list.Week.selectedIndex;
    //
    var exdate = new Date();
    var expiredays = 1;
    exdate.setDate(exdate.getDate() + expiredays);
    //
    // The cookie is written in the following order: Year, division1Viewed, division2Viewed, division3Viewed, division4Viewed, then the expiry date
    document.cookie = (escape(yearViewed)) + ("_xXx_" + escape(weekViewed)) + (";expires=" + exdate.toGMTString()); 
       
    //document.cookie = "Year=" + escape(yearViewed)+((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());    
    //alert("Cookie value: " + yearViewed);
}
function killCookie()
// Use this function on a button to immediately kill a cookie
{
    // The following lines are to kill (or delete) any existing cookie data before creating a new one
    var kill_time = new Date('January 1, 1970');
    var kill_string = "stub;expires=" + kill_time.toGMTString();
    document.cookie = kill_string;
}
