// JavaScript Document
/*
Content: Common components;
Remark: 验证,返回错误;
Version: v0.1;
Author: guanxin.com JF.Z;
Date: 2010-05-17
*/

var a = b = c = 1;
var A;
var B;
$.fn.clickreceive = function(obj, str, st) {
    //初始化
    $(st).hide();
	$(st + " span").hide();
    A = obj;
    B = st;
    //初始化是否文本框为空
    if ($(obj + "[name='txtGiftCode']").val().length != 0) {
        validate(obj, st, "txtGiftCode")
    }
    if ($(obj + "[name='txtPwd']").val().length != 0) {
        validate(obj, st, "txtPwd")
    }
    if ($(obj + "[name='txtValidCode']").val().length != 0) {
        validate(obj, st, "txtValidCode")
    }
    //设置获取焦点
    $(obj).focusin(function() {
        $(this).addClass("focusin")
		if($(this).attr("name") == "txtGiftCode"){
			$(st + ":eq(0) span").hide();
			$(st).eq(0).show();
			$(st + " strong").eq(0).css({"background-position":"99px 99px"});
			$(st + ":eq(0) span").eq(1).show();
		}
		if($(this).attr("name") == "txtPwd"){
			$(st + ":eq(1) span").hide();
			$(st).eq(1).show();
			$(st + " strong").eq(1).css({"background-position":"99px 99px"});
			$(st + ":eq(1) span").eq(1).show();
		}
		if($(this).attr("name") == "txtValidCode"){
			$(st + ":eq(2) span").hide();
			$(st).eq(2).show();
			$(st + " strong").eq(2).css({"background-position":"99px 99px"});
			$(st + ":eq(2) span").eq(1).show();
		}
    })
    //设置时区焦点
    $(obj).focusout(function() {
        $(this).removeClass("focusin")
        $(this).keyup()
    })
    //设置鼠标弹起
    $(obj).keyup(function() {
        validate(obj, st, $(this).attr("name"))
    })
    //设置提交
    $(str).click(function() {
        return loginreceive();

    })
}
function validate(obj, st, d) {
    if (d == "txtGiftCode") {
        $(obj + ":text").eq(0).val($(obj + ":text").eq(0).val().replace(/[^\d]/g, ""))
        if ($(obj + ":text").eq(0).val().length != 14) {
            userclick(st, 0, 1)
            a = 1;
        } else {
            userclick(st, 0, 0)
            a = 0;
           
        }
    }
    if (d == "txtPwd") {
        if ($(obj + ":password").eq(0).val().length != 6) {
            userclick(st, 1, 1)
            b = 1;
        } else {
            userclick(st, 1, 0)
            b = 0;
        }
    }
    if (d == "txtValidCode") {
        if ($(obj + ":text").eq(1).val().length != 4) {
            userclick(st, 2, 1)
            c = 1;
        } else {
            userclick(st, 2, 0)
            c = 0;
        }
    }
}
function loginreceive() {
    if (a != 0) {
        userclick(B, 0, 1)
        error("请输入正确的礼物领取码");
        return false;
    } else if (b != 0) {
        userclick(B, 1, 1)
        error("请输入有效的密码");
        return false;
    } else if (c != 0) {
        userclick(B, 2, 1)
        error("请输入有效的验证码");
        return false;
    } else {
        $(".error").remove();
        document.forms[0].submit();
    }
}
function error(obj) {
    var box = ".mainContent"
    var div = "<strong class='errorTB'>&nbsp;</strong>";
    var span = "<span>" + obj + "</span>";
    var erro = "<div class='recive-error '></div>";
    if ($(box + " .recive-error ").length != 0) {//防止重复增加
        $(".recive-error  span").html(obj);
    } else {
        $(box).append(erro);
        $(div + span).prependTo(".recive-error ");
    }
    $(".recive-error ").css({ "top": $(box).offset().top - 25 })
    $(".recive-error ").css({ "left": $(box).offset().left })
}

function userclick(str, num, obj) {
    if (obj == 1) {
		$(str + ":eq("+num+") span").hide();
        $(str).eq(num).show();
        $(str + " strong").eq(num).removeClass("mianCBCLSRight");
		$(str + " strong").eq(num).css({"background-position":"-28px 5px"});
        $(str + ":eq("+num+") span").eq(0).show();
    } else {
		$(str + ":eq("+num+") span").hide();
        $(str).eq(num).show();
        $(str + " strong").eq(num).addClass("mianCBCLSRight");
		$(str + " strong").eq(num).css({"background-position":"-31px -20px"});
        $(str + ":eq("+num+") span").eq(0).hide();
    }
}



//button
$.fn.button = function(obj, str) {
    $(".mianCBBBClose").click(function() {
        //window.opener = null;
        //window.open('','_parent','');
        //window.close();
    })
    //$(str).width($(obj).width()-3+"px")
    $(".mianCBBBFRight").height($(".mianCBBBFLeft").height())


    if ($(obj + " li").length >= 28) {
        $(str).width($(obj).width() - 43 + "px")
    } else if ($(obj + " li").length == 4) {
        $(str).width("130px")
    } else {
        $(str).width($(obj + " li").length / 4 * 129 - 35 + "px")
    }
}

//判断填写收礼信息
/**/
$(function() {
    $(".payButton").click(function() {
        if ($("#txtReciveName").val().length == 0) {
            error("您的姓名不能为空");
            $("#txtReciveName").focus();
            return false;
        }
        if ($("#province").val() == "") {
            error("请选择省、市、区");
            return false;
        }
        if ($("#txtAddress").val().length == 0) {
            error("详细地址不能为空");
            $("#txtAddress").focus();
            return false;
        }
        if ($("#txtZipcode").val().length == 0) {
            error("邮编不能为空");
            $("#txtZipcode").focus();
            return false;
        }
        else {
            if (!/^[0-9]\d{5}$/.test($("#txtZipcode").val())) {
                error("您的邮编填写错误");
                $("#txtZipcode").focus();
                return false;
            }
        }
        if ($("#txtMobile").val().length == 0) {
            error("手机不能为空");
            $("#txtMobile").focus();
            return false;
        }
        else {
            if (!/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test($("#txtMobile").val())) {
                error("您的手机格式填写错误");
                $("#txtMobile").focus();
                return false;
            }
        }
        if (($("#txtPhone1").val() + "-" + $("#txtPhone2").val()).length > 1) {
            if (!/^0(([1,2]\d)|([3-9]\d{2}))(-\d{7,8})$/.test($("#txtPhone1").val() + "-" + $("#txtPhone2").val())) {
                error("您的联系电话填写错误");
                $("#txtPhone2").focus();
                return false;
            }
        }
        if ($("#txtEmail").val().length > 0) {
            if (!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test($("#txtEmail").val())) {
                error("您的Email填写错误");
                $("#txtEmail").focus();
                return false;
            }
        }
    });
});

//收礼成功注册
$.fn.clickreg = function(obj, str, Error) {
    //初始化隐藏
    $(Error).hide();
    //获取焦点
    $(obj).focusin(function() {
        $(this).addClass("focusin")
    });
    //失去焦点
    $(obj).focusout(function() {
        $(this).removeClass("focusin")
        //if($(this).val().length != 0)
        if ($(this).attr("name") == "UserName") {
            clickregyz($(this).val(), 0, Error, obj)
        } else if ($(this).attr("name") == "UserEmail") {
            clickregyz($(this).val(), 1, Error, obj)
        } else if ($(this).attr("name") == "UserPass") {
            clickregyz($(this).val(), 2, Error, obj)
        } else if ($(this).attr("name") == "ConfirmPass") {
            clickregyz($(this).val(), 3, Error, obj)
        }
    });
    //键盘弹起
    $(obj).keyup(function() {
        //if($(this).val().length != 0)
        if ($(this).attr("name") == "UserName") {
            clickregyz($(this).val(), 0, Error, obj)
        } else if ($(this).attr("name") == "UserEmail") {
            clickregyz($(this).val(), 1, Error, obj)
        } else if ($(this).attr("name") == "UserPass") {
            clickregyz($(this).val(), 2, Error, obj)
        } else if ($(this).attr("name") == "ConfirmPass") {
            clickregyz($(this).val(), 3, Error, obj)
        }
    });
}
var cRega = cRegb = cRegc = cRegd = 1;
function clickregyz(obj, num, str, Input) {
    switch (num) {
        case 0: //用户名
            if (obj.length != 0 && obj.length >= 2 && obj.length <= 20) {
                userclick(str, num, 0);
                cRega = 0;
            } else {
                userclick(str, num, 1);
                cRega = 1;
            }
            break;
        case 1: //email
            if (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(obj) == false) {
                userclick(str, num, 1);
                cRegb = 1;
            } else {
                userclick(str, num, 0);
                cRegb = 0;
            }
            break;
        case 2: //第一次输入密码
            if ($(Input + "[name='ConfirmPass']").val().length != 0) {
                if ($(Input + "[name='UserPass']").val().length >= 6 && $(Input + "[name='UserPass']").val().length <= 16) {
                    userclick(str, num, 0);
                    clickregyz(obj, 3, str, Input);
                    cRegc = 0;
                } else {
                    userclick(str, num, 1);
                    cRegc = 1;
                }
            } else {
                if ($(Input + "[name='UserPass']").val().length >= 6 && $(Input + "[name='UserPass']").val().length <= 16) {
                    userclick(str, num, 0);
                    cRegc = 0;
                } else {
                    userclick(str, num, 1);
                    cRegc = 1;
                }
            }
            break;
        default: //第二次输入密码
            if ($(Input + "[name='ConfirmPass']").val() != $(Input + "[name='UserPass']").val() || $(Input + "[name='ConfirmPass']").val().length < 6 || $(Input + "[name='ConfirmPass']").val().length > 16) {
                userclick(str, num, 1);
                cRegd = 1;
            } else if ($(Input + "[name='ConfirmPass']").val().length == 0) {
                //userclick(str,num,1)
            } else {
                userclick(str, num, 0);
                cRegd = 0;
            }
    }
    if (cRega + cRegb + cRegc + cRegd == 0) {
        //alert("ok" + (cRega + cRegb + cRegc + cRegd));
        $(".mianCBCButton").click(function() { document.forms[0].submit(); });
    } else {
        //alert("no" + (cRega + cRegb + cRegc + cRegd));
        $(".mianCBCButton").click(function() { return false; }); 
   }
}

function closeWin() {
    var ua = navigator.userAgent
    var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false
    if (ie) {
        var IEversion = parseFloat(ua.substring(ua.indexOf("MSIE") + 5, ua.indexOf(";", ua.indexOf("MSIE"))))
        if (IEversion < 5.5) {
            var str = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">'
            str += '<param name="Command" value="Close"></object>';
            document.body.insertAdjacentHTML("beforeEnd", str);
            document.all.noTipClose.Click();
        }
        else {
            window.opener = null;
            window.close();
        }
    }
    else {
        window.close()
    }

}


//判断返回错误
$(function() {
    if ($(".field-validation-error").length != 0) {
        error($(".field-validation-error").text())
    }
})
