/************************************************************************************

Javascript form validation functions.

Use this js file to validate the following data types:

Currency
Date
Decimal
Email
Integer
Phone (###-###-#### format only)

Use validateAny() function as base for any validation function.  Simply pass in
the regular expression, the value of the field and whether or not the field is
required.  After that, you may perform any other operations on the value such
as checking for valid value ranges, etc.

************************************************************************************/

function validateAny(re, val, required) {
	if (!required && trim(val).length == 0) {
		// if the value is not required and empty, return true
		return true;
	}
	return (re.test(val));	
}

function validateSimpleString(val, required) {
	re = /.+/;
	return validateAny(re, val, required);
}

function validatePersonalName(val, required) {
	re = /^[a-zA-Z]+[a-zA-Z\-\.'\s]*$/;
	return validateAny(re, trim(val), required);
}

/************************************************************************************
CURRENCY
************************************************************************************/

function validateCurrency(val, min, max, required) {
	var re = /^\$?(\d|-)?(\d|,)*\.?\d*$/;
	if (validateAny(re, val, required)) {
		var floatVal = (val == "" ? parseFloat("0") : parseFloat(val.replace(/\$/, "")));
		if (floatVal >= min && floatVal <= max) {
			return true;
		}
	}
	return false;
}

/************************************************************************************
DATE
************************************************************************************/

function validateDate(val, required)	{
	var seperator = "/";
	var day = 0;
	var month = 0;
	var year = 0;
	var leap = 0;
	var err = 0;
	var regExp = /^(\d{1,2})[\/\-]+(\d{1,2})[\/\-]+(\d{4})$/;
		
	if (validateAny(regExp, val, required)) {
		if (regExp.exec(val)) {
			month = parseInt(RegExp.$1, 10);
			day = parseInt(RegExp.$2, 10);
			year = parseInt(RegExp.$3, 10);
		} else {
			return false;
		}
			
		if (year == 0){
			return false;
		}

		if ((month < 1) || (month > 12)) {
			return false;
		}

		if (day < 1) {
			return false;
		}

		if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
			leap = 1;
		}

		if ((month == 2) && (leap == 1) && (day > 29)) {
			return false;
		}

		if ((month == 2) && (leap != 1) && (day > 28)) {
			return false;
		}

		if ((day > 31) && ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))) {
			return false;
		}

		if ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))) {
			return false;
		}
	} else {
		return false;
	}
	return true;
}

/************************************************************************************
DECIMAL
************************************************************************************/

function validateDecimal(val, min, max, required) {
	var re = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
	
	if (validateAny(re, val, required)) {
		if (val >= min && val <= max) {
			return true;
		}
	}
	return false;
}

/************************************************************************************
EMAIL
************************************************************************************/

function validateEmail(val, required) {
	var re = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-\._]+\.[a-zA-Z]{2,3}$/;
	
	re.ignoreCase = true;
	
	return validateAny(re, val, required);
}

/************************************************************************************
INTEGER
************************************************************************************/

function validateInteger(val, min, max, required) {
	var re = /^\d+$/;
	
	if (validateAny(re, val, required)) {
		if (val >= min && val <= max) {
			return true;
		}
	}
	return false;
}

/************************************************************************************
PHONE
************************************************************************************/

function validatePhone(val, required) {
	//		 /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
	var re = /^[\d]{3}\-?[\d]{3}\-?[\d]{4}$/;
	return validateAny(re, val, required);
}

/************************************************************************************
UTILITY FUNCTION(S)
************************************************************************************/

function trim(val) {
	var expRSpace = /\s{1,}$/;       // Catches any trailing spaces
	var expLSpace = /^\s{1,}/;       // Catches any leading spaces
	var expMSpace = /\s{2,}/;        // Catches any double spaces in the middle of the value
	
	val = new String(val);
	
    if(val.length > 0) {
	    while(val.match(expMSpace) != null) {
	        val = val.replace(expMSpace, " ");
	    }

	    val = val.replace(expRSpace, "").replace(expLSpace, "");
    }
    
	return val;
}
