// --------------------------------------------------
// - Add trimm-functions to string object           -
// --------------------------------------------------
<!-- Hide from non-JavaScript browsers
            
function strltrim() {
    return this.replace(/^\s+/,'');
}

function strrtrim() {
    return this.replace(/\s+$/,'');
}
    
function strtrim() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
                    
// End hiding --> 

// --------------------------------------------------
// -------- ISEMPTY CHECK ---------------------------
// --------------------------------------------------
function isEmpty(p_input){
	return ((p_input == null) || (p_input == "")) ? true : false;
}

// --------------------------------------------------
// -------- ISPOSINTEGER CHECK ----------------------
// --------------------------------------------------

function isPosInteger(p_numb){
	var re = /[0-9]{1,}/;
	var match = re.exec(p_numb);
	return (match==p_numb);
	//return true;
}

// --------------------------------------------------
// -------- DATE CHECK ------------------------------
// --------------------------------------------------
// a date is valid from 01/01/1900 till 01/01/3999

var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function getDays(month, year) {
   // Test for leap year when February is selected.
   if (2 == month)
      return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
   else
      return daysInMonth[month-1];
}

function checkDate(p_day, p_month, p_year){			
	var re = /\b(0?[1-9]|[12][0-9]|3[01])[\x2F](1[0-2]|0?[1-9])[\x2F]((19|[23][0-9])\d{2})/;
	if (!isPosInteger(p_day))  //a negative day will not be considered as fault from the regexp
		return false;
	var str_date = p_day + "/" + p_month + "/" + p_year;
	var match = re.exec(str_date);
	return (match) ? (parseInt(match[1]) > getDays(match[2], match[3])) ? false : true : false;
}

// --------------------------------------------------
// -------- X-CHARACTER CHECK -----------------------
// --------------------------------------------------

function checkxCharacters(p_x, p_toCheck){
	if (!(isPosInteger(p_x)))
		return false;
	else{	
		var re = new RegExp("[a-z]{" + p_x + ",}","i");
		var match;
		match = re.exec(p_toCheck);
		return (match!=null);
	}
}

// --------------------------------------------------
// -------- E-MAIL CHECK ----------------------------
// --------------------------------------------------

function isEmail(p_mail){
	var reg = /[\-'#$%*=?^`~|.&_!0-9A-Z]+[^.][@][^.][\-#$%*=?^`~|.&_!0-9A-Z]+(.[\-#$%*=?^`~|&_!0-9A-Z]+)+/i;
	var match = reg.exec(p_mail);
	return (match!=null);
}

// --------------------------------------------------
// -------- CHECK IF DATE 'A' IS OLDER THAN DATE 'B'-
// --------------------------------------------------

function isDateaOlderThanDateb  (a_day, a_month, a_year, b_day, b_month, b_year){
	if ((checkDate(a_day, a_month, a_year)) && (checkDate(b_day, b_month, b_year))){
		if (a_year == b_year){
			if (a_month == b_month){
				return (a_day < b_day) ? true : false;					
			}
			else{
				return (a_month < b_month) ? true : false;
			}
		}
		else{
			return (a_year < b_year) ? true : false;
		}						
	}
	else{
		alert("One of the dates is not a valid date!");
		return false;
	}
}

// --------------------------------------------------
// -------- REMOVE ALL LEADING SPACES FROM STRING
// --------------------------------------------------
	
	function LTrim(strText)
	{
		var intSpacePos, strcurText;
		
		strcurText=strText;
		//removes all spaces starting the string
		intSpacePos=strcurText.indexOf(" ");
		while (intSpacePos==0)
		{
			strcurText=strcurText.substring(1,strcurText.length);
			intSpacePos=strcurText.indexOf(" ");
		}
		//alert ("Ltrim is: /%" + strcurText + "%/");
		return strcurText;
	}
	
// --------------------------------------------------
// -------- REMOVE ALL ENDING SPACES FROM STRING
// --------------------------------------------------
	function RTrim(strText)
	{
		var intSpacePos, strcurText;
		
		strcurText=strText;
		//removes all spaces ending the string
		if (strcurText.length!=0)
		{
			intSpacePos=strcurText.lastIndexOf(" ");
			while (intSpacePos==strcurText.length-1)
			{
				strcurText=strcurText.substring(0,strcurText.length-1);
				intSpacePos=strcurText.lastIndexOf(" ");
			}
		}
		//alert ("Rtrim is: /%" + strcurText + "%/");
		return strcurText;
	}
	
// -------------------------------------------------------
// -------- REMOVE ALL STARTING+LEADING SPACES FROM STRING
// -------------------------------------------------------
	function Trim(strText)
	{
		var strcurText=strText;
		strcurText=RTrim(LTrim(strcurText));
		//alert ("Trimmed: /%" + strcurText + "%/");
		return strcurText;
	}
	
// -------------------------------------------------------
// -------- CHECKS IF THE FIELD IS EMPTY, IF SO IT ALERT THE ERROR
// -------------------------------------------------------
	
	function FieldIsEntered(field, error){
		return (Trim(field.value).length == 0) ? false : true;
	}	
	
// -------------------------------------------------------
// -------- CHECKS IF A FIELD EXCEEDS A CERTAIN AMOUNT OF
// -------- CHARACTERS (CAN BE INTERESTING FOR TEXTAREAS).
// -------------------------------------------------------
	
	//ATTENTION: this function is left there for backwards compatibility only!!
	function FieldExceeds(field, length){
		return (FieldNotExceeds(field,length));
	}
	
	function FieldNotExceeds(field, length){
		return (field.value.length > length) ? false : true;
	}		
	
// -------------------------------------------------------
// -------- VALIDATES THE FIELD
// -------------------------------------------------------
	
	function validField(field, isOK ,error){
		if (!(isOK)){
			alert(error);
			field.focus();
			return false;			
		}	
		else{
			return true;
		}
	}	

// ------------------------------------------------------------------------------
// -------- VALIDATES TEXT LINES IN TEXTAREA AGAINST BACKEND REQUEREMENTS (INPUT)
// ------------------------------------------------------------------------------
function ValidateTextAreaAgainstBackend(taForm, tafieldName, noOfLines, maxLineLength, noOfLinesErrorMsg, noOfLinesInfo, maxLineLengthErrorMsg, maxLineLengthInfo) {
	var theField = eval(document[taForm][tafieldName]);
    re = /\n/g;
	var	arr = theField.value.split(re);
		
	if (arr.length > noOfLines) {
		alert(noOfLinesErrorMsg + '\n' + noOfLinesInfo + noOfLines);
		return false;
	}
		
    for (i=0; i<arr.length; i++) {
		if (String(arr[i]).length > maxLineLength) {
			alert(maxLineLengthErrorMsg + ':\n' + arr[i] + '\n' + maxLineLengthInfo + maxLineLength);
			return false;
		}
    }
	return true;
}