﻿// Update information based on selections

var selectedCourses = [];
var selectTime=[];
var selectedTT = [];
var selectC = [];
function resetTeeSelect()
{
    // un-select radios
    document.teeselect.reset();
    // clear itinerary
    document.getElementById("golfItinerary").innerHTML="No Tee Times Selected";
    // clear array for selected courses
    selectedCourses=[];
    selectTime=[];
    selectedTT=[];
    selectC=[];
    RemoveItineraryAll();
    
}
function SelectCourse(course)
{
    // check to make sure there is no conflict with another selection
    if (course + "x" == "x")
    {
        return;
    }
    // each course only allows one selection
    var selData = document.getElementsByName(course);
    //alert(selData[0]);
    var selVal = "";
    for (i = 0; i < selData.length; i++) 
        {
        if (selData[i].checked) {
            //alert("checked");
            //alert(selData[i].value);
            selVal = selData[i].value;
            }
         }
    //alert(selData);
    // got a value? add it to message
    // split this out
    var aTT = selVal.split('|');
    var sC = aTT[1];
    var sD = aTT[4];
    var sT = aTT[5];
    //--- new fields
    //cartfee + "|" + teetimeid + "|" + teenumber
    var sCF = aTT[6];
    var ttID = aTT[7];
    var tN = aTT[8];
    var courseDispName=aTT[9];
    var sGF = aTT[10];
    
    selectedCourses[course]=[sC,sD,sT,courseDispName,sGF];
    // save to database
    //UpdateItinerary(crs,dte,tm,ratecode,rateid,num,cart)
    UpdateItinerary(sC,sD,sT,aTT[2],aTT[3],numGolf,'0',sCF,ttID,tN,sGF,sCF);
    
    //alert(selectedCourses[course]);
    var disp = "<table width=\"165\">";
    var countTTs = 0;
    var cls="itinerary1"; // 
    for (var g in selectedCourses)
    {
    //alert(g);
        // split values of array
      //  var aTT2 = selectedCourses[g].split('|');
      //  alert(aTT2);
        var sC2 = selectedCourses[g][0];
        var sD2 = selectedCourses[g][1];
        var sT2 = selectedCourses[g][2];
        var selCourseDisp = selectedCourses[g][3];
        var GreenFee = selectedCourses[g][4];
        var ajaxQ = "";
        /*
        Request["crsid"]
        Request["sesID"]
        Request["time"]
        Request["date"]
        Request["num"]
        Request["tlRateid"]
        Request["cart"]
        */
        //\'' + sC2 + ',' + sD2 + ',' + formattime(sT2) + ',' + TransID + '\'
        ajaxQ = ajaxQ + "crsid=" + sC2;
        ajaxQ = ajaxQ + "&sesID=" + TransID;
        ajaxQ = ajaxQ + "&time=" + sT2;
        ajaxQ = ajaxQ + "&date=" + sD2 + "  12:00:00 AM";
        
        disp = disp + "<tr class='" + cls + "' id=\"itin_" + countTTs + "\" valign=top>";
        disp = disp + "<td>";
        disp = disp + "<b>" + selCourseDisp + "</b><br>";
        disp = disp + "<table border=0 cellpadding=0 cellspacing=0 width=155><tr valign=top><td>";
        disp = disp + "" + sD2 + "<br>Time: ";
        disp = disp + formattime(sT2) + "<br>";
        disp = disp + "Golfers: " + numGolf;
        disp = disp + "<br>Price: " + GreenFee;
        disp = disp + '</td><td><a href="javascript:nothing();" onclick="RemoveItinerary(\'' + ajaxQ + '\',\'itin_' + countTTs + '\')"><img src="images/trash.gif" border="0"></a>';
        disp = disp + "</td></tr></table></td>";
        disp = disp + "</tr>";
        countTTs = countTTs + 1;
        //alert(disp);
        if (cls == "itinerary1")
        {
            cls = "itinerary2";
        } else
        {
            cls = "itinerary1";
        }
    }
    disp = disp + "</table>";
    document.getElementById("golfItinerary").innerHTML=disp; 
    if (countTTs > 1)
    {
        TimeValidation();
    }
     
}

//--------------------- Validate times ---------------------------------------

function TimeValidation()
{
    // need to make sure that all selected tee-times are at least 5 hours apart
   //alert("checking times");
    var count=0;
    var dt;
    for (var g in selectedCourses)
    {
        //alert(g);
        var sC2 = selectedCourses[g][0];
        var sD2 = selectedCourses[g][1];
        var sT2 = selectedCourses[g][2];
        dt=new Date(sD2 + " " + sT2);
        selectedTT[count]=sD2 + " " + formattime(sT2);
        selectC[count] = sC2;
        //alert(dt);
        if (count > 0)
        {
        //alert(dt.getTime());
            compareTTdates(sD2 + " " + formattime(sT2),sC2);
        }
        count = count + 1;
    }
    // now we have to compare each value with the others
    
}
function compareTTdates(curdt,curcrs)
{
   
    var rtrn = "";
    //alert(curdt);
    var dt1 = new Date(curdt);
   // dt1.setDate(curdt);
   // dt1.setTime(curtm);
    var dif=0;
    // loop through current array values
    for (var t=0;t<selectedTT.length;t++)
    {
    //alert("array value " + t + " is " + selectedTT[t]);
        var dt2 = new Date(selectedTT[t]);
        //dt2.set(selectedTT[t]);
        dif = ((dt2-dt1) / 1000)/60;
        
        if(dif < 0)
        {
            dif = ((dt1-dt2) / 1000) / 60;
        }
        // make sure that they are 5 hours apart
        // dif is in minutes
        // 
        var difcompare = 5*60;
        if (dif < difcompare)
        {
            // same course?
            if (curcrs != selectC[t])
            {
            rtrn = "You have selected tee times that are less than 5 hours apart";
           // alert(rtrn);
            }
        }
       // alert("difference between " + dt1 + " and " + dt2 + " is " + dif);
    }
    //alert(rtrn);
    //return rtrn;
    // set message
    document.getElementById("messages").innerHTML += "<br><div style='font-weight:bold;color:red;font-size:10px;width:200px;'>" + rtrn + "</div>";
    
}
//------------ formatting ----------------------------------------------------
function TrimString(sInString) {
  sInString = sInString.replace( /^s+/g, "" );// strip leading
  return sInString.replace( /s+$/g, "" );// strip trailing
}
function formattime(timevalue)
    {
    var minhour = 6;        //This and higher is considered 'am'
   
  //  var timevalue = new String(control.value);
    var errvalue = new String("");
   
    var minutes = new String("");
    var hours = new String("");
    var ampm = new String("");
   
    if (timevalue == "")
        {
        errvalue = "Blank";
        }
   
    if (timevalue.indexOf(":")!=-1)
    /*  This is a 'normal' time.  Split it out as necessary.  */
        {
        var tempArray = timevalue.split(":");
        hours = tempArray[0];
        if (isNaN(hours))
        /*  First split is not a valid number, crash out  */
            {
            errvalue = "Yes";
            }
        else
            {
            /*  Make sure the number is between 0 and 23  */
            if ((hours> 23) || (hours <0))
                {
                errvalue = "Yes";
                }
            else
                {
                if (hours> 12)
                    {
                    hours = hours - 12;
                    ampm = "pm";
                    }
                else if (hours == 12)
                    {
                    ampm = "pm";
                    }
                else if (hours == 0)
                    {
                    ampm = "am";
                    hours = 12;
                    }
                else
                    {
                    if (hours <minhour)
                        {
                        ampm = "pm";       
                        }
                    else
                        {
                        ampm = "am";
                        }
                    hours = hours;
                    }
                }
            }
        /*  Gen the minutes   */
        minutes = tempArray[1];
        if (minutes.indexOf("a")> -1)
            {
            ampm = "am";
            minutes = parseInt(minutes);
            }
        else if (minutes.indexOf("p")> -1)
            {
            ampm = "pm";
            minutes = parseInt(minutes);
            }
        else
            {
            minutes = parseInt(minutes);
            }
        /*  Make sure the minutes are in a valid range (00-59)    */
        if (isNaN(minutes))
            {
            errvalue = "Yes";
            minutes = 0;    //Just to make sure for later
            }
        else
            {
            if ((minutes <0) || (minutes> 59))
                {
                minutes = 0;
                errvalue = "Yes";
                }
            else
                {
                //Minutes is valid
                if (minutes <10)
                    {
                    minutes = "0" + String(minutes);
                    }
                else
                    {
                    minutes = minutes;
                    }
                }
            }
        }
    else
    /*  Have to work a little harder  */
        {
        if (isNaN(timevalue))
            {
            errvalue = "Yes";
            }
        else
            {
            if ((timevalue.length == 3) || (timevalue.length ==4))
                {
                //Get the minutes
                minutes = timevalue.substr((timevalue.length -2), 2);
                if ((minutes>= 0) && (minutes <60))
                    {
                    //Ignore this
                    }
                else
                    {
                    errvalue = "Yes";
                    }
               
                //Get the hours
                hours = timevalue.substr(0, (timevalue.length - 2));
                if ((hours <0) || (hours> 23))
                    {
                    errvalue = "Yes";
                    }
                if (hours <10)
                    {
                    if (timevalue.substr(0,1)=="0")
                        {
                        if (hours == 0)
                            {
                            ampm = "am";
                            hours = 12;
                            }
                        else
                            {
                            ampm = "am";
                            }
                        }
                    else
                        {
                        if (hours <minhour)
                            {
                            ampm = "pm";
                            }
                        else
                            {
                            ampm = "am";
                            }
                        }
                    }
                else
                    {
                    if ((hours == 10) || (hours == 11))
                        {
                        ampm = "am";
                        }
                    else if (hours == 12)
                        {
                        ampm = "pm";
                        }
                    else
                        {
                        ampm = "pm";
                        hours = hours - 12;
                        }
                    }
                }
            else
                {
                errvalue = "Yes";
                }
            }
        }
    if (errvalue!="")
    /*  There was an error    */   
        {
      //  if (TrimString(control.value)!="")
       //     {
           // alert("You appear to have entered an invalid time.  Please correct it.");
           // control.select();
           // control.focus();
         //   }
        }
    else
        {
        return (hours * 1) + ":" + minutes + " " + ampm;
        }
   
    }
    

