var g_months = null;
var g_dayNames = null;

function DatePicker(oDate, format)
{
	this._matrix = [[],[],[],[],[],[],[]];
	this._showNone = false;
	this._showToday = true;
	this._format = format ? format : "mm/dd/yyyy";
	this._firstWeekDay = 6;
	this._redWeekDay = 6;
	this._createdCalandar = false;
	this._dontChangeNone = false;
	this._elementToHide = null;
	this._startObj = null;
	this._endObj = null;
	this._today = "Today";
	this._noneLabel = "None";

	if (typeof(oDate) == "object")
	{
		this._sDate = oDate || new Date();
		this._none = oDate == null;
		
		if (oDate)
		{
			this._populateInput = true;
		}
	}
	else if (typeof(oDate) == "string" && oDate.length > 0)
	{	
		var userDate = parseDate(oDate,this._format);
			
		if (userDate)
		{
			this._sDate = userDate;
			this._populateInput = true;
		}
	}
	else
	{
		this._sDate = new Date;
	}
}

DatePicker.prototype.Enable = function (bVal) 
{
	this._input.disabled = !bVal;
	this._button.disabled = !bVal;
};

DatePicker.prototype.Show = function () 
{

////////////////////////////////////////////////////////////////////////////////
//        document.getElementById('priority').style.visibility = 'hidden';////////
////////////////////////////////////////////////////////////////////////////////

	var dp = this;
	var userDate = parseDate(this._input.value,this._format);

	if (userDate)
	{
		this.setDate(userDate);
	}

	if (!this._createdCalandar)
	{
		this.CreateCalandar();
	}
	
	this._data_div.style.display = "block";
	if (document.onclick)
	{
		document.onclick();
	}

	if (is_ie && this._elementToHide)
	{
		this._elementToHide.style.visibility = "hidden";
	}

	if (this._showToday)
		this._todayButton.focus();

	document.onclick = function(e)
	{ 
		dp.Hide();
	};
};

DatePicker.prototype.Hide = function ()
{

////////////////////////////////////////////////////////////////////////////////
//        document.getElementById('priority').style.visibility = 'visible';////////
////////////////////////////////////////////////////////////////////////////////

	document.onclick = function(e) {};
	this._data_div.style.display = "none";
			
	if (is_ie && this._elementToHide)
	{
		this._elementToHide.style.visibility = "";
	}
};

DatePicker.prototype.create = function (name) 
{
	var dp = this;

	this._document = document;
	this._el = document.createElement("span");
	this._el.style.whiteSpace = "nowrap";

	this._valueMonth = document.createElement("input");
	this._valueMonth.name = name + "Month";
	this._valueMonth.type = "hidden";

	this._valueDay = document.createElement("input");
	this._valueDay.name = name + "Day";
	this._valueDay.type = "hidden";

	this._valueYear = document.createElement("input");
	this._valueYear.name = name + "Year";
	this._valueYear.type = "hidden";

	this._input = document.createElement("input");
/////////////////////////////////////////////////////////////////////////////////////////////////////////
        this._input.type = "hidden";
        
        this._dateText = document.createElement("input");


/////////////////////////////////////////////////////////////////////////////////////////////////////////
	if (this._populateInput)
	{
		this._input.value = formatDate(this.getDate(),this._format);
	}

	this._input.onkeydown = function (e)
	{
		if (dp._createdCalandar && dp._data_div.style.display == "block")
		{
			if (e == null)
			{
				e = document.parentWindow.event;
			}
				
			var kc = e.keyCode != null ? e.keyCode : e.charCode;

			if (kc == 13)
			{	
				dp.Hide();
			}
		}
	};
	
	this._input.onblur = function()
	{
		var pre;
		
		if (dp.getDate())
		{
			pre = dp.getDate().getTime();
		}

		dp.Update();

		if (dp.getDate())
		{
			dp._input.value = formatDate(dp.getDate(),dp._format);

			if ((pre != dp.getDate().getTime()) && typeof dp.onchange == "function")
			{
				dp.onchange();
			}
		}
	};

	this._button = document.createElement("input");
	this._button.type = "button";
	this._button.value = "";
	this._button.className = "dropButton";

	var input_div = document.createElement("div");
	input_div.appendChild(this._valueMonth);
	input_div.appendChild(this._valueDay);
	input_div.appendChild(this._valueYear);
	input_div.appendChild(this._input);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
	input_div.appendChild(this._dateText);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
	input_div.appendChild(this._button);
	this._el.appendChild(input_div);
                  
	this._button.onclick = function (e)
	{
		if (!dp._createdCalandar)
		{
			dp.CreateCalandar();
		}
		
		if (dp._data_div.style.display != "block")
		{
			dp.Show();
		}
		else
		{
			dp.Hide();
		}
		
		if (e) {window.event = e;}else{e = window.event;}
		e.cancelBubble = true;
	};

	this._button.ondblclick = function ()
	{
		if (is_ie)
		{
			this.onclick();
		}
	};

	return this._el;
};

DatePicker.prototype.CreateCalandar = function ()
{
	var dp = this;
	var Node = null;

	this._data_div = document.createElement("div");
	this._data_div.className = "datePicker";
	this._data_div.style.display = "none";
	this._data_div.style.position = "absolute";
	this._data_div.style.zIndex = 1000;

	this._data_div.onclick = function(e)
	{
		if (e) {window.event = e;}else{e = window.event;}
		e.cancelBubble = true;
	};

	var div = document.createElement("div");
	div.className = "header";
	this._data_div.appendChild(div);
	
	var headerTable = document.createElement("table");
	headerTable.className = "headerTable";
	headerTable.cellSpacing = 0;
	div.appendChild(headerTable);
	
	var tBody = document.createElement("tbody");
	headerTable.appendChild(tBody);
	
	var tr = document.createElement("tr");
	tBody.appendChild(tr);
	
	var	td = document.createElement("td");
	Node = document.createElement("input");
	Node.type = "button";
	Node.value = "";
	Node.className = "previousYearButton";
	Node.onclick = function() { dp.goToPreviousYear(); };
	td.appendChild(Node);
	tr.appendChild(td);

	td = document.createElement("td");
	Node = document.createElement("input");
	Node.type = "button";
	Node.value = "";
	Node.className = "previousButton";
	Node.onclick = function() {dp.goToPreviousMonth();};
	td.appendChild(Node);
	tr.appendChild(td);

	td = document.createElement("td");
	td.className = "labelContainer";
	tr.appendChild(td);
	this._topLabel = document.createElement("a");
	this._topLabel.className = "topLabel";
	this._topLabel.href = "#";
	this._topLabel.appendChild(document.createTextNode(String.fromCharCode(160)));
	td.appendChild(this._topLabel);

	this._labelPopup = document.createElement("div");
	this._labelPopup.className = "labelPopup";

	td = document.createElement("td");
	Node = document.createElement("input");
	Node.className = "nextButton";
	Node.type = "button";
	Node.value = "";
	Node.onclick = function () {dp.goToNextMonth();};
	td.appendChild(Node);
	tr.appendChild(td);

	td = document.createElement("td");
	Node = document.createElement("input");
	Node.type = "button";
	Node.value = "";
	Node.className = "nextYearButton";
	Node.onclick = function () {dp.goToNextYear();};
	td.appendChild(Node);
	tr.appendChild(td);
	
	div = document.createElement("div");
	div.className = "grid";
	this._data_div.appendChild(div);
	this._table = div;
	
	div = document.createElement("div");
	div.className = "footer";
	this._data_div.appendChild(div);
	
	var footerTable = document.createElement("table");
	footerTable.className = "footerTable";
	footerTable.cellSpacing = 0;
	div.appendChild(footerTable);
	
	tBody = document.createElement("tbody");
	footerTable.appendChild(tBody);
	
	tr = document.createElement("tr");
	tBody.appendChild(tr);
	
	td = document.createElement("td");
	this._todayButton = document.createElement("input");
	this._todayButton.type = "button";
	this._todayButton.className = "todayButton";
	this._todayButton.value = this._today;
	td.appendChild(this._todayButton);
	tr.appendChild(td);
/*//////////////////////////////////////////////////////////////////////////////////////////////////
	td = document.createElement("td");
	td.className = "qlabelContainer";
	tr.appendChild(td);
	this._todayLabel = document.createElement("a");
	this._todayLabel.className = "qtopLabel";
	this._todayLabel.href = "#";
//	this._todayLabel.appendChild(document.createTextNode(String.fromCharCode(160)));
//	td.appendChild(this._topLabel);                      
*///////////////////////////////////////////////////////////////////////////////////////////////////
/*//////////////////////////////////////////////////////////////////////////////////////////////////
	td = document.createElement("td");
	td.className = "labelContainer";
	tr.appendChild(td);
	this._topLabel = document.createElement("a");
	this._topLabel.className = "topLabel";
	this._topLabel.href = "#";
	this._topLabel.appendChild(document.createTextNode(String.fromCharCode(160)));
	td.appendChild(this._topLabel);
//////////////////////////////////////////////////////////////////////////////////////////////////*/
	
	td = document.createElement("td");
	td.className = "filler";
	td.appendChild(document.createTextNode(String.fromCharCode(160)));
	tr.appendChild(td);
	
	td = document.createElement("td");
	this._noneButton = document.createElement("input");
	this._noneButton.type = "button";
	this._noneButton.className = "noneButton";
	this._noneButton.value = this._noneLabel;
	td.appendChild(this._noneButton);
	tr.appendChild(td);
		
	this._createTable(document);
	
	this._updateTable();
	this._setTopLabel();

	if (!this._showNone)
	{
		this._noneButton.style.visibility = "hidden";
	}
	
	if (!this._showToday)
	{
		this._todayButton.style.visibility = "hidden";
	}

	this._todayButton.onclick = function () 
	{ 
		dp.goToToday();

		if (is_ie && this._elementToHide)
		{
			this._elementToHide.style.visibility = "hidden";
		}

		dp.Hide();

		if (typeof dp.onchange == "function")
		{
			dp.onchange();
		}
	};

	this._noneButton.onclick = function () 
	{
		dp.setDate(null); 
		dp._input.value = "";
		dp.Hide();

		if (typeof dp.onchange == "function")
		{
			dp.onchange();
		}
	};

	this._data_div.onselectstart = function () { return false; };
		
	this._table.onclick = function (e) 
	{
		if (e == null)
		{
			e = document.parentWindow.event;
		}
		
		var el = (e.target != null) ? e.target : e.srcElement;
		
		while (el.nodeType != 1)
		{
			el = el.parentNode;
		}
		
		while (el != null && el.tagName && el.tagName.toLowerCase() != "td")
		{
			el = el.parentNode;
		}
		
		if (el == null || el.tagName == null || el.tagName.toLowerCase() != "td")
		{
			return;
		}
		
		var n = null;
		
		if (el.firstChild)
		{
			n = Number(el.firstChild.data);
		}

		if (isNaN(n) || n <= 0 || n == null)
		{
			return;
		}
		
		var d = new Date(dp._sDate);				
		d.setDate(n);
		dp.setDate(d);

		dp._input.value = formatDate(dp.getDate(),dp._format);
		dp.Hide();
		
		if (typeof dp.onchange == "function")
		{
			dp.onchange();
		}	
	};
	
	this._topLabel.onclick = function (e) 
	{
		dp._showLabelPopup();
		return false;
	};
	
	this._data_div.onkeydown = function (e) 
	{
		if (e == null)
		{ 
			e = document.parentWindow.event;
		}
		
		var kc = (e.keyCode != null) ? e.keyCode : e.charCode;
		var d = new Date(dp._sDate).valueOf();

		switch (kc)
		{
		case 37:
			d -= 86400000;
			break;
			
		case 39:
			d += 86400000;
			break;
			
		case 38:
			d -= 604800000;
			break;
			
		case 40:
			d += 604800000;
			break;
		
		case 13:
			dp._input.value = formatDate(dp.getDate(),dp._format);
			dp.Hide();
			
			if (typeof dp.onchange == "function")
			{
				dp.onchange();
			}	
				
			return false;
		
		case 27:
			dp.Hide();
			return false;

		default:
			return true;
		}

		dp.setDate(new Date(d));
		return false;
	};
	
	this._data_div.onmousewheel = function (e) 
	{
		if (!e)
		{
			e = document.parentWindow.event;
		}

		var n = - e.wheelDelta / 120;
		
		if (n > 0)
		{
			while (n-- > 0)
			{
				dp.goToNextMonth();
			}
		}
		else
		{
			while (n++ < 0)
			{
				dp.goToPreviousMonth();
			}
		}
		
		return false;
	};
	
	this._el.appendChild(this._data_div);
	this._createdCalandar = true;
};

DatePicker.prototype.Update = function (bValidate, sErrMsg)
{
	var userDate = parseDate(this._input.value,this._format);

	if (userDate)
	{
		this._valueMonth.value = userDate.getMonth() + 1;
		this._valueDay.value = userDate.getDate();
		this._valueYear.value = userDate.getFullYear();
		this.setDate(userDate);
		return true;
	}
	
	this._valueMonth.value = "";
	this._valueDay.value = "";
	this._valueYear.value = "";

	if (bValidate)
	{
		this._input.focus();
		this._input.select();
	}

	return false;
};

DatePicker.prototype.setDate = function (oDate)
{
	this._hideLabelPopup();

	if (oDate == null)
	{
		if (!this._none)
		{
			this._none = true;
			this._setTopLabel();
			this._updateTable();
		}

		return;
	}

	if (typeof oDate == "string" || typeof oDate == "number")
	{
		oDate = new Date(oDate);
	}

	if (this._sDate.getDate() != oDate.getDate() ||
		this._sDate.getMonth() != oDate.getMonth() ||
		this._sDate.getFullYear() != oDate.getFullYear() ||
		this._none)
	{
		if (!this._dontChangeNone)
		{
			this._none = false;
		}

		this._sDate = oDate;

		this._setTopLabel();
		this._updateTable();
	}

	if (!this._dontChangeNone)
	{
		this._none = false;
	}
};

DatePicker.prototype.getDate = function ()
{
	if (this._none)
	{
		return null;
	}

	return new Date(this._sDate);
};

DatePicker.prototype._createTable = function (document)
{
	var str, i;
	var rows = 6;
	var cols = 7;
	var currentWeek = 0;

	var table = document.createElement("table");
	table.className = "gridTable";
	table.cellSpacing = 0;

	var tBody = document.createElement("tbody");
	table.appendChild(tBody);

	var tr = document.createElement("tr");
	tr.className = "daysRow";

	var td, tn;
	var nbsp = String.fromCharCode(160);
	for (i = 0; i < cols; i++)
	{
		td = document.createElement("td");
		td.appendChild(document.createTextNode(nbsp));
		tr.appendChild(td);
	}
	tBody.appendChild(tr);

	tr = document.createElement("tr");
	td = document.createElement("td");
	td.className = "upperLine";
	td.colSpan = 7;
	tr.appendChild(td);
	tBody.appendChild(tr);

	for (i = 0; i < rows; i++)
	{
		tr = document.createElement("tr");

		for (var j = 0; j < cols; j++)
		{
			td = document.createElement("td");
			td.appendChild(document.createTextNode(nbsp));
			tr.appendChild(td);
		}

		tBody.appendChild(tr);
	}

	str += "</table>";

	if (this._table != null)
	{
		this._table.appendChild(table);
	}
};

DatePicker.prototype._updateTable = function(bDateOnly)
{
	if (this._table == null)
	{
		return;
	}

	var i;
	var str = "";
	var rows = 6;
	var cols = 7;
	var currentWeek = 0;

	var cells = new Array(rows);
	this._matrix = new Array(rows);

	for (i = 0; i < rows; i++)
	{
		cells[i] = new Array(cols);
		this._matrix[i] = new Array(cols);
	}

	var weekDay = (this._sDate.getDay() + 6) % 7;
	var colIndex = (weekDay - this._firstWeekDay + 7) % 7;

	var tmpDate = new Date(this._sDate.getFullYear(),this._sDate.getMonth(), 1);
	var today = new Date();

	for (i = 1; i < 32; i++)
	{
		tmpDate.setDate(i);
		weekDay = (tmpDate.getDay() + 6) % 7;
		colIndex = (weekDay - this._firstWeekDay + 7) % 7;
		if (tmpDate.getMonth() == this._sDate.getMonth()) {

			var isToday = tmpDate.getDate() == today.getDate() &&
						tmpDate.getMonth() == today.getMonth() &&
						tmpDate.getFullYear() == today.getFullYear();

			cells[currentWeek][colIndex] = { text: "", className: "" };

			if (this._sDate.getDate() == tmpDate.getDate() && !this._none)
			{
				cells[currentWeek][colIndex].className += "selected ";
			}

			if (isToday)
			{
				cells[currentWeek][colIndex].className += "today ";
			}

			if ((tmpDate.getDay() + 6) % 7 == this._redWeekDay)
			{
				cells[currentWeek][colIndex].className += "red";
			}

			cells[currentWeek][colIndex].text =
				this._matrix[currentWeek][colIndex] = tmpDate.getDate();

			if (colIndex == 6)
			{
				currentWeek++;
			}
		}
	}

	var weekDays = new Array(7);

	for (i = 0; i < 7; i++)
	{
		weekDays[i] = g_dayNames[(i+this._firstWeekDay+1)%7].substr(0,1);
	}

	var tds = this._table.firstChild.tBodies[0].rows[0].cells;

	for (i = 0; i < cols; i++)
	{
		tds[i].firstChild.data = weekDays[i];
	}

	var trs = this._table.firstChild.tBodies[0].rows;
	var tmpCell;
	var nbsp = String.fromCharCode(160);
	for (var y = 0; y < rows; y++) {
		for (var x = 0; x < cols; x++) {
			tmpCell = trs[y + 2].cells[x];
			if (typeof cells[y][x] != "undefined") {
				tmpCell.className = cells[y][x].className;
				tmpCell.firstChild.data = cells[y][x].text;
			}
			else {
				tmpCell.className = "";
				tmpCell.firstChild.data = nbsp;
			}
		}
	}
};

DatePicker.prototype._setTopLabel = function ()
{
	var str = this._sDate.getFullYear() + " " + g_months[ this._sDate.getMonth() ];
	if (this._topLabel != null)
	{
		this._topLabel.lastChild.data = str;
	}
};

DatePicker.prototype.goToNextYear = function ()
{
	var d = new Date(this._sDate);
	d.setFullYear(this._sDate.getFullYear() + 1);

	this._dontChangeNone = true;
	this.setDate(d);
	this._dontChangeNone = false;
};

DatePicker.prototype.goToPreviousYear = function ()
{
	var d = new Date(this._sDate);
	d.setFullYear(this._sDate.getFullYear() - 1);

	this._dontChangeNone = true;
	this.setDate(d);
	this._dontChangeNone = false;
};

DatePicker.prototype.goToNextMonth = function ()
{
	var d = new Date(this._sDate);

	var current_date = d.getDate();
	var current_month = d.getMonth();

	d.setMonth(current_month + 1);

	while (d.getDate() != current_date)
	{
		d.setDate(--current_date);
		d.setMonth(current_month + 1);
	}

	this._dontChangeNone = true;
	this.setDate(d);
	this._dontChangeNone = false;
};

DatePicker.prototype.goToPreviousMonth = function () 
{	
	var d = new Date(this._sDate);

	var current_date = d.getDate();
	var current_month = d.getMonth();

	d.setMonth(current_month - 1);

	while (d.getDate() != current_date)
	{
		d.setDate(--current_date);
		d.setMonth(current_month - 1);
	}

	this._dontChangeNone = true;
	this.setDate(d);
	this._dontChangeNone = false;
};

DatePicker.prototype.gotoNextDay = function()
{
	var value = this._sDate.value();
	this._sDate.value = value + 86400000;

	this._dontChangeNone = true;
	this.setDate(this._sDate);
	this._dontChangeNone = false;
};

DatePicker.prototype.gotoPreviousDay = function()
{
	var value = this._sDate.value();

	this._sDate.value = value - 86400000;

	this._dontChangeNone = true;
	this.setDate(this._sDate);
	this._dontChangeNone = false;
};

DatePicker.prototype.goToToday = function () 
{
	if (this._none)
	{
		this._sDate = new Date(this._sDate + 10000000000);
	}

	this._none = false;
	this.setDate(new Date());
	this._input.value = formatDate(this.getDate(),this._format);
};

DatePicker.prototype.setShowToday = function (bShowToday) 
{		
	if (this._todayButton != null)
	{
		this._todayButton.style.visibility = bShowToday ? "visible" : "hidden";
	}
	
	this._showToday = bShowToday;
};

DatePicker.prototype.getShowToday = function () {
	return this._showToday;
};

DatePicker.prototype.setShowNone = function (bShowNone) 
{
	if (this._noneButton != null)
	{
		this._noneButton.style.visibility = bShowNone ? "visible" : "hidden";
	}

	this._showNone = bShowNone;
};

DatePicker.prototype.getShowNone = function () 
{
	return this._showNone;
};

DatePicker.prototype.setFirstWeekDay = function (nFirstWeekDay) 
{
	if (this._firstWeekDay != nFirstWeekDay) 
	{
		this._firstWeekDay = nFirstWeekDay;
		this._updateTable();
	}
};

DatePicker.prototype.getFirstWeekDay = function () 
{
	return this._firstWeekDay;
};

DatePicker.prototype.setRedWeekDay = function (nRedWeekDay)
{
	if (this._redWeekDay != nRedWeekDay) 
	{
		this._redWeekDay = nRedWeekDay;
		this._updateTable();
	}
};

DatePicker.prototype.getRedWeekDay = function () 
{
	return this._redWeekDay;
};

DatePicker.prototype.SetElementToHide = function(e)
{
	this._elementToHide = e;
};

DatePicker.prototype._showLabelPopup = function ()
{
	var dateContext = function (dp, d) 
	{
		return function (e) 
		{
			dp._dontChangeNone = true;
			dp._hideLabelPopup();
			dp.setDate(d);
			dp._dontChangeNone = false;
			return false;
		};
	};
	
	var dp = this;
	
	while (this._labelPopup.hasChildNodes())
	{
		this._labelPopup.removeChild(this._labelPopup.firstChild);
	}

	var a, tmp;
	for (var i = -3; i < 4; i++) 
	{
		tmp = new Date(this._sDate);
		tmp.setDate(1);
		tmp.setMonth(tmp.getMonth() + i);
		
		a = this._document.createElement("a");
		a.href = "javascript:void 0;";
		a.onclick = dateContext(dp, tmp);
		a.appendChild(this._document.createTextNode(tmp.getFullYear() + " " + g_months[ tmp.getMonth() ]));

		if (i == 0)
		{
			a.className = "selected";
		}

		this._labelPopup.appendChild(a);
	}
	
	this._topLabel.parentNode.insertBefore(this._labelPopup, this._topLabel.parentNode.firstChild);
};

DatePicker.prototype._hideLabelPopup = function ()
{
	if (this._labelPopup && this._labelPopup.parentNode)
	{
		this._labelPopup.parentNode.removeChild(this._labelPopup);
	}
};

DatePicker.prototype.SetFormat = function (s) 
{
	this._format = s;
};

DatePicker.prototype.SetFirstWeekDay = function (s) 
{
	this._firstWeekDay = s==0?6:s-1;
};

DatePicker.prototype.SetToday = function (s) 
{
	this._today = s;
};

DatePicker.prototype.SetNone = function (s) 
{
	this._noneLabel = s;
};

DatePicker.prototype.SetMonths = function (s) 
{
	g_months = s;
};

DatePicker.prototype.SetDayNames = function (s) 
{
	g_dayNames = s;
};

function LZ(x) {return(x<0||x>9?"":"0")+x;}

function formatDate(date,format) 
{
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var E=date.getDay();
	var yyyy,yy,mmm,mm,dd;
	
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["Y"]=y;
	value["y"]=y.substring(2,4);
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["m"]=M;
	value["mm"]=LZ(M);
	value["d"]=d;
	value["dd"]=LZ(d);
	value["e"]=g_dayNames[E+7];
	value["A"]=g_dayNames[E];
	value["B"]=g_months[M-1];
	
	while (i_format < format.length)
	{
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length))
		{
			token += format.charAt(i_format++);
		}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
	}
	return result;
}
	
function _isInteger(val) 
{
	var re = /^\d*$/;
	return re.test(val);
}

function _getInt(str,i,minlength,maxlength) 
{
	for (var x=maxlength; x>=minlength; x--)
	{
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
	}
	return null;
}
	
function getDateFromFormat(val,format) 
{
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=1;

	while (i_format < format.length)
	{
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length))
		{
			token += format.charAt(i_format++);
		}
	
		if (token=="yyyy" || token=="yy" || token=="Y" || token=="y") 
		{
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			if (token=="Y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) 
			{
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
			}
		}
		else if (token=="B")
		{
			month=0;
			for (var i=0; i<g_months.length; i++) 
			{
				var month_name=g_months[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase())
				{
					month=i+1;
					if (month>12) { month -= 12; }
					i_val += month_name.length;
					break;
				}
			}
			if ((month < 1)||(month>12)){return 0;}
		}
		else if (token=="A"||token=="e")
		{
			for (i=0; i<g_dayNames.length; i++) 
			{
				var day_name=g_dayNames[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase())
				{
					i_val += day_name.length;
					break;
				}
			}
		}
		else if (token=="mm"||token=="m")
		{
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;
		}
		else if (token=="dd"||token=="d")
		{
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;
		}
		else 
		{
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
		}
	}
	
	if (i_val != val.length) { return 0; }
	
	if (month==2)
	{
		if (((year%4==0)&&(year%100 != 0)) || (year%400==0))
		{
			if (date > 29){ return 0; }
		}
		else { if (date > 28) { return 0; } }
	}
	
	if ((month==4)||(month==6)||(month==9)||(month==11))
	{
		if (date > 30) { return 0; }
	}
	
	var newdate=new Date(year,month-1,date,0,0,0);
	return newdate.getTime();
}

function parseDate(val,perFormat) 
{
	var preferEuro=(arguments.length==3)?arguments[1]:false;
	generalFormats=new Array(perFormat,'y-m-d','B d, y','B d,y','y-B-d','d-B-y','B d');
	monthFirst=new Array('e m/d/y','m/d/y','m-d-y','m.d.y','B-d','m/d','m-d');
	dateFirst =new Array('d/m/y','d-m-y','d.m.y','d-B','d/m','d-m');
	var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst');
	var d=null;
	for (var i=0; i<checkList.length; i++)
	{
		var l=window[checkList[i]];
		for (var j=0; j<l.length; j++)
		{
			d=getDateFromFormat(val,l[j]);
			if (d!=0) { return new Date(d); }
		}
	}
	return null;
}
/*
function TimePick() 
{
	this._is24h = false;
	this._AM = "AM";
	this._PM = "PM";
	this._hrs = "hrs";
	this._startObj = null;
	this._endObj = null;
	this._startDateObj = null;
	this._endDateObj = null;
	this._value = 0;
}

TimePick.prototype.create = function (name) 
{	
	var tp = this;
	var span = document.createElement("span");

	var table = document.createElement("table");
	table.cellSpacing = 0;
	table.cellPadding = 0;
	span.appendChild(table);

	var tBody = document.createElement("tbody");
	table.appendChild(tBody);
		
	var tr = document.createElement("tr");
	tBody.appendChild(tr);

	var td = document.createElement("td");
	this._input = document.createElement("input");
	this._input.type = "text";
	this._input.style.width = "5em";
	
	this._input.onblur = function(e)
	{
		tp.SetValue(this.value);
	};

	td.appendChild(this._input);

	this._valueHour = document.createElement("input");
	this._valueHour.name = name + "Hour";
	this._valueHour.type = "hidden";
	td.appendChild(this._valueHour);

	this._valueMinute = document.createElement("input");
	this._valueMinute.name = name + "Minute";
	this._valueMinute.type = "hidden";
	td.appendChild(this._valueMinute);

	tr.appendChild(td);

	this._button = document.createElement("input");
	this._button.className = "dropButton";
	this._button.type = "button";
	this._button.value = "";
	td.appendChild(this._button);
	tr.appendChild(td);

	tp._button.onclick = function(e) 
	{
		tp.Toggle();

		if (e) {window.event = e;}else{e = window.event;}
		e.cancelBubble = true;
	};

	this._button.ondblclick = function(e)
	{
		if (is_ie)
		{
			try{this.onclick();}catch(e){}
		}
	};

	span.appendChild(this.CreateDiv());
	return span;
};

TimePick.prototype.CreateDiv = function()
{
	var tp = this;
	this._div = document.createElement("div");
	this._div.style.position = "absolute";
	this._div.style.display = "none";
	
	this._itemList = document.createElement("select");
	this._itemList.size = 10;

	this._itemList.onclick = function(e) 
	{
		tp.SetValue(parseInt(this.value));	
	};

	this._itemList.onkeydown = function(e) 
	{
		if (e == null)
		{
			e = document.parentWindow.event;
		}
				
		var kc = e.keyCode != null ? e.keyCode : e.charCode;

		if (kc == 13 || kc == 32)
		{	
			tp.SetValue(parseInt(this.value));
		}	
	};

	this._div.appendChild(this._itemList);
	return this._div;
};

TimePick.prototype.AddItem = function(value,display)
{
	var option = document.createElement("option");
	option.value = value;
	option.appendChild(document.createTextNode(display));
	this._itemList.appendChild(option);
};

TimePick.prototype.Toggle = function()
{
	var tp = this;

	if (this._div.style.display != "block")
	{
		if (this._startObj)
		{
			this.PopulateEnd(true);
		}

		var i = this._itemList.length;
		
		while (i--)
		{
			if (Math.abs(this._itemList.options[i].value - this._value) <= 900)
			{
				this._itemList.selectedIndex = i;
				break;
			}
		}

		this._div.style.display = "block";
		this._itemList.focus();

		if (document.onclick)
		{
			document.onclick();
		}

		document.onclick = function(e)
		{ 
			tp.Hide();
		};

		return;
	}
	
	this.Hide();
};

TimePick.prototype.Hide = function()
{
	document.onclick = function(e) {};
	this._div.style.display = "none";
};

TimePick.prototype.Enable = function (bVal) 
{
	this._input.disabled = !bVal;
	this._button.disabled = !bVal;
};

TimePick.prototype.GetValue = function()
{
	return this._value;
};

TimePick.prototype.SetValue = function(value)
{
	var preValue = this._value;

	if (typeof(value) == "string")
	{
		value = this.ToSeconds(value);
	}
	else if (typeof(value) == "object")
	{
		value = ((value.getHours()*3600)+(value.getMinutes()*60));
	}

	value = value % 86400;
	this._value = value;
	this._valueHour.value = parseInt(value/3600); 
	this._valueMinute.value = parseInt((value%3600)/60);
	this._input.value = this.Format(value);

	this.Hide();
	
	if (typeof this.onchange == "function")
	{
		this.onchange(this._value, preValue);
	}	
};

TimePick.prototype.ChangeBy = function(seconds)
{
	this._value += seconds;
	if (this._value < 0) {this._value += 3600*24;}
	this.SetValue(this._value);
};

TimePick.prototype.Populate = function()
{
	for(i=0; i<24; i++)
	{
		if (this._is24h)
		{
			s="";
			h=LZ(i);
		}
		else
		{	
			x = i % 12 ? i % 12 :12;
			h=LZ(x);
			s = i < 12 ? this._AM : this._PM;	
		}

		this.AddItem(i*3600,h+":00 " + s);
		this.AddItem((i*3600)+1800,h+":30 " + s);
	}
};

TimePick.prototype.PopulateEnd = function()
{
	var bIsSameDay = true;
	this._itemList.options.length = 0;
	
	if (this._startDateObj && this._endDateObj)
	{
		var start = this._startDateObj.getDate(); 
		var end = this._endDateObj.getDate();
		bIsSameDay = ((start.getFullYear()==end.getFullYear())&&(start.getMonth()==end.getMonth())&&(start.getDate()==end.getDate()));
	}

	if (!bIsSameDay)
	{
		this.Populate();
		return;
	}

	s = this._startObj.GetValue();
	hour = parseInt(s/3600);
	min  = parseInt((s%3600)/60);
	
	s = "";
	next = 30 + min;

	for(i=0; i<24; i++)
	{
		x = hour + i;

		if (this._is24h)
		{
			d = x%24;
		}
		else
		{
			d = x%12 ? x%12 : "12";
			s = x % 24 < 12 ? this._AM : this._PM;
		}
		
		m=LZ(min);
		h=LZ(d);

		this.AddItem((x%24*3600)+(min*60),h+":"+m+" "+s+" ("+i+" "+this._hrs+")");
		m = next;

		if (m>59)
		{
			x++;

			if (this._is24h)
			{
				d = x%24;
			}
			else
			{
				d = x%12 ? x%12 : 12;
				s = x%24 < 12  ? this._AM : this._PM;
			}

			m -= 60;
			m=LZ(m);
			h=LZ(d);
		}
		
		this.AddItem((x%24*3600)+(m*60),h +":"+m+" "+s+" ("+i+".5 "+this._hrs+")");
	}

	if (!this.GetValue())
	{
		this.SetValue(this._startObj.GetValue() + 1800);
	}
};

TimePick.prototype.Format = function(s)
{
	hour = parseInt(s/3600);
	min  = parseInt((s%3600)/60);
	s = this._is24h ? "" : hour%24<12?this._AM:this._PM;

	if (!this._is24h)
	{
		hour = hour%12 ? hour%12 : 12;
	}

	return LZ(hour)+":"+LZ(min)+" "+s;
};

TimePick.prototype.ToSeconds = function(s)
{
	s = new String(s);
	i = s.indexOf(":");

	if (i == -1)
	{
		return 0;
	}

	Hour = s.substr(s.charAt(0)=="0"?1:0 ,i);

	if (Hour.length && Hour != ":")
	{
		Hour = parseInt(Hour);
	}
	else
	{
		Hour = 0;
	}

	if (s.charAt(i+1)=="0")
	{
		i++;
	}

	Min = s.substr(i+1,2);
	
	if (Min.length)
	{
		Min = parseInt(Min);
	}
	else
	{
		Min = 0;
	}
	
	s = s.toUpperCase();

	if ((s.indexOf(this._PM) != -1 || s.indexOf("PM") != -1) && Hour < 12 )
	{
		Hour += 12;
	}
	else if ((s.indexOf(this._AM) != -1 || s.indexOf("AM") != -1) && Hour == 12 )
	{
		Hour = 0;
	}

	return (Hour*3600) + (Min*60);
};

TimePick.prototype.Set24h = function(is24h)
{
	this._is24h = is24h;
};

TimePick.prototype.SetAM = function(s)
{
	this._AM = s;
};

TimePick.prototype.SetHrs = function(s)
{
	this._hrs = s;
};

TimePick.prototype.SetPM = function(s)
{
	this._PM = s;
};

TimePick.prototype.SetStartObj = function(o)
{
	this._startObj = o;
};

TimePick.prototype.SetEndObj = function(o)
{
	this._endObj = o;
};

TimePick.prototype.SetStartDateObj = function(o)
{
	this._startDateObj = o;
};

TimePick.prototype.SetEndDateObj = function(o)
{
	this._endDateObj = o;
};                */ 
