var loopvar = 0;  //used to count errors to prevent "ping pong" between two controls

function compareString (str1, str2) 
{
	if (str1.length == str2.length) 
	{
		if (str1.indexOf(str2) == 0) 
		{
			return true;
		}
	}
	
   return false;
}

function SetValue (Object, str1) 
{
	var nothing = 0;
	if (Object.type == "text" || Object.type == "hidden" ) 
	{
		Object.value = str1;
	}
	else if (Object.type == "select-one") 
	{
		ChangeTo (Object, str1)
	}
	else if (Object.type == "textarea") 
	{
		Object.value = str1;
		Object.text = str1;
	}
	//if there's only one checkbox:
	else if (Object.type == "checkbox") 
	{
		var ObjectValue;
		ObjectValue = Object.value
		Object.checked = (str1.indexOf(ObjectValue) >= 0);
	}
	//if there's a list of checkboxes:
	else if (Object[0].type == "checkbox") 
	{
		setCheckBoxes( Object, str1 );
	}
	//if there is a radio set, at least one MUST be checked:
	else if (Object[0].type == "radio") 
	{
		var index = str1 * 1;	
		Object[index].checked = true;
	}
}

function ChangeTo(theCombo, str1) 
{
	var x;

	for (x=0; x<theCombo.options.length; x++) 
	{
     	if (compareString (theCombo.options[x].text, str1)) 
	 	{
       	theCombo.options[x].selected = true;
     	}
   	}
}

function setCheckBoxes(checkBoxList, answer)
{
	var x = 0;
 	var obj = checkBoxList;
 	var str = "";
	
 	if (answer == "")
	{
		// nothing set:
		str = "off"; 
	}
	//expect answer in form "<text>=<true|false>,<text>=<true|false>...":
 	str = answer.split(","); 
 	for (x=0; x< str.length; x++)
	{
 		obj[x].checked = (str[x] == "on")
 	}
}

function CheckRange(RangeStr, Min, Max)
{
	if ( RangeStr > Max )
	{
   		Err = 3;
		Message = "Value greater than " + Max + "\nSetting value to " + Max;
		alert(Message);
		document.forms[0].tempfield.value = Max;
  	}
	else if (RangeStr < Min)
	{
		Err = 3;
		Message = "Value less than " + Min + "\nSetting value to " + Min;
		alert(Message);
		document.forms[0].tempfield.value = Min;
	}
	
	return Err;
}

function CheckNegative(RangeStr)
{
	if ( RangeStr > 0 )
	{
		Err = 3;
		RangeStr = RangeStr * (-1);
		Message = "Value not negative.\nSetting value to " + RangeStr;
		alert(Message);
		document.forms[0].tempfield.value = RangeStr;
	}
	
	return Err;
}

function CheckPositive(RangeStr)
{
	if (RangeStr < 0)
	{
   		Err = 3;
   		RangeStr = RangeStr * (-1);
		Message = "Value not positive.\nSetting value to " + RangeStr;
		alert(Message);
		document.forms[0].tempfield.value = RangeStr;
	}
	
	return Err;
}

function CheckPercentage(PercentStr)
{
	if ( PercentStr > 100 )
	{
		Err = 3;
		alert("Percentage greater than 100\nSetting percentage to 100");
		document.forms[0].tempfield.value = 100;
	}
	else if (PercentStr < 0)
	{
		Err = 3;
		alert("Percentage less than 0\nSetting percentage to 0");
		document.FORM.tempfield.value = 0;
	}
	
	return Err;
}

function CheckFloat(FloatStr)
{
	Err = 0;
	Message = "";
	floatValue = parseFloat(FloatStr);
	
	if (isNaN(floatValu ))
	{
		Err = 1;
		alert("Non-numeric value entered");
	}
	else
	{
		if (floatValue != FloatStr)
		{
			Err = 2;
			Message = "Invalid numeric value.\nValue will be replaced by " + floatValue;
			alert(Message);
			document.forms[0].tempfield.value = floatValue;
		}
  	}

  	return Err;
}

function CheckInt(Intstr)
{
		Err = 0;
		Message = "";

		intValue = parseInt(Intstr)
		if (isNaN(intValue))
		{
			Err = 1;
			alert("Non-numeric value entered");
		}
		else
			{
   				if (intValue != Intstr)
				{
					Err = 2;
					Message = "Invalid integer value.\nValue will be replaced by " + intValue;
					alert(Message);
					document.forms[0].tempfield.value = intValue;
   				}
  			}

  		return Err
}

function arr() 
{
	this.length=arr.arguments.length;
	for(n=0; n<arr.arguments.length; n++)
	{
   		this[n] = arr.arguments[n];
  	}
}

function CheckDate(Datestr) 
{
	Err = 0;
	Message = "";
	ArrayMonth = new arr("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");

	if (Datestr.length == 11)
	{
		year    = Datestr.substring(7,11);
		spacer1 = Datestr.substring(2,3);
		month   = Datestr.substring(3,6);
		spacer2 = Datestr.substring(6,7);
		day     = Datestr.substring(0,2);
	}
	else if (Datestr.length == 10)
	{
		year    = Datestr.substring(6,10);
		spacer1 = Datestr.substring(1,2);
		month   = Datestr.substring(2,5);
		spacer2 = Datestr.substring(5,6);
		day     = Datestr.substring(0,1);
  	}
	else
	{
		Err = 1;
		Message = "Incorrect input length";
  	}

	if (Err == 0)
	{
		monthchar = month.toUpperCase();
		i = 0;

		while(i < 12)
		{
			if (ArrayMonth[i] == monthchar)
			{
				month = i + 1;
     			i = 12;
    		}
			else
			{
     			i++;
    		}
   		}

		if (isNaN(month))
		{
			Err = 1;
			Message = "Invalid month";
		}
		else if (isNaN(year) || isNaN(day))
		{
			Err = 1;
			Message = "Non-numeric date values";
		}
		else
		{
			if ((spacer1 != " ") || (spacer2 != " ") || (year<0 || year>9999) || (month<1 || month>12) || (day<1 || day>31))
			{
				Err = 1;
				Message = "Date entry out of range";
			}
			else
			{
				if ((month==4 || month==6 || month==9 || month==11) && day==31)
				{
					Err = 1;
					Message = "Day value out of range";
				}

				if (month == 2)
				{
					if ((day > 29) || (day == 29 && ((year/4) != parseInt(year/4))))
					{
						Err = 1;
						Message = "Day value out of range";
	 				}
     			}
			}
		}
	}

	if (Err == 1)
	{
  		alert(Message)
	}

  return Err;
}

function validateAllControls(objForm)
{
	for (var i=0; i<objForm.elements.length; i++)
	{
		if (objForm.elements[i].alt)
		{		
			if (!validateSingleControl(objForm.elements[i]))
			{
				return false;
				break;
			}		
		}
	}
	return true;
}

function validateSingleControl(objControl)
{
	switch (objControl.alt)
	{
		case "alphanumeric" :
			re = /[\038-\177 0-9]/;
			errorMessage = "The field must contain valid characters";
			break;
		case "alphanumeric man" :
			re = /[\038-\177 0-9]/;
			errorMessage = "This is a required field.  Please supply a value.";
			break;
		case "integer" :
			re = /^[-]?\d+$/;
			errorMessage = "The field must only contain numbers";
			break;
		case "real" :
			re = /^[-]?\d+(\.\d+)?$/;
			errorMessage = "The field must only contain whole or floating point numbers";
			break;
		
		case "dd mmm yyyy" :
			re = /^(3[01]|0[1-9]|[12]\d)\ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\ ([1|2]\d{3})/i;
			errorMessage = "The date must be entered in this format: DD MMM YYYY (eg 01 Jan 2000)";
			break;
		
		case "dd mmm yyyy MAN" :
			re = /^(3[01]|0[1-9]|[12]\d)\ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\ ([1|2]\d{3})/i;
			errorMessage = "The date must be entered in this format: DD MMM YYYY (eg 01 Jan 2000)";
			break;
	}	

	if (re.test(objControl.value) == false&&(objControl.value.length<1||objControl.alt=="alphanumeric man"))
	{
	   loopvar++;
		popupMessage(objControl.name + " : \n" +	errorMessage);
//		if (loopvar < 5)
//		{
//			objControl.focus();	
//		} else { loopvar = 0 }
		return false;		
	}
	else
		{
			if (objControl.alt == "dd mmm yyyy" && !checkValidDateRange(objControl.value))
			{
				errorMessage = "The date entered is invalid.  Please enter a valid date in the format DD MMM YYYY.";
				popupMessage(errorMessage);
				objControl.focus();	
				return false;
			}
			return true;
		}
}


function validateSingleControlRange(objControl, minVal, maxVal)
{
	if (validateSingleControl(objControl)) {
	   if(objControl.value.length>0) {
		if (objControl.value < minVal || objControl.value > maxVal) {
			popupMessage("This answer must be between " + minVal.toString() + " and " + maxVal.toString() + "\n\nPlease correct the value.");	
			if (loopvar < 5)
			{
				loopvar++;
				objControl.focus();	
			} else { 
				loopvar = 0 
			}
			//answer is outside the allowable range...
			return false;
		}
	   }
	   //answer is between allowable ranges...
	   return true;
	} else {
		//validateSingleControl failed...
		return false;
	}
}



function popupMessage(error)
{
	alert(error);
}

function checkValidDateRange(theDate)
{
	theDay = theDate.substr(0, 2);
	theMonth = theDate.substr(3, 3);
	theYear = theDate.substr(7);
	
	if (theDay > 31)
	{
		return false;
	}
	
	if (theDay == 31 && (theMonth == "Feb" || theMonth == "Apr" || theMonth == "Jun" || theMonth == "Sep" || theMonth == "Nov"))
	{
		return false;
	}
	
	if (theMonth == "Feb" && theDay > 29)
	{
		return false;
	}
	
	if (theMonth == "Feb" && (theYear%4 !== 0) && theDay > 28)
	{
		return false;		
	}
	
	return true;
}
