/*


	ajaxSubmit() - see IMPORTANT Notes below.
	
	Allows you to submit forms to an action page and place the returned results in the DOM Id of your choosing.
	
	This kind of replaces the process of filling in a form, pressing submit, and then having the next page load.
	In this case, you fill in a form, and whether you submit it or have it triggered by onchanges, the page does 
	not reload, it only has the DIV you've specified get its content rewritten.
	
	Example: 

	<form  action="ajax-request-handler.asp" id="frm" method="post" onsubmit="return false;">
	<!-- "onsubmit='return false;'" is important in this example because we want to make sure that the 
	user does not go somewhere else when they hit enter (which in most cases, will submit a form) -->
		<input type="hidden" name="operation"  value="update_inventory_qty"/>
		<input type="hidden" name="p_id" value="2"/>
		<input type="hidden" name="c_id" value="3"/>
		<input type="hidden" name="m_id" value="9"/>
		<input type="hidden" name="s_id" value="1"/>
		<input type="text" size="2" name="qty" value=""
			onchange="ajaxSubmit(this.form, 'ajax_feedback'); "
		/>
	</form>
	<div id="ajax_feedback"></div>


	
	IMPORTANT NOTE #1 
	

	'#########################################################################################
	'THE SERVER PROCESS THAT HANDLES THE AJAX REQUESTS MUST RETURN THE RESULTS IN THE FOLLOWING FORMAT: 
	'#########################################################################################
	
	<results>
		<result success="True" FeedbackDivID="SimplyReturningWhatWasSubmitted"/>
		Arbitrary HTML [that is escaped / html-encoded; this is inside XML after all], preformatted and returned by your server program, that you will want to be placed in the div of your choosing on the original page. 
		</result>
	</results>

	IMPORTANT NOTE #2
	
	This script depends on Prototype. 
	


*/


	//---------------------------------------------	
	//---------------------------------------------	
	function ajax_returnXmlObject(sXmlBlob) {
		var xmlDoc;
		try {
			//Internet Explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.loadXML(sXmlBlob);
			xmlDoc.async=false;
		}
		catch(e) {
			try {
				//Firefox, Mozilla, Opera, etc.
		    var parser=new DOMParser();
    		xmlDoc=parser.parseFromString(sXmlBlob,"text/xml");
			}	
			catch(e) {
				alert('Your browser does not support client-side XML processing. ');
			}
		}
		return xmlDoc;
	}

	//---------------------------------------------	
	//---------------------------------------------	
	function ajax_createRequestObject() {
		var ro;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			ro = new XMLHttpRequest();
		}
		return ro;
	}
	
	//---------------------------------------------	
	//---------------------------------------------	
	function ajax_SendRequest(action) {
		var http = ajax_createRequestObject();
		http.open('get', action);
		http.onreadystatechange = function () {
			//alert('http.readyState:' + http.readyState.toString());
			if(http.readyState != 4){
				return;
			}
			var s = http.responseText;
			var xmlDoc = ajax_returnXmlObject(s);
			//alert(xmlDoc.xml);
			var oResults = xmlDoc.getElementsByTagName("result");
			//alert(oResults);
			var FeedbackDivID = oResults[0].getAttribute("FeedbackDivID");
			//alert(FeedbackDivID);
			var success = oResults[0].getAttribute("success");
			//alert(success);
			var color;
			if ("true" == String(success).toLowerCase()) {
				color = "#ccffcc"
			} else {
				color = "#ffcccc"
			}
			
			//alert(FeedbackDivID);
			//alert(oResults[0].firstChild.nodeValue);
			$(FeedbackDivID).innerHTML = s;
			$(FeedbackDivID).innerHTML = oResults[0].firstChild.nodeValue;	
			var bgclOrig = $(FeedbackDivID).style.backgroundColor;
			//alert($(FeedbackDivID));
			//alert($(FeedbackDivID).style);
			//alert(bgclOrig);
			$(FeedbackDivID).style.backgroundColor = color;
			Animation($(FeedbackDivID)).duration(500).checkpoint().to("backgroundColor", bgclOrig).go(); 
			//Animation($(FeedbackDivID)).duration(3000).checkpoint().to("opacity", 0).to("height", "0px").hide().go();
		}
		http.send(null);
		return false;
	}

	
	//---------------------------------------------	
	//---------------------------------------------	
	function AjaxSubmit(form, sFeedbackDivID){
		var req = form.action + "?feedbackDivID=" + escape(sFeedbackDivID);
		var oInput;
		var sType;
		for (iElementsCount=0; iElementsCount < form.elements.length; iElementsCount++) {
			req += "&" + escape(form.elements[iElementsCount].name) + "=";
			oInput = form.elements[iElementsCount];
			sType = oInput.type;
			if (sType == "hidden" || sType == "text" || sType == "textarea" || sType == "file" || sType == "password") {
				req += escape(oInput.value);

			} else if (sType == "button" || sType == "submit" || sType == "reset"){
				//ignore these.

//			} else if (sType == "radio"){

			} else if (sType == "select" || sType == "select-single" || sType == "select-multiple" ||  sType == "select-one" ){
				req +=escape(oInput.options[oInput.selectedIndex].value);
				//alert(req);

			} else if (sType == "checkbox"){
				if (oInput.checked){
					req += escape(oInput.value);
				}else{
					req +="";
				}


			} else {
				alert("Unhandled input type - '" + sType +"'. The site admin will need to fix this. [lib-ajax.js, AjaxSubmit(), line 158] ");
				return false;
			}

					
		
		}
		$(sFeedbackDivID).innerHTML = "Working...";
		//$(sFeedbackDivID).innerHTML = req;
		ajax_SendRequest(req);
		//alert(req);
		return false;
	}








	
