/**
*  class of div operation system .
*
* This class will provide the div operation function set 
*
* @author      Carl carl@heimavista.com , Ryan ryan@heimavista.com
* @version  $Id: divos.js,v 1.30 2009/05/22 03:22:47 carl Exp $
*/

/**
*  Constructor function,Initialize the parameters.
*
*
*  @param string name
*  @return null
*/
function divOsClass(name) {
   this.name = name;
	this.Cookie = new Cookie();
	this.sajaxIO = new sajaxIO();
	this.setDebug(0);

	this.tagTitleVisibleStyle = "active";
	this.tagTitleInVisibleStyle = "inactive";
	this.tagContentVisibleStyle = "showThis";
	this.tagContentInVisibleStyle = "hideThis";

	this.waitWord = "Please waiting...";

	this.contentDivPrefix = "C";
	this.TagDivPrefix = "T";

	this.ResizeFix = "0";

	this.tagSeq = 10;
	this.popSeq = 10;

	this.act="";
	
	this.imagedir = "images";
	this.styledir = "style";

	this.hashTable = new hashUtil();
	this.tagUrl = new hashUtil();
	this.hashTable.put("home",0);
	this.TagName = "C0";

	this.popHandle = new hashUtil();

	this.closeTagHtml = "<div onclick='"+this.name+".selectTag(===TAG===)'><div><a href=\"javascript:void(0)\" class=\"close\" title=\"\" onclick='"+this.name+".delTag(===TAG===);event.cancelBubble=true'><img src=\""+this.imagedir+"/clear.gif\" alt=\"close\" /></a><a href=\"javascript:void(0)\"><img src=\"===ICON===\" align=\"absmiddle\" alt=\"\" />&nbsp;===TITLE===</a></div></div>	";
	this.Info = new Array();
	this.getBrowser();
	this.position="";
   this.onloadFuncs = new Array();
	this.onloadFailFuncs = new Array();
   this.onloadTimeOut = 500;
	this.onloadCount = 0;
   this.onloadPoint = 0;
   this.onloadStart = 1;
   return this;
}
var tempHideDiv="";   // Global variable for hide/show a div
divOsClass.prototype.AlertSuccess= 100;
divOsClass.prototype.AlertFailed = 101;
divOsClass.prototype.AlertWait= 102;
divOsClass.prototype.AlertNote= 103;

/**
*  set Variable
*
*
*  @param string p_word , keyword
*  @param string p_info , content
*  @return null
*/
divOsClass.prototype.setInfo = function(p_word,p_info) {
	eval("this."+p_word+"='"+p_info+"'");
}

/**
*  set close Tag Html
*
*  @return null
*/
divOsClass.prototype.refreshCloseTagHtml = function() {
	this.closeTagHtml = "<div onclick='"+this.name+".selectTag(===TAG===)'><div><a href=\"javascript:void(0)\" class=\"close\" title=\"\" onclick='"+this.name+".delTag(===TAG===);event.cancelBubble=true'><img src=\""+this.imagedir+"/clear.gif\" alt=\"close\" /></a><a href=\"javascript:void(0)\"><img src=\"===ICON===\" align=\"absmiddle\" alt=\"\" />&nbsp;===TITLE===</a></div></div>	";
}

/**
*  get client browser's type,such as "Microsoft IE" or "FireFox"
*
*
*  @return String , 'IE' or 'FF'
*/
divOsClass.prototype.getBrowser = function() {
	var navi = window.navigator.userAgent.toUpperCase();
	if (navi.indexOf("MSIE")>=1) this.browser = "IE";
	else if (navi.indexOf("FIREFOX")>=1) this.browser = "FF";
	return this.browser;
}

/**
*  set tag div object to this.tagDiv, and it's name to this.tagDivName
*
*
*  @param String tagname
*  @return null
*/
divOsClass.prototype.setTagDiv = function(tagname) {
	this.tagDivName = tagname;
	this.tagDiv = document.getElementById(tagname);
	//obj= document.getElementById('mainarea');
}

/**
*  set main content div object to this.contentDiv, and it's name to this.contentDivName
*
*
*  @param String main
*  @return null
*/
divOsClass.prototype.setContentDiv = function(main) {
	this.contentDivName = main;
	this.contentDiv = document.getElementById(main);
}

/**
*  set debug mode , if param 1 isn't null,debug information will be shown.
*
*
*  @param Integer p_debug
*  @return null
*/
divOsClass.prototype.setDebug = function(p_debug) {
	if(p_debug)	
		this._Debug = true;
	else 
		this._Debug = false;
}

/**
*  set tag title's html code to this.closeTagHtml
*
*
*  @param String p_str
*  @return null
*/
divOsClass.prototype.setTagCloseHtml = function(p_str) {
	this.closeTagHtml = p_str;
}

/**
*  Virtual open a new tag
*
*
*  @param String p_url
*  @param String p_name
*  @param String p_target
*  @return null
*/
divOsClass.prototype.virtualOpen = function(p_url,p_name,p_target,p_icon,p_setcookie) {
	var tagSeq;
	if(p_name=='undefined' || p_name=='') return;
	tagSeq = this.addTag(p_name,p_icon);
	this.hashTable.put(p_target,tagSeq) ;
	this.tagUrl.put(tagSeq,p_url) ;
	if(typeof(p_setcookie)!="undefined" && p_setcookie) {
		var u=p_url.split("?");
		this.Cookie.setCookie("_epage_tags",this.Cookie.getCookie("_epage_tags")+";;"+p_target+"=="+p_name+"=="+u[1]+"=="+p_icon);
	}
}

/**
*  open a new tag
*
*
*  @param String p_url
*  @param String p_name
*  @param String p_target
*  @return null
*/
divOsClass.prototype.open = function(p_url,p_name,p_target,p_icon) {
	var tagSeq;
	//if(p_target=='_blank' || p_target==null || p_target='')  {
	if(p_target=='_blank' )  {
		tagSeq = this.addTag(p_name,p_icon);
	}
	else {
		tagSeq = this.hashTable.get(p_target);
		if(!this.hasTag(tagSeq) || tagSeq==null) {
			tagSeq = this.addTag(p_name,p_icon);
			var u=p_url.split("?");
			this.Cookie.setCookie("_epage_tags",this.Cookie.getCookie("_epage_tags")+";;"+p_target+"=="+p_name+"=="+u[1]+"=="+p_icon);
		}
		else {
			this.tagUrl.remove(tagSeq);
			this.selectTag(tagSeq);
		}
	}
	this.hashTable.put(p_target,tagSeq) ;

	var contDiv = this.contentDivPrefix+tagSeq;
   this.openUrl(contDiv,p_url);
}

/**
*  put the contents from a url into specific div
*
*
*  @param string p_div
*  @param string p_url
*  @param bool p_silent
*  @return null
*/
divOsClass.prototype.openUrl = function(p_div,p_url,p_silent) {
	if(p_url.indexOf("?")<0) p_url+="?";
	else p_url+="&";
	p_url+="TagName="+p_div;

	this.genDivContentCallBackFunction(p_div);
	this.sajaxIO.sajaxSubmit('','',this.name+".setdivContent"+p_div,'_displayProgram',p_url);
	//if (!p_silent) divOs.openAlertWindow(divOsClass.prototype.AlertWait,divOs.waitWord,"Static=1;Close=1;height=80;width:200;Static:0;top=180","sending","NoButton");
	if (!p_silent) divOs.openWaitingWindow(divOs.waitWord,"sending");

}

/**
*  put the contents from a url which is a standard(heimavista) sajax return into specific div
*
*
*  @param String p_div
*  @param String p_url
*  @return null
*/
divOsClass.prototype.openSajaxUrl = function(p_div,p_url) {
	if(p_url.indexOf("?")<0) p_url+="?";
	else p_url+="&";
	p_url+="TagName="+p_div;
	p_url+="&DivId="+p_div;
	//this.genDivContentCallBackFunction(p_div);
	this.sajaxIO.sajaxSubmit('','',divOsClass.prototype.DivSajaxCallBack,'sajaxSubmit',p_url);
}

/**
*  put the contents from a url which is a standard(heimavista) sajax return into specific div
*
*
*  @param String p_div
*  @param String p_url
*  @return null
*/
divOsClass.prototype.openSubmitForm = function(p_title,p_url,p_form,p_param,p_style,p_event) {
	divid="tmp"+this.popSeq;
	this.openPopWindow(p_title,p_style,'',divid,p_event) ;
	this.submitForm('ppcont'+divid,p_url,p_form,p_param,'sajaxSubmit');
}

/**
*       submit form
*
*
*       @return null
*/
divOsClass.prototype.submitForm = function() {
	a=divOsClass.prototype.submitForm.arguments;

	argDiv = a[0];
	argUrl = a[1];
	if(a[2]) argForm = a[2];
	else argForm = "";
	if(a[3]) argParam = a[3];
	else argParam = "";
	if(a[4]) argFun = a[4];
	else argFun = "sajaxSubmit";

	if(argUrl.indexOf("?")<0) argUrl+="?";
	else argUrl+="&";
	argUrl+="TagName="+argDiv;

	this.genDivSubmitCallBackFunction(argDiv);
	this.sajaxIO.sajaxSubmit(argParam,argForm,this.name+".setDivSubmitContent"+argDiv,argFun,argUrl);
	this.Cookie.setCookie(argDiv,argUrl);
}

/**
*  inner function, deal contents getted by openSajaxUrl function
*
*
*  @access private
*  @return null
*/
divOsClass.prototype.DivSajaxCallBack= function(z) {
   var divOs_obj = eval("divOs");
   if(divOs_obj._Debug)  alert(z);
   Res= sajaxIO.prototype.getMsg(z);
	if(Res.DivId) $("#"+Res.DivId).html(Res.Content);
   if(Res.JsFunction) eval(Res.JsFunction);
}

/**
*  return url that the div content get from.
*
*
*  @param String p_div
*  @return String
*/
divOsClass.prototype.getDivUrl = function(p_div) {
	var p =  this.Cookie.getCookie(p_div);
	return p;
}

/**
*  generate div content callback function
*
*
*  @param string p_div
*  @return null
*/
divOsClass.prototype.genDivContentCallBackFunction = function(p_div) {
	var str = "divOsClass.prototype.setdivContent"+p_div+" = function(z) {\n";
	if(this._Debug) str+=" alert(z);\n";
	str+=	"var obj = document.getElementById('"+p_div+"');\n";
	str+=	"divOsClass.prototype.setInnerHTML(obj, z);\n"+
	//str+=	"obj.innerHTML = z;\n"+
		   	//"divOs.closePopWindow('sending');}";
		   	"divOs.closeWaitingWindow('sending');}";
	if(this._Debug) alert(str);
	eval(str);
}

/**
*  generate div submit callback function
*
*
*  @param string p_div
*  @return null
*/
divOsClass.prototype.genDivSubmitCallBackFunction = function(p_div) {
	var str = "divOsClass.prototype.setDivSubmitContent"+p_div+" = function(z) {\n";
	if(this._Debug) str+=" alert(z);\n";
	str +=	 "var ret = sajaxIO.prototype.getMsg(z);\n"+
				 "divOsClass.prototype.setInnerHTML(document.getElementById('"+p_div+"'),ret.Content);\n"+
				 "if(ret.JsFunction!='') eval(ret.JsFunction);\n"+
				 "return ret.Content;\n"+
				 "}\n";
	if(this._Debug) alert(str);
	eval(str);
}

/**
*  deal the html content which want to put into object by innerHTML method
*
*
*  @param Object, el
*  @return String, htmlCode
*/
divOsClass.prototype.setInnerHTML = function (el, htmlCode) {
   if(el==null) return false;
	// 設置之前，先將所有的js引用先執行一遍
	var reg1 = new RegExp("<script[^>]*src=[\"\']*([^>\"\']+)[\"\']*[^>]*>[\s|\S]*?<\/script\s*>","ig");
	while(1) {		
		var arr = reg1.exec(htmlCode);
		if(reg1.lastIndex==0) break;
		jQuery.ajax({
			url: RegExp.$1,
			async: false,
			dataType: "script"
		});
	}
   var ua = navigator.userAgent.toLowerCase();
   if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
      if(el.tagName=="TEXTAREA") el.setAttribute("value",htmlCode);
      else {
         fixHtmlCode = '<div style="display:none">for IE</div>' + htmlCode;
         fixHtmlCode = fixHtmlCode.replace(/<script([^>]*)>/gi,
                     '<script$1 defer>');
         el.innerHTML = '';
         if(htmlCode){
            el.innerHTML = fixHtmlCode;
            el.removeChild(el.firstChild);
         }
      }
   } else {
      if(el.tagName=="TEXTAREA") el.setAttribute("value",htmlCode);
      else {
         var el_next = el.nextSibling;
         var el_parent = el.parentNode;
         el_parent.removeChild(el);
         el.innerHTML = htmlCode;
         if (el_next) {
            el_parent.insertBefore(el, el_next)
         } else {
            el_parent.appendChild(el);
         }
      }
   }
}

/**
*  create a div that setted parent Object,id and className
*
*
*  @param String container
*  @param String id
*  @param String className
*  @return null
*/
divOsClass.prototype.createDiv = function(container,id,className) {
	if(typeof(container)=="string") containerObj = document.getElementById(container);
	else containerObj = container;

   var newDiv=document.createElement("DIV");
   containerObj.appendChild(newDiv);
   newDiv.id=id;
   newDiv.className=className;
}

/**
*  delete a div object by id and parent Object.
*
*
*  @param String, container
*  @param String, id
*  @return null
*/
divOsClass.prototype.delDiv = function(container,id) {
	if(typeof(container)=="string") containerObj = document.getElementById(container);
	else containerObj = container;
   var child = document.getElementById(id);
	if(child==null) return false;
   containerObj.removeChild(child);
}
divOsClass.prototype.handleIEhasLayout=function(){
	//trigger re-rendering
	document.body.style.zoom = '1.1';
	//restore it
	document.body.style.zoom = '';
}
divOsClass.prototype.checkTagDivReady = function() {
	
	if(typeof(this.tagDiv)!='undefined')  return true;
	return false;
}
/**
*  add a heimavista backoffice standard tag
*
*
*  @param String, p_name
*  @return null
*/
divOsClass.prototype.addTag = function(p_name,p_icon) {
	var tagDivName = this.TagDivPrefix+this.tagSeq;
   var newLi=document.createElement("li");
	newLi.id= tagDivName;
	newLi.className = this.tagTitleVisibleStyle ;

	var res = this.closeTagHtml.replace(/===TITLE===/,p_name);
	var icon = this.styledir+"/backstyle/back_skin_grey/icon/6.png";
   if (typeof p_icon != 'undefined' && trim(p_icon)!='' && p_icon!='undefined') {
		icon = p_icon;
	}
	res = res.replace(/===ICON===/,icon);
	var str = " ";
	while(res!=str) {
		str = res;
		res = str.replace(/===TAG===/,this.tagSeq);
	}
	str = res;
	newLi.innerHTML = str;
	
	try{
		this.tagDiv.appendChild(newLi);
	
		var contDivName = this.contentDivPrefix+this.tagSeq;
		this.createDiv(this.contentDiv,contDivName,this.tagContentVisibleStyle);
		this.selectTag(this.tagSeq);
	}catch(e) {
		if(this._Debug) alert(" addTag Exception \n");
		this.tagDiv = document.getElementById(this.tagDivName);
	}
	return this.tagSeq++;
	//return contDivName;
}

/**
*  check if a div is empty or not 
*
*  @param string p_seq
*  @return bool
*/
divOsClass.prototype.isEmptyDiv = function(p_div) {
	var tdiv = document.getElementById(p_div);
	if(tdiv==null) return false;
	if(tdiv.innerHTML.trim()) return false;
	return true;
}
/**
*  check if has tag
*
*  @param string p_seq
*  @return bool
*/
divOsClass.prototype.hasTag = function(p_seq) {
	var tdiv = document.getElementById(this.TagDivPrefix+p_seq);
	if(tdiv==null) return false;
	return true;
}

/**
*  delete a heimavista backoffice standard tag
*
*
*  @param String, p_name
*  @return null
*/
divOsClass.prototype.delTag = function(p_seq) {
	this.delDiv(this.tagDiv,this.TagDivPrefix+p_seq);
	this.delDiv(this.contentDiv,this.contentDivPrefix+p_seq);
	var key = this.hashTable.getKey(p_seq);
	var newcookie = "";
	var tags = this.Cookie.getCookie("_epage_tags").split(";;");
	var tag;
	
	for(var i=0;i<tags.length;i++) {
		if(tags[i]=='undefine') continue;
		if(tags[i]=='') continue;
		tag = tags[i].split("==");
		if(tag[0]==key) continue;
		newcookie+=tag[0]+"=="+tag[1]+"=="+tag[2]+"=="+tag[3]+";;";	
	}
	this.Cookie.setCookie("_epage_tags",newcookie);
	this.hashTable.remove(key);
	seq = this.hashTable.getFirst();
	this.selectTag(seq);
	try{
		resize(this.contentDivPrefix+seq);
	}catch(e){}
}

/**
*  select/switch heimavista backoffice standard tag
*
*
*  @param Integer p_seq
*  @return null
*/
divOsClass.prototype.selectTag = function(p_seq) {
	var cDiv;
	for(var i=0;i<this.tagSeq;i++) {
		cDiv = document.getElementById(this.contentDivPrefix+i);
		tDiv = document.getElementById(this.TagDivPrefix+i);
		if(cDiv==null) continue;
		if(i==p_seq) {
			cDiv.className=this.tagContentVisibleStyle;
			tDiv.className=this.tagTitleVisibleStyle;
		}
		else {
			cDiv.className=this.tagContentInVisibleStyle;
			tDiv.className=this.tagTitleInVisibleStyle;
		}
	}
	this.TagName="C"+p_seq;
	resize(this.TagName);
	var key = this.hashTable.getKey(p_seq);
	var url = this.tagUrl.get(p_seq);
	if(key!=null)
		this.hashTable.mvToFirst(key);
	if(url) {
		this.openUrl(this.contentDivPrefix+p_seq,url);
		this.tagUrl.remove(p_seq);
	}
	this.handleIEhasLayout();
}

/**
*  get browser's client top position when it was been scrolled.
*
*
*  @return null
*/
divOsClass.prototype.getScrollTop = function() { 
   if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
      scrollPos = document.documentElement.scrollTop;
   }
   else if (typeof document.body != 'undefined') {
      scrollPos = document.body.scrollTop;
   }
   return scrollPos;
}

/**
*  clear quote
*
*  @param string text
*  @return string text
*/
divOsClass.prototype.clearQuote = function(text){
	if (typeof( text ) == "undefined") return text; 
   if (typeof( text ) != "string") text = text.toString() ;
   text = text.replace(/"/g, "&quot;") ;
   text = text.replace(/'/g, "&#39;") ;
   return text ;
}

/** Open a url by popup method
*
*
*  @param String, p_title, Pop window Title
*  @param String, p_style, like "width:100;height:200;top:30"
*  @param String, p_url, url
*  @param String, p_div, div id
*  @return null
*/
divOsClass.prototype.openPopUrl = function(p_title,p_style,p_url,p_div,p_event) {
	var divid = "urlPop"+this.popSeq;
	this.openPopWindow(p_title,p_style,"<div id=\""+divid+"\">loading...</div>",p_div);
	this.openUrl(divid,p_url);
	return false;
}

/** Open a sajax url by popup method
*
*
*  @param String, p_title, Pop window Title
*  @param String, p_style, like "width:100;height:200;top:30"
*  @param String, p_url, url
*  @param String, p_div, div id
*  @return null
*/
divOsClass.prototype.openPopSajaxUrl = function(p_title,p_style,p_url,p_div,p_event) {
	var divid = "urlPop"+this.popSeq;
	this.openPopWindow(p_title,p_style,"<div id=\""+divid+"\">loading...</div>",p_div,p_event);
	this.openSajaxUrl(divid,p_url);
	return false;
}

/**
*  Open a alert window
*
*  p_button usage:
*  var btn = new hashUtil();
*  btn.put('test','ShowTitle=o=alert(\"ddd\")');
*  divOs.openAlertWindow(divOs.AlertSuccess,'success title','width:300;height:200','mydivname',btn);
*
*  @param Integer, choices:divOsClass.prototype.AlertSuccess,....AlertWait,.....AlertFailed
*  @param String, p_msg
*  @param String, p_style
*  @param String, p_div
*  @param String, p_button
*  @return null
*/
divOsClass.prototype.openAlertWindow = function(p_type,p_msg,p_style,p_div,p_button,p_title) {
	var divClass, icon,cont2="";
	icon ="wait.gif";	
	if(!p_style) p_style="width:350;height:110;Button:ok;top:180";
	if(p_type==divOsClass.prototype.AlertSuccess) icon ="success.png";	
	if(p_type==divOsClass.prototype.AlertWait) icon = "wait.gif";	
	if(p_type==divOsClass.prototype.AlertFailed) icon ="failure.png";	
	var cont;// = document.getElementById("ppcont"+seq);
	cont =	 "<div><table width=\"100%\" cellpadding=\"5\"><tr><td width=\"1%\" nowrap=\"nowrap\"><img src=\""+this.imagedir+"/"+icon+"\" width=\"34\" height=\"35\" align=\"absmiddle\" /></td><td align=\"left\" valign=\"top\">"+p_msg+"</td></tr></table></div>\n";
	cont += "<div style=\"text-align:center\">\n";
	if (p_button && p_button!="NoButton"){
		while (p_button.getFirstKey()){
			var BtnKey = p_button.getFirstKey();	
			if(BtnKey=='_Ok')
				cont+="<input type='button' name='_Ok' value='  OK  ' onclick=\"javascript:"+this.clearQuote(p_button.get(BtnKey))+";"+this.name+".closePopWindow("+this.popSeq+");\" />\n";
			if(BtnKey=='_Cancel')
				cont+="<input type='button' name='_Cancel' value='Cancel' onclick=\"javascript:"+this.clearQuote(p_button.get(BtnKey))+";"+this.name+".closePopWindow("+this.popSeq+");\" />\n";
			if(BtnKey=='_Js')
				cont2="<script language='javascript' defer>"+(p_button.get(BtnKey))+";</script>\n";
			if (BtnKey!="_Ok" && BtnKey!="_Cancel" && BtnKey!="_Js"){
				var BtnVal = p_button.get(BtnKey).split("=o=");  
				cont+=" <input type='button' name='"+BtnKey+"' value='"+BtnVal[0]+"' onclick=\"javascript:"+this.clearQuote(BtnVal[1])+";"+this.name+".closePopWindow("+this.popSeq+");\" />\n";
			}
			p_button.remove(BtnKey);
		}
	}
	else if(p_button!="NoButton") cont+="<input type='button' name='_Close' value=' Close ' onclick=\"javascript:"+this.name+".closePopWindow("+this.popSeq+");\" />\n";
	cont += "</div>";
	cont += cont2;
	if(!p_title) p_title = window.location.host;
	var seq = this.openPopWindow(p_title,p_style,cont,p_div);
	return seq;
}
divOsClass.prototype.closeWaitingWindow = function(p_id) {
	if (!p_id) p_id="waitingId";
	try {
		var obj = document.getElementById(p_id);
		obj.parentNode.removeChild(obj);
	}catch(e){}
}
/**
*  Open  waiting window
*
*
*  @param String, p_cont,  wait prompt content
*/
divOsClass.prototype.openWaitingWindow = function(p_cont,p_id) {
	if (!p_id) p_id="waitingId";
	var newDiv=document.createElement("DIV");
	newDiv.id = p_id;
   document.body.appendChild(newDiv);
	str="<style>body,td,th,img,a,* {cursor:normal;}</style><div class=\"alertmsg\"><div class=\"alertmsg-inner2\"><div class=\"alertmsg-inner\"><a href=\"javascript:void(0)\" onclick=\"divOs.closeWaitingWindow('"+p_id+"');\" class=\"close\">Close</a><p><img class=\"loadingimg\" src=\""+this.imagedir+"/loading.gif\" align=\"absmiddle\" width=\"16\" height=\"16\" border=\"0\" />"+p_cont+"</p></div></div></div>";
	this.setInnerHTML(newDiv,str);
}

divOsClass.prototype.changePopTitle = function(p_divid,p_title) { 
	try{ 
		var headDiv = document.getElementById(p_divid+"__overPopTitle"); 
		divOsClass.prototype.setInnerHTML(headDiv,"<h4>"+strUtil.prototype.htmlspecialchars(decodeURIComponent(p_title))+"</h4>"); 
	}catch(e){} 
}

/**
*  Open a Pop window
*
*
*  @param String, p_title, Pop window Title
*  @param String, p_style, like "width:100;height:200;top:30"
*  @param String, p_cont, content
*  @param String, p_div, div id
*  @return null
*/
divOsClass.prototype.openPopWindow = function(p_title,p_style,p_cont,p_div,event) {
//alert(p_title);
//	alert(encodeURIComponent(" "));
//	alert(decodeURIComponent(p_title));
	if(p_div!="") {
		var checkObj=document.getElementById(p_div);
		if(checkObj) return false;
	}
	var divWidth="100%",divHeight=300,divTop=70,divLeft=0,divStatic=1,divSpeed=0;
	var Maximize = false;	
	var Close = true;	
	var ret ;
	var divNavigate=false;
	var divFoot=false;
	var Nav = "";
	if(typeof p_style!="undefined") {
		ret = p_style.split(";");
		for(var i=0;i<ret.length;i++) {
			var tmp;
			if(ret[i].indexOf('=')!=-1) tmp = ret[i].split("=");
			if(ret[i].indexOf(':')!=-1) tmp = ret[i].split(":");
			if(tmp[0]=="width") divWidth = tmp[1];
			else if(tmp[0]=="height") divHeight = tmp[1];
			else if(tmp[0]=="top") divTop = tmp[1];
			else if(tmp[0]=="left") divLeft = tmp[1];
			else if(tmp[0]=="Maximize") {
				if(tmp[1]==1) Maximize=true;
				else Maximize=false;
			}
			else if(tmp[0]=="Close") {
				if(tmp[1]==1) Close=true;
				else Close=false;
			} 
			else if(tmp[0]=="Static") {
				if(tmp[1]==1) divStatic=true;
				else divStatic=false;
			}
			else if(tmp[0]=="Nav") {
				Nav = tmp[1];
				Nav.substr(0,1) 
				if(Nav.substr(0,1)=="1") divNavigate=1;
				else if(Nav.substr(0,1)=="2") divNavigate=2;
				else divNavigate=0;

				if(Nav.substr(1,1)=="1") divFoot=1;
				else divFoot=0;
			}
		}
	}
	divTop=0; //set all pop top is 0 for drag.
	AbsDivTop=parseInt(divTop);
	divTop=parseInt(parseInt(divTop)+parseInt(divOsClass.prototype.getScrollTop()));//add the mouse scroll distance
	var ppcontId = "";
	var newDiv=document.createElement("DIV");
   document.body.appendChild(newDiv);
	if(typeof p_div=="undefined" ||  p_div=="") {
   	newDiv.id="popDiv"+this.popSeq;
		ppcontId = "ppcont"+this.popSeq;	
		p_div=newDiv.id;
	}
	else{
		ppcontId ="ppcont"+p_div;	
   	newDiv.id=p_div;
	}
	newDiv.className="pop-float";
	if(event){
		newDiv.style.width="100px"; //for use drag change auto to 100px.
	}
	var str="<script language=\"javascript\">\n"+
			  "var popMaxWidth =document.body.offsetWidth-80;\n"+
			  "var popMaxHeight =document.body.offsetHeight-100;\n";
	if(event){
		str +="var h=document.getElementById(\""+newDiv.id+"\");\n"+
		"var o=document.getElementById(\""+newDiv.id+"__overPopTitle\");\n"+
		"Drag.init(o,h,0,popMaxWidth,0,popMaxHeight);\n";
	}else{
		str +="var h=document.getElementById(\""+newDiv.id+"__movePopOut\");\n"+
      "var o=document.getElementById(\""+newDiv.id+"__overPopTitle\");\n"+
      "Drag.init(o,h,0,popMaxWidth,0,popMaxHeight);\n";	
	}
	if(!event)
		str +="__InitPopDiv(o,h);\n";
	str +="h.onDragEnd = function (x, y) {RecP(x,y,h);}\n"+
		"function RecP(x,y,h){\n"+
			"if(divOs.position==\"back\"){\n"+
				"if(x){Cookie.prototype.setCookie(\"__movePopOut_back_x\",x);}\n"+
				"if(y){Cookie.prototype.setCookie(\"__movePopOut_back_y\",y);}\n"+
			"}else{\n"+
				"if(x){Cookie.prototype.setCookie(\"__movePopOut_front_x\",x);}\n"+
				"if(y){Cookie.prototype.setCookie(\"__movePopOut_front_y\",y);}\n"+
			"}\n"+
		"}\n"+
		"function __InitPopDiv(o,h)\n"+
			"{h.style.position=\"absolute\";\n"+
			"o.style.cursor=\"move\";\n"+
			"if(divOs.position==\"back\"){\n"+
				"var a=Cookie.prototype.getCookie(\"__movePopOut_back_x\");\n"+
				"var b=Cookie.prototype.getCookie(\"__movePopOut_back_y\");\n"+
			"}else{\n"+
				"var a=Cookie.prototype.getCookie(\"__movePopOut_front_x\");\n"+
//				"var b=Cookie.prototype.getCookie(\"__movePopOut_front_y\");\n"+  取消前臺的上下定位,多個彈出框會有定位問題.
				"var b=80;\n"+
			"}\n"+
			"if(a==null || a==\"\"){\n"+
				"var tmpWidth=parseInt(\""+divWidth+"\");\n"+
				"if(tmpWidth==\""+divWidth+"\"){\n"+
					"a=(popMaxWidth/2)-(tmpWidth/2);\n"+
				"}else a=popMaxWidth*(100-tmpWidth)/200;\n"+
				"b=30;\n"+
			"}\n"+
			"if (a!=null&&a!=\"\"){ a=parseInt(a)+\"px\";\n h.style.left=a; }\n"+
			"if (b!=null&&b!=\"\"){ b=parseInt(b)+\"px\";\n h.style.top=b; }\n"+
			"}\n"+
		"</script>\n";
 		str += "<div id=\""+newDiv.id+"__movePopOut\" class=\"pop-outer\">";
	if(event){
	   str += "<div  class=\"pop-container pop-shadow\">\n"+
      "<div id=\"fullDiv"+this.popSeq+"\" class=\"pop-module pop-overlay\">\n"+
             "<div class=\"pop-head\">\n";
	}
	else{
      str += "<div  class=\"pop-container pop-shadow\">\n"+
         		 "<div id=\"fullDiv"+this.popSeq+"\" class=\"pop-module pop-overlay\">\n"+
             	 "<div class=\"pop-head\">\n";
	}
	str+="<div id=\""+newDiv.id+"__overPopTitle\" class=\"pop-head-inner\"><h4>"+strUtil.prototype.htmlspecialchars(decodeURIComponent(p_title))+"</h4></div>\n";
	if(Close) {
		str+= "<div class=\"pop-close\" onclick=\"javascript:"+this.name+".closePopWindow("+this.popSeq+");\"></div>\n";
	}
	str+="</div>\n";

	var navigateHeight=0;
	if(divNavigate==1) {
		str+="<div class=\"pop-navigate\" style=\"height:61px\"><div class=\"pop-navigate-inner\"></div></div>\n";
		navigateHeight=61;
	}
	else if(divNavigate==2) {
		str+="<div class=\"pop-navigate\" style=\"height:36px\"><div class=\"pop-navigate-inner\"></div></div>\n";
		navigateHeight=36;
	}
	if(divFoot) navigateHeight+=43;

	if(typeof(p_cont)=='object') {
		str+=	"<div id=\""+ppcontId+"\" class=\"pop-body\"><div class=\"pop-body-inner\">"+p_cont.innerHTML+"\n";
	}
	else 
		str+=	"<div id=\""+ppcontId+"\" class=\"pop-body\"><div class=\"pop-body-inner\">"+p_cont+"\n";
	str+= "</div></div>\n"; // body end .
	str+= "</div>\n";  		// overlay .
	str+= "<div id=\"underLay"+this.popSeq+"\" class=\"underlay\"></div>\n";
	str+= "</div></div>\n";
	this.setInnerHTML(newDiv,str);
	var fullDiv = document.getElementById("fullDiv"+this.popSeq);
	var ppcontent = document.getElementById(ppcontId);
	var underLay = document.getElementById("underLay"+this.popSeq);

	if(divTop) {
		newDiv.style.top = divTop+"px";
	}
	if(divLeft) {
		newDiv.style.left = divLeft+"px";
	}
	if(event){  //add this for creat div by mouse postion.
		var chkTopSide=false;
		var chkLeftSide=false;
   	var bodywidth = document.body.offsetWidth;
   	var bodyheight ;
		if(document.documentElement)
			bodyheight = document.documentElement.clientHeight;
		else 
	   	bodyheight = document.body.offsetHeight;
   	var chktop =parseInt(event.clientY) + parseInt(divHeight);
	   var chkleft=parseInt(event.clientX) + parseInt(divWidth);
      if(chktop > bodyheight){
		//	chkLeftSide=true;
			chkTopSide=true;
	   	newtop=parseInt(event.clientY) - parseInt(divHeight);
			if(newtop<0) newtop=10;
      }
   	else newtop=event.clientY;
	   newDiv.style.top = newtop+"px";
      if(chkleft > bodywidth){
			//chkTopSide=true;
			chkLeftSide=true;
   	   newleft=parseInt(event.clientX)-parseInt(divWidth)-215;
			if(newleft<0) newleft=10;
	   }else newleft=event.clientX-215;
     	newDiv.style.left = newleft+"px";
      divWidthSm =20;
      divHeightSm =20;
   	divSpeed =10;
	}
	if(divWidth) {	
		if(divWidth.indexOf("%")!=-1) {
			fullDiv.parentNode.parentNode.style.width = divWidth;
			if (this.browser=="FF") ppcontent.style.width="100%";
			//else ppcontent.style.width=(document.documentElement.clientWidth*divWidth.slice(0,-1)/100-19)+"px";
		}
		else {
			fullDiv.parentNode.parentNode.style.width=divWidth+"px";
			if (this.browser=="FF") ppcontent.style.width="100%";//divWidth+"px";
			//else ppcontent.style.width=(divWidth-19)+"px";
		}
	}
	if(divHeight) {	
		if(divHeight.indexOf("%")!=-1) {
			var myOffset = 0;
			if (this.browser=="FF") myOffset = 0;
			newDivHeight=(document.documentElement.clientHeight-myOffset)*divHeight.slice(0,-1)/100;
			if(newDivHeight>(divTop+myOffset)) newDivHeight = newDivHeight-AbsDivTop-myOffset;
			if(newDivHeight<40) newDivHeight=40;
			fullDiv.style.height=newDivHeight+"px";
			h = newDivHeight+3;
			$(newDiv).children("DIV").css("height",h);
			ppcontent.style.height=(newDivHeight-27-navigateHeight)+"px";
			underLay.style.height=(newDivHeight+2)+"px";
		}
		else{
			divHeight = parseInt(divHeight);
			if(divHeight<40) divHeight=40;
			fullDiv.style.height=divHeight+"px";
			h = divHeight+3;
			$(newDiv).children("DIV").css("height",h);
			ppcontent.style.height=(parseInt(divHeight)-27-navigateHeight)+"px";
			underLay.style.height =(parseInt(divHeight)+2)+"px";
		}
	}
	if(divWidth && divHeight && divSpeed){
		$(newDiv).children("DIV").css("width",divWidthSm+"px");
		$(newDiv).children("DIV").css("height",divHeightSm+"px");
		newDiv.style.left = event.clientX+"px";
		newDiv.style.top = (event.clientY+parseInt(divOsClass.prototype.getScrollTop()))+"px";
		widthSpeed = (divWidth-divWidthSm)/10;
		heightSpeed = (divHeight-divHeightSm)/10;
		if(widthSpeed<10) widthSpeedperH=10;
		if(heightSpeed<10) heightSpeed=10;
		divOsClass.prototype.ActiveDiv(p_div,chkTopSide,chkLeftSide,divWidth,divHeight,widthSpeed,heightSpeed,divSpeed);
		$(p_div).children("DIV").css("overflow","hidden");;
	}
	if(true) {
		var floatDiv=document.createElement("DIV");
   	document.body.appendChild(floatDiv);
		floatDiv.id="floatDiv"+this.popSeq;
		floatDiv.className="floatDiv_1";
		floatDiv.style.height=document.documentElement.scrollHeight;
		var clientHeight = document.documentElement.clientHeight;

		floatDiv.innerHTML="<iframe class=\"superDiv\" height=\""+clientHeight+"\" width=\"100%\" border=\"0\" frameborder=\"0\" src=\"about:blank\"></iframe>";
	}
	this.popHandle.put(this.popSeq,newDiv.id) ;
	return this.popSeq++;
}
divOsClass.prototype.freePopdivHeight =function(p_div) {
	var obj = document.getElementById(p_div);
	if(obj.style.height) {
		obj.style.height='auto';
		obj.style.position='static';
		obj.style.padding='0 0 0 0';
		obj.style.overflowY='hidden';
		obj.parentNode.parentNode.parentNode.style.height="auto";
		obj.parentNode.parentNode.parentNode.style.overflow="";
		obj.parentNode.parentNode.parentNode.style.overflow="";
		obj.parentNode.style.height="auto";
		divOs.nextSibling(obj.parentNode).style.height="auto";
	}
}
/*
show div 
*/
divOsClass.prototype.ActiveDiv =function(p_div,chkTop,chkLeft,Rwidth,Rheight,width,height,speed){
	obj = document.getElementById(p_div);
	var fixHeight=false;
	if($(obj).children("DIV").css("height")=='auto') fixHeight = true;
	if(!fixHeight) {
   	var height =  $(obj).children("DIV").css("height").split('px')[0];
   	var perH = (Rheight-height)/7;
	}
   var width =  $(obj).children("DIV").css("width").split('px')[0];
   var top =  obj.style.top.split('px')[0];
   var left =  obj.style.left.split('px')[0];
	
	if((Rwidth-width)>140)
	   var perW = (Rwidth-width)/7;
	else perW=20;
	//else perH=perW=20;
//   if(perH<10) perH=10;
//   if(perW<10) perW=10;

   if (width < Rwidth){
		if(!fixHeight && height<Rheight)
      	height = parseInt(height)+perH;
      width = parseInt(width)+perW;
		if(chkTop && chkLeft){
      	top = parseInt(top)-perH;
      	left = parseInt(left)-perW;
		}
		else if(chkTop){
			top = parseInt(top)-perH;
		}
		else if(chkLeft){
			left = parseInt(left)-perW;
		}
		if(!fixHeight) {
      	$(obj).children("DIV").css("height",height+"px");
		}
    	$(obj).children("DIV").css("width",width+"px");
		if(width>Rwidth) {
			Rheight+=10;
      	$(obj).children("DIV").css("height",Rheight+"px");
		}
		if(top<0) top=0;
		if(left<0) left=0;
      obj.style.top = top+"px";
      obj.style.left = left+"px";
      clearTimeout(this.act);
      this.act = setTimeout('divOsClass.prototype.ActiveDiv(\"'+p_div+'\",'+chkTop+','+chkLeft+','+Rwidth+','+Rheight+','+width+','+height+','+speed+')', speed);
   }
   return false;	
}

/** Open a url by popup method
*
*
*  @return null
*/
divOsClass.prototype.popWindow = divOsClass.prototype.openPopWindow ; 

/**
*  close prior pop window
*
*
*  @param String, p_div
*  @return null
*/
divOsClass.prototype.closePopWindow = function(p_div) {
	if(strUtil.prototype.isInteger(p_div)) p_seq = p_div;
	else p_seq = this.popHandle.getKey(p_div);
	try{
		var floatDiv = document.getElementById("floatDiv"+p_seq);
		var obj = document.getElementById(this.popHandle.get(p_seq));
		obj.parentNode.removeChild(obj);
		floatDiv.parentNode.removeChild(floatDiv);
	}catch(e){}
}

/**
*  close all pop window before specific seq
*
*
*  @param Integer,p_from
*  @return null
*/
divOsClass.prototype.closeAllPopWindow = function(p_from) {
	if(!p_from) p_from=this.popSeq;
	var p_to=p_from-10;
	if(p_to<0) p_to=0;	
   for (var i=p_from;i>p_to;i--){
      try{
         divOs.closePopWindow(i);
      }catch(e){}
   }
}

/**
*  get pop handle
*
*
*  @param string p_seq
*  @return string 
*/
divOsClass.prototype.getPopHandle = function(p_seq)  {
	return this.popHandle.get(p_seq);
}
divOsClass.prototype.getParentNodeById = function(p_obj,p_id,p_islike){
	var str = new String();
	if(p_id == "") return p_obj.parentNode;
	if(p_obj==null) return null;
	if(typeof(p_obj)=="undefined" || p_obj.tagName.toLowerCase()=="body") return null;
	p_obj = p_obj.parentNode;
	if(p_obj==null) return null;
	if(p_obj.id=="") return divOsClass.prototype.getParentNodeById(p_obj,p_id,p_islike);
	if(p_islike){
		str = p_obj.id;
		if(str.indexOf(p_id)>-1){
			return p_obj;
		}
		else
			return divOsClass.prototype.getParentNodeById(p_obj,p_id,p_islike);
	}
	else{
		if(p_obj.id==p_id)
			return p_obj;
		else
			return divOsClass.prototype.getParentNodeById(p_obj,p_id,p_islike);
	}
}
/**  get Parent Form from inner element 
*
*
*    @param p_obj ,object
*    @author carl@heimavista.com
*/
divOsClass.prototype.getParentForm = function(p_obj){
	while(p_obj.tagName.toLowerCase()!="body" && p_obj.tagName.toLowerCase()!="form") {
		p_obj = p_obj.parentNode;
	}
	if(p_obj.tagName.toLowerCase()=="form") return p_obj;
	else return null;
}
/**  get previousSibling of current element 
*
*
*    @param p_obj ,object
*    @author carl@heimavista.com
*/
divOsClass.prototype.previousSibling = function(p_obj){
	if(typeof(p_obj.previousSibling.tagName)=='undefined')
		return divOsClass.prototype.previousSibling(p_obj.previousSibling);
	else return p_obj.previousSibling;
}
divOsClass.prototype.childNode = function(p_obj,p_seq){
	var counter = 0;
	try{
		for(var i=0;i<p_obj.childNodes.length;i++) {
			if(typeof(p_obj.childNodes[i].tagName)=='undefined') continue;
			else {
				if(counter==p_seq) return p_obj.childNodes[i];
				counter++;
			}
		}
	}catch(e) {
		if(this._Debug) alert(" childNode Exception \n");
		return null;
	}
}
/**  get nextSibling of current element 
*
*
*    @param p_obj ,object
*    @author carl@heimavista.com
*/
divOsClass.prototype.nextSibling = function(p_obj){
	if(typeof(p_obj.nextSibling.tagName)=='undefined')
		return divOsClass.prototype.nextSibling(p_obj.nextSibling);
	else return p_obj.nextSibling;
}
/**  when onclick hide the div or user method
*
*
*    @param p_clickobj ,object
*    @param p_hidediv ,div tag name
*    @param p_method ,other method user defined
*    @author ryan@heimavista.com
*/
divOsClass.prototype.addClickEvent = function(p_clickobj,p_hidediv,p_method,p_bubble){
	tempHideDiv = p_hidediv;
	if (typeof event=='undefined')
		p_clickobj.parentNode.addEventListener("click",divOsClass.prototype.tempStop,false);
	else 
		event.cancelBubble = true;
	if(p_bubble==undefined)
		p_bubble = true;
//	if (!p_hidediv && !p_method) return;
	if (p_method)
		Func = eval(p_method);
	else Func = divOsClass.prototype.tempHide;
	if (document.addEventListener){
		if(p_bubble)
			document.addEventListener("click", Func, false)
		else
			document.addEventListener("click", Func, true)
	}
	else if (document.attachEvent)
		document.attachEvent("onclick", Func)
	else if (document.getElementById)
		window.onclick=Func
}
divOsClass.prototype.triggerClickEvent = function(){
	if (document.addEventListener)
		$(document.body).click();
	else if (document.attachEvent)
		$(document.body).click();
	else if (document.getElementById)
		$(window).click();
}
divOsClass.prototype.tempStop = function (func){
	func.cancelBubble = true;
}
divOsClass.prototype.tempHide = function (){
	try	{
		if (tempHideDiv!=null)
			document.getElementById(tempHideDiv).style.display="none";
	}catch (e){}
}
divOsClass.prototype.showDetailMsg = function(p_type,p_msg) {
	try{
	var objDetail = document.getElementById(p_type+"_DetailMsg");
	objDetail.style.display="";
	objDetail.className = "msg msg_success";
	objDetail.innerHTML = p_msg;
	setTimeout("divOsClass.prototype.hideDetailMsg('"+p_type+"')",2000);
	}
	catch(e) {
		if(this._Debug) alert(" showDetailMsg Exception \n");
	}
}
divOsClass.prototype.hideDetailMsg = function(p_type,p_msg) {
   var objDetail = document.getElementById(p_type+"_DetailMsg");
	if(objDetail) objDetail.className = "msg msg_success2";
}

divOsClass.prototype.swapImgRestore = function() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
divOsClass.prototype.preloadImages = function() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=divOsClass.prototype.preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
divOsClass.prototype.findObj = function(n, d) { //v4.01
	if(typeof(n)=="object") return n;
  	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  	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=divOsClass.prototype.findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
divOsClass.prototype.swapImage = function() { //v3.0
  var i,j=0,x,a=divOsClass.prototype.swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=divOsClass.prototype.findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
divOsClass.prototype.getCurrentStyle = function(ele) { 
	if (ele.currentStyle) //IE
		return ele.currentStyle; 
	else 
		return document.defaultView.getComputedStyle(ele, null); 
}
divOsClass.prototype.getUrlParam = function(paramName,url) {
   var oRegex = new RegExp( '[\?&]' + paramName + '=([^&]+)', 'i' ) ;
   var oMatch = oRegex.exec(url) ;
   if ( oMatch && oMatch.length > 1 )
      return oMatch[1] ;
   else
      return '' ;
}
divOsClass.prototype.setOnloadFunc = function(p_fn) {
   this.onloadFuncs.push(p_fn);
	if(!this.onloadStart){
		this.onloadStart = 1;
		this.onloadCount = 0;
		this.onloadTimeOut = 500;
		this.runOnload();
	}
}
divOsClass.prototype.runOnload = function() {
	this.onloadCount++;
   if((this.onloadTimeOut>90000 || this.onloadCount > 180) && this.onloadFuncs.length==this.onloadPoint){
		this.onloadStart = 0;
		return;
	}
   if(this.onloadFuncs.length==0 || (this.onloadFuncs.length==this.onloadPoint && this.onloadFailFuncs.length==0)){
      this.onloadTimeOut = this.onloadTimeOut+1000;
      setTimeout(this.name+".runOnload()",this.onloadTimeOut);
      return;
   }
	var tmp_onloadFuncs = this.onloadFailFuncs;
	this.onloadFailFuncs = new Array();
   for(var j = 0; j < tmp_onloadFuncs.length; j++) {
      try { eval(tmp_onloadFuncs[j]);}
      catch(e) {
   		this.onloadFailFuncs.push(tmp_onloadFuncs[j]);
		}
	}
   for(var i = this.onloadPoint; i < this.onloadFuncs.length; i++) {
      try { eval(this.onloadFuncs[i]);}
      catch(e) {
   		this.onloadFailFuncs.push(this.onloadFuncs[i]);
      }
   }
	
   this.onloadPoint=i;
	if(this.onloadFailFuncs.length>0)
		this.onloadTimeOut = 500;
	else
   	this.onloadTimeOut = this.onloadTimeOut+1000;
   setTimeout(this.name+".runOnload()",this.onloadTimeOut);
}

