function postAndConfirm(form, destination, messageId) {
/*****************************************************
* This collection of functions accepts a             *
* form & hidden div id, validates form               *
* input, POSTs it and then displays the div          * 
*****************************************************/	

	// If the fields validate...
	if (valid() == true) {
		var post = "";
	
		// We'll verify first that there are children nodes
		if (form.hasChildNodes()) {
			var children = form.childNodes;
			
			// Loop through children
			for (var i = 0; i < children.length; i++) {
				var tempElement = children[i];
				
				// If the child has children, and is a known valid container of form fields, throw it to secondary function
				if (children[i].hasChildNodes()) {
					if (tempElement.nodeName == "P" || tempElement.nodeName == "FIELDSET" || tempElement.nodeName == "DIV") {
						
						var tempPost = postAndConfirm_loop(tempElement);
						
						// If the returned string has length, add it to the post string
						if (tempPost != "") {
							if (post != "") { 
								post += "&" + tempPost;
							} else {
								post = tempPost;
							}
						}	
					}
				}			
			}

			//Now that we have the entire string, we can post
			ajax(post, destination, messageId);
			form.reset();
		}
	} else {
		document.getElementById("errors").innerHTML = valid();
		return false;
	}
}
function postAndConfirm_loop(element) {
	var children = element.childNodes;
	var returnString = ""; 
	
	// Iterate through the children
	for (var i = 0; i < children.length; i++) {
		var tempNode = children[i];
		
		// We'll check to see if the node is a form type
		if (tempNode.nodeName == "INPUT" || tempNode.nodeName == "TEXTAREA" || tempNode == "SELECT") {

			// Now that we've got a data children, we make sure it has a name or id
			// NOTE: The name will be used first, as is customary
			if (tempNode.getAttribute("name") != null) {
				// If the post string has content already, add an ampersand
				if (returnString != "") { returnString += "&"; }
				
				// Add the title to the post string
			 	returnString += tempNode.getAttribute("name") + "=";
				
				// And add the data, encoded of course
				returnString += encodeURI(tempNode.value);
			} else if (tempNode.getAttribute("id") != null) {
				//Do the same thing if it has an ID and no name
				if (returnString != "") { returnString += "&"; }
				returnString += tempNode.getAttribute("id") + "=";
				returnString += encodeURI(tempNode.value);
			} else {
				// If it doesn't have a name or id, let 'em know! And then, keep moving in the for loop.
				alert("One of your form's children (" + tempNode.nodeName + ")\n does not have a name or id \nand will NOT be included in the post!");
				continue;
			}
		}
	}
	return returnString;
}
function ajax(parameters, destination, messageId) {
  	var xmlHttp;
  	try {
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
  	} catch (e) {
    	// Internet Explorer
    	try {
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
      		try {
        	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     		} catch (e) {
        		alert("Your browser does not support AJAX!");
        		return false;
        	}
     	}
   	}

	//Send the Request
	xmlHttp.open("POST", destination, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);

	//Function to check for finished exchange
	xmlHttp.onreadystatechange=function() {
		
		//Display loading message
		document.getElementById("loading").style.display = "inline";
		
		if (xmlHttp.readyState==4) {
		//When its finished...		
			//Make things appear
			Effect.SlideDown(document.getElementById(messageId));
			confirmationPopUp("black");
			
			//Make the loading message disappear
			document.getElementById("loading").style.display = "none";
		}
	}
	
}

function confirmationPopUp(backgroundColor) {
	//create element
	var backgroundDiv = document.createElement("div");
	
	//add css id
	backgroundDiv.setAttribute("id", "backgroundpopup");

	//change background color
	backgroundDiv.style.backgroundColor = backgroundColor;
	
	//append element
	document.body.appendChild(backgroundDiv);
	
	//make visible, resize to window
	backgroundDiv.style.display = "block";
	backgroundDiv.style.height = document.body.clientHeight + "px";
	backgroundDiv.style.width = document.body.clientWidth + "px";
	
	//scroll to top
	window.scrollTo(0,0);	
}


//This function validates the users' input.  Validation is also done server-side.
function valid() {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	var errors = "";	
	if (filter.test(document.getElementById("email").value) == false && document.getElementById("email").value != "") {
		errors += "<ul><li>Please supply a correct e-mail address, or none at all.</li></ul>";
		document.getElementById("email").style.border = "2px solid red";
	}
	if (document.getElementById("thoughts").value == "") {
		errors += "<ul><li>Please add your thoughts.</li></ul>";
		document.getElementById("thoughts").style.border = "2px solid red";
	} 
	if (errors == "") {
		errors += "</ul>"
		document.getElementById("errors").innerHTML = "";
		return true;
	} else {
		return errors;
	}
}
function plainBorders(element) {
	element.style.borderLeft = "2px solid #444";
	element.style.borderTop = "2px solid #444";
	element.style.borderRight = "1px solid #ccc";
	element.style.borderBottom = "1px solid #ccc";
}


