﻿var _isIE	 = window.navigator.appName == "Microsoft Internet Explorer" ? true:false;
var _isOpera = window.navigator.appName == "Opera" ? true:false;

// outerHTML for firefox
if(typeof(HTMLElement)!="undefined" && !window.opera)
{
  HTMLElement.prototype.__defineGetter__("outerHTML",function()
  {
    var a=this.attributes, str="<"+this.tagName, i=0;for(;i<a.length;i++)
    if(a[i].specified) str+=" "+a[i].name+'="'+a[i].value+'"';
    if(!this.canHaveChildren) return str+" />";
    return str+">"+this.innerHTML+"</"+this.tagName+">";
  });
  HTMLElement.prototype.__defineSetter__("outerHTML",function(s)
  {
    var r = this.ownerDocument.createRange();
    r.setStartBefore(this);
    var df = r.createContextualFragment(s);
    this.parentNode.replaceChild(df, this);
    return s;
  });
  HTMLElement.prototype.__defineGetter__("canHaveChildren",function()
  {
    return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());
  });
}

String.prototype.toDate = function(x, p) {
  if(x == null) x = "-";
  if(p == null) p = "ymd";
  var a = this.split(x);
  var y = parseInt(a[p.indexOf("y")]);
  if(y.toString().length <= 2) y += 2000;
  if(isNaN(y)) y = new Date().getFullYear();
  var m = parseInt(a[p.indexOf("m")]) ;
  var d = a[p.indexOf("d")];
	m--;
  if(isNaN(d)) d = 1;

  return new Date(y, m, d);
}


/* -------- Common Method Congregate By Desmond ------------------ */


var CommonFunctions = new Object();

CommonFunctions.Interval = [];
CommonFunctions._isIE	 = window.navigator.appName == "Microsoft Internet Explorer"?true:false;
CommonFunctions._isOpera = window.navigator.appName == "Opera"?true:false;
// trim string
CommonFunctions.trim = function(str)
{
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
// create html element

CommonFunctions.createElement = function(type, parent) {
	var el = null;
	if (document.createElementNS) {		// use the XHTML namespace;
		el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
	} else {
		el = document.createElement(type);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(el);
	}
	return el;
}

CommonFunctions.createSizeString = function(size){
	if(typeof(size) == "number"){
		return(size + "px");
	}
	else if((size*1).toString()==size){
		return(size + "px");
	}
	else{
		return(size);
	}
}

CommonFunctions.getElement = function(ev) {
	if (CommonFunctions._isIE) {
		return window.event.srcElement;
	} else {
		return ev.currentTarget;
	}
};

CommonFunctions.getTargetElement = function(ev) {
	if (CommonFunctions._isIE) {
		return window.event.srcElement;
	} else {
		return ev.target;
	}
};

CommonFunctions.getInnerSize = function(){
	var w = window.innerWidth  || (document.documentElement.offsetWidth  || document.body.offsetWidth);
	var h = window.innerHeight || (document.documentElement.offsetHeight || document.body.offsetHeight);
	return [w,h];
}

CommonFunctions.getScrollSize = function(){
	var w = document.documentElement.scrollLeft  || document.body.scrollLeft;
	var h = document.documentElement.scrollTop || document.body.scrollTop;
	return [w,h];
}


CommonFunctions.copyNodeInto	= function(id,newid,target){
	var newnode = $(id).cloneNode(true);
	$(id).id = id + "tmp";
	target.insertBefore(newnode,null);
	$(id).id = newid;
	$(id + "tmp").id = id;
}


CommonFunctions.cutNodeInto	= function(id,target){
	var newnode = $(id).cloneNode(true);
	$(id).outerHTML = '';
	target.insertBefore(newnode,null);
}


CommonFunctions.getCurrentStyle = function(obj, prop) {
	if (obj.currentStyle) {
		return obj.currentStyle[prop];
	}
	else if (window.getComputedStyle) {
		prop = prop.replace (/([A-Z])/g, "-$1");
		prop = prop.toLowerCase ();
		return window.getComputedStyle (obj, "").getPropertyValue(prop);
	}
	return null;
}


CommonFunctions.startDrag = function(ev,target,r,moveevent,offset,limit){
	ev 				= ev || window.event;
	var obj 		= Event.element(ev);
	var x 			= ev.layerX || ev.offsetX,y = ev.layerY || ev.offsetY;
	var offrange 	= !offset?[0,0]:offset;
	var hp 	= vp = 0;
		var hx 	= Event.pointerX(ev) - offrange[0] - x;
		var vy 	= Event.pointerY(ev) - offrange[1] - y;
		hp 		= !r?hx:(hx<r[0]?r[0]:hx>r[1]?r[1]:hx);
		vp 		= !r?vy:(vy<r[2]?r[2]:vy>r[3]?r[3]:vy);
		target.style.left 	= hp + "px";
		target.style.top 	= vp + "px";
	document.onmousemove = function(ev){
		ev 		= ev || window.event;
		var hp 	= vp = 0;
		var hx 	= Event.pointerX(ev) - offrange[0] - x;
		var vy 	= Event.pointerY(ev) - offrange[1] - y;
		hp 		= !r?hx:(hx<r[0]?r[0]:hx>r[1]?r[1]:hx);
		vp 		= !r?vy:(vy<r[2]?r[2]:vy>r[3]?r[3]:vy);
		target.style.left 	= hp + "px";
		target.style.top 	= vp + "px";
		if(moveevent)
			moveevent();

	}
	if(!limit && _isIE){
		if(document.body.setCapture)
			document.body.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	}

	if(CommonFunctions._isIE){
		document.onselectstart = function(){return false};
	}
	document.onmouseup = CommonFunctions.stopDrag;
}


CommonFunctions.stopDrag = function(ev,stopmoveevent){
	ev 		= ev || window.event;
	var obj = Event.element(ev);

	if(stopmoveevent)
		stopmoveevent();
	if(document.body.releaseCapture)
		document.body.releaseCapture();
	else if(window.captureEvents)
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	document.onmousemove 	= null;
	document.onmouseup		= null;
	if(CommonFunctions._isIE){
		document.onselectstart = function(){return true};
	}
}


CommonFunctions.StartFloat = function(interid,target,floatoffset,floatevent){
	var myfoffset 	= floatoffset || 1;
	var mytarget	= target;
	var myevent		= floatevent;
	var _moveLayerBy	= function(l,x,y){
		l.style.left	= (l.offsetLeft	+ x) + "px";
 		l.style.top		= (l.offsetTop	+ y) + "px";
	}
	var _float = function(){
		if  (typeof(mytarget.sX) == "undefined")  { mytarget.sX=0; };
    	if  (typeof(mytarget.sY) == "undefined")  { mytarget.sY=0; };
		var sx = CommonFunctions.getScrollSize()[0],sy = CommonFunctions.getScrollSize()[1];
		if(mytarget.sX != sx|| mytarget.sY != sy){
			var mx 		= sx>mytarget.sX?Math.ceil:Math.floor;
			var my 		= sy>mytarget.sY?Math.ceil:Math.floor;
    	    var dx 		= mx((sx - mytarget.sX)/myfoffset);
			var	dy 		= my((sy - mytarget.sY)/myfoffset);
			mytarget.sX += dx;
			mytarget.sY += dy;
			_moveLayerBy(mytarget,dx,dy);
			if(myevent)
				myevent();
		}
	}
	CommonFunctions.Interval.push({intervalid:interid,interval:eval(interid + " = setInterval(_float,50)")});
}
CommonFunctions.StopFloat = function(interid){
	for(var i=0;i<CommonFunctions.Interval.length;i++){
		if(CommonFunctions.Interval[i].intervalid == interid)
			eval("clearInterval('" + CommonFunctions.Interval[i].interval + "')");
	}
}


CommonFunctions.getAbsPoint = function(e){
	if(!e)
	 return [0,0];
	var x = e.offsetLeft;
	var y = e.offsetTop;
	while(e = e.offsetParent){
		x += e.offsetLeft;
		y += e.offsetTop;
	}
	return {"x": x, "y": y};
}


CommonFunctions.preloadImages = function() { //v3.0
	var d=document;
	if(d.images){
		if(!d.MM_p)
			d.CF_p=new Array();
   		var i,j=d.CF_p.length,a=CommonFunctions.preloadImages.arguments;
		for(i=0; i<a.length; i++)
   			if (a[i].indexOf("#")!=0){
				d.CF_p[j]=new Image;
				d.CF_p[j++].src=a[i];
			}
	}
}



function Cookie()
{
  this.SetValue=function(name,value,hours,path,domain,secure){
    var str=new String();
    var nextTime=new Date();
    nextTime.setHours(nextTime.getHours()+hours);
    str=name+"="+escape(value);
    if(hours)
      str+=";expires="+nextTime.toGMTString();
    if(path)
      str+=";path="+path;
    if(domain)
      str+=";domain="+domain;
    if(secure)
      str+=";secure";
    document.cookie=str;
    }
  this.GetValue=function(name){
    var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
    if(tmp=rs)
      return unescape(tmp[2]);
    return null;
    }
}







/* ----- functions for current project ---------- */

function setSelectedMenu(){
	if(typeof crtMainMenu != 'undefined'){
		if($(crtMainMenu))
			$(crtMainMenu).className = "active"	;
	}

}

function setCurrentPosition(){
	var str='';
	if(strPositionText)
		str += strPositionText ;
	if($(crtMainMenu)){
	//	str += "&nbsp;&gt;&nbsp;";
		var mylink =$(crtMainMenu).getElementsByTagName("a");

		if(mylink[0]){
			str += "<a href='" + mylink[0].href + "'>";
			str += mylink[0].innerHTML;
			str += "</a>";
		}
		else
			str += "<a href='index.shtml'>棣?椤?/a>";
	}
	if(window.location.href.indexOf("menuname=")>0){
		var mstr = getLocationString();
		var arrId = mstr.split("_");
		for(var i=0;i<arrId.length;i++){
			str += "<a href='" + $(arrId[i]).href + "'>";
			str += $(arrId[i]).innerHTML;
			str += "</a>";
			if(i != arrId.length-1)
				str += "&nbsp;&gt;&nbsp;";
		}
	}

	if($("CurrentPosition") && $(crtMainMenu))
		$("CurrentPosition").innerHTML = str;
}

function switchTable(arrTabs,arrTables,selTab,selTable){
	for(var i=0;i<arrTabs.length;i++){
		$(arrTabs[i]).className = "normal";
		$(arrTables[i]).style.display = "none";
	}
	$(selTab).className = "active";
	$(selTable).style.display = "";
}

function getLocationString(){
//	var hs = window.location.href
//	var start = hs.indexOf("menuname=") + 9;
//	var end	= hs.indexOf("&")>0?(hs.split("&")[0].length):hs.length;
//	var mstr = hs.substring(start,end);
//	return mstr	;

	var hs = window.location.href
	var start = hs.indexOf("menuname=") + 9;
	var end	= hs.indexOf("&",start)>0?(hs.indexOf("&",start)):hs.length;
	var mstr = hs.substring(start,end);
	return mstr	;

}

function initLocation(){
if(window.location.href.indexOf("menuname=")>0){
	var mstr = getLocationString();
	var arrId = mstr.split("_");
	for(var i=0;i<arrId.length;i++){
		if(arrId[i]!=""){
		var p =$(arrId[i]).parentNode;
		p.className = p.getAttribute("sclass");
		if($(arrId[i]+"_subarea"))
			$(arrId[i]+"_subarea").style.display = '';
		}
    	}
	}
}







/* ---------- Eric ---------- */
function listTable_trMouseOver(o){
	o.style.background = "#fafafa";
}
function listTable_trMouseOut(o){
	o.style.background = "#ffffff";
}

function switchSearchTab(o,tabGropId,blockId,tableId){
	try{
		var oTables = document.getElementById(blockId).getElementsByTagName("table");
		var olis = document.getElementById(tabGropId).getElementsByTagName("li");
		if(oTables && olis){
			for(var i = 0; i < oTables.length; i++){
				if(oTables[i].className == "searchTable"){
					oTables[i].style.display = "none";
				}
			}
			document.getElementById(tableId).style.display = "";
			for(var j = 0; j < olis.length; j++){
				olis[j].className = "normal";
			}
			o.className = "active";
		}
	}catch(e){}
}

function switchTabGroup(o,tabGropId,blockId,tabId){
	try{
		var oDivs = document.getElementById(blockId).getElementsByTagName("div");
		var olis = document.getElementById(tabGropId).getElementsByTagName("li");
		if(oDivs && olis){
			for(var i = 0; i < oDivs.length; i++){
				if(oDivs[i].className == "Main_tabGroupBar_body"){
					oDivs[i].style.display = "none";
				}
			}
			document.getElementById(tabId).style.display = "";
			for(var j = 0; j < olis.length; j++){
				olis[j].className = "normal";
			}
			o.className = "active";
		}
setHeight('Main_body','Main_left');
	}catch(e){}

}
function switchMainLeft(o){
	try{
		var oMainLeft = document.getElementById('Main_left');
		var oMainBody = document.getElementById('Main_body');
		var oDivs = oMainLeft.getElementsByTagName("div");
		if(o.className == "arrowLeft"){
			$("LeftSide").style.display="none";
			o.className = "arrowRight";
			oMainLeft.style.width = "12px";
			oMainBody.style.marginLeft = "16px";
			oMainLeft.style.left = 0;

		}else if(o.className == "arrowRight"){
			$("LeftSide").style.display="";
			oMainLeft.style.width = "193px";
			oMainBody.style.marginLeft = "202px";
			oMainLeft.style.left = "5px";
			o.className = "arrowLeft";
		}
	}catch(e){}
}


function switchSubTabGroup(o,tabGropId,blockId,tabId){
	try{
		var oDivs = document.getElementById(blockId).getElementsByTagName("div");
		var olis = document.getElementById(tabGropId).getElementsByTagName("li");
		if(oDivs && olis){
			for(var i = 0; i < oDivs.length; i++){
				if(oDivs[i].className == "Main_subTabGroupBar_body"){
					oDivs[i].style.display = "none";
				}
			}
			document.getElementById(tabId).style.display = "";
			for(var j = 0; j < olis.length; j++){
				olis[j].className = "normal";
			}
			o.className = "active";
		}
		setHeight('Main_body','Main_left');
	}catch(e){}
}


function toggleDisplay(id){
	try{
		var obj = document.getElementById(id);
		if(obj){
			if(obj.style.display == ""){
				obj.style.display = "none";
			}else{
				obj.style.display = "";
			}
		}
	}catch(e){}
}

function setPageSize(){
	var w = CommonFunctions.getInnerSize()[0];
	if(w>1000)
		document.getElementById('autoFit').style.width = "100%";
	else
		document.getElementById('autoFit').style.width = "1000px";
}

function setHeight(id1,id2){
	//getInnerHeight("Main_body");
	try{
		var obj1 = document.getElementById(id1);
		var obj2 = document.getElementById(id2);
		
		
		if(obj1 && obj2){

			
			obj1.style.height = obj2.style.height = "auto";
			obj1.style.minHeight = obj2.style.minHeight = "auto";
			
			//alert(obj1.offsetHeight);
			//alert(obj2.offsetHeight);
			
			if(_isIE){
				if(obj1.offsetHeight < obj2.offsetHeight){
						obj1.style.height = obj2.offsetHeight + "px";
				}else{
						//obj2.style.height = obj1.offsetHeight + "px";
						obj1.style.height = "auto";
				}
			}else{
				if(obj1.offsetHeight < obj2.offsetHeight){
						obj1.style.minHeight = obj2.offsetHeight + "px";
				}else{
						//obj2.style.minHeight = obj1.offsetHeight + "px";
						obj1.style.minHeight = "auto";
				}
				
			}
		}
	}catch(e){}
}



function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return([myWidth,myHeight]);
}

function setDivVerticalAlign(id){
	var matrix = getWindowSize();
	var bodyHeight,bodyWidth;
	var oDiv = document.getElementById(id);
	var divHeight,divWidth;
	try{
		if(matrix && oDiv){
			bodyWidth = matrix[0];
			bodyHeight = matrix[1];
			divWidth = oDiv.offsetWidth;
			divHeight = oDiv.offsetHeight;
			oDiv.style.marginTop = (bodyHeight - divHeight) / 2 + "px";
		}
	}catch(e){}
}

function showAndSwitch(o,idstr){
	var idArr = idstr.split(",");
	if(o.src.indexOf("minus.gif") != -1){
		for(var i = 0; i < idArr.length; i++){
			$(idArr[i]).style.display = 'none';
			o.src = o.src.substring(0,o.src.lastIndexOf("/") + 1) + "plus.gif";
		}
	}else{
		for(var i = 0; i < idArr.length; i++){
			$(idArr[i]).style.display = '';
			o.src = o.src.substring(0,o.src.lastIndexOf("/") + 1) + "minus.gif";
		}
	}
	/*if($(id).style.display == 'none'){
		o.src = o.src.substring(0,o.src.lastIndexOf("/") + 1) + "minus.gif";
		$(id).style.display ='';
	}else{
		o.src = o.src.substring(0,o.src.lastIndexOf("/") + 1) + "plus.gif";
		$(id).style.display ='none';		
	}*/
}

function showTipBar(id){
	var oDivs = document.getElementsByTagName('div');
	for(var i = 0; i < oDivs.length; i++){
		if(oDivs[i].className == 'Main_tipBar'){
			oDivs[i].style.display = "none";
		}
	}
	$(id).style.display = '';
	$(id).style.zIndex = 9999;
}
function hideTipBar(id){
	var doHide = function(){				
		$(id).style.display = "none";
	}
	window.setTimeout(doHide, 1000);
}


function toggleDisplay2(o,id1,id2,id3,id4){
	try{
		var obj1 = document.getElementById(id1);
		var obj2 = document.getElementById(id2);
		var obj3 = document.getElementById(id3);
		var obj4 = document.getElementById(id4);
		if(obj1 && obj2 && obj3){
			if(o.value == 0){
				if(obj1.style.display != "" && obj2.style.display != "none" && obj3.style.display != "" && obj4.style.display != ""){
					obj1.style.display = "";
					obj2.style.display = "none";
					obj3.style.display = "";
					obj4.style.display = "";
				}
			}else{
				if(obj1.style.display != "none" && obj2.style.display != "" && obj3.style.display != "none" && obj4.style.display != "none"){
					obj1.style.display = "none";
					obj2.style.display = "";
					obj3.style.display = "none";
					obj4.style.display = "none";	
				}
			}
		}
	}catch(e){}
}

/* ---------- Eric ---------- */


/*----------- Select -----------*/
/*function Select(record,multiple){
	this.record = record == null ? [] : record;
	this.multiple = multiple == null ? null : multiple;

	this.createTo = function(id){
		var count = 0;
		var oUl = document.createElement("ul");
		oUl.id = id + "_ul";
		for(var i = 0; i < this.record.length; i++){
			var oLi = document.createElement("li");
			oLi.innerHTML = this.record[i].name;
			oLi.title = this.record[i].title;
			oLi.className = "normal";
			if(this.multiple == false){
				oLi.onclick = function(){
					if(this.className == "normal"){
						var oLis = this.parentNode.getElementsByTagName("li");
						for(var j = 0; j < oLis.length; j++){
							oLis[j].className = "normal";
						}
						this.className = "active";
						count = 1;

					}else{
						this.className = "normal";
						count = 0;
					}
					//alert(count);
				}
			}else{
				oLi.onclick = function(){
					if(this.className == "normal"){
						this.className = "active";
						count++;
					}else{
						this.className = "normal";
						count--;
					}
				}
			}
			oUl.appendChild(oLi);
		}
		document.getElementById(id).innerHTML = "";
		document.getElementById(id).appendChild(oUl);
	}


}
*/


function Select(record,multiple){
	this.record = record == null ? [] : record;
	this.multiple = multiple == null ? null : multiple;

	this.createTo = function(id){
		var count = 0;
		var str = "<ul id='" + id + "_ul'>";
		for(var i = 0; i < this.record.length; i++){
			str += "<li class='normal' id='" + this.record[i].id;
			str += "' title='"+this.record[i].title+"' onclick=";
			str += "'Select.oc(this," + this.multiple + ")'>";
			str += this.record[i].name;
			str += "</li>"
		}
		str += "</ul>"
		document.getElementById(id).innerHTML = str;
	}

}Select.oc = function(obj,ismulti){
	if(ismulti){
		obj.className=obj.className=='active'?'normal':'active';
	}
	else{
		if(obj.className == "normal"){
			var oLis = obj.parentNode.getElementsByTagName("li");
			for(var j = 0; j < oLis.length; j++){
				oLis[j].className = "normal";
			}
			obj.className = "active";
			count = 1;

		}else{
			obj.className = "normal";
			count = 0;
		}
	}
}


function allMoveTo(id1,id2,isAll){
	var oUl1 = document.getElementById(id1 + "_ul");
	var oUl1Lis = oUl1.getElementsByTagName("li");
	var oUl2 = document.getElementById(id2 + "_ul");
	var oUl2Lis = oUl2.getElementsByTagName("li");
	var l = oUl1Lis.length;
	for(var i = 0; i < l; i++){
		if(isAll == true){
			var oLi = document.createElement("li");
			oLi = oUl1Lis[i];
			oLi.className = "normal";
			oUl2.appendChild(oLi);

			l = document.getElementById(id1 + "_ul").getElementsByTagName("li").length;
			i--;
		}else{
			if(oUl1Lis[i].className == "active"){
				var oLi = document.createElement("li");
				oLi = oUl1Lis[i];
				oLi.className = "normal";
				oUl2.appendChild(oLi);

				l = document.getElementById(id1 + "_ul").getElementsByTagName("li").length;
				i--;
			}
		}
	}
}

function switchTabBar(o,id,strId){
	var oUl = o.parentNode;
	var aId = strId.split(',');
	var oLis = oUl.getElementsByTagName("li");
	for(var i = 0; i < oLis.length; i++){
		oLis[i].className = "normal";
	}
	o.className = "active";
	for(var i = 0; i < aId.length; i++){
		document.getElementById(aId[i]).style.display = "none";
	}
	document.getElementById(id).style.display = "";
}

function switchTabBar2(o){
	var oUl = o.parentNode;
	var oLis = oUl.getElementsByTagName("li");
	for(var i = 0; i < oLis.length; i++){
		oLis[i].className = "normal";
	}
	o.className = "active";
}
