
function val(f) {

	var msg = '';
	var r = new Array();
	// Loop through each form element
	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		// Enforce syntax of TEXT elements
		// MANDATORY
		if ((e.type == "text" || e.type == "textarea" || e.type == "password") && (e.mandatory)) {
		    
		    if ((e.value == null) || (e.value == "")) {
                try {    
				e.focus();
				e.select();
				}catch(ex){}
				msg = errMsg(e, f.valmsg_obligatory != null ? f.valmsg_obligatory.value : 'is mandatory');
				break;
			}
		}
		// DATE
		if ((e.type == "text" ) && (e.ddmmyy)) {
			var d = e.value.substr(0,2);
			var m = e.value.substr(2,2);
			var y = e.value.substr(4,2);
			if ((d == null) || (d == "") || (m == null) || (m == "") || (y == null) || (y == "")) {
				msg = errMsg(e, 'is not a valid date');
				e.focus();
				e.select();
				break;
			}
			if (isNaN(parseInt(d, 10)) || isNaN(parseInt(m, 10)) || isNaN(parseInt(y, 10))) {
				msg = errMsg(e, 'is not a valid date');
				e.focus();
				e.select();
				break;
			}
			if ((d < 1) || (((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)) && (d > 31)) || (((m == 4) || (m == 6) || (m == 9) || (m == 11)) && (d > 30)) || ((m == 2) && (d > 29))) {
				msg = errMsg(e, 'is not a valid date');
				e.focus();
				e.select();
				break;
			}
		}
		if ((e.type == "text" ) && (e.today)) {
			var d = e.value.substr(0,2);
			var m = e.value.substr(2,2);
			var y = e.value.substr(4,2);
			if ((d == null) || (d == "") || (m == null) || (m == "") || (y == null) || (y == "")) {
				msg = errMsg(e, 'is not a valid date');
				e.focus();
				e.select();
				break;
			}
			var t = new Date();
			var td = t.getDate();
			var tm = t.getMonth() + 1;
			var ty = t.getYear();
			if (ty < 1000) ty+=1900;
			ty-=2000;
			if ((d != td) || (m != tm) || (y != ty)) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be today');
				break;
			}
		}
		// NUMERIC
		if ((e.type == "text") && (e.numeric)) {
			if (!isNumber(e.value)) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be numeric');
				break;
			}
		}
		// INTEGER
		if ((e.type == "text") && (e.integer)) {
			if (!isInteger(e.value)) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be numeric');
				break;
			}
		}
		// MIN
		if ((e.type == "text") && (e.min)) {
			if (!isNumber(e.value)) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be numeric');
				break;
			}
			if (parseInt(e.value, 10) < e.min) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be at least ' + e.min);
				break;
			}
		}
		// MAX
		if ((e.type == "text") && (e.max)) {
			if (!isNumber(e.value)) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be numeric');
				break;
			}
			if (parseInt(e.value, 10) > e.max) {
				msg = errMsg(e, 'must be no more than ' + e.max);
				break;
			}
		}
		// MINLEN
		if ((e.type == "text") && (e.minlen)) {
			if (e.value.length < e.minlen) {
				e.focus();
				e.select();
				msg = errMsg(e, 'must be at least ' + e.minlen + ' characters long');
				break;
			}
		}
		// email
		if ((e.type == "text") && (e.email)) {
			if (isValidemail(e.value) != '') {
				e.focus();
				e.select();
				msg = errMsg(e, f.valmsg_validemail != null ? f.valmsg_validemail.value : 'is not a valid email address');
				break;
			}
		}
		// repeat control
		if ((e.type == "text" || e.type == "password") && (e.repeat)) {
			var repeat = e.name.replace("_repeat", "");
			if (e.value != f.elements[repeat].value) {
				e.focus();
				e.select();
				msg = errMsg(e, 'is not the same as above');
				break;
			}
		}
		// CHECKBOX elements are grouped together
		// If this is the checkbox master...
		if ((e.type == "hidden") && (e.name.indexOf("Master_") != -1)) {
			var n = e.name.substr(e.name.indexOf('_') + 1, e.name.length - 1);
			// ref to checkbox thas should be focused
			var first;
			if (e.mandatory) {
				var v = '';
				// Then loop through the form collecting all related checkboxes
				for (var j = 0; j < f.length; j++) {
					var z = f.elements[j];
					if ((z.type == "checkbox") && (z.name.indexOf(n) != -1)) {
						if(first == null) // first chb will be focused
							first = z;
						if (z.checked)
							v += z.value;
					}
				}
				if (v == '') {
					if (e.label != '')
						msg = e.label + ' is mandatory';
					else
						msg = 'Field is mandatory';
					e = first; // first chb will be focused
					break;
				}
			}
		}
		// RADIO buttons must have an option selected
		if ((e.type == "radio") && f.elements[e.name] != null && (f.elements[e.name][0].mandatory)) {
			// Search for the name in the radio button array
			var l = false;
			for (var a = 0; a < r.length; a++ ) {
				if (r[a][0] == e.name) {
					l = true;
					if (e.checked) r[a][1] = true;
				}
			}
			if (l == false) r[r.length] = [e.name, e.checked] ;
		}
		// SELECT controls must have an option selected
		if ((e.type == "select-one") && (e.mandatory == true)) {
			var v = e.options[e.selectedIndex].value;
			if ((v == null) || (v == "") || e.selectedIndex < 1) {
				msg = errMsg(e, 'is mandatory');
				break;
			}
		}
	}
	// Check for incomplete radio button groups
	for (a = 0; a < r.length; a++) {
		if (r[a][1] == false) {
			e=f.elements[r[a][0]][0];
				msg = errMsg(e, 'is mandatory');
			break;
		}
	}
	if (msg != '') {
	    try {
	        e.focus();
	    } catch (ex) { }
		alert(msg);
		return false;
	}
	// All checks passed OK
	return true;
}

function checkChars(email) {
	var idx, chr;

	for (idx=0; idx < email.length; idx++) {
		chr = email.substr(idx, 1);
		if ( (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') || (chr >= '0' && chr <= '9') || (chr == '.') || (chr == '@') || (chr == '-') || (chr == '_') )
			;
		else
			return false;
	}
	return true;
}

function ltrim(str) {
    for (var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
    return str.substring(k, str.length);
}
function rtrim(str) {
    for (var j = str.length - 1; j >= 0 && isWhitespace(str.charAt(j)); j--);
    return str.substring(0, j + 1);
}
function trim(str) {
    return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
    var whitespaceChars = " \t\n\r\f";
    return (whitespaceChars.indexOf(charToCheck) != -1);
}


function isValidemail(emailList) {
//* Input: string email
//* Output: string containing error message if email is not a
//*          valid email address, or empty string if it is valid
    var emails = new Array();
    if (emailList.indexOf(';') > 0)
        emails = emailList.split(';');
    else
        emails = emailList.split(',');

    for(var i = 0; i < emails.length; i++) 
    {
        email = emails[i];
        if (email != null) {
            email = trim(email);
            //* Email must be at least seven characters long
            if (email.length < 7)
                return 'email address is not long enough to be valid';

            //* Email cannot contain invalid characters
            if (checkChars(email) == false)
                return 'email address contains invalid character(s)';

            //* Email must contain a '@' symbol
            var atSign = email.indexOf('@');
            if (atSign == -1)
                return ('email address does not contain @ symbol');

            //* Email cannot contain another '@' symbol
            if (email.lastIndexOf('@') != atSign)
                return ('email address contains mulitple @ symbols');

            //* @ symbol cannot be at beginning or end of email
            if (atSign == 0 || atSign == email.length - 1)
                return ('@ symbol in invalid position');

            //* Break email into name and address portions
            var eName = email.substr(0, atSign - 1);
            var eAddr = email.substr(atSign + 1);

            //* Address portion must contain at least one '.' separator
            if (eAddr.indexOf('.') == -1)
                return ('Address portion does contain contain . symbol');
        }
	}
	//* All tests completed - email address must be valid
	return '';
}

function isNumber(x) {
	var r = false;
	x = x.toString();
	if (parseFloat(x) == parseFloat(x)) {
		if (parseFloat(x) == x) r = true;
	}
	return r;
}

function isInteger(x) {
	x = x.toString();
	for (var i = 0; i < x.length; i++) {
		var c = x.substr(i, 1);
		if ((c < "0") || (c > "9")) return false;
	}
	return true;
}

function errMsg(ctrl, msg) {
    if (ctrl.override_message)
		return ctrl.label;
	if (ctrl.label)
		return ctrl.label + ' ' + msg;
	else
		return 'Field ' + msg;
}

function disable(id) {
	document.getElementById(id).disabled = true;
}

function enable(id) {
	document.getElementById(id).disabled = false;
}

function changeType(typeId) {
	document.getElementById("contTypeId").value = typeId;
}

function changeABCType(typeId) {
	document.getElementById("abcTypeId").value = typeId;
}

function disableOther(ddl,val,id) {
	if(ddl.options[ddl.selectedIndex].value == val)
		document.getElementById(id).disabled = false;
	else
		document.getElementById(id).disabled = true;
}
