// threadsafe asynchronous XMLHTTPRequest code
// We use a javascript feature here called "inner functions".
// Using these means the local variables retain their values
// after the outer function has returned. this is useful for
// thread safety, so reassigning the onreadystatechange
// function doesn't stomp over earlier requests.
function ajaxSend(url, callback) {
	function ajaxBindCallback() {
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if (ajaxCallback) {
					ajaxAct(ajaxRequest.responseXML);
				} else {
					alert('No callback defined.');
				}
			} else {
				alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
			}
		}
	}
	// use a local variable to hold our request and callback
	// until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallback = callback;
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}


function displayManagers(response) {
	Readout = document.getElementById('managerOutput');
	Readout.innerHTML = "";
	Readout.innerHTML = response;
}


function editOrderForm3(response) {
	responseArray = response.split("|")
	for (i=0; i<responseArray.length; i+=3) {
		elementToChange = document.getElementById(responseArray[i]);
		elementType = responseArray[i+1];
		elementValue = responseArray[i+2];
//		alert("Dealing with " + elementToChange +" and "+ elementType + " and " + elementValue);
		if (elementType == "plaintext") {
			elementToChange.innerHTML = "";
			elementToChange.innerHTML = elementValue;
		} else if (elementType == "select") {
			for (j=elementToChange.length-1; j>=0; j--) {
				elementToChange.remove(j);
				}
			if (elementValue == "" || elementValue == "empty") {
				optNew = document.createElement('option');
				optNew.text = "-";
				optNew.value = "";
				optNew.selected = true;
				try {
					elementToChange.add(optNew, null);
				} catch(ex) {
					elementToChange.add(optNew);
				}
				elementToChange.style.display = "none";
			} else {
				optionsArray = elementValue.split("~");
				elementToChange.style.display = '';
				for (j=0; j<optionsArray.length; j+=3) {
					optNew = document.createElement('option');
					optNew.text = optionsArray[j];
					optNew.value = optionsArray[j+1];
					if (optionsArray[j+2] == "y") {
						optNew.selected = true;
					} else {
						optNew.selected = false;
					}
					try {
						elementToChange.add(optNew, null);
						// standards compliant; doesn't work in IE
					} catch(ex) {
						elementToChange.add(optNew); // IE only
					}
				}
			}
		} else if (elementType == "checkbox" || elementType == "radio") {
			if (elementValue == "y") {
				elementToChange.checked = true;
			} else {
				elementToChange.checked = false;
			}
		} else if (elementType == "textbox") {
			elementToChange.value= elementValue;
		}
	}
	disableme(document.goform.lenstype.value,document.goform.thin_and_light.value,document.goform.polycarbonate.value,document.goform.polarised.value,document.goform.transitions.value,document.goform.crystalvu.checked,document.goform.uv400.checked)
	
}

/* was this but changed it - Matt august 07 */
function ajaxAct(theData) {
	response = theData.documentElement;
	method = response.getElementsByTagName('method')[0].firstChild.data;
	result = response.getElementsByTagName('result')[0].firstChild.data;
	eval(method + '(result)');
}


/*function ajaxAct(theData) {
	response = theData.documentElement;
	method = response.getElementsByTagName('method')[0].firstChild.data;
	resultCount = response.getElementsByTagName('resultcount')[0].firstChild.data;
	result = "";
	for (i=0; i<=resultCount; i++) {
		result += response.getElementsByTagName('result')[i].firstChild.data;
	}
	eval(method + '(result)');
}
*/

function disableme(lenstype,thinandlight,polycarb,polarised,transitions,crystal,uv400) {
	document.goform.scratchresistant.disabled=false;
	document.goform.crystalvu.disabled=false;
	document.goform.uv400.disabled=false;		
		
	if (lenstype=="clearplastic") {
		document.goform.crystalvu.disabled=true;
		document.goform.scratchresistant.disabled=true;
		document.goform.uv400.disabled=true;		
	}	
	
	else {
	if (polycarb!="" && (lenstype=="single" || lenstype=="vdu" || lenstype=="distance")) {
		document.goform.crystalvu.disabled=true;
		document.goform.scratchresistant.disabled=true;
		document.goform.uv400.disabled=true;		
	}
	else if (polycarb!="" && lenstype=="bifocal") {
		document.goform.uv400.disabled=true;		
	}	
	else if (lenstype=="executive") {
		document.goform.uv400.disabled=true;
	}		
	else if (lenstype=="bifocal" && thinandlight!="") {
		//alert('dis1:' + dis2);
		document.goform.crystalvu.disabled=false;
		document.goform.scratchresistant.disabled=false;
		document.goform.uv400.disabled=true;
	}	
	else if (thinandlight!="") {
		//alert('dis1:' + dis2);
		document.goform.crystalvu.disabled=true;
		document.goform.scratchresistant.disabled=true;
		document.goform.uv400.disabled=true;
	}
	else if (transitions!="") {
		document.goform.uv400.disabled=true;
	}	
	else if (polycarb=="hardcoat") {
		document.goform.scratchresistant.disabled=true;
		document.goform.crystalvu.disabled=false;
	}
	else if (polarised!="") {
		document.goform.uv400.disabled=true;
	}	
	else {
		document.goform.scratchresistant.disabled=false;
		document.goform.crystalvu.disabled=false;
		document.goform.uv400.disabled=false;		
	}
	//alert('crystal:' + crystal);
	if (crystal==true) {
		document.goform.scratchresistant.disabled=true;
		//document.goform.scratchresistant.value='';
	}
	if (uv400==true) {
		document.goform.crystalvu.disabled=true;
	}	
	
	}
	
}

function modPrice(response) {
	responseArray = response.split("|")
	for (i=0; i<responseArray.length; i+=3) {
		elementToChange = document.getElementById(responseArray[i]);
		elementType = responseArray[i+1];
		elementValue = responseArray[i+2];
		if (elementType == "plaintext") {
			elementToChange.innerHTML = "";
			elementToChange.innerHTML = elementValue;
		}
	}
}

function ShowRecipients(response) {
	responseArray = response.split("|")
	for (i=0; i<responseArray.length; i+=3) {
		elementToChange = document.getElementById(responseArray[i]);
		elementType = responseArray[i+1];
		elementValue = responseArray[i+2];
		if (elementType == "plaintext") {
			elementToChange.innerHTML = "";
			elementToChange.innerHTML = elementValue;
		}
	}
}

