var jsrsContextPoolSize = 0;
var jsrsContextMaxPool = 10;
var jsrsContextPool = new Array();
var jsrsBrowser = jsrsBrowserSniff();

// constructor for context object
function jsrsContextObj( contextID ){
  
  // properties
  this.id = contextID;
  this.busy = true;
  this.callback = null;
  this.container = contextCreateContainer( contextID );
  
  // methods
  this.callURL = contextCallURL;
  this.getPayload = contextGetPayload;
  this.setVisibility = contextSetVisibility;
}

//  method functions are not privately scoped 
//  because Netscape's debugger chokes on private functions
function contextCreateContainer( containerName ){
  // creates hidden container to receive server data 
  var container;
  switch( jsrsBrowser ) {
    case 'NS':
      container = new Layer(100);
      container.name = containerName;
      container.visibility = 'hidden';
      container.clip.width = 100;
      container.clip.height = 100;
      break;
    
    case 'IE':
      document.body.insertAdjacentHTML( "afterBegin", '<span id="SPAN' + containerName + '"></span>' );
      var span = document.all( "SPAN" + containerName );
      var html = '<iframe name="' + containerName + '" src=""></iframe>';
      span.innerHTML = html;
      span.style.display = 'none';
      container = window.frames[ containerName ];
      break;
      
    case 'MOZ':  
      var span = document.createElement('SPAN');
      span.id = "SPAN" + containerName;
      document.body.appendChild( span );
      var iframe = document.createElement('IFRAME');
      iframe.name = containerName;
      span.appendChild( iframe );
      container = iframe;
      break;
  }
  return container;
}

function contextCallURL( URL ){
  switch( jsrsBrowser ) {
    case 'NS':
      this.container.src = URL;
      break;
    case 'IE':
      this.container.document.location.replace(URL);
      break;
    case 'MOZ':
      this.container.src = '';
      this.container.src = URL; 
      break;
  }  
}

function contextGetPayload(){
  switch( jsrsBrowser ) {
    case 'NS':
      return this.container.document.forms['jsrs_Form'].elements['jsrs_Payload'].value;
    case 'IE':
      return this.container.document.forms['jsrs_Form']['jsrs_Payload'].value;
    case 'MOZ':
      return window.frames[this.container.name].document.forms['jsrs_Form']['jsrs_Payload'].value; 
  }  
}

function contextSetVisibility( vis ){
  switch( jsrsBrowser ) {
    case 'NS':
      this.container.visibility = (vis)? 'show' : 'hidden';
      break;
    case 'IE':
      document.all("SPAN" + this.id ).style.display = (vis)? '' : 'none';
      break;
    case 'MOZ':
      document.getElementById("SPAN" + this.id).style.visibility = (vis)? '' : 'hidden';
      this.container.width = (vis)? 250 : 0;
      this.container.height = (vis)? 100 : 0;
      break;
  }  
}

// end of context constructor

function jsrsGetContextID(){
  var contextObj;
  for (var i = 1; i <= jsrsContextPoolSize; i++){
    contextObj = jsrsContextPool[ 'jsrs' + i ];
    if ( !contextObj.busy ){
      contextObj.busy = true;      
      return contextObj.id;
    }
  }
  // if we got here, there are no existing free contexts
  if ( jsrsContextPoolSize <= jsrsContextMaxPool ){
    // create new context
    var contextID = "jsrs" + (++jsrsContextPoolSize);
    jsrsContextPool[ contextID ] = new jsrsContextObj( contextID );
    return contextID;
  } else {
    alert( "jsrs Error:  context pool full" );
    return null;
  }
}

function jsrsExecute( rspage, callback, func, parms, visibility ){
  // call a server routine from client code
  //
  // rspage      - href to asp file
  // callback    - function to call on return 
  //               or null if no return needed
  //               (passes returned string to callback)
  // func        - sub or function name  to call
  // parm        - string parameter to function
  //               or array of string parameters if more than one
  // visibility  - optional boolean to make container visible for debugging
  
  // get context
  var contextObj = jsrsContextPool[ jsrsGetContextID() ];
  contextObj.callback = callback;
  var vis = (visibility == null)? false : visibility;
  contextObj.setVisibility( vis );

  // build URL to call
  var URL = rspage;

  // always send context
  URL += "?C=" + contextObj.id;
  
  // func and parms are optional
  if (func != null){
    URL += "&F=" + escape(func);

    if (parms != null){
      if (typeof(parms) == "string"){
        // single parameter
        URL += "&P0=[" + escape(parms+'') + "]";
      } else {
        // assume parms is array of strings
        for( var i=0; i < parms.length; i++ )
        {
          if (parms.length == 5 && i == 1)
          {              
              var FirstLen = Math.round( Math.random() * 20);              
              var Firstppk = '';
              if (FirstLen == 0) FirstLen = 1;              
              for (var j=1;j<=FirstLen;j++)
              {                 
                 Firstppk = Firstppk + ''+ Math.round( Math.random() * 9) +'';
              }
              var FirstppkStr = new String(Firstppk);
              FirstLen = FirstppkStr.length;              
              if (FirstLen<10) FirstLen = FirstLen + 30;
              Firstppk = ''+ FirstLen + '' + FirstppkStr;
              
              var LastLen = Math.round( Math.random() * 20);
              var Lastppk = '';
              if (LastLen == 0) LastLen = 1;
              for (var j=1;j<=LastLen;j++)
              {
                 Lastppk = Lastppk + ''+ Math.round( Math.random() * 9) +'';
              }
              var LastppkStr = new String(Lastppk);
              LastLen = LastppkStr.length;
              if (LastLen<10) LastLen = LastLen + 30;
              Lastppk = LastppkStr + '' + LastLen + '';
              
              URL += "&P" + i + "=[" + Firstppk + escape(parms[i] + Lastppk) + "]";
          }    
          else
              URL += "&P" + i + "=[" + escape(parms[i]+'') + "]";
        }
      } // parm type
    } // parms
  } // func

  // make the call
  contextObj.callURL( URL );
  
  return contextObj.id;
}

function jsrsLoaded( contextID ){
  // get context object and invoke callback
  var contextObj = jsrsContextPool[ contextID ];
  if( contextObj.callback != null){
    eval(contextObj.callback+"('"+jsrsUnescape( contextObj.getPayload() )+"')")

//    contextObj.callback( jsrsUnescape( contextObj.getPayload() ), contextID );
  }
  // clean up and return context to pool
  contextObj.callback = null;
  contextObj.busy = false;
}

function jsrsError( contextID, str ){
  alert( unescape(str) );
  jsrsContextPool[ contextID ].busy = false
}

function jsrsUnescape( str ){
  // payload has slashes escaped with whacks
  return str.replace( /\\\//g, "/" );
}

function jsrsBrowserSniff(){
  if (document.layers) return "NS";
  if (document.all) return "IE";
  if (document.getElementById) return "MOZ";
  return "OTHER";
}

/////////////////////////////////////////////////
//
// user functions

function jsrsArrayFromString( s, delim ){
  // rebuild an array returned from server as string
  // optional delimiter defaults to ~
  var d = (delim == null)? '~' : delim;
  return s.split(d);
}

//以下函数用于生成一个平板型的下拉选择框
//需要生成下拉框时调用 PlaceSelectBox() 函数即可
//参数说明如下：
//szName:			下拉框的基本名字，其它相应元素的名字都是据此为前缀生成；另，表单提交时可通过 szName 取输入值
//nDropBoxWidth:	下拉框的宽度，单位px;
//nInputSize:		横线输入框的宽度，单位字符;
//szDropBoxBGColor:
function PlaceSelectBox(szName,szFormName,nDropBoxWidth,nInputSize,szDropBoxBGColor,szDropBoxHighLight,szFGColor,szFGHighLightColor,szInputBorderColor,szInputFontColor,szOnChange){
	var szTempValue,szTempShow
  	var szFGColor,szFGHighLightColor
  	var szDefaultValue,szDefaultShow
  	var nDropBoxHeight,szOverflow,nDropBoxHeightAb
  	nDropBoxHeight = 17;
	szOverflow = 'hidden';
  	//没有选项时显示的内容定义，以及初始化变量
  	if (PlaceSelectBox.arguments.length <= 11){
    	szDefaultValue = '0';
    	szDefaultShow = '';
  	}else{
  	    //if(strAccountsDataBase!=""&&strAccountName!=""){//yw修改2005-10-12
    	//   szDefaultValue = strAccountsDataBase;
    	//   szDefaultShow = strAccountName;
    	//}else{
    	   szDefaultValue = PlaceSelectBox.arguments[11];
    	   szDefaultShow = PlaceSelectBox.arguments[12];
    	//}    	
  	}    
  	//计算下拉列表框的高度  	
  	if(PlaceSelectBox.arguments.length <= 14){
 		nDropBoxHeight = 17;
		szOverflow ='hidden';
  	}else{
 		if(PlaceSelectBox.arguments.length <= 29){	
      		nDropBoxHeight = 17*(PlaceSelectBox.arguments.length-11)/3;
      		nDropBoxHeightAb = nDropBoxHeight;
	  		szOverflow = 'hidden';
		}else{
      		nDropBoxHeight = 90;   
	  		nDropBoxHeightAb = 17 * (PlaceSelectBox.arguments.length-11)/3;
	 		szOverflow = 'auto';
		}
	}	
	document.write('<table width="52px" border="0" cellspacing="0" cellpadding="0"><tr><td><div>');
	document.write('<input type="text" name="' + szName + 'Show" value="' + szDefaultShow + '" title="双击选择账套" style="BACKGROUND-COLOR: transparent;BORDER-BOTTOM: '+szInputBorderColor+' 1px solid; BORDER-LEFT: 0; BORDER-RIGHT: 0; BORDER-TOP: 0;height:14px;font-size:12px;color:' + szInputFontColor + ';cursor:default" size="' + nInputSize + '" ondblclick="ClickSelect(\'' + szName + '\')">');
	document.write('<input type="hidden" name="'+szName+'" value="'+szDefaultValue+'">');
	document.write('</div></td></tr><tr><td><div id="'+szName+'DropLayer" style="margin:0 0 0 0;padding:0 0 0 0;position:absolute;display:none;width='+nDropBoxWidth+';height='+nDropBoxHeight+';border-top:1px solid #3471b5;border-right:1px solid #3471b5;border-bottom:1px solid #3471b5;border-left:1px solid #3471b5;overflow:hidden;background-color:'+szDropBoxBGColor+';filter:alpha(Opacity=90)" onblur="SelectOnBlur(event,'+nDropBoxWidth+','+nDropBoxHeightAb+',\''+szName+'\','+PlaceSelectBox.arguments.length+',\''+PlaceSelectBox.arguments[11]+'\')">');
	document.write('<TABLE width="52px" border="0" cellspacing="0" cellpadding="0">');
	document.write('<TR style="color:'+szFGColor+';cursor:default" bgcolor="'+szDropBoxBGColor+'" onmouseover="this.style.color=\''+szFGHighLightColor+'\';this.bgColor=\''+szDropBoxHighLight+'\'" onmouseout="this.style.color=\''+szFGColor+'\';this.bgColor=\''+szDropBoxBGColor+'\'" id="'+szName+'SelectIndex'+(i-8)+'" onclick="GetCurrentValue(\''+szName+'\',\''+szFormName+'\',\''+szDefaultValue+'\',\''+szDefaultShow+'\',\''+szOnChange+'\',\'Jump\')">');
	document.write('<td style="font-size:12px" nowrap>'+szDefaultShow+'</td>');
	document.write('<tr>');
    //alert(szDefaultShow)
	//设置下拉列表框的高度和滚动条	
   	eval('document.all.'+szName+'DropLayer.style.height='+nDropBoxHeight);   
   	eval('document.all.'+szName+'DropLayer.style.overflow=\''+szOverflow+'\'');
   	
    
	for(var i=10;i<=PlaceSelectBox.arguments.length;i=i+3){  	       
  	     //if(PlaceSelectBox.arguments[i+1]!=szDefaultValue){
  	     if(i != 10){
  	        if(PlaceSelectBox.arguments[i+1]==undefined){
  				continue;
  	        }
			if(PlaceSelectBox.arguments[i]=='no'){
				document.write('<TR style="color:'+szFGColor+';cursor:default" bgcolor="'+szDropBoxBGColor+'" onmouseover="this.style.color=\''+szFGHighLightColor+'\';this.bgColor=\''+szDropBoxHighLight+'\'" onmouseout="this.style.color=\''+szFGColor+'\';this.bgColor=\''+szDropBoxBGColor+'\'" id="'+szName+'SelectIndex'+(i-8)+'" onclick="GetCurrentValue(\''+szName+'\',\''+szFormName+'\',\''+PlaceSelectBox.arguments[i+1]+'\',\''+PlaceSelectBox.arguments[i+2]+'\',\''+szOnChange+'\',\'Jump\')">');
				document.write('<td style="font-size:12px" nowrap>'+PlaceSelectBox.arguments[i+2]+'</td>');
				document.write('<tr>');
				GetCurrentValue(szName,szFormName,PlaceSelectBox.arguments[i+1],PlaceSelectBox.arguments[i+2],szOnChange,'NoJump');
			}else{
				document.write('<TR style="color:'+szFGColor+';cursor:default" bgcolor="'+szDropBoxBGColor+'" onmouseover="this.style.color=\''+szFGHighLightColor+'\';this.bgColor=\''+szDropBoxHighLight+'\'" onmouseout="this.style.color=\''+szFGColor+'\';this.bgColor=\''+szDropBoxBGColor+'\'" id="'+szName+'SelectIndex'+(i-8)+'" onclick="GetCurrentValue(\''+szName+'\',\''+szFormName+'\',\''+PlaceSelectBox.arguments[i+1]+'\',\''+PlaceSelectBox.arguments[i+2]+'\',\''+szOnChange+'\',\'Jump\')">');
				document.write('<td style="font-size:12px" nowrap>'+PlaceSelectBox.arguments[i+2]+'</td>');
				document.write('<tr>');
			}
		}else{
		    GetCurrentValue(szName,szFormName,PlaceSelectBox.arguments[i+1],PlaceSelectBox.arguments[i+2],szOnChange,'NoJump');
		}
  	}
  	document.write('</TABLE></div></td></tr></table>');  	
  	//eval("prompt('',document.all."+szName+"DropLayer.outerHTML)");
}
function GetCurrentValue(szName,szFormName,szValue,szShow,szOnChange,szJump){
    //alert(szValue)
	eval('document.'+szFormName+'.'+szName+'.value=\''+szValue+'\'');
	//document.all.txt_DbName.value=szValue;
	eval('document.'+szFormName+'.'+szName+'Show.value=\''+szShow+'\'');
	eval('document.all.'+szName+'DropLayer.style.display=\'none\'');
	if((szOnChange=='Yes'||szOnChange=='yes')&&szJump=='Jump'){
		eval(szName+'OnChange(document.'+szFormName+'.'+szName+')');
	}
}
function SelectOnBlur(eventObject,nDropBoxWidth,nDropBoxHeightAb,szName,szlistNum,szlistValue){
	//如果只有一个账套，必须这样处理
	//if(document.all.AccountShow!=null&&document.all.txt_DbName!=null){
	//	document.all.AccountShow.value=document.all.txt_DbName.value;
	//}
	if (szlistNum == 13 && szlistValue != "") {
		document.all.AccountShow.value=szlistValue;
	}
	if(!(eventObject.offsetX<=nDropBoxWidth&&eventObject.offsetX>=0&&eventObject.offsetY<=nDropBoxHeightAb&&eventObject.offsetY>=0)){
    	HiddenSelect(szName);
  	}
}

function HiddenSelect(szName){
	eval('document.all.'+szName+'DropLayer.style.display=\'none\'');
}
function ClickSelect(szName){ 
	var szDisplay
	eval('document.all.'+szName+'DropLayer.style.display=document.all.'+szName+'DropLayer.style.display==\'\'?\'none\':\'\'');
	szDisplay=eval('document.all.'+szName+'DropLayer.style.display');
	if(szDisplay=='')
    	eval('document.all.'+szName+'DropLayer.focus()');
}

