var syncnow = false;

if ( navigator.userAgent.indexOf("MSIE 7.") != -1 )
	syncnow = true ;

if ( navigator.userAgent.indexOf("Firefox/4.") != -1 )
	syncnow = true ;

// current active command
var activeCommand = null;

// get the instance to the xmlhttp function
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = null;
      }
    }
  @else
  xmlhttp = null;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = null;
    }
  }

  return xmlhttp;
}

//This is basic function that executes the command
function ExecuteCommand(commandname,command,arguments,responseHandler,cleanup)
{
	DisplayMessage("Please Wait...");
	activeCommand = new ActiveCommand(commandname,command,arguments,responseHandler,cleanup,false);
	activeCommand.send();
}

function ExecuteCommandExt(commandname,command,arguments,responseHandler,cleanup)
{
	DisplayMessage("Please Wait ...");
	activeCommand = new ActiveCommandExt(commandname,command,arguments,responseHandler,cleanup,false);
	activeCommand.send();
}

function ExecuteCommand2(commandname,command,arguments,responseHandler,cleanup)
{
	DisplayMessage("Please Wait ...");
	activeCommand = new ActiveCommand(commandname,command,arguments,responseHandler,cleanup,true);
	activeCommand.httperror = null;
	activeCommand.send();
}

function ExecuteCommandExt2(commandname,command,arguments,responseHandler,cleanup)
{
	DisplayMessage("Please Wait ...");
	activeCommand = new ActiveCommandExt(commandname,command,arguments,responseHandler,cleanup,false);
	activeCommand.sync = false;
	activeCommand.send();
}

function ActiveCommandExt(commandname,command,arguments,responseHandler,cleanup,isdirect)
{
	this.name = commandname;
	if ( isdirect )
		this.command = command + "?" + arguments;
	else
		this.command = "./XmlRequestExt?action=" + command + "&" + arguments;
	this.responseHandler = responseHandler
	this.send = ActiveCommandSend;
	this.cleanup = cleanup;
	this.httperror = httperror ;
	this.httpobject = null;
	this.sync = true ;
}

function ActiveCommand(commandname,command,arguments,responseHandler,cleanup,isdirect)
{
	this.name = commandname;
	if ( isdirect )
		this.command = command + "?" + arguments;
	else
		this.command = "./XmlRequest?action=" + command + "&" + arguments;
	this.responseHandler = responseHandler
	this.send = ActiveCommandSend;
	this.cleanup = cleanup;
	this.httperror = httperror ;
	this.httpobject = null;
	this.sync = true ;

}


function ActiveCommandSend()
{
	this.httpobject = getHTTPObject();

	if ( this.httpobject == null )
	{
		DisplayMessageError("Connection object missing, Please contact support for more information");
	}
	else
	{
		this.httpobject.open("GET", this.command , 	this.sync);
		this.httpobject.onreadystatechange = BaseResponce;
		this.httpobject.send(null);

		try{
			if (this.sync==false && syncnow == false )
				BaseResponce();
		}
		catch (e){}
	}
}

function BaseResponce()
{
  switch (activeCommand.httpobject.readyState)
  {
	  case 4:
	  	DisplayMessage("Command Run ..." ) ;
	  	try
		{
	  		if ( activeCommand.httpobject.status == 200 )
			{
				if ( activeCommand.responseHandler != null )
				{
					if ( activeCommand.responseHandler  ( activeCommand.httpobject) && activeCommand.cleanup != null )
						activeCommand.cleanup();
				}
			}
			else if ( activeCommand.httperror != null )
			{
				activeCommand.httperror();
			}
			else if ( activeCommand.httperror == null )
			{
				if ( activeCommand.responseHandler  ( activeCommand.httpobject) && activeCommand.cleanup != null )
					activeCommand.cleanup();
			}
		}
		catch (e)
		{
			alert (e);
		}
		finally
		{
		}
		break;
	  case 2:
 	  case 3:
	  	DisplayMessage("Connecting ..." ) ;
		break;
	 }
}

function httperror()
{
	var messagetext = " As error has occured access in the system " + this.httpobject.status + " (" + this.httpobject.statusText + ")"
				+ " If the error Persisist please contact support";

	DisplayMessageError(messagetext);

}

function DisplayMessage(messagetext)
{
	try	{
		var message = document.getElementById("feedbackmessage");

		if ( message == null )
			message = parent.document.getElementById("feedbackmessage");


		if ( message != null ) {
			message.innerHTML = messagetext;

			var icon = document.getElementById("messagedialogtype");
			if ( icon != null )
				icon.src="/images/dinfo.gif";
		}
	}
	catch(e){
	}
}

function DisplayMessageError(messagetext)
{

	var message = document.getElementById("feedbackmessage");
	if ( message == null )
	{
		alert("Error = " + messagetext) ;
	}
	else
	{
		var icon = document.getElementById("messagedialogtype");
		if ( icon != null )
			icon.src="/images/derror.gif";
		var ok = document.getElementById("messagedialog_ok");
		if ( ok != null )
			ok.disabled = false;
		message.innerHTML = messagetext;
	}
}

function TranslateField(fieldvalue)
{
	return fieldvalue.replace('$','£' ) ;
}


function StdHandleError(httpobject)
{
	var error = httpobject.responseXML.getElementsByTagName("error");
	if  (error.length > 0 )
	{
		DisplayMessageError (error[0].firstChild.data ) ;
		return true;
	}

	return false;
}

function _getXmlData(httpobject,tag)
{
try {
    return httpobject.responseXML.getElementsByTagName(tag)[0].firstChild.data;
} catch(e) { return ""; }
}

function _getXmlDecode(httpobject,tag)
{
	var data = _getXmlData(httpobject,tag);

	data = data.replace(/&amp;/g,"&");

	return data;
}
