function getHTTPObjectUser() {
    var xmlhttp;
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
      try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
    }
    if(!xmlhttp && typeof ActiveXObject != 'undefined'){
      try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;}
      if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;}
    }
    return xmlhttp;
}
function AJAXredir(url, target) {

    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
		location.href=target;
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function AJAXwrite(url, target) {

    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
	  document.getElementById(target).innerHTML=req.responseText;
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function AJAXwriteCallback(url, target, callback) {

    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
	  document.getElementById(target).innerHTML=req.responseText;
          if (callback){
			loadAjaxBundleUsageIntoDOM(req);
           	callback(req.responseXML);
          }
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function AJAXxmlCallback(url, callback) {

    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          if (callback){
          	loadAjaxBundleUsageIntoDOM(req);
           	callback(req.responseXML);
          }
	}
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function AJAXxmlCallbacks(url, callback1, callback2) {

    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          if (callback1){
           	loadAjaxBundleUsageIntoDOM(req);
           	callback1(req.responseXML);
          }
          if (callback2) callback2();
	}
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function AJAXxml(url) {
    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
          	if (req.responseXML) {
          		loadAjaxBundleUsageIntoDOM(req);
	      		return req.responseXML;
	  		}
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function AJAX(url) {
    var req = init();
    req.onreadystatechange = processRequest;

    function init() {
      return getHTTPObjectUser();
    }

    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
	  return true;
	}
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }

    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}
function ajaxGetRedir(url, query, target) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233) + '&' + query;
	var ai = new AJAXredir(new_url, target);
  	ai.doGet();
}

function ajaxGetXML(url, query) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233) + '&' + query;
	var ai = new AJAXxml(new_url);
  	ai.doGet();
}

function ajaxGetXMLfromForm(url, f) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxGetXML(url, query);
}

function ajaxPostXML(url, query) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233);
	var ai = new AJAXxml(new_url);
  	ai.doPost(query);
}

function ajaxPostXMLfromForm(url, f) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxPostXML(url, query);
}
function ajaxPost(url, query) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233);
	var ai = new AJAX(new_url);
  	ai.doPost(query);
}
function ajaxPostfromForm(url, f) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxPost(url, query);
}

function ajaxGetXMLCallback(url, query, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233) + '&' + query;
	var ai = new AJAXxmlCallback(new_url, callback);
  	ai.doGet();
}

function ajaxGetXMLCallbackfromForm(url, f, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxGetXMLCallback(url, query, callback);
}

function ajaxPostXMLCallback(url, query, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233);
	var ai = new AJAXxmlCallback(new_url, callback);
  	ai.doPost(query);
}

function ajaxPostXMLCallbackfromForm(url, f, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxPostXMLCallback(url, query, callback);
}

function ajaxGetXMLCallbacks(url, query, callback1, callback2) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233) + '&' + query;
	var ai = new AJAXxmlCallbacks(new_url, callback1, callback2);
  	ai.doGet();
}

function ajaxGetXMLCallbacksfromForm(url, f, callback1, callback2) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxGetXMLCallbacks(url, query, callback1, callback2);
}

function ajaxPostXMLCallbacks(url, query, callback1, callback2) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233);
	var ai = new AJAXxmlCallbacks(new_url, callback1, callback2);
  	ai.doPost(query);
}

function ajaxPostXMLCallbacksfromForm(url, f, callback1, callback2) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxPostXMLCallbacks(url, query, callback1, callback2);
}

function ajaxGetWrite(url, query, target) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233) + '&' + query;
	var ai = new AJAXwrite(new_url, target);
  	ai.doGet();
}

function ajaxGetWritefromForm(url, f, target) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxGetWrite(url, query, target);
}

function ajaxPostWrite(url, query, target) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233);
	var ai = new AJAXwrite(new_url, target);
  	ai.doPost(query);
}

function ajaxPostWritefromForm(url, f, target) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxPostWrite(url, query, target);
}

function ajaxGetWriteCallback(url, query, target, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233) + '&' + query;
	var ai = new AJAXwriteCallback(new_url, target, callback);
  	ai.doGet();
}

function ajaxGetWriteCallbackfromForm(url, f, target, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxGetWriteCallback(url, query, target, callback);
}

function ajaxPostWriteCallback(url, query, target, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var new_url = url + '?rgn=' + Math.floor(Math.random()*1100233);
	var ai = new AJAXwriteCallback(new_url, target, callback);
  	ai.doPost(query);
}

function ajaxPostWriteCallbackfromForm(url, f, target, callback) {
	if(document.getElementById('progressDiv') != null) {
		document.getElementById('progressDiv').style.visibility='visible';
	}

	var query = formIntoString(f);
	ajaxPostWriteCallback(url, query, target, callback);
}

function formIntoString(docForm) {
	var query = '';
	var formElem;
	for (i=0;i< docForm.elements.length; i++) {
		formElem = docForm.elements[i];

		switch (formElem.type) {
			case 'radio':
				if(formElem.checked) {
					query += formElem.name + '=' + encodeURIComponent(formElem.value) + '&'
				}
				break;
			case 'checkbox':
				if (formElem.checked) {
					query += formElem.name + '=' + encodeURIComponent(formElem.value) + '&'
				}
				break;
			case 'select-multiple':
				for (j=0;j< formElem.options.length;j++) {
					if (formElem.options[j].selected) {
						query += formElem.name + '=' + encodeURIComponent(formElem.options[j].value) + '&'
					}
				}
				break;
			default:
				query += formElem.name + '=' + encodeURIComponent(formElem.value) + '&'
				break;
		}
	}
	return query;
}



function loadAjaxBundleUsageIntoDOM(response){

	var xml = response.responseXML;

	if(xml){

		var divElements = xml.getElementsByTagName('div');

		//if there are any <div> elements in the xml response that have an id that starts with "BundleUsage", then load them into the
		//DOM into a div with the id of "AjaxXmlBundleUsage". generated by BundleUsageLinker.jsp

		if(divElements.length > 0){

			for(i = 0 ; i < divElements.length ; i++){

				var idAttribute = divElements[i].getAttribute("id");

				if(idAttribute && idAttribute.indexOf("BundleUsage")!=-1){

					//found a bundle div, so insert into the DOM

					var targetLocation = document.getElementById("AjaxXmlBundleUsage");

					if(targetLocation){

						var div = document.createElement('div');

						div.innerHTML=divElements[i];

						targetLocation.appendChild(div)
					}
				}
			}
		}
	}
}

//perform a server side resource bundle entry lookup with the given key.
//the session's locale is used when looking for the proper translation if necessary.
function getText(key){

	var parameters = new Array();

	parameters[0]=new Parameter("String",key);

	result = executeApi("concept.core.locale.Localizer", "getString", parameters);

	return result.toString();

}

	/**
	* Execute a Java method in Concept.
	*
	* className =  the name of the class .
	* value = the method to execute.
	* parameters = array of Parameter instances.
	* callback = optional callback function. if callback provided, the call is asynchronous other it is synchronous (optional)
	* forceSynchronous = true for call to be synchronous, false for asynchronous. If present this overrides callback impact on sychronicity.(optional)
	
	* if a callback function IS NOT provided, a synchronous AJAX call is executed and this function
	* returns XmlResultWrapper instance with a property for each getter value returned.
	*
	* if a callback function IS provided, an asynchronous AJAX call is executed which will
	* execute the callback function when complete. The callback function must accept the
	* XmlResultWrapper object as a parameter.
	*
	* Using a callback and setting forceSynchronous to true, results in a synchronous call with callback execution.
	*/
	function executeApi(className, methodName, parameters ,callback, forceSynchronous){

		//if not specified, assume synchronous call.
		asynchronous=false;
		if(callback) asynchronous=true;

		//force synchronicity a certain way
		if(forceSynchronous) asynchronous = false;
	
		var parameterQueryString="";
		for(var i=0 ; i<parameters.length ; i++){

			parameterQueryString = parameterQueryString + "&param_"+(i+1)+"_"+encodeURIComponent(parameters[i].parameterClass)+"="+encodeURIComponent(parameters[i].parameteValue);
		}

		var url = "/JavaScriptBridge?command=Execute&class="+encodeURIComponent(className)+"&method="+encodeURIComponent(methodName)+parameterQueryString;

		if(!asynchronous ){
			if (window.XMLHttpRequest) {

			    AJAX=new XMLHttpRequest();

			} else {

			    AJAX=new ActiveXObject("Microsoft.XMLHTTP");

			}

			result = "TEST";

			if (AJAX) {

			     AJAX.open("GET", url, asynchronous);  //synchronous call for this functionality.

			     AJAX.send(null);

			     result = AJAX.responseXML;

			}

			if(callback){
				//synchronous callback
				callback(new XmlResultWrapper(result));
			}else{
				//synchronous return result.
				return new XmlResultWrapper(result);
			}
		}else{
			//asynchronous with callback
			var ai = new ExecuteApiAjax(url, asynchronous, callback)
  			ai.doGet();

		}
	}

	/*
	* Special AJAX object for performing asynchronous calls with a callback
	* that expects a wrapped result for the Java API call.
	*/
	function ExecuteApiAjax(url, asynchronous, callback) {

	    var req = init();
	    req.onreadystatechange = processRequest;

	    function init() {
	      if (window.XMLHttpRequest) {
	        return new XMLHttpRequest();
	      } else if (window.ActiveXObject) {
	        return new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    }

	    function processRequest () {
	      if (req.readyState == 4) {
	        if (req.status == 200) {
	          if (callback){
	          	//wrap the result and perform the callback function
				var result = new XmlResultWrapper(req.responseXML)
	           	callback(result);
	          }
			}
	      }
	    }

	    this.doGet = function() {
	      req.open("GET", url, asynchronous);
	      req.send(null);
	    }

	    this.doPost = function(body) {
	      req.open("POST", url, asynchronous);
	      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	      req.send(body);
	    }
	}

	/*
	* Used to create an array of parameters for the executeApi function above
	*
	* className =  the name of the class of the parameter
	* value = the value of parameter
	*/
	function Parameter(className, value){

		this.parameterClass = className;
		this.parameteValue = value;  //if the value looks like "session:blah" then this identifies a session variable to be used as a parameter

	}

	/*
	* returned as result of executeApi which wraps the xml
	* and creates a property for each "<get...>" and "<is...>" element in the xml
	*/
	function XmlResultWrapper(xml){

		this.xmlData = xml;

		/*
		* generic getter function that can get any property's value from the xml.
		*/
		this.get = function( property ){

			var value="";
			objectElement = xml.getElementsByTagName(property)[0];

			if(objectElement.firstChild != null){
				value = objectElement.firstChild.nodeValue;
			}

			return value;
		}


		//dynamically add a "get" and 'is' functions to this object for each element within the xml object tag.
		//these minic the getter methods
		objectElement = xml.getElementsByTagName("object")[0];

		for( x =0 ; x < objectElement.childNodes.length ; x++){

				//add this xml element's data to this object as a property
				var value="";
				if(objectElement.childNodes[x].firstChild != null){
					value = objectElement.childNodes[x].firstChild.nodeValue;

				}
				var evalCode="this."+objectElement.childNodes[x].tagName+" = function() { return this.get('"+objectElement.childNodes[x].tagName+"'); } " ;

				eval(evalCode);



		}


	}

