function XMLWriter()
{
    this.XML=[];
    this.Nodes=[];
    this.State="";
    this.FormatXML = function(Str)
    {
        if (Str)
            return Str.toString().replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        return ""
    }
    this.BeginNode = function(Name)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.State="beg";
        this.Nodes.push(Name);
        this.XML.push("<"+Name);
    }
    this.EndNode = function()
    {
        if (this.State=="beg")
        {
            this.XML.push("/>");
            this.Nodes.pop();
        }
        else if (this.Nodes.length>0)
            this.XML.push("</"+this.Nodes.pop()+">");
        this.State="";
    }
    this.Attrib = function(Name, Value)
    {
        if (this.State!="beg" || !Name) return;
        this.XML.push(" "+Name+"=\""+this.FormatXML(Value)+"\"");
    }
    this.WriteString = function(Value)
    {
        if (this.State=="beg") this.XML.push(">");
        this.XML.push(this.FormatXML(Value));
        this.State="";
    }
    this.Node = function(Name, Value)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.XML.push((Value=="" || !Value)?"<"+Name+"/>":"<"+Name+">"+this.FormatXML(Value)+"</"+Name+">");
        this.State="";
    }
    this.CDATASectionNode = function(Name, Value)
    {
        if (!Name) return;
        if (this.State=="beg") this.XML.push(">");
        this.XML.push((Value=="" || !Value)?"<"+Name+"/>":"<"+Name+"><![CDATA["+this.FormatXML(Value)+"]]></"+Name+">");
        this.State="";
    }
    this.Close = function()
    {
        while (this.Nodes.length>0)
            this.EndNode();
        this.State="closed";
    }
    this.ToString = function(){return this.XML.join("");}
}

function GetNodeValue(Node)
{
    var _value;
    if(Node.childNodes.length>0)
    {
        _value = Node.childNodes[0].nodeValue;
    }
    else
    {
        _value = Node.nodeValue;
    }    
    if ((_value) && (_value != null) && (_value != ""))
    {
        return _value;
    }
    return "";
}
function GetChildNodeValue(Node,ChildNodeName)
{
    var _nodeList = Node.getElementsByTagName(ChildNodeName);
    if (_nodeList.length > 0)
    {
        return GetNodeValue(_nodeList[0]);
    }
    return "";
}

function AjaxFormatRequest(method,paramFunction)
{
    var XML=new XMLWriter();
    XML.BeginNode("ajax");
        XML.BeginNode("head");
            XML.Node("visirorid" ,Cookies['VisitorID']);
            XML.Node("visirotuid",Cookies['VisitorUID']);          
            XML.Node("version",Cookies['Version']);     
            XML.Node("method",method);
       XML.EndNode();
       XML.BeginNode("request");  
            paramFunction(XML);
       XML.EndNode();
    XML.EndNode();
    XML.Close();          
    return 'ajax_request=' + XML.ToString();
}

function AjaxFormatResponse(XmlString,XmlProcessingFunction,HideProgressFunction)
{
    var xmlDoc;
    if (XmlString)
    {
        XmlString = XmlString.replace(/&/g,"&amp;");
    }
    try //Internet Explorer
    {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(XmlString);
    }
    catch(e)
    {
      try //Firefox, Mozilla, Opera, etc.
      {
          parser=new DOMParser();
          xmlDoc=parser.parseFromString(XmlString,"text/xml");
      }
      catch(e)
      {
          alert(e.message);
          if (HideProgressFunction) HideProgressFunction();
          return;
      }
    }
    try
    {
        if (xmlDoc && xmlDoc.documentElement && (xmlDoc.documentElement.tagName == "ajax"))
        {
            // process success element from head
            var headNode = xmlDoc.getElementsByTagName("head")[0];
            if (headNode)
            {
                var successNode = headNode.getElementsByTagName("success")[0];
                if (GetNodeValue(successNode.firstChild) == "1")
                {
                    // process response
                    var responseNode = xmlDoc.getElementsByTagName("response")[0];
                    if (XmlProcessingFunction) XmlProcessingFunction(responseNode, null);
                    if (HideProgressFunction) HideProgressFunction();
                }
                else
                {
                    // display error massages
                    var errorMessage = "";
                    var errorListNode = xmlDoc.getElementsByTagName("errorlist")[0];
                    if (errorListNode)
                    {
                        for (var i=0;i<errorListNode.childNodes.length;i++)
                        {
                            errorMessage += GetNodeValue(errorListNode.childNodes[i].firstChild);
                            errorMessage += "\r\n";
                        }
                        if (XmlProcessingFunction) 
                        {
                            XmlProcessingFunction(null, errorMessage);
                        }
                        else
                        {                        
                            alert(errorMessage);
                        }
                    }
                    else
                    {
                        if (XmlProcessingFunction) 
                        {
                            XmlProcessingFunction(null, "Error list is not defined.");
                        }
                        else
                        {                        
                            alert ("Error list is not defined.");
                        }                        
                    }
                    if (HideProgressFunction) HideProgressFunction();
                }            
            }
            else
            {
                if (HideProgressFunction) HideProgressFunction();
            }            
        }
        else
        {
            if (HideProgressFunction) HideProgressFunction();        
        }
    }
    catch (e)
    {
        alert(e.message);
        if (HideProgressFunction) HideProgressFunction();
    }
}

function AjaxDisplayError(errorMessage,functiontodisplayerror)
{
    if (errorMessage != null)
    {
        if(functiontodisplayerror)
        {
            functiontodisplayerror(errorMessage);
        }
        else
        {
            alert(errorMessage);
        }
        return true;
    }
    return false;
}

var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();

function ajaxRequest(theURL, sendString, callbackFunction, displayProgressFunction, hideProgressFunction)
  {
  	var thisRequestObject;
  	
  	if (displayProgressFunction) displayProgressFunction();
  	
  	thisRequestObject = initiateRequest();
  	thisRequestObject.onreadystatechange = processRequest;
  	
  	function initiateRequest()
  	{
        if (typeof XMLHttpRequest != 'undefined') return new XMLHttpRequest();
        else if (window.ActiveXObject) {
            var avers = ["Microsoft.XMLHTTP", "Msxml2.XMLHTTP"]; // "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.5.0"
            for (var i = avers.length -1; i >= 0; i--) {
                try {
                    obj = new ActiveXObject(avers[i]);
                    return obj;
                } catch(e) {}
            }
        }  	
        if (hideProgressFunction) hideProgressFunction();
        throw new Error('XMLHttp (AJAX) not supported');
  	}
  	
  	function processRequest()
  	{
  		if (thisRequestObject.readyState == 4)
  		{
  			if (thisRequestObject.status == 200)
  			{
  				if (callbackFunction)
  				{
  				    AjaxFormatResponse(thisRequestObject.responseText,callbackFunction,hideProgressFunction)
  			    }
  			}
  			else
  			{
  				//alert("There was an error: (" + thisRequestObject.status + ") " + thisRequestObject.statusText);
  				if (hideProgressFunction) hideProgressFunction();
  			}
  		}
  	}
  	
  	this.sendGetData = function()
  	{
  		if (theURL)
  		{
  			thisRequestObject.open("GET", theURL, true);
  			thisRequestObject.send(sendString);
  		}
  	}
  	
  	this.sendPostData = function()
  	{
  		if (theURL)
  		{
  			thisRequestObject.open("POST", theURL, true);
  			thisRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  			thisRequestObject.send(sendString);
  		}
  	}
}


function toHtml(value)
{ 
  value = value.replace(/\&amp;/gi,'&');
  value = value.replace(/\&lt;/gi,'<');
  value = value.replace(/\&gt;/gi,'>');
  value = value.replace(/&amp;nbsp;/gi,'&nbsp;');
  value = value.replace(/&nbsp;/gi,' ');
  value = value.replace(/<br \/>/gi,'\n\r');
  return value;
}
