// JavaScript Document
// <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> show chinese in DW
// 动态创建一个form,可以提交该form
function UserForm(name){
    this.newWindowName = "newWin";
    this.features = null;

    // 成员变量
    this.name = name;
    this.action = "";

    this.method = "post";
    this.target = "";
    this.hiddenNameArray = new Array();
    this.hiddenValueArray = new Array();
    this.hiddenNumber = 0;
    
    // 方法
    this.setTarget = _setTarget;
    this.setAction = _setAction;
    this.setMethod = _setMethod;
    this.addHidden = _addHidden;
    this.submit = _submit;
    this.getDefaultFeatures = _getDefaultFeatures;
    this.setFeatures = _setFeatures;
    this.openURLWithoutParams = openURLWithoutParams;
}

// 添加form的action的值
function _setAction(action){
    this.action = action;
}

// 添加form对应的method
function _setMethod(method){
    this.method = method;
}

// 添加一个参数
function _addHidden(name, value){
    var currentIndex = this.hiddenNumber;
    this.hiddenNumber ++ ;
    this.hiddenNameArray[currentIndex] = name;
    this.hiddenValueArray[currentIndex] = value;
}

// 提交生成的form
function _submit(){
	var formTag = "<form name=" + this.name;
	if (this.method != null){
		formTag += " method=" + this.method;
	}
	formTag += ">";
    var formObj = document.createElement(formTag);
    if (this.action != undefined && this.action != "") {
        formObj.action = this.action;
    }
    if (this.target == "about:blank") {
        if (this.features == null) {
            this.features = this.getDefaultFeatures();
        }
        window.open("", this.newWindowName, this.features);
        formObj.target = this.newWindowName;
    } else {
		formObj.target = this.target;
	}
    for (var i=0; i<this.hiddenNumber; i++) {
        var hiddenObj = document.createElement("<input type=hidden name=" + this.hiddenNameArray[i] 
            + " value=" + this.hiddenValueArray[i] + ">");
        formObj.appendChild(hiddenObj);
    }
    document.body.appendChild(formObj);
	//alert(formObj.outerHTML);
    formObj.submit();
}

// 设置form的target
function _setTarget(target){
    this.target = target;
}

// 获取默认的新窗口属性
function _getDefaultFeatures(){
	var width  = screen.width-screen.width*0.2;
	var height = screen.height-screen.height*0.3;
	var top = screen.height*0.1;
	var left = screen.width*0.1;
    return ('width=' + width + ',height=' + height + ',top=' + top + ',left=' + left
            + ',toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=1,status=0');
}

// 设置新打开的窗口的属性
function _setFeatures(features){
    this.features = features;
}

// 打开一个链接,把参数隐藏起来
function openURLWithoutParams(url, target){
  var userForm = new UserForm("hiddenForm");
  var questionMarkIndex = url.indexOf("?");
  var param = null;
  var params = null;
  if (questionMarkIndex > 0) {
    param = url.substr(questionMarkIndex+1);
    url = url.substr(0, questionMarkIndex);
    params = param.split("&");
  }
  userForm.setAction(url);
  if (params != null) {
    for (var i=0; i<params.length; i++) {
      var keyValue = params[i].split("=");
      if (keyValue.length == 2) {
        userForm.addHidden(keyValue[0], keyValue[1]);
      } else if (keyValue.length == 1) {
        userForm.addHidden(keyValue[0], "");
      }
    }
  }
  if (target && target != "") {
	userForm.target = target;
  } else {
	userForm.target = "_blank";
  }
  userForm.submit();
}