var Validation = {
	missingValues: false,
	
	isEmpty: function(fieldName, isOptionalField)
	{
		var el = document.getElementById(fieldName);
		
		this.flagField(fieldName, true);

		if (this.removeWhiteSpace(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 = '#E8A6B0';
		else 
			el.style.backgroundColor = '#fff';
	},

	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 regex = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
		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);
		}
	},
	
	getDateFromSelectGroup: function(groupId)
	{
		if ((document.getElementById(groupId +'[day]').value == null || document.getElementById(groupId +'[day]').value.length == 0)
			&& (document.getElementById(groupId +'[month]').value == null || document.getElementById(groupId +'[month]').value.length == 0)
			&& (document.getElementById(groupId +'[year]').value == null || document.getElementById(groupId +'[year]').value.length == 0)
		)
			return '';
			
		var day = document.getElementById(groupId +'[day]').value;
		var month = document.getElementById(groupId +'[month]').value;
		var year = document.getElementById(groupId +'[year]').value;
		
		if (day < 10) day = '0'+ day;
		if (month < 10) month = '0'+ month;
		
		return day +'.'+ month +'.'+ year;
	},
	
	isDate: function(strDate)
	{
		var isDateFormat = true;
		
		if (strDate.length != 10 && strDate.length != 0) {
		   isDateFormat = false;
		} else {
		   if(strDate.length==10){
		   	  isDateFormat = true;
		   	  var s = strDate;
			  for(i = 0; i < s.length; i++) {	  	 
				 if(i == 2 || i == 5) {
					if(s.charAt(i) != '.') {
					   isDateFormat = false;
					   break;
					}
				 } else {
					if(!/\d/.test(s.charAt(i))) {
					   isDateFormat = false;
					   break;
					} else {
					   if (i == 0) { //Check day
						  var d;
						  if (s.charAt(i) == '0')
						  	d = s.charAt(i+1);
						  else
						  	d = s.charAt(i) + s.charAt(i+1);
						  if (parseInt(d) <= 0 || parseInt(d) > 31) {
							 isDateFormat = false;
					   		 break;
						  }
					   }
					   if (i == 3) { //Check month
						  var m;
						  if (s.charAt(i) == '0')
						  	m = s.charAt(i+1);
						  else
						  	m = s.charAt(i) + s.charAt(i+1);
						  if (parseInt(m) <= 0 || parseInt(m) > 12) {
							 isDateFormat = false;
					   		 break;
						  }
					   }
					   if (i == 6) { //Check year
						  var m = s.charAt(i) + s.charAt(i+1) + s.charAt(i+2) + s.charAt(i+3);
						  if (parseInt(m) <= 1800 || parseInt(m) > 2100) {
							 isDateFormat = false;
					   		 break;
						  }
					   }
				 	} 
			  	 }
		   	  } 
		   } else {
		   	isDateFormat = false;  
		   } 
		 }
		 return isDateFormat;
	},
	
	removeWhiteSpace: function(strVal)
	{
		return strVal.split(' ').join('');
	}
}

