/*
     MODULE:	CheckFunctions.js
     PURPOSE:	provides general JavaScript functions to check different datatypes
     CREATED:	20.02.2002
     AUTHOR:	Trummer Martin
     COPYRIGHT:	(c) 2002 Trummer Martin for FH Joanneum
	 INFO:		
     CHANGE HISTORY:
*/

/*
	converts a string entered by the user to a number that can be used in JavaScript
	NOTE: each user can have "," or "." as decimalseparator
*/
function userNum2JS(NumString)
{
	if (getDecimalSeparator() == ",")
		return NumString.replace(",", ".");
	else
		return NumString;	 
}

/*  containsSpace(
    checks if the given string contains a space
	return values: 	true	string contains a space
					false	string does not contain a space
*/
function containsSpace(object_value)
{

	res = object_value.search(/ /);
	if(res == -1)
 	{
	 	return false;
	}
	else
	{
		return true;
	}
}

/*	checkInteger
	checks if a string can be converted into an integer
	parameters:		object_value 	the string to be checked
	return values:	true			conversion is ok
					false			conversion is wrong
*/
function checkInteger(object_value)
{
    if (object_value.length == 0)
        return false;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -
    //   otherwise containing only the characters 0-9
	var decimal_format = getDecimalSeparator();
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char >= 0) 					
		return false;
    else
		return checkNumber(object_value);
}

/*	checkNumber
	checks if a string can be converted into an number
    	having an optional leading + or -.
    	having at most 1 decimal seperator
    	otherwise containing only the characters 0-9.
	
	parameters:		object_value 	the string to be checked
	return values:	true			conversion is ok
					false			conversion is wrong
*/
function checkNumber(object_value)
{
    if (object_value.length == 0)
        return false;

    //Returns true if value is a number defined as
    //   having an optional leading + or -
    //   having at most 1 decimal point
    //   otherwise containing only the characters 0-9
	var start_format = " "+getDecimalSeparator()+"+-0123456789";
	var number_format = " "+getDecimalSeparator()+"0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - ,  blank or a digit
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 0)
		return false;
    
	//Remaining characters can be only , or a digit, but only one decimal
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		
		if (check_char < 0)
			return false;         	// no digit
		else if (check_char == 1) 	// decimal separator
		{
			if (decimal)			
				return false;		// second decimal!
			else
				decimal = true;		// first decimal is ok
		}
		else if (check_char == 0)	// space
		{
		   // ignore leading blanks
			if (decimal || digits)	
				trailing_blank = true;
		}
	    else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
}

/*	checkRange
	combines checkNumber and numberRange
	
	parameters:		object_value 	the string to be checked
					min_value		the minimum value for the rangecheck
					max_value		the maximum value for the rangecheck
	return values:	true			conversion and rangecheck are ok
					false			conversion or rangecheck are wrong
*/
function checkRange(object_value, min_value, max_value)
{
    if (checkNumber(object_value))
	{
		return (numberRange((eval(object_value)), min_value, max_value));
	}
	
	return false;
}

/*	numberRange
	checks if the number is between the rangeborders
	
	parameters:		object_value 	the number to be checked
					min_value		the minimum value for the rangecheck
					max_value		the maximum value for the rangecheck
	return values:	true			rangecheck is ok
					false			rangecheck is wrong
*/
function numberRange(object_value, min_value, max_value)
{
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
			return false;
	}

    // check maximum
    if (max_value != null)
	{
		if (object_value > max_value)
			return false;
	}
	
    //All tests passed, so...
    return true;
}

/*	checkValidTime
	checks if the given string is a valid time
	uses 24 hr clock
	formats: H:m, HH:m, H:mm, HH:mm
	valid time examples: 00:00, 9:4, 13:10, 23:59
	
	parameters:		object_value 	the string to be checked
	return values:	true			check is ok
					false			check failed
*/
function checkValidTime(object_value)
{
	var colonPos = object_value.indexOf(":");
	var hours = "";
	var mins = "";
	
	//blank is not valid
	if (object_value == "")
	{
		return false;
	}
	
	//entry must have a colon to separate hours and minutes
	if (colonPos == -1)
	{
		return false;
	}
	
	//split in hours and minutes
	hours = object_value.slice(0, colonPos);
	mins = object_value.slice(colonPos+1);
	
	//both must be integers
	if (!checkInteger(hours) || !checkInteger(mins))
	{
		return false;
	}
	
	//check the ranges
	if (!numberRange(hours, 0, 23) || !numberRange(mins, 0, 59))
	{
		return false;
	}
	
	//still here, so everything's ok
	return true;
}

/*	getHoursFromTime
	returns the hour part of the time
	
	parameters:		object_value 	the time-string to be checked
	return value:	hours
					
	the time must be valid: see checkValidTime()
*/
function getHoursFromTime(object_value)
{
	var colonPos = object_value.indexOf(":");
	return object_value.slice(0, colonPos);
}

/*	getMinsFromTime
	returns the minute part of the time
	
	parameters:		object_value 	the time-string to be checked
	return value:	minutes
					
	the time must be valid: see checkValidTime()
*/
function getMinsFromTime(object_value)
{
	var colonPos = object_value.indexOf(":");
	return object_value.slice(colonPos+1);
}


/*	getDateFromUser
	returns a JavaScript Date object - built from the input string
	hours, minutes, seconds and milliseconds are zero!
	
	parameters:		object_value 	the date-string to be checked
	return value:	NULL			if the date is invalid
					JS Date-Object	if the date is valid
					
	supported formats
	[1] = "dd.mm.yyyy"
	[2] = "dd.mm.yy"
	[3] = "dd/mm/yyyy"
	[4] = "dd/mm/yy"
	[5] = "mm/dd/yyyy"
	[6] = "mm/dd/yy"
	
	see also: userDate()
*/			
function getDateFromUser(object_value)
{
	var usrFormat = getDateFormat();
	var	sepChar = '/';
	var arrayParts;
	var day = 0;
	var month = 0;
	var year = 0;
	var theDate = new Date(1976, 12, 14, 0, 0, 0);
		
	if (usrFormat < 3)
	{
		sepChar = '.';
	}
	
 	arrayParts = object_value.split(sepChar);
	
	if (arrayParts.length != 3)
	{
		return null;
	}
	
	if (usrFormat < 5)
	{
		day = arrayParts[0];
		month = arrayParts[1];
		year = arrayParts[2];
	}
	else
	{
		day = arrayParts[1];
		month = arrayParts[0];
		year = arrayParts[2];
	}
	
	if (usrFormat == 2 || usrFormat == 4)
	{
		if (year.length == 2)
		{
			//otherwise JS would interpret it as 19xx
			year = "20"+year;
		}
	}
	
	// subtract 1 from month entry - see JavaScript Date documentation
	month = month - 1;
	
	theDate.setFullYear(year);
	theDate.setMonth(month);
	theDate.setDate(day);
	theDate.setMilliseconds(0);
	/* 
		the following check is neccessary because of following case:
		input "32.12.2002" is in JS not invalid, but interpreted as "01.01.2003"
		since this is obviously not what the user meant the result of this function is null
	*/
	if (	
		theDate.getFullYear() != year ||
		theDate.getMonth() != month ||
		theDate.getDate() != day
		)
	{
		return null;
	}

	return theDate;
}

/*	fillLeadingZeros
	fills the given string (value) with as many zeros at the beginning, so that the
	lenght of the return-string is noOfZeros
	
	parameters:		value		any string
					noOfZeros	the desired length of the returnstring
	return value:	string of lenght noOfZeros filled with 0
*/
function fillLeadingZeros(value, noOfZeros)
{
	var result = "";
	//force value to type string
	value = "" + value;
	for(var i=value.length; i<noOfZeros; i++)
	{
		result = result + "0";
	}
	result = result + value;
	
	return result;
}

/*	userDate
	returns a userdepandant string representation of a valid JS Date object
	
	parameters:		date_object	valid JS Date object
	return value:	string in one of the following formats:
						[1] = "dd.mm.yyyy"
						[2] = "dd.mm.yy"
						[3] = "dd/mm/yyyy"
						[4] = "dd/mm/yy"
						[5] = "mm/dd/yyyy"
						[6] = "mm/dd/yy"
	
	see also: getDateFromUser()
*/		
function userDate(date_object)
{
	var usrFormat = getDateFormat();
	var	sepChar = '/';
	var day = 0;
	var month = 0;
	var year = 0;

	if (usrFormat < 3)
	{
		sepChar = '.';
	}
	
	day = date_object.getDate();
	month = date_object.getMonth();
	year = date_object.getFullYear();
	
	day = fillLeadingZeros(day, 2);
	
	//add 1: see JS-Date documentation
	month++;
	month = fillLeadingZeros(month, 2);
	
	if (usrFormat == 2 || usrFormat == 4)
	{
		year = year.slice(1);
	}
	
	if (usrFormat < 5)
	{
		return day + sepChar + month + sepChar + year;
	}
	else
	{
		return month + sepChar + day + sepChar + year;
	}
}

/*	compareDates
	Compares the 2 valid javascript Dates by year, month and day.
	
	parameters:		date1	first JS-Date
					date2	second JS-Date
	return value:	<0	date1 < date2
					 0	date1 == date2
					>0	date1 > date2
*/
function compareDates(date1, date2)
{
	return 	( 	
				(date1.getFullYear()*12+date1.getMonth()*31+date1.getDate()*1)
				-
				(date2.getFullYear()*12+date2.getMonth()*31+date2.getDate()*1)
			);
}

/*	endsWith
	checks if the soruce string ends with the search sting
	e.g. endsWith("fucku", "ku")  --> true
	
	parameters:	source	the source string
				search	the string to search for
				
 	return value:	true	source ends with search
					false	source does not end with search
 */
function endsWith(source, search)
{
	if (source == "" || 
		search == "" ||
		search.length > source.length)
	{
		return false;
	}
	
	for (var i = 1; i <= search.length; i++)
	{
		if (source.charAt(source.length - i) != search.charAt(search.length - i))
		{
			return false;
		}
	}
	
	return true;
}

/*	endsWithNoCase
	same as endsWith but caseinsensitive
 */
function endsWithNoCase(source, search)
{
	return endsWith(source.toUpperCase(), search.toUpperCase());
}

