


var http_request = null;
var gOutputType = null;
var gCount = 0;
gFocus = null;
 function getPrice(outputType) {
	gOutputType = parseInt(outputType);
	 if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	 	if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
 	 } else if (window.ActiveXObject){
      	http_request = new ActiveXObject("Microsoft.XMLHTTP");
  	} else {
      return;
  	}
 	http_request.onreadystatechange = setPrice;
    http_request.open ('GET', "http://www.weba11y.com/AjaxDemo/cgi-bin/rand.pl", true);
    http_request.send (null);
  }

  function setPrice() {
  	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var randVal = parseFloat(http_request.responseText);
			randVal = randVal * 10; 			var resetStyle = "";
			var priceLoc = document.getElementById("priceLoc");
			curValue = parseFloat(priceLoc.value); 
			//curValue = parseFloat(priceLoc.firstChild.nodeValue); 
			var isUp = (gCount++ % 2);
			var newPrice;
			var symbol = document.getElementById("symbol").firstChild.nodeValue;
			if (isUp) {
				newPrice = curValue + randVal;
			}
			else {
				newPrice = curValue - randVal;
			}
			newPrice = newPrice.toFixed(2); 
			// always set newprice
			priceLoc.value  = newPrice; //priceLoc.firstChild.nodeValue  = newPrice;
			switch(gOutputType) {
				case 0:
					resetStyle = "roInput";
					priceLoc.className = "notice";
		    		setTimeout("clearActive('priceLoc','" + resetStyle +"');", 2000); // clear the background;
				break;
				case 1:
					var ptext = "Current Price of " + symbol + ": $"  + newPrice;
					window.alert(ptext);

				break;
		    	case 2:
		    		var alert = document.getElementById("curAlert");
		    		var ptext = "Current Price of " + symbol + ": $"  + newPrice;
		    		if (alert == null || alert == undefined) {
		    			alert = document.createElement("div");
		    			var text = document.createTextNode(ptext);
		    			alert.appendChild(text);
		    			alert.className="popup";
		    			alert.id="curAlert";
		    			document.body.appendChild(alert);
		    			alert.setAttribute("role", "wairole:alert");
		    		}
		    		else { // update the time
		    			text = alert.firstChild;
		    			text.data = ptext;
		    		}
		    		alert.style.display="block";
		    	//	gFocus=alert;
		    	//	setTimeout("gFocus.alert();", 0);
		    		setTimeout("clearAlert();", 5000);
		    		alert.focus();
				break;

		    }// end switch





	     } else {
	          alert('There was a problem with the request for the current price.');
	     }
	}

    }

function updatePrice(type) {

	if (type == 3) {
		getPrice(0);
		document.getElementById('priceLoc').focus();
	}
	else if (type==4) {
		getPrice(1);
	}

}

function clearAlert() {
	var alert = document.getElementById("curAlert");
	if (alert) {
		alert.style.display="none";
		document.body.removeChild(alert);

	}
}

function clearActive(activeId, resetStyle) {
	var curActive = document.getElementById(activeId);
	curActive.className = resetStyle;
}

function priceLoop(type) {
	setInterval("getPrice(" + type + ");",10000);
}

function setAppRole() { 
	var theBody = document.body;
	theBody.setAttribute("role", "wairole:application");
}

var nsMapping = new Array();
  nsMapping["http://www.w3.org/TR/xhtml2"] = "x2:";
  nsMapping["http://www.w3.org/2005/07/aaa"] = "aaa:";

  function setAttrNS(element, nameSpace, attr, attrValue) {
  	var result = null;

  	if (element.setAttributeNS) {
  		result = element.setAttributeNS(nameSpace, attr, attrValue);
  	}
  	else if (element.setAttribute) {
  		// for user agents that do not support setAttributeNS(), use the name space parameter  to map to the appropriate
  		// prefix. This will make the attribute names match up with those set from the setAttributeNS() calls.
  		// The nsMapping array (above) must be set up properly so that the strings in the array match the actual namespaces
  		// in the xmlns parameters in the html element of the page.

  		result = element.setAttribute((nsMapping[nameSpace] + attr), attrValue);
  	}
  	return result;
}

function getAttrNS(element, nameSpace, attr) {
	var result = null;

	if (element.getAttributeNS) {
		result = element.getAttributeNS(nameSpace, attr);
		//alert("getAttributeNS() result is " + result);
	}
	else if (element.getAttribute) {
		// for user agents that do not support getAttributeNS(), use the name space parameter  to map to the appropriate
		// prefix. This will make the attribute names match up with those returned from the getAttributeNS() calls.
		// The nsMapping array (above) must be set up properly so that the strings in the array match the actual namespaces
		// in the xmlns parameters in the html element of the page.

		result = element.getAttribute(nsMapping[nameSpace] + attr);

	}

	return result;
}