var isPost = false;

$(function () {
    //alert('test');
    //$("input#btSubmit").click(function(){
    function submitLoginForm() {
        if (validateLoginForm()) {

            //$.blockUI({ message: $('#overlay') }); 
            //$.blockUI({ message: null }); 
            $.ajax({
                type: "POST",
                url: "login.aspx",
                data: $("form#frmServerLogin").serialize(),
                success:
			   	function (msg) {
			   	    //alert(msg);
			   	    if (msg != "2") {
			   	        if (msg == "0") {
			   	            alert(strAlertMemberError);
			   	            $("div#T_IncLogin table#tblLogin td#tdEmail").addClass("bgLoginBoxAlert");
			   	            $("div#T_IncLogin table#tblLogin td#tdEmail").removeClass("bgLoginBox");
			   	            $("div#T_IncLogin table#tblLogin td#tdPasswordLogin").addClass("bgLoginBoxAlert");
			   	            $("div#T_IncLogin table#tblLogin td#tdPasswordLogin").removeClass("bgLoginBox");
			   	        } else {
			   	            if (msg == "1") {
			   	                alert(strAlertMemberActivate);
			   	            } /*else{
								window.location.replace("password-re-discovery.aspx?id=1");
						 	 }*/
			   	            else if(msg == "3") {
			   	                alert("Oops! คุณยังไม่ได้คลิกลิงค์ยืนยันการตอบรับข่าวสารผ่านทางอีเมล์จากเนสท์เล่\nกรุณาคลิกยืนยันจากอีเมล์ที่ส่งไป เพื่อเป็นสมาชิก GoodFoodGoodLife.in.th ค่ะ");
			   	            }
			   	        }
			   	    } else {
			   	        if ($("#url").val() != "") {
			   	            window.location.replace($("#url").val());
			   	        } else {
			   	            window.location.replace("member-my-page.aspx");
			   	        }
			   	        //MM_openBrWindow('quiz-result.aspx?id='+$("form#frmServerLogin input#QuizID").val()+'&aid='+msg,'','width=620,height=620');
			   	        //alert("success"); 
			   	        //$.unblockUI({ 
			   	        // onUnblock: function(){alert("success"); } 
			   	        //}); 
			   	        // Redirect to thank you page

			   	    }

			   	    //alert( msg );
			   	},
                error: function (xhr, ajaxOptions, thrownError) {
                    //alert(xhr.status);
                    alert(thrownError);
                }

            });

        }
    }
    //});
    //alert($("form#frmServer").attr("id"));
    $("form#frmServerLogin").submit(function () {
        submitLoginForm();
        return false;
    });

    function validateLoginForm() {
        //var started = +new Date;

        isPost = false;
        bitAlert = false;
        contextLogin = $("table#tblLogin");

        contextLogin.find("input.bgAlert").removeClass("bgAlert");

        if (!ValidateEmail("emaillogin") || ($.trim($("#emaillogin").val()) == "Login Email")) {
            bitAlert = true;
            //if ($("div#T_IncLogin table#tblLogin td#tdEmail").hasClass("bgLoginBox")) $("div#T_IncLogin table#tblLogin td#tdEmail").addClass("bgLoginBoxAlert");
            $("div#T_IncLogin table#tblLogin td#tdEmail").addClass("bgLoginBoxAlert");
            $("div#T_IncLogin table#tblLogin td#tdEmail").removeClass("bgLoginBox");
        } else {
            $("div#T_IncLogin table#tblLogin td#tdEmail").addClass("bgLoginBox");
            $("div#T_IncLogin table#tblLogin td#tdEmail").removeClass("bgLoginBoxAlert");
        }

        if (!ValidateText("passwordlogin")) {
            bitAlert = true;
            $("div#T_IncLogin table#tblLogin td#tdPasswordLogin").addClass("bgLoginBoxAlert");
            $("div#T_IncLogin table#tblLogin td#tdPasswordLogin").removeClass("bgLoginBox");
        } else {
            $("div#T_IncLogin table#tblLogin td#tdPasswordLogin").addClass("bgLoginBox");
            $("div#T_IncLogin table#tblLogin td#tdPasswordLogin").removeClass("bgLoginBoxAlert");
        }

        if (bitAlert) {
            alert(strAlertMember);
            return false;
        } else {
            isPost = false;
            return true;
        }
        ////////


    }

    function AlertBg(objId) {
        if ((objId != undefined) && (objId != "")) {
            if (!$("#" + objId).hasClass("bgAlert")) $("#" + objId).addClass("bgAlert");
        }
    }

    function AlertBg2(objId) {
        obj = $(objId);
        if (!obj.hasClass("bgAlert")) obj.addClass("bgAlert");

    }

    function AlertTr4(objId) {
        objTr = $(objId);
        if (!objTr.hasClass("bgAlert")) objTr.addClass("bgAlert");
    }

    function ValidateRadio2(objName) {
        if ($(objName + ":checked").length == 0) {
            return false;
        } else {
            return true;
        }
    }

    function ValidateText(objId) {
        if ((objId != undefined) && (objId != "")) {
            if ($("input#" + objId).attr("id") != undefined) {
                if ($.trim($("input#" + objId).val()) == "") {
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }
    }

    function ValidateEmail(objId) {
        validRegExp = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if ((objId != undefined) && (objId != "")) {
            if ($("input#" + objId).attr("id") != undefined) {
                //alert(validRegExp.test($("input#"+objId).val()));
                if (validRegExp.test($("input#" + objId).val())) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        }
    }

});
