﻿//验证Email地址
function RegexEmail(textput) {
    var search_str = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
    if (!search_str.test(textput)) {
        return false;
    }
    return true;
}

function GetErrorInfo(flag) {
    var result = 0;
    switch (parseInt(flag)) {
        case 0:
            result = "The user does not exist"; //不存在该用户
            break;
        case 1:
            result = "This user has been deleted"; //该用户已被删除
            break;
        case 2:
            result = "This user has been banned Login"; //该用户已被禁止登陆
            break;
        case 3:
            result = "password do not match."; //密码错误
            break;
        //case 5:                                         
        //    result = "登陆成功";                                         
        //    break;                                         
    }
    return result;
}

/*
**************图片按比例缩放、预加载插件******************
///说明：在图片加载前显示一个加载标志，当图片下载完毕后显示图片出来
可对图片进行是否自动缩放功能
此插件使用时可让页面先加载，而图片后加载的方式，
解决了平时使用时要在图片显示出来后才能进行缩放时撑大布局的问题
///参数设置：
scaling 是否等比例自动缩放
width 图片最大高
height 图片最大宽
loadpic 加载中的图片路径
*/
jQuery.fn.LoadImage = function (scaling, width, height, loadpic) {
    if (loadpic == null) loadpic = "load3.gif";
    return this.each(function () {
        var t = $(this);
        var src = $(this).attr("src")
        var img = new Image();
        //alert("Loading")
        img.src = src;
        //自动缩放图片
        var autoScaling = function () {
            if (scaling) {

                if (img.width > 0 && img.height > 0) {
                    if (img.width / img.height >= width / height) {
                        if (img.width > width) {
                            t.width(width);
                            t.height((img.height * width) / img.width);
                        } else {
                            t.width(img.width);
                            t.height(img.height);
                        }
                    }
                    else {
                        if (img.height > height) {
                            t.height(height);
                            t.width((img.width * height) / img.height);
                        } else {
                            t.width(img.width);
                            t.height(img.height);
                        }
                    }
                }
            }
        }
        //处理ff下会自动读取缓存图片
        if (img.complete) {
            //            alert("getToCache!");
            autoScaling();
            return;
        }
        // $(this).attr("src", loadpic);
        //        var loading = $("<img alt=\"加载中\" title=\"图片加载中\" src=\"" + loadpic + "\" />");

        t.hide();
        //        t.after(loading);
        $(img).load(function () {
            autoScaling();
            //                    loading.remove();
            t.attr("src", this.src);
            t.show();
            //alert("finally!")
        });

    });
}

function WebService(url, callback, pars) {
    $.ajax({
        data: pars,
        url: url,
        type: "POST",
        contentType: "application/json;utf-8",
        dataType: 'json',
        cache: false,
        success: function (json) {
            callback(json.d);
        },
        error: function (xml, status) {
            if (status == 'error') {
                try {
                    var json = eval('(' + xml.responseText + ')');
                    alert(json.Message + '\n' + json.StackTrace);
                } catch (e) { }
            } else {
                alert(status);
            }
        },
        beforeSend: function (xml) {
            if (!pars) xml.setRequestHeader("Content-Type", "application/json;utf-8")
        }
    });
}

/*
Name:判断字符串是否已指定字符串结尾
Author:Maikou
DateTime:2010-12-02
*/
jQuery.fn.EndsWith = function (tarval) {
    return (this.toString() && this.toString().indexOf(tarval) == (this.toString().length - tarval.length));
}

/*
Name:获取图片名称
Author:Maikou
DateTime:2010-12-02
*/
jQuery.fn.ImgName = function () {
    var s = this.attr("src");
    return s.substring(s.lastIndexOf("/") + 1);
}

/*
限制多行文本框长度
count：最大字符数
displayId：此ID所在的元素显示可输入字符数
*/
jQuery.fn.SetTextAreaLength = function (count, displayId) {
    if (displayId) {
        $("#" + displayId).text(count);
    }
    this
    //    .mouseout(function () {
    //        $(this).change();
    //    })
    //            .mouseup(function () {
    //                $(this).change();
    //            })
            .keydown(function () {
                $(this).change();
            })
            .keyup(function () {
                $(this).change();
            })
            .keypress(function (event) {
                $(this).change();
            })
            .change(function () {

                if ($(this).val().length > count) {
                    $(this).val($(this).val().substring(0, count));
                }
                if (displayId) {
                    $("#" + displayId).text(count - $(this).val().length);
                }

            });
}


/*
Name:获取商品原图路径
Author:Maikou
DateTime:2011-01-04
*/
$.fn.ProductImageUrl = function (nostr, prefix2) {
    var url = this.selector;
    //    if (prefix2 && $.trim(prefix2) && url.indexOf(prefix2) < 0) {
    //        return url.replace("_mid", "_big");
    //    }
    //    else {
    if (nostr && $.trim(nostr) && url.indexOf(nostr) > -1)
        return url;
    var prefix = url.substring(0, url.lastIndexOf("/") + 1);
    var sufix = url.substring(url.lastIndexOf("/") + 1);
    var mid = sufix.substring(0, sufix.lastIndexOf("."));
    sufix = sufix.replace(mid, "");
    if (mid.indexOf("_small") > -1) {
        mid = mid.replace("_small", "");
    }
    return prefix + mid + sufix;
    //    }
}

/*
Name:获取商品缩略图路径
Author:Maikou
DateTime:2011-01-04
*/
jQuery.fn.ProductSmallImageUrl = function (nostr, prefix2) {
    var url = this.selector;
    //    if (prefix2 && $.trim(prefix2) && url.indexOf(prefix2) > -1) {
    //        return url.replace("_big", "_mid");
    //    }
    //    else {
    if (nostr && $.trim(nostr) && url.indexOf(nostr))
        return url;
    var prefix = url.substring(0, url.lastIndexOf("/") + 1);
    var sufix = url.substring(url.lastIndexOf("/") + 1);
    var mid = sufix.substring(0, sufix.lastIndexOf("."));
    sufix = sufix.replace(mid, "");
    if (mid.indexOf("_small") < 0) {
        mid += "_small";
    }
    return prefix + mid + sufix;
    //    }
}
