﻿var AhwaloLib = {};
AhwaloLib.Server = { Static: "http://static.chinayrs.com", Web: "http://www.chinayrs.com", My: "http://my.chinayrs.com" };
AhwaloLib.Validator = function(rules) {
    var rulesresult = { result: true, errortext: "", errortexts: [], errorobjects: [] }
    try {
        $.each(rules, function(selector, rule) {
            if ($(selector).length > 0) {
                var ruleresult = { result: true };
                $.each(rule, function(func, arg) {
                    var itemresult = false;
                    var value = $(selector).val();
                    switch (func) {
                        case "NotEmpty":
                            itemresult = AhwaloLib._ValidateLib.NotEmpty(value);
                            break;
                        default:
                            alert("undefind Rule (" + func + ")");
                            return false;
                            break;
                    }
                    if (!itemresult) {
                        ruleresult.result = false;
                        ruleresult.errortext = arg;
                        return false;
                    }
                });
                if (!ruleresult.result) {
                    rulesresult.result = false;
                    rulesresult.errortexts.push(ruleresult.errortext);
                    rulesresult.errorobjects.push({ selector: selector, errortext: ruleresult.errortext });
                }
            }
        });
    } catch (e) {
        rulesresult.result = false;
    }
    return rulesresult;
}
AhwaloLib._ValidateLib = {
    /* 验证成功时返回 true */
    NotEmpty: function(value) {
        return AhwaloLib._ValidateLib.Regex(value, "^.+$");
    },
    //    MaxLength: function(selector, maxlength, errortext) {
    //        return $(selector).RegexValid("^.{0," + length + "}$", errortext);
    //    },
    //    MinLength: function(selector, minlength, errortext) {
    //        return $(selector).RegexValid("^.{" + minlength + ",}$", errortext);
    //    },
    Regex: function(value, regex) {
        return (new RegExp(regex)).test(value);
    }
}

AhwaloLib.LoadLastView = function(id) {
    var Container = typeof (id) == "string" ? $(id) : id;
    $("ul", Container).load("/ajax/lastview", function() {
        $("ul > li:last", Container).css("border-bottom", "none");
        Container.show();
    })
}
AhwaloLib.ProductImageList = function(container, img) {
    var Container = typeof (container) == "string" ? $(container) : container;
    var Img = typeof (img) == "string" ? $(img) : img;

    $("a", Container).click(function() {
        Img.attr("src", $(this).attr("href"));
        $(this).siblings("a").removeClass("current");
        $(this).addClass("current");
        return false;
    });
    $("a:first", Container).click();
}

AhwaloLib.LoadConsultations = function(SaleId, Page) {
    Page = Page || 1;

    if ($("#consultations").length > 0) {
        $.ajax({
            url: "/ajax/consultations",
            cache:false,
            data: { SaleId: SaleId, p: Page },
            dataType: "html",
            type: "get",
            success: function(html) {
                $("#consultations").empty().html(html);
                $("#consultations > .pager > a").click(function() {
                    AhwaloLib.LoadConsultations(SaleId, $(this).attr("page"));
                    return false;
                });
            }
        });
    }
}

AhwaloLib.Buy = function(form) {

}