function submit_form(frm)
{
	/* code van wouter, maar getXMLHTTPRequest_Captcha not found; maar wordt http variable gebruikt ?
	if(!http) {
		var http 
		//http = getXMLHTTPRequest_Captcha();
	}	
	*/
	var info = new Array();
	
	var elements = frm.elements;
		
	for(var i=0; i < elements.length; i++)
	{
		var required = false;
		var id = "";
		var name = "";
		var alias = "";
		var var_type = "text";
		var found = false;
		var info_index = info.length;
		
		//Required
		if(elements[i].getAttribute("required") == "true") required = true;
		
		//Id
		if(elements[i].id != null && elements[i].id != "") id = elements[i].id;
		else if(elements[i].name != null && elements[i].name != "") id = elements[i].name;
		
		//Name
		if(elements[i].name != null && elements[i].name != "") name = elements[i].name;
		else name = id;
		
		//Alias
		if(elements[i].getAttribute("alias") != null && elements[i].getAttribute("alias") != "") alias = elements[i].getAttribute("alias");
		
		//Var Type
		if(elements[i].getAttribute("var_type") == "int") var_type = "int";
		else if(elements[i].getAttribute("var_type") == "float") var_type = "float";
		
		//Find duplicate fields
		for(j=0; j < info.length; j++)
		{
			if(info[j]['type'] == elements[i].type && (info[j]['id'] == id || info[j]['name'] == name))
			{
				found = true;
			}
		}
		
		//If no duplicate, then add the element to the info array
		if(!found && name != "")
		{
			switch(elements[i].type)
			{
				case "text":
					info[info_index] = new Array();
					info[info_index]['id'] = trim(id);
					info[info_index]['name'] = trim(name);
					info[info_index]['alias'] = trim(alias);
					info[info_index]['type'] = elements[i].type;
					info[info_index]['value'] = getValue(elements[i]);
					info[info_index]['required'] = required;
					info[info_index]['var_type'] = var_type;
					
					if(elements[i].getAttribute("maxlength") < 2147483647) info[info_index]['maxlength'] = elements[i].getAttribute("maxlength");
					else info[info_index]['maxlength'] = "";
					break;
				case "textarea":
					info[info_index] = new Array();
					info[info_index]['id'] = trim(id);
					info[info_index]['name'] = trim(name);
					info[info_index]['alias'] = trim(alias);
					info[info_index]['type'] = elements[i].type;
					info[info_index]['value'] = getValue(elements[i]);
					info[info_index]['required'] = required;
					info[info_index]['var_type'] = var_type;
					break;
				case "hidden":
					break;
				case "select-one":
				case "select-multiple":
				case "radio":
				case "checkbox":
					info[info_index] = new Array();
					info[info_index]['id'] = trim(id);
					info[info_index]['name'] = trim(name);
					info[info_index]['alias'] = trim(alias);
					info[info_index]['type'] = elements[i].type;
					info[info_index]['value'] = getValue(elements[i]);
					info[info_index]['required'] = required;
					info[info_index]['var_type'] = var_type;
					break;
				case "file":
					info[info_index] = new Array();
					info[info_index]['id'] = trim(id);
					info[info_index]['name'] = trim(name);
					info[info_index]['alias'] = trim(alias);
					info[info_index]['type'] = elements[i].type;
					
					if(getValue(elements[i]).indexOf('\\') != -1)
						info[info_index]['value'] = getValue(elements[i]).substring(getValue(elements[i]).lastIndexOf('\\') + 1, getValue(elements[i]).length);
					else
						info[info_index]['value'] = getValue(elements[i]).substring(getValue(elements[i]).lastIndexOf('\/') + 1, getValue(elements[i]).length);
						
					info[info_index]['required'] = required;
					break;
				default:
					break;
			}
		}
	}
	
	var info_text = "";
	
	for(var i=0; i < info.length; i++)
	{
		if(info[i])
		{
			for(var key in info[i])
			{
				info_text += key+'='+info[i][key]+"%%";
			}
			info_text += "**";
		}
	}
	
	//Generate hidden form_info field
	var input_form_info = document.createElement("input");
	input_form_info.setAttribute("type", "hidden");
	input_form_info.setAttribute("id", "form_info");
	input_form_info.setAttribute("name", "form_info");
	input_form_info.setAttribute("value", info_text);
	frm.appendChild(input_form_info);
	
	//Generate hidden browser_info field
	var browser_info = document.createElement("input");
	browser_info.setAttribute("type", "hidden");
	browser_info.setAttribute("id", "browser_info");
	browser_info.setAttribute("name", "browser_info");
	browser_info.setAttribute("value", BrowserDetect.browser + " " + BrowserDetect.version + " on " + BrowserDetect.OS);
	frm.appendChild(browser_info);
	
	//Validate and submit the form
	return validate(frm);
}

function check_conditions(frm)
{
	var elements = frm.elements;
	
	for(var i=0; i < elements.length; i++)
	{
		pre_cond = elements[i].getAttribute("pre_condition");
		question_row = elements[i].getAttribute("question");
		
		if(pre_cond != null && pre_cond != "")
		{
			if(eval(pre_cond))
			{
				//alert("Pre Condition for "+question_row+" : "+elements[i].name+" => "+pre_cond+" => OK");
				document.getElementById("row_"+question_row).style.display = "block";
				document.getElementById("row_"+question_row).disabled = false;
				elements[i].disabled = false;
			}
			else
			{
				document.getElementById("row_"+question_row).style.display = "none";
				document.getElementById("row_"+question_row).disabled = true;
				elements[i].disabled = true;
			}
		}
	}
}

function submit_on_enter(e, frm)
{
	var keycode;
	if(window.event) keycode = window.event.keyCode;
	else if(e) keycode = e.which;
	else return true;
	
	if(keycode == 13)
	{
	   go_next();
	   return false;
	}
	else return true;

}

function getValue(field)
{
	var retval = null
	
	if(field)
	{
		switch(field.type)
		{
			case "text":
			case "textarea":
			case "file":
			case "hidden":
				retval = field.value;
				break;
			case "select-one":
			case "select-multiple":
				var value = new Array();
				for(var i=0; i < field.options.length; i++)
				{
					if(field.options[i].selected) value[value.length] = field.options[i].value;
				}
				retval = value.join(", ");
				break;
			case "submit" :
				break;
			default: //radio, checkbox
				if (field.name) {
					cfield = field.form[field.name];
					var value = new Array();
					if (cfield) {
						if(cfield.length > 0)
						{
							for(var i=0; i < cfield.length; i++)
							{
								if(cfield[i].checked) value[value.length] = cfield[i].value;
							}
							retval = value.join(", ");
						}
						else
						{
							if(cfield.checked) retval = cfield.value;
							else retval = "";
						}
					}	
				}
				/* else alert("missing info : field.type=["+field.type+"],field.name=["+field.name+"]"); */
				break;
		}
		
		if(field.getAttribute("var_type") != null && field.getAttribute("var_type") == "int")
		{
			if(retval == "") retval = "#null#";
		}
		else if(field.getAttribute("var_type") != null && field.getAttribute("var_type") == "float")
		{
			if(retval == "") retval = "#null#";
		}
	}
	
	return retval;
}

function notEmpty(field)
{
	if(field)
	{
		switch(field.type)
		{
			case "text":
			case "textarea":
			case "file":
			case "hidden":
				return !field.value.match("^[ |-]*$");
				break;
			case "select-one":
			case "select-multiple":
				for(var i=0; i < field.options.length; i++)
				{
					if(field.options[i].selected && !field.options[i].value.match("^[ |-]*$")) return true;
				}
				return false;
				break;
			default: //radio, checkbox
				cfield = field.form[field.name];
				if(cfield.length > 0)
				{
					for(var i=0; i < cfield.length; i++)
					{
						if(cfield[i].checked && !cfield[i].value.match("^[ |-]*$")) return true;
					}
					return false;
				}
				else
				{
					if(cfield.checked && !cfield.value.match("^[ |-]*$")) return true;
					return false;
				}
				break;
		}
	}
}

function focus(field)
{
	if(field)
	{
		switch(field.type)
		{
			case "hidden":
				break;
			default:
				field.focus();
		}
	}
}

function validate(frm)
{
	var elements = frm.elements; 
	
	//Check all form elements
	for(var i=0; i < elements.length; i++)
	{
		var required = elements[i].getAttribute("required");
		var pattern = elements[i].getAttribute("pattern");
		var validation = elements[i].getAttribute("validation");
		var message = elements[i].getAttribute("message");
		
		//check required
		if(required != null && required == "true" && !notEmpty(elements[i]) && !elements[i].disabled)
		{
			if(message != null) alert(message);
			else alert(js_translation["Gelieve alle verplichte velden in te vullen"]);
			focus(elements[i]);
			return false;
		}
		
		//check file extensions
		if(elements[i].type == "file" && notEmpty(elements[i]))
		{
			var allowed_ext = elements[i].getAttribute("allowed_extensions");
			
			if(allowed_ext != null)
			{
				var file_ext = elements[i].value.substring(elements[i].value.lastIndexOf('.') + 1, elements[i].value.length);
				var found = false;
				var ext_arr = allowed_ext.split(',');
				
				for(var e=0; e < ext_arr.length; e++)
				{
					if(ext_arr[e].toUpperCase() == file_ext.toUpperCase()) found = true;
				}
				
				if(!found)
				{
					if(message != null) alert(message);
					else alert(js_translation["Foutieve extensie"]);
					focus(elements[i]);
					return false;
				}
			}
		}
		
		//check var_type if no validation or pattern given
		if(getValue(elements[i]) != "#null#" && !elements[i].disabled)
		{
			if(elements[i].getAttribute("var_type") == "int" && validation == null && pattern == null)
			{
				if(!is_int(getValue(elements[i])))
				{
					if(message != null) alert(message);
					else alert(js_translation["Numeriek veld niet correct ingevuld"]);
					focus(elements[i]);
					return false;
				}
			}
			else if(elements[i].getAttribute("var_type") == "float" && validation == null && pattern == null)
			{
				if(!is_float(getValue(elements[i])))
				{
					if(message != null) alert(message);
					else alert(js_translation["Numeriek veld niet correct ingevuld"]);
					focus(elements[i]);
					return false;
				}
			}
		}
		
		//check pattern
		if(pattern != null && message != null && notEmpty(elements[i]) && !String(getValue(elements[i])).match(pattern) && !elements[i].disabled)
		{
			alert(message);
			focus(elements[i]);
			return false;
		}
		
		//check validation
		if(validation != null && message != null && notEmpty(elements[i]) && !elements[i].disabled)
		{
			var valid = true;

			switch(validation)
			{
				case "name":
					if(!is_name_valid(String(getValue(elements[i])))) valid = false;
					break;
				case "email":
					if(!is_email_valid(String(getValue(elements[i])))) valid = false;
					break;
				case "phone":
					if(!is_phone_valid(String(getValue(elements[i])))) valid = false;
					break;
				case "zipcode":
					if(!is_zipcode_valid(String(getValue(elements[i])))) valid = false;
					break;
				case "address":
					if(!is_address_valid(String(getValue(elements[i])))) valid = false;
					break;
				case "city":
					if(!is_city_valid(String(getValue(elements[i])))) valid = false;
					break;
				case "year":
					if(!is_year(String(getValue(elements[i], false)))) valid = false;
					break;
				case "int":
					if(!is_int(String(getValue(elements[i], false)))) valid = false;
					break;
				case "int_positive":
					if(!is_int_positive(String(getValue(elements[i], false)))) valid = false;
					break;
				case "float":
					if(!is_float(String(getValue(elements[i], false)))) valid = false;
					break;
				case "float_positive":
					if(!is_float_positive(String(getValue(elements[i], false)))) valid = false;
					break;
			}
			
			if(!valid)
			{
				alert(message);
				focus(elements[i]);
				return false;
			}
		}
	}
	
	//check captcha
	if(frm.validate_captcha != null && frm.validate_captcha.value == "true")
	{
		captcha_msg = js_translation["Foutieve code, probeer opnieuw aub"];
		validate_captcha(frm);
		return false;
	}
	else return true;
}

