﻿var info = null;
var fade = 500;
var MS_CRM_FETCH = true;
var CRMEX_FETCH = true;
function swapview(j1, j2) {
    j1.fadeOut(fade, function() {
        j2.fadeIn(fade);
    });
}
function IsValidEmail(email) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return filter.test(email);
}
$(function () {
    $("#show_new_case_form").click(function () {
        $("#step1").fadeOut(500, function () {
            if ($("#dropdownisfilled").val()) { // Lav evt. dette tjeck anderledes
                $("#ajaxloader").fadeIn(100);
                $.ajaxDotNet('/Service.asmx/GetContactsByAccountId', {
                    verb: 'POST',
                    data: { supportcode: $("#supportplan_copy").val(), accountId: $("#accountId").val() },
                    success: function (obj) {

                        $("#dropdownisfilled").val(true);
                        info = obj.d;
                        $("#name").hide();
                        $("#company").attr("value", obj.d.Account.name);
                        $("#email").attr("value", obj.d.Contacts[0].emailaddress1);
                        $("#phone").attr("value", obj.d.Contacts[0].telephone1);
                        $("#title").attr("value", obj.d.Contacts[0].jobtitle);
                        $("#support_plan_id").val(info.Supportplan.Id);
                        var p = $("#name_pick");
                        $("#contactid_pick").empty();
                        $("#contactid_pick").show();
                        for (i = 0; i < obj.d.Contacts.length; i++) {
                            $("#contactid_pick").append("<option value=\"" + obj.d.Contacts[i].contactid.Value + "\">" + obj.d.Contacts[i].fullname + "</option>");
                        }
                        for (i = 0; i < obj.d.CasePicklist.length; i++) {
                            if (obj.d.CasePicklist[i].Value.Value == 2) {
                                $("#priority_pick").append("<option selected value=\"" + obj.d.CasePicklist[i].Value.Value + "\">" + obj.d.CasePicklist[i].Label.UserLocLabel.Label + "</option>");
                            } else {
                                $("#priority_pick").append("<option value=\"" + obj.d.CasePicklist[i].Value.Value + "\">" + obj.d.CasePicklist[i].Label.UserLocLabel.Label + "</option>");
                            }
                        }

                        for (i = 0; i < obj.d.CaseStatus.length; i++) {
                            if (obj.d.CaseStatus[i].ap_statuscode.Value == 1005) {
                                $("#StatusValue")[0].value = obj.d.CaseStatus[i].ap_statusid.Value;
                                $("#Status").append("<option selected='selected' value=\"" + obj.d.CaseStatus[i].ap_statusid.Value + "\">" + obj.d.CaseStatus[i].ap_name + "</option>");
                            }
                            else {
                                $("#Status").append("<option value=\"" + obj.d.CaseStatus[i].ap_statusid.Value + "\">" + obj.d.CaseStatus[i].ap_name + "</option>");
                            }
                        }
                        if (obj.d.Priority.length > 0) {
                            $("#responseTime").attr("value", obj.d.Priority[0].ap_responsetime.Value + ' hour(s)');
                        }
                        //clear all priotity items
                        $("select[id$=Priority] > option").remove();
                        for (i = 0; i < obj.d.Priority.length; i++) {
                            $("#Priority").append("<option value=\"" + obj.d.Priority[i].ap_priorityresponseid.Value + "\">" + obj.d.Priority[i].ap_name + "</option>");
                        }

                        $("#ajaxloader").fadeOut(100);
                        $("#step2").fadeIn(500);
                        $("#extra_step1").fadeOut(500, function () {
                            $("#extra_step2").fadeIn(500);
                            $("#extra_step2_plan").fadeIn(500);
                            $("#extra_planid").html(info.Supportplan.Name);
                            $("#extra_clip").html(info.Supportplan.IncidentsLeft);
                            $("#extra_accountname").html(info.Account.name);
                            $("#extra_from").html(info.Supportplan.EffectiveFrom.substring(8, 10) + "/" + info.Supportplan.EffectiveFrom.substring(5, 7) + " - " + info.Supportplan.EffectiveFrom.substring(0, 4));
                            $("#extra_to").html(info.Supportplan.EffectiveTo.substring(8, 10) + "/" + info.Supportplan.EffectiveTo.substring(5, 7) + " - " + info.Supportplan.EffectiveTo.substring(0, 4));
                        });
                        $("#step4_planid").html(info.Supportplan.Name);
                        $("#step4_planclip").html(info.Supportplan.IncidentsLeft.Value);
                        $("#step4_account").html(info.Account.name);
                        $("#step4_from").html(info.Supportplan.EffectiveFrom.substring(8, 10) + "/" + info.Supportplan.EffectiveFrom.substring(5, 7) + " - " + info.Supportplan.EffectiveFrom.substring(0, 4));
                        $("#step4_to").html(info.Supportplan.EffectiveTo.substring(8, 10) + "/" + info.Supportplan.EffectiveTo.substring(5, 7) + " - " + info.Supportplan.EffectiveTo.substring(0, 4));
                        $("#step4_plan").show();
                        $("#step4_noplan").hide();
                        $("#extra_step4_plan").show();
                    },
                    other: function () {
                        alert("An error occured retrieving contact information from web service.");
                        $("#ajaxloader").fadeOut(fade, function () {
                            $("#step1").fadeIn(fade);
                        });
                    }
                });
            } else {
                $("#ajaxloader").fadeOut(100);
                $("#name").show();
                $("#other_name").hide();
                $("#contactid_pick").hide();
                $("#company").removeAttr("readonly");
                $("#step2").fadeIn(500);
                $("#extra_step1").fadeOut(500, function () {
                    $("#extra_step2").fadeIn(500);
                    $("#extra_step2_noplan").fadeIn(500);
                });
                $("#company").val("");
                $("#title").val("");
                $("#phone").val("");
                $("#email").val("");
                $("#step4_plan").hide();
                $("#step4_noplan").show();
            }
            return false;
        });
    });


    $("#Priority").change(function () {

        var v = $(this);
        for (i = 0; i < info.Priority.length; i++) {
            if (v.val() == info.Priority[i].ap_priorityresponseid.Value) {

                $("#responseTime").attr("value", info.Priority[i].ap_responsetime.Value + ' hour(s)');
            }
        }
    });

    $("#supportplanpass").keypress(function (event) {
        if (event.which == 13) {
            $("#case_submit").click();
        }
    });

    $("#no_supportplan_check").click(function () {

        if ($(this).attr("checked")) {
            $.ajaxDotNet('/Service.asmx/GetPicklist', {
                verb: 'POST',
                data: {},
                success: function (obj) {
                    info = obj.d;

                    for (i = 0; i < obj.d.CasePicklist.length; i++) {
                        if (obj.d.CasePicklist[i].Value.Value == 2) {
                            $("#priority_pick").append("<option selected value=\"" + obj.d.CasePicklist[i].Value.Value + "\">" + obj.d.CasePicklist[i].Label.UserLocLabel.Label + "</option>");
                        } else {
                            $("#priority_pick").append("<option value=\"" + obj.d.CasePicklist[i].Value.Value + "\">" + obj.d.CasePicklist[i].Label.UserLocLabel.Label + "</option>");
                        }
                    }

                    for (i = 0; i < obj.d.CaseStatus.length; i++) {
                        $("#Status").append("<option value=\"" + obj.d.CaseStatus[i].ap_statusid.Value + "\">" + obj.d.CaseStatus[i].ap_name + "</option>");
                    }
                },
                other: function () {
                    alert("An error occured, Please notify support@alfapeople.com that an error 60 has occured");
                }
            });
            $("#step1,#extra_step1").fadeOut(fade, function () {
                $("#step2,#extra_step2").fadeIn(500);
            });
        }
    });

    $("#case_submit").click(function () {

        $("#lookuploader").show();
        $.ajaxDotNet('/Service.asmx/QuerySupportplan', {
            verb: 'POST',
            data: { supportcode: $("#supportplanid").val(), supportpass: $("#supportplanpass").val() },
            success: function (obj) {
                $("#lookuploader").hide();
                window.location = "/Case/Overview/" + $("#supportplanid").val();
            },
            other: function (obj) {
                $("#lookuploader").hide();
                alert("Invalid combination of case id and password.");
            }
        })
    });
    $("#code_submit").click(function () {
        $("#lookuploader2").show();
        $.ajaxDotNet('/Service.asmx/QuerySupportplan', {
            verb: 'POST',
            data: { supportcode: $("#codeid").val() },
            success: function (obj) {
                $("#lookuploader2").hide();
                window.location = "/Case/Overview/" + $("#codeid").val();
            },
            other: function (obj) {
                $("#lookuploader2").hide();
                alert("Could not find cases with the given supporcode");
            }
        })
    });
    $("#step1_next").click(function () {
        $("#step1").fadeOut(500, function () {
            if ($("#support_yes").attr("checked") && $("#supportcode").val() != "") { // Lav evt. dette tjeck anderledes
                $("#ajaxloader").fadeIn(100);
                $.ajaxDotNet('/Service.asmx/GetContacts', {
                    verb: 'POST',
                    data: { supportcode: $("#supportcode").val() },
                    success: function (obj) {
                        info = obj.d;
                        $("#name").hide();
                        $("#company").attr("value", obj.d.Account.name);
                        $("#email").attr("value", obj.d.Contacts[0].emailaddress1);
                        $("#phone").attr("value", obj.d.Contacts[0].telephone1);
                        $("#title").attr("value", obj.d.Contacts[0].jobtitle);
                        $("#support_plan_id").val(info.Supportplan.Id);
                        var p = $("#name_pick");
                        $("#contactid_pick").empty();
                        $("#contactid_pick").show();
                        for (i = 0; i < obj.d.Contacts.length; i++) {
                            $("#contactid_pick").append("<option value=\"" + obj.d.Contacts[i].contactid.Value + "\">" + obj.d.Contacts[i].fullname + "</option>");
                        }
                        $("#ajaxloader").fadeOut(100);
                        $("#step2").fadeIn(500);
                        $("#extra_step1").fadeOut(500, function () {
                            $("#extra_step2").fadeIn(500);
                            $("#extra_step2_plan").fadeIn(500);
                            $("#extra_planid").html(info.Supportplan.Name);
                            $("#extra_clip").html(info.Supportplan.IncidentsLeft);
                            $("#extra_accountname").html(info.Account.name);
                            $("#extra_from").html(info.Supportplan.EffectiveFrom.substring(8, 10) + "/" + info.Supportplan.EffectiveFrom.substring(5, 7) + " - " + info.Supportplan.EffectiveFrom.substring(0, 4));
                            $("#extra_to").html(info.Supportplan.EffectiveTo.substring(8, 10) + "/" + info.Supportplan.EffectiveTo.substring(5, 7) + " - " + info.Supportplan.EffectiveTo.substring(0, 4));
                        });
                        $("#step4_planid").html(info.Supportplan.Name);
                        $("#step4_planclip").html(info.Supportplan.IncidentsLeft.Value);
                        $("#step4_account").html(info.Account.name);
                        $("#step4_from").html(info.Supportplan.EffectiveFrom.substring(8, 10) + "/" + info.Supportplan.EffectiveFrom.substring(5, 7) + " - " + info.Supportplan.EffectiveFrom.substring(0, 4));
                        $("#step4_to").html(info.Supportplan.EffectiveTo.substring(8, 10) + "/" + info.Supportplan.EffectiveTo.substring(5, 7) + " - " + info.Supportplan.EffectiveTo.substring(0, 4));
                        $("#step4_plan").show();
                        $("#step4_noplan").hide();
                        $("#extra_step4_plan").show();
                    },
                    other: function () {
                        alert("An error occured retrieving contact information from web service.");
                        $("#ajaxloader").fadeOut(fade, function () {
                            $("#step1").fadeIn(fade);
                        });
                    }
                });
            } else {
                $("#ajaxloader").fadeOut(100);
                $("#name").show();
                $("#other_name").hide();
                $("#contactid_pick").hide();
                $("#company").removeAttr("readonly");
                $("#step2").fadeIn(500);
                $("#extra_step1").fadeOut(500, function () {
                    $("#extra_step2").fadeIn(500);
                    $("#extra_step2_noplan").fadeIn(500);
                });
                $("#company").val("");
                $("#title").val("");
                $("#phone").val("");
                $("#email").val("");
                $("#step4_plan").hide();
                $("#step4_noplan").show();
            }
            return false;
        });
    });
    $("#step2_next").click(function () {
        if ($("#company").val() != "" && ($("#name").val() != "" || $("#contactid_pick").val() != null) && $("#email").val() != "" && $("#phone").val() != "") {
            if (IsValidEmail($("#email").val())) {
                $("#step2,#extra_step2").fadeOut(fade, function () {
                    $("#step3,#extra_step3").fadeIn(fade);
                    $("#contactinfo_invalid").hide();

                    $("#step4_name").html($("#name").val());
                    $("#step4_email").html($("#email").val());
                    $("#step4_phone").html($("#phone").val());
                    $("#step4_title").html($("#title").val());
                });
            }
            else {
                $("#contactinfo_error").hide();
                $("#contactinfo_invalid").show();
            }
        }
        else {
            $("#contactinfo_error").show();
            $("#contactinfo_invalid").hide();
        }

    });
    $("#step2_back").click(function () {
        $("#no_supportplan_check").removeAttr("checked");
        $("#step2").fadeOut(500, function () {
            $("#step1").fadeIn(500);
            $("#extra_step2_noplan").fadeOut(500);
            $("#extra_step2_plan").fadeOut(500, function () {
                $("#extra_step1").fadeIn(500);
            });
        });
    });
    $("#step3_crmex_next").click(function () {

        $("#step3").fadeOut(500, function () {
            $("#step4").fadeIn(500);
        });
        $("#extra_step3").fadeOut(500, function () {
            $("#extra_step4").fadeIn(500);
        });


    });
    $("#step3_mscrm_back").click(function () {
        $("#step3,#extra_step3").fadeOut(fade, function () {
            $("#step2,#extra_step2").fadeIn(fade);
        });
    });

    $("#AttachAnother1").click(function () {
        var noOfAttachments = parseInt($("#noOfFileBrowser1").val());
        if (noOfAttachments == 5) {
            $("#AttachAnother1").css("display", "none");
        }
        $("#file" + (noOfAttachments + 1)).css("display", "block");
        $("#noOfFileBrowser1").val(noOfAttachments + 1);
        return false;
    });

    $("#AttachAnother2").click(function () {
        var noOfAttachments = parseInt($("#noOfFileBrowser2").val());
        if (noOfAttachments == 5) {
            $("#AttachAnother2").css("display", "none");
        }
        $("#file" + (noOfAttachments + 1)).css("display", "block");
        $("#noOfFileBrowser2").val(noOfAttachments + 1);
        return false;
    });

    $("#step3_mscrm_next").click(function () {
        var invalidExt = "ade;adp;app;asa;ashx;asmx;asp;bas;bat;cdx;cer;chm;class;cmd;com;config;cpl;crt;csh;dll;exe;fxp;hlp;hta;htr;htw;ida;idc;idq;inf;ins;isp;its;js;jse;ksh;lnk;mad;maf;mag;mam;maq;mar;mas;mat;mau;mav;maw;mda;mdb;mde;mdt;mdw;mdz;msc;msh;msh1;msh1xml;msh2;msh2xml;mshxml;msi;msp;mst;ops;pcd;pif;prf;prg;printer;pst;reg;rem;scf;scr;sct;shb;shs;shtm;shtml;soap;stm;tmp;url;vb;vbe;vbs;vsmacros;vss;vst;vsw;ws;wsc;wsf;wsh;";
        if ($.trim($("#mscrm_title").val()) != "" && ($.trim($("#mscrm_description").val()) != "")) {
           
            var file1Value = $("#file1").val();
            dots = file1Value.split(".")
            var file1Type = dots[dots.length - 1];
            if (file1Type != "" && invalidExt.indexOf(file1Type) != -1) {
                alert("File should not be of type " + file1Type);
                return false;
            }

            var file2Value = $("#file2").val();
            dots2 = file2Value.split(".")
            var file2Type = dots2[dots2.length - 1];
            if (file2Type != "" && invalidExt.indexOf(file2Type) != -1) {
                alert("File should not be of type " + file2Type);
                return false;
            }

            var file3Value = $("#file3").val();
            dots3 = file3Value.split(".")
            var file3Type = dots3[dots3.length - 1];
            if (file3Type != "" && invalidExt.indexOf(file3Type) != -1) {
                alert("File should not be of type " + file3Type);
                return false;
            }
            var file4Value = $("#file4").val();
            dots4 = file4Value.split(".")
            var file4Type = dots4[dots4.length - 1];
            if (file4Type != "" && invalidExt.indexOf(file4Type) != -1) {
                alert("File should not be of type " + file4Type);
                return false;
            }
            var file5Value = $("#file5").val();
            dots5 = file5Value.split(".")
            var file5Type = dots5[dots5.length - 1];
            if (file5Type != "" && invalidExt.indexOf(file5Type) != -1) {
                alert("File should not be of type " + file5Type);
                return false;
            }
            var file6Value = $("#file6").val();
            dots6 = file6Value.split(".")
            var file6Type = dots6[dots6.length - 1];
            if (file6Type != "" && invalidExt.indexOf(file6Type) != -1) {
                alert("File should not be of type " + file6Type);
                return false;
            }



            //alert(this.files[0].size);

            $("#step3").fadeOut(500, function () {
                $("#step4").fadeIn(500);
            });
            $("#extra_step3").fadeOut(500, function () {
                $("#extra_step4").fadeIn(500);
            });
        }
        else {

            $("#caseInfoError").show();
        }
    });
    $(".step3_back").click(function () {
        swapview($("#step3"), $("#step2"));
        swapview($("#extra_step3"), $("#extra_step2"));
    });
    $("#contactid_pick").change(function () {
        var v = $(this);
        for (i = 0; i < info.Contacts.length; i++) {
            if (v.val() == info.Contacts[i].contactid.Value) {
                $("#email").attr("value", info.Contacts[i].emailaddress1);
                $("#phone").attr("value", info.Contacts[i].telephone1);
                $("#title").attr("value", info.Contacts[i].jobtitle);
            }
        }
    });

    $("#MS_CRM_LINK").click(function () {
        if (MS_CRM_FETCH) {
            $("#support_type").fadeOut(fade, function () {
                $("#ajaxloader").fadeIn(100);
                $.ajaxDotNet('/Service.asmx/GetCrmSubjects', {
                    verb: 'POST',
                    data: {},
                    success: function (obj) {
                        for (i = 0; i < obj.d.Subjects.length; i++) {
                            $("#subjectid").append("<option value=\"" + obj.d.Subjects[i].id + "\">" + obj.d.Subjects[i].title + "</option>");
                        }
                        $("#ajaxloader").fadeOut(100, function () {
                            $("#MS_CRM").fadeIn(fade)
                        });
                        swapview($("#extra_step3_type"), $("#extra_step3_data"));
                        MS_CRM_FETCH = false;
                    },
                    other: function () {
                        alert("An error occured retrieving product information from web service.");
                    }
                });
            });
        } else {
            $("#ajaxloader").fadeOut(100)
            $("#support_type").fadeOut(fade, function () {
                $("#MS_CRM").fadeIn(fade);
                swapview($("#extra_step3_type"), $("#extra_step3_data"));
            });
        }
    });
    $("#CRM_EX_LINK").click(function () {
        if (CRMEX_FETCH) {
            $("#support_type").fadeOut(fade, function () {
                $("#ajaxloader").fadeIn(100);
                $.ajaxDotNet('/Service.asmx/GetExSubjects', {
                    verb: 'POST',
                    data: {},
                    success: function (obj) {
                        for (i = 0; i < obj.d.Subjects.length; i++) {
                            $("#productid").append("<option value=\"" + obj.d.Subjects[i].id + "\">" + obj.d.Subjects[i].title + "</option>");
                        }
                        $("#ajaxloader").fadeOut(100, function () {
                            $("#CRM_EX").fadeIn(fade)
                        });
                        swapview($("#extra_step3_type"), $("#extra_step3_data"));
                        CRMEX_FETCH = false;
                    },
                    other: function () {
                        alert("An error occured retrieving product information from web service.");
                    }
                });
            });
        } else {
            $("#ajaxloader").fadeOut(100)
            $("#support_type").fadeOut(fade, function () {
                $("#CRM_EX").fadeIn(fade);
                swapview($("#extra_step3_type"), $("#extra_step3_data"));
            });
        }
    });

    $("#other_name").click(function () {
        if ($("#contactid_pick:hidden").length) {
            var p = $("#contactid_pick");
            for (i = 0; i < info.Contacts.length; i++) {
                if (info.Contacts[i].contactid.Value == p.val()) {
                    $("#name").val("");
                    $("#email").val(info.Contacts[i].emailaddress1);
                    $("#phone").val(info.Contacts[i].telephone1);
                    $("#title").val(info.Contacts[i].jobtitle);
                }
            }
            $("#contactid_pick").show();
            $("#name").hide();
        } else {
            $("#contactid_pick").hide();
            $("#name").show();
            $("#name").val("");
            $("#email").val("");
            $("#phone").val("");
            $("#title").val("");
        }
    });
    $("#new_case").click(function () {
        $("#old_case_form").hide();
        $("#new_case_form").fadeIn(500);
        $("#extra_step1").fadeIn(500);
    });
    $("#old_case").click(function () {
        $("#new_case_form").hide();
        $("#old_case_form").fadeIn(500);
        $("#extra_step1").fadeIn(500);
    });
    $("#support_no").click(function () {
        $("#extra_step1_noplan").fadeIn(fade);
        $("#code_container").hide();
        $("#extra_step1").fadeIn(500);
        $("#step1_next").fadeIn(500);
    });
    $("#support_yes").click(function () {
        $("#code_container").fadeIn(500);
        $("#extra_step1").fadeIn(500);
        $("#step1_next").fadeIn(500);
    });
    $("#support_contact_mark").click(function () {
        $("#support_contact").val(this.checked);
    });
    $("#nosubmit").click(function () {
        $("#step4").fadeOut(500, function () {
            $("#Cancel").fadeIn(500);
        });
    });
    $("#finalcheck").click(function () {
        $("#submit").attr("disabled", $(this).attr("checked") ? "" : "disabled");
    });
});
