﻿/// <reference path="Jquery.js" />

var CommonBase = {
    AjaxForHTML: function(url, type, data, successfn, errorfn)
    {
        $.ajax({
            url: url, type: type, cache: false,
            dataType: "html", data: data,
            success: successfn != undefined ? successfn : function(o)
            {
                alert(o);
            },
            error: errorfn != undefined ? errorfn : function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText); }
        });
    },
    DialogDiv: function(dialogName, dialogTitle, iframeSrc, isScrolling, srcParams, iframeWidth, iframeHeight, isOk, isExit, isDrag)
    {
        JqueryDialog.OpenDialog(dialogName, dialogTitle, iframeSrc, isScrolling, srcParams, iframeWidth, iframeHeight, isOk, isExit, isDrag);
    }
}

CommonBase.Default = {
    GetThemeTemplateList: function(themeId) {

        var iptThemeIdObj = $("#iptThemeId");

        if ($.trim(iptThemeIdObj.val()) == themeId) return;

        CommonBase.AjaxForHTML("/CommSource/ThemeTemolateList.ashx", "get", "themeId=" + themeId, function(data) {
            if (data == "N") {
                alert("暂无相关数据...");
            }
            else {
                $("#themeTemplateListDiv").html(data);
            }

            iptThemeIdObj.val(themeId);
        });
    },    
    GetProductBookBrowById: function(id) {
        if (id == "") { alert("参数错误，请刷新页面后再试..."); return; }
        CommonBase.AjaxForHTML("/Default.aspx", "get", "photoBookId=" + id, function(data) {
            if (data == "N") {
                alert("参数错误，获取信息失败，请刷新页面后再试...");
            }
            else {            
                $("#productBookIframe").attr("src", data);
            }
        });
    },
    BrowPhotoBookDiv: function(id) {
        //获得浏览地址...
        CommonBase.AjaxForHTML("/Sites/Template/TemplateDetail.aspx", "get", "browTemplateId=" + id, function(data) {
        if (data != "N") {
                CommonBase.DialogDiv("photoBook", "", data, false, null, 710, 310, false, false, false);
                $('#photoBook_dialog_s').hide();
            }
            else
                alert("参数错误，获取信息失败，请刷新页面后再试...");
        });
    },
    AutoRegisterAccount: function(id) {
        if (id == "") { alert("参数错误，请刷新页面后再试..."); return; }

        if (!confirm("您确定要注册此账号吗?")) return;
        
        var hostname=location.hostname;
        if(hostname.indexOf("shop.1651ky.cn")<0)
        {
            CommonBase.AjaxForHTML("/CommSource/CommGetSource.ashx", "get", "getUrl=true", function(data) {            
            window.location.href='http://'+data +'/CommSource/CommGetSource.ashx?iframe=true&register=auto&id='+id;                
            });
            
        }else{        
            CommonBase.AjaxForHTML("/CommSource/CommGetSource.ashx", "get", "register=auto&id=" + id, function(data) {
            var result = data.split('|');
            if (result.length > 1) {
                alert("恭喜您注册成功，您现在可以进入后台修改个人信息资料。\r\r请记住您账号的密码“" + result[1] + "”。");
                window.location.reload();
            }
            else {
                alert("参数错误，自动添加账号信息失败，请稍后再试...");
            }       
            });
        }
    },
    CheckUserLogin:function()
    {
        var name = jQuery.trim($("#tbxLoginName").val());
        var pwd = jQuery.trim($("#tbxPwd").val());
        var chxCheck="";
        if($("#chxActive").attr("checked")==true)
        {
            chxCheck="yes";
        }
        if (name == "")
        {
            alert("请输入用户名...");
            $("#tbxLoginName")[0].focus();
            return false;
        } 
        if (pwd == "")
        {
            alert("请输入密码...");
            $("#tbxPwd")[0].focus();
            return false;
        }
        if (pwd.length < 6 || pwd.length > 30)
        {
            alert("密码格式错误...");
            $("#tbxPwd")[0].select();
            return false;
        }
        var url = "/default.aspx?iframe=false&LoginAction=Check&UserName=" + escape(name)+"&password="+escape(pwd)+"&chxActive="+chxCheck;
        send_request("GET", url, null, "text", populateClass5);
    }
}

function populateClass5() {
    var f = document.getElementById("city1");
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            var data = http_request.responseText;
            if (data.indexOf("Err-")>=0) {
                    var result=data.replace("Err-","");
                    alert(result);
                    return false;
                }
                else
                {
                    if(data.indexOf("账号激活成功")>=0)
                    {
                        var result=data.replace("账号激活成功...!","");
                        alert("账号激活成功...!");
                        window.location.href=result;
                    }else{
                        window.location.href=data;
                    }
                 }
            }else{
            alert(http_request.status);
			return false;
            }
      }
}

CommonBase.Template = {
    LoadOnlineCustomerServiceQQ: function()
    {
        CommonBase.AjaxForHTML("/CommSource/CommGetSource.ashx", "get", "shoppingQQ=ok", function(data)
        {
            if(document.getElementById("spanOnlineCustomerServiceQQ1"))
            {
                $("#spanOnlineCustomerServiceQQ1").html(data);
            }
            if(document.getElementById("spanOnlineCustomerServiceQQ"))
            {
                $("#spanOnlineCustomerServiceQQ").html(data);
            }
            
        });
    },
    LoadShoppingQQ: function()
    {
        CommonBase.AjaxForHTML("/CommSource/CommGetSource.ashx", "get", "shoppingQQ=ok", function(data)
        {
            $("#spanShoppingQQ").html(data);
        });
    },
    GoToandMakeit:function(templateid)
    {
        var FirstPrintAttributeId=jQuery.trim($("#hfFirstPrintAttributeId").val());
        var SecondPrintAttributeId=jQuery.trim($("#hfSecondPrintAttributeId").val());
        
        CommonBase.AjaxForHTML("/Sites/Template/TemplateDetail.aspx", "get", "Action=GoToandMakeit&id="+templateid+"&FirstPrintAttributeId="+escape(FirstPrintAttributeId)+"&SecondPrintAttributeId="+escape(SecondPrintAttributeId), function(data) {
        if (data.indexOf("Err-")>=0) {
            var result=data.replace("Err-","");
                alert(result);
                return false;
        }else
        {
                window.location.href=data;
         }
        });
    },
    AddtoFavorites:function(templateid)
    {
        CommonBase.AjaxForHTML("/Sites/Template/TemplateDetail.aspx", "get", "Action=AddtoFavorites&id="+templateid, function(data) {
        alert(data);
        });
    }
}

CommonBase.Product={
    AddToShoppingCart:function(Argument)
    {
        var Productid=jQuery.trim($("#ProductId").val());
        var FirstPrintAttributeId=jQuery.trim($("#hfFirstPrintAttributeId").val());
        var SecondPrintAttributeId=jQuery.trim($("#hfSecondPrintAttributeId").val());
        var num=jQuery.trim($("#txtNumber").val());
         CommonBase.AjaxForHTML("/Sites/Product/ProductDetail.aspx", "get", "Action=AddtoCart&id="+Productid+"&Argument=" + escape(Argument)+"&FirstPrintAttributeId="+escape(FirstPrintAttributeId)+"&SecondPrintAttributeId="+escape(SecondPrintAttributeId)+"&num="+num, function(data) {
        if (data.indexOf("Err-")>=0) {
            var result=data.replace("Err-","");
                alert(result);
                return false;
        }else
        {
                window.location.href=data;
         }
        });
    }
}

CommonBase.Points={
    AddToShoppingCart:function(Proid)
    {
        CommonBase.AjaxForHTML("/Sites/Club/ProductDetail.aspx", "get", "Action=AddtoCart&id="+Proid, function(data) {
        if (data.indexOf("Err-")>=0) {
            var result=data.replace("Err-","");
                alert(result);
                return false;
        }else
        {
                window.location.href=data;
         }
        });
    }
}

CommonBase.Site = {
    GetObjTop: function(e) {
        var offset = e.offsetTop;
        if (e.offsetParent != null) offset += CommonBase.Site.GetObjTop(e.offsetParent);
        return offset;
    },
    GetObjLeft: function(e) {
        var offset = e.offsetLeft;
        if (e.offsetParent != null) offset += CommonBase.Site.GetObjLeft(e.offsetParent);
        return offset;
    },
    AdByObjID: function(o, obj) {

        var myObject = eval('(' + o + ')');
        var link = obj + "a";
        var img = obj + "img";

        if (myObject == undefined) return;

        if (myObject[0] == undefined) return;

        var linkurl = myObject[0].LinkUrl;

        if (linkurl != "#") {
            $("#" + link).attr("href", myObject[0].LinkUrl);
        }
        var imgurl=myObject[0].Path;
        
        if(imgurl!="")
        {
        $("#" + img).attr("src",imgurl);
        }else{
        $("#" + img).attr("display", "none");
        }
    },

    AdByCreateHtml: function(o, obj) {
        var myObject = eval('(' + o + ')');

        var linkurl = myObject[0].LinkUrl;
        var html = "";
        if (linkurl != "#") {
            html = "<a href='" + myObject[0].LinkUrl + "'><img src='" + myObject[0].Path + "' /></a>";
        }
        else {
            html = "<img src='" + myObject[0].Path + "' />";
        }

        $("#" + obj).html(html);
    },

    AdByIndexLh: function(o) {
        var myObject = eval('(' + o + ')');

        var html = "";
        var htmlMin = "<ul><li class='first btnPrev'><img id='play_prev' src='images/btn_prev.gif'> </li>";
        for (var i = 0; i < myObject.length; i++) {
            var ii = parseInt(i) + 1;
            var linkurl = myObject[i].LinkUrl;
            if (ii == 1) {
                if (linkurl != "#") {
                    html += "<div class='image' class='open' id='image_xixi-0" + ii + "'><a  href='" + myObject[i].LinkUrl + "' target='_blank'><img src='" + myObject[i].Path + "'> </a><div class='word'><h3></h3><p></p></div></div>";
                }
                else {
                    html += "<div class='image' class='open' id='image_xixi-0" + ii + "'><img src='" + myObject[i].Path + "'><div class='word'><h3></h3><p></p></div></div>";
                }
            }
            else {
                if (linkurl != "#") {
                    html += "<div class='image' id='image_xixi-0" + ii + "'><a  href='" + myObject[i].LinkUrl + "' target='_blank'><img src='" + myObject[i].Path + "'> </a><div class='word'><h3></h3><p></p></div></div>";
                }
                else {
                    html += "<div class='image' id='image_xixi-0" + ii + "'><img src='" + myObject[i].Path + "'><div class='word'><h3></h3><p></p></div></div>";
                }
            }

            htmlMin += "<li class='slideshowItem'><a id='thumb_xixi-0" + ii + "' href='#image_xixi-0" + ii + "'><img height='20' src='" + myObject[i].Path.split('-')[0] + "-100-50" + myObject[i].Path.substring(myObject[i].Path.lastIndexOf('.'), myObject[i].Path.length) + "' width='48'> </a></li>";
        }

        $("#featured").html(html);

        htmlMin += "<li class='last btnNext'><img id='play_next' src='images/btn_next.gif'><div class='clear'></div></li></ul>";
        $("#thumbs").html(htmlMin);
    }
}

function TopDomainLogin(url)
    {
        var name = jQuery.trim($("#tbxLoginName").val());
        var pwd = jQuery.trim($("#tbxPwd").val());
        var chxCheck="";
        if($("#chxActive").attr("checked")==true)
        {
            chxCheck="yes";
        }
        if (name == "")
        {
            alert("请输入用户名...");
            $("#tbxLoginName")[0].focus();
            return false;
        } 
        if (pwd == "")
        {
            alert("请输入密码...");
            $("#tbxPwd")[0].focus();
            return false;
        }
        if (pwd.length < 6 || pwd.length > 30)
        {
            alert("密码格式错误...");
            $("#tbxPwd")[0].select();
            return false;
        }
        document.frmLogin.target="_top";
        document.frmLogin.action=url +"/Default.aspx?iframe=true&LoginAction=Check&UserName=" + escape(name)+"&password="+escape(pwd)+"&chxActive="+chxCheck;
        document.frmLogin.submit();
    }

function base64encode(str) {
    str = utf16to8(str);
    var out, i, len;
    var c1, c2, c3;
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    len = str.length;
    i = 0;
    out = "";
    while (i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if (i == len) {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if (i == len) {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}
function utf16to8(str) {
    var out, i, len, c;
    out = ""; len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}
