﻿var Brochure = {

    _postType: "POST",
    _contentType: "application/json; charset=utf-8",
    _netver: 3,

    AddContactInfo: function() {
        //debugger;
        var strCompanyName = document.getElementById("txtCompany");
        var strState = document.getElementById("cboState");
        var strTrained = document.getElementById("cboTrained");
        var strName = document.getElementById("txtName");
        var strTitle = document.getElementById("txtTitle");
        var strPhone = document.getElementById("txtPhone");
        var strEmail = document.getElementById("txtEmail");
        //var strContactBy = document.getElementById("cboContactBy");
        //var strTimeRange = document.getElementById("cboTimeRange");
        var strCourseOther = '';
        var strContactBy = '';
        var strTimeRange = '';
        if (Trim(strCompanyName.value) == "") {
            alert("Enter company name");
            strCompanyName.focus();
            return false;
        } else if (strState.selectedIndex <= 0) {
            alert("Select state");
            strState.focus();
            return false;
        } else if (strTrained.selectedIndex <= 0) {
            alert("Select no of trainees");
            strTrained.focus();
            return false;
        }
        else if (Trim(strTitle.value) == "") {
            alert("Enter title");
            strTitle.focus();
            return false;
        }
        else if (Trim(strName.value) == "") {
            alert("Enter name");
            strName.focus();
            return false;
        }
      
        else if (Trim(strPhone.value) == "") {
            alert("Enter phone");
            strPhone.focus();
            return false;
        }
        else if (Trim(strEmail.value) == "") {
            alert("Enter valid email");
            strEmail.focus();
            return false;
        } else if (!ValidEmail(strEmail.value)) {
            alert("Enter valid email");
            strEmail.focus();
            return false;
        }
        //        else if (strContactBy.selectedIndex <= 0) {
        //            alert("Select contact me");
        //            strContactBy.focus();
        //            return false;
        //        } 
        //        else if (strTimeRange.selectedIndex <= 0) {
        //            alert("Select time");
        //            strTimeRange.focus();
        //            return false;
        //        }
        //        if ($("input[name='cs']:checked").length > 0) {
        //            var filter = "input[name='cs']";
        //            $(filter).each(
        //                 function(index, obj) {
        //                     if (obj.checked) {
        //                         strCourseOther = strCourseOther + obj.value + ",";
        //                     }
        //                 }
        //            );
        //        }
        //        } else {
        //            alert("Select course");
        //            return false;
        //        }

        var ContactInfo = new Object();
        ContactInfo.UserName = strName.value;
        ContactInfo.Company = strCompanyName.value;
        ContactInfo.Title = strTitle.value;
        ContactInfo.Email = strEmail.value;
        ContactInfo.Phone = strPhone.value;
        ContactInfo.State = strState.value
        ContactInfo.TrainedCount = strTrained.value;
        ContactInfo.ContactBy = strContactBy;
        ContactInfo.ContactTime = strTimeRange;
        ContactInfo.CourseOther = strCourseOther;
        $.ajax({
            type: Brochure._postType,
            contentType: Brochure._contentType,
            url: '/Services/Brochure.asmx/UpdateContactInfo',
            data: JSON.stringify(ContactInfo),
            dataType: "json",
            success:
                        function(result) {
                            var jsonTxt = (Brochure._netver <= 2) ? result : result.d;
                            //Brochure.ToggleContact();
                            Brochure.ClearContact();
                            //jsonTxt = eval('(' + jsonTxt + ')');
                            if (jsonTxt == "S") {
                                //Brochure.ClearContact();
                                //Brochure.ToggleContact(false);                            
                                $("#divMsg").html("Thank You!<p>We have real humans monitoring our emails. And they read them... We'll respond as quickly as we can.</p>");
                                $.blockUI({ message: $('#divContent'), centerY: true, css: { border: 'none', cursor: 'default', top: '30%' }, overlayCSS: { backgroundColor: '#ccc', opacity: '0.0', cursor: 'default'} });
                                setTimeout("Brochure.Cancel()", 5000);
                            } else {
                                //document.getElementById("divMsg").innerHTML = "Added Failed";
                                //$.blockUI({ message: $('#divContent'), centerY: false, css: { border: 'none', cursor: 'default', top: '10%' }, overlayCSS: { backgroundColor: '#ccc', opacity: '0.0', cursor: 'default'} });
                            }
                        },
            error:
                        function(XMLHttpRequest, textStatus, errorThrown) {
                            alert("Error Occured: " + errorThrown);
                        }
        });
    },

    TrainedSelectedChange: function() {
        var strTrained = document.getElementById("cboTrained");
        if (strTrained.selectedIndex > 0) {
            this.ToggleContact(true);
            //$("#move").animate({ 'height': '685px' }, { duration: 2000, easing: "swing" });
            //expand();
        } else {
            this.ToggleContact(false);
            //$("#move").animate({ height: 185 }, { queue: false, duration: 2000 });
            //collapse();
        }
    },

    ToggleContact: function(show) {
        if (show) {
            $("#move")
                .animate({ 'height': '685px' }, { duration: 1000 })
                .animate({ 'height': '630px' }, { duration: 500 });
        } else {
            $("#move")
            .animate({ height: '185px' }, { duration: 1000 })
            .animate({ height: '185px' }, { duration: 500 });
        }
    },

    ClearContact: function() {
        $("#txtCompany").val("");
        $("#txtName").val("");
        $("#txtTitle").val("");
        $("#txtPhone").val("");
        $("#txtEmail").val("");
        $("#cboTrained").attr("selectedIndex", 0);
        $("#cboState").attr("selectedIndex", 0);
        $("#cboContactBy").attr("selectedIndex", 0);
        $("#cboTimeRange").attr("selectedIndex", 0);
    },

    Cancel: function() {
        $.unblockUI();
    }
};
