﻿/// <reference path="~/lib/js/jquery-1.2.6-vsdoc.js" />

var selectedFormLanguage;
var selectedAOI;
var selectedSG;
var selectedLocation;

var hasSubGroups;

var ddIdAOI = "select[id='contactUsControl_ddAreaOfInterest']";
var ddIdSG = "select[id='contactUsControl_ddSubgroup']";
var ddIdLocation = "select[id='contactUsControl_ddLocation']";

$(document).ready(function() {
    selectedFormLanguage = $("select[id='contactUsControl_FormControl_InputDropDownCountry']").val();
    selectedAOI = $(ddIdAOI).val();
    selectedSG = $(ddIdSG).val();
    selectedLocation = $(ddIdLocation).val();
    hasSubGroups = (!$(ddIdSG).hasClass("invisible"));

    $("select[id='contactUsControl_FormControl_InputDropDownCountry']").find("option[value='Finland']").attr("selected", "selected");

    $("select[id='contactUsControl_FormControl_InputDropDownCountry']").change(function() {
        selectedFormLanguage = $("select[id='contactUsControl_FormControl_InputDropDownCountry']").val();
        $("input[id='contactUsControl_hfSelCountry']").val(selectedFormLanguage);
    });

    $(ddIdAOI).change(function() {
        selectedAOI = $(ddIdAOI).val();
        selectedSG = $(ddIdSG).val();
        recordArea(selectedAOI);
        $(ddIdSG).empty("option");
        $("div#contactDetailsText").html("");

        FindSubgroupsOnAOI(selectedAOI);
    });

    $(ddIdSG).change(function() {
        selectedSG = $(ddIdSG).val();
        recordArea(selectedSG);
        GetContactInformation(selectedSG, selectedLocation);
    });

    $(ddIdLocation).change(function() {
        selectedLocation = $(ddIdLocation).val();
        selectedAOI = $(ddIdAOI).val();
        selectedSG = $(ddIdSG).val();
        if (hasSubGroups == true) {
            GetContactInformation(selectedSG, selectedLocation);
        }
        else {
            GetContactInformation(selectedAOI, selectedLocation);
        }
    });

    function FindSubgroupsOnAOI(contactAOIFolder) {
        var paramList = "{'contactFolderPageId':'" + contactAOIFolder + "'}";
        result = $.ajax({
            type: "POST",
            url: "/Services/contactus.asmx/RetrieveSubgroups",
            contentType: "application/json; charset=utf-8",
            data: paramList,
            dataType: "json",
            success: successfulGroupResponse,
            error: couldNotRetrieveSubGroups
        });
    }

    function successfulGroupResponse(result) {
        if (result.d != "") {   // new aoi has subgroups
            $("div#contactUsControl_subgroupContainer").removeClass("invisible");
            $(ddIdSG).removeClass("invisible");     // not used anymore
            hasSubGroups = true;

            var jsonResultString = result.d;
            var options = eval(jsonResultString);

            fillSubGroupDropDown(options);
        }
        else {
            $("div#contactUsControl_subgroupContainer").addClass("invisible");
            $(ddIdSG).addClass("invisible");     // not used anymore
            hasSubGroups = false;

            GetContactInformation(selectedAOI, selectedLocation);
        }
    }

    function fillSubGroupDropDown(subGroups) {
        $(ddIdSG).empty("option");
        var options = '';
        for (var i = 0; i < subGroups.length; i++) {
            options += '<option value="' + subGroups[i].optionValue + '">' + subGroups[i].optionDisplay + '</option>';
        }
        $(ddIdSG).html(options);

        recordArea($(ddIdSG).val());
        GetContactInformation($(ddIdSG).val(), selectedLocation);
    }

    function couldNotRetrieveSubGroups(err) {
        /// TODO: How can we log this?
        $("div#contactDetailsText").html("<p>We're sorry, an error occured during the execution of your enquiry. <br />Please e-mail us at info[@]luvata.com</p>");
    }

    function GetContactInformation(contactFolder, country) {
        var paramList = "{'contactFolderPageId':'" + contactFolder + "', 'location':'" + country + "'}";

        $.ajax({
            type: "POST",
            data: paramList,
            url: "/Services/contactus.asmx/GetInfo",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: foundContactInformation,
            error: couldNotRetrieveContactInformation
        });
    }

    function foundContactInformation(result) {
        $("div#contactDetailsText").html("");
        $("div#contactDetailsText").html(result.d);
    }

    function couldNotRetrieveContactInformation(err) {
        /// TODO: How can we log this?
        $("div#contactDetailsText").html("<p>We're sorry, an error occured during the execution of your enquiry. <br />Please e-mail us at info[@]luvata.com</p>");
    }

    $("input[name='contactUsControl$FormControl$ctl00']").click(function(event) {
        $("#contactUsForm").validate();

        if ($("#contactUsForm").valid()) {
            $("#contactUsForm").ajaxSubmit();
        }
        else {
            event.preventDefault();
        }
    });

    function recordArea(id) {
        $("input[id='contactUsControl_hfArea']").val(id);
    }
});