var jsidcollection = new Array();

function autosave(){

	var email = prompt("Please enter your email address, you will be sent an email with a link to your partially completed form.", "");

	if(!email)
		return;

	var atpos = email.indexOf("@");
	if(atpos <= 0){
		alert("Please enter a valid email address");
		return;
	}
	var dotpos = email.indexOf(".", atpos);
	if(dotpos <= 0){
		alert("Please enter a valid email address");
		return;
	}

	if(email == "")
		return;
	
	var xml = "<root><email>" + email + "</email><url>" + window.location.href + "</url><controls>";

	for(var i = 0; i < jsidcollection.length; i++){
		var el = document.getElementById(jsidcollection[i]);
		var val = null;
		if(!el){
			//check to see if its a radiogroup
			var j = 0;
			var tmp = document.getElementById(jsidcollection[i] + "_" + j);
			while(tmp){
				//alert("looking...");
				j++;
				if(tmp.checked){
					//alert("found");
					val = tmp.value;
				}
				tmp = document.getElementById(jsidcollection[i] + "_" + j);
			}
			
			if(val){
				xml += "<control name=\"" + jsidcollection[i] + "\"><![CDATA[" + val + "]]></control>";
				continue;
			}
			else
				continue;
		}
		if(el.type == "checkbox") el = document.getElementById(jsidcollection[i] + "_check");
		val = el.value;
		if(el.tagName == "select") val = el.options[el.selectedIndex].value;
		xml += "<control name=\"" + jsidcollection[i] + "\"><![CDATA[" + val + "]]></control>";
	}
	
	xml += "</controls></root>";
	
	//alert(xml);
	sendToServer(xml);

	alert("Thank you, your form has been saved");
}

addLoadEvent("autoloadFromQS");
function autoloadFromQS(){
	//alert(document.getElementsByTagName("form")[0].innerHTML.substring(2000));
	var url = window.location.href;
	var qs = url.indexOf("?") > 0 ? url.substring(url.indexOf("?")+1) : "";
	
	var currentname = "";
	var currentvalue = "";
	var inname = true;
		
	for(var i = 0; i < qs.length; i++){
		var chr = qs.substring(i,i+1);
		switch(chr){
			case "=":
				inname = false;
				break;
			case "&":
				inname = true;
				populateControlFromName(currentname, currentvalue);
				currentname = "";
				currentvalue = "";
				break;
			default:
				if(inname) currentname += chr; else currentvalue += chr;
				break;
		}
	}

	populateControlFromName(currentname, currentvalue);
	
}

function populateControlFromName(name, value){
	//alert(name);
	var id = getControlIDFromName(name);
	
	if(id == "") return;
	
	populateControl(id, value);
}

function getControlIDFromName(name){
	if (name == "")
		return "";

	for(var i = 0; i < 50; i++){
		var el = document.getElementById("fd_control_" + i);
		//alert(el.name);
		if (el == null) {
			//alert("undefined");
			return "";
		}
		if(el && el.name.toLowerCase() == name.toLowerCase())
			return "fd_control_" + i;
	}
	
	return "";
}

addLoadEvent("autoload");
function autoload(){
	//alert(document.getElementsByTagName("form")[0].innerHTML.substring(2000));
	var url = window.location.href;
	var qs = url.indexOf("?") > 0 ? url.substring(url.indexOf("?")+1) : "";
	
	var currentname = "";
	var currentvalue = "";
	var inname = true;
	
	var ticks = "";
	var email = "";
		
	for(var i = 0; i < qs.length; i++){
		var chr = qs.substring(i,i+1);
		switch(chr){
			case "=":
				inname = false;
				break;
			case "&":
				inname = true;
				if(currentname == "_fbe") email = currentvalue;
				if(currentname == "_fbt") ticks = currentvalue;
				currentname = "";
				currentvalue = "";
				break;
			default:
				if(inname) currentname += chr; else currentvalue += chr;
				break;
		}
	}
	
	
	if(currentname == "_fbe") email = currentvalue;
	if(currentname == "_fbt") ticks = currentvalue;
	
	if(ticks == "" || email == "") return;
	
	var xml = "<root><ticks>" + ticks + "</ticks><email>" + email + "</email></root>";
	
	var response = sendToServer(xml);
	//alert(response);
	currentname = "";
	currentvalue = "";
	inname = true;
	for(var i = 0; i < response.length; i++){
		var chr = response.substring(i,i+1);
		switch(chr){
			case "=":
				inname = false;
				break;
			case "&":
				inname = true;
				populateControl(currentname, currentvalue);
				currentname = "";
				currentvalue = "";
				break;
			default:
				if(inname) currentname += chr; else currentvalue += chr;
				break;
		}
	}
	
	populateControl(currentname, currentvalue);
	
}

function populateControl(name, value){
	var el = document.getElementById(name);
	if(el){
		switch(el.tagName.toLowerCase()){
			case "input":
				switch(el.type){
					case "text":
						//alert("textbox");
						el.value = value;
						return;
					case "checkbox":
						//alert("checkbox");
						el.checked = value.toLowerCase() == "true";
						return;
				}
				break;
			case "textarea":
				//alert("textarea");
				el.value = value;
				return;
			case "select":
				//alert("dropdown");
				for(var i = 0; i < el.options.length; i++)
					if(el.options[i].value == value){
						el.selectedIndex = i;
						return;
					}
				return;
		}
	}

	//Custom controls
	var eltmp;
	
	//Check for radio group
	var i = 0;
	eltmp = document.getElementById(name + "_" + i);
	//alert("looking for custom controls named " + name);
	//if(eltmp) alert("radiogroup");
	while(eltmp){
		i++;
		if(eltmp.value == value){
			eltmp.checked = true;
			return;
		}
		eltmp = document.getElementById(name + "_" + i);
	}
	
	//check for date picker
	eltmp = document.getElementById(name + "_day");
	if(eltmp){
		//alert("datedropdown");
		
		var dateparts = value.split("/");
		//alert(dateparts[0]);
		for(var j = 0; j < eltmp.options.length; j++)
			if(eltmp.options[j].value == dateparts[0])
				eltmp.selectedIndex = j;
		//alert(dateparts[1]);	
		eltmp = document.getElementById(name + "_month");
		for(var j = 0; j < eltmp.options.length; j++)
			if(eltmp.options[j].value == dateparts[1])
				eltmp.selectedIndex = j;
		//alert(dateparts[2]);	
		eltmp = document.getElementById(name + "_year");
		for(var j = 0; j < eltmp.options.length; j++)
			if(eltmp.options[j].value == dateparts[2])
				eltmp.selectedIndex = j;
		
		return;
	}
}

function sendToServer(xml){
	var xmlhttp = createHttpObject();
	//xmlhttp.open("POST", "http://localhost/autosave.aspx", false);
	xmlhttp.open("POST", "http://www.form-builder.co.uk/autosave.aspx", false);
	xmlhttp.send(xml);
	return xmlhttp.responseText;
}

function createHttpObject(){
	var xmlhttp = false;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(ex1){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(ex2){
			xmlhttp = false;
		}
	}
	if(!xmlhttp && typeof XMLHttpRequest!='undefined')
		xmlhttp = new XMLHttpRequest();
	return xmlhttp;
}
