// Absolute min/max for currency values in SQL server
//var MAX_CURRENCY = 922337203685477.5807;
//var MIN_CURRENCY = -922337203685477.5808;
// Current min/max for currency values in SQL server
var MAX_CURRENCY = 1000000000.00;
var MIN_CURRENCY = -1000000000.00;
var MAX_CURR_STRING = "1000000000.00";
var MIN_CURR_STRING = "0.00";

// Absolute min/max value for ints
//var MIN_INTEGER = Math.pow(2, 32)/2 * -1;
//var MAX_INTEGER = Math.pow(2, 32)/2 - 1;
var MAX_INTEGER = 1000000000.00;
var MIN_INTEGER = -1000000000.00;

function validateForm(form)
{
	if (!form)
	{
		alert("Please pass in the form to validate.");
		return false;
	}
	
	var successful = true;
	var elements = form.elements;
	
	for (var i = 0; successful && i < elements.length; i++)
		if (elements[i].validate)
			successful = elements[i].validate();

	return successful;
}

function validateAlert(message, field)
{
	var fieldTitle = ((field.title) ? "the \"" + field.title + "\"" : "this") + " field";
	message = message.replace(/%f/, fieldTitle);
	alert(message);
}

function checkEmail(fieldName, emailStr, blnAllowBlank)
{
	if ((emailStr == "" || emailStr == null) && blnAllowBlank)
		return true;

	var emailPat = /^(\".*\"|[A-Za-z0-9\_][A-Za-z0-9\.\-\_]*)@(\[\d{1,3}(\.\d{1,3}){3}]|([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,3})$/;
	var matchArray = emailStr.match(emailPat);
	
	if (matchArray == null)
	{
		alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
		fieldName.select();
		fieldName.focus();
		return false;
	}

	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	
	if (IPArray != null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i] > 255)
			{
				alert("The destination IP address is invalid.")
				fieldName.select();
				fieldName.focus();
				return false;
			}
		}
	}

	return true;
}

function checkDecimals(fieldName, fieldValue, decimals)
{
	if (fieldValue == "" || fieldValue == null)
		return true;

	if (isNaN(fieldValue))
	{
		alert("That does not appear to be a valid number.  Please try again.");
		fieldName.select();
		fieldName.focus();
		return false;
	}
	else
	{
		var regExp = '^[0-9]*(\.[0-9]{0,' + decimals + '})?$'
		var matchArray = fieldValue.match(regExp);
		
		if (matchArray == null)
		{
			alert ("Please enter a number with up to " + decimals + " decimal places.  Please try again.");
			fieldName.select();
			fieldName.focus();
			return false;
		}
			
	}

	return true;
}

function checkLength(varName, varMaxLength)
{
	if (varName.value.length > varMaxLength)
	{
		alert("You cannot exceed "+varMaxLength+" characters in this field.");
		varName.select();
		varName.focus();
		return false;
	}

	return true;
}

// Returns true if the
function checkDate(varForm, strName)
{
	var intDay, intMonth, intYear;
	var valid = false;

	if (varForm[strName + "_year"])
	{
		var varYear = varForm[strName + "_year"];
		intYear = varYear.options[varYear.selectedIndex].value;
	}

	if (varForm[strName + "_month"])
	{
		var varMonth = varForm[strName + "_month"];
		intMonth = new Number(varMonth.options[varMonth.selectedIndex].value);
		intMonth = intMonth.toString();
	}

	if (varForm[strName + "_day"])
	{
		var varDay = varForm[strName + "_day"];
		intDay = varDay.options[varDay.selectedIndex].value;
	}

	switch (intMonth)
	{
		case '4':
		case '6':
		case '9':
		case '11':
			valid = intDay > 0 && intDay <= 30;
			break;
		case '2':
			var max;
			
			if (intYear % 100 == 0)
			{
				if (intYear % 400 == 0)
					max = 29;
				else
					max = 28;
			}
			else if (intYear % 4 == 0)
				max = 29;
			else
				max = 28;

			valid = intDay > 0 && intDay <= max;
			break;
		default:
			valid = intDay > 0 && intDay <= 31;
			break;
	}

	if (intDay == "" && intMonth == 0 && intYear == "")
		valid = false;

	if ((intDay == "" || intMonth == 0 || intYear == "") && !(intDay == "" && intMonth == 0 && intYear == ""))  
		valid = false;
	
	if (!valid)
	{
		alert("The date selected does not actually exist.\nPlease specify correct date.");
		varForm[strName + "_day"].focus();
	}

return valid;
}

function afterToday(varForm, strName)
{
	var selected_year = varForm[strName + "_year"].options[varForm[strName + "_year"].selectedIndex].value;
	var selected_month = varForm[strName + "_month"].options[varForm[strName + "_month"].selectedIndex].value - 1;
	var selected_day = varForm[strName + "_day"].options[varForm[strName + "_day"].selectedIndex].value;

	var dteDate = new Date(selected_year, selected_month, selected_day, 0, 0, 0, 0);
	var dteToday = new Date();
	var dteNow = new Date(dteToday.getFullYear(), dteToday.getMonth(), dteToday.getDate(), 0, 0, 0, 0);

	if (dteDate <= dteNow)
	{
		alert ("The date must be after today.\nPlease specify correct date.");
		return false;
	}
	else
		return true;
}
  
function validateCurrency()
{
	if (!this.mandatory && this.value == "")
		return true;

	var pattern = /^(Nil|[+\-\$]?(([0-9]+)|([1-9][0-9]{0,2}(,[0-9]{3})+))([\.-]([0-9]){2}))$/i;
	var found = this.value.search(pattern);

	if (found < 0)
	{
		this.select();
		this.focus();
		validateAlert("Please enter a valid currency value (i.e. in digits, not words, showing dollars and cents separated by a decimal place) into %f.", this);
		return false;
	}

	var minValue = (!isNaN(this.minValue)) ? Math.max(this.minValue, MIN_CURRENCY) : MIN_CURRENCY;
	var maxValue = (!isNaN(this.maxValue)) ? Math.min(this.maxValue, MAX_CURRENCY) : MAX_CURRENCY;
	var value = this.value.replace(/(.)\-/g, '$1.');

	if (value < minValue)
	{
		this.select();
		this.focus();
		validateAlert("Please enter a number greater than or equal to " + MIN_CURR_STRING + " into %f.", this);
		return false;
	}
	else if (value > maxValue)
	{
		this.select();
		this.focus();
		validateAlert("Please enter a number less than or equal to " + MAX_CURR_STRING + " into %f. (This is an arbitrary maximum imposed by Incorporator.)", this);
		return false;
	}

	return true;
}



