var http_request = false;
function makePOSTRequest(url, parameters) {
	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) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = alertContents;
	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);
}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById('contentarea').innerHTML = result;			
		} else {
			alert('There was a problem with the request: debug:' + http_request.responseText);
		}
	}
}

function getForm() {
	/*generic counter*/
	var $counter = 0;
	/*object container*/
	var $post_str ="";

	/*build lists of potential input fields*/
	$input_list = document.getElementById("mail_form").getElementsByTagName("input");
	$select_list = document.getElementById("mail_form").getElementsByTagName("select");
	$text_list = document.getElementById("mail_form").getElementsByTagName("textarea");

	/*check actual fields, get values, prep post_str */
	if($input_list.length >= 1) {
	/*we have input fields, capture ID & value, stash in post_str*/

	while($counter <= $input_list.length -1) {
		/*get radio buttons*/
		if($input_list[$counter].type=="radio") {
			if($input_list[$counter].checked == true) {
				if($counter > 0) {
					$post_str += '&';
				}
				$post_str += $input_list[$counter].name + '=' + $input_list[$counter].value;
			}
		}

		/*Get values for text fields*/
		if($input_list[$counter].type=="text"){
		/*DHTML Error routine*/
			if(!$input_list[$counter].value) { 
				if($input_list[$counter].getAttribute("required") == "yes") {
					/* do error */
					if(document.getElementById($input_list[$counter].id + "space").lastChild.innerHTML != $input_list[$counter].getAttribute("error")) {
						document.getElementById($input_list[$counter].id + "space").innerHTML +="<div class\"errorspace\">" + $input_list[$counter].getAttribute("error") + "<"+"/div>";
						document.getElementById($input_list[$counter].id + "space").style.border="2px solid #800000";
						return false;
					}
				}
			} else {
				/*if required, check for error, clear as necessary */
				if($input_list[$counter].getAttribute("required") == "yes") {
					if(document.getElementById($input_list[$counter].id + "space").lastChild.innerHTML == $input_list[$counter].getAttribute("error")) {
						document.getElementById($input_list[$counter].id + "space").lastChild.innerHTML = "";
						document.getElementById($input_list[$counter].id + "space").style.border = "0px solid #fff";
					}
				}
			}

			if($counter > 0) {
				$post_str += '&';
			}
			$post_str += $input_list[$counter].id + '=' + $input_list[$counter].value;
		}	
		$counter += 1;	 
		}
	}

	if($select_list.length >= 1) {
		/* we have select fields, capture ID & option selected, stash in post_str*/
		$counter = 0;
		while($counter <= $select_list.length -1) {
			$select_options_list = $select_list[$counter].getElementsByTagName("option");
			for(i=0;i<$select_options_list.length;i++) {
				if($select_options_list[i].selected == true){
					$post_str += '&' + $select_list[$counter].id + "=" + $select_options_list[i].value;
				}
			}
			$counter += 1;
		}
	}

	if($text_list.length >= 1){
		$counter = 0;

		while($counter <= $text_list.length -1) {
			if(!$text_list[$counter].value && $text_list[$counter].getAttribute("required") == "yes") {
				alert("Please enter a message.");
				return false;
			}
			if($text_list[$counter].value) {
				$post_str += '&' + $text_list[$counter].id + '=' + $text_list[$counter].value;
			}	
			$counter += 1;
		}
	}

	$post_str += '&form_id=' + document.getElementById("form_id").value;
	submitForm($post_str);
}

function radios(container,id) {
	$radio_list = document.getElementById(container).getElementsByTagName("input");
	$counter = 0;
	while($counter <= $radio_list.length -1) {
		if($radio_list[$counter].type == "radio" && $radio_list[$counter].id != id && $radio_list[$counter].checked == true) {
			$radio_list[$counter].checked = false;
		} 
		$counter += 1;
	}
}

function submitForm(poststr) {
	makePOSTRequest('/cgi-bin/mi/mailer2.cgi', poststr);
}
