var MSG_TRUE="1";

function CMqHTTPRequest_js() {
	var CANCEL_PROCESS="-99999999";
	var REQUEST_REMOTE_CALL="REMOTE_CALL";
	var REMOTE_CALL_TRUE="-1";
	var appRequest;
	var objSysMsg=new CMqSysMsg;
	var m_oMqString=new MqString;
	var m_strParam;
	var m_blnUseCache=false;
	var m_varCachedResponse;
	var INVALID_INDEX=-1;
	var ARR_URL=0;
	var ARR_OUTPUT=1;
	var INDEX_ENCODED_SYSMSG=0;
	var INDEX_ENCODED_MSGTEXT=1;
	var INDEX_ENCODED_FORCEEXIT=2;
	var PROPERTY_DELIMITER="&";
	var self=this;
	var mlngAppMsgTries=0;
	var MAX_TRIES_BEFORE_MSG=20;
	var DEF_CHSET="ISO-8859-1";
	var UTF8_CHSET="UTF8";
	var m_objXMLHttpReqObj;
	var USE_APPLET=false;
	
	function Get(strUrlPath, bUTF8)
	{
		var strResult="";
		var blnHTTPGet =true;
		try{
			
		    if (m_blnUseCache){
			    var lngCacheIndex=IsCached(strUrlPath, m_varCachedResponse);
			    if (lngCacheIndex!=INVALID_INDEX){
				    strResult=GetCachedResponse(lngCacheIndex,m_varCachedResponse);
				    blnHTTPGet=false;
			    }
			
		    }
		    if (blnHTTPGet){
				var strHTTPUrlPath=strUrlPath;
				if(strUrlPath.indexOf("?")>0){
				    strHTTPUrlPath+="&";
				}else{
					strHTTPUrlPath+="?";
				}
				strHTTPUrlPath+=REQUEST_REMOTE_CALL + "=" + REMOTE_CALL_TRUE;
				
				strHTTPUrlPath+="&rd=" + Math.floor(Math.random()*1000001)
				
			    if (m_strParam){
				    strHTTPUrlPath=strHTTPUrlPath+"&"+ m_strParam;
			    }
				
				if(USE_APPLET){
					if (IsAppletLoaded(appRequest)){
						if (bUTF8){
							appRequest.SetCharset(UTF8_CHSET);
						}
						var co=appRequest.Get(strHTTPUrlPath);

						strResult=ProcessResponse(co,m_blnUseCache,strUrlPath);
						if (bUTF8){
							appRequest.SetCharset(DEF_CHSET);
						}
					}else{
						strResult=CANCEL_PROCESS;
					}
				}else{
					var objReq = GetXMLHttpReqObj();
					if(objReq){
						objReq.open("GET", strHTTPUrlPath, false);
						objReq.send(null);
						if(objReq.status==200){
							var co=objReq.responseText;
							strResult=ProcessResponse(co,m_blnUseCache,strUrlPath);
						}else{
							//alert(objReq.status);
							strResult=CANCEL_PROCESS;
						}
					}else{
						strResult=CANCEL_PROCESS;
					}
				}
		    }
		}catch(err){
			alert(err.description);
		}
		return strResult;
	}
	
	function Post(strUrlPath,strPostData,bUTF8)
	{
		var strResult="";
		try{
			if(strUrlPath.indexOf("?")>0){
				strUrlPath+="&";
		    }else{
				strUrlPath+="?";
		    }
		    strUrlPath+=REQUEST_REMOTE_CALL + "=" + REMOTE_CALL_TRUE;
		     
		    if (m_strParam){
			    strUrlPath=strUrlPath+"&"+ m_strParam;
		    }
			
			if(USE_APPLET){
				if (IsAppletLoaded(appRequest)){
					if (bUTF8){
						appRequest.SetCharset(UTF8_CHSET);
					}
					var co=appRequest.Post(strUrlPath,strPostData);

				    strResult=ProcessResponse(co,false,"");
					if (bUTF8){
						appRequest.SetCharset(DEF_CHSET);
					}
				}else{
					strResult=CANCEL_PROCESS;
				}		
			}else{
				var objReq = GetXMLHttpReqObj();
				if(objReq){
					objReq.open("POST", strUrlPath, false);
					objReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					if(!(g_MqRef.MqBrowser.ie==true && g_MqRef.MqBrowser.major <=6)){
						//objReq.setRequestHeader("Content-length", strPostData.length);
						//objReq.setRequestHeader("Connection", "close");
					}
					objReq.send(strPostData);
					if(objReq.status==200){
						var co=objReq.responseText;
						strResult=ProcessResponse(co,false,"");
					}else{
						//alert(objReq.status);
						strResult=CANCEL_PROCESS;
					}
				}else{
					strResult=CANCEL_PROCESS;
				}
			}		    	
		}catch(err){
			alert(err.description);
		}
		return strResult;
	}
	
	function GetXMLHttpReqObj()
	{
		if(m_objXMLHttpReqObj){
			return m_objXMLHttpReqObj;
		}else{
			// Provide the XMLHttpRequest class for IE 5.x-6.x:
			if( typeof XMLHttpRequest == "undefined" ){
				try { 
				  m_objXMLHttpReqObj = new ActiveXObject("Msxml2.XMLHTTP.6.0") 
				  } catch(e) {}
				
				if(!m_objXMLHttpReqObj){
					try { 
					  m_objXMLHttpReqObj = new ActiveXObject("Msxml2.XMLHTTP.3.0") 
					  } catch(e) {}
					  
					if(!m_objXMLHttpReqObj){
						try { 
						  m_objXMLHttpReqObj = new ActiveXObject("Msxml2.XMLHTTP") 
						  } catch(e) {}
						
						if(!m_objXMLHttpReqObj){
							try { 
							  m_objXMLHttpReqObj = new ActiveXObject("Microsoft.XMLHTTP") 
							  } catch(e) {}
						}
					}
				}
			}else{
				m_objXMLHttpReqObj = new XMLHttpRequest();
			}
			return m_objXMLHttpReqObj;
		}
	}
	function ProcessResponse(strSvrResponse, blnCache, strUrlPath)
	{
		var strResult="";
		
		if (strSvrResponse){
			strSvrResponse=String(strSvrResponse);
			strSvrResponse= m_oMqString.getNullTerminatedString(strSvrResponse);
		
			if (objSysMsg.isSysMsg(strSvrResponse)){
				var arrSysMsg=ParseSysMsg(strSvrResponse);
				if (arrSysMsg){
					objSysMsg.doSysMsg(arrSysMsg[INDEX_ENCODED_SYSMSG], arrSysMsg[INDEX_ENCODED_MSGTEXT],
										arrSysMsg[INDEX_ENCODED_FORCEEXIT]);
				}else{
					objSysMsg.doSysMsg(strSvrResponse);
				}
				strResult= CANCEL_PROCESS;
			}else{
				strResult= strSvrResponse;
				if (blnCache && strUrlPath){
					SetCachedResponse(strUrlPath,strResult);
				}
			}
		}
		return strResult;
	}
	
	function ParseSysMsg(strEncodedMessage)
	{
		var arrSysMsg;
		
		if (strEncodedMessage){
			arrSysMsg=strEncodedMessage.split(PROPERTY_DELIMITER);
		}
		return arrSysMsg;
	}
	
	
	function setParam(strParam)
	{
		m_strParam=strParam;
	}
	
	function IsAppletLoaded(objApplet)
	{
		if(USE_APPLET){
			var fAppletLoaded=false;
			if(IsJavaEnabled(objApplet)){
				if (objApplet.isActive()){
					fAppletLoaded=true;
				}
			}else{
				self.OpenJavaVMHelpWindow();
			}
								
			return fAppletLoaded;
		}else{
			return true;
		}
	}
	
	function IsJavaEnabled(objApplet)
	{
		var blnJavaEnabled=true;
		if (navigator.javaEnabled()){
			try{
				if (objApplet){	
				
					if (objApplet.isActive()){
					}
				}
				
			}catch (errorObject) {
				blnJavaEnabled=false;
			}
		}else{
			blnJavaEnabled=false;
		}
					
		return blnJavaEnabled;
	}
	
	function OpenJavaVMHelpWindow()
	{
		if(mlngAppMsgTries==0){
			OpenChildWindow(g_MqRef.MqFS.GetCurrentVDURL(window) + "/HTML/TmplJavaVMHelp.htm", "JavaVMHelp","toolbar=no,resizable=no,directories=no,width=600,height=300", true, 600, 300);
		}

		if(mlngAppMsgTries==MAX_TRIES_BEFORE_MSG){
			mlngAppMsgTries=0;
		}else{
			mlngAppMsgTries++;
		}
	}
	
	function GetRspObj(strResponse){
		var objResponse =new CMqHttpResponse(strResponse);
		return objResponse;
	}
	
	function SetUseCache(blnCache)
	{
		m_blnUseCache=blnCache;
	}
	
	function GetUseCache()
	{
		return m_blnUseCache;
	}
	
	function ClearCache()
	{
		m_varCachedResponse=null;
	}
	function GetAppRequest()
	{
		return appRequest;		
	}
	function SetAppRequest(objAppRequest)
	{
		appRequest=objAppRequest;
	}
	function WriteAppletHTML(strCodeBase,objDocument)
	{
		if(USE_APPLET){
			if(!objDocument){
				objDocument=document;
			}
			if(objDocument){
				objDocument.write(GetAppletHTML(strCodeBase,objDocument));
			}
		}
	}
	
  function GetRsp(strURL)
  {
    return GetRspObj(Get(strURL));
  }

	this.setParam=setParam;
	this.Get=Get;
	this.Post=Post;
	this.GetAppRequest=GetAppRequest;
	this.SetAppRequest=SetAppRequest;
	this.Response=GetRspObj;
	this.SetUseCache=SetUseCache;
	this.GetUseCache=GetUseCache;
	this.ClearCache=ClearCache;
	this.CANCEL_PROCESS=CANCEL_PROCESS;
	this.WriteAppletHTML=WriteAppletHTML;
	this.IsAppletLoaded=IsAppletLoaded;
	this.OpenJavaVMHelpWindow=OpenJavaVMHelpWindow;
	this.GetRsp=GetRsp;
	this.IsJavaEnabled=IsJavaEnabled;
	
	//private methods
	function GetAppletHTML(strCodeBase,objDocument)
	{
		var strHtml="";
		if (objDocument){							

			strHtml="<applet codebase='" + strCodeBase +"' ";
			strHtml+=" code='MqHttpRequest.class' archive='MqHttpRequest.class' width=1 height=1 id='MqHttpRequest' name='MqHttpRequest' MAYSCRIPT>";
			strHtml+="<param name=\"browserCookie\" value=\"" + objDocument.cookie + "\">";
			strHtml+="<param name=\"charset\" value=\"" + DEF_CHSET + "\"><param name=\"debug\" value=\"false\"></applet>";
			
		}				
		return strHtml;
	}
	function IsCached(strUrl,varCachedResponse){
		var lngCount;
		var lngIndex=INVALID_INDEX;
		if (varCachedResponse){
			for (lngCount=0;lngCount<varCachedResponse.length;lngCount++){
				if (varCachedResponse[lngCount][ARR_URL]==strUrl){
					lngIndex=lngCount;
					break;
				}
			}
		}
		return lngIndex;
	}
	function GetCachedResponse(lngIndex,varCachedResponse){
		var lngCount;
		var strFound;		
		strFound="";
		if(varCachedResponse){
			strFound=varCachedResponse[lngIndex][ARR_OUTPUT];
		}
		return strFound;
	}
		
	function SetCachedResponse(strUrl,strReponse){
		if (!m_varCachedResponse){
			m_varCachedResponse=new Array();	
		}
		if (strReponse!= CANCEL_PROCESS){			
			m_varCachedResponse[m_varCachedResponse.length]=new Array(strUrl,strReponse);		
		}		
	}	
}

function CMqHttpResponse(strResponse){
	//constructor *****
	var m_strResponse=unescape(strResponse);
	var m_strRefLocation;
	if (g_MqRef.RefLocation){
	  m_strRefLocation=g_MqRef.RefLocation;
	}else{
	  m_strRefLocation="./";
	}
  
	var ERROR_ITEM="Err";
	var CONTENT_ITEM="Content";
	var m_objString=new MqString();	
	var m_strErrors;
	var self=this;
  
  try{
    this.IsOK=GetContent().GetText()==MSG_TRUE;
  }catch(e){
    this.IsCancel=true;
  }
  
	function HasErrors()
	{
		var blnHasErrors=false;		
		m_strErrors= m_objString.GetPropertyValue(ERROR_ITEM,m_strResponse,true);
		if(m_strErrors){
			blnHasErrors=true;
		}
		return blnHasErrors;
	}
	function GetErrors(blnUnescape){
		var objErrors;
		
		if (blnUnescape==null){
			blnUnescape=true;
		}
		
		if (m_strResponse){
			if(!m_strErrors){
				m_strErrors= m_objString.GetPropertyValue(ERROR_ITEM,m_strResponse,true);
			}
			if (m_strErrors){
				objErrors= new CMqMessageCol(m_strErrors,blnUnescape);
				if (objErrors.Count==0){
					objErrors=null;
				}
			}
		}
		return objErrors;
	}
	function GetContent(blnUnescape)
	{
		var objMessage;
		if (blnUnescape==null){
			blnUnescape=true;
		}
		
		if (m_strResponse){
			var strContent=m_objString.GetPropertyValue(CONTENT_ITEM,m_strResponse,true);
			if (strContent){
				objMessage= new CMqMessage();
				objMessage.Load(strContent,blnUnescape);
			}			
		}
		return objMessage;
	}
 
    function ProcessErrors(strMsgMask,blnShowFirstOnly,objWindow)
    {
        var objErrors=GetErrors();
        var strErrStr=m_strErrors;
        if (objErrors && strErrStr){
            var blnCancel=false;
            var arrErrors=objErrors.Items();
                    
            if (arrErrors){
                for (i=0;(i<arrErrors.length) && (!blnCancel);i++){
                    blnCancel=self.ProcessSingleError(arrErrors[i],objErrors);
                }

                if (!blnCancel){
                    if ((arrErrors.length==1) || blnShowFirstOnly){
                        var objError=arrErrors[0];
                        var strErr;
                        if (strMsgMask){
                            strErr=m_objString.Format(strMsgMask,objError.GetText(),objError.GetAdditionalInfo() );
                        }else{
                            strErr=objError.GetText();
                            strErr+="\n" + objError.GetAdditionalInfo();
                        }
                        if (strErr){
							if(objWindow){
								objWindow.alert(strErr);
							}else{
								alert(strErr);
							}
                        }
                    }else{
                        if (strErrStr){
                            strErrStr=escape(strErrStr);
                            if (window.BrowserDetector){
                                if (window.BrowserDetector.minor=="5.01"){
                                    // Bug in ie 5.01: decodes urls
                                    strErrStr=escape(strErrStr);
                                }
                            }
                            OpenModalDialog(m_strRefLocation + "HTML/MsgViewer.htm?Msgs=" + strErrStr,
                                "MsgViewer","",false,600,480);
                        }
                    }
                }
            }
        }
    }
    
    function SetRefLocation(strRefLocation)
    {
        m_strRefLocation=strRefLocation;
    }
    
	this.GetErrors=GetErrors;
	this.GetContent=GetContent;
	this.HasErrors=HasErrors;
	this.ProcessSingleError=function(){return false;};
	this.ProcessErrors=ProcessErrors;
	this.SetRefLocation=SetRefLocation;
}


function CMqMessage()
{
	var m_strType;
	var m_strText="";
	var m_lngAction=0;
	var m_strSource="";
	var m_strAdditionalInfo="";
	var m_objHelp;
	var m_strActionSource="";
	var m_strExcludeInstances="";
	var m_lngNumber=0;

	
	function SetNumber(lngNumber)
	{
		m_lngNumber=lngNumber;
	}
	
	function GetNumber()
	{
		return m_lngNumber;
	}
	function GetText()
	{
		return m_strText;
	}
	
	function SetText(strText)
	{
		m_strText=strText;
	}
	
	function GetType()
	{
		return m_strType;
	}
	
	function SetType(strMessageType)
	{
		m_strType=strMessageType;
	}
	function GetAction()
	{
		return m_lngAction;
	}
	
	function SetAction(lngAction)
	{
		m_lngAction=lngAction;
	}
	
	function GetSource()
	{
		return m_strSource;
	}
	
	function SetSource(strSource)
	{
		m_strSource=strSource;
	}
	
	function GetHelp()
	{
		return m_objHelp;
	}
	
	function SetHelp(objHelp)
	{
		m_objHelp=objHelp;
	}
	
	function GetAdditionalInfo()
	{
		return m_strAdditionalInfo;
	}
	
	function SetAdditionalInfo(strAdditionalInfo)
	{
		m_strAdditionalInfo=strAdditionalInfo;
	}
	
	function GetActionSource()
	{
		return m_strActionSource;
	}
	
	function SetActionSource(strActionSource)
	{
		m_strActionSource=strActionSource;
	}
	
	function SetExcludeInstances(strExcludeInstances)
	{
		m_strExcludeInstances=strExcludeInstances;
	}
	function GetExcludeInstances()
	{
		return m_strExcludeInstances;
	}
	
	
	function Load(strMessage,blnUnescape,strChildrenDelim)
	{
		var MSG_TYPE_INDX=0;
		var MSG_TEXT_INDX=1;
		var MSG_SOURCE_INDX=2;
		var MSG_ADD_INFO_INDX=3;
		var MSG_ACTION_INDX=4;
		var MSG_NUMBER_INDX=5;
		var CHILDREN_DELIM=";";
		if (blnUnescape==null){
			blnUnescape=true;
		}
		if (!strChildrenDelim){
			strChildrenDelim=CHILDREN_DELIM;
		}
		if(strMessage){
			var varMessageInfo=strMessage.split(strChildrenDelim);
			m_strText=unescape(varMessageInfo[MSG_TEXT_INDX]);
			m_strType=unescape(varMessageInfo[MSG_TYPE_INDX]);
			m_lngAction=unescape(varMessageInfo[MSG_ACTION_INDX]);
			m_strSource=unescape(varMessageInfo[MSG_SOURCE_INDX]);
			m_strAdditionalInfo=unescape(varMessageInfo[MSG_ADD_INFO_INDX]);
			m_lngNumber=unescape(varMessageInfo[MSG_NUMBER_INDX]);
		}
	}
	
	this.GetText=GetText;
	this.SetText=SetText;
	this.GetType=GetType;
	this.SetType=SetType;
	this.GetSource=GetSource;
	this.SetSource=SetSource;
	this.GetHelp=GetHelp;
	this.SetHelp=SetHelp;
	this.GetAdditionalInfo=GetAdditionalInfo;
	this.SetAdditionalInfo=SetAdditionalInfo;
	this.GetActionSource=GetActionSource;
	this.SetActionSource=SetActionSource;
	this.SetExcludeInstances=SetExcludeInstances;
	this.GetExcludeInstances=GetExcludeInstances;
	this.SetNumber=SetNumber;
	this.GetNumber=GetNumber;
	this.GetAction=GetAction;
	this.SetAction=SetAction;
	this.Load=Load;
  
  this.MSG_TRUE=MSG_TRUE;
		
}

function CMqMessageCol(strMessages,blnUnescape,strDelim)
{
	var m_lngCount=0;
	var DELIM="&";
	var m_varMessages= new Array();
	//start constructor 
	Load(m_varMessages,strMessages,blnUnescape,strDelim);
	//end constructor 	
	
	function Item(lngIndex)
	{
		return m_varMessages[lngIndex];
	}
	
	function Exists(lngIndex)
	{
		var blnExists=false;
		if (Item(lngIndex)!=null){
			blnExists=true;
		}
		return blnExists;
	}
	function Items()
	{
		return m_varMessages;
	}
	function Count()
	{		
		return m_lngCount;
	}
	
	
	this.Item=Item;
	this.Exists=Exists;
	this.Items=Items;
	this.Count=Count;
	
	//private methods
	function Load(varMessages,strMessages,blnUnescape, strDelim, strChildrenDelim)
	{
		if (!strDelim){
			strDelim=DELIM;
		}
		if (blnUnescape==null){
			blnUnescape=true;
		}
		if (strMessages){
			var varMessageIDs =strMessages.split(strDelim);
			if (varMessageIDs){
				var strMessage;
				var objMessage;
				var strMessageType;
				for(var i=0;i<varMessageIDs.length;i++){
					strMessage =varMessageIDs[i];
					if(strMessage){
						objMessage=new CMqMessage();
						objMessage.Load(strMessage,blnUnescape,strChildrenDelim);
						strMessageType=String(objMessage.GetType());
						if (strMessageType){
							varMessages[m_lngCount]=objMessage;
							m_lngCount++;
						}
					}
				}
			}
		}
	}
}

