function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

function getXMLObj() {

     var req

	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) 
    {
    	try {
		req = new XMLHttpRequest();
       	} catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
	}
    }

    return req	
}

/******************
*  RequestHandler *
******************/

function RequestHandler()
{
	
}

RequestHandler.prototype.handleContentRequest = function(oXML,div)
{
	if ( oXML.readyState == 4 )
	{
		var items = oXML.responseXML.getElementsByTagName("content");
		var html = getElementTextNS("", "html", items[0], 0 );
		
		var contentDiv = document.getElementById(div);
		contentDiv.innerHTML = html;
	}
}

RequestHandler.prototype.handleOptionsRequest = function(oXML,dropdown)
{
	if ( oXML.readyState == 4 )
	{				
		var items = oXML.responseXML.getElementsByTagName("option");

		
		for (i = dropdown.options.length;i >= 0;i--)
		{
			dropdown.options[i] = null;
		}
		
		for (i = 0;i < items.length;i++)
		{
			value = getElementTextNS("", "value", items[i], 0 );
			
			text = getElementTextNS("", "text", items[i], 0 );
			dropdown.options[i] = new Option( text,  value );	
		}
	}
}

RequestHandler.prototype.handleFormPost = function(oXML,objAjax)
{
	if ( oXML.readyState == 4 )
	{	
		var items = oXML.responseXML.getElementsByTagName("content");
		var html = getElementTextNS("", "html", items[0], 0 );
		objAjax.content = html;
		
		var error = getElementTextNS("", "error", items[0], 0 );
		objAjax.error = error;
		objAjax.callbackfunction.call(objAjax);
	}
}

RequestHandler.prototype.handleXMLRequest = function(oXML,objAjax)
{
	if ( oXML.readyState == 4 )
	{
		objAjax.xml = oXML.responseXML;
		objAjax.text = oXML.responseText;
		objAjax.callbackfunction.call(objAjax);
	}
}

RequestHandler.prototype.handleJSONRequest = function(oXML,objAjax)
{
	if ( oXML.readyState == 4 )
	{
		objAjax.text = oXML.responseText;
		
		if (typeof(objAjax.callbackfunction) === "function")
		{
			objAjax.callbackfunction.call(objAjax);
		}
		else alert("callback funtion is not valid");
	}
}

/*****************
*   Ajax Class   *
******************/

var requestHandler = new RequestHandler();

function Ajax(url)
{
	this.url = url;
}

Ajax.prototype.url;
Ajax.prototype.callbackfunction;
Ajax.prototype.oXML;
Ajax.prototype.html;
Ajax.prototype.xml;
Ajax.prototype.text;
Ajax.prototype.error;
Ajax.prototype.content;
Ajax.prototype.response;

Ajax.prototype.getContent = function(div)
{
	var oXML = new getXMLObj();
	
	oXML.onreadystatechange = function()
	{
		requestHandler.handleContentRequest(oXML,div);
	}
	
	oXML.open( "GET", this.url, true );
	oXML.send(null);
}

Ajax.prototype.fillDropdown = function(dropdown)
{
    var oXML = new getXMLObj();
    
    oXML.onreadystatechange = function()
	{
		requestHandler.handleOptionsRequest(oXML,dropdown);
	}
	
	oXML.open( "GET", this.url, true );
	oXML.send(null);
}

Ajax.prototype.postForm = function(formVars,callbackfunction)
{
	this.callbackfunction = callbackfunction;
	
	var oXML = new getXMLObj();
	var objAjax = this;
	
	var strFormVars = "";
	var separator = "";
	
	for (key in formVars)
	{
			strFormVars += separator + key + "=" + formVars[key];
			separator = "&";
	}
	
	
	oXML.onreadystatechange = function()
	{
		requestHandler.handleFormPost(oXML,objAjax);
	}
	
	oXML.open( "POST", this.url, true );
	oXML.setRequestHeader("Content-type","application/x-www-form-urlencoded");

	oXML.send(strFormVars);
}


Ajax.prototype.getXML = function(callbackfunction)
{
	var oXML = new getXMLObj();
	var objAjax = this;
	this.callbackfunction = callbackfunction;
	
	oXML.onreadystatechange = function()
	{
		requestHandler.handleXMLRequest(oXML,objAjax);
		
	}
	
	oXML.open( "GET", this.url, true );
	oXML.send(null);
}

Ajax.prototype.getJSON = function(callbackfunction)
{
	var oXML = new getXMLObj();
	var objAjax = this;
	this.callbackfunction = callbackfunction;
	
	oXML.onreadystatechange = function()
	{
		requestHandler.handleJSONRequest(oXML,objAjax);
		
	}
	
	oXML.open( "GET", this.url, true );
	oXML.send(null);
}

