/****************************************************
*   Name:  Code.js
*   Description: get all code description from js
*   Creator: rodolf.li
*   Date:   2001.09.14
*   Function: getCodeDesc( table_name, code_value)
*   Para: table_name ---  name of table storing code & desc
*         code_vaule ---  current value of code
*   Return value: desc of code
*
*   function showCodePop(table_name,hide_list,x,y)
*      在制定位置(x,y)弹出table_name相关的代码描述
*      hide_list:(0,1),是否隐藏边上的列表框
***********************************************************/
                                 
 function    getCodeDescDirect(table_name, desc_column_name, id_column_name, id_column_value, id_column_type,pNotFoundMessage ) 
{

   if ( table_name==""  ){
       window.alert("Please input table name!");
       return null;
   }
   var sTableName = table_name.toUpperCase();
	try{
		var count;
		var oXML=new ActiveXObject("Microsoft.XMLDOM");
		oXML.async=false;
		var url="/autoclaim/servlet/com.ebao.pub.util.multilang.CodeTableServlet?sAction=getCodeDesc&table_name="
+sTableName+"&desc_column_name="+desc_column_name+"&id_column_name="+id_column_name
+"&id_column_value="+id_column_value+"&id_column_type="+id_column_type;
		oXML.load(url);
  	var currNode = oXML.documentElement.selectSingleNode("Count");
		if(currNode==null){
			window.alert ("Data format error!");
			window.alert (oXML.xml);
			return null;
		}
		else{
			count= currNode.text;
		}
		if(count=="0"){
			if((pNotFoundMessage+""!="undefined")&&(pNotFoundMessage!="")&&(pNotFoundMessage!=null)){
				alert (pNotFoundMessage);
				return null;
			}
        		else{
        			alert( "Code not exist!");
        			return null;
        		}
		}
         var currNode = oXML.documentElement.selectSingleNode("CodeDesc");
		if(currNode!=null){
			return currNode.text;
		}
	}
	catch(e){
		window.alert ("Error while read code table data:"+e.description);
		return null;
	}

}




/*
 * return value desc of code
 * if desc not exist
 * return ""
 */
function getCodeDesc(table_name,code_id,pNotFoundMessage){
	if ( code_id=="" ){
       return "";
    }
   if ( table_name==""  ){
       window.alert("Please input table name!");
       return null;
   }
   var sTableName = table_name.toUpperCase();
	try{
		var count;
		var oXML=new ActiveXObject("Microsoft.XMLDOM");
		oXML.async=false;
		var url="/autoclaim/servlet/com.ebao.pub.util.multilang.CodeTableServlet?sAction=getCodeDesc&table_name="+sTableName+"&code_id="+escape(code_id);
		oXML.load(url);
  	var currNode = oXML.documentElement.selectSingleNode("Count");
		if(currNode==null){
			window.alert ("Data format error!");
			window.alert (oXML.xml);
			return null;
		}
		else{
			count= currNode.text;
		}
		if(count=="0"){
			if((pNotFoundMessage+""!="undefined")&&(pNotFoundMessage!="")&&(pNotFoundMessage!=null)){
				alert (pNotFoundMessage);
				return null;
			}
        		else{
        			alert( "Code not exist!");
        			return null;
        		}
		}
         var currNode = oXML.documentElement.selectSingleNode("CodeDesc");
		if(currNode!=null){
			return currNode.text;
		}
	}
	catch(e){
		window.alert ("Error while read code table data:"+e.description);
		return null;
	}

}
/*
 * this function is for job code desc
 */
function getJobCodeDesc( code_value ){
   return getCodeDesc("T_JOB_CATEGORY",code_value);
}

/**
 *display code description in textbox2 by textbox1's value
 *code description data is in parameter
 *@param pFirstTextboxName:the name of the first textbox, String Type
 *@param pSecondTextboxName:the name of the second textbox,String Type
 *@param pCodeArray:a two dimension array that contains the code description data
 *@param pNotFoundMessage:when input code not found in the CodeDescArray,then alert this message
 */
 function showDescWithArray(pFirstTextboxName,pSecondTextboxName,pCodeDescArray,pNotFoundMessage){
    var firstVal = document.all(pFirstTextboxName);
    var secondVal = document.all(pSecondTextboxName);
    var secondObj;
    var firstObj;
    if (firstVal.length+""!="undefined") {
      for (var i=0;i<firstVal.length;i++) {
        if (firstVal[i]==window.event.srcElement) {
          firstObj = firstVal[i];
          secondObj = secondVal[i];
        }
      }
    }else{
       firstObj = firstVal;
       secondObj=secondVal;
    }
    if(window.event.srcElement.value==""){
      secondObj.value="";
      return;
    }

    for( i=0;i<pCodeDescArray.length;i++){
//    alert(pCodeDescArray[i][0]);
    	if(window.event.srcElement.value==pCodeDescArray[i][0]){
    		secondObj.value=pCodeDescArray[i][1];
    		return;
    	}
    }

//not found,alert and set focus;
    alert(pNotFoundMessage);
    window.event.srcElement.focus();
 }
 
 
 /**
 *display code description in textbox2 by textbox1's value
 *code description data is in the server
 *@param pFirstTextboxName:the name of the first textbox, String Type
 *@param pSecondTextboxName:the name of the second textbox,String Type
 *@param pCodeArray:a two dimension array that contains the code description data
 *@param pNotFoundMessage:when input code not found in the CodeDescArray,then alert this message
 */
 function showDescFromServer(pFirstTextboxName,pSecondTextboxName,pTableName,pNotFoundMessage){
    var firstVal = document.all(pFirstTextboxName);
    var secondVal = document.all(pSecondTextboxName);
    var secondObj;
    var firstObj;
    if (firstVal.length+""!="undefined") {
      for (var i=0;i<firstVal.length;i++) {
        if (firstVal[i]==window.event.srcElement) {
          firstObj = firstVal[i];
          secondObj = secondVal[i];
        }
      }
    }else{
       firstObj = firstVal;
       secondObj=secondVal;
    }
    if(window.event.srcElement.value==""){
      secondObj.value="";
      return;
    }
    var needFocus = false;
    var desc=getCodeDesc(pTableName,firstObj.value,pNotFoundMessage);
    if(desc!=null){
    	if((desc.indexOf("Msg Not Found!")==0)||(desc.indexOf("Error:Code not found!")==0)){
    		alert(pNotFoundMessage);
		needFocus = true;
    	}else if(desc.indexOf("Error:")==0){
    		alert(desc);
		needFocus = true;
    	}else{

    	}
    }else{
    	alert("Error:server return null!");
    	needFocus=true;
    }
    if(needFocus){
       window.event.srcElement.focus();
    }else{
      secondObj.value=desc;
    }
 }