function submitForm(fname)
{
	form = document.forms[fname]
	if (validateForm(form))
		form.submit()
}

function validateControl(control)
{
	if (control.type == "select-one")
		return (control.options[control.selectedIndex].value != "")
	else
		return (control.value != "")
}

function validateForm(aform)
{
	
	var message = ""
	var correct = true
	var Checked = false
	for (c in aform.controls)	{
		control = aform.controls[c]
		
		varError = false
		varChecked = false
		
		if (control.extra.required == true){
			
				var i = 0
				
				
				
				if(control.length > 1){
					if(control[0].type == "radio" || control[0].type == "checkbox"){
						while(i < control.length){
							if(control[i].checked==true){
								varChecked = true
							}
						++i
						}
							
						if(varChecked == false){
							correct = false
							varError = true
							message = message + control.extra.msg + "\n"
						}
					}else
					{
						if (control.extra.validateFunction(control) == false){
							correct = false
							varError = true
							message = message + control.extra.msg + "\n"
						}
					}
				}else
				{
					if (control.extra.validateFunction(control) == false){
						correct = false
						varError = true
						message = message + control.extra.msg + "\n"
					}
				}
		}

		if (!varError &&(control.extra.allowdefault == false) && (control.value == control.extra.defaultValue))
			correct = false

		if (!varError && control.extra.format == "number" && control.value != "") {
			if (!isInteger(stripWhitespace(control.value))) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "email" && control.value != "") {
			if (!isEmail(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "strict" && control.value != "") {
			if (!isAlphanumericOrUnderscore(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "alphanumeric" && control.value != "") {
			if (!isAlphanumeric(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "phone" && control.value != "") {
			if (!isPhoneNumber(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "mobile" && control.value != "") {
			if (!isPhoneNumber(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "postcode" && control.value != "") {
			if (!isPostCode(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		
		if (!varError && control.extra.format == "date" && control.value != "") {
			if (!isDate(control.value.slice(6,10),control.value.slice(3,5),control.value.slice(0,2))) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		if (!varError && control.extra.format == "currency" && control.value != "") {
			if (!isFloat(control.value)) {
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
		
		if (!varError && control.extra.format == "Password" && control.value != "") {
			if (document.formName.password.value != document.formName.password2.value){
				correct = false
				varError = true
				message = message + control.extra.format_msg + "\n"
			}
		}
				
		
		
		if (!varError && (control.extra.reglength > 0)&& (control.extra.required || (control.value != "")))
			if (control.extra.reglength != control.value.length) {
				correct = false
				varError = true
				message = message + control.extra.reglength_msg + "\n"
			}
		if (!varError && (control.extra.maxlength > 0))
			if (control.extra.maxlength < control.value.length) {
				correct = false
				varError = true
				message = message + control.extra.maxlength_msg + "\n"
			}
		if (!varError && (control.extra.minlength > 0) && (control.extra.required || (control.value != "")))
			if (control.extra.minlength > control.value.length) {
				correct = false
				varError = true
				message = message + control.extra.minlength_msg + "\n"
			}
	}


		if (!correct) {
			alert(message)
			return false
		}else
		{	return true 
		}
}


function initForm(fname)
{
	f = document.forms[fname]

	f.controls = new Object()

	return f

}

function initControl(fname, cname)
{
	f = document.forms[fname]
	control = f[cname]

	if (!control)
		alert("no object " + cname + " in " + fname)

	if (!f.controls)
		alert("form " + fname + " has not been inited")

	extra = new Object()
	control.extra = extra

	extra.defaultValue = control.value
	extra.required = false
	extra.allowdefault = true
	extra.msg = "You must fill in " + cname
	extra.format_msg = cname + " is not valid"
	extra.validateFunction = validateControl
	extra.focuscontrol = control
	extra.maxlength = 0
	extra.maxlength_msg = cname + " is too long"
	extra.minlength = 0
	extra.minlength_msg = cname + " is too short"


	f.controls[cname] = control

	return control
}

function removeText(control)
{
	if (control.value == control.extra.defaultValue)
		control.value = ""
}

function setText(control)
{
	if (control.value == "")
		control.value = control.extra.defaultValue
}

function isAlphanumericOrUnderscore(s)
{
	var i;

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (!(isLetter(c) || isDigit(c) || c == "_"))
        	return false;
    }

    return true;
}





function deleteSelectedOptions(fname, cname)
{
	control = document[fname][cname]

	i = 0
	while (i < control.options.length)
	{
		if (control.options[i].selected)
			control.options[i] = null
		else
			i++
	}
}


function addOptionToList(control, s)
{
	for (i = 0; i < control.options.length; i++)
		if (control.options[i].value == s)
			return

	control.options[control.options.length] = new Option(s, s)
}

function w_addOption(s)
{
	addOptionToList(addctrl, s)
}

function addOptionTo(fname, cname, destname)
{
	src = document[fname][cname]
	dest = document[fname][destname]


	if (src.extra.format == "email")
		if (!isEmail(src.value)) {
			alert(src.extra.format_msg)
			src.focus()
			return
		}


	if (src.value == '')
		return

	if (src.value.indexOf(' ') != -1) {
		alert ('Spaces are not allowed')
		src.focus()
		return
	}

	addOptionToList(dest, src.value)

	src.value = ""
}

function setCheckboxes(formname, prefix, value)
{
	form = document.forms[formname]


	for (c in form) {
		if (String(c).indexOf(prefix) == 0) {
			form[c].checked = value
		}
	}
}
