// ##################################################################################
// Function: parseTime
// Description: Validates an input time string of format HH:mm and returns
//		standardized, left padded time string of format HH:mm if valid,
//		otherwise return value is 00:00.
//
// Parameters:
//	time:	input time in format HH:mm, e.g. 23:15
//
// Created by: Maic Stohr, 2008-04-17
//
// Modified by:
// 1.
// ##################################################################################

function parseTime (time) {
	if (time == '')
		return false;
	else { 
		var hours = 0;
		var minutes = 0;
		
		if (time.indexOf(':') != -1) { 
			time.search (/^(\d{1,2}):(\d{0,2}).*$/);
			hours = RegExp.$1/1;
			minutes = RegExp.$2/1;
		}
		else {
			time.search (/^(\d{1,2}).*$/);
			hours = RegExp.$1/1;
		}
	}
	if (minutes >= 60) {
		return false;
	}
	if (hours > 24) {
		return false;
	}
	return lpad(hours.toString(),2,'0') + ':' + lpad(minutes.toString(),2,'0');	
}



// ##################################################################################
// Function: lpad
// Description: Left pads input char
//
// Parameters:
//		str: Input string to be left padded
//		len: Number of characters of result string
//		char: Character used to left pad
//
// Created by: Maic Stohr, 2008-04-17
//
// Modified by:
// 1.
// ##################################################################################

function lpad (str, len, char) {
	return pad (str, len, char, 1);
}



// ##################################################################################
// Function: rpad
// Description: Right pads input char
//
// Parameters:
//		str: Input string to be right padded
//		len: Number of characters of result string
//		char: Character used to right pad
//
// Created by: Maic Stohr, 2008-04-17
//
// Modified by:
// 1.
// ##################################################################################

function rpad (str, len, char) {
	return pad (str, len, char, 2);
}



// ##################################################################################
// Function: pad
// Description: Left, right or left and right pads input char
//
// Parameters:
//		str: Input string to be padded
//		len: Number of characters of result string
//		pad: Character used to pad
//		dir: Indicates left = 1, right = 2, both = 3
//
// Created by: Maic Stohr, 2008-04-17
//
// Modified by:
// 1.
// ##################################################################################

function pad (str, len, pad, dir) {
	var STR_PAD_LEFT = 1;
	var STR_PAD_RIGHT = 2;
	var STR_PAD_BOTH = 3;

	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	if (typeof(dir) == "undefined") { var dir = STR_PAD_RIGHT; }

	if (len + 1 >= str.length) {

		switch (dir) {
			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
			break;
			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
			break;
			default:
				str = str + Array(len + 1 - str.length).join(pad);
			break;

		} // switch
	}
	return str;
}

// ##################################################################################
// Functions to validate date
// Description: 
//
// Parameters:
//	dateStr: input date string of format DD.MM.YYYY
//
// Created by: Maic Stohr, 2008-05-16
//
// Modified by:
// 1.
// ##################################################################################

var dtCh = ".";
var dtLang = 'de';
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
        if (dtLang == 'de')
            alert("Bitte verwenden Sie tt.mm.jjjj als Datumsformat!");
        else
            alert("Please use date format dd/mm/jjjj.");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		if (dtLang == 'de') 
            alert("Bitte geben Sie einen gültigen Monat ein!");
        else
            alert("Please enter a valid month!");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (dtLang == 'de') 
            alert("Bitte geben Sie einen gültigen Tag ein!");
        else
            alert("Please enter a valid day!");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        if (dtLang == 'de')
            alert("Bitte geben Sie ein gültiges 4-stelliges Jahr zwischen "+minYear+" und "+maxYear+" ein!");
        else
            alert("Please enter a 4-digit year between "+minYear+" and "+maxYear+"!");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		if (dtLang == 'de')
            alert("Bitte geben Sie ein gültiges Datum ein!");
        else
            alert("Please enter a valid date!");
		return false;
	}
return true;
}