/**
*	class of Sajax operation .
*
*	This class will provide the interface of  all the sajax operation. Such like sending sajax request,Dealing return data.
*
*	@author   Carl carl@heimavista.com , Ryan ryan@heimavista.com , ..
*	@version  $Id: ajaxedit.js,v 1.7.2.1 2009/09/22 10:54:39 carl Exp $
*/

/**
*  Initialize column parameters.
*
*  @param string p_field
*  @param string p_name
*  @param string p_type
*  @return object
*/
function Column(p_field,p_name,p_type) {
	this.Field = p_field;
	this.Name = p_name;
	this.Type = p_type;
	return this;
}

/**
*  Initialize attachUploadField parameters.
*
*  @param string p_field
*  @param string p_name
*  @param string p_type
*  @param string p_kind
*  @return object
*/
function AttachUploadField(p_field,p_name,p_type,p_kind){
	this.Field = p_field;
	this.Name = p_name;
	this.Type = p_type;
	this.Kind = p_kind;
	return this;
}

/**
*  Initialize attachFile parameters.
*
*  @param string p_name
*  @param string p_tmpname
*  @param string p_field
*  @return object
*/
function AttachFile(p_name,p_tmpname,p_field){
	this.RealName = p_name;
	this.TmpName = p_tmpname;
	this.FieldName = p_field;
	return this;
}

/**
*  Constructor function,Initialize the parameters.
*
*
*  @param string p_name
*  @return null
*/
function AjaxEdit(p_name) {
	this.name = p_name;
	this.TagName = p_name;
	this.sajaxIO = new sajaxIO();
	this.Column = new Array();
	this.AttachUploadField = new Array();
	this.addAttach = new Array();
	this.AttachLength = 0;
	this.AttachTmpLength = 0;
	this.ColumnLength = 0;
	this.DispCheckBox = false;
	this.DispUpdate = false;
	this.ListForm = this.TagName+'_ListForm';
	this.ListDiv = this.TagName+'_ListDiv';
	this.AddForm = this.TagName+'_AddForm';
	this.RefreshStat=0;
	this.RowValue=new Array(15);
	this.RowNewValue=new Array(15);
	this.RowText=new Array(15);
	this.PicUrl = 'pictures';
	this.msgDiv = this.TagName+'_msg';
	this.silent = 0;
	this.QuickEditSave = "Save";
	this.QuickEditCancel = "Cancel";
	this.QuickEditButton = "Quick Edit";
	this.FullEditButton = "Full Edit";
	this.noteMsgClassName="note";
	this.successMsgClassName="msg success";
	this.failedMsgClassName="msg failure";
	this.waitWord="Please wait...";
	this.NoChk="Select items Please!";
	this.ConfirmDelete="Are you sure to delete!";
	this.Browser = divOsClass.prototype.getBrowser();
	this.CheckData = true;
	//this.msg[img] = name + " please upload image file ";
	//this.msg[zip] = name + " please upload zip file ";
	//this.initConst();
} 
AjaxEdit.prototype._DEBUG_Display_z = 1
AjaxEdit.prototype._DEBUG_Display_content = 10;
AjaxEdit.prototype._DEBUG_Display_Retcode = 100;
AjaxEdit.prototype._DEBUG_Display_xxx = 1000;

AjaxEdit.prototype.dispField = new Array();

/**
*  set the check form data flag 
*
*  @param boolean flag
*/
AjaxEdit.prototype.setCheckData = function(flag){
	this.CheckData = flag;
}

/**
*  set the target page which sajax parameters to submit.
*
*  @param string p_uri
*/
AjaxEdit.prototype.setUri = function(p_uri){
	this.sajaxUri = p_uri;
}
/**
*  set the target page which sajax parameters to submit.
*
*  @param string p_uri
*/
AjaxEdit.prototype.setTmpUri = function(p_uri){
	this.backupUri = this.sajaxUri ;
	this.sajaxUri = p_uri;
}

/**
*  set the target page which sajax parameters to submit.
*
*  @param string p_uri
*/
AjaxEdit.prototype.restoreUri = function(){
	if(this.backupUri)
		this.sajaxUri = this.backupUri;
}


/**
*  set the name of List item Form (generally in x_ files)
*
*  default: TagName+'_ListForm'
*
*  @param string p_form
*  @return null
*/
AjaxEdit.prototype.setListForm = function(p_form) {
	this.ListForm = p_form;
}

/**
*  set the name of List item Div
*
*  default: TagName+'_ListDiv'
*
*  @param string p_div
*  @return null
*/
AjaxEdit.prototype.setListDiv = function(p_div) {
	this.ListDiv = p_div;
}

/**
*  set the name of Add Form
*
*  default: TagName+'_AddForm'
*
*  @param string p_form
*  @return null
*/
AjaxEdit.prototype.setAddForm = function(p_form) {
	this.AddForm = p_form;
}

/**
*  set the Url of pictures
*
*  default: 'pictures'
*
*  @param string p_form
*  @return null
*/
AjaxEdit.prototype.setPicUrl = function(p_url) {
	this.PicUrl = p_url;
}

/**
*  set other Variable in AjaxEdit class
*
*
*  @param string p_word
*  @param string p_info
*  @return null
*/
AjaxEdit.prototype.setVar = function(p_word,p_info) {
	eval("this."+p_word+"='"+p_info+"'");
}

/**
*  set message Div's style class name
*
*  @param p_div
*  @param p_noteClass , default tagname+'_msg'
*  @param p_succClass , default "msg success"
*  @param p_failedClass , default "msg failure"
*/
AjaxEdit.prototype.setMsgDiv = function(p_div,p_noteClass,p_succClass,p_failedClass) {
	this.msgDiv = p_div;
	if(typeof p_noteClass!="undefined")
		this.noteMsgClassName=p_noteClass;
	if(typeof p_succClass!="undefined")
		this.successMsgClassName=p_succClass;
	if(typeof p_succClass!="undefined")
		this.failedMsgClassName=p_failedClass;
}

/**
*  set Debug mode
*
*  When call this function without any param,It will show in the debug window both sajax sending XML and sajax Return content.
*  param p_debug detail:
*  11 => all the sajax return in text mode,which html source code will be showed.
*  12 => all the sajax return in html mode.
*  'sending' => show the sajax sending XML content.
*  'close' => shutdown this debug function.
*
*  @param string p_debug
*  @return null
*/
AjaxEdit.prototype.setDebug = function(p_debug) {
	if (!p_debug) {
		AjaxEdit.prototype.DebugNo = 11;
		this.sajaxIO.setDebug();
		return;
	}
	if (p_debug=='send') {
		this.sajaxIO.setDebug();
		return;
	}
	if (p_debug=='close') {
		AjaxEdit.prototype.DebugNo = 0;
		return;
	}
	AjaxEdit.prototype.DebugNo = p_debug;
	
}

/**
*  add list column
*
*  
*  @param string p_field
*  @param string p_name
*  @param string p_type
*  @return null
*/
AjaxEdit.prototype.addListColumn = function(p_field,p_name,p_type) {
	this.Column[this.ColumnLength++] = new Column(p_field,p_name,p_type);
}

/**
*  set checkbox state
*
*  @param string p_field
*  @return null
*/
AjaxEdit.prototype.setCheckBoxStat = function(p_field) {
	this.DispCheckBox = true;
	this.PrimaryKey = p_field;
}

/**
*  set updateField
*
*  @param string p_opjs
*  @return null
*/
AjaxEdit.prototype.setUpdateField = function(p_opjs) {
	this.DispUpdate = true;
	if(p_opjs)
		this.UpdateOpJs = p_opjs;
	else this.UpdateOpJs = '';
}

/**
* get value in a string;

* @param string p_form
* @param string p_othervalue: parse this string to get value;
* @return array the value
*/
AjaxEdit.prototype.getValue = function(p_form,p_othervalue) {  
	//Field1=Value&Field2=Value 
	var form = p_form;
	this.form=p_form;
//alert("form:"+form);
	var value = new Array();
	for(i=0;i<this.ColumnLength;i++){
		if(this.Column[i].Type !="b"){
//alert(this.Column[i].Field);
			//value[this.Column[i].Field] = eval(form+".TB_"+this.Column[i].Field).value; 
//alert("value:"+"document.getElementById('"+form+"').TB_"+this.Column[i].Field); 
		}
	}
	other_arr=p_othervalue.split("&");
	for(j=0;j<other_arr.length;j++){
		other_string=other_arr[j].split("=");
		value[other_string[0]] = other_string[1];
	}
	return value;
}

/**
* add line

* @param array p_value
* @param string p_line
* @return null
*/
AjaxEdit.prototype.addLine = function(p_value,p_line) {  // p_value[field] = value;
	var i;
	var primary = p_value[this.PrimaryKey];	
	var form = this.form;
   if(p_line=='1')
      var newobj = document.getElementById('Add_line').insertRow(p_line);
   else var newobj = document.getElementById('Add_line').insertRow();
   newobj.className="addline";
   newobj.id = this.name + "-+-" + primary+"_tr";	

   if(this.DispUpdate) {
      this.dispField["UpdateColumn"](newobj,p_value,this.UpdateOpJs);
   }
	for(i=0;i<this.ColumnLength;i++) {
		var field = this.Column[i].Field;
		var val = p_value[this.Column[i].Field];

		if(AjaxEdit.prototype.dispField[field]) {
			AjaxEdit.prototype.dispField[field](p_value);
			continue;
		}
	
		if(val=="") {
			str1="var cell"+i+" =newobj.insertCell(null);\n";
			str2="cell"+i+".innerHTML='&nbsp;'\n";
			str3="";
		}
      else if(this.Column[i].Type=='b'){
         str1="var cell"+i+" =newobj.insertCell(null);\n";
         str2="cell"+i+".innerHTML='&nbsp;'\n";
			str3="";
      }
		else if(this.Column[i].Type=='v'){
      	str1="var cell"+i+" =newobj.insertCell(null)\n";
      	str2="cell"+i+".innerHTML=\""+val+"\"\n";
			str3="cell"+i+".id='"+primary+"-+-"+field+"-+-td'";
		}
		else if(this.Column[i].Type=='e'){
			str1="var cell"+i+" =newobj.insertCell(null)\n";
			str2="cell"+i+".innerHTML='<a href=mailto:"+val+">"+val+"</a>'\n";
			str3="cell"+i+".id='"+primary+"-+-"+field+"-+-td'";
		}
		eval(str1);
		eval(str2);
		eval(str3);
	}

   if(this.DispCheckBox) {
      this.dispField["CheckBox"](newobj,p_value);
   }
	newobj.style.display="none";
}

AjaxEdit.prototype.dispField["UpdateColumn"] = function(p_obj,p_value,p_js) {

	var nbr = p_value[this.PrimaryKey];
   var cell0=p_obj.insertCell(null);

   //this.UpdateOpJs="doit('ef','Form1','===key==')";
   Js=p_js.replace("===key===",nbr);
	
   cell0.innerHTML="<a href=\"javascript:void(0)\" onclick=\"javascript:void("+Js+")\"><img src='"+this.PicUrl+"/edit.gif' border=0></a>";
	cell0.align="middle";
}

/**
*  display checkBox
*
*	@param object p_obj
*  @param string p_value
*  @return null
*/
AjaxEdit.prototype.dispField["CheckBox"] = function(p_obj,p_value) {
	var primary = p_value[this.PrimaryKey];	
	var cell99=p_obj.insertCell(null);
	cell99.innerHTML="<input type='checkbox' name='Chk[]' id='"+primary+"_input' value='"+primary+"' onclick='changeStyle(this)'>";
}

/**
*  show message in a defined div which will show different message in different styles.
*
*  when p_tyle is "Success",the style class of message div will be this.successMsgClassName which defined in setMsgDiv function;
*
*  @param string p_word
*  @param string p_info
*  @return null
*/
AjaxEdit.prototype.showMsg = function(content,p_type) {
	if(typeof p_type == "undefined") p_type="note";	
   var obj = document.getElementById(this.msgDiv);
	if(obj==null) return ;
	obj.style.display = "block";
	obj.innerHTML = content;
	if(p_type=='Success')
		obj.className=this.successMsgClassName;
	else if(p_type=='Failure')
		obj.className=this.failedMsgClassName;
	else 
		obj.className=this.noteMsgClassName;
}

/**
*  hide the message div
*
*  @return null
*/
AjaxEdit.prototype.hideMsg = function() {
	try{
   	var obj = document.getElementById(this.msgDiv);
		if (typeof obj !="undefined" && obj) obj.style.display = "none";
	}catch(e){}
}

/**
*  remove node by id
*
*
*  @param string p_tag
*  @return null
*/
AjaxEdit.prototype.removeTag = function(p_tag) {
	if(typeof p_tag=="object")	obj.parentNode.removeChild(p_tag); 
	else {
		if (document.getElementById(p_tag))  document.getElementById(p_tag).parentNode.removeChild(document.getElementById(p_tag));
	}
}

/**
*  show div by id or Object
*
*
*  @param string p_div
*  @return null
*/
AjaxEdit.prototype.showDiv = function (p_div) {
	if(typeof p_div=="object")	p_div.style.display='block';
	else {
		if(document.getElementById(p_div)) document.getElementById(p_div).style.display='block';
	}
}

/**
*  hide div by id or Object
*
*
*  @param string p_div
*  @return null
*/
AjaxEdit.prototype.hideDiv = function (p_div) {
	if(typeof p_div=="object")	p_div.style.display='none';
	else {
		if(document.getElementById(p_div)) document.getElementById(p_div).style.display='none';
	}
}

/**
*  hide div by id or Object
*
*
*  @param string p_div
*  @return null
*/
AjaxEdit.prototype.showDiv = function (p_div) {
	if(typeof p_div=="object")	p_div.style.display='inline';
	else {
		if(document.getElementById(p_div)) document.getElementById(p_div).style.display='inline';
	}
}

/**
*  delete line
*
*
*  @param string obj
*  @return null
*/
AjaxEdit.prototype.delLine = function(obj){
   var arr=obj.split(",");
   for(i=0;i<arr.length-1;i++){
		if(arr[i])
      document.getElementById(arr[i]+'_tr').parentElement.deleteRow(document.getElementById(arr[i]+'_tr').rowIndex-1);
   }
	if(arr.length==1)
		 document.getElementById(arr[0]+'_tr').parentElement.deleteRow(document.getElementById(arr[0]+'_tr').rowIndex-1);
}
 
AjaxEdit.prototype.rightClick = function(p_nbr,a,p_obj){
   var menu=document.getElementById("menu");
      a = window.event || a;
      if(!a.pageX)a.pageX=a.clientX;
      if(!a.pageY)a.pageY=a.clientY;
      menu.style.left = a.pageX + "px";
      menu.style.top  = a.pageY + "px";
      menu.style.display = "block";
      return false;
   return false;	
}

/**
*   select checkbox when double click a row;

*   @param string p_nbr the nbr of the check box;
*   @return null.
*/
AjaxEdit.prototype.selectCheckbox = function(p_nbr){
   var chkBox=p_nbr+"_input";
   document.getElementById(chkBox).checked="true";
}

AjaxEdit.prototype.rightClickChild = function(str,a,obj){
   var menuChild=document.getElementById("menuChild");
   menuChild.style.display = "block";
   return false;		
}

/**
*   hide menuChild
*  
*   @return null.
*/
AjaxEdit.prototype.hideMenuChild = function(){
   document.getElementById('menuChild').style.display='none';
}

/**
*  inner function ,get brower height after scroll
*
*  @access private
*  @return null
*/
AjaxEdit.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;
}

/**
*  add line
*
*  @param string p_form
*  @param string p_posi
*  @return null
*/
AjaxEdit.prototype.preAddLine = function(p_form,p_posi) {
	if (this.isNull(p_form)) p_form=this.AddForm;
	if (this.isNull(p_posi)) p_posi="";
  	this.addLine(this.getValue(p_form,p_posi));
}

/**
*  deal sajax return XML content
*
*  There are some preset methods which defined by the value of sajaxDivId,such as:
*  _PopUp => Pop up a div-window to display content of sajaxReturn;
*  _List => Replace sajaxReturn to List Div (default:Tagname+"_ListDiv");
*  _Msg => Show sajaxReturn in message Div (default:Tagname+"_msg");
*  _QuickEdit => When there is a Quick Edit operation's return,It will refresh the edited row;
*
*
*  @return null
*/
AjaxEdit.prototype.callBack = function(z) {
   var scrollHeight=AjaxEdit.prototype.getScrollTop() + 150;
	var scrollWidth=document.body.clientWidth/2-150;
   if (AjaxEdit.prototype.DebugNo==11) divOs.popWindow("===DEBUG===","Close=1;width:90%;height:95%;top:30",AjaxEdit.prototype.clearHtml(z));
   if (AjaxEdit.prototype.DebugNo==12) divOs.popWindow("===DEBUG===","Close=1;width:90%;height:95%;top:30",z);
	try{
   	Res= sajaxIO.prototype.getMsg(z);
		var TagId = Res.TagId;
		var editor = eval(TagId+"_editor");	

		//var trId = Res.Other+"_tr";
   	arr=Res.Content;
   	if (AjaxEdit.prototype.DebugNo==21) divOs.popWindow("===DEBUG===","Close=1;width:90%;height:95%;top:30",AjaxEdit.prototype.clearHtml(arr));
   	if (AjaxEdit.prototype.DebugNo==22) divOs.popWindow("===DEBUG===","Close=1;width:90%;height:95%;top:30",arr);
   	if (AjaxEdit.prototype.DebugNo==31) divOs.popWindow("===DEBUG===","Close=1",Res.RetCode);
	
		//if (editor.silent == 0) divOs.closePopWindow('sending');
		if (editor.silent == 0) divOs.closeWaitingWindow('sending');

   	if(Res.DivId=="_List"){
			divOs.setInnerHTML(document.getElementById(editor.ListDiv),Res.Content);
		}
   	else if(Res.DivId=="_PopUp"){
			if (!Res.RetCode) Res.RetCode="Pop Up Title";
			var infoArr=Res.Other.split('=o=');
			var p_style=infoArr[0];
			var p_div=infoArr[1];
			if (!p_style) p_style="middle";
			if (p_style=="large") p_style="width:95%;height:90%;top:20;Close:1";
			if (p_style=="middle") p_style="width:420;height:300;top:100;Close:1";
			if (p_style=="small") p_style="width:280;height:150;top:150;Close:1";
			divOs.popWindow(Res.RetCode,p_style,Res.Content,p_div);
			//var dispdiv = editor.createDivNode(Res.Content,"Close=1");
			//return;
		}
   	else if(Res.DivId=="_Msg"){
			editor.showMsg(Res.Content,Res.RetCode) ;   //RetCode="Success","Failure" 
		}
   	else if(Res.DivId=="_QuickEdit"){
			editor.refreshEditRow(editor.OperateObj,1);
		}
   	else if(Res.DivId.indexOf("(")>-1){					  //If define a callback function in DivId Tag , call the callback function .
			var fun = Res.DivId.replace("()","(Res)");  //a() ; a(Res);
			eval(fun);
		}
		else {
			if(Res.DivId && document.getElementById(Res.DivId))
				document.getElementById(Res.DivId).innerHTML = Res.Content;
		}
		editor.hideMsg();
		if(Res.JsFunction) eval(Res.JsFunction);
	}
   catch(e) {
      divOs.popWindow("ERROR FOUND!","Close=1;width:50%;height:50%;top:30px",z);
      try{
         divOs.closeWaitingWindow('sending');
      }catch(e) {}
   }

}

/**
*   attach upload

*   @param string p_successJs
*   @param string p_faildJs
*   @param string p_form
*   @return null.
*/
AjaxEdit.prototype.attachUpload = function(p_successJs,p_faildJs,p_form) {
   var FieldInfo='';
   for(i=0;i<this.AttachLength;i++){
      FieldInfo +=this.AttachUploadField[i]["Name"]+"@@"+this.AttachUploadField[i]["Type"]+"@@"+this.AttachUploadField[i]["Kind"]+"&&";
   }
	this.successJs = p_successJs;
	this.faildJs = p_faildJs;
	this.UploadForm = p_form;
   var obj = eval("document."+this.UploadForm+".AttachUploadFildHidden");
   obj.value=FieldInfo;
}

/**
*   save attachUploadField information;

*   @param string p_form
*   @param string p_name
*   @param string p_type
*   @param string p_kind
*   @return null.
*/
AjaxEdit.prototype.attachUploadFild = function(p_field,p_name,p_type,p_kind){
	this.AttachUploadField[this.AttachLength++] = new AttachUploadField(p_field,p_name,p_type,p_kind);	
}

AjaxEdit.prototype.UploadSuccess = function(){
	var xmlVal=AjaxEdit.prototype.attachXml();
	var obj = eval("document."+this.UploadForm+".__AttachList");		
	obj.value=xmlVal;
	if(this.successJs) eval(this.successJs); 
	else {
		eval("document."+this.UploadForm+".submit()");
	}
}

AjaxEdit.prototype.UploadFaild = function(p_type,p_field){
	if(p_type==1) {
		for(i=0;i<this.AttachLength;i++){
			if(this.AttachUploadField[i]["Name"]==p_field){
				this.FaildField=this.AttachUploadField[i]["Field"];
				break;
			}
		}
   document.getElementById('msgbox2').style.display='block';
   document.getElementById('msgbox2').innerHTML=msghead +this.FaildField+":"+this.faildJs+ msgtail;
	}
	if(p_type==2) {
	}
}

/**
*   add attach file

*   @param string p_name
*   @param string p_tmpname
*   @param string p_field
*   @return null.
*/
AjaxEdit.prototype.addAttachFile = function(p_name,p_tmpname,p_field){
	this.addAttach[this.AttachTmpLength++] = new AttachFile(p_name,p_tmpname,p_field);
}

/**
*   create xml of attach file

*   @return string xmlValue
*/
AjaxEdit.prototype.attachXml = function(){
	var xmlValue = "<?xml version='1.0'?><Input>";
   for(var i=0;i<this.AttachTmpLength;i++){
   	xmlValue += "<F><Name>"+this.addAttach[i]["RealName"]+"</Name><Tmp>"+this.addAttach[i]["TmpName"]+"</Tmp><Field>"+this.addAttach[i]["FieldName"]+"</Field></F>";
   }
   xmlValue += "</Input>";
   return xmlValue;

}

/**
*  clear all the text input content in specified form.
*
*
*  @param string p_form
*  @return null
*/
AjaxEdit.prototype.clearForm = function (p_form){
   var formObj=eval("document."+p_form);
   var cols = formObj.getElementsByTagName("INPUT");
   var length = cols.length;
   for (j=0;j<length;j++){
		if (cols[j].getAttribute("type")=="text") cols[j].value="";
   }
}

/**
*  focus specified input in specified form.
*
*
*  @param string p_form
*  @return null
*/
AjaxEdit.prototype.focusForm = function (p_form,p_focus){

	if (!p_focus) p_focus = "firstText";
	if (!p_form) return false;
   var formObj=eval("document."+p_form);
	if (typeof formObj =='undefined') return false;
   var cols = formObj.getElementsByTagName("INPUT");
   var length = cols.length;
   for (j=0;j<length;j++){
		if (cols[j].getAttribute("type")=="text") {
			if (p_focus=="firstText") {cols[j].focus();return;}
   	}
	}
}

/**
*  convert html string into text string,html code will be fully display.
*
*
*  @param string text
*  @return string text
*/
AjaxEdit.prototype.clearHtml = function(text){
	if (typeof( text ) != "string") text = text.toString() ;
   text = text.replace(/&/g, "&amp;") ;
   text = text.replace(/"/g, "&quot;") ;
   text = text.replace(/</g, "&lt;") ;
   text = text.replace(/>/g, "&gt;") ;
   text = text.replace(/'/g, "&#39;") ;
   text = text.replace(/\n/g, "<br />") ;
   return text ;
}

/**
*  convert quot(") and apos(') to unicode string.
*
*
*  @param string text
*  @return string text
*/
AjaxEdit.prototype.clearQuote = function(text){
	if (typeof( text ) != "string") text = text.toString() ;
   text = text.replace(/"/g, "&quot;") ;
   text = text.replace(/'/g, "&apos;") ;
   return text ;
}

/**
*  convert quot and apos's unicode string to general style like "/' .
*
*
*  @param string text
*  @return string text
*/
AjaxEdit.prototype.unClearQuote = function(text){
	if (typeof( text ) != "string") text = text.toString() ;
   text = text.replace(/&quot;/g, "\"") ;
   text = text.replace(/&apos;/g, "'") ;
   return text ;
}

/**
*  When you click the title of list, it will actived this method to do a sajax change sort submit.
*
*
*  @param string p_form
*  @param string p_sort
*  @param string p_callBack
*  @return null
*/
AjaxEdit.prototype.chgSort = function(p_form,p_sort,p_callBack){
	if (AjaxEdit.prototype.isNull(p_form)) p_form=this.ListForm;
	if (AjaxEdit.prototype.isNull(p_callBack)) p_callBack=this.name+"_editor.callBack";
   var frm = eval("document."+p_form);
   if(this.trim(p_sort) == this.trim(frm.Sort.value)) frm.Direct.value = (-1)*frm.Direct.value;
   else frm.Sort.value = p_sort;
   frm.Gopage.value = 1;
   frm.Begin.value = 0;
	//add this when change search list sort.
   var refreshOpStat = this.RefreshStat;
   if(refreshOpStat) var refreshOp="Op=search";
   else var refreshOp="";
   this.sajaxSubmit(refreshOp,p_form,p_callBack);
}

/**
*  send the parameters to target page(set in setUri method) in sajax mode.
*
*
*  @param string p_op
*  @param string p_form
*  @param string p_callBack
*  @param integer p_silent, when this is not null, it will send the info without any prompt.
*  @return null.
*/
AjaxEdit.prototype.sajaxSubmit = function(p_op,p_form,p_callback,p_silent){
   if (this.CheckData && p_form) {
		if (!this.checkData(p_form)) return false;
	}
	if(p_op.indexOf("=")>=0) p_op+="&TagName="+this.TagName;
	else p_op = "TagName="+this.TagName;
   this.sajaxIO.sajaxSubmit(p_op,p_form,p_callback,'sajaxSubmit',this.sajaxUri);
   var scrollHeight= 180;
	var scrollWidth=document.body.clientWidth/2-100;
	this.silent = 0;
	//if (!p_silent) divOs.openAlertWindow(divOsClass.prototype.AlertWait,divOs.waitWord,"Static=1;Close=1;height=80;width:200;Static:0;top="+scrollHeight,"sending","NoButton");
	if (!p_silent) divOs.openWaitingWindow(divOs.waitWord,"sending");
	else this.silent=1;
	this.editing = false;
}

/**
*  check if input in null;

*  @param p_val;
*  @return bool
*/
AjaxEdit.prototype.isNull = function(p_val){
	var isnull = false;
	if (p_val==null || p_val=="null" || p_val=="")  isnull = true;
	return isnull;
}

/**
*  When you click the Quick Edit button, It will drive this method to convert row value into form, and provide sajax update button to submit form content.
*
*
*  @param Object obj, Quick Edit button object
*  @param String p_form, List Form
*  @param String p_callBack
*  @return null.
*/
AjaxEdit.prototype.editRow = function(obj,p_form,p_callBack){

	if (this.editing) return;
	if (this.isNull(p_form)) p_form=this.ListForm;
	if (this.isNull(p_callBack)) p_callBack=this.name+"_editor.callBack";
   var par=obj.parentNode.parentNode;
	//par.className="row_hover";
   var cols = par.getElementsByTagName("TD");
   var length = cols.length;
   for (j=0;j<length-1;j++){
		if (this.isNull(cols[j].getAttribute("id"))) continue;
      var myid=cols[j].getAttribute("id");
      var idarr=myid.split("-+-");
   	var sendid="TB_"+idarr[1];
		if(cols[j].innerHTML=="&nbsp;"){
     		this.RowValue[j]="&nbsp;";
     		this.RowText[j]="&nbsp;";
		}
     	else {
			this.RowValue[j]=escape(this.clearQuote(this.trim(cols[j].innerHTML)));
			if (this.Browser=="IE") {
				if (idarr[3] == 'select') this.RowText[j]=escape(this.clearQuote(this.trim(cols[j].childNodes[0].innerText)));
				else this.RowText[j]=escape(this.clearQuote(this.trim(cols[j].innerText)));
			}
			else {
				if (idarr[3] == 'select') this.RowText[j]=escape(this.clearQuote(this.trim(cols[j].childNodes[0].textContent)));
				else this.RowText[j]=escape(this.clearQuote(this.trim(cols[j].textContent)));

			}
		}
		if (idarr[3] == 'select'){
			document.getElementById(this.name+'_selectField_'+idarr[0]).style.display="block";
			document.getElementById(this.name+'_originValue_'+idarr[0]).style.display="none";
			document.getElementById(this.name+'_select_'+idarr[0]).name=sendid;
			}
		else {
			if (idarr[2] == 'boolean') {
				var chked="";
				if (unescape(this.trim(this.RowText[j]))==1) chked = " checked=\"checked\" ";
     			cols[j].innerHTML="<input type=\"checkbox\" class=\"checkbox\" id=\""+idarr[1]+"\" "+chked+" name=\""+sendid+"\"/ value=\"1\">";
			
			}else {
     			//cols[j].innerHTML="<input type=\"text\" id=\""+idarr[1]+"\" name=\""+sendid+"\" style=\"width:90%;\" value=\""+unescape(this.trim(this.RowText[j]))+"\">";
				var showCal ="";
				var inputWidth ="90%";
				if (idarr[2] == 'date') {
					showCal = "<input class=\"checkbox\" onclick=\"showCalendar(document."+this.ListForm+"."+sendid+",this);return false\" type=\"image\" alt=\"Calendar\" src=\""+this.PicUrl+"/cal.gif\" align=\"absMiddle\" \/>";
					inputWidth = "80%";
				}
     			cols[j].innerHTML="<input type=\"text\" class=\"inputtext\" id=\""+idarr[1]+"\" name=\""+sendid+"\" style=\"width:"+inputWidth+";\" value=\""+unescape(this.trim(this.RowText[j]))+"\">"+showCal;
			}
		}
	}
	this.OperationInnerHtml = escape(obj.parentNode.innerHTML);
	this.focusForm(this.name+"_ListForm");
	if (idarr) obj.parentNode.innerHTML="<a href='javascript:void(null)' onclick=\"if("+this.name+"_editor.checkData('"+p_form+"')){"+this.name+"_editor.saveNewValue(this);"+this.name+"_editor.sajaxSubmit('Op=edit&keyId="+idarr[0]+"','"+p_form+"','"+p_callBack+"');"+this.name+"_editor.editing=false;}\"><img border='0' src='"+this.PicUrl+"/save.gif' alt='"+this.QuickEditSave+"' /></a><a href='javascript:void(null)' onclick=\""+this.name+"_editor.refreshEditRow(this)\"><img border='0' src='"+this.PicUrl+"/close.gif' alt=\""+this.QuickEditCancel+"\" /></a>";
	this.editing = true;
}

/**
*  use in Quick Edit mode,it use to submit quick edit form values.
*
*
*  @param Object obj, quick edit update button
*  @return null;
*/
AjaxEdit.prototype.saveNewValue = function(obj){
   var par=obj.parentNode.parentNode;
	this.OperateObj = obj;
   var cols = par.getElementsByTagName("TD");
   var length = cols.length;
   for (j=0;j<length-1;j++){
		if (this.isNull(cols[j].getAttribute("id"))) continue;
      var myid=cols[j].getAttribute("id");
      var idarr=myid.split("-+-");
		if (idarr[3] == 'select'){
			var SelObj = cols[j].getElementsByTagName("SELECT");
			var SelText = SelObj[0].options[SelObj[0].selectedIndex].text;
			this.RowNewValue[j] = escape(this.clearQuote(SelText));
		}
		else this.RowNewValue[j] = escape(this.clearQuote(cols[j].childNodes[0].value));
   }
}

/**
*  Refresh the list.
*
*
*  @return null
*/
AjaxEdit.prototype.refreshList = function(str){
	var formName = this.name+"_searchTmp";
	var obj = eval("document."+formName);
	p_form = this.name+"_searchForm";
	if(obj && obj.parentNode.style.display !="none") p_form =this.name+"_searchTmp";
	var refreshOpStat = this.RefreshStat;
	if(refreshOpStat && str) var refreshOp="Op=search&refStr="+str;
	else if(refreshOpStat && !str) var refreshOp="Op=search";
	else var refreshOp="";
	this.sajaxSubmit(refreshOp,p_form,this.callBack);
//	this.sajaxSubmit('',p_form,this.callBack);
}

/*
*   check if datas in the form validate;

*   @param string p_form name of the form;
*   @param string p_prefix
*   @return bool
*/
AjaxEdit.prototype.checkData = function(p_form,p_prefix){
	if (!p_prefix) p_prefix = "TB_";
	var FormData ;
	try {
		FormData = eval(this.name+"_FormData");
	}catch(e){return true;}
   var checked = FormData.datavalid(p_form,p_prefix);
   if (checked == false) return false;
	return true;
}

/**
*  inner method.replace row value by newest input value.
*
*
*  @access private
*  @return null
*/
AjaxEdit.prototype.refreshEditRow = function(obj,p_newValue){
	this.editing = false;
   var par=obj.parentNode.parentNode;
	if (p_newValue) par.className="row_operate";
	//else par.className="tr_normal";
   var cols = par.getElementsByTagName("TD");
   var length = cols.length;
   for (j=0;j<length-1;j++){
		if (this.isNull(cols[j].getAttribute("id"))) continue;
      var myid=cols[j].getAttribute("id");
      var idarr=myid.split("-+-");
		if (p_newValue) {
			if (idarr[3]=="select") {
				cols[j].innerHTML=this.unClearQuote(unescape(this.RowValue[j].replace(this.RowText[j]+"%3C\/span%3E",this.RowNewValue[j]+"%3C\/span%3E")));
			}
			else cols[j].innerHTML=this.unClearQuote(unescape(this.RowValue[j].replace(eval("/"+this.RowText[j]+"/g"),this.RowNewValue[j])));
		}
		else cols[j].innerHTML=this.unClearQuote(unescape(this.RowValue[j]));
   }
 obj.parentNode.innerHTML=unescape(this.OperationInnerHtml);
}

/**
*  Submit sajax infomation when page change
*
*
*  @param p_page
*  @return null
*/
AjaxEdit.prototype.view = function(p_page){
	eval("document."+this.ListForm+".Gopage.value="+p_page);
	this.sajaxSubmit('',this.ListForm,this.callBack);
}

/**
*  Submit sajax infomation when search
*
*
*  @param p_form
*  @return null
*/
AjaxEdit.prototype.search = function(p_form){
	if (!p_form) p_form = this.name+"_searchForm";
	this.RefreshStat =1;
	this.sajaxSubmit('Op=search',p_form,this.callBack);
}

/**
*  Pop up a window to show item detail
*
*
*  @param p_seq
*  @return null
*/
AjaxEdit.prototype.showDetail = function(p_seq,p_noPop,p_param){
	if (!p_seq) return false;
	if (!p_param) p_param="";
	if (p_noPop)
		this.sajaxSubmit('Op=detail&NoPop=1'+p_param+'&seq='+p_seq,'',this.callBack);
	else
		this.sajaxSubmit('Op=detail'+p_param+'&seq='+p_seq,'',this.callBack);
}

/**
*  toolbar function update
*
*
*  @param p_form
*  @return null
*/
AjaxEdit.prototype._update = function(p_form){
	if (!p_form) p_form = this.ListForm;
	if (this.checkData(p_form)){
		this.sajaxSubmit('Op=update',p_form,this.callBack);
	}
}

/**
*  toolbar function moveTo
*
*
*  @param p_key
*  @return null
*/
AjaxEdit.prototype.moveTo = function(p_key,p_div){
	if(typeof(p_div)=="undefined") p_div="";
	if (!p_key) return false;
   if (!chkSel(this.ListForm,'Chk[]')) {
      alert(this.NoChk);
      return false;
   }
	this.sajaxSubmit('Op=moveTo&moveDiv='+p_div+'&moveKey='+p_key,this.ListForm,this.callBack);
}
/**
*	toolbar function delete 
*
*
*	@param p_key
*	@return null
*/
AjaxEdit.prototype._delete = function(p_id) {
	if (p_id) {
   	if (confirm(this.ConfirmDelete)) this.sajaxSubmit('Op=delete&Chk='+p_id,'',this.callBack); 
		return;
	}
   if (!chkSel(this.ListForm,'Chk[]')) {
      alert(this.NoChk);
      return false;
   }
   if (confirm(this.ConfirmDelete)) this.sajaxSubmit('Op=delete',this.ListForm,this.callBack); 
}
AjaxEdit.prototype.deleteOne = function(p_id,p_extparam) {
   if (p_id) {
		if(p_extparam) p_extparam="&"+p_extparam;
		else p_extparam="";
      if (confirm(this.ConfirmDelete)) this.sajaxSubmit('Op=deleteOne&Chk='+p_id+p_extparam,'',this.callBack);
   }
}
/**
*	Pop up a Quick add Div (name:tagname+"_addDiv"; 
*
*
*	@param p_title
*	@param p_style
*	@return null
*/
AjaxEdit.prototype.showAdd = function(p_title,p_style,p_div) {
	if (!p_title) p_title="Quick Add";
	if (!p_style) p_style="middle";
	if (!p_div) p_div=this.name+"_popDiv";
	if (p_style=="large") p_style="width:95%;height:90%;top:20;Close:1";
	if (p_style=="middle") p_style="width:420;height:300;top:100;Close:1";
	if (p_style=="small") p_style="width:280;height:150;top:150;Close:1";
	try {	
		var Obj_addDiv = eval(this.name+"_addDiv");
	}
	catch(e){
		alert(e.message);
		return false;
	}
   divOs.popWindow(p_title,p_style,Obj_addDiv,p_div);
	this.focusForm(this.name+"_AddForm","firstText");

}
/**
*	When Pop up a Quick add Div , This method will submit the add form values.  
*
*
*	@return null
*/
AjaxEdit.prototype.saveAdd = function(p_noclose,p_prefix) {
	if (!p_prefix) p_prefix = "TB_";
	var FormData = eval(this.name+"_FormData");
   var checked = FormData.datavalid(this.name+"_AddForm",p_prefix);
   if (checked == false) return false;
   this.sajaxSubmit("Op=add",this.name+"_AddForm",this.callBack);
}
/**
*	When Pop up a full modify Div , This method will submit the modify form values.  
*
*
*	@return null
*/
AjaxEdit.prototype.saveFull = function(p_id,p_form,p_noclose,p_prefix) {
	if (!p_prefix) p_prefix = "TB_";
	var FormData = eval(this.name+"_FormData");
	if (!p_form) p_form = this.name+"_FullForm";
   var checked = FormData.datavalid(p_form,p_prefix);
   if (checked == false) return false;
   this.sajaxSubmit("FullEdit=1&Op=edit&keyId="+p_id,p_form,this.callBack);
}
AjaxEdit.prototype.createDivNode = AjaxEdit.prototype.popWindow;

/**
*  trim special characters
*
*  @param string p_str
*  @return string
*/
AjaxEdit.prototype.trim = function(p_str) {
   p_str= p_str.replace(/(^\s*)|(\s*$)/g, "");
   p_str= p_str.replace(/(^&nbsp;*)|(&nbsp;*$)/g, "");
   //return p_str.replace("&nbsp;", "");
   return p_str;
}
AjaxEdit.prototype.chkSel = function(p_chk){
   if (!chkSel(this.ListForm,p_chk)) {
      alert(this.NoChk);
      return false;
   }
	return true;
}
