﻿var SendContactMailAction = "/Home/SendContactMail";
var SendMailAction = "/Home/SendMail";
var GetBusinessEntityAction = "/Home/GetBusinessEntity";
var GetEmailWindowAction = "/Home/GetEmailWindow";
var GetLocationsAction = "/Home/GetLocations";

$(document).ready(function () {
    
});

//To Send Email
function SendContactMail() {
    var name = $('#txtName').val();
    var fromAddress = $('#txtFromAddress').val();
    var message = $('#txtMessage').val();

    $.ajax({
        url: SendContactMailAction,
        type: "GET",
        dataType: "html",
        cache: false,
        data: { name: name, fromAddress: fromAddress, message: message },
        success: function (html) {
            $("#divMessage").html("Email Sent.");
            $('#txtName').val("");
            $('#txtFromAddress').val("");
            $('#txtMessage').val("");
            //HideDialogError("divMessage");
        },
        error: function (request, textStatus, errorThrown) {            
            ShowCallBackError(request, textStatus, errorThrown, "#divMessage");

        }
    });
}

function SendMail() {
    var toName = $('#txtToName').val();
    var fromName = $('#txtFromName').val();
    var fromAddress = $('#txtEmailAddress').val();
    var subject = $('#txtSubject').val();
    var message = $('#txtNewMessage').val();

    $.ajax({
        url: SendMailAction,
        type: "GET",
        dataType: "html",
        cache: false,
        data: { toName: toName, fromName: fromName, fromAddress: fromAddress, subject: subject, message: message },
        success: function (html) {
            $("#divMessage").html("Email Sent.");
            $('#txtName').val("");
            $('#txtFromAddress').val("");
            $('#txtMessage').val("");
            CloseDialogWindow();
        },
        error: function (request, textStatus, errorThrown) {
            ShowCallBackError(request, textStatus, errorThrown, "#divMessage");

        }
    });
}

//To Get BusinessEntity
function GetBusinessEntity(id) { 

    $.ajax({
        url: GetBusinessEntityAction,
        type: "GET",
        dataType: "html",
        cache: false,
        data: { id: id},
        success: function (html) {
            OpenDialogWindow("", html, 400);
            //HideDialogError("divMessage");
        },
        error: function (request, textStatus, errorThrown) {
            ShowCallBackError(request, textStatus, errorThrown, "#divMessage");
        }
    });
}

//To Get Email Windows
function GetEmailWindow(to) {

    $.ajax({
        url: GetEmailWindowAction,
        type: "GET",
        dataType: "html",
        cache: false,
        data: { to: to },
        success: function (html) {
            OpenDialogWindow("", html, 400);
            //HideDialogError("divMessage");
        },
        error: function (request, textStatus, errorThrown) {
            ShowCallBackError(request, textStatus, errorThrown, "#divMessage");
        }
    });
}

//To Get Email Windows
function GetLocations() {
    var id = $("#drpBusinessEntities").attr("selectedIndex")

    $.ajax({
        url: GetLocationsAction,
        type: "GET",
        dataType: "json",
        cache: false,
        data: { id: id },
        success: function (data) {
            $("#drpLocations").fillSelect(data);
        },

        error: function (request, textStatus, errorThrown) {
            ShowCallBackError(request, textStatus, errorThrown, "#divMessage");
        }
    });
}

$.fn.clearSelect = function () {
    return this.each(function () {
        if (this.tagName == 'SELECT')
            this.options.length = 0;
    });
} 

$.fn.fillSelect = function (data) {
    return this.clearSelect().each(function () {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;
            $.each(data, function (index, optionData) {
                var option = new Option(optionData.Address1 + ", " + optionData.City + "," + optionData.State, optionData.LocationID);

                if ($.browser.msie) {
                    dropdownList.add(option);
                }
                else {
                    dropdownList.add(option, null);
                }
            });
        }
    });
}


function ShowCallBackError(request, textStatus, errorThrown, divTagName) {
    var data = jQuery.parseJSON(request.responseText);
    ShowError(data.Message, divTagName);
}

function ShowError(message, divTagName) {
    ShowMessage("error", message, divTagName)
}

function ShowMessage(messageType, message, divTagName) {
    $("#divMessage").html(message);
    //$(divTagName).msgBox({ msg: message, type: messageType, animateMsg: true, scrollToTop: true, autoHideMsg: false });
    //$(divTagName).show();
}

function HideDialogError(dialog) {
    $('#' + dialog).hide();
}

/* Functions for generic Dialog */

// function for date setup
$(document).ready(function () {
    //Initialize generic dialog
    $("#appDialogWindow").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        resizable: false,
        title: ""
    });
});

function OpenDialogWindow(title, html, width) {
    $('#appDialogWindow').dialog("open");
    $('#appDialogWindow').dialog("option", "title", title);
    $('#appDialogWindow').dialog("option", "position", "center");
    $('#appDialogWindow').dialog("option", "width", width);
    $('#appDialogWindowContent').html(html);
    $('#appDialogWindowError').hide();
}

function CloseDialogWindow() {
    $('#appDialogWindow').dialog("close");
}
