// These are some javsscript functions that are used in the events section.  Mainly for validating dates.

// document.write("THis is the external jas file");
//-----------------------------------------------------------------------------------------
function numDays()
{
    var theForm = document.frmNewFixture;
    var theValue = getCheckedValue(theForm.single_multi_day)

    // alert("event length radio has been changed to " + theValue);

    if(theValue == "Single")
    {
        theForm.end_sel_month_year.disabled = true;
        theForm.end_selDay.disabled = true;
    }
    if(theValue == "Multi")
    {
        theForm.end_sel_month_year.disabled = false;
        theForm.end_selDay.disabled = false;
    }

}
// //===========================================================================================
// function DaysInMonth(intMon,intYr)
// This function returns the number of days in the month for a given month and year.

function DaysInMonth(intMon,intYr)
{
    switch(parseInt(intMon,10))
    {
        case 2:		if ((intYr%100)==0)
                    {
                        if ((intYr%400)==0)
                            return 29;
                        else
                            return 28;
                    }
                    else if((intYr%4)==0)
                        return 29;
                    else
                        return 28;
                    break;

        case 4:		return 30;
                    break;

        case 6:		return 30;
                    break;

        case 9:		return 30;
        		    break;

        case 11:    return 30;
                    break;

        default:	return 31;
                    break;
    }
}
//===========================================================================================
// function setDays(theForm,y)
// This function is quite specific - to be used only with the create event form.
// When the user chooses a month / year combination for the start date of the event, this function
// is called.
// (1)  It changes the selected month / year combination for 'end date' to be the same as that for the
//      start date
// (2)  It changes the available options in the day / date list (for both start and end dates)
//      to be compatible with the chosen month / year combination
function setDays(x,y)
{
   var theForm = document.x;
   var i = 0;
   var num_options = theForm.start_selDay.length;
   var month = "";
   var year = "";
   var strlength = 0;
   var pos = 0 ;
   var year_month = "";
   var start_year_month = "";
   var end_year_month = "";
   var days_in_month = 0;
   var theDate = new Date();
   var day_num = 0;
   var day_of_week = "";
   var days_of_week = new Array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
   var optn;
   var start_month ="";
   var start_year ="";
   var end_month ="";
   var end_year ="";

    alert ("y is '" + y + "'");

   if (y == "start")
   {
       // The user has changed the month and year for the start date for the event.  We need to
       // change the available day/date options accordingly, and to set the end date to be the same
       // as the start date.

       start_year_month = theForm.start_sel_month_year.value;

      // First, set the end date month and year to match the start month and year selected
      // by the user
      theForm.end_sel_month_year.value = theForm.start_sel_month_year.value;

      // Next, get rid of the existing options in the day/date area.  They were for the
      // current month and year, not for those selected by the user
      for(i=num_options-1; i >=0; i--)
      {
         // alert("Removing " + i + "  " + theForm.endDate_selDay.options[i].text);
          theForm.start_selDay.remove(i);
          theForm.end_selDay.remove(i);
      }

      add_day_date_options(theForm.start_selDay)  ;
      add_day_date_options(theForm.end_selDay) ;



      theForm.start_selDay.focus();
    }
    else
    {
        // The user has changed the end date for the event.  We need to check that she has
        // not changed the end month/year.  If so, we must offer new day/date options
        // Otherwise, she has apparently finished selecting the start and end times.  We must
        // check that they are valid - ie that the end date/time is after the start date/time

        // alert ("End date selected");

        // theForm.endDate_selDay.value = theForm.startDate_selDay.value;
        start_year_month = theForm.start_sel_month_year.value;
        strlength = start_year_month.length;
        pos = strlength - 4;
        start_month = start_year_month.substr(0,3);
        start_year = start_year_month.substr(pos,4) ;

        end_year_month = theForm.end_sel_month_year.value;
        strlength = end_year_month.length;
        pos = strlength - 4;
        end_month = end_year_month.substr(0,3);
        end_year = end_year_month.substr(pos,4) ;

        pos = theForm.start_selDay.value.length-4;
        end_date = theForm.start_selDay.value.substr(pos,4) ;
        // alert("start month date is " + start_month + "  End month date is "+ end_month);


    }
}
//----------------------------------------------------------------------------------------
function add_options()
{
   Alert ("Adding options");

}
//-----------------------------------------------------------------------------------------
function test1(a, b)
{
	alert("In test function");
}
// =============================================================================================
function test(theForm,y)
{
   var form_name = theForm;
   var this_form ;
   var start_select;
   var end_select;
   var i = 0;
   var num_elements = 0;
   var num_options = 0;
   var month = "";
   var year = "";
   var strlength = 0;
   var pos = 0 ;
   var year_month = "";
   var start_year_month = "";
   var end_year_month = "";
   var days_in_month = 0;
   var theDate = new Date();
   var day_num = 0;
   var day_of_week = "";
   var days_of_week = new Array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
   var optn;
   var start_month ="";
   var start_year ="";
   var end_month ="";
   var end_year ="";
   var theDate = new Date();

//	alert("In test function");
   x = document.getElementsByName(theForm)  ;
   this_form = x[0];

   for (i=0; i<this_form.elements.length; i++ )
   {
        // Move through and find all the elements
       if (this_form.elements[i].name == (y + "_selDay") )
       {
         num_options = this_form.elements[i].length;
         break;
       }
   }


   // alert ("parameters are " + theForm + " and " + y );
   if (y == "start")
   {
       // The user has changed the month and year for the start date for the event.  We need to
       // change the available day/date options accordingly, and to set the end date to be the same
       // as the start date.

       start_year_month = this_form.start_sel_month_year.value;

      // First, set the end date month and year to match the start month and year selected
      // by the user
      
      // this_form.end_sel_month_year.value = this_form.start_sel_month_year.value;

      // Next, get rid of the existing options in the day/date area.  They were for the
      // current month and year, not for those selected by the user
      for(i=num_options-1; i >=0; i--)
      {
          // alert("Removing " + i + "  " + this_form.end_selDay.options[i].text);
          this_form.start_selDay.remove(i);
          // this_form.end_selDay.remove(i);
      }

      strlength = this_form.start_sel_month_year.value.length;
      pos = strlength - 4;
      month = this_form.start_sel_month_year.value.substr(0,3);
      year = this_form.start_sel_month_year.value.substr(pos,4) ;


      // days_in_month = DaysInMonth(month, year);
       theDate.setMonth(month-1);
      // theDate.setYear(year);



        add_day_date_options(this_form, 'start', month, year)  ;
        // add_day_date_options(this_form, 'end', month, year)  ;



        this_form.start_selDay.focus();

    }
/*    else
    {
        // The user has changed the end date for the event.  We need to check that she has
        // not changed the end month/year.  If so, we must offer new day/date options
        // Otherwise, she has apparently finished selecting the start and end times.  We must
        // check that they are valid - ie that the end date/time is after the start date/time

        // alert ("End date selected");

        // theForm.endDate_selDay.value = theForm.startDate_selDay.value;
        start_year_month = this_form.start_sel_month_year.value;
        strlength = start_year_month.length;
        pos = strlength - 4;
        start_month = start_year_month.substr(0,3);
        start_year = start_year_month.substr(pos,4) ;

        end_year_month = this_form.end_sel_month_year.value;
        strlength = end_year_month.length;
        pos = strlength - 4;
        end_month = end_year_month.substr(0,3);
        end_year = end_year_month.substr(pos,4) ;

        pos = this_form.start_selDay.value.length-4;
        end_date = this_form.start_selDay.value.substr(pos,4) ;
        // alert("start month date is " + start_month + "  End month date is "+ end_month);


    }

*/
   // alert ("form_name is " + this_form.name + " and num_options is " + num_options );
 


}

//------------------------------------------------------------------------------------------
function getCheckedValue(radioObj)
{

     chosen = ""
     // len = theForm.single_multi_day.length
     len = radioObj.length

    for (i = 0; i <len; i++)
    {
        if (radioObj[i].checked)
        {
            chosen = radioObj[i].value
        }
    }

    if (chosen == "")
    {
        //alert("No Location Chosen")
        chosen = "none";
    }
    else
    {
        //alert(chosen)
    }
    return chosen;
}

//-------------------------------------------------------------------------------------------
function add_day_date_options(theForm, start_end, month, year)
{
    // var this_form = document.getElementById(theForm)  ;
    var theDate = new Date();

    // alert("Adding the options");

    days_in_month = DaysInMonth(month, year);
    theDate.setMonth(month-1);
    theDate.setYear(year);



    for(i=1; i <= days_in_month; i++)
    {

        theDate.setDate(i);
        day = theDate.getDay();
        day_of_week = theDate.toDateString().substr(0,3);

        // alert("Adding " + i + "  " + day_of_week + "  " + theDate.getDate() );

        optn = document.createElement("OPTION");
        optn.text =  day_of_week + "  " + theDate.getDate();
        optn.value = day_of_week + " " + theDate.getDate();

        if (start_end == 'start')
            theForm.start_selDay.options.add(optn);
        else
            theForm.end_selDay.options.add(optn);

        // optn = document.createElement("OPTION");
        // optn.text =  day_of_week + "  " + theDate.getDate();
        // optn.value = day_of_week + " " + theDate.getDate();
        // theForm.start_selDay.options.add(optn);


    }

}
//----------------------------------------------------------------------------------------
function check_dates(theForm)
{
    // This function is called as part of the submit process.  It checks that the start and
    // end date for the event are valid, and that the former is earlier than the latter

    var theForm = document.frmNewEvent;
    var start_time = new Date();
    var end_time = new Date();
    var strlength = 0;
    var pos = 0;
    var month = 0;
    var year = 0;
    var minute = 0;
    var hour = 0;
    var day = 0;
    var theValue = getCheckedValue(theForm.single_multi_day) ;

    // alert("checking dates");

   // set the start time object
   // get the month and year selected by the user in the 'start' area
    strlength = theForm.start_sel_month_year.value.length;
    pos = strlength - 4;
    month = theForm.start_sel_month_year.value.substr(0,3);
    year = theForm.start_sel_month_year.value.substr(pos,4) ;
    // days_in_month = DaysInMonth(month, year);

    // get the hours and minutes from the start area
    hour = theForm.start_sel_Time.value.split(":")[0];
    mintue = theForm.start_sel_Time.value.split(":")[1];

    // get the date from the start area
    strlength = theForm.start_selDay.value.length;
    pos = strlength - 2;
    day = theForm.start_selDay.value.substr(pos,2) ;

    start_time.setMonth(month-1);
    start_time.setFullYear(year);
    start_time.setHours(hour);
    start_time.setMinutes(minute);
    start_time.setDate(day);

    // Now do the same thing for the end date / time
    // set the end time object
    // get the month and year selected by the user in the 'end' area
    if(theValue == "Multi")
    {
        // if this is a mutli day event, we need to gather the date information
        // for the end date.  If it is a single day event, we can leave these
        // the same as for the start date
        strlength = theForm.end_sel_month_year.value.length;
        pos = strlength - 4;
        month = theForm.end_sel_month_year.value.substr(0,3);
        year = theForm.end_sel_month_year.value.substr(pos,4) ;

        // get the date from the end area
        strlength = theForm.end_selDay.value.length;
        pos = strlength - 2;
        day = theForm.end_selDay.value.substr(pos,2) ;

    }


    // get the hours and minutes from the end area
    hour = theForm.end_sel_Time.value.split(":")[0];
    mintue = theForm.end_sel_Time.value.split(":")[1];


    end_time.setMonth(month-1);
    end_time.setFullYear(year);
    end_time.setHours(hour);
    end_time.setMinutes(minute);
    end_time.setDate(day);

    // Now we have constructed the date objects, we need to compare them
    if (start_time >= end_time)
    {
        // the event starts after it ends - not good
        alert(start_time + "\n" + end_time);
        return false;
    }
    else
    {
        // the dates are okay.  Go ahead and submit the form.
        return true ;

    }

}
