﻿String.prototype.ReplaceAll = function(searchArray, replaceArray) {
    var replaced = this;

    for (var i = 0; i < searchArray.length; i++) {
        replaced = replaced.replace(searchArray[i], replaceArray[i]);
    }

    return replaced;
}

String.prototype.StartsWith = function(value) {
    return (this.substr(0, value.length) == value);
}

// Extends the String object, creating a "EndsWith" method on it.
String.prototype.EndsWith = function(value, ignoreCase) {
    var L1 = this.length;
    var L2 = value.length;

    if (L2 > L1)
        return false;

    if (ignoreCase) {
        var oRegex = new RegExp(value + '$', 'i');
        return oRegex.test(this);
    }
    else
        return (L2 == 0 || this.substr(L1 - L2, L2) == value);
}

String.prototype.Remove = function(start, length) {
    var s = '';

    if (start > 0)
        s = this.substring(0, start);

    if (start + length < this.length)
        s += this.substring(start + length, this.length);

    return s;
}

String.prototype.Trim = function() {
    // We are not using \s because we don't want "non-breaking spaces to be caught".
    return this.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '');
}

String.prototype.LTrim = function() {
    // We are not using \s because we don't want "non-breaking spaces to be caught".
    return this.replace(/^[ \t\n\r]*/g, '');
}

String.prototype.RTrim = function() {
    // We are not using \s because we don't want "non-breaking spaces to be caught".
    return this.replace(/[ \t\n\r]*$/g, '');
}

String.prototype.ReplaceNewLineChars = function(replacement) {
    return this.replace(/\n/g, replacement);
}

String.prototype.Replace = function(regExp, replacement, thisObj) {
    if (typeof replacement == 'function') {
        return this.replace(regExp,
			function() {
			    return replacement.apply(thisObj || this, arguments);
			});
    }
    else
        return this.replace(regExp, replacement);
}

function onloadimage(ImgD, iwidth, iheight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        //ImgD.alt = "原图尺寸 宽 " + image.width + " × 高 " + image.height;
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}

//从RadioButtonList中得到选中的值
function getrbl(obj) {
    if (typeof obj == "string") obj = document.getElementById(obj);
    for (var i = 0; i < obj.getElementsByTagName('INPUT').length; i++) {
        var input = obj.getElementsByTagName('INPUT')[i];
        if (input.tagName == 'INPUT') {
            if (input.checked == true) {
                return input.value;
                break;
            }
        }
    }
    return null;
}

function setrbl(obj, value) {
    if (typeof obj == "string") obj = document.getElementById(obj);
    for (var i = 0; i < obj.getElementsByTagName('INPUT').length; i++) {
        var input = obj.getElementsByTagName('INPUT')[i];
        if (input.tagName == 'INPUT') {
            if (input.value == value) {
                input.checked = true;
                break;
            }
        }
    }
    return null;
}

//从DropDownList中得到选中的值
function getdrl(obj) {
    if (typeof obj == "string") obj = document.getElementById(obj);
    return obj.options[obj.selectedIndex].value;
}

function setdrl(obj, value) {
    if (typeof obj == "string") obj = document.getElementById(obj);
    if (value == null || value == '') {
        obj.options[0].selected = true;
        return;
    }
    for (var i = 0; i < obj.options.length; i++) {
        if (obj.options[i].value == value) {
            obj.options[i].selected = true;
            break;
        }
    }
}

Array.prototype.remove = function(string, limit) {
    var len = this.length, i = 0, count = 0;
    for (i; i < len; i++) {
        if (this[i] == string) {
            this.splice(i, 1);
            if (limit && limit > 0) {
                count++;
                if (count == limit) break;
            }
        }
    }
    return this;
}

String.prototype.substitute = function(sub) {
    return this.replace(/\{(.+?)\}/g, function($0, $1) {
        return (sub[$1] != undefined) ? sub[$1] : $0;
    });
}

function IsNum(digitnum) {
    if (digitnum == undefined) {
        return false;
    }
    if (typeof digitnum != "string") {
        return false;
    }

    var re = /[^0-9]/;
    var ret = digitnum.match(re);
    if (ret == null) {
        return true;
    }
    else {
        return false;
    }
}

function FormatDecimal(num, v) {
    var vv = Math.pow(10, v);
    return Math.round(num * vv) / vv;
}

function getposition(obj) {
    var r = new Array();
    r['x'] = obj.offsetLeft;
    r['y'] = obj.offsetTop;
    while (obj = obj.offsetParent) {
        r['x'] += obj.offsetLeft;
        r['y'] += obj.offsetTop;
    }
    return r;
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options);
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function CloseDialog()
{
    //jQuery('#mini-cart-dialog').hide();
    jQuery('#mini-cart-dialog').fadeOut(500);
}
function ShowDialog(title, content, width, height, target) {

    var targetheight = 0;
    var p = null;    
    if (target instanceof jQuery) {
        p = getposition(target[0]);
        targetheight = target.height();
    } else {
        p = getposition(target);
        targetheight = target.offsetHeight;
    }
    var dialog = jQuery('#mini-cart-dialog');
    
    var html = jQuery('#template-modal').html();
    
    var js_obj = { 'title': title, 'content': content };
    html = html.substitute(js_obj);
    dialog.html(html);

    //dialog.css({ 'width': width, 'top': target.offset().top, 'left': target.offset().left });

    dialog.css({ 'width': width, 'height': height, 'top': p['y'] + targetheight, 'left': p['x'] });
    dialog.fadeIn(200);
    jQuery('#mini-cart-dialog .dialog-close-btn').unbind('click').bind('click', function() {
        dialog.fadeOut(500);
    });
    update_cartstatus();
}

function AddToCart(ProductId, target, GoCar) {
    ShowDialog('正在加入购物车', '正在加入购物车...', 300, 50, target);

    jQuery.get("/ajax.aspx?t" + new Date().getTime(), { "cmd": "addtocart", "ProductId": ProductId }, function(responseText, textStatus, XMLHttpRequest) {
    ShowDialog('成功加入购物车', responseText, 300, 50, target);

        if (GoCar != null && GoCar) {
            location = '/cart.html';
        }
    });
}
function AddToCollection(goods_Id,target) {
    ShowDialog('正在加入收藏夹', '正在加入收藏夹...', 200, 40, target);
    jQuery.get("/ajax.aspx?t" + new Date().getTime(), { "cmd": "addtocollection", "goods_id": goods_Id }, function(responseText, textStatus, XMLHttpRequest) {
        var title = responseText;
        var content = responseText;
        if (responseText == '-1') {
            title = '收藏失败';
            content = '收藏失败';
        }
        if (responseText == '0') {
            title = '成功加入收藏夹';
            content = '成功加入收藏夹';
            window.setTimeout(CloseDialog, 2000);
        }
        if (responseText == '1') {
            title = '已经加入收藏夹';
            content = '已经加入收藏夹';
            window.setTimeout(CloseDialog, 2000);
        }
        if (responseText == '9') {
            title = '请登录';
            content = '请登录';
        }
        ShowDialog(title, content, 200, 40, target);
    });
}
function AddToNotify(ProductId) {
    alert(ProductId);
}

function update_cartstatus() {
    jQuery.get("/ajax.aspx?t" + new Date().getTime(), { "cmd": "getcartcount" }, function(responseText, textStatus, XMLHttpRequest) {
        jQuery('#span_cartcount').html('(' + responseText+')');
    });
}
function loginout() {
    if (window.confirm('确定注销吗？')) {
        location = '/profile/loginout.html';
    }
}



function share(sType, sName, iGameId, imgurl) {
    var en = encodeURIComponent;
    //var l=document.location.href;
    var l = 'http://www.662.com/android/' + iGameId + '.html';
  
    var t = sName + '在线小游戏 — www.662.com，662小游戏，就是好玩！'
    var s = screen;
    var sImgUrl = ['http://www.662.com/' + imgurl];
    
    var sContent = ['我在662小游戏上玩了《', sName, '》，很有意思~~分享给大家！来试试吧！'].join('');
    var sUrl = '';
    var w = 600;
    var h = 500;
    var statFlag = '';
    if (sType == 'qzone') {
        sUrl = ['http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=',
		en('http://www.662.com/android/' + iGameId + '.html?ADTAG=share.mid.qzone&imgtype=' + imgurl)].join('');
        w = 850;
        h = 650;
        statFlag = 'qzone';
    }
    if (sType == 'renren') {
        l += 'renren';
        sUrl = ['http://www.connect.renren.com/share/sharer?url=', en(l), '&title=', en(t)].join('');
        w = 626;
        h = 436;
        statFlag = 'renren';
    }
    else if (sType == 'douban') {
        l += 'douban';
        sUrl = ['http://www.douban.com/recommend/?url=', en(l), '&title=', en(t), '&sel=', en(sContent), '&v=1'].join('');
        w = 450;
        h = 330;
        statFlag = 'douban';
    }
    else if (sType == 'kaixin') {
        l += 'kaixin';
        sUrl = ['http://www.kaixin001.com/repaste/bshare.php?rtitle=', en(t), '&rurl=', en(l), '&rcontent=', en(sContent)].join('');
        w = 545;
        h = 360;
        statFlag = 'kaixin';
    }
    else if (sType == 'xlwb') {
        l += 'xlwb';
        sUrl = ['http://v.t.sina.com.cn/share/share.php?c=&url=', en(l), '&title=', en(sContent), '&content=utf8&pic=', en(sImgUrl)].join('');
        w = 610;
        h = 570;
        statFlag = 'tsina';
    }
    else if (sType == 'txwb') {
        l += 'txwb';
        sUrl = ['http://v.t.qq.com/share/share.php?site=', en('www.662.com'), '&url=', en(l), '&title=', en(sContent), '&pic=', en(sImgUrl), '&appkey=', en("c347633533ae4e6986928a3f45a4a605aaa")].join('');
        w = 700;
        h = 470;
        statFlag = 'tqq';
    }

    x = function() {
        if (!window.open(sUrl, sType, ['toolbar=0,resizable=1,status=0,width=', w, ',height=', h, ',left=', (s.width - w) / 2, ',top=', (s.height - h) / 2].join(''))) {
            location.href = sUrl;
        }
    }
    if (/Firefox/.test(navigator.userAgent)) { setTimeout(x, 0) } else { x() }

  
}
