var sNewFieldString = '<s'+'cript language="JavaScript">'
	+ '\n' + getObjByID.toString()
	+ '\n' + showFieldType.toString()
	+ '\n' + checkFieldType.toString()
	+ '\n' + checkInput.toString()
	+ '\n' +'function doNewField(o){'
	+ '\n' +'	var sSqlString = "";'
	+ '\n' +'	if(o.fieldName.value.length==0){alert("请输入字段名");o.fieldName.focus();return false;}'
	+ '\n' +'	if(!checkInput(o.fieldName.value)){alert("名称中包含非法字符，请重新输入");return false;}'
	+ '\n' +'	sSqlString = "ALTER TABLE ["+o.sTblName.value+"] \\n"'
	+ '\n' +'	+ "    ADD COLUMN ["+o.fieldName.value+"] "+o.fieldType[o.fieldType.selectedIndex].value'
	+ '\n' +'	+ (o.fieldSize.value?"("+o.fieldSize.value+")":"") + " "'
	+ '\n' +'	+ (o.isNULL.checked?"NULL":"NOT NULL") + " \\n"'
	+ '\n' +'	+ (o.fieldPK.checked?"PRIMARY KEY":"") + " "'
	+ '\n' +'	+ (parseInt(o.fieldSeed.value)&&parseInt(o.fieldIncre.value)?"IDENTITY ("+o.fieldSeed.value+","+o.fieldIncre.value+")":"") + " \\n"'
	+ '\n' +'	+ (o.fieldDefault.value?"DEFAULT "+o.fieldDefault.value+" WITH VALUES":"");'
	+ '\n' +'	var oFormSQL = window.opener.getObjByID("formSQL");'
	+ '\n' +'	oFormSQL.sSqlString.value = sSqlString;'
	+ '\n' +'	oFormSQL.sOper.value = "Design";'
	+ '\n' +'	if(!confirm("确认要添加新列吗？")) return false;'
	+ '\n' +'	oFormSQL.submit();'
	+ '\n' +'}'
	+ '\n' +'</scrip'+'t>'
	+ '\n' +'<table width="500" border="1" cellspacing="0" cellpadding="0">'
	+ '\n' +'  <form name="formAddFld" id="formAddFld" method="post" onSubmit="doNewField(this);return false;">'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td colspan="2">为表 [table name] 添加新字段</td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td width="30%">字段名</td>'
	+ '\n' +'    <td><input name="fieldName" type="text" id="fieldName" size="30"></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td>类型</td>'
	+ '\n' +'    <td>'
	+ '\n' +'<select id="fieldType" name="fieldType" onChange="checkFieldType(this.value,this[this.selectedIndex].sLength)" onBlur="showFieldType(0)" onFocus="window.currElement=this;" style="width:130">'
	+ '\n' +'<OPTION value=TinyInt sLength=1>TinyInt'
	+ '\n' +'<OPTION value=SmallInt sLength=1>SmallInt'
	+ '\n' +'<OPTION value=Int sLength=1>Int'
	+ '\n' +'<OPTION value=BigInt sLength=1>BigInt'
	+ '\n' +'<OPTION value=Real sLength=1>Real / Single'
	+ '\n' +'<OPTION value=Float sLength=1>Float / Double'
	+ '\n' +'<OPTION value=Money sLength=1>Money'
	+ '\n' +'<OPTION value=Decimal sLength=1>Decimal'
	+ '\n' +'<OPTION value=Numeric sLength=1>Numeric'
	+ '\n' +'<OPTION value=Bit sLength=1>Bit'
	+ '\n' +'<OPTION value=Variant>Variant'
	+ '\n' +'<OPTION value=UniqueIdentifier sLength=1>UniqueIdentifier'
	+ '\n' +'<OPTION value=DateTime sLength=1>DateTime'
	+ '\n' +'<OPTION value=Char selected>Char'
	+ '\n' +'<OPTION value=VarChar>VarChar'
	+ '\n' +'<OPTION value=Text sLength=1>Text'
	+ '\n' +'<OPTION value=nChar>nChar'
	+ '\n' +'<OPTION value=nVarChar>nVarChar'
	+ '\n' +'<OPTION value=nText sLength=1>nText'
	+ '\n' +'<OPTION value=TimeStamp sLength=1>TimeStamp'
	+ '\n' +'<OPTION value=VarBinary sLength=1>VarBinary'
	+ '\n' +'<OPTION value=Image sLength=1>Image'
	+ '\n' +'</select>'
	+ '\n' +'	</td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td>宽度</td>'
	+ '\n' +'    <td><input name="fieldSize" type="text" id="fieldSize"  value="50" size="30"></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td>允许空</td>'
	+ '\n' +'    <td><input name="isNULL" type="checkbox" id="isNULL" value="checkbox" checked></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td>主键</td>'
	+ '\n' +'    <td><input name="fieldPK" type="checkbox" id="fieldPK" value="PRIMARY KEY"></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td>递增</td>'
	+ '\n' +'    <td>种子数 <input name="fieldSeed" type="text" id="fieldSeed" size="5">'
	+ '\n' +'      递增量 '
	+ '\n' +'      <input name="fieldIncre" type="text" id="fieldIncre" size="5"></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr> '
	+ '\n' +'    <td>默认值</td>'
	+ '\n' +'    <td><input name="fieldDefault" type="text" id="fieldName4" size="30"></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  <tr>'
	+ '\n' +'    <td>&nbsp;</td>'
	+ '\n' +'    <td align="right">'
	+ '\n' +'	<input type="hidden" name="sTblName" value="[table name]">'
	+ '\n' +'	<input type="submit" name="Submit2" value="确定">'
	+ '\n' +'      <input name="Reset" type="reset" id="Reset" value="关闭" onClick="window.close();"></td>'
	+ '\n' +'  </tr>'
	+ '\n' +'  </form>'
	+ '\n' +'</table>';

var CCNS_program = "COCOON  DB Tools";
var CCNS_version = "Final Release";
function getObjByID(n) {

//This function was re-written form DreamWeaver v4.01 by Sunrise_Chen.
	var p,i,x;
	var d=document; 
	if(!(x=d[n])&&d.all) x=d.all[n];
	for(i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObjByID(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); 
	return x;
}

function doClick(o){
	var sObjName, sObjType, sSqlString;
	var oFormSql = getObjByID("formSQL");
	for(var i=0;i<o.form.objName.length;i++){
		if(o.form.objName[i].checked){
			sObjName = o.form.objName[i].objName;
			sObjType = o.form.objName[i].objType;
			break;
		}
	}
	switch(o.value){
	case "查看":
		if(!sObjName){alert("请选择对象"); return false;}
		sSqlString = "SELECT * FROM [" + sObjName + "]";
		oFormSql.sTblName.value = sObjName;
		oFormSql.sTblType.value = sObjType;
		oFormSql.sSqlString.value = sSqlString;
		oFormSql.sOper.value = "Select";
		oFormSql.submit();
		break;
	case "删除":
		if(!sObjName){alert("请选择对象"); return false;}
		switch(sObjType){
		case "TABLE" :
			sSqlString = "DROP TABLE [" + sObjName + "]";
			break;
		case "VIEW" :
			sSqlString = "DROP VIEW [" + sObjName + "]";
			break;
		}
		oFormSql.sSqlString.value = sSqlString;
		if(!confirm("你确认要删除 "+sObjName+' 对象吗？')) break;
		oFormSql.sTblName.value = "";
		oFormSql.sTblType.value = "";
		oFormSql.sOper.value = "";
		oFormSql.submit();
		break;
	case "新建表":
		var sObjName = prompt("请输入表名:",'');
		while(!checkInput(sObjName)){alert('名称中包含非法字符，请重新输入');sObjName = prompt("请输入表名",'');}
		if(sObjName){
			sSqlString   = "CREATE TABLE ["+sObjName+"] ( \n"
						 + "ID int IDENTITY(1,1) PRIMARY KEY CLUSTERED \n"
						 + ")"
			oFormSql.sTblName.value = sObjName;
			oFormSql.sTblType.value = "TABLE";
			oFormSql.sSqlString.value = sSqlString;
			if(!confirm("你确认要新建 "+sObjName+' 表吗？')) break;
			oFormSql.submit();
		}
		break;
	case "设计":
		if(!sObjName){alert("请选择对象"); return false;}
		sSqlString = "SELECT * FROM [" + sObjName + "]";
		oFormSql.sTblName.value = sObjName;
		oFormSql.sTblType.value = sObjType;
		//oFormSql.sSqlString.value = sSqlString;
		oFormSql.sSqlString.value = "";
		oFormSql.sOper.value = "Design";
		oFormSql.submit();
		break;
	case "显示所有对象" :
		oFormSql.sOper.value = "";
		oFormSql.sSqlString.value = "";
		oFormSql.sTblName.value = "";
		oFormSql.sTblType.value = "";
		oFormSql.submit();
		break;
	case "清空" :
		if(sObjType!="TABLE"){alert("你不能清空该对象。");break;}
		oFormSql.sOper.value = "";
		oFormSql.sSqlString.value = "DELETE * FROM ["+sObjName+"]";
		oFormSql.sTblName.value = sObjName;
		oFormSql.sTblType.value = sObjType;
		if(!confirm("你确认要清空 "+sObjName+' 表吗？')) break;
		oFormSql.submit();
		break;
	case "新建查询" :
		sTblName = prompt("请输入查询名称：","")
		while(!checkInput(sTblName)){alert('名称中包含非法字符，请重新输入');sTblName = prompt("请输入表名",'');}
		if(!sTblName) return false;
		sSqlString = "CREATE VIEW ["+sTblName+"] \nAS \n" 
		oFormSql.sSqlString.value = sSqlString;
		alert("请在SQL框里将语句填写完整。");
		oFormSql.sSqlString.focus();
	}
}

function showFieldType(o){
	var oDivType = getObjByID("divFieldType");
	var oFieldType = getObjByID("selFieldType");
	if(!o){if(oDivType) oDivType.style.display='none'; return;}
	if(!oFieldType) return;
	window.currElement = o;
	oFieldType.value = o.value;
	oDivType.style.top = event.y + document.body.scrollTop - event.offsetY -4 ;
	oDivType.style.left = event.x + document.body.scrollLeft - event.offsetX -4 ;
	oDivType.style.display="";
	oFieldType.focus();
}

function checkFieldType(s,i){
	if(window.currElement){
		window.currElement.value = s;
		if(i) window.currElement.form.fieldSize.value = "";
	}
}

function showDetial(o){
	if(!o) return;
	var iSW = parseInt(screen.AvailWidth);
	var iSH = parseInt(screen.AvailHeight);
	iW = 300;
	iH = 100;
	var newWin = open('about:blank','','width='+iW+',height='+iH+',top='+((iSH/2)-(iH/2))+',left='+((iSW/2)-(iW/2)));
	newWin.document.title = CCNS_program + " - Detial Page　　　　　　　　";
	newWin.document.body.innerHTML = o.title;
}

function doOper(o,sTblName){
	var iSW = parseInt(screen.AvailWidth);
	var iSH = parseInt(screen.AvailHeight);
	iW = 520;
	iH = 260;
	var oFormSql = getObjByID("formSQL");
	var sSqlString = "";
	switch(o.value){
	case "新增" :
	var newWin = open('about:blank','','width='+iW+',height='+iH+',top='+((iSH/2)-(iH/2))+',left='+((iSW/2)-(iW/2)));
		newWin.document.write(sNewFieldString.replace(/\[table name\]/g,sTblName));
		newWin.document.title = CCNS_program + " - New Field　　　　　　　　";
		break;
	case "删除" : 
		sSqlString = "ALTER TABLE [" + sTblName + "] \n"
			+ "    DROP COLUMN [" + o.form.fieldName.value + "]";
		oFormSql.sSqlString.value = sSqlString;
		if(!confirm("确认要删除“"+o.form.fieldName.value+"”列吗？")) return false;
		oFormSql.sOper.value = "Design";
		oFormSql.submit();
		break;
	case "修改" : 
		sSqlString = "ALTER TABLE [" + sTblName + "] \n"
			+ "    ALTER COLUMN [" + o.form.fieldName.value + "] "
			+ o.form.fieldType.value + " "
			+ (o.form.fieldSize.value.length?"("+o.form.fieldSize.value+")":"") + " "
			+ (o.form.fieldNull.checked?"NULL":"NOT NULL");
		oFormSql.sSqlString.value = sSqlString;
		if(!confirm("确认要修改“"+o.form.fieldName.value+"”列吗？")) return false;
		oFormSql.sOper.value = "Design";
		oFormSql.submit();
		break;
	case "新记录" :
		iW = 600;
		iH = 480;
		var newWin = open('about:blank','','scrollbars=yes,width='+iW+',height='+iH+',top='+((iSH/2)-(iH/2))+',left='+((iSW/2)-(iW/2)));
		newWin.document.write(sNewRecString);
		newWin.document.title = CCNS_program + " - New Record　　　　　　　　";
		break;
	case "删" :
		if(o.tblName.length<1||o.sCondition.length<1){alert("条件不正确，不能删除。");return false;}
		sSqlString = "DELETE * FROM ["+o.tblName+"] WHERE "+o.sCondition;
		oFormSql.sSqlString.value = sSqlString;
		if(!confirm("确认要删除吗？")) return false;
		oFormSql.submit();
		break;
	}
}

function changePage(o,i){
	var oFormSql = getObjByID("formSQL");
	switch(i){
	case 0 :
		oFormSql.PageNo.value = o.form.gotoPage.value;
		break;
	case 1 :
		oFormSql.PageNo.value = 1;
		break;
	case 2 :
		oFormSql.PageNo.value = parseInt(o.form.PageNo.value)-1;
		break;
	case 3 :
		oFormSql.PageNo.value = parseInt(o.form.PageNo.value)+1;
		break;
	case 4 :
		oFormSql.PageNo.value = 9999;
		break;
	}
	oFormSql.submit();
}

function doNewRec(o){
	var sSqlString = "INSERT INTO ["+o.tblName.value+"] ("
	for(var i=0;i<o.length;i++){
		if(o[i].type!="hidden"&&o[i].type!="submit"){
			sSqlString += "[" + o[i].name + "], "
		}
	}
	sSqlString = sSqlString.substr(0,sSqlString.length-2) + ") VALUES("
	for(var i=0;i<o.length;i++){
		if(o[i].type!="hidden"&&o[i].type!="submit"){
			sSqlString += "'" + o[i].value + "', ";
		}
	}
	sSqlString = sSqlString.substr(0,sSqlString.length-2) + ") "
	var oFormSQL = window.opener.getObjByID("formSQL");
	oFormSQL.sSqlString.value = sSqlString;
	if(!confirm("确认提交吗？")) return false;
	oFormSQL.submit();
}

function clearSubmitForm(o){
	var oFormSQL = getObjByID("formSQL");
	oFormSQL.sTblName.value = "";
	oFormSQL.sTblType.value = "";
	oFormSQL.sOper.value = "";
}

function doSelField(o){
	var bSelect = false;
	var oFormSQL = getObjByID("formSQL");
	var oForm = o;
	var sSqlString = "SELECT "
	for(var i=0;i<oForm.fieldName.length;i++){
		if(oForm.fieldName[i].checked){
			sSqlString += "[" + oForm.fieldName[i].value + "],";
			bSelect = true;
		}
	}
	sSqlString = sSqlString.substr(0,sSqlString.length-1);
	if(!bSelect) sSqlString += " *";
	sSqlString += (" FROM " + "[" + oForm.sTblName.value + "] ");
	if(oForm.orderField.selectedIndex>0){
		sSqlString += "ORDER BY [" + oForm.orderField[oForm.orderField.selectedIndex].value + "]";
		sSqlString += " " + oForm.orderBy[oForm.orderBy.selectedIndex].value;
	}
	oFormSQL.sSqlString.value = sSqlString;
	oFormSQL.submit();
}

function checkInput(s){
	if(s.indexOf('"')>=0||s.indexOf(' ')>=0) 
		return false; 
	return true;
}