//===========================================
//生成代码
//===========================================
function CreatCode(n){
    var bString = "0123456789";
    var cString = "0123456789abcdefABCDEF";
    var ii = 0;

    if(document.getElementById('size'+n).value.length < 2){
        alert("「字体大小」的值请填写2位数字！");
        document.getElementById('size'+n).focus();
        return false;
    }
  
    while(ii < document.getElementById('size'+n).value.length){
        if(bString.indexOf(document.getElementById('size'+n).value.substring(ii,ii+1)) == -1){
            alert("「字体大小」的值只能填写2位十进制数字！");
            document.getElementById('size'+n).focus();
            return false;
        }
        ii = ii+1;
    }

    if(document.getElementById('lineht'+n).value.length < 2){
        alert("「文字行高」的值请填写2位数字！");
        document.getElementById('lineht'+n).focus();
        return false;
    }

    ii = 0;
    while(ii < document.getElementById('lineht'+n).value.length){
        if(bString.indexOf(document.getElementById('lineht'+n).value.substring(ii,ii+1)) == -1){
            alert("「文字行高」的值只能填写2位十进制数字！");
            document.getElementById('lineht'+n).focus();
            return false;
        }
        ii = ii+1;
    }

    if(document.getElementById('lineht'+n).value < document.getElementById('size'+n).value){
        alert("「文字行高」不能小于「字体大小」的值！");
        document.getElementById('lineht'+n).focus();
        return false;
    }
    if(document.getElementById('color'+n).value.length < 6){
        alert("「文字颜色」的值请填写6位十六进制数字！");
        document.getElementById('color'+n).focus();
        return false;
    }

    ii = 0;
    while(ii < document.getElementById('color'+n).value.length){
        if(cString.indexOf(document.getElementById('color'+n).value.substring(ii,ii+1)) == -1){
            alert("「文字颜色」的值只能填写6位十六进制数字！");
            document.getElementById('color'+n).focus();
            return false;
        }
        ii = ii+1;
    }

    if(document.getElementById('bgcolor'+n).value.length < 6){
        alert("「背景颜色」的值请填写6位十六进制数字！");
        document.getElementById('bgcolor'+n).focus();
        return false;
    }

    if(document.getElementById('bgcolor'+n).value != "XXXXXX"){
        ii = 0;
        while(ii < document.getElementById('bgcolor'+n).value.length){
            if(cString.indexOf(document.getElementById('bgcolor'+n).value.substring(ii,ii+1)) == -1){
                alert("「背景颜色」的值只能填写6位十六进制数字！");
                document.getElementById('bgcolor'+n).focus();
                return false;
            }
            ii = ii+1;
        }
    }

    if(document.getElementById('bgcolor'+n).value == document.getElementById('color'+n).value){
        alert("「背景颜色」不能等于「文字颜色」的值！");
        document.getElementById('bgcolor'+n).focus();
        return false;
    }
    if(document.getElementById('type'+n).value == 0){
        alert("请选择IP显示方式！");
        document.getElementById('type'+n).focus();
        return false;
    }
    if(n == 1){
        var i = 1;
    }else{
        var i = 4;
    }
    document.getElementById('tex'+n).value = "<iframe width='"+80*i+"' height='"+((document.getElementById('lineht'+n).value)*n)+"' scrolling='no' frameborder='0' allowTransparency='true' src='"+SiteUrl+"/ShowIP.asp?size="+document.getElementById('size'+n).value+"&lineht="+document.getElementById('lineht'+n).value+"&color="+document.getElementById('color'+n).value+"&bgcolor="+document.getElementById('bgcolor'+n).value+"&type="+document.getElementById('type'+n).value+"'></iframe>";
}

//===========================================
//其他操作生成代码
//===========================================
function ReCreatCode(n){
    if(document.getElementById('tex'+n).value != ''){
        return CreatCode(n);
    }
}

//===========================================
//预览代码
//===========================================
function PreviewCode(n){
    if(document.getElementById('tex'+n).value == ''){
        alert('代码尚未生成，无法预览！');
        document.getElementById('creattex'+n).focus();
        return false;
    }
    if(CreatCode(n) != false){
        var BodyElement = ((document.compatMode.toLowerCase().indexOf('css') >= 0) ? (document.documentElement) : (document.body));  //判断是否符合W3C标准：如果符合返回值为document.documentElement 否则，返回值为document.body
        var WindowWidth = 520;
        var WindowHeight = 200;
        var WindowLeft = Math.floor(BodyElement.clientWidth / 2) - Math.floor(WindowWidth / 2);
        var WindowTop = Math.floor(BodyElement.clientHeight / 2) - Math.floor(WindowHeight / 8);

        var TypeUrl = SiteUrl+"/ShowIP.asp?size="+document.getElementById('size'+n).value+"&lineht="+document.getElementById('lineht'+n).value+"&color="+document.getElementById('color'+n).value+"&bgcolor="+document.getElementById('bgcolor'+n).value+"&type="+document.getElementById('type'+n).value;

        window.showModalDialog(TypeUrl, "", "dialogWidth:" + WindowWidth + "px; dialogHeight:" + WindowHeight + "px; dialogLeft:" + WindowLeft + "px; dialogTop:" + WindowTop + "px; resizable:yes; status:no; scroll:no; help:no;");
    }
}

//===========================================
//复制代码
//===========================================
function CopyCode(n){
    if(document.getElementById('tex'+n).value == ''){
        alert('代码尚未生成，无法复制！');
        document.getElementById('creattex'+n).focus();
        return false;
    }
    if(CreatCode(n) != false){
        document.getElementById('tex'+n).select();
        try{
            document.execCommand("copy");
        }catch(e){
            alert("提示：复制代码失败！\r\n\r\n原因：您的浏览器设置不允许进行剪切板操作！\r\n\r\n解决：请手动复制代码 或 更改您的浏览器设置 或 使用IE操作。");
            return false;
        }
        alert('复制成功，将代码粘贴到您网页中需要显示地区、城市的位置即可。');
    }
}

//===========================================
//清空代码
//===========================================
function ClearCode(n){
    document.getElementById('tex'+n).value = '';
}

//===========================================
//改变“背景颜色”文本框可编辑状态及其值
//===========================================
function ChangeValue(n){
    if(document.getElementById('bgtra'+n).checked == true){
        document.getElementById('bgcolor'+n).value = "XXXXXX";
        document.getElementById('bgcolor'+n).disabled = true;
    }else{
        document.getElementById('bgcolor'+n).disabled = false;
        document.getElementById('bgcolor'+n).value = "000000";
    }
}

//===========================================
//复选框状态变化
//===========================================
function ClickCheckBox(n){
    document.getElementById('bgtra'+n).checked = !(document.getElementById('bgtra'+n).checked);
    return ChangeValue(n);
}

//===========================================
//测试IP提交
//===========================================
function TestIP(str){
    document.getElementById('QueryText').value = str;
    document.Queryform.submit();
}
