/// <reference path="jquery-1.10.2.min.js" />
var canvas, context;
var ids = 1;
$(function () {
    draw();
    $("#Web_Cv_Code").click(function () {
        context.clearRect(0, 0, 120, 40);
        draw();

    })

    $("#Web_link_btn").click(function (e) {
        e.preventDefault();
        var web_link_name = $.trim($("#Web_Link_Name").val());
        var web_link_link = $.trim($("#Web_Link_Link").val());
        var web_link_weight = $.trim($("#Web_Link_Weight").val());
        var web_link_code = $.trim($("#Web_Link_Txtcode").val());
        var pContent = $.trim($("#pContent").val());
        if (web_link_name == "") {
            alert("网站名称不能为空");
            return;
        }

        if (web_link_link == "") {

            alert("网站链接不能为空");
            return;
        }
        if (web_link_weight == "") {
            alert("权重不能为空");
            return;
        }
        if (web_link_code == "") {
            alert("验证码不能为空");
            return;
        } else if (web_link_code.toUpperCase() != $.cookie('logincode')) {
            alert("验证码不正确");
            return;

        }

        $.post("ajax/LinkBase.ashx", { "web_link_name": web_link_name, "web_link_link": web_link_link, 'web_link_weight': web_link_weight, "cmd": "linkajax", "pContent": pContent, "ids": ids}, function (data) {
            var data = JSON.parse(data);
            if (data.Success == true) {
                alert("提交成功，预计1-2工作日审核完毕，请耐心等待！！！")
                window.location.href = "/link.aspx";
            }  if (data.Success == false) {
                alert("提交失败，你已提交过，等待审核吧！！！")
            }


        })



    })



})





function draw() {
    $.cookie('logincode', null, { path: '/' });
    canvas = document.getElementById("Web_Cv_Code");
    context = canvas.getContext("2d");
    canvas.width = 120;
    canvas.height = 40;
    context.strokeStyle = "#009688";
    context.strokeRect(0, 0, 120, 40);
    var scode = "";
    var aCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,1,2,3,4,5,6,7,8,9";
    var aLength = aCode.split(",").length;
    for (var i = 0; i <= 3; i++) {
        var x = 20 + i * 20;
        var y = 20 + Math.random() * 10;
        var j = Math.floor(Math.random() * aLength);
        var deg = Math.random() * 90 * Math.PI / 180;//随机弧度
        txt = aCode.split(",")[j];
        scode += txt;
        context.fillStyle = randomRgbColor();
        context.font = "bold 20px 微软雅黑";
        //修改坐标原点和旋转角度
        context.translate(x, y);
        context.rotate(deg);
        context.fillText(txt, 0, 0);
        //恢复坐标原点和旋转角度
        context.rotate(-deg);
        context.translate(-x, -y);
    }
    $.cookie('logincode', scode, { path: '/' });
    //干扰线
    for (var i = 0; i < 8; i++) {
        context.strokeStyle = randomRgbColor();
        context.beginPath();
        context.moveTo(Math.random() * 120, Math.random() * 40);
        context.lineTo(Math.random() * 120, Math.random() * 40);
        context.stroke();
    }
    /**绘制干扰点**/
    for (var i = 0; i < 10; i++) {
        context.fillStyle = randomRgbColor();
        context.beginPath();
        context.arc(Math.random() * 120, Math.random() * 40, 1, 0, 2 * Math.PI);
        context.fill();
    }
}
function randomRgbColor() { //随机生成RGB颜色
    var r = Math.floor(Math.random() * 256); //随机生成256以内r值
    var g = Math.floor(Math.random() * 256); //随机生成256以内g值
    var b = Math.floor(Math.random() * 256); //随机生成256以内b值
    return "rgb(" + r + "," + g + "," + b + ")"; //返回rgb(r,g,b)格式颜色
}
