var obj;
function CheckSetting(){
var connectionname=document.getElementsByName("connectionname");
var connectionmaxpoolsize=document.getElementsByName("connectionmaxpoolsize");
var connectionstring=document.getElementsByName("connectionstring");
for(var i=0;i<=connectionname.length-1;i++){
if(connectionname[i].value==""){
ShowMessageDialog("提示","数据库连接配置的名称不能为空");
return;
}
if(connectionmaxpoolsize[i].value==""){
ShowMessageDialog("提示","数据库连接配置的最大连接数不能为空");
return;
}
if(connectionstring[i].value==""){
ShowMessageDialog("提示","数据库连接配置的连接字符串不能为空");
return;
}
}

document.setting.uploadmaximagefilesize.value=document.setting.uploadmaximagefilesize.value.trim();
if(document.setting.uploadmaximagefilesize.value==""){
ShowMessageDialog("提示","上传的图片最大几K 不能为空");
return false;
}
if(IsInt(document.setting.uploadmaximagefilesize.value)==false){
if(IsFloat(document.setting.uploadmaximagefilesize.value)==false){
ShowMessageDialog("提示","上传的图片最大几K 格式错误,必须填写数值");
return false;
}
}

document.setting.uploadmaxmediafilesize.value=document.setting.uploadmaxmediafilesize.value.trim();
if(document.setting.uploadmaxmediafilesize.value==""){
ShowMessageDialog("提示","上传的音视频最大几K 不能为空");
return false;
}
if(IsInt(document.setting.uploadmaxmediafilesize.value)==false){
if(IsFloat(document.setting.uploadmaxmediafilesize.value)==false){
ShowMessageDialog("提示","上传的音视频最大几K 格式错误,必须填写数值");
return false;
}
}

document.setting.uploadmaxotherfilesize.value=document.setting.uploadmaxotherfilesize.value.trim();
if(document.setting.uploadmaxotherfilesize.value==""){
ShowMessageDialog("提示","上传的其它文件最大几K 不能为空");
return false;
}
if(IsInt(document.setting.uploadmaxotherfilesize.value)==false){
if(IsFloat(document.setting.uploadmaxotherfilesize.value)==false){
ShowMessageDialog("提示","上传的其它文件最大几K 格式错误,必须填写数值");
return false;
}
}

document.setting.defaultmaximagewidth.value=document.setting.defaultmaximagewidth.value.trim();
if(document.setting.defaultmaximagewidth.value==""){
ShowMessageDialog("提示","封面图片默认压缩宽度 不能为空");
return false;
}
if(IsInt(document.setting.defaultmaximagewidth.value)==false){
ShowMessageDialog("提示","封面图片默认压缩宽度 格式错误,必须填写整数");
return false;
}

document.setting.defaultmaximageheight.value=document.setting.defaultmaximageheight.value.trim();
if(document.setting.defaultmaximageheight.value==""){
ShowMessageDialog("提示","封面图片默认压缩高度 不能为空");
return false;
}
if(IsInt(document.setting.defaultmaximageheight.value)==false){
ShowMessageDialog("提示","封面图片默认压缩高度 格式错误,必须填写整数");
return false;
}
}

function ListAdd(){
var i=$("connections").rows.length-1;
if(i<0){
i=0;	
}
var temp=document.createElement("tr");
var td=document.createElement("td"); 
td.innerHTML="<button onclick=\"ListRemove(this)\">移除配置</button>";
temp.appendChild(td);
td=document.createElement("td"); 
td.innerHTML="<input id=\"connectionname" + i + "\" name=\"connectionname\" maxlength=\"30\" value=\"\" style=\"width:150px\"/>";
temp.appendChild(td);
td=document.createElement("td"); 
td.innerHTML="<input id=\"connectionmaxpoolsize" + i + "\" name=\"connectionmaxpoolsize\" maxlength=\"5\" value=\"100\" style=\"width:60px\"/>";
temp.appendChild(td);
td=document.createElement("td"); 
td.innerHTML="<select id=\"connectiontype" + i + "\" name=\"connectiontype\" style=\"width:100px;\"><option value=\"excel\" selected>Excel</option><option value=\"access\">Access</option><option value=\"sqlserver\">SqlServer</option><option value=\"mysql\">MySql</option><option value=\"oracle\">Oracle</option></select>";
temp.appendChild(td);
td=document.createElement("td"); 
td.innerHTML="<input id=\"connectionstring" + i + "\" name=\"connectionstring\" maxlength=\"500\" value=\"\" style=\"width:99%;\"/>";
temp.appendChild(td);
td=document.createElement("td"); 
td.innerHTML="<button type=\"button\" onclick=\"SetConnectionString(" + i + ")\">生成连接字符串</button>";
temp.appendChild(td);
td=document.createElement("td"); 
td.innerHTML="<button type=\"button\" onclick=\"TestConnection("+i+")\">测试</button>";
temp.appendChild(td);
$("connections").childNodes[1].appendChild(temp);
}

function ListRemove(obj){
isSubmitTwo=true;
obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);
}

function FileSelected(path){
EasyMask.close();
type=document.setting.elements["connectiontype"+connectioni].value;
if(type=="access"){
SetAccessSql("{$physicalapplicationpath}"+path.replace(/\//g,"\\"));
}
else if(type=="excel"){
SetExcelSql("{$physicalapplicationpath}"+path.replace(/\//g,"\\"));
}
}

var connectioni;
function SetConnectionString(i){
connectioni=i;
var type=$("connectiontype"+i).value;
if(type=="access"){
var content="<iframe src=\"selectfile.ashx?rootpath=" + escape("../") + "&extension=.mdb\" frameborder=\"0\" scrolling=\"no\" width=\"100%\" height=\"100%\"></iframe>";
EasyMask.init({ width: 700, height: 400, padding:"0", title: '选取access数据库文件(后缀名.mdb)', detail: content, padding:'0'});
}
else if(type=="excel"){
var content="<iframe src=\"selectfile.ashx?rootpath=" + escape("../") + "&extension=.xls\" frameborder=\"0\" scrolling=\"no\" width=\"100%\" height=\"100%\"></iframe>";
EasyMask.init({ width:700, height: 400, padding:"0", title: '选取excel数据库文件(后缀名.xls)', detail: content,padding:'0'}); 
}
else if(type=="sqlserver"){
SetSqlServer()
}
else if(type=="oracle"){
SetOracle()     
}
else if(type=="mysql"){
SetMySql()     
}
}

function SetAccessSql(path){
var content="<table class='attribtes'><tr><th width='120'>属性&nbsp;</th><th width='285'>属性值</th><th>&nbsp;提示</th></tr>";
content+="<tr><td>数据库路径</td><td><input id='database' value='"+path+"'/></td><td></td></tr>";
content+="<tr><td>密码</td><td><input id='password'/></td><td></td></tr>";
content+="<tr><td align=\"right\" colspan=\"3\"><button type=\"button\" id=\"yes\" onclick=\"SetAccessSqlOK();\">确定</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button></td></tr></table>";
EasyMask.init({ width: 650, height: 162, title: '生成Access数据库连接字符串', detail: content });
$("database").focus(); 
}

function SetAccessSqlOK(){
var database=$("database").value;
var password=$("password").value;
if(database!=""){
EasyMask.close();
var connectionstring="";
if(password!=""){
connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database+";Jet OLEDB:Database Password="+password+";";
}
else{
connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database+";";
}
$("connectionstring"+connectioni).value=connectionstring;
}
}

function SetExcelSql(path){
var content="<table class='attribtes'><tr><th width='120'>属性&nbsp;</th><th width='285'>属性值</th><th>&nbsp;提示</th></tr>";
content+="<tr><td>数据库路径</td><td><input id='database' value='"+path+"'/></td><td></td></tr>";
content+="<tr><td align=\"right\" colspan=\"3\"><button type=\"button\" id=\"yes\" onclick=\"SetExcelSqlOK();\">确定</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button></td></tr></table>";
EasyMask.init({ width:586, height: 130, title: '生成Excel数据库连接字符串', detail: content });
$("database").focus(); 
}

function SetExcelSqlOK(){
var database=$("database").value;
if(database!=""){
EasyMask.close();
var connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database+";Extended Properties='Excel 8.0;IMEX=2';";
$("connectionstring"+connectioni).value=connectionstring;
}
}

function SetSqlServer(){
var content="<table class='attribtes'><tr><th width='120'>属性&nbsp;</th><th width='285'>属性值</th><th>&nbsp;提示</th></tr>";
content+="<tr><td>服务器名称</td><td><input id='server' value='localhost'/></td><td></td></tr>";
content+="<tr><td>数据库名</td><td><input id='database'/></td><td></td></tr>";
content+="<tr><td>登录名</td><td><input id='userid' value='sa'/></td><td></td></tr>";
content+="<tr><td>密码</td><td><input id='password' value='sa'/></td><td></td></tr>";
content+="<tr><td align=\"right\" colspan=\"3\"><button type=\"button\" id=\"yes\" onclick=\"SetSqlServerOK();\">确定</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button></td></tr></table>";
EasyMask.init({ width: 650, height: 226, title: '生成SqlServer数据库连接字符串', detail: content });
$("server").focus(); 
}

function SetSqlServerOK(){
var server=$("server").value;
var database=$("database").value;
var userid=$("userid").value;
var password=$("password").value;
if(server!=""){
EasyMask.close();
var connectionstring="server="+server+";";
if(database!=""){
connectionstring+="database="+database+";";
}

if(userid==""||password==""){
connectionstring+="integrated security=true;";
}
else{
connectionstring+="uid="+userid+";pwd="+password+";";
}
$("connectionstring"+connectioni).value=connectionstring;
}
}

function SetMySql(){
var content="<table class='attribtes'><tr><th width='120'>属性&nbsp;</th><th width='285'>属性值</th><th>&nbsp;提示</th></tr>";
content+="<tr><td>主机名 / IP地址</td><td><input id='server' value='localhost'/></td><td></td></tr>";
content+="<tr><td>端口</td><td><input id='port' value='3306'/></td><td></td></tr>";
content+="<tr><td>数据库名</td><td><input id='database'/></td><td></td></tr>";
content+="<tr><td>用户名</td><td><input id='userid' value='root'/></td><td></td></tr>";
content+="<tr><td>密码</td><td><input id='password'/></td><td></td></tr>";
content+="<tr><td align=\"right\" colspan=\"3\"><button type=\"button\" id=\"yes\" onclick=\"SetMySqlOK();\">确定</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button></td></tr></table>";
EasyMask.init({ width: 650, height: 258, title: '生成MySql数据库连接字符串', detail: content });
$("server").focus(); 
}

function SetMySqlOK(){
var server=$("server").value;
var port=$("port").value;
var database=$("database").value;
var userid=$("userid").value;
var password=$("password").value;
if(server!=""&&database!=""&&userid!=""){
EasyMask.close();
var connectionstring="server="+server+";port="+port+";database="+database+";uid="+userid+";pwd="+password+";";
$("connectionstring"+connectioni).value=connectionstring;
}
}

function SetOracle(){
var content="<table class='attribtes'><tr><th width='120'>属性&nbsp;</th><th width='285'>属性值</th><th>&nbsp;提示</th></tr>";
content+="<tr><td>主机名 / IP地址</td><td><input id='server' value='localhost'/></td><td></td></tr>";
content+="<tr><td>端口</td><td><input id='port' value='1521'/></td><td></td></tr>";
content+="<tr><td>服务名</td><td><input id='database' value='orcl'/></td><td></td></tr>";
content+="<tr><td>用户名</td><td><input id='userid' value='system'/></td><td></td></tr>";
content+="<tr><td>密码</td><td><input id='password' value='manager'/></td><td></td></tr>";
content+="<tr><td align=\"right\" colspan=\"3\"><button type=\"button\" id=\"yes\" onclick=\"SetOracleOK();\">确定</button><button type=\"button\" onclick=\"EasyMask.close();\">取消</button></td></tr></table>";
EasyMask.init({ width: 650, height: 258, title: '生成Oracle数据库连接字符串', detail: content });
$("server").focus(); 
}

function SetOracleOK(){
var server=$("server").value;
var port=$("port").value;
var database=$("database").value;
var userid=$("userid").value;
var password=$("password").value;
if(server!=""){
EasyMask.close();
var connectionstring="data source=(description=(address_list=(address=(protocol=tcp)(host="+server+")(port="+port+")))(connect_data=(service_name="+database+")));persist security info=true;user id="+userid+";password="+password+";";
$("connectionstring"+connectioni).value=connectionstring;
}
}

function TestConnection(i){
var connectiontype=$("connectiontype"+i).value;
var connectionstring=$("connectionstring"+i).value;
if(connectionstring==""){
ShowMessageDialog("提示","数据库连接配置的连接字符串不能为空");
return;
}
AjaxRequest("?_action=testconnection&connectiontype="+connectiontype+"&connectionstring="+escape(connectionstring),"",TestConnectionReturn,null);
}

function TestConnectionReturn(r){
if(r.substring(0,1)==' '){
ShowMessageDialog("测试结果",r);
}
else{
ShowMessageDialog("提示",r);
}
}