function trim() {
	var val = this;

	if (val != '') {
		var re = /^ +/;
		val = val.replace(re, '');
		re = / +$/;
		val = val.replace(re, '');
	}
	else {
		val = '';
	}
	
	return val;
}

String.prototype.trim = trim;

function utErrorMessage(language) {
	this.language = language;
	
	this.error0 = new Array (
	'Invalid \"$\".', 
	'\"$\" must not be empty.',
	'\"$\" must not be greater than \"$\".',
	'\"$\" must be numeric.',
	'Please select at least one \"$\".',
	'Please select at most $ \"$\".',
	'Please select \"$\".');
	
	this.error1 = new Array (
	'Invalid \"$\".', 
	'\"$\" must not be empty.',
	'\"$\" must not be greater than \"$\".',
	'\"$\" must be numeric.',
	'Please select at least one \"$\".',
	'Please select at most $ \"$\".',
	'Please select \"$\".');
}

utErrorMessage.prototype = {
	setLanguage : function(language) {
		this.language = language;
	},
	getErrorMessage : function(no, value) {
		if (typeof value == "undefined") {
			value = new Array();
		}
		else {
			if (typeof value == "string") {
				value = new Array(value);
			}
		}
		
		var message = '';
		
		if (no < this.error1.length) {
			if (this.language == 1) {
				message = this.error1[no];
			}
			else {
				message = this.error0[no];
			}
			
			var re = /\$/;
			
			for (var v = 0; v < value.length; v++) {
				message = message.replace(re, value[v]);
			}
		}
		
		return message;
	}
};

function utData(utErrorMessage) {
	this.utErrorMessage = utErrorMessage;
}

utData.prototype = {
	is_date : function(value) {
		var valid_day = new Array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		
		var datetime = value.split(" ");
		var date = datetime[0].split("-");
		
		if ((((date[2]%4) == 0) && ((date[2]%100) != 0)) || ((date[2]%400) == 0) ) {
			valid_day[2] = 29;
		}
		
		var re = /^0/;
			
		date[0] = date[0].replace(re, "");
		date[1] = date[1].replace(re, "");
		
		if (date[1] < 1 || date[1] > 12) {
			return false;
		}
		else if (date[0] < 1 || date[0] > valid_day[date[1]]) {
			return false;
		}
		
		if (typeof datetime[1] != "undefined") {
			var time = datetime[1].split(":");
			
			time[0] = time[0].replace(re, "");
			time[1] = time[1].replace(re, "");
			time[2] = time[2].replace(re, "");
			
			if (time[0] > 23) {
				return false;
			}
			else if (time[1] > 59) {
				return false;
			}
			else if (time[2] > 59) {
				return false;
			}
		}
		
		return true;
	},
	findObject : function(theForm, n){
		var total = theForm.length;
		
		for (var x=0; x<total; x++){
			if (theForm[x].name == n){
				return x;
			}
		}
		
		return false;
	},
	checkLine : function(theForm) {
		var total = theForm.options.length;
		
		for(var x=0; x<total; x++){
			theForm.options[x].value = theForm.options[x].value.trim();

	  	if(theForm.options[x].value == 'ReMaRkS' && theForm.options[x].selected == true){
	    	theForm.options[x].selected = false;
	    	theForm.options[x+2].selected = true;
	      theForm.focus();
	      break;
	    }
	    
	    if(theForm.options[x].value == 'line' && theForm.options[x].selected == true){
	    	theForm.options[x].selected = false;
	    	theForm.options[x+1].selected = true;
	      if (theForm.options[x+1].value == 'ReMaRkS'){
	      	theForm.options[x+3].selected = true;
	      }
	      theForm.focus();
	      break;
	    }
	    
	    if(theForm.options[x].value == 'NuLlS' && theForm.options[x].selected == true){
	    	theForm.options[x].selected = false;
	    	theForm.options[x+3].selected = true;
	      theForm.focus();
	      break;
	    }
		}
	},
	checkNumber : function(theForm, theObject, theSubject, empty, maxLimit) {
		var objects = theForm[theObject];

		if (typeof objects == "undefined") {
	  	var xn = this.findObject(theForm, theObject);
			objects = theForm[xn];
		}
		
		if (typeof objects != "undefined") {
			if (typeof objects.length != "undefined") {
				objects = objects[0];
			}
			
	    objects.value = objects.value.trim();
	    var value = objects.value;
	    
			if (empty && value.length == 0) {
				var message = this.utErrorMessage.getErrorMessage(1, new Array(theSubject));
				alert(message);
	    	objects.focus();
	    	return false;
			}
			else if (value.length > maxLimit) {
				var message = this.utErrorMessage.getErrorMessage(2, new Array(theSubject, maxLimit));
				alert(message);
	    	objects.focus();
	    	return false;
			}
			else if (!empty && value.length == 0) {
				return true;
			}
			else {
				var re = /^[0-9]+$/;
				
				if (!value.match(re)){
					var message = this.utErrorMessage.getErrorMessage(3, new Array(theSubject));
					alert(message);
	    		objects.focus();
	    		return false;
	    	}
			}
		}
		else {
			return false;
		}
		
		return true;
	},
	checkString : function(theForm, theObject, theSubject, empty, maxLimit, typ) {
	  var objects = theForm[theObject];
		
		if (typeof objects == "undefined") {
	  	var xn = this.findObject(theForm, theObject);
			objects = theForm[xn];
		}
		
		if (typeof objects != "undefined") {
			if (typeof objects.length != "undefined") {
				objects = objects[0];
			}
			
	    objects.value = objects.value.trim();
	    var value = objects.value;
	    
	    if (empty && value.length == 0) {
				var message = this.utErrorMessage.getErrorMessage(1, new Array(theSubject));
				alert(message);
	    	objects.focus();
	    	return false;
			}
			else if (value.length > maxLimit) {
				objects.value = value.substring(0, maxLimit);
			}
			
			if (typ == 1 && value.length > 0) { //Name
				var re = /^[a-zA-Z ]+$/;
		
				if(!value.match(re)) {
					var message = this.utErrorMessage.getErrorMessage(0, new Array(theSubject));
					alert(message);
		    	objects.focus();
		    	return false;
				}
			}
		}
		else {
			return false;
		}

		return true;
	},
	checkAddress : function(theForm, theObject, theSubject, empty, maxLimit, typ) {
		var objects = theForm[theObject];

		if (typeof objects == "undefined") {
	  	var xn = this.findObject(theForm, theObject);
			objects = theForm[xn];
		}
		
		if (typeof objects != "undefined") {
			if (typeof objects.length != "undefined") {
				objects = objects[0];
			}
			
    	objects.value = objects.value.trim();
    	objects.value = objects.value.toLowerCase();
    	var value = objects.value;
	    
			if (empty && value.length == 0) {
				var message = this.utErrorMessage.getErrorMessage(1, new Array(theSubject));
				alert(message);
    		objects.focus();
    		return false;
			}
			else if (value.length > maxLimit) {
				var message = this.utErrorMessage.getErrorMessage(2, new Array(theSubject, maxLimit));
				alert(message);
    		objects.focus();
    		return false;
			}
			else if (typ == 1) {
				var re = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
		
				if(!value.match(re)) {
					var message = this.utErrorMessage.getErrorMessage(0, new Array(theSubject));
					alert(message);
	    		objects.focus();
					return false;
				}
			}
			else if (typ == 2) {
				if (value == '') {
					objects.value = 'http://';
				}
				else if (value != 'ftp://' && value != 'http://' && value != 'https://') {
					var re = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
			
					if(!value.match(re)) {
						var message = this.utErrorMessage.getErrorMessage(0, new Array(theSubject));
						alert(message);
		    		objects.focus();
						return false;
					}
				}
			}
		}
		else {
			return false;
		}
		
		return true;
	},
	//Format date must be 'DD-MM-YYYY or DD-MM-YYYY HH24:MI:SS'
	checkDate : function(theForm, theObject, theSubject, empty, value) {
		var objects = theForm[theObject];
		
		if (typeof objects == "undefined") {
	  	var xn = this.findObject(theForm, theObject);
			objects = theForm[xn];
		}
		
		if (typeof objects != "undefined") {
			if (typeof objects.length != "undefined") {
				objects = objects[0];
			}
			
			value = value.trim();
			
			if (empty && value.length == 0) {
				var message = this.utErrorMessage.getErrorMessage(1, new Array(theSubject));
				alert(message);
		    objects.focus();
		    return false;
			}
			else if (!empty && value.length == 0) {
				return true;
			}
			else {
				if (!this.is_date(value)) {
					var message = this.utErrorMessage.getErrorMessage(0, new Array(theSubject));
					alert(message);
		    	objects.focus();
		    	return false;
				}
			}
		}
		else {
			return false;
		}
		
		return true;
	},
	checkSelected : function(theForm, theObject, theSubject, empty, maxLimit){
	  var y=0;
	  var xn = this.findObject(theForm, theObject);
		var objects = theForm[xn];
		
		if (typeof objects != "undefined") {
			var total = objects.length;
			
		  for (var x=0; x<total; x++){
		    if (objects[x].selected){
		      y++;
		    }
		  }
		  
		  if(empty && y < 1){
		    var message = this.utErrorMessage.getErrorMessage(4, new Array(theSubject));
		    alert(message);
		    objects.focus();
		    return false;
		  }
		  else if(y > maxLimit){
	    	var message = this.utErrorMessage.getErrorMessage(5, new Array(maxLimit, theSubject));
	    	alert(message);
	      objects.focus();
	      return false;
		  }
		}
		else {
			return false;
		}
	  
	  return true;
	},
	checkRadio : function(theForm, theObject, theSubject, empty) {
		if (empty) {
			var objects = theForm[theObject];
			
			if (typeof objects == "undefined") {
		  	var xn = this.findObject(theForm, theObject);
				objects = theForm[xn];
			}
			
			if (typeof objects != "undefined") {
				if (typeof objects.length == "undefined") {
					objects = new Array(objects);
				}
				
				var total = objects.length;
				
				for (var x=0; x<total; x++){
					if (objects[x].checked) {
						return true;
					}
				}
				
				var message = this.utErrorMessage.getErrorMessage(6, new Array(theSubject));
				alert(message);
	    	objects[0].focus();
	    	return false;
			}
			else {
				return false;
			}
		}

		return true;
	}
}
