// ===================================================================
// Author: Radek Parchanski <diwi@seznam.cz>
// 20.01.2008
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. 
// ===================================================================

// jsDate 1.0

// HELP ---
// Days
// d  	Day of the month, 2 digits with leading zeros  	01 to 31
// D  	A textual representation of a day, three letters  	Mon through Sun
// j  	Day of the month without leading zeros  	1 to 31
// l   	A full textual representation of the day of the week  	Sunday through Saturday
// N  	ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)  	1 (for Monday) through 7 (for Sunday)
// S  	English ordinal suffix for the day of the month, 2 characters  	 st, nd, rd or th. Works well with j
// w  	Numeric representation of the day of the week  	0 (for Sunday) through 6 (for Saturday)
// z  	The day of the year (starting from 0)  	0 through 365

// Weeks
// W  	ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)  	Example: 42 (the 42nd week in the year)

// Months
// F  	A full textual representation of a month, such as January or March  	January through December
// m  	Numeric representation of a month, with leading zeros  	01 through 12
// M  	A short textual representation of a month, three letters  	Jan through Dec
// n  	Numeric representation of a month, without leading zeros  	1 through 12
// t  	Number of days in the given month  	28 through 31

/// Years
// L  	Whether it's a leap year  	1 if it is a leap year, 0 otherwise.
// Y  	A full numeric representation of a year, 4 digits  	Examples: 1999 or 2003
// y  	A two digit representation of a year  	Examples: 99 or 03


// date must be array [year, month, day] (month start from 0 to 11)
function jsDate(format, date) {
	var str = '';
	format = format.split("");

	for (var i=0; i<format.length; i++) {
		if (format[i].match(/[a-z]/gi)) str += jsDateConvertChar(format[i], date);
		else str += format[i];
	}
	return str;
}

// return array [year, month, day] or false
function jsParseDate (format, date) {
	var dateArr = new Array();
	format = format.split("");

	for (var i=0; i<format.length; i++) { // step char by char
		if (format[i].match(/[a-z]/gi)) date = jsCreateDate(format[i], date, dateArr);
		else date = date.substr(1); // all non alphanumeric characters are removed
	}

	return (dateArr[0] && dateArr[1] >= 0 && dateArr[2]) ? dateArr : false;
}

function jsCreateDate (dateChar, date, dateArr) {
	var num = '' + date.match(/^[0-9]+/); // find numbers from begining of string, this is later removed from date
	var str = '' + date.match(/^[a-z]+/gi); // find letters from begining of string, this is later removed from date

	if (num != 'null' && (dateChar == 'd' || dateChar == 'j')) {
		date = date.substr(num.length);
		dateArr[2] = parseInt(num);
	}
	else if (num != 'null' && (dateChar == 'm' || dateChar == 'n')) {
		date = date.substr(num.length);
		dateArr[1] = parseInt(num) - 1;
	}
	else if (str != 'null' && (dateChar == 'F' || dateChar == 'M')) {
		for (var i=0; i<monthNames.length; i++)	{
			if (str == monthNames[i] || str == monthNames[i].substring(0, 3)) {
				date = date.substr(str.length);
				dateArr[1] = parseInt(i);
				break;
			}
		}
	}
	else if (num != 'null' && (dateChar == 'y' || dateChar == 'Y')) {
		date = date.substr(num.length);
		dateArr[0] = parseInt(num.length == 2 ? '20' + num : num);
	}
	else { // other chars which doesnt contain day or month or year we just strip
		if (num != 'null') date = date.substr(num.length);
		else if (str != 'null') date = date.substr(str.length);
	}

	return date;
}

function jsDateConvertChar(dateChar, date) { // funkci projde i znak, ktery nepodporuje a vrati jej bez zmeny

	Date.prototype.getDOY = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		return Math.ceil((this - onejan) / 86400000);
	}

	Date.prototype.getWeek = function() {
		var onejan = new Date(this.getFullYear(),0,1);
		return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
	}

	var tmpDate = new Date();
	tmpDate.setFullYear(date[0],date[1],date[2]);

	var monthLength = new Array(1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1); // konce mesice 31 nebo 30
	var leapYear = ( !(date[0]%4) && date[0]%100 || !(date[0]%400) ); // prestupny rok je pokud je delitelny 4, ale neni delitelny 100, ale pokud je delitelny 400 je prestupny take

	var normal_year = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var leap_year = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	// Days
	if (dateChar == 'd') dateChar = (date[2] < 10) ? '0' + date[2] : date[2];
	else if (dateChar == 'j') dateChar = date[2];
	else if (dateChar == 'l') dateChar = dayNames[tmpDate.getDay()];
	else if (dateChar == 'D') dateChar = dayNames[tmpDate.getDay()].substring(0, 3);
	else if (dateChar == 'w') dateChar = tmpDate.getDay();
	else if (dateChar == 'N') dateChar = (tmpDate.getDay() == 0) ? 7 : tmpDate.getDay();
	else if (dateChar == 'z') dateChar = tmpDate.getDOY();
	else if (dateChar == 'S') {
		if (date[2] == 1 || date[2] == 21 || date[2] == 31) dateChar = 'st';
		else if (date[2] == 2 || date[2] == 22) dateChar = 'nd';
		else if (date[2] == 3 || date[2] == 33) dateChar = 'rd';
		else dateChar = 'th';
	}

	// Weeks
	else if (dateChar == 'W') dateChar = tmpDate.getWeek();

	// Months
	else if (dateChar =='m') dateChar = (date[1]+1 < 10) ? '0' + (date[1]+1) : date[1]+1;
	else if (dateChar == 'n') dateChar = date[1]+1;
	else if (dateChar == 'F') dateChar = monthNames[date[1]];
	else if (dateChar == 'M') dateChar = monthNames[date[1]].substring(0, 3);
	else if (dateChar == 't') {
		if (date[1] == 1) dateChar = leapYear ? 29 : 28;
		else dateChar = (monthLength[date[1]]) ? 31 : 30;
	}

	// Years
	else if (dateChar == 'Y') dateChar = date[0];
	else if (dateChar == 'y') dateChar = String(date[0]).substring(2);
	else if (dateChar == 'L') dateChar = leapYear ? 1 : 0;

	return dateChar;
}
