var l_item = [];
var lyr=null;
var lyrobj;

function resp(pResp) {
  var xmlDoc=null;
  if(window.ActiveXObject) { 
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
      xmlDoc.loadXML(pResp);
  } else {
      var parser = new DOMParser();
      xmlDoc = parser.parseFromString(pResp,"text/xml");
  }
  if(xmlDoc==null) return;
  
  for(var i=0; i<xmlDoc.childNodes[0].childNodes.length; i++) {
      var node=xmlDoc.childNodes[0].childNodes[i];
      var s="";
      if(node.text)
          s=node.text;
      else {
          for(j=0; j < node.childNodes.length; j++)
              s += node.childNodes[j].nodeValue;
      }

      var id;
      var div;
      if(node.attributes.length>0) {
          id=node.attributes[0].value;
          div=document.getElementById(id);
      }

      if(node.tagName == "con") {
          if(s.indexOf('<script>')==0){
              eval(s.replace('<script>','').replace('</script>',''));
          } else {
              if(div != null) div.innerHTML=s;
          }
      } else if(node.tagName == "val") {
          if(div != null) div.value=s;
      } else if(node.tagName == "js") {
          eval(s);
      }
  }
}

function callServer(pButton,pDiv,pWaitImage,pJS) {
    var pForm = document.forms[0];
    var qs = '';
    var temp;

    if(pButton != '') qs = pButton.replace(/_/g,'$') + "=1";
    for(var i=0; i<pForm.elements.length; i++){
        var el=pForm.elements[i];
        var add=true;
        if(el.name=="") add=false;
        if(el.type=="submit") add=false;
        if(el.type=="checkbox" && !el.checked) add=false;
        
        if (add) {
            qs+=(qs=='')?'':'&';
            temp=el.name;
            if(temp=='__VIEWSTATE') temp='x__VIEWSTATE';
            qs+=temp+'='+encodeURIComponent(el.value);
        }
    }

    var tg = "/globals/ajax.aspx?action=" + escape(getAction(pForm));
    if(typeof(ajaxPath)!="undefined") tg = ajaxPath + "?action=" + escape(getAction(pForm));
    if(pJS) tg += "&js=" + pJS;
    if (pDiv != null) {
        tg += "&id=" + pDiv;
        if(pWaitImage) {
            var id=pWaitImage.split(",");
            for (var i = 0; i < id.length; i++) {
                showWaitImage(pDiv,document.getElementById(id[i]));
            }
        }
    }

    if (!aj(resp, tg, qs)) return true;
    return false;
}

function getAction(form) {
    var action = form.action.toLowerCase();
    if (action.substr(0, 4) == "http") return action;
    if (action[0] == '/') return window.location.protocol + "//" + window.location.host + action;
    var loc = window.location.toString();
    return loc.substr(0, loc.lastIndexOf("/")) + "/" + action;
}

function showWaitImage(id,base) {
    var div = document.getElementById(id);
    if (div != null) {
        if(base==null) base=div;
        var height = base.offsetHeight;
        var width = base.offsetWidth;
        var left = findPosx(base);
        var top = findPosy(base);
//        if (document.body.leftMargin && document.compatMode != "BackCompat") {
//            left += parseInt(document.body.leftMargin);
//            top += parseInt(document.body.topMargin);
//        }
        div.innerHTML = div.innerHTML + '<div style="top:' + top + 'px;left:' + left + 'px;height:' + height + 'px;width:' + width + 'px;position:absolute; z-index:100; filter:alpha(opacity=15); -moz-opacity: 0.15;background-color:black;"></div><img style="position:absolute;left:' + (left + width / 2 - 10) + 'px;top:' + (top + height / 2 - 10) + 'px;" src="/forumx/graphics/loading5.gif" />';
    }
}

function findPos(obj) {
    var top = obj.offsetTop;
    var left = obj.offsetLeft;
    while (obj.offsetParent != null) {
        obj=obj.offsetParent;
        top += obj.offsetTop;
        left += obj.offsetLeft;
    }
    return { x:left, y:top };
}

function findPosx(obj)
{
	var curleft=0;
	if (obj.offsetParent)
	{
		while(obj.offsetParent)
		{
			curleft+=obj.offsetLeft;
			obj=obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft+=obj.x;
	return curleft;
}

function findPosy(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function aj(pCallback, pReceiver, pParams) {
  var req = newXMLHttpRequest();
  if(req==null) return false;
  var handlerFunction = getReadyStateHandler(req, pCallback);
  req.onreadystatechange = handlerFunction;
  req.open("POST", pReceiver, true);
  req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  req.send(pParams);
  return true;
}

function ajg(pCallback,pReceiver) {
  var req = newXMLHttpRequest();
  var handlerFunction = getReadyStateHandler(req, pCallback);
  req.onreadystatechange = handlerFunction;
  req.open("GET", pReceiver, true);
  req.send("");
}

/*
 * Returns a new XMLHttpRequest object, or null if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = null;

  if (window.XMLHttpRequest) {
    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      // Failed to create required ActiveXObject
      try {
        // Try version supported by older versions
        // of Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, responseXmlHandler) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {
    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseText);

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status+" "+req.responseText);
      }
    }
  }
}

function showItem(it,lang)
{
  lyrobj=it;
  lyrobj.onmouseout=hideList;
  itemid=lyrobj.id.substr(3);
  if(l_item[itemid]==null)
  {
    l_item[itemid]=' ';
    aj(respItem, '/forumx/Items.aspx?id=' + itemid + '&lang=' + lang, '');
  }
  else
  {
    respItem(l_item[itemid]);
  }
}

function respItem(resp)
{
  if(resp!=' ') 
  {
    alert(resp);
    itemid=resp.substr(0,resp.indexOf(';'));
    l_item[itemid]=resp;
    showLyr(resp.substr(resp.indexOf(';')+1));
  }
}  
