/***
 * Index Javascript Functions
 * Powered By YAO Yong(dnysea@yahoo.com)
 ***/

var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function openWindow(uri) {
  w = 430;
  h = 460;
  myWindow = window.open(uri, 'newWindow', 'scrollbars=1,resizable=1,toolbar=0,menubar=0,width='+w+',height='+h);
  myWindow.moveTo(100, 100);
  return true;
}

function openWindowX(uri) {
  w = 800;
  h = 600;
  myWindow = window.open(uri, 'newWindow', 'scrollbars=1,resizable=1,toolbar=0,menubar=0,width='+w+',height='+h);
  myWindow.moveTo(100, 100);
  return true;
}

function openImgWindow(uri) {
  w = 400;
  h = 450;
  myImgWindow = window.open(uri, 'newImgWindow', 'resizable=1,toolbar=0,menubar=0,width='+w+',height='+h);
  myImgWindow.moveTo(100, 100);
  return true;
}

function XMLgetContent(uri) {
	var URI;
	URI = uri;
	if (window.XMLHttpRequest) {
		isIE = false;
		xml = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		isIE = true;
		xml = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return false;
	}
	try {
		xml.open("GET", URI, false);
		xml.send(null);
	}
	catch(ex){}
	return xml.responseText;
}

function XMLgetHTTP(uri) {
	return XMLgetContent(uri);
}

function XMLpostForm(uri, formObj) {
	if (window.XMLHttpRequest) {
		xml = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xml = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return false;
	}
	try {
		var sendData = formObj.elements[0].name + "=" + encodeURI(formObj.elements[0].value);
		for ( var i = 1; i < obj.length; i ++ ) {
			sendData += "&" + formObj.elements[i].name + "=" + encodeURI(formObj.elements[i].value);
		}

		xml.open("POST", uri, true);
		xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xml.send(sendData);
	}
	catch(ex) {}
}

function $(objName) {
	if ( document.getElementById ) {
		return document.getElementById(objName);
	} else if ( document.layers ) {
		return eval("document.layers['" + objName + "']");
	} else {
		return eval("document.all." + objName);
	}
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

/* trim() function like PHP does */
String.prototype.trim = function () {
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/* select/option functions */
function getSelectValue(obj_id) {
  var obj = document.getElementById(obj_id);
  var i = obj.selectedIndex;
  if (i > -1) {
    return obj.options[i].value;
  } else {
    return "";
  }
}

/* the `obj_name` MUST be unique */
function getSelectValueByName(obj_name) {
  var obj = document.getElementsByName(obj_name)[0];
  var i = obj.selectedIndex;
  if (i > -1) {
    return obj.options[i].value;
  } else {
    return "";
  }
}

function setSelectedOptionByValue(obj_id, val) {
  var obj = document.getElementById(obj_id);
  var i;
  for (i = 0; i < obj.options.length; i ++) {
    if (val == obj.options[i].value) {
      obj.options[i].selected = true;
    }
  }
  return true;
}

function addSelectOption(selObjId, sName, sValue) {
  var optionObj = document.createElement("option");
  optionObj.appendChild(document.createTextNode(sName));

  if (arguments.length == 3) {
    optionObj.setAttribute("value", sValue);
  }

  document.getElementById(selObjId).appendChild(optionObj);
  return true;
}

function removeSelectLastOption(selObjId) {
  var selObj = document.getElementById(selObjId);
  var len = selObj.options.length;
  if (len > 0) {
    selObj.remove(len - 1);
  }
  return true;
}

function removeSelectAllOptions(selObjId) {
  var selObj = document.getElementById(selObjId);
  var len = selObj.options.length;
  var i;
  for (i = (len - 1); i >= 0; i--) {
    selObj.remove(i);
  }
  return true;
}

function initSelectOptions(selObjId) {
  removeSelectAllOptions(selObjId);
  addSelectOption(selObjId, "---", "");
}

function sysAddOption(full_obj_id, curr_obj_id) {
  var objFull = document.getElementById(full_obj_id);
  var objCurr = document.getElementById(curr_obj_id);
  var full_len = objFull.options.lenght;
  var i, j;
  i = objFull.selectedIndex;
  if (i > -1) {
    for (j = 0; j < objCurr.options.length; j++) {
      if (objCurr.options[j].value == objFull.options[i].value) {
        alert("不能重复添加");
        return false;
      }
    }
    addSelectOption(curr_obj_id, objFull.options[i].text, objFull.options[i].value);
  } else {
    alert("请在左边选择要添加的内容");
    return false;
  }
  return true;
}
function sysDelOption(curr_obj_id) {
  var objCurr = document.getElementById(curr_obj_id);
  var len = objCurr.options.length;
  var i = objCurr.selectedIndex;
  if (i > -1) {
    var arrValue = new Array();
    var arrText = new Array();
    var j;
    for (j = 0; j < len; j++) {
      if (j != i) {
        arrValue.push(objCurr.options[j].value);
        arrText.push(objCurr.options[j].text);
      }
    }
    removeSelectAllOptions(curr_obj_id);
    for (j = 0; j < (len - 1); j++) {
      addSelectOption(curr_obj_id, arrText[j], arrValue[j]);
    }
  } else {
    alert("请在右边选择所要删除的内容");
    return false;
  }
  return true;
}
/* end of select/option functions */

/* set cookie */
function setCookie(name, value, expires, path, domain) {
  /* set time, it's in milliseconds */
  var today = new Date();
  today.setTime( today.getTime() );

  /*
  if the expires variable is set, make the correct
  expires time, the current script below will set
  it for x number of days, to make it for hours,
  delete * 24, for minutes, delete * 60 * 24
  */
  if (expires) {
    expires = expires * 86400000;
  }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" );
}



