﻿//v1.5 2003/03/19
function SynnexService()
{
	this._mProg=null;
	this._Params=new Array;
	//this._ParamsKey=new Array;
	//this._ParamsValue=new Array;
	//this._ResponseMsg=new Array;
	this._ResponseMsgKey=new Array;
	this.L_InProgress_Text = "In Progress";

	this.IsDebug=false;

	this.showProgress=false;
	//method
	this.Ary2Xml=_Ary2Xml;
	this.Xml2Ary=_Xml2Ary;

	if (this.showProgress == "true" || this.showProgress == true)
		fnShowProgress();
}
SynnexService.prototype.useService=function(sURL)
{
	var xmldom=this.Ary2Xml();

	//alert(typeof(xmldom.xml));

	if (xmldom!=null)
	{
		//var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
		xmlHttp.open("POST",sURL,false);
		xmlHttp.setRequestHeader("Content-Type", "text/html;charset=Big5");
		if (this.IsDebug)
		{
			alert(xmldom.xml);
		}
		xmlHttp.send(xmldom);
		var httpstat=xmlHttp.status+'';

		//alert("parseError.errorCode::"+httpstat.substring(0,1));
		//alert("parseError.errorCode::"+xmlHttp.responseText.parseError.errorCode);
		if (httpstat.substring(0,1) != '5')
		{
			if (this.IsDebug)
			{
				alert(xmlHttp.responseText);
			}
			this.Xml2Ary(xmlHttp.responseText);
		}

		var xmlHttp = null;
	}
	//addParameter("a","apple");
	//addParameter("b","book");

}
SynnexService.prototype.addParameter=function(sKey,sValue)
{
	var ln=this._Params.length;
	//this._ParamsKey[ln]=sKey;
	//this._ParamsValue[ln]=sValue;
	//this._Params[ln]=new Array(this._ParamsKey[ln],this._ParamsValue[ln]);
	this._Params[ln]=new Array(sKey,sValue);
}

SynnexService.prototype.resetParameter=function()
{
	this._Params=new Array;
	//this._ParamsKey=new Array;
	//this._ParamsValue=new Array;
}

SynnexService.prototype.getMsgByKey=function(sKey)
{
	var output;
	if ((sKey!="")&&(sKey!=null))
	{
		output=this._ResponseMsgKey[sKey];
//		if (typeof(output)=="undefined")
//			return "";
//		else
			return output;
	}
}

	function _Ary2Xml()
	{
		var sXML="";

		//alert(this._Params.length);

		if (this._Params.length>0)
		{
			sXML+="<?xml version='1.0'?><SynnexService>";
			for(var i=0;i<this._Params.length;i++)
			{
				var sKey=this._Params[i][0];
				var sValue=this._Params[i][1];
				if ((sKey!=null)||(sKey!=""))
				{
					//alert(sKey+"-"+sValue);
					sXML+="<"+sKey+">"+sValue+"</"+sKey+">";
				}

			}
			sXML+="</SynnexService>"

			//alert(sXML);
		}
		if (sXML!="")
		{
			var oXML=new ActiveXObject("Microsoft.XMLDOM");
			oXML.async=false;
			oXML.loadXML(sXML);

			//alert(oXML.documentElement.xml);

			return oXML;
		}else{return null;}
	}

	function _Xml2Ary(xmltext)
	{
		var oXML=new ActiveXObject("Microsoft.XMLDOM");
		oXML.async=false;
		oXML.loadXML(xmltext);
		var oDom=oXML.documentElement;
		for(var i=0;i<oDom.childNodes.length;i++)
		{
			var sKey=oDom.childNodes.item(i).nodeName;
			var sValue=oDom.childNodes.item(i).text;
			//alert(sKey+"/"+sValue);
			this._ResponseMsgKey[sKey]=sValue;
			//this._ResponseMsg[this._ResponseMsg.length]=new Array(sKey,sValue);
		}
		oXML=null;
	}
	//show process status
	function fnShowProgress()
	{
		if (_mProg == null)
		{
			var s = "<MARQUEE WIDTH=160 HEIGHT=20 BEHAVIOR=alternate SCROLLDELAY=1"
				+ " STYLE='background:lightblue;position:absolute;top:300;left:200'>"
				+ "</MARQUEE>";
			_mProg = element.document.createElement(s);
			_mProg.innerHTML = L_InProgress_Text;
		}
		if (_mProg.parentElement == null)
			element.document.body.insertAdjacentElement("beforeEnd",_mProg);
	}

	function hideProgress()
	{
		if (_mProg == null || _mProg.parentElement == null)
			return;
		element.document.body.removeChild(_mProg);
	}

function replaceSpecialChar(x) {
		x=String(x);
      	reg=/&/g
     	x=x.replace(reg, "&amp;");
	   	reg=/</g
     	x=x.replace(reg, "&lt;");
		reg=/>/g
     	x=x.replace(reg, "&gt;");
//		reg=/"/g
//     	x=x.replace(reg, '&quot;');
//      reg=/'/g
//     	x=x.replace(reg, '&apos;');
//        x=convertLineBreak(x);
	return x;
}
