// JavaScript Document
function $get(id)
{
  return document.getElementById(id);
}

function openImageMgr(imgName)
{
  imageMgr = window.open("?action=imageMgr&imgName="+imgName, "ImageMgr",
                         "toolbar=1, scrollbars=0, height=400, width=600, statusbar=1");

  imageMgr.moveTo(100,100);
}

function removeImage(imgName)
{
  $get(imgName).src = '';
  $get(imgName+'ID').value = 0;
}

// Drag and drop functions
var DragHandler =
{
	// private property.
  _oElem : null,


	// public method. Attach drag handler to an element.
  attach : function(oElem)
  {
  	oElem.onmousedown = DragHandler._dragBegin;
	  // callbacks
  	oElem.dragBegin = new Function();
	  oElem.drag = new Function();
  	oElem.dragEnd = new Function();
  	return oElem;
  },

	// private method. Begin drag process.
	_dragBegin : function(e)
  {
		var oElem = DragHandler._oElem = this;
		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;
		oElem.dragBegin(oElem, x, y);
		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},

	// private method. Drag (move) element.
	_drag : function(e)
  {
		var oElem = DragHandler._oElem;
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;
		oElem.drag(oElem, x, y);
		return false;
	},

	// private method. Stop drag process.
	_dragEnd : function()
  {
		var oElem = DragHandler._oElem;
		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);
		oElem.dragEnd(oElem, x, y);
		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}
}

function createXHR()
{
  if (typeof XMLHttpRequest != "undefined")
  {
    return new XMLHttpRequest();
  }
  else
  {
    var aVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
    for (var z=0; z < aVersions.length; z++)
    {
      try
      {
        var oXHR = new ActiveXObject(aVersions[z]);
        return oXHR;
      }
      catch (oError)
      {
      }
    }
  }

  throw new Error("XMLHttpRequest or XMLHttp could not be created");
}

function loadStyleSheets(templateSelectID, styleSheetSelectDivID)
{
  var templateID = document.getElementById(templateSelectID).value;
  var styleSheetDiv = document.getElementById(styleSheetSelectDivID);
  var xhr = createXHR();
  xhr.open("POST", "/ajaxServer/getStyleSheets.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               styleSheetDiv.innerHTML = xhr.responseText;
                             }
                           }

  xhr.send("templateID="+templateID);
}

function checkDupURL(url, agentID)
{
  var dupURLSpan = $get('dupURLSpan');
  var xhr = createXHR();
  xhr.open("POST", "/ajaxServer/checkDupURL.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               dupURLSpan.innerHTML = xhr.responseText;
                             }
                           }

  if(agentID == undefined)
    xhr.send('machineURL='+url);
  else
    xhr.send('machineURL='+url+"&ID="+agentID);
    
}

function reseqMenu(menuItems)
{
  var xhr = createXHR();
  xhr.open("POST", "/ajaxServer/reseqMenu.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                             }
                           }

  var menuItemSeq = '';
  for(var z = 0; z < menuItems.length; z++)
  {
    if(z > 0)
      menuItemSeq += '&';
      
    menuItemSeq += 'menuItem[]='+menuItems[z].id.replace('menuItemDD', '');
  }

  xhr.send(menuItemSeq);
}

function reseqListings(menuItems)
{
  var xhr = createXHR();
  xhr.open("POST", "/ajaxServer/reseqListings.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                             }
                           }

  var menuItemSeq = '';
  for(var z = 0; z < menuItems.length; z++)
  {
    if(z > 0)
      menuItemSeq += '&';

    menuItemSeq += 'listing[]='+menuItems[z].id.replace('menuItemDD', '');
  }

  xhr.send(menuItemSeq);
}

function delPage(pageID, newLocation)
{
  var xhr = createXHR();
  xhr.open("POST", "/ajaxServer/delPage.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               location = newLocation;
                             }
                           }

  xhr.send('pageID='+pageID);
}

function delListing(listingID, newLocation)
{
  var xhr = createXHR();
  xhr.open("POST", "/ajaxServer/delListing.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               location = newLocation;
                             }
                           }

  xhr.send('listingID='+listingID);
}
