// **************************************************************************
// *****
// *****		Validation Scripts
// *****		Version 1.009
// *****		
// *****		Last Modified: 31/10/2002  5:09pm
// *****		by Glendon Manners (glendon@symbiation.com.au)
// *****
// **************************************************************************


function updateDate()
{
	jdate = document.formName.DOBday.value + "/" + document.formName.DOBmonth.value + "/" + document.formName.DOByear.value;
	document.formName.date.value = jdate; 
}

function submitForm(fname)
{
	form = document.forms[fname]
	if (validateForm(form))
		form.submit()
}


function validateForm2(theForm) {
    
    var theForm= formName;   
   var checkboxChecked = false;
       
   for (var i=0, j=theForm.elements.length; i<j; i++) {
			alert(i);
       myType = theForm.elements[i].type;
       
       alert(myType);
       
       if (myType == 'checkbox') {
           if (theForm.elements[i].checked) checkboxChecked = true;
       }
   }

   if (!checkboxChecked)
       alert('Please check a checkbox');

   return valid;
}


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
		varDoCheck = true

		if (control.extra.required == true){
		
		
		// Check to see if input is visible.
		  var dislpaygroup = control.extra.dislpaygroup;
		  
		  if(dislpaygroup > ""){
				if ((eval("document.all."+dislpaygroup+".style.display") == "none")){
				varDoCheck = false;
				}
			}
			
				var i = 0
				
	   if(varDoCheck){	
				
		//	alert(control.name + "=" +control.value);
			if((control.type != "text" && control.type != "select-one" && control.type != "textarea" && control.type != "password")){
		
			if (control.length > 0){
			var doLoopCheck = control.length;
			}else{
			var doLoopCheck = 1;
			}
			
			if(doLoopCheck > 0 ){
						while(i < doLoopCheck){
							if(doLoopCheck>1){
								if(control[i].checked==true){
									varChecked = true
								}
							}else{
								if(control.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 == "alphaandnumeric" && control.value != "") {
			if (!isAlphaAndNumeric(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)) {
				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.equalinput > "" && control.value != "") {		
			if (control.value != eval("document.all."+control.extra.equalinput+".value")) {
				correct = false
				varError = true
				message = message + control.extra.equalinput_msg + "\n"
			}
		}	

		
		
		if (!varError && control.extra.setvalue > 0 && control.value != "") {
			if (control.value != control.extra.setvalue) {
				correct = false
				varError = true
				message = message + control.extra.setvalue_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
		}
	}
}


