addEvent(window, "load", preparePopups);

var popupCount = 0;

function preparePopups() {
  var inputs;
  var thisInput;
  var loop;

  if (!document.getElementsByTagName) return;
  inputs = document.getElementsByTagName("div");
  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty
    if ((' '+thisInput.className+' ').indexOf("popUp") != -1) {
      addEvent(thisInput, "click", closePopup);
    }
  }

  inputs = document.getElementsByTagName("a");
  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty
    if ((' '+thisInput.className+' ').indexOf("popUpData") != -1) {
			addEvent(thisInput, "click", newPopupEvent);
    }
  }
}

function newPopupEvent(e) {
	var theEvent = e || event;
  var theElement = theEvent.srcElement || theEvent.target;
	
	stopEvent(e);
	
	if (theElement.tagName.toUpperCase() != 'A') {
		theElement = theElement.parentNode;
	}
	
	debug("The elemnt id = " + theElement.id);
	
  //Create the popup
  popupCount=0;

  //Fetch the data
  var contentArray = theElement.id.split("_");
  var paramCount = contentArray.length - 4;
  var paramString = "";

  var count = 0;
  for(loop = 0; loop < paramCount;loop++) {
    count++;
    paramString = paramString + "&param" + (count) + "=" + contentArray[(count + 2)];
  }

  xmlString = getpathtoxml() + "?action=xmlFunctionExec&function=" + contentArray[2] + "&parameters=" + paramCount + "&linkToSpan=thickbox" + paramString;

	var widthHeightArray = contentArray[contentArray.length - 1].split("x");
	if (widthHeightArray[0] != 0) {
		xmlString = xmlString + "&width=" + widthHeightArray[0];
	} else {
		xmlString = xmlString + "&width=800";
	}
	
	if (widthHeightArray[1] != 0) {
		xmlString = xmlString + "&height=" + widthHeightArray[1];
	} else {
	}

  debug(xmlString);
  tb_show(contentArray[1], xmlString);
	
	return(false);
}


function newPopup(windowTitle, content) {
  //Create the popup
  popupCount=0;

  //Fetch the data
  var contentArray = content.split(";");
  var paramCount = contentArray.length - 1;
  var paramString = "";

  var count = 0;
  for(loop = 0; loop <= paramCount;loop++) {
    count++;
    paramString = paramString + "&param" + (count) + "=" + contentArray[(count)];
  }

  xmlString = getpathtoxml() + "?action=xmlFunctionExec&function=" + contentArray[0] + "&parameters=" + paramCount + "&linkToSpan=thickbox" + paramString + "&width=800";

  tb_show(windowTitle, xmlString);
}

function closePopup(e) {
  var theEvent = e || event;
  var theElement = theEvent.srcElement || theEvent.target;

  if (theEvent.type == "click") {
    if(theElement.id == "closeButton") {
      thePopup = getParent(theElement, "div");
      theBody = getParent(thePopup, "body");

      //thePopup.innerHTML = "";
      theBody.removeChild(thePopup);
    }
  }

  return(0);
}
