/**
* Form validation script included with the JMForm plugin for TinyMCE
* @author Jason McInerney
*/
//does the actual checking
function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function CheckForm(theform) {
	var form_err = '';
	for (var i = 0; i < theform.elements.length; i++) {
		theform.elements[i].disabled = 'true';
		var name = capitaliseFirstLetter(theform.elements[i].name.replace('cmp_', ''));
		if (theform.elements[i].getAttribute('required') != undefined && theform.elements[i].getAttribute('required') == 'true') {
			var type = theform.elements[i].getAttribute('type');
			if (type == 'radio') {
				var list = theform.elements[theform.elements[i].name];
				if (list.length != undefined && list.length > 0) {
					for (j = 0; j < list.length; j++) {
						if (!list[j].checked) {
							form_err = form_err + '\n  -- ' + name;
							break;
						}
					}
				}
				else {
					if (!list.checked) {
						form_err = form_err + '\n  -- ' + name;
					}
				}
			}
			else if (type == 'checkbox') {
				if (!theform.elements[i].checked) {
					form_err = form_err + '\n  -- ' + name;
				}
			}
			else if (theform.elements[i].length != undefined) {//for "select" type elements
					if (theform.elements[i].selectedIndex == undefined || theform.elements[i].options[theform.elements[i].selectedIndex] == undefined || theform.elements[i].options[theform.elements[i].selectedIndex].value.replace(' ', '') == '') {
						form_err = form_err + '\n  -- ' + name;
					}
				}
			else if (theform.elements[i].value == undefined || theform.elements[i].value == ''){
				form_err = form_err + '\n  -- ' + name;
				}
			}
		}
	for (var i = 0; i < theform.elements.length; i++) theform.elements[i].disabled = '';

	if (form_err != '') {
		form_err = "The following fields are required:" + form_err;
		alert(form_err);
		return false;
	} else return true;
}

//for full browser compatibility, instead of javascript: void or similar
function doNothing() { }

//holds the form action attribute
var actTemp = '';

//called onsubmit
function SendRequest(theform, ajax) {
	var the_err = '';
	if (theform.action != 'javascript: doNothing();' && theform.action != '') actTemp = theform.action;
	theform.action = 'javascript: doNothing();';
	var formConf = CheckForm(theform);
	if (formConf) {
		if (ajax) {
			// coming soon...
		}
		else {
			theform.action = actTemp;
			theform.onsubmit = '';
			theform.submit();
		}
	}
}
