﻿$(document).ready(function() {
    $("#ctl00_ContentPlaceHolder1_txtName").focus();
    $("#ctl00_ContentPlaceHolder1_imgBtnSubmit").click(function(e) {
        var msg = "";
        var msgstr = "";

        var email = $("#ctl00_ContentPlaceHolder1_txtEmail").val();
        if (email.length == 0) {
            msgstr += " - Email is reqiured\n";
            $("#ctl00_ContentPlaceHolder1_txtEmail").css("border-color", "red");
            $("#ctl00_ContentPlaceHolder1_txtEmail").focus();
        } else {
            if (!emailcheck(email)) {
                msgstr += " - Invalid email address\n";
                $("#ctl00_ContentPlaceHolder1_txtEmail").css("border-color", "red");
                $("#ctl00_ContentPlaceHolder1_txtEmail").focus();
            }
        }

        var company = $("#ctl00_ContentPlaceHolder1_txtCompany").val();
        if (company.length == 0) {
            msgstr += " - Company is reqiured\n";
            $("#ctl00_ContentPlaceHolder1_txtCompany").css("border-color", "red");
            $("#ctl00_ContentPlaceHolder1_txtCompany").focus();
        }

        var name = $("#ctl00_ContentPlaceHolder1_txtName").val();
        if (name.length == 0) {
            msgstr += " - Name is reqiured\n";
            $("#ctl00_ContentPlaceHolder1_txtName").css("border-color", "red");
            $("#ctl00_ContentPlaceHolder1_txtName").focus();
        }

        if ($.trim(msgstr).length > 0) {
            msg = "_________________________________________________________________\n\n"
            msg += "The form was not submitted because of the following error(s).\n";
            msg += "Please correct these error(s) and re-submit.\n";
            msg += "_________________________________________________________________\n\n"
            msg += msgstr;
            alert(msg);
        } else {
            var maxWidth = 0;
            var maxHeight = 0;
            if ($.browser.msie) {
                maxWidth = document.documentElement.scrollWidth - 1;
                maxHeight = document.documentElement.scrollHeight - 1;
            } else {
                maxWidth = Math.max($("html").width(), $(window).width() - 1);
                maxHeight = Math.max($("html").height(), $(window).height() - 1);
            }
            $("#modal").css("height", maxHeight)
            $("#modal").css("width", maxWidth)
            $("#modal").show()
            var Data = new Array();
            var i = -1;
            $.each($("#tblForm input.field, #tblForm textarea.field"),
                function() {
                    sobj = new Object();
                    if ($(this).attr("rel") != "undefined") {
                        sobj = new Object();
                        sobj.Key = $(this).attr("rel");
                        var eleValue = this.value;
                        sobj.Value = eleValue;
                        Data[i = i + 1] = sobj;
                    }
                }
            );

            //alert($.toJSON(Data));                                                                                                  
            PageMethods.SubmitForm($.toJSON(Data), OnSubmitReturn);
        }

        return false;
    });
});

function OnSubmitReturn(result) {
    $("#modal").hide()
    $("#tblForm").hide();
    $("#resultId").html(result).slideDown();
}

function emailcheck(str) {
    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);
    return regex.test(str);
}
