// For a more flexible event registration routine, see
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
//window.onload = attachFormHandlers;

/*function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(attachFormHandlers);
addLoadEvent(function() {
  // more code to run on page load
});


function attachFormHandlers()
{

	// Ensure we're working with a 'relatively' standards compliant browser
	if (document.getElementsByTagName) {
		var objForm = document.getElementsByTagName('form');

		for (var iCounter=0; iCounter<objForm.length; iCounter++)
			objForm[iCounter].onsubmit = function(){convertCheckboxs(this); return checkForm(this);}
	}
}
*/

function checkForm(objForm) {

	convertCheckboxs(objForm);

	var arClass, bValid, lblError;
	var objField =  objForm.getElementsByTagName('*');
	var objLabels = objForm.getElementsByTagName('label');
	
	//alert("debugging info - please ignore:\nTag By Name Count " +objField.length)

	// reset label - class
	for (var iLabelIDX=0; iLabelIDX < objLabels.length; iLabelIDX++)
		if (objLabels[iLabelIDX].className = "error") objLabels[iLabelIDX].className = "";

	for (var iFieldCounter=0; iFieldCounter<objField.length; iFieldCounter++) {
		// Allow for multiple values being assigned to the class attribute

		arClass = objField[iFieldCounter].className.split(' ');
		for (var iClassCounter=0; iClassCounter<arClass.length; iClassCounter++) {
			objFrmItemID = objField[iFieldCounter].id
			if (objFrmItemID != "") {
				switch (arClass[iClassCounter]) {
					case 'string':
						bValid = isString(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, ''));
						break;
					case 'number' :
						bValid = isNumber(objField[iFieldCounter].value);
						break;
					case 'email' :
						bValid = isEmail(objField[iFieldCounter].value);
						break;
					case 'password' :
						bValid = isPassword(objField[iFieldCounter].value);
						break;
					case 'username' :
						bValid = isString(objField[iFieldCounter].value);
						break;
					case 'checkboxrequired' :
						bValid = objField[iFieldCounter].checked;
						break;
					case 'dropdownrequired' :
						bValid = isDropDownReq(objField[iFieldCounter][objField[iFieldCounter].selectedIndex].value)
						break;
					default:
						bValid = true;
				}

				if (bValid == false) {
					// get the label for the field that's got a problem
					for (var iLabelIDX=0; iLabelIDX < objLabels.length; iLabelIDX++) {
						if (objLabels[iLabelIDX].htmlFor == objFrmItemID) {
							objLabels[iLabelIDX].className = "error";
							lblError = objLabels[iLabelIDX].innerHTML
							break;
						}
						else
							lblError = ""
					}

					// if the field does not have a label associated, then just grab the field name value
					if (lblError == "")
						lblError = objField[iFieldCounter].name;

					// If this field is invalid, leave the tesing early,
					// and alert the visitor to this error
					alert('Please review the value you provided for "' + lblError + '"');
					if (objField[iFieldCounter].select)
						objField[iFieldCounter].select();
					objField[iFieldCounter].focus();
					return false;
				}
			}
		}
	}
	return true;
}


function isString(strValue) {
	return (typeof strValue == 'string' && strValue != '');
}


function isUsername(strValue) {
	return (typeof strValue == 'string' && strValue != '' && (strValue.length >= 6 && strValue.length <= 18));
}


function isPassword(strValue) {
	return (typeof strValue == 'string' && strValue != '' && (strValue.length >= 6 && strValue.length <= 18));
}


function isNumber(strValue) {
	return (!isNaN(strValue) && strValue != '');
}


function isEmail(strValue) {
	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	return (strValue != '' && objRE.test(strValue));
}

function isDropDownReq(strValue) {
	if (strValue == "")
		return false;
	else
		return true;
}
