﻿
//        var cal18 = new CalendarPopup("testdiv1");
//        cal18.setCssPrefix("TEST");
                
        var comparedcourselist = "";
        function SetDateFromToBox(id,context)
        {
            if(context == "Home") {
                var cal18 = new CalendarPopup("dateFromCalendarHome");
            } else if(context == "MediaBar") {
                var cal18 = new CalendarPopup("dateFromCalendarMediaBar");
            } else {
                var cal18 = new CalendarPopup("dateFromCalendarCF");
            }
            cal18.setCssPrefix("TEST");
            cal18.select(document.getElementById(id),'anchor18','dd/MM/yyyy'); 
            return false;
        }
        function SetDateToBox(id,context)
        {
            if(context == "Home") {
                var cal19 = new CalendarPopup("dateToCalendarHome");
            } else if(context == "MediaBar") {
                var cal19 = new CalendarPopup("dateToCalendarMediaBar");
            } else {
                var cal19 = new CalendarPopup("dateToCalendarCF");
            }        
            cal19.setCssPrefix("TEST");
            cal19.select(document.getElementById(id),'anchor19','dd/MM/yyyy'); 
            return false;
        }
        
        function confirmation()
        {
            var _confirm = confirm("Do you wish to add the course into your favourite course list?");
            if (_confirm == true)
                return true;
            else
                return false;
                
        }
        
        function selectTheCoursee(id)
        {
            var resultbox = document.getElementById("resultbox");
            var inputElements = resultbox.getElementsByTagName("input");
            for (var i = 0; i < inputElements.length; i++)
            {
                var myElement = inputElements[i];
                if (myElement.type == "checkbox")
                {
                    myElementchecked = document.getElementById(id).checked;
                }
            }
        }
        
        function ShowHideForm(formid)
        {
           var id = document.getElementById(formid);
           id.style.display = "block";    
        }     
         
         function GetCourseNameFromCookie(cookieKey)
         {
            start = document.cookie.indexOf(cookieKey);
            end = document.cookie.indexOf(";", start);
            cookieValue = document.cookie.substring(start + 15 , end);   
            return cookieValue;                   
         }

        var flag = false;
        function LoadFromCookie()
        {
            flag = true;
        }
        
        function setCookie(c_name,value,expiredays)
        {
            var exdate=new Date();
            exdate.setDate(exdate.getDate()+expiredays);
            document.cookie=c_name+ "=" +(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
        }
        
         function CheckCompareCourseStatus()
         {
            var CoursesInCookie = new Array();
            var tempArray = new Array();    
            var coursenamelist = "";
            var count = 0;
            var coursename = "";
            var cookieValue = "";
            theForm = document.aspnetForm;
            if (document.cookie.indexOf("coursenamelist=") >=  0) 
            {
               cookieValue = GetCourseNameFromCookie("coursenamelist=");
               CoursesInCookie = cookieValue.split(",");
            }
            
            for (thing in theForm)
            {
                elementName = thing.toString();
                if (elementName.indexOf("CheckedCourse") >= 0 &&
                    theForm[thing].tagName.toLowerCase() == 'input' && 
                    theForm[thing].type.toLowerCase() == 'checkbox')
                {
                    if (!theForm[thing].checked)
                    {
                        coursename = theForm[thing].parentNode.title.toString();
                        if (cookieValue.indexOf(coursename) >= 0)
                        {
                            cookieValue = cookieValue.replace("," + coursename + ",", ",");
                            cookieValue = cookieValue.replace(coursename + ",", "");
                            cookieValue = cookieValue.replace("," + coursename, "");
                            cookieValue = cookieValue.replace(coursename, "");
                            CoursesInCookie = cookieValue.split(",");
                        }
                    }
                    if (theForm[thing].checked)
                    {
                        coursename = theForm[thing].parentNode.title;
                        //check duplicate                        
                        if (cookieValue.indexOf(coursename) < 0)
                           {
                            
                            CoursesInCookie.push(coursename);
                           } 
                    }
                }    
            }
            count = CoursesInCookie.length;
            CheckCheckBoxStatus(count);
            document.cookie = "coursenamelist=" + CoursesInCookie.toString();
            theForm.SelectedCompareCourses.value = CoursesInCookie.toString();
         }
            
        function CheckCheckBoxStatus(count)
        {
            if (count < 3) 
            {
                 theForm = document.aspnetForm;
                 for (thing in theForm)
                    {
                        thingName = thing.toString();
                        if (thingName.indexOf("CheckedCourse") >= 0 && 
                            theForm[thing].tagName.toLowerCase() == 'input' && 
                            theForm[thing].type.toLowerCase() == 'checkbox')
                        {
                             if (!theForm[thing].checked) 
                                theForm[thing].disabled = false;
                        }
                    }
             } 
             else 
             {
                     theForm = document.aspnetForm;
                     for (thing in theForm)
                        {
                            thingName = thing.toString();
                            if (thingName.indexOf("CheckedCourse") >= 0 && 
                                theForm[thing].tagName.toLowerCase() == 'input' && 
                                theForm[thing].type.toLowerCase() == 'checkbox')
                            {
                                 if (!theForm[thing].checked)
                                    theForm[thing].disabled = true;
                            }
                        }
             }
        }
        
        function SubmitForm(url) {
            theForm = document.aspnetForm;
            if(document.getElementById("hiddenComparedCourse").value != "") {
                theForm.action = url;
                theForm.submit();
            } else {
                alert("You have not chosen any courses to compare.");
            }
        }
        
        function CheckPageChange() {
           cookievalue = GetCourseNameFromCookie("coursenamelist=");
           CoursesInCookie = cookieValue.split(",");        
            count = CoursesInCookie.length;
            CheckCheckBoxStatus(count);        
        }
        
        function TickCoursesInCookie()
        {
           cookievalue = GetCourseNameFromCookie("coursenamelist=");
           CoursesInCookie = cookieValue.split(",");     
           
           theForm = document.aspnetForm;
             for (thing in theForm)
                {
                    thingName = thing.toString();
                    if (thingName.indexOf("CheckedCourse") >= 0 && 
                        theForm[thing].tagName.toLowerCase() == 'input' && 
                        theForm[thing].type.toLowerCase() == 'checkbox')
                    {
                        coursename = theForm[thing].parentNode.title;
                        if (CoursesInCookie.toString().indexOf(coursename) >= 0)
                        theForm[thing].checked = true;
                    }
                }
        }
        
        function SetSelectionCriteria()
        {
         var cookieValue = "";
         theForm = document.aspnetForm;
         
         //deppoint point        
         cookieValue = GetValueFromCookie("deppoint=", 9);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_DeparturePointDropDown", cookieValue);
                           
         //coursetype      
         cookieValue = GetValueFromCookie("coursetype=", 11);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_CourseTypeDropDown", cookieValue);
         
         //DurationOfTrip
         cookieValue = GetValueFromCookie("durationOfTrip=", 15);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_DurationOfTripDropDown", cookieValue);
         
         //price From
         cookieValue = GetValueFromCookie("pricemin=", 9);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_priceFrom", cookieValue);
         
         //price To
         cookieValue = GetValueFromCookie("pricemax=", 9);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_priceTo", cookieValue);
         
         cookieValue = GetValueFromCookie("flagcode=", 9);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_DestinationCountryDropDown", cookieValue);
         
         cookieValue = GetValueFromCookie("accommodationtype=", 18);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_AccommodationTypeDropDown", cookieValue);
         
         cookieValue = GetValueFromCookie("locationcode=", 13);
         SetDropDownValue("ctl00_searchBoxArea_SearchingBox_DestinationTownDropDown", cookieValue);
         
         //text field
         //Date From
         cookieValue = GetValueFromCookie("startdate=", 10);
         SetTextFieldValue("ctl00_searchBoxArea_SearchingBox_dateFrom", cookieValue);
         //Date To
         cookieValue = GetValueFromCookie("enddate=", 8);
         SetTextFieldValue("ctl00_searchBoxArea_SearchingBox_dateTo", cookieValue);  
         
        }
        
        function SetTextFieldValue(id, value)
        {
            var element = document.getElementById(id);
            element.value = value;
        }
        
        function SetDropDownValue(id, value)
        {
            var element = document.getElementById(id);
            for (i = 0; i < element.options.length; i++)
            {
                if (element.options[i].value == value)
                {
                    element.options[i].selected = true;
                    return;
                }
            }
        }
       
       function LoadPreviousSelectionCriteria(deppoint, flagcode, locationcode, startdate, enddate)
       {
            document.cookie = "deppoint=" + deppoint;
            document.cookie = "flagcode=" + flagcode;
            document.cookie = "locationcode=" + locationcode;
            if (startdate == "1900-01-01" || startdate == "1/1/1900" || startdate == "01.01.1900")
                document.cookie = "startdate=" + "";
            else 
                document.cookie = "startdate=" + startdate;
                
            if (enddate == "1900-01-01" || enddate == "1/1/1900" || enddate == "01.01.1900")
                document.cookie = "enddate=" + "";
            else
                document.cookie = "enddate=" + enddate;      
       } 
       function GetValueFromCookie(cookieKey, offset)
         {
            if (document.cookie.indexOf(cookieKey) >= 0 )
            {
                start = document.cookie.indexOf(cookieKey);
                end = document.cookie.indexOf(";", start);
                cookieValue = document.cookie.substring(start + offset , end);   
                return cookieValue;                   
            }
            else
                return "";
         }
         
      function SetElementValueToCookie(id, cookiekey)
      {
        //alert("id = " + id + ";cookiekey = " + cookiekey);
        var value = document.getElementById(id).value; 
        //alert("value = " + value);
        document.cookie = cookiekey + value;
      }
        
      function SetValueToCookie()
      {
        //Set deppoint
                                 
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_DeparturePointDropDown", "deppoint=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_CourseTypeDropDown", "coursetype=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_DurationOfTripDropDown", "durationOfTrip=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_priceFrom", "pricemin=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_priceTo", "pricemax=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_DestinationCountryDropDown", "flagcode=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_AccommodationTypeDropDown", "accommodationtype=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_DestinationTownDropDown", "locationcode=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_dateFrom", "startdate=");
        SetElementValueToCookie("ctl00_searchBoxArea_SearchingBox_dateTo", "enddate=");
      }
      
      function SetDestinationCountry()
      {
        //alert("in destination");
        SetValueToCookie();
      }
      
      function SetCFValueToCookie()
      {
        var whichPage;
        whichPage = document.getElementById("ctl00_CentralContent_coursefinder_DeparturePointDropDown");
        if (whichPage != "undefined" && whichPage != "" && whichPage != null)
        {
            SetElementValueToCookie("ctl00_CentralContent_coursefinder_DeparturePointDropDown", "deppoint=");
            SetElementValueToCookie("ctl00_CentralContent_coursefinder_DestinationCountryDropDown", "flagcode=");
            SetElementValueToCookie("ctl00_CentralContent_coursefinder_DestinationTownDropDown", "locationcode=");
            SetElementValueToCookie("ctl00_CentralContent_coursefinder_dateFrom", "startdate=");
            SetElementValueToCookie("ctl00_CentralContent_coursefinder_dateTo", "enddate=");
        }
        else
        {
            whichPage = document.getElementById("ctl00_ctl00_CentralContent_MediaBarContent_mediaBar_DeparturePointDropDown");
            if (whichPage != "undefined" && whichPage != "" && whichPage != null)
            {
                SetElementValueToCookie("ctl00_ctl00_CentralContent_MediaBarContent_mediaBar_DeparturePointDropDown", "deppoint=");
                SetElementValueToCookie("ctl00_ctl00_CentralContent_MediaBarContent_mediaBar_DestinationCountryDropDown", "flagcode=");
                SetElementValueToCookie("ctl00_ctl00_CentralContent_MediaBarContent_mediaBar_DestinationTownDropDown", "locationcode=");
                SetElementValueToCookie("ctl00_ctl00_CentralContent_MediaBarContent_mediaBar_dateFrom", "startdate=");
                SetElementValueToCookie("ctl00_ctl00_CentralContent_MediaBarContent_mediaBar_dateTo", "enddate=");
            }
        }
        theForm = document.aspnetForm;
        var currentURL = location.href
        theForm.action = currentURL;
        //alert(document.cookie);
        //alert(theForm.action);
      }
      
    function LoadDestinationTown(countryID, townID)
    {   
        var flagcode = document.getElementById(countryID).value;
        document.getElementById(townID).innerHTML = "";
               
        var townDropDown = document.getElementById(townID);
        var startelement = document.createElement('option');
        startelement.value = ""
        startelement.text = "Destination Town"
        try 
        {
            townDropDown.add(startelement, null);
        } catch (e)
        {
            townDropDown.add(startelement); //for IE only
        }
        
        for (i = 0; i < townArray.length; i++)
        {
            var elementArray = townArray[i].split("@");
            
            if (elementArray[0].toLowerCase() == flagcode.toLowerCase())
            {
                var element = document.createElement('option');
                element.value = elementArray[1];
                element.text = elementArray[2];
                try 
                {
                    townDropDown.add(element, null);
                }
                catch (e)
                {
                    townDropDown.add(element);
                }
            }
        }
        
    }