/* 兼容IE 7.0以前版本 XMLHttpRequest */
	//var http=null;
	var tmpAJaxFillData=null;
	var tmpAJaxQuote=new AJaxQuote();
  if (window.ActiveXObject) {
	window.XMLHttpRequest = function() {
	  var x = null; var progIds = [
		'MSXML3.XMLHTTP.5.0',
		'MSXML3.XMLHTTP.4.0',
		'MSXML3.XMLHTTP.3.0',
		'MSXML3.XMLHTTP.2.0',
		'MSXML3.XMLHTTP',
		'MSXML2.XMLHTTP.5.0', 
		'MSXML2.XMLHTTP.4.0', 
		'MSXML2.XMLHTTP.3.0', 
		'MSXML2.XMLHTTP', 
		'Microsoft.XMLHTTP'];
	  for (var i=0; i<progIds.length; i++) {
		try { x = new ActiveXObject(progIds[i]); break; } catch (ex) {};
	  }; if (!x) throw new Error([0,"Can not create XMLHttpRequest Object"]);
	  return x;
	}
  }
  /*处理兼容*/
  if(document.implementation && document.implementation.createDocument)
  {
	XMLDocument.prototype.loadXML = function(xmlString)
    {
        var childNodes = this.childNodes;
        for (var i = childNodes.length - 1; i >= 0; i--)
            this.removeChild(childNodes[i]);

        var dp = new DOMParser();
        var newDOM = dp.parseFromString(xmlString, "text/xml");
        var newElt = this.importNode(newDOM.documentElement, true);
        this.appendChild(newElt);
    };

    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var oNSResolver = this.createNSResolver(this.documentElement)
          var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
          var aResult = [];
          for( var i = 0; i < aItems.snapshotLength; i++)
          {
             aResult[i] =  aItems.snapshotItem(i);
          }
          return aResult;
       }

       // prototying the Element
       Element.prototype.selectNodes = function(cXPathString)
       {
          if(this.ownerDocument.selectNodes)
          {
             return this.ownerDocument.selectNodes(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }

    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var xItems = this.selectNodes(cXPathString, xNode);
          if( xItems.length > 0 )
          {
             return xItems[0];
          }
          else
          {
             return null;
          }
       }
       
       // prototying the Element
       Element.prototype.selectSingleNode = function(cXPathString)
       {    
          if(this.ownerDocument.selectSingleNode)
          {
             return this.ownerDocument.selectSingleNode(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }
}
/****************************************************************************/
  function UrlEncode(str){
  var ret="",tt;
  var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
  for(var i=0;i<str.length;i++){
   var chr = str.charAt(i);
    var c=str2asc(chr);
    tt += chr+":"+c+"n";
    if(parseInt("0x"+c) > 0x7f){
      ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
    }else{
      if(chr==" ")
        ret+="+";
      else if(strSpecial.indexOf(chr)!=-1)
        ret+="%"+c.toString(16);
      else
        ret+=chr;
    }
  }
  return ret;
}
function UrlDecode(str){
  var ret="";
  for(var i=0;i<str.length;i++){
   var chr = str.charAt(i);
    if(chr == "+"){
      ret+=" ";
    }else if(chr=="%"){
     var asc = str.substring(i+1,i+3);
     if(parseInt("0x"+asc)>0x7f){
			ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
	      i+=5;
     }else{
     	ret+=asc2str(parseInt("0x"+asc));
     	i+=2;
     }
    }else{
      ret+= chr;
    }
  }
  return ret;
}
  function AjaxHdle (Rdobj,sql,CallBack,MaxCount,PostStr) {
	/* If the keycode is not a valid key or the query value in the cache, show autocomplete */
	/* 如果输入的键是有效的，即非方向键等无效键，或是在缓存中有该值，则显示自动完成 */
	if(Rdobj.recordcount==0||Rdobj.regetdata==true){
		//content = "sql="+encodeURIComponent(sql);
		if(MaxCount==null)
			MaxCount='';
		//alert(UrlEncode(sql));
		if(tmpAJaxQuote.status){
	        tmpAJaxQuote.cmds.push(new AJaxParam());
	        tmpAJaxQuote.cmds[tmpAJaxQuote.cmds.length-1].Rdobj=Rdobj;
            tmpAJaxQuote.cmds[tmpAJaxQuote.cmds.length-1].sql=sql;
            tmpAJaxQuote.cmds[tmpAJaxQuote.cmds.length-1].CallBack=CallBack;
            tmpAJaxQuote.cmds[tmpAJaxQuote.cmds.length-1].MaxCount=MaxCount;
            tmpAJaxQuote.cmds[tmpAJaxQuote.cmds.length-1].PostStr=PostStr;
	        return
	    }else
	        tmpAJaxQuote.status=true;
		//alert(sql);
		if((PostStr!='') && (typeof(PostStr)!='undefined'))
			content = "sql="+encodeURIComponent(sql)+"&MaxCount="+MaxCount+'&'+PostStr
		else
			content = "sql="+encodeURIComponent(sql)+"&MaxCount="+MaxCount;
		//alert(content);
		http = new XMLHttpRequest();
		http.onreadystatechange = function(){loadCompleteData(Rdobj,CallBack)};
		//alert(evt.srcElement.value);
		http.open("Post", "/Utility/Ajax/AJaxQuery.asp", true);
		http.setRequestHeader("Content-Length",content.length);
		http.setRequestHeader("Content-Type","utf-8");
		http.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
		http.send(content);
	}
	//http.send(null);
	//alert(typeof(Rdobj));
	return Rdobj;
  }
  function AJaxShowImage(ImageData,ImageObj){
		//content = "ImageData="+encodeURIComponent(ImageData);
		http = new XMLHttpRequest();
		//http.onreadystatechange = function() {LoadImageStream(ImageObj)};
		//http.open("Post", "/Utility/Ajax/AJaxShowImage.asp", false);
		http.open("GET", "/Utility/Ajax/AJaxShowImage.asp", false);
		
		//http.setRequestHeader("Content-Length",content.length);
//		http.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
		http.send(null);
		if (http.readyState!=4) { return; }
		if(http.status==200)
		{
			alert('form1.textarea.value');
			//return http.ResponseBody;
			return http.responseStream;
			//responseStream
			//ImageObj.src=http.responsetext;
			//alert(http.responsetext);
			//ImageObj.src=;
		}else{
			alert('not ok');
		}
	//http.send(null);
	//alert(typeof(Rdobj));
  }
 function LoadImageStream(ImageObj)
 {
	if (http.readyState!=4) { return; }
		if(http.status==200)
		{
			//alert(http.ResponseBody);
			//ImageObj.src=http.responsetext;
			//alert(http.responsetext);
			//ImageObj.src=;
			return http.ResponseBody
		}
 }
function loadCompleteData(Rdobj,CallBack)
{
	
	if (http.readyState!=4) { 
		ShowAutoProcessMess();
	return; }
	if(http.status==200)
	{
		//alert(http.responseText);
		//responseXML ResponseBody
		if (document.all){
			var xmldoc=new ActiveXObject('MSXML2.DOMDocument');
		}
		else {
			var xmldoc=document.implementation.createDocument("","",null);
		}
		//alert(bytes2BSTR(http.ResponseBody));
		//if(xmldoc.loadXML(bytes2BSTR(http.ResponseBody))){
		
		try{
		    xmldoc.loadXML(http.responseText);
		    //alert(http.responseText);
			//obj.value=xmldoc.xml;
			if(document.all){
				var text=xmldoc.xml;
			}else{
			var text=new XMLSerializer().serializeToString(xmldoc);
			}
			if(text!=''){
				//alert(xmldoc.xml);
				//tmpAJaxFillData=AJaxFillData;
				if(tmpAJaxFillData==null)
					tmpAJaxFillData=AJaxFillData;
				else
					AJaxFillData=tmpAJaxFillData;
				if(CallBack){
					AJaxFillData=(AJaxFillData).andThen(function(){CallBack(Rdobj);});
				}
				//alert(tmpAJaxFillData);
				AJaxFillData(xmldoc,Rdobj);
				HiddenAutoProcessMess();
			}
		}catch(e){debugger;
		    HiddenAutoProcessMess();
			//alert('xml Loading error'+e.description);
		}
//		AJaxFillData=tmpAJaxFillData;
		//alert('record='+Rdobj.recordcount);
		//alert(http.responseXML.text);
		//alert(UrlDecode(http.ResponseBody.length));
		//alert(xmldoc.xml);
		//alert('Userfunc');
		
		//obj.value=bytes2BSTR(http.ResponseBody);
		//encodeURI
		//alert(UrlDecode('%B1%B1%BE%A9%D6%D0%C2%C3'));
		
		//evt.srcElement.value=UrlDecode(bytes2BSTR(http.ResponseBody));
		//obj.value=UrlDecode(bytes2BSTR(http.ResponseBody));
	}
}
//xmldoc,Rdobj
function AJaxFillData(xmldoc,Rdobj)
{	
	var i,j,k;
	//alert(typeof(xmldoc));
	xmldoc=AJaxFillData.arguments[0];
	Rdobj=AJaxFillData.arguments[1];
	Rdobj.fields=[];
	Rdobj.rows=[];
	Rdobj.colums=[];
	Rdobj.recordcount=0;

	var pattern = new RegExp('<.+?>');
	
	var tmNodefields=xmldoc.selectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/fields");
	if(tmNodefields!=null){
		//alert(tmNodefields.selectSingleNode('@count').nodeValue);
		if (document.all){
			Rdobj.xml=xmldoc.xml;
		}
		else{
			Rdobj.xml=new XMLSerializer().serializeToString(xmldoc);
		}
		//alert(Rdobj.xml);
		Rdobj.fieldcount=parseInt(tmNodefields.selectSingleNode('@count').nodeValue);
		var tmNodefield=tmNodefields.selectNodes('field');
		for(i=0;i<=tmNodefields.selectSingleNode('@count').nodeValue-1;i++)
		{
			Rdobj.colums.push(tmNodefield[i].nodeValue);
		}
		var tmNoderecords=xmldoc.selectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/records");
		if(tmNoderecords!=null){
			if(parseInt(tmNoderecords.selectSingleNode('@count').nodeValue)>0){
				Rdobj.recordcount=parseInt(tmNoderecords.selectSingleNode('@count').nodeValue);
				var tmNodeRecord=tmNoderecords.selectNodes('Record');
				var tmField=new Array(Rdobj.fieldcount-1);
				//alert(tmField.length);
				for(k=0;k<=Rdobj.fieldcount-1;k++)
				{
					//tmField[k]=new Array(Rdobj.recordcount-1);
					tmField[k]=[];
				}
				for(i=0;i<=tmNoderecords.selectSingleNode('@count').nodeValue-1;i++)
				{
					var row=new Array(Rdobj.fieldcount-1);
					for(j=0;j<=tmNodefields.selectSingleNode('@count').nodeValue-1;j++)
					{
						//alert(tmNodeRecord.item(i).selectNodes('field').item(j).nodeTypedValue);
						//alert('j='+j+',i='+i);
						//Rdobj.filterhtml ? tmField[j][i]=tmNodeRecord.item(i).selectNodes('field').item(j).nodeTypedValue.replace(/<.+?>/g,''):tmField[j][i]=tmNodeRecord.item(i).selectNodes('field').item(j).nodeTypedValue;
						//alert(tmNodeRecord.item(i).selectNodes('field').item(j).nodeTypedValue+'='+tmNodeRecord.item(i).selectNodes('field').item(j).nodeTypedValue.length);
						//alert("123"+tmNodeRecord.item(i).selectNodes('field').item(j).childNodes.item(0).nodeTypedValue);
						if(tmNodeRecord[i].selectNodes('field')[j].childNodes.length>=1)
						{
							if(!Rdobj.filternull)
								Rdobj.filterhtml ? tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue.replace(/<.+?>/g,'')):tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue);
							else
								if(tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue!='')
									Rdobj.filterhtml ? tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue.replace(/<.+?>/g,'')):tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue);	
							Rdobj.filterhtml ? row[j]=tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue.replace(/<.+?>/g,''):row[j]=tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue;
						}else{
							if(!Rdobj.filternull){
								try{
								Rdobj.filterhtml ? tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].nodeValue.replace(/<.+?>/g,'')):tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].nodeValue);
								}catch(ex)
								{tmField[j].push('');}
							}
							else
								if(tmNodeRecord[i].selectNodes('field')[j].nodeValue!='')
									Rdobj.filterhtml ? tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].childNodes[0].nodeValue.replace(/<.+?>/g,'')):tmField[j].push(tmNodeRecord[i].selectNodes('field')[j].nodeValue);	
							try{
								Rdobj.filterhtml ? row[j]=tmNodeRecord[i].selectNodes('field')[j].nodeValue.replace(/<.+?>/g,''):row[j]=tmNodeRecord[i].selectNodes('field')[j].nodeValue;
							}catch(ex)
							{row[j]='';}
						}
					}
					Rdobj.rows.push(row);
				}
				Rdobj.fields=tmField;
			}
		}		
	}
	if(tmpAJaxQuote.cmds.length<1)
    {
        tmpAJaxQuote.status=false;
    }else{
        tmpAJaxQuote.status=false;
        AjaxHdle(tmpAJaxQuote.cmds[0].Rdobj,tmpAJaxQuote.cmds[0].sql,tmpAJaxQuote.cmds[0].CallBack,tmpAJaxQuote.cmds[0].MaxCount,tmpAJaxQuote.cmds[0].PostStr);
        tmpAJaxQuote.cmds=tmpAJaxQuote.cmds.del(0);
    }
}
function RecordsetClass()
{
	this.xml='';
	this.filterhtml=true;
	this.filternull=false;
	this.regetdata=false;
	this.fieldcount=0;
	this.recordcount=0;
	this.fields=[];
	this.rows=[];
	this.colums=[];
}
Function.prototype.andThen=function(g) {
var f=this;
  return function() {
	f();g();
}
};

/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
 * Version: 1.0
 * LastModified: Dec 25 1999
 * This library is free.  You can redistribute it and/or modify it.
 */

/*
 * Interfaces:
 * b64 = base64encode(data);
 * data = base64decode(b64);
 */


function base64encode(str) {
	var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var base64DecodeChars = new Array(
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
		52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
		-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
		15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
		-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
		41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	c1 = str.charCodeAt(i++) & 0xff;
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt((c1 & 0x3) << 4);
	    out += "==";
	    break;
	}
	c2 = str.charCodeAt(i++);
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	    out += base64EncodeChars.charAt((c2 & 0xF) << 2);
	    out += "=";
	    break;
	}
	c3 = str.charCodeAt(i++);
	out += base64EncodeChars.charAt(c1 >> 2);
	out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
	out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
	var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	var base64DecodeChars = new Array(
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
		52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
		-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
		15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
		-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
		41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	/* c1 */
	do {
	    c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c1 == -1);
	if(c1 == -1)
	    break;

	/* c2 */
	do {
	    c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c2 == -1);
	if(c2 == -1)
	    break;

	out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

	/* c3 */
	do {
	    c3 = str.charCodeAt(i++) & 0xff;
	    if(c3 == 61)
		return out;
	    c3 = base64DecodeChars[c3];
	} while(i < len && c3 == -1);
	if(c3 == -1)
	    break;

	out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

	/* c4 */
	do {
	    c4 = str.charCodeAt(i++) & 0xff;
	    if(c4 == 61)
		return out;
	    c4 = base64DecodeChars[c4];
	} while(i < len && c4 == -1);
	if(c4 == -1)
	    break;
	out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}
function AJaxParam()
{
    this.Rdobj=null;
    this.sql='';
    this.CallBack=null;
	this.MaxCount='';
	this.PostStr='';
}
function AJaxQuote()
{
    this.status=false;  //記錄是否在執行中
    this.cmds=[];
}
function ShowAutoProcessMess(){
	return;
	if(document.getElementById('AutoProcessMess')==null){
		var img=document.createElement('img')
		img.setAttribute('id','AutoProcessMess');
		img.setAttribute('src','/Img/EN/Photo/eloading.gif');
		img.className='AutoProcessMessCss';
		img.style.marginTop='-'+(img.height/2).toString()+'px';
		img.style.marginLeft='-'+(img.width/2).toString()+'px';
		document.body.appendChild(img);
		//img.style.visibility='visible';
	}else{
	    var img=document.getElementById('AutoProcessMess');
	    img.style.marginTop='-'+(img.height/2).toString()+'px';
		img.style.marginLeft='-'+(img.width/2).toString()+'px';
	    //img.style.visibility='visible';
	}
}
function HiddenAutoProcessMess(){
	return;
    if(document.getElementById('AutoProcessMess')!=null){
        var img=document.getElementById('AutoProcessMess');
        img.style.display='none';
    }
}
