// form_java - used for form manipulation and realtime saving
var xmlhttp; // used for saving

function write_key() {
	var d = new Date();
	var rand_num = d.getMonth()+""+d.getDate()+""+d.getFullYear()+""+d.getTime();
	document.write("<input type='hidden' name='id_key' value='"+rand_num+"'>");
} // end write_key

function save_form(fid) {
	// Precondition: fid is the id of the form
	// Postcondition: All fields have been saved to a text file

	var p_string = build_post_string(fid);	

	// save data
	xmlhttp=null;
	var url = "scripts/save_temp_data.php";
	
	if (window.XMLHttpRequest) {
  		xmlhttp=new XMLHttpRequest();
  	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
	} // end if
	
	if (xmlhttp!=null) {
  		//alert("Request Ready");
		//xmlhttp.onreadystatechange=function() { alert("Changes");}
  		xmlhttp.open('POST',url,true);
  		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send("p_string="+p_string);
  	} else {
  		alert('Your browser does not support XMLHTTP.');
  	} // end if

} // end save_form

function build_post_string(fid) {
	// Precondition: fid is the form id
	// Postcondition: a string of values is returned

	var frm = document.getElementById(fid);
	var max = frm.elements.length;
	var list = "";

	for (i=3;i<max;i++) {
		if (i==3) {
			list += frm.elements[i].value+"::K::";
		} else {
			list += "<br>"+frm.elements[i].name+"::V::"+frm.elements[i].value;
		} // end if
		if (i+1 == max) {
			list += "::-::";
		} // end if
	} // end for
	return list;
} // end buile_post_string
