﻿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 ClearValueFromCookie(c_name) {
    cookieValueArray = document.cookie.split(";");
    for (i = 0; i <= cookieValueArray.length - 1; i++) {
        if (cookieValueArray[i].indexOf(c_name)) {
            cookieValueArray.splice(i, 1);
            break;
        }
    }
    document.cookie = cookieValueArray.join(";");
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
         
function setCookie(c_name, value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() - 1);
    document.cookie = c_name + "=" + escape(value) + ";path=/";
}

function UpdatingBookingDetails(id, value) {
    var detail = document.getElementById(id);
    
    if (detail != null && detail != "undefined" && detail != "") 
    {
        setCookie(id, value);
        detail.innerHTML = value;
    } else if (id == "course") 
    {
        setCookie("course", value);
    }
}

function loadDeparturePoints() 
{
    ClearCourseInfo();

    var CourseTownID = document.getElementById("ctl00_CentralContent_businessGenerator_CourseTownsDropDown");
    var DepartPointID = "";
    var CourseID = "";
    if (CourseTownID != "undefined" && CourseTownID != null && CourseTownID != "") {
        CourseTownID = "ctl00_CentralContent_businessGenerator_CourseTownsDropDown";
        DepartPointID = "ctl00_CentralContent_businessGenerator_DeparturePointsDropDown";
        CourseID = "ctl00_CentralContent_businessGenerator_CourseDropDown";
    }
    else {
        CourseTownID = document.getElementById("ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseTownsDropDown");
        if (CourseTownID != "undefined" && CourseTownID != null && CourseTownID != "") {
            CourseTownID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseTownsDropDown";
            DepartPointID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_DeparturePointsDropDown";
            CourseID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseDropDown";
        }
        else {
            CourseTownID = "ctl00_CentralContent_CourseTownsDropDown";
            DepartPointID = "ctl00_CentralContent_DeparturePointsDropDown";
            CourseID = "ctl00_CentralContent_CourseDropDown";
        }
    }

    var destination = document.getElementById(CourseTownID);
    var destinationCode = destination[destination.selectedIndex].value.toUpperCase();
    var destinationText = destination[destination.selectedIndex].text;
    UpdatingBookingDetails("destDetail", destinationText);
    var x = document.getElementById(DepartPointID);
    var startText = x[0].text;
    document.getElementById(DepartPointID).innerHTML = "";
    var y = document.createElement('option');
    y.text = startText;
    y.value = "";
    try {
        x.add(y, null);
    }
    catch (err) {
        x.add(y);
    }
    var sel = 0;
    for (var k = 0; k < destArray.length; k++) {
        var arrdest = destArray[k].split("@");
        var code = arrdest[0].toUpperCase();
        var depName = arrdest[1];
        if (code == destinationCode) {
            var x = document.getElementById(DepartPointID);
            var y = document.createElement('option');
            y.text = depName;
            y.value = depName;
            try {
                x.add(y, null);
            }
            catch (err) {
                x.add(y);
            }
        }
    }
    
    
}

function loadCourse() {
    ClearCourseInfo();

    var CourseTownID = document.getElementById("ctl00_CentralContent_businessGenerator_CourseTownsDropDown");
    var DepartPointID = "";
    var CourseID = "";
    if (CourseTownID != "undefined" && CourseTownID != null && CourseTownID != "") {
        CourseTownID = "ctl00_CentralContent_businessGenerator_CourseTownsDropDown";
        DepartPointID = "ctl00_CentralContent_businessGenerator_DeparturePointsDropDown";
        CourseID = "ctl00_CentralContent_businessGenerator_CourseDropDown";
    }
    else {
        CourseTownID = document.getElementById("ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseTownsDropDown");
        if (CourseTownID != "undefined" && CourseTownID != null && CourseTownID != "") {
            CourseTownID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseTownsDropDown";
            DepartPointID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_DeparturePointsDropDown";
            CourseID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseDropDown";
        }
        else {
            CourseTownID = "ctl00_CentralContent_CourseTownsDropDown";
            DepartPointID = "ctl00_CentralContent_DeparturePointsDropDown";
            CourseID = "ctl00_CentralContent_CourseDropDown";
        }
    }

    var destination = document.getElementById(CourseTownID);
    var destinationCode = destination[destination.selectedIndex].value.toUpperCase();
    var DeparturePoint = document.getElementById(DepartPointID);
    var DeppointCode = DeparturePoint[DeparturePoint.selectedIndex].value.toUpperCase();
    var DeppointText = DeparturePoint[DeparturePoint.selectedIndex].text;
    UpdatingBookingDetails("depDetail", DeppointText);
    var course = document.getElementById(CourseID);
    course.innerHTML = "";
    var courseOption = document.getElementById(CourseID);
    var element = document.createElement('option');
    
    if (courseDDDefaultBlurb != null && courseDDDefaultBlurb != "" && courseDDDefaultBlurb != "undefined")
        {
         element.text = courseDDDefaultBlurb;
         element.value = "";
        }
    else
        {element.text = ""; element.value = "";}
    try {
        courseOption.add(element, null);
    }
    catch (e) {
        courseOption.add(element);
    }
    for (var k = 0; k <= courseArray.length - 1; k++) {
        var courseElementArray = courseArray[k].split("@");
        var destcode = courseElementArray[0].toUpperCase();
        var depcode = courseElementArray[1].toUpperCase();
        var date = courseElementArray[2].toUpperCase();
        var coursetype = courseElementArray[3];
        var coursename = courseElementArray[4];
        var NoOfDays = courseElementArray[5];
        var CourseNumber = courseElementArray[6];
        var TravelBy = courseElementArray[7];
        var cprice = courseElementArray[8];
        
        
        if (destcode == destinationCode && depcode == DeppointCode) {
            var courseOption = document.getElementById(CourseID);
            var element = document.createElement('option');
            element.text = date + "   " + coursetype + "   " + CourseNumber;
            element.value = coursename + "@" + date + "@" + coursetype + "@" + NoOfDays + "@" + CourseNumber + "@" + TravelBy + "@" + cprice;
            try {
                courseOption.add(element, null);
            }
            catch (e) {
                courseOption.add(element);
            }
        }
    }
}

function submitForm(url) {
    var form = document.aspnetForm;
    form.action = url;
    form.submit();
}

function AddExtras(id) {
    var Itemprice;
    var inputCollection = document.getElementsByTagName('input');
    var tprice = 0;
    var selectedArticles = "";
    var selectedArticleCodes = "";
    for (var i = 0; i < inputCollection.length; i++) {
        if (inputCollection[i].type == "checkbox" && inputCollection[i].checked) {
            var id = inputCollection[i].id;
            var extraItem = document.getElementById(id).value;
            var ItemArray = extraItem.split("@");
            var Itemcode = ItemArray[0];
            var Itemprice = ItemArray[1];
            var ItemTitle = ItemArray[2];

            //Itemprice = GetItemPrice(id);
            tprice += parseInt(Itemprice);
            selectedArticles += "<p class='summaryDetail'>" + ItemTitle + "</p>";
            selectedArticleCodes += Itemcode + ";";
        }
    }
    document.getElementById("extraprice").innerHTML = tprice;
    setCookie("extraprice", tprice);  
    document.getElementById("selectedArticleCodes").value = selectedArticleCodes;

    var extrascode_value = selectedArticleCodes + "|" + selectedArticles;
    setCookie("extras", extrascode_value);
    
    if (selectedArticles != "" && selectedArticles != "undefined" && selectedArticles != null) {
        document.getElementById("extraDetailsText").innerHTML = selectedArticles;
    } else {
        document.getElementById("extraDetailsText").innerHTML = "";
    }
    
    UpdatingTotalPrice(tprice);
}

function ClearSafe(id, value)
{
    try
    {
        var item = document.getElementById(id);
	    if (item != null)
		    item.innerHTML = value;
    } catch(e) { }		    
}

function ClearCourseInfo()
{
    ClearSafe("extraDetailsText", "");
	ClearSafe("bookingExtras", "<h6>Booking Extras</h6>");
	ClearSafe("cprice", "0");
	ClearSafe("extraprice", "0");
	ClearSafe("rebate", "0");
	ClearSafe("totalprice", "0");
}

function FormatCurrency(f)
{
    var tmp = f.toString().replace(",", ".");
    return isNaN(tmp) ? "0" : parseFloat(tmp).toFixed(2).replace(".00","").replace(",00","");
}

function ConvertToFloat(str)
{
    if (str == null || str.length == 0)
        return 0.0;
        
    str = str.toString().replace(',', '.');
    return (isNaN(str) ? 0.0 : parseFloat(str));
}

function UpdatingTotalPrice(tprice)
{    
    var cprice = document.getElementById("cprice");
    var eprice = document.getElementById("extraprice");
    var rebate = document.getElementById("rebate"); 
        
    cprice = ConvertToFloat(cprice.innerHTML);
    eprice = ConvertToFloat(eprice.innerHTML);
    rebate = ConvertToFloat(rebate.innerHTML);
    
    var totalprice = cprice + eprice - rebate;
    
    document.getElementById("totalprice").innerHTML = FormatCurrency(totalprice);
    document.getElementById("selectedTotalprice").value = totalprice;
    document.getElementById("selectedExtraprice").value = eprice;
    document.getElementById("selectedRebate").value = rebate;
    
    setCookie("totalprice", totalprice);
    setCookie("extraprice", eprice);
    setCookie("rebate", rebate);
}

function setBookingDetailsFromCookie() 
{
    var destDetail = getCookie("destDetail");
    var depDetail = getCookie("depDetail");
    var courseDetail = getCookie("courseDetail");
    var extras = getCookie("extras");
    var extrasArray = extras.split("|");
    var extrasText = extrasArray[1];
    var cprice = getCookie("cprice");
    var extraprice = getCookie("extraprice");
    var rebate = getCookie("rebate");    
    
    if (rebate == "" || rebate == null) {
        rebate = "0";
    }
    var totalprice = getCookie("totalprice");
    UpdatingBookingDetails("destDetail", destDetail);
    UpdatingBookingDetails("depDetail", depDetail);
    document.getElementById("ctl00_CentralContent_hdnDeparturePoint").value = depDetail;

    UpdatingBookingDetails("courseDetail", courseDetail);
    if (extrasText != "" && extrasText != "undefined" && extrasText != null) {
        document.getElementById("extraDetailsText").innerHTML = extrasText;
        document.getElementById("ctl00_CentralContent_hdnSelectedArticlesId").value = extrasArray[0];
    } else {
        document.getElementById("extraDetailsText").innerHTML = "";
        document.getElementById("ctl00_CentralContent_hdnSelectedArticlesId").value = "";
    }
    UpdatingBookingDetails("cprice", cprice);    
    
    document.getElementById("ctl00_CentralContent_hdnCoursePrice").value = cprice;
    document.getElementById("ctl00_CentralContent_hdnTotalPrice").value = totalprice;
    document.getElementById("ctl00_CentralContent_hdnExtraPrice").value = extraprice;
    document.getElementById("ctl00_CentralContent_hdnRebatePrice").value = rebate;

    UpdatingBookingDetails("extraprice", FormatCurrency(extraprice));
    UpdatingBookingDetails("rebate", FormatCurrency(rebate));
    UpdatingBookingDetails("totalprice", FormatCurrency(totalprice));
}


function SetSimpleBookingToCookie() {
    var CourseTownID = document.getElementById("ctl00_CentralContent_businessGenerator_CourseTownsDropDown");
    var DepartPointID = "";
    var CourseID = "";
    if (CourseTownID != "undefined" && CourseTownID != null && CourseTownID != "") {
        CourseTownID = "ctl00_CentralContent_businessGenerator_CourseTownsDropDown";
        DepartPointID = "ctl00_CentralContent_businessGenerator_DeparturePointsDropDown";
        CourseID = "ctl00_CentralContent_businessGenerator_CourseDropDown";
    }
    else {
        CourseTownID = document.getElementById("ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseTownsDropDown");
        if (CourseTownID != "undefined" && CourseTownID != null && CourseTownID != "") {
            CourseTownID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseTownsDropDown";
            DepartPointID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_DeparturePointsDropDown";
            CourseID = "ctl00_ctl00_CentralContent_RightNavPlaceHolder_businessGenerator_CourseDropDown";
        }
        else {
            CourseTownID = "ctl00_CentralContent_CourseTownsDropDown";
            DepartPointID = "ctl00_CentralContent_DeparturePointsDropDown";
            CourseID = "ctl00_CentralContent_CourseDropDown";
        }
    }

    var deppoint = document.getElementById(DepartPointID).value;
    var dest = document.getElementById(CourseTownID).value;
    var course = document.getElementById(CourseID).value;
    setCookie("depDetail", deppoint);
    setCookie("destDetail", dest);
    setCookie("course", course);
}


//Get Value from Cookie
function GetBookingDetailsFromCookie() {
    var desttown = getCookie("destDetail");
    var deppoint = getCookie("depDetail");
    var course = getCookie("course");
    var courseDetail = getCookie("courseDetail");
    if (desttown != null && desttown != "") {
        AssignBookingDetailsToDropDown("ctl00_CentralContent_CourseTownsDropDown", desttown);
        //loadDeparturePoints('ctl00_CentralContent_CourseTownsDropDown','ctl00_CentralContent_DeparturePointsDropDown');
        loadDeparturePoints();
        AssignBookingDetailsToDropDown("ctl00_CentralContent_DeparturePointsDropDown", deppoint);
        loadCourse();
        AssignBookingDetailsToDropDown("ctl00_CentralContent_CourseDropDown", course);
        ShowBookingExtras('ctl00_CentralContent_CourseDropDown');
    }
}

//Assign Value to Dropdown
function AssignBookingDetailsToDropDown(id, value) {
    var element = document.getElementById(id);
    for (var k = 0; k < element.options.length; k++) {
        if (element.options[k].innerHTML == value) {
            element.options[k].selected = true;
            return;
        }
    }
    for (var k = 0; k < element.options.length; k++) {
        if (element.options[k].value == value) {
            element.options[k].selected = true;
            return;
        }
    }
}

function setPersonalDetails() {
    if (document.getElementById('ctl00_CentralContent_FirstName') != null) {
        UpdatingBookingDetails('FirstNameSummary', document.getElementById('ctl00_CentralContent_FirstName').value);
    }
    if (document.getElementById('ctl00_CentralContent_LastName') != null) {
        UpdatingBookingDetails('LastNameSummary', document.getElementById('ctl00_CentralContent_LastName').value);
    }
    if (document.getElementById('ctl00_CentralContent_Address1') != null) {
        UpdatingBookingDetails('AddressSummary', document.getElementById('ctl00_CentralContent_Address1').value + '<br />' + document.getElementById('ctl00_CentralContent_Address2').value + '<br />' + document.getElementById('ctl00_CentralContent_Address3').value);
    }
    if (document.getElementById('ctl00_CentralContent_PostalCode') != null) {
        UpdatingBookingDetails('PostalCodeSummary', document.getElementById('ctl00_CentralContent_PostalCode').value);
    }
    if (document.getElementById('ctl00_CentralContent_City') != null) {
        UpdatingBookingDetails('CitySummary', document.getElementById('ctl00_CentralContent_City').value);
    }
    if (document.getElementById('ctl00_CentralContent_Country') != null) {
        UpdatingBookingDetails('CountrySummary', document.getElementById('ctl00_CentralContent_Country').options[document.getElementById('ctl00_CentralContent_Country').selectedIndex].innerHTML);
    }
    if (document.getElementById('ctl00_CentralContent_Email') != null) {
        UpdatingBookingDetails('EmailSummary', document.getElementById('ctl00_CentralContent_Email').value);
    }
    if (document.getElementById('ctl00_CentralContent_Phone') != null) {
        UpdatingBookingDetails('PhoneSummary', document.getElementById('ctl00_CentralContent_Phone').value);
    }
    if (document.getElementById('ctl00_CentralContent_Mphone') != null) {
        UpdatingBookingDetails('MphoneSummary', document.getElementById('ctl00_CentralContent_Mphone').value);
    }
    if (document.getElementById('ctl00_CentralContent_Male') != null) {
        isMale = document.getElementById('ctl00_CentralContent_Male').checked;
        if (isMale) {
            UpdatingBookingDetails('GenderSummary', 'Male');
        } else {
            UpdatingBookingDetails('GenderSummary', 'Female');
        }
    }
    if (document.getElementById('ctl00_CentralContent_BirthDayD') != null) {
        UpdatingBookingDetails('BirthdaySummary', document.getElementById('ctl00_CentralContent_BirthDayD').options[document.getElementById('ctl00_CentralContent_BirthDayD').selectedIndex].value + '/' + document.getElementById('ctl00_CentralContent_BirthDayM').options[document.getElementById('ctl00_CentralContent_BirthDayM').selectedIndex].value + '/' + document.getElementById('ctl00_CentralContent_BirthDayY').options[document.getElementById('ctl00_CentralContent_BirthDayY').selectedIndex].value);
    }
    if (document.getElementById('ctl00_CentralContent_referral') != null) {
        UpdatingBookingDetails('referralSummary', document.getElementById('ctl00_CentralContent_referral').options[document.getElementById('ctl00_CentralContent_referral').selectedIndex].innerHTML);
    }
    if (document.getElementById('ctl00_CentralContent_Guardian') != null) {
        UpdatingBookingDetails('GuardianSummary', document.getElementById('ctl00_CentralContent_Guardian').value);
    }
    if (document.getElementById('ctl00_CentralContent_Phoneday') != null) {
        UpdatingBookingDetails('PhonedaySummary', document.getElementById('ctl00_CentralContent_Phoneday').value);
    }
    if (document.getElementById('ctl00_CentralContent_Gmobile') != null) {
        UpdatingBookingDetails('GmobileSummary', document.getElementById('ctl00_CentralContent_Gmobile').value);
    }
    if (document.getElementById('ctl00_CentralContent_GEmail') != null) {
        UpdatingBookingDetails('GEmailSummary', document.getElementById('ctl00_CentralContent_GEmail').value);
    }
    if (document.getElementById('ctl00_CentralContent_Recommend') != null) {
        UpdatingBookingDetails('RecommendSummary', document.getElementById('ctl00_CentralContent_Recommend').value);
    }
    if (document.getElementById('ctl00_CentralContent_Roommate') != null) {
        UpdatingBookingDetails('RoommateSummary', document.getElementById('ctl00_CentralContent_Roommate').value);
    }
    if (document.getElementById('ctl00_CentralContent_Medical') != null) {
        UpdatingBookingDetails('MedicalSummary', document.getElementById('ctl00_CentralContent_Medical').value);
    }
    if (document.getElementById('ctl00_CentralContent_Sname') != null) {
        UpdatingBookingDetails('SnameSummary', document.getElementById('ctl00_CentralContent_Sname').value);
    }
    if (document.getElementById('ctl00_CentralContent_Scity') != null) {
        UpdatingBookingDetails('ScitySummary', document.getElementById('ctl00_CentralContent_Scity').value);
    }
    if (document.getElementById('ctl00_CentralContent_Tname') != null) {
        UpdatingBookingDetails('TnameSummary', document.getElementById('ctl00_CentralContent_Tname').value);
    }
    if (document.getElementById('ctl00_CentralContent_Langyears') != null) {
        UpdatingBookingDetails('LangyearsSummary', document.getElementById('ctl00_CentralContent_Langyears').value);
    }
    if (document.getElementById('ctl00_CentralContent_Grade') != null) {
        UpdatingBookingDetails('GradeSummary', document.getElementById('ctl00_CentralContent_Grade').value);
    }
    if (document.getElementById('ctl00_CentralContent_LastGrade') != null) {
        UpdatingBookingDetails('LastGradeSummary', document.getElementById('ctl00_CentralContent_LastGrade').value);
    }
}

function setPersonalDetailsFromCookie() {
    UpdatingBookingDetails('FirstNameSummary', getCookie("FirstNameSummary"));
    UpdatingBookingDetails('LastNameSummary', getCookie("LastNameSummary"));
    UpdatingBookingDetails('AddressSummary', getCookie("AddressSummary"));
    UpdatingBookingDetails('PostalCodeSummary', getCookie("PostalCodeSummary"));
    UpdatingBookingDetails('CitySummary', getCookie("CitySummary"));
    UpdatingBookingDetails('CountrySummary', getCookie("CountrySummary"));
    UpdatingBookingDetails('EmailSummary', getCookie("EmailSummary"));
    UpdatingBookingDetails('PhoneSummary', getCookie("PhoneSummary"));
    UpdatingBookingDetails('MphoneSummary', getCookie("MphoneSummary"));
    theGender = getCookie("GenderSummary");
    if (theGender == "Male") {
        UpdatingBookingDetails('GenderSummary', 'Male');
    } else {
        UpdatingBookingDetails('GenderSummary', 'Female');
    }
    UpdatingBookingDetails('BirthdaySummary', getCookie("BirthdaySummary"));
    UpdatingBookingDetails('referralSummary', getCookie("referralSummary"));
    UpdatingBookingDetails('GuardianSummary', getCookie("GuardianSummary"));
    UpdatingBookingDetails('PhonedaySummary', getCookie("PhonedaySummary"));
    UpdatingBookingDetails('GmobileSummary', getCookie("GmobileSummary"));
    UpdatingBookingDetails('GEmailSummary', getCookie("GEmailSummary"));
    UpdatingBookingDetails('RecommendSummary', getCookie("RecommendSummary"));
    UpdatingBookingDetails('RoommateSummary', getCookie("RoommateSummary"));
    UpdatingBookingDetails('MedicalSummary', getCookie("MedicalSummary"));
    UpdatingBookingDetails('SnameSummary', getCookie("SnameSummary"));
    UpdatingBookingDetails('ScitySummary', getCookie("ScitySummary"));
    UpdatingBookingDetails('TnameSummary', getCookie("TnameSummary"));
    UpdatingBookingDetails('LangyearsSummary', getCookie("LangyearsSummary"));
    UpdatingBookingDetails('GradeSummary', getCookie("GradeSummary"));
    UpdatingBookingDetails('LastGradeSummary', getCookie("LastGradeSummary"));
}

