var isIE = false;
var Jackpots = new Object();
var postLoadFunction = null;

// jp object constructor
jp = function (pId, pType, pValue, pGrowth) {

	var me = this;
	var _id = '';
	var _value = 17500;
	var _growth = .01;
	var _type = "progressive";

  this.getId = getId;
  this.setId = setId;
  this.getValue = getValue;
  this.setValue = setValue;
  this.getGrowth = getGrowth;
  this.setGrowth = setGrowth;
  this.getType = getType;
  this.setType = setType;

  function setId(x) {	_id = x;  }
  function getId() {	return _id;  }
  function setValue(x) {	_value = x;  }
  function getValue() {	return _value;  }
  function setGrowth(x) {	_growth = x;  }
  function getGrowth() {	return _growth;  }
  function setType(x) {	_type = x;  }
  function getType() {	return _type;  }

  setId(pId);
  if (pType) {
	setType(pType);
    _growth = pType == "progressive" ? 1 : 0;
  }
  if (pValue) {setValue(pValue)}
  if (pGrowth) {setGrowth(pGrowth)}
  Jackpots[pId] = me;
}

function displayJP(msg) {
  var s = msg + "\n\n";
  for (var i in Jackpots) {
	//	s = s +  i + ": " + "typeof i : " + typeof i + "typeof Jackpots[i]: "  + typeof Jackpots[i] + "\n";
	with (Jackpots[i]) {
	  s = s +  "id: " + getId() + "\n" + "type: " + getType() + "\n" + "value: " + getValue() + "\n" + getGrowth();
	}
	s = s + "\n";
  }
  alert(s);
}

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
	    isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            readJackpots();
			postLoadFunction();
         } else {
		   // alert("There was a problem retrieving the XML data:\n" + req.statusText);
        	postLoadFunction();
         }
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
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 readJackpots() {
	var id = "";
    var v = 0
    var g = 0;
	var t = "";

    var items = req.responseXML.getElementsByTagName("item");
    for (var i = 0; i < items.length; i++) {
	  id = getElementTextNS("", "title", items[i], 0);
	  v = parseFloat(getElementTextNS("gv", "amount", items[i], 0));
	  t = getElementTextNS("gv", "type", items[i], 0);
	  g = (t == "progressive") ? growth = parseFloat(getElementTextNS("gv", "growth", items[i], 0)) : 0;
	  new jp(id, t, v, g);
	}
	//displayJP("readJP");
}


function initializeJP(fname, func) {
  postLoadFunction = func;
   try {
      loadXMLDoc(fname);
   }	
   catch(e) {
	 // ignore errors;
   }
}
