document.write('<script type="text/javascript" src="/apps/includes/js/ajaxRequest.js"></script>');
document.write('<script type="text/javascript" src="/apps/includes/js/utilities.js"></script>');

//START PhysicianQuickFinder Object
function PhysicianQuickFinder (prefix,redirectTo) {

	//Save prefix
	this.prefix = prefix;
	this.redirectTo = redirectTo;
	
	var pcHolder = document.getElementById(this.prefix+".postalCode");
	var tHolder = document.getElementById(this.prefix+".treatments");
	var sHolder = document.getElementById(this.prefix+".submit");
	
	if(pcHolder!=null) {
		//Init Objects
		var pcInput = document.createElement("input")
		pcInput.setAttribute("type","text");
		pcInput.setAttribute("name","postalCode");
		pcInput.setAttribute("id","postalCode");
		pcInput.setAttribute("size","5");
		pcInput.onkeydown = function(e) {
			var keynum;
			
			if(window.event) // IE
			{
				keynum = window.event.keyCode;
			}
			else if(e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
			
			if (keynum==13) {
				clickedSubmit();
			}
		}
		pcHolder.appendChild(pcInput);
	}
	
	
	if(sHolder!=null) {
		var submitInput = document.createElement("input")
		submitInput.setAttribute("type","button");
		submitInput.setAttribute("name","pqfsubmit");
		submitInput.setAttribute("id","pqfsubmit");
		submitInput.setAttribute("value","Find Physicians");
		sHolder.appendChild(submitInput);
		submitInput.onclick = clickedSubmit;
	}
	
	//the treatment select is optional
	if(tHolder!=null) {
		var tInput = document.createElement("select");
		var option = document.createElement("option");
		option.appendChild(document.createTextNode("All Treatments"));
		option.setAttribute("value","ALL");
		tInput.appendChild(option);
		
		AjaxRequest.get(
		  {
		    'url':'/apps/data/treatments.xml'
		    ,'onSuccess':function(req){
		    	var root = req.responseXML.documentElement;
		    	var points = root.getElementsByTagName("t");
		    	
		    	for(var i = 0 ; i < points.length ; i++) {
		    		var p = points[i];
					var option = document.createElement("option");
					option.appendChild(document.createTextNode(p.getAttribute("treatmentName")));
					option.setAttribute("value",p.getAttribute("treatmentName"));
					tInput.appendChild(option);
		    	}
		    	
				tHolder.appendChild(tInput);
		    }
		  }
		);
	}
	
	function keyUpEvent() {
		alert(event);
	}
	
	function clickedSubmit() {
		if(pcInput.value.length == 0) {
			pcInput.setAttribute("style","border: red 1px solid");
			return;
		}else{
			pcInput.setAttribute("style","");
		}
		
		var treatmentValue = "ALL";
		if(tInput!=null)
			treatmentValue = tInput.options[tInput.selectedIndex].value;
		
		document.location=String.format(
			redirectTo+"?postalCode={0}&treatment={1}",
			pcInput.value,
			treatmentValue
		);
	}
	
}
//END PhysicianQuickFinder object