﻿var manualId = 0;
var instructionId = 0;
var problemId = 0;
var manufacturerId = 0;
var productType = 0;
var keywords = "";
var rawUrl = "";



function SwapValues() {
    swapValues = [];
    $(".swap_value").each(function(i) {
        swapValues[i] = $(this).val();
        $(this).focus(function() {
            if ($(this).val() == swapValues[i]) {
                $(this).val("")
            }
        }).blur(function() {
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i])
            }
        })
    }
    )
};

function loadAutoCompleteSearch() {
    searchbox.autocomplete(manuals, {
        matchContains: true,
        width: 205,
        scrollHeight: 195,
        scroll: false,
        max: 3,
        formatItem: function(item) {
            return '<table cellspacing="0" cellpadding="0" style="height: 63px;"><tr><td style="width: 75px; text-align: center; vertical-align: middle;"><img src="ml/' + item.ThumbPictId + '.png" height="56" /></td><td>' + item.ManufacturerName + ' ' + item.ProductName + '</td></table>';
        },
        formatMatch: function(item) {
            return item.ManufacturerName + " " + item.ProductName;
        }
    }).result(function(event, item) {
        if (item.DatacardGroupId == 0) {
            location.href = "Home.aspx?Id=" + item.ManualId;
        } else {
            showDatacardSelector(item.DatacardGroupId, item.ThumbPictId, item.ManufacturerName, item.ProductName);
        };
    });

    searchbox.focus();

    searchbox.keydown(function(event) {
        if ($.trim($(this).val()) == watermarkText) {
            $(this).val("");
        };
    }).blur(function() {
        if ($.trim($(this).val()) == "") {
            $(this).val(watermarkText)
        }
    }).click(function() {
        if ($.trim($(this).val()) == watermarkText) {
            $(this).val("");
        };
    });
};

function ValidateManualSearch() {
    var $textbox = $("#ctl00_ctl00_ContentPlaceHolder_txtDeviceSearch").val();
    if (($textbox == "") || ($textbox == searchText)) {
        alert(searchError);
        return false;
    };
};

var linkReference;

function ShowCauseCatch(link) {
    linkReference = link.href;
    $("#CausePopup").show();
};

function CauseCatchUnsolved() {
    window.location = linkReference;
};

function CauseCatchSolved(manualId, problemId, causeId) {
    $.jmsajax({
        type: "POST",
        url: "AjaxWebService.asmx/DatamineSolvedProblem",
        dataType: "msjson",
        data: { "manualId": manualId, "problemId": problemId, "causeId": causeId }
    });

    window.location = linkReference;
};

//
// Feedback functions

function ToggleFeedback() {
    $("#FeedbackPopup").toggle();
};

function SaveFeedback() {
    var comment = $("#Feedback_Comment").val();
    var name = $("#Feedback_Name").val();
    var email = $("#Feedback_Email").val();
    var Phonenumber = $("#Feedback_Phonenumber").val();
    
    if(comment == "") {alert("Please enter your comment"); return false;}
    if(name == "") {alert("Please enter your name"); return false;}
    if(email == "") {alert("Please enter your email");  return false;}

    $.jmsajax({
        type: "POST",
        url: "AjaxWebService.asmx/SaveFeedback",
        dataType: "msjson",
        data: { "comment": comment,
                "name": name,
                "email": email,
                "Phonenumber": Phonenumber },
        success: ShowFeedbackConfirm
    });    
};

function ShowFeedbackConfirm() {
    $("#Feedback_Form").hide();
    $("#Feedback_Confirm").show();
    $("#Feedback_Cancel").hide();
    $("#Feedback_Send").hide();
    $("#Feedback_Close").show();
};

//
// Link to this page
function ToggleLinkToThisPage() {
    $("#SendEmailPopup").hide();
    $("#LinkToThisPagePopup").toggle();
};

//
// Send via email
function ToggleSendEmail() {
    $("#LinkToThisPagePopup").hide();
    $("#SendEmailPopup").toggle();
};

function SendEmail() {
    var recipient = $("#SendEmail_Recipient").val();
    var name = $("#SendEmail_Name").val();
    var comment = $("#SendEmail_Comment").val();

    $.jmsajax({
        type: "POST",
        url: "AjaxWebService.asmx/SendEmail",
        dataType: "msjson",
        data: { "recipient": recipient,
            "name": name,
            "comment": comment,
            "manualId": manualId,
            "instructionId": instructionId,
            "problemId": problemId,
            "manufacturerId": manufacturerId,
            "productType": productType,
            "keywords": keywords,
            "rawUrl": rawUrl,
            "comparisonId1": 0,
            "comparisonId2": 0,
            "comparisonId3": 0,
            "comparisonDatacard": false
        },
        success: ShowSendEmailConfirm
    });
};

function ShowSendEmailConfirm(result) {
    if (result == "ok") {
        var recipient = $("#SendEmail_Recipient").val();
        $("#SendEmail_Confirm_Recipient").text(recipient);
        $("#SendEmail_Form").hide();
        $("#SendEmail_Confirm").show();
        $("#SendEmail_Cancel").hide();
        $("#SendEmail_Send").hide();
        $("#SendEmail_Close").show();
    }
    else {
        alert(result);
    };
}

//
// Submit problem
function ToggleSubmitProblem() {
    $("#SubmitProblemPopup").toggle();
};

function SubmitProblem() {
    var description = $("#SubmitProblem_Description").val();

    $.jmsajax({
        type: "POST",
        url: "AjaxWebService.asmx/SaveProblemFeedback",
        dataType: "msjson",
        data: { "manualId": manualId,
            "description": description},
        success: ShowSubmitProblemConfirm
    });
};

function ShowSubmitProblemConfirm(result) {
    $("#SubmitProblem_Form").hide();
    $("#SubmitProblem_Confirm").show();
    $("#SubmitProblem_Cancel").hide();
    $("#SubmitProblem_Send").hide();
    $("#SubmitProblem_Close").show();
};

//
// Print function
function OpenPrint(url) {
    window.open(url, "VFTR", toolbar = 0, scrollbars = 1, location = 0, statusbar = 0, menubar = 0, resizable = 1);
};

//
// Functions list
var container;
var ajaxLoad;
var chaptersDLL;

function GetChapter(chapterId) {
    if (chapterId != 0) {
        chaptersDDL.attr("disabled", true);
        $("#selectInfo").hide();
        container.html(null);
        ajaxLoad.show();
        $.jmsajax({
            type: "POST",
            url: "AjaxWebService.asmx/GetInstructionsByChapterId",
            dataType: "msjson",
            data: { "manualId": manualId, "ChapterId": chapterId },
            success: RenderChapter
        });
        $.address.value(chapterId);
    }
};

function RenderChapter(instructions) {
    var lastSubChapterId = 0;

    for (var instruction in instructions) {
        var instructionsContainer;

        //Render SubChapter and following instruction
        if (lastSubChapterId != instructions[instruction].SubChapterId) {
            lastSubChapterId = instructions[instruction].SubChapterId;

            var subChapterBlock = $("<div class=\"ContentBlock Padding\" />");
            var subChapterH2 = $("<h2 class=\"SubChapter\"/>").text(instructions[instruction].SubChapterName).appendTo(subChapterBlock);

            instructionsContainer = $("<div class=\"InstructionsPadding\"/>").appendTo(subChapterBlock);

            var instructionLink = $("<a class=\"ArrowLink\" style=\"background-position: left top;\" href=\"ShowInstruction.aspx?Id=" + manualId + "&InstructionId=" + instructions[instruction].InstructionId + "\"/>").text(instructions[instruction].InstructionName);
            instructionLink.appendTo(instructionsContainer);

            subChapterBlock.appendTo(container);

        } else { //Render instruction in same instruction
            var instructionBreak = $("<br />").appendTo(instructionsContainer);
            var instructionLink = $("<a class=\"ArrowLink\" style=\"background-position: left top;\" href=\"ShowInstruction.aspx?Id=" + manualId + "&InstructionId=" + instructions[instruction].InstructionId + "\"/>").text(instructions[instruction].InstructionName);
            instructionLink.appendTo(instructionsContainer);
        }
    };
    ajaxLoad.hide();
    chaptersDDL.attr("disabled", false);
    container.show();
};

//Mouseover function
$(document).ready(function() {
    $("img.mouseover, input.mouseover").mouseover(function() {
        //Mouse over
        var srcArray = $(this).attr("src").split(".");
        $(this).attr("src", srcArray[0] + "_mouseover." + srcArray[1]);
    }).mouseout(function() {
        //Mouse out
        $(this).attr("src", $(this).attr("src").replace("_mouseover", ""));
    });

});