window.onload = InitializeAllForms;

function InitializeAllForms() {
	var arrForm = document.getElementsByTagName("FORM");

	for(i=0; i<arrForm.length; i++) {
		arrForm[i].onreset = ValidationReset;
		InitializeForm(arrForm[i]);
	}
}

function InitializeForm(objForm) {
	for (j=0; j<objForm.elements.length; j++) {
		if (objForm.elements[j].getAttribute("miaErrorIndicatorID") != null && objForm.elements[j].getAttribute("miaErrorIndicatorID") != "") {
			objForm.elements[j].setAttribute("miaCSS", document.getElementById(objForm.elements[j].getAttribute("miaErrorIndicatorID")).className);
		}
		else {
			objForm.elements[j].setAttribute("miaCSS", objForm.elements[j].className);
		}
		switch (objForm.elements[j].getAttribute("miaType")) {
			case "text":
			case "integer":			
			case "email":
			case "date":
			case "time":
			case "datetime":
			case "confirm":
				objForm.elements[j].onblur = ValidateOnBlur;
				objForm.elements[j].onkeydown = ValidateOnKeydown;
				break;
			case "radio":
				objForm.elements[j].onclick = ValidateRadio;
				break;
			case "submit":
				objForm.elements[j].onclick = SubmitForm;
				break;
		}
	}
	return false;
}

function ValidationReset(objEvent) {
	var objForm = GetTarget(objEvent);

	for (i=0; i<objForm.elements.length; i++) {
		if (objForm.elements[i].type != "button" && objForm.elements[i].type != "reset" && objForm.elements[i].type != "submit") {
			if (objForm.elements[i].getAttribute("miaErrorIndicatorID") != null && objForm.elements[i].getAttribute("miaErrorIndicatorID") != "") {
				document.getElementById(objForm.elements[i].getAttribute("miaErrorIndicatorID")).className = objForm.elements[i].getAttribute("miaCSS");
			}
			else {
				objForm.elements[i].className = objForm.elements[i].getAttribute("miaCSS");
			}
		}
	}
}

function SubmitForm(objEvent) {
	var objForm = GetTarget(objEvent).form;
	ValidateForm(objForm);
}

function ValidateForm(objForm) {
	var rtnValue;
	var isSuccess = true;

	for (i=0; i<objForm.elements.length; i++) {
		if (objForm.elements[i].type != "button" && objForm.elements[i].type != "reset" && objForm.elements[i].type != "submit") {
			rtnValue = ValidateElement(objForm.elements[i]);

			if (rtnValue == false) {
				isSuccess = false;

				objForm.elements[i].className = "fieldError";
			}
		}
	}

	if (!ValidateRadios(objForm)) {
		isSuccess = false;
	}

	if (isSuccess) {
		objForm.submit();
	}
}

function ValidateOnBlur(objEvent) {
	var objElement = GetTarget(objEvent);

	ValidateRealtime(objElement, true);
}

function ValidateOnKeydown(objEvent) {
	var objElement = GetTarget(objEvent);

	ValidateRealtime(objElement, false);
}


function ValidateRealtime(objElement, showError) {
	var objForm = objElement.form;

	if (ValidateElement(objElement)) {
		objElement.className = objElement.getAttribute("miaCSS");
	}
	else {
		if (showError == true) {
			objElement.className = "fieldError";
		}
	}
}


function ValidateElement(objElement) {
	var objForm = objElement.form;

	switch (objElement.getAttribute("miaType")) {
		case "text":
			return ValidateText(objElement);
		case "integer":
			return ValidateInteger(objElement);
		case "email":
			return ValidateEmail(objElement);
		case "date":
			return ValidateDate(objElement);
		case "time":
			return ValidateTime(objElement);
		case "datetime":
			return ValidateDateTime(objElement);
		case "confirm":
			return (objElement.value == objForm.elements[objElement.getAttribute("miaCompareTo")].value);
		default:
			return null;
	}
}

function ValidateText(objElement) {
	if (objElement.value == "") {
		if (objElement.getAttribute("miaRequired") == "Y") {
			return false;
		}
	}
	return true;
}

function ValidateInteger(objElement) {
	if (objElement.value == "") {
		if (objElement.getAttribute("miaRequired") == "Y") {
			return false;
		}
	}
	else 
	{
		var txtNumber = objElement.value;		
		if (isInteger(txtNumber))
			return true;
		else
			return false;
	}
}

function ValidateEmail(objElement) {
	if (objElement.value == "") {
		if (objElement.getAttribute("miaRequired") == "Y") { return false; }
	}
	else {
		var txtEmail = objElement.value;

		if(txtEmail.length > 0 && txtEmail.indexOf("@") > 0 && txtEmail.indexOf(".") > 0 && txtEmail.indexOf(" ") == -1) {
			// Check for one @
			if(txtEmail.indexOf("@") != txtEmail.lastIndexOf("@")) { return false; }

			for(j=0; j<txtEmail.length; j++) {
				if(txtEmail.charCodeAt(j) < 45 || txtEmail.charCodeAt(j) == 47) { return false; }
				if(txtEmail.charCodeAt(j) >= 58 && txtEmail.charCodeAt(j) < 64) { return false; }
				if(txtEmail.charCodeAt(j) >= 91 && txtEmail.charCodeAt(j) < 95 || txtEmail.charCodeAt(j) == 96) { return false; }
				if(txtEmail.charCodeAt(j) >= 123) { return false; }
			}

			emailArray = txtEmail.split("@");
			userName = emailArray[0]
			domain = emailArray[1]

			if(userName.length == 0 || domain.length == 0) { return false; }
			if(domain.indexOf(".") <= 0) { return false; }
			if(domain.lastIndexOf(".") == (domain.length - 1)) { return false; }

			return true;
		}
		else {
			return false;
		}
	}
}


function ValidateDate(objElement) {
	if (objElement.value == "") {
		if (objElement.getAttribute("miaRequired") == "Y") {
			return false;
		}
	}
	else {
		return ValidateDateString(objElement.value);
	}
}


function ValidateDateString(txtDate) {
	var numDashes = 0;

	for (j=0; j<txtDate.length; j++) {
		if(txtDate.charAt(j) == "-") {
			numDashes ++;
		}
		else if(txtDate.charCodeAt(j) < 48 || txtDate.charCodeAt(j) > 57) {
			return false;
		}
	}

	if (numDashes != 2) {
		return false;
	}

	aryDate = txtDate.split("-");

	var objDate = new Date(aryDate[0], aryDate[1] - 1, aryDate[2]);
	if (objDate.getFullYear() != aryDate[0] || objDate.getMonth() != aryDate[1] - 1 || objDate.getDate() != aryDate[2]) {
		return false;
	}
	return true;
}

function ValidateTime(objElement) {
	if (objElement.value == "") {
		if (objElement.getAttribute("miaRequired") == "Y") {
			return false;
		}
	}
	else {
		return ValidateTimeString(objElement.value);
	}
}

function ValidateTimeString(txtTime) {
	var numColons = 0;

	for (j=0; j<txtTime.length; j++) {
		if(txtTime.charAt(j) == ":") {
			numColons ++;
		}
		else if(txtTime.charCodeAt(j) < 48 || txtTime.charCodeAt(j) > 57) {
			return false;
		}
	}

	if (numColons != 2) {
		return false;
	}

	aryTime = txtTime.split(":");

	var objTime = new Date(0, 0, 0, aryTime[0], aryTime[1], aryTime[2]);
	if (objTime.getHours() != aryTime[0] || objTime.getMinutes() != aryTime[1] || objTime.getSeconds() != aryTime[2]) {
		return false;
	}
	return true;
}


function ValidateDateTime(objElement) {
	if (objElement.value == "") {
		if (objElement.getAttribute("miaRequired") == "Y") {
			return false;
		}
	}
	else {
		var txtDateTime = objElement.value;

		if (txtDateTime.indexOf(" ") > -1) {
			aryDateTime = txtDateTime.split(" ");

			return ValidateDateString(aryDateTime[0]) && ValidateTimeString(aryDateTime[1]);
		}
		else {
			return false;
		}
	}
}


function ValidateRadios(objForm) {
	var aryRadioGroups = new Array();
	var nameExists;
	var i, j;
	var currIndex;
	var isSuccess = true;

	for (i=0; i<objForm.elements.length; i++) {
		if (objForm.elements[i].type == "radio") {
			nameExists = false;
			for (j=0; j<aryRadioGroups.length; j++) {
				if (aryRadioGroups[j][0] == objForm.elements[i].name) {
					currIndex = j;
					nameExists = true;
					break;
				}
			}
			if (!nameExists) {
				if (objForm.elements[i].getAttribute("miaRequired") == "Y") {
					currIndex = aryRadioGroups.length;
					aryRadioGroups[aryRadioGroups.length] = new Array(objForm.elements[i].name, false, objForm.elements[i].getAttribute("miaErrorIndicatorID"), objForm.elements[i].getAttribute("miaCSS"));
				}
			}

			if (objForm.elements[i].checked == true) {
				if (objForm.elements[i].getAttribute("miaRequired") == "Y") {
					aryRadioGroups[currIndex][1] = true;
				}
			}
		}
	}

	for (i=0; i<aryRadioGroups.length; i++) {
		if (aryRadioGroups[i][1] == true) {
			document.getElementById(aryRadioGroups[i][2]).className = aryRadioGroups[i][3];
		}
		else {
			isSuccess = false;
			document.getElementById(aryRadioGroups[i][2]).className = "fieldError";
		}
	}

	if (!isSuccess) {
		return false;
	}
	else {

		return true;
	}
}

function ValidateRadio(objEvent) {
	var objElement = GetTarget(objEvent);

	if (objElement.getAttribute("miaRequired") == "Y") {
		if (objElement.checked) {
			document.getElementById(objElement.getAttribute("miaErrorIndicatorID")).className = objElement.getAttribute("miaCSS");
		}
	}
}



function GetTarget(objEvent) {
	var objTarget;
	if (!objEvent) {
		var objEvent = window.event;
	}
	if (objEvent.target) {
		objTarget = objEvent.target;
	}
	else if (objEvent.srcElement) {
		objTarget = objEvent.srcElement;
	}
	if (objTarget.nodeType == 3) {
		objTarget = objTarget.parentNode; // defeat Safari bug
	}
	return objTarget;
}

function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
	 var c = s.charAt(i);

	 if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}
