// http://www.captain.at/howto-ajax-form-post-request.php
function makebox(box){ // make a dynamic progress box
	var b=document.createElement('div');
	b.id=box;
	b.style.color="white";
	b.style.backgroundImage="url(images/blank.gif)";
	b.style.backgroundColor="Black";
	b.style.position="absolute";
	b.style.zIndex="0";
	//b.style.padding="20px";
	//b.style.padding="20px";
	if(window.innerWidth){
		w=window.innerWidth;
		h=window.innerHeight;
	}else{
		w=document.body.scrollWidth;
		h=document.body.scrollHeight+40;
	}
	b.style.top=0;
	b.style.left=0;
	b.style.width=w+"px";
	b.style.height=h+"px";
	b.style.display="none";
	b.innerHTML="<table height='100%' width='100%'><tr><td align='center'><div style='border:1px solid gray;padding:20px;background-color:#333;color:#FFF;width:80px;font-size:14px'><img src='images/loading.gif'/><p><b>Loading</b></p></div></td></tr></table>";
	document.body.appendChild(b);
	// scriptaculous / lightbox
	new Effect.Appear(b, { duration: 0.2, from: 0.0, to: 0.8 });
}
function killbox(box){ //kill the box
	b=document.getElementById(box);
	document.body.removeChild(b);
}
function getstatus(box,target){ // actions for the ajax request
	if(http_request.readyState==4){
		 if(http_request.status==200){
			 killbox(box); // hide the progress box
			 if(http_request.responseText=="" || !http_request.responseText){ //if there was no error message from the script
					//alert('Error message here'); // display the script error message
					alert('failed');
					return false;
					//clearTimeout(t);
				}else{
					t=document.getElementById(target);
					//alert(http_request.responseText);
					t.innerHTML=http_request.responseText;
					myLightbox.updateImageList();
					//success, clear form info
					return true;
					//clearTimeout(t);				
				}
		 }else{
				alert("There was a problem sending your request \nTry resubmitting the form"); //ajax problem - try again
		 }
	}
}
function sendvars(url,parameters,formname,target){ // start the ajax functions
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
		 if (http_request.overrideMimeType) {
				// set type accordingly to anticipated content type
				//http_request.overrideMimeType('text/xml');
				http_request.overrideMimeType('text/html');
		 }
	}else if(window.ActiveXObject){ // IE
		 try{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
		 } 
		 catch(e){ 
		 		//alert(e.name);
				try{
					 http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch(e){
					//alert(e.name);
				}
		 }
	}
	if(!http_request){
		 formname.submit(); //failed, no ajax - submit the form normally (the page looks out for a postback and execs the script normally)
		 return false;
	}
	//make a dynamic progress box
	box="progbox";
	makebox(box);
	//window['f']=f;
	//var t=setTimeout("f.submit()",5000);
	http_request.onreadystatechange = function(){getstatus(box,target)}
	http_request.open('POST',url,true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}
// process form variables into url and pass to http request function
function doajax(formname){
	var url=formname.url.value;	//the script page
	var target=formname.target.value;	// the html holder for the returned ajax code
	var qstr=''; // query string
	for(i=0;i<formname.length;i++){ // loop through all form elements
		e=formname.elements[i];
		if(e.type && e.type!="submit"){ //eliminate fieldsets and submit button being returned
			if(e.disabled!=true || e.value=="" || e.value==undefined){ // don't add disabled fields or empty fields
				if(e.type=="radio" && e.checked==false || e.type=="checkbox" && e.checked==false){ //only add checked radios, only add checked checkboxes
					continue;
				}
				if(e.multiple){
					for(z=0;z<e.length;z++){
						if(e.options[z].selected){
							qstr+=encodeURI(e.name)+'='+encodeURI(e.options[z].value)+'&';
						}
					}
				}else{
					qstr+=encodeURI(e.name)+'='+encodeURI(e.value)+'&'; // add value to query string
				}
			}
		}
	}
	//alert(qstr);
	qstr+="ajax=true"; //add an ajax identifier for the server side script
	sendvars(url,qstr,formname,target); //all event-fired alerts and objects in the ajax script file
}