

// Trim leading and trailing spaces
function trim(lstr) 
{
    return ltrim(rtrim(stripLineFeed(lstr)));
}

function stripLineFeed(strText)
{
	var strReturnText = strText;
	var flgContinue = true;

	// Only check if the string passed in has a length greater than zero	
	if (strReturnText.length > 0)
	{
		// Loop as long as the last character is either a line feed or a carriage return
		while (flgContinue == true)
		{
			// If the last character is either a backspace or a line feed, strip it off
			if (strReturnText.charAt(strReturnText.length - 1) == '\n' || strReturnText.charAt(strReturnText.length - 1) == '\r')
			{
				strReturnText = strReturnText.substr(0, strReturnText.length - 1);
			}
			else
			{
				// If the last character is not a carriage return or line feed, stop looping
				flgContinue = false;
			}
		}
	}

	return strReturnText;
}
  
//  This function trims all spaces from the left-hand side of a string.
function ltrim(lstr) 
{
	if (lstr != "") 
	{
		var strlen, cptr, lpflag, chk;
		strlen = lstr.length;
		cptr = 0;
		lpflag = true;

		do 
		{
			chk = lstr.charAt(cptr);
            if (chk != " ") 
            {
				lpflag = false;
			}
            else 
            {
                if (cptr == strlen) 
                {
					lpflag = false;
				}
                else 
                {
					cptr++;
				}
			}
		}
        
        while (lpflag == true)
		if (cptr > 0) 
		{
			lstr = lstr.substring(cptr,strlen);
		}
	}
	
	return lstr;
}

//  This function trims all spaces from the right-hand side of a string.
function rtrim(lstr) 
{
	if (lstr != "") 
	{
		var strlen, cptr, lpflag, chk;
		strlen = lstr.length;
		cptr = strlen;
		lpflag = true;

		do 
		{
			chk=lstr.charAt(cptr-1);
			if (chk != " ") 
			{
			    lpflag = false;
			}
			else 
			{
				if (cptr == 0) 
				{
					lpflag = false;
				}
				else 
				{
				    cptr--;
				}
			}
		}

        while (lpflag == true)
        if (cptr < strlen) 
        {
			lstr = lstr.substring(0, cptr);
		}
	}
    
    return lstr;
}


/*	The isNumeric function validates a string to determine whether or not it contains a numeric value.
	Parameters: lstr - Contains string value to validate
	Returns: true/false  */
function isNumeric(lstr) 
{
	lstr = rtrim(lstr);
	if (lstr != "") 
	{
		//declare local variables
		var strlen, curptr, setptr, chk, inloop, decflag, minusflag, iserror;
		iserror = false;
		decflag = false;
		minusflag = false;
		strlen = lstr.length;
		curptr = 0;
		chk;
		// first check for space - .
		inloop = true;
		for (curptr = 0; curptr < strlen; curptr++) 
		{
			chk = lstr.charAt(curptr);
			if (chk >= "0" && chk <= "9")
			{
				break;
			}
			
			if (chk == "-") 
			{
				minusflag = true;
				break;
			}
			if (chk == ".") 
			{
				decflag = true;
				break;
			}
			if (chk != " ") 
			{
				return false;
			}
		}
		
		if (curptr >= strlen-1) 
		{
			if (decflag || minusflag || chk == " ") 
			{
				return false;
			}
			else 
			{
				return true;
			}
		}
		setptr = curptr + 1;
		for (curptr = setptr; curptr < strlen; curptr++) 
		{
			chk = lstr.charAt(curptr);
			if (chk < "0" || chk > "9") 
			{
				if (chk != ".") 
				{
					return false;
				}
				else
				{
					if (decflag) 
						{
							return false;
						}
					else 
					{
						decflag = true;
					}
				}
			}
		}
		return true;
	}
	return false;
}



function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsInteger=true;
   var Char;

 
   for (i = 0; i < sText.length && IsInteger == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsInteger = false;
         }
      }
   return IsInteger;
   
   }




// This function verifies that an email conforms to RFC 822 email address standard
function isValidEmailAddress(strEmail) 
{
    if (strEmail == null || strEmail == "") 
    {
        return false;
    }
    else 
    {
        // Patterns
        var emailPat=/^(.+)@(.+)$/
        var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
        var validChars="\[^\\s" + specialChars + "\]"
        var quotedUser="(\"[^\"]*\")"
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
        var atom=validChars + '+'
        var word="(" + atom + "|" + quotedUser + ")"
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

        var matchArray = strEmail.match(emailPat)
        if (matchArray == null) 
        {
	        return false;
        }

        var user = matchArray[1]
        var domain = matchArray[2]

        // See if "user" is valid 
        if (user.match(userPat) == null) 
        {
            // user is not valid
			return false;
		}

        var IPArray = domain.match(ipDomainPat)
        if (IPArray!=null) 
        {
			// this is an IP address
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
			        return false;
				}
			}
        }

        // Domain is symbolic name
        var domainArray = domain.match(domainPat)
        if (domainArray == null) 
        {
	        return false;
        }

        // Now we need to break up the domain to get a count of how many atoms it consists of.
        var atomPat = new RegExp(atom,"g")
        var domArr = domain.match(atomPat)
        var len = domArr.length
        if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) 
        {
           // the address must end in a two letter or three letter word.
	        return false;
        }

        // Make sure there's a host name preceding the domain.
        if (len < 2) 
        {
	        return false;
        }
    }

    return true;
}

function isAlphaNumeric(strText)
{
	// Check each letter to make sure it's a letter or a number
	for (var i = 0; i < strText.length; i++)
	{
		if ((strText.charAt(i) < '0' || strText.charAt(i) > '9') && (strText.charAt(i) < 'a' || strText.charAt(i) > 'z') && (strText.charAt(i) < 'A' || strText.charAt(i) > 'Z'))
		{
			return false;
		}
	}

	return true;
}

function isAlphaNumericSymbol(strText) 
{
  var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO0123456789!@#$%^&*()_-+=[]{};:',./?><>"
  var ok = "yes";
  var temp;
  for (var i=0; i<strText.value.length; i++)
   {
     temp = "" + strText.value.substring(i, i+1);
     if (valid.indexOf(temp) == "-1") ok = "no";
   
  return false;
  }

return true;
   
}



/* Takes two strings that represents numbers, and returns a boolean
   indicating whether or not the first number is larger than the second number */
function isFirstNumberLarger(strFirst, strSecond)
{
	var lngFirst = parseFloat(strFirst);
	var lngSecond = parseFloat(strSecond);
	var flgReturnValue = false;

	// If the first number is greater than the second number
	if (lngFirst > lngSecond)
	{
		flgReturnValue = true;
	}
	
	return flgReturnValue;
}

/* Takes two strings that represents numbers, and returns a boolean
   indicating whether or not the first number is smaller than the second number */
function isFirstNumberSmaller(strFirst, strSecond)
{
	var lngFirst = parseFloat(strFirst);
	var lngSecond = parseFloat(strSecond);
	var flgReturnValue = false;

	// If the first number is greater than the second number
	if (lngFirst < lngSecond)
	{
		flgReturnValue = true;
	}
	
	return flgReturnValue;
}


function isFirstNumberLargerOrEqual(strFirst, strSecond)
{
	var lngFirst = parseFloat(strFirst);
	var lngSecond = parseFloat(strSecond);
	var flgReturnValue = false;

	// If the first number is greater than the second number
	if (lngFirst >= lngSecond)
	{
		flgReturnValue = true;
	}
	
	return flgReturnValue;
}

/* Takes two strings that represents dates, and returns a boolean
   indicating whether or not the first date is after the second date. */
function isFirstDateEarlier(strDelimiter, strFirstDate, strSecondDate)
{
	var month;
	var day;
	var year;
	var splitArray;
	
	// Get the first date
	splitArray = strFirstDate.split(strDelimiter);
	month = splitArray[1];
	day = splitArray[0];
	year = splitArray[2];
	var firstDate = new Date(year, month - 1, day)

	// Get the second date
	splitArray = strSecondDate.split(strDelimiter);
	month = splitArray[1];
	day = splitArray[0];
	year = splitArray[2];
	var secondDate = new Date(year, month - 1, day)
	
	if (firstDate < secondDate)
	{
		return true;
	}
	else
	{
		return false;
	}
}


function isDateEuropean(dateStr) {

    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Παρακαλώ η ημερομηνία πρέπει να είναι της μορφής ηη/μμ/εεεε.");
        return false;
    }

    day = matchArray[1]; // parse date into variables
    month = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Ο μήνας πρέπει να είναι 1 και 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Η ημέρα πρέπει να είναι μεταξύ 1 και 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Ο μήνας "+month+" δεν έχει 31 μέρες!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("Ο Φεβρουάριος " + year + " δεν έχει " + day + " μέρες!");
            return false;
        }
    }
	
	
	
    return true; // date is valid
}

// This function verifies that an email conforms to RFC 822 email address standard
function isValidEmailAddress(strEmail) 
{
    if (strEmail == null || strEmail == "") 
    {
        return false;
    }
    else 
    {
        // Patterns
        var emailPat=/^(.+)@(.+)$/
        var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
        var validChars="\[^\\s" + specialChars + "\]"
        var quotedUser="(\"[^\"]*\")"
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
        var atom=validChars + '+'
        var word="(" + atom + "|" + quotedUser + ")"
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

        var matchArray = strEmail.match(emailPat)
        if (matchArray == null) 
        {
	        return false;
        }

        var user = matchArray[1]
        var domain = matchArray[2]

        // See if "user" is valid 
        if (user.match(userPat) == null) 
        {
            // user is not valid
			return false;
		}

        var IPArray = domain.match(ipDomainPat)
        if (IPArray!=null) 
        {
			// this is an IP address
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
			        return false;
				}
			}
        }

        // Domain is symbolic name
        var domainArray = domain.match(domainPat)
        if (domainArray == null) 
        {
	        return false;
        }

        // Now we need to break up the domain to get a count of how many atoms it consists of.
        var atomPat = new RegExp(atom,"g")
        var domArr = domain.match(atomPat)
        var len = domArr.length
        if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) 
        {
           // the address must end in a two letter or three letter word.
	        return false;
        }

        // Make sure there's a host name preceding the domain.
        if (len < 2) 
        {
	        return false;
        }
    }

    return true;
}

/*	The isNumericCurrency function validates a string to determine whether or not it contains a numeric value.
	Parameters: lstr - Contains string value to validate
	Returns: true/false  */
function isNumericCurrency(lstr) 
{
	lstr = rtrim(lstr);
	if (lstr != "") 
	{
		//declare local variables
		var strlen, curptr, setptr, chk, inloop, decflag, minusflag, iserror;
		iserror = false;
		decflag = false;
		minusflag = false;
		strlen = lstr.length;
		curptr = 0;
		chk;
		// first check for space - .
		inloop = true;
		for (curptr = 0; curptr < strlen; curptr++) 
		{
			chk = lstr.charAt(curptr);
			if (chk >= "0" && chk <= "9")
			{
				break;
			}
			
			if (chk == "-") 
			{
				minusflag = true;
				break;
			}
			if (chk == ",") 
			{
				decflag = true;
				break;
			}
			if (chk != " ") 
			{
				return false;
			}
		}
		
		if (curptr >= strlen-1) 
		{
			if (decflag || minusflag || chk == " ") 
			{
				return false;
			}
			else 
			{
				return true;
			}
		}
		setptr = curptr + 1;
		for (curptr = setptr; curptr < strlen; curptr++) 
		{
			chk = lstr.charAt(curptr);
			if (chk < "0" || chk > "9") 
			{
				if (chk != ",") 
				{
					return false;
				}
				else
				{
					if (decflag) 
						{
							return false;
						}
					else 
					{
						decflag = true;
					}
				}
			}
		}
		return true;
	}
	return false;
}

function num2money(n_value) {

	// validate input
	if (isNaN(Number(n_value)))
		return 'ERROR';

	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2 + '00').substring(0,2);

	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value > 1) {
		s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);
		s_result = s_subresult.slice(-3) + (b_first ? ',' : '.') + s_result;
		b_first = false;
		n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
		s_result = '0.' + s_result;
	
	// apply formatting and return
	return  b_negative
		? '(' + s_result + ')'
		: '' + s_result;
}
