﻿$(document).ready(function () {
    var beforeSendMessage = 'Loading, please wait ...';
    var ErrorMessage = '<span class="red">Sorry, wrong request. Please try again!</span>';

    /*shoping cart*/

    refreshMiniCart();

     $("#MiniRegForm").submit(function () {
        checkRegedit();
      });

      
     $("#MiniLoginForm").submit(function () {
        checkLogin();
      });

});



function refreshMiniCart() {
    var rand = genRandNumber(0, 10000);
    var Product_Quantity;

    $.ajax({
    url: "/SetCart.aspx?action=minicart&rond=" + rand + "jsoncallback=?",
        cache: false,
        success: function (html) {
            $("#shopingCart-list").html(html);
            Product_Quantity = $("#Total_Product_Quantity", html).html();
            if (Product_Quantity == "") {
                Product_Quantity = 0;
            }
            $('#shopingCart-amount').text(Product_Quantity);
        }
    });

}

$(function () {
    var object = $("#o-shopingCart-list");
    $("#i-shopingCart").bind("mouseover", function () {
        object.fadeIn();
    }).bind("mouseleave", function () {
        object.fadeOut();
    })

})




function genRandNumber(startNum, endNum) {
    var randomNumber;
    randomNumber = Math.round(Math.random() * (endNum - startNum)) + startNum;

    return randomNumber;
}

function addCartTip() {
    jQuery.jMessageBox.show('The goods you selected is out of stock, please consider the same model with different color.');

}

function addCart() {
    var rand = genRandNumber(0, 10000);
    var id,num, Goods_color;
    num = $("#num").val();
    id = $("#id").val();

    if (num < 1) {
        jQuery.jMessageBox.show('Please enter the number of products!');
        return;
    }

    jQuery.getJSON("/SetCart.aspx?action=addcart&jsoncallback=?",
    { "id": id, "num": num, "rond": rand },
   function (data) {
       if (data.id == 11) {
           refreshMiniCart();
           jQuery.jMessageBox.show(data.messages);

       }
       else if (data.id == 0) {
           login();
           ///todo
       }  else {
           jQuery.jMessageBox.show(data.messages);
            
       }

   });

}

function addCartBy(pid) {    
    $("#id").val(pid); 
    addCart();
}

function addProduct(pid) {
    var pnum = $("#pnum").val();
    $("#id").val(pid);
    if (pnum > 1) {
        $("#num").val(pnum);  
    }
    
   addCart();
}

    function login() {

        $.blockUI({
            message: $("#UserLoginDialogPanel"),
            css: {
                top: ($(window).height() - 500) / 2 + 'px',
                left: ($(window).width() - 700) / 2 + 'px',
                width: '700px'
            }
        });  
        
    }
/*
    function LoginAddCart() {
        var rand = genRandNumber(0, 10000);
        var num, id, Goods_color;
        num = $("#num").val();
        id = $("#id").val();

        jQuery.getJSON("/AjaxLogin.aspx?action=userlogin",
    { "action": "checklogin", "rond": rand },
     function (data) {
          
         $.each(data, function (i, item) {
             if (item.id == 1) {
                 $("#UserLoginTopMessages").html(item.mtop);
                 $("#UserLoginRightMessages").html(item.mright);
             }
             else if (item.id == 0) {
                 $("#UserLoginTopMessages").html(item.mtop);
                 $("#UserLoginRightMessages").html(item.mright);
             }
         });
     });

        if (num < 1) {
            jQuery.jMessageBox.show('Please enter the number of products!');
            return;
        }

        jQuery.getJSON("/SetCart.aspx?action=addcart&jsoncallback=?",
    { "id": id, "num": num, "rond": rand },
   function (data) {
       if (data.id == 11) {
           refreshMiniCart();
           jQuery.jMessageBox.show(data.messages);

       }
       else if (data.id == 0) {
           login();
           ///todo
       }else {
           jQuery.jMessageBox.show(item.messages);
       }

   });

    }

 */   


////////////////////


function userLoginClose() {
    jQuery.unblockUI();
}

function checkLogin() {
    checkLoginClear();
    var messageFromateStrat = '<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center"> ';
    var messageFromateEnd = '</font>';
    var userName = $("#loginUserName").val();
    var password = $("#loginPassword").val();
    var isValid = false;
    var message = "";
    if ($.trim(userName) == "" || $.trim(password) == "") {
        isValid = false;
        if ($.trim(userName) == "") {
            $("#spt_loginUserName").html(messageFromateStrat + "User name can not be empty!" + messageFromateEnd);
            $("#spt_loginUserName").show();
        }

        if ($.trim(password) == "") {
            $("#spt_loginPassword").html(messageFromateStrat + "Password can not be empty!" + messageFromateEnd);
            $("#spt_loginPassword").show();
        }

    }
    else {
        checkLoginClear();
        isValid = true;
    }

    if (isValid) {
        $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center">Being logged, please wait. . . ! </font>').slideDown("slow");

        CheckLoginPost(userName, password);

    }
}





function checkLoginClear() {
    $("#showmessage").html("")
						 .hide();
    $("#spt_loginUserName").html("")
						 .hide();
    $("#spt_loginPassword").html("")
						 .hide();
}
function checkLoginClear2() {
    var rand = genRandNumber(0, 10000);
    var num, id;
    num = $("#num").val();
    id = $("#id").val();
    jQuery.getJSON("/AjaxLogin.aspx?action=logincheck&jsoncallback=?",
    { "rond": rand },
     function (msg) {
         if (msg.id == 11) {
             $("#UserLoginTopMessages").html(msg.messages);

         } else {
             $("#UserLoginTopMessages").html(msg.messages);

         }

     });

    if (num < 1) {
        jQuery.jMessageBox.show('Please enter the number of products!');
        return;
    }

    jQuery.getJSON("/SetCart.aspx?action=addcart&jsoncallback=?",
    { "id": id, "num": num, "rond": rand },
   function (data) {
       if (data.id == 11) {
           refreshMiniCart();
           jQuery.jMessageBox.show(data.messages);

       }
       else if (data.id == 0) {
           // login();
           alert("Login failed!");
       } else {
           jQuery.jMessageBox.show(item.messages);
       }

   });

    $("#showmessage").html("")
						 .hide();
    $("#spt_loginUserName").html("")
						 .hide();
    $("#spt_loginPassword").html("")
						 .hide();
    jQuery.unblockUI();

}


function CheckLoginPost(userName, password) {

    $.ajax({
        type: "POST",
        url: "/AjaxLogin.aspx?action=userlogin&jsoncallback=?",
        data: "username=" + userName + "&password=" + password + "",
        dataType: "jsonp",
        success: function (msg) {
            if (msg.id == 11) {
                checkLoginClear2();
            } else {
                alert(msg.messages);
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center">Login failed, please re-login ! </font>').slideDown("slow");
            }

        }
    });


}


/////////////////////////////////////////////////////////






////////////////////


function checkRegedit() {
    checkRegeditClear();
    var messageFromateStrat = '<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center"> ';
    var messageFromateEnd = '</font>';
    var userName = $("#regUserName").val();
    var password = $("#regPassword").val();
    var password1 = $("#regPassword1").val();
    var isValid = false;
    var message = "";
    if ($.trim(userName) == "" || $.trim(password) == "" || $.trim(password1) == "") {
        isValid = false;
        if ($.trim(userName) == "") {
            $("#spt_regUserName").html(messageFromateStrat + "User name can not be empty!" + messageFromateEnd);
            $("#spt_regUserName").show();
        }

        if ($.trim(password) == "") {
            $("#spt_regPassword").html(messageFromateStrat + "Password can not be empty!" + messageFromateEnd);
            $("#spt_regPassword").show();
        }

        if ($.trim(password1) == "") {
            $("#spt_regPassword1").html(messageFromateStrat + "Confirm password can not be empty!" + messageFromateEnd);
            $("#spt_regPassword1").show();
        }

    } else if (password != password1) {
        $("#spt_regPassword1").html(messageFromateStrat + "Passwords do not match!" + messageFromateEnd);
        $("#spt_regPassword1").show();
    }
    else {
        checkRegeditClear();
        isValid = true;
    }

    if (isValid) {
        $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center">Being logged, please wait. . . ! </font>').slideDown("slow");

        checkRegeditPost(userName, password, password1);

    }
}





function checkRegeditClear() {

    $("#showmessage").html("")
						 .hide();
    $("#spt_regUserName").html("")
						 .hide();
    $("#spt_regPassword").html("")
						 .hide();
    $("#spt_regPassword1").html("")
						 .hide();
}
function checkRegeditClear2() {
    var rand = genRandNumber(0, 10000);
    var num, id;
    num = $("#num").val();
    id = $("#id").val();

    jQuery.getJSON("/AjaxLogin.aspx?action=logincheck&jsoncallback=?",
    { "rond": rand },
     function (msg) {
         if (msg.id == 11) {
             $("#UserLoginTopMessages").html(msg.messages);

         } else if (msg.id == 0) {
             $("#UserLoginTopMessages").html(msg.messages);

         }

     });

    if (num < 1) {
        jQuery.jMessageBox.show('Please enter the number of products!');
        return;
    }

    jQuery.getJSON("/SetCart.aspx?action=addcart&?jsoncallback=?",
    { "id": id, "num": num, "rond": rand },
   function (data) {
       if (data.id == 11) {
           refreshMiniCart();
           jQuery.jMessageBox.show(data.messages);

       }
       else if (data.id == 0) {
           //login();
           ///todo
       } else if (data.id == 3) {
           addCartTip();
       } else {
           jQuery.jMessageBox.show(item.messages);
       }

   });

    $("#showmessage").html("")
						 .hide();
    $("#spt_regUserName").html("")
						 .hide();
    $("#spt_regPassword").html("")
						 .hide();
    $("#spt_regPassword1").html("")
						 .hide();
    jQuery.unblockUI();

}


function checkRegeditPost(userName, password, password1) {

    $.ajax({
        type: "POST",
        url: "/AjaxLogin.aspx?action=reg&jsoncallback=?",
        data: "userName=" + userName + "&password=" + password + "&password1=" + password1 + "",
        dataType: "jsonp",
        success: function (msg) {
            if (msg.id == 1) {
                checkRegeditClear2();
            } else if (msg.id == 0) {
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center">' + msg.messages + ' </font>').slideDown("slow");
            } else if (msg.id == 2) {
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center"> Validation error, please re-submit the revised! </font>').slideDown("slow");
                $("#spt_regUserName").html('<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center">' + msg.messages + ' </font>').show();
            } else if (msg.id == 3) {
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center"> Validation error, please re-submit the revised! </font>').slideDown("slow");
                $("#spt_regPassword1").html('<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center">' + msg.messages + ' </font>').show();
            } else if (msg.id == 4) {
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center"> Validation error, please re-submit the revised! </font>').slideDown("slow");
                $("#spt_regPassword").html('<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center">' + msg.messages + ' </font>').show();
                $("#spt_regPassword1").html('<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center">' + msg.messages + ' </font>').show();
            } else if (msg.id == 5) {
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center"> Validation error, please re-submit the revised! </font>').slideDown("slow");
                $("#spt_regUserName").html('<font style="font-size:12px; line-height:1.5; color:#9a0636; text-align:center">' + msg.messages + ' </font>').show();
            } else {
                alert(msg.messages);
                $("#showmessage").html('<font style="font-size:14px; line-height:1.5; color:#9a0636; text-align:center">Unknown error, please contact our staff! </font>').slideDown("slow");
            }

        }
    });


}


function addComment() {

    jQuery.blockUI();
    var rand = genRandNumber(0, 10000);
    var Title, Contents, pid;
    Title = $.trim($("#Title").val());
    Contents = $.trim($("#Contents").val());
    pid = $.trim($("#pid").val());  

    if (Title == "") {
        jQuery.unblockUI();
        jQuery.jMessageBox.show('Please enter the  Title !');
        return;
    }

    if (Contents == "") {
        jQuery.unblockUI();
        jQuery.jMessageBox.show('Please enter the Contents !');
        return;
    }

    $.ajax({
        type: "POST",
        url: "/SetUser.aspx?format=json&jsoncallback=?",
        data: "action=AddReview&Title=" + Title + "&Contents=" + Contents + "&pid=" + pid + "&rond=" + rand + "",
        dataType: "jsonp",
        success: function (msg) {
            if (msg.id == 1) {
                jQuery.unblockUI();
                jQuery.jMessageBox.show(msg.messages);
            } else if (msg.id == 2) {
                jQuery.unblockUI();
                 jQuery.jMessageBox.show(msg.messages);
                  location.href = "/login.html";
                 
            } else {
                jQuery.unblockUI();
                jQuery.jMessageBox.show(msg.messages);

            }

        }
    });


}


function gotoReview(currentPage) {

    jQuery.blockUI();
    var rand = genRandNumber(0, 10000);
    var PageSize, currentPage, pid;
  
    pid = $.trim($("#pid").val());
    PageSize = $.trim($("#PageSize").val()); 
    $.ajax({
        type: "POST",
        url: "/SetUser.aspx?format=json&jsoncallback=?",
        data: "action=GetListReview&currentPage=" + currentPage + "&PageSize=" + PageSize + "&pid=" + pid + "&rond=" + rand + "",
        dataType: "jsonp",
        success: function (msg) {
            if (msg.id == 1) {
                jQuery.unblockUI();
                $("#ListReview").html(msg.messages);
             }  else {
                jQuery.unblockUI();
                jQuery.jMessageBox.show(msg.messages);

            }

        }
    });


}


function ClearViewList() {

    jQuery.blockUI();
    var rand = genRandNumber(0, 10000);
 
    $.ajax({
        type: "POST",
        url: "/SetUser.aspx?format=json&jsoncallback=?",
        data: "action=ClearViewList&rond=" + rand + "",
        dataType: "jsonp",
        success: function (msg) {
            if (msg.id == 1) {
                jQuery.unblockUI();
                $("#viewlistul").html("<li>No Data!</li>");
            } else {
                jQuery.unblockUI();
                jQuery.jMessageBox.show(msg.messages);

            }

        }
    });

}

/////////////////////////////////////////////////////////
 
