var Validation = {
	missingValues: false,
	
	isEmpty: function(fieldName, isOptionalField)
	{
		var el = document.getElementById(fieldName);
		
		this.flagField(fieldName, true);

		if (el.value == '') {
			if (!isOptionalField) {
				this.missingValues = true;
				this.flagField(fieldName, false);
			}
			
			return true;
		}
		
		return false;
	},
	
	flagField: function(fieldName, isValid)
	{
		var el = document.getElementById(fieldName);
			
		if (isValid)
			el.style.backgroundColor = '#fff';
		else
			el.style.backgroundColor = '#E8A6B0';
	},

	isSomething: function(sValue, validChars)
	{
		var isValid = true;
		var currentChar;
		
		for (i = 0; i < sValue.length && isValid == true; i++) 
		{ 
			currentChar = sValue.charAt(i); 
			if (validChars.indexOf(currentChar) == -1) 
			{
				isValid = false;
			}
		}
		
		return isValid;
	},

	isNumeric: function(sValue)
	{
		return this.isSomething(sValue, "0123456789");
	},

	isPhoneNo: function(sValue)
	{
		return (this.isNumeric(sValue) && sValue.length == 8);
	},

	isValidPostalCode: function(postal)
	{
		return (this.isNumeric(postal) && postal.length == 4);
	},
	
	isValidEmail: function (email)
	{
		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regex = new RegExp(emailReg);
		return regex.test(email);
	},
	
	postalCodeLookup: function (postal_code, fieldName, displayOption)
	{
		if (!_ajax_enabled) {
			alert('AJAX support disabled');
			return false;
		}

		// Lookup postal code
		if (postal_code == '') {
			$(fieldName).value = '';
		} else {
			setValue('get_postal_info.php?id='+ postal_code, fieldName, displayOption);
		}
	}
}
