function colorToggle(tag, before, after)
{
	tag.className = tag.className.replace(before, after);
}
function hideBgdiv()
{
	document.getElementById('dattime_backgroud').style.display = "none";
	document.getElementById('dateD').style.display = "none";
}
String.prototype.parseDate = function() {
	var pattern;
	pattern = /^(\d{4})[^\d](\d{1,2})[^\d](\d{1,2}) (\d{1,2})[^\d](\d{1,2})[^\d](\d{1,2})$/;
	if(pattern.test(this)) {
		return RegExp.$1 + "/" + RegExp.$2 + "/" + RegExp.$3 + " " + RegExp.$4 + ":" + RegExp.$5 + ":" + RegExp.$6;
	} else {
		return false;
	}
}

var week_text = new Array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat');

function datePicker(thisId)
{
	this.thisId		= thisId;
	this.fromDate	= new Date("1970/1/1");
	this.toDate		= new Date("3000/1/1");
	this.dateTarget	= '';
	this.timeTarget = '';

	this.datePick = function(inp) {
		var temp;
		this.dateTag = document.getElementById(this.dateTarget);
		this.dateInp = inp;
		temp = this.dateInp.value.parseDate();
		if(temp) {
			this.thisDate = new Date(temp);
		} else {
			this.thisDate = new Date();
		}

		this.makeTable();
		return true;
	}

	this.setFromDate = function(inp) {
		var temp;
		temp = inp.parseDate();
		if(temp) {
			this.fromDate = new Date(temp);
		}
	}

	this.setToDate = function(inp) {
		var temp;
		temp = inp.parseDate();
		if(temp) {
			this.toDate = new Date(temp);
		}
	}

	this.makeTable = function() {
		var tempHTML, tempDate, i, tmp, tmp2, limitStart = 0, limitEnd = 99;
		if(this.thisDate * 1 < this.fromDate * 1) {
			this.thisDate = new Date(this.fromDate);
		} else if(this.thisDate * 1 > this.toDate * 1) {
			this.thisDate = new Date(this.toDate);
		}
		if(this.thisDate.getFullYear() == this.fromDate.getFullYear()) {
			if(this.thisDate.getMonth() == this.fromDate.getMonth()) {
				limitStart = this.fromDate.getDate();
			}
		}
		if(this.thisDate.getFullYear() == this.toDate.getFullYear()) {
			if(this.thisDate.getMonth() == this.toDate.getMonth()) {
				limitEnd = this.toDate.getDate();
			}
		}
		tempHTML = "<table class='datepick_title' cellpadding='0' cellspacing='0'><tr><td width=\"60\">&nbsp;</td>";
		tempHTML += "<td class='datepick_title_left datepick_title_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".monthLeft();'>&lt;</td>";
		tempHTML += "<td class='datepick_title_center'>" + (this.thisDate.getMonth() + 1) + " </td>";
		tempHTML += "<td class='datepick_title_center'>" + this.thisDate.getFullYear() + " </td>";
		tempHTML += "<td class='datepick_title_right datepick_title_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".monthRight();'>&gt;</td>";
		tempHTML += "</td><td width=\"60\" align=\"right\"><a href=\"javascript:void(0);\" onclick='hideBgdiv();'><img src=\"themes/default/images/x_to_hide_gray.gif\" /></td></tr></table>";
		
		tempHTML += "<table class='datepick_body' cellpadding='0' cellspacing='0'><tr class='datepick_head'><td class='datepick_sun'>SUN</td><td class='datepick_mon'>MON</td><td class='datepick_tue'>TUE</td><td class='datepick_wed'>WED</td><td class='datepick_thu'>THU</td><td class='datepick_fri'>FRI</td><td class='datepick_sat'>SAT</td></tr>";
		
		tempDate = new Date(this.thisDate);
		tempDate.setDate(1);
		
		tempHTML += "<tr>";
		for(i = 0; i < tempDate.getDay(); i ++) {
			tempHTML += "<td class='datepick_blank'>&nbsp;</td>";
		}
		
		tmp = tempDate.getMonth();
		tmp2 = tempDate.getFullYear();
		i = 27;
		while(tmp == tempDate.getMonth()) {
			tempDate.setDate(i);
			i ++;
		}
		tempDate.setMonth(tmp);
		tempDate.setFullYear(tmp2);

		tmp = i - 1;
		for(i = 1; i < tmp; i ++) {
			tempDate.setDate(i);
			if(i >= limitStart && i <= limitEnd) {
				tempHTML += "<td class='datepick_" + week_text[tempDate.getDay()] + " datepick_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".choice(\"" + tempDate.getFullYear() + "-" + (tempDate.getMonth() + 1) + "-" + tempDate.getDate() + "\");'>" + i + "</td>";
			} else {
				tempHTML += "<td class='datepick_disabled'>" + i + "</td>";
			}
			if(tempDate.getDay() == 6) {
				tempHTML += "</tr><tr>";
			}
		}
		
		for(i = tempDate.getDay() + 1; i < 7; i ++) {
			tempHTML += "<td class='datepick_blank'>&nbsp;</td>";
		}
		
		tempHTML += "</tr></table>";
		this.dateTag.innerHTML = tempHTML;
		
		this.dateTag.style.display = "block";
		return true;
	}
	
	this.yearLeft = function() {
		this.thisDate.setYear(this.thisDate.getYear() - 1);
		this.makeTable();
		return true;
	}
	
	this.yearRight = function() {
		this.thisDate.setYear(this.thisDate.getYear() + 1);
		this.makeTable();
		return true;
	}

	this.monthLeft = function() {
		this.thisDate.setMonth(this.thisDate.getMonth() - 1);
		this.makeTable();
		return true;
	}

	this.monthRight = function() {
		this.thisDate.setMonth(this.thisDate.getMonth() + 1);
		this.makeTable();
		return true;
	}
	
	this.choice = function(val) {
		this.dateInp.value = val;
		this.dateTag.style.display = "none";
		document.getElementById('dattime_backgroud').style.display = "none";
		return true;
	}



	this.timePick = function(inp) {
		if(this.dateInp == null) {
			return false;
		}
		if(this.dateInp.value == "") {
			return false;
		}
		this.timeInp = inp;
		this.timeTag = document.getElementById(this.timeTarget);
		this.pickMeridiem();
		return true;
	}
	
	this.pickMeridiem = function() {
		var tempHTML;
		
		tempHTML = "<table class='timepick' cellpadding='0' cellspacing='0'><tr><td class='timepick_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".setMeridiem(0);'>AM</td><td class='timepick_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".setMeridiem(1);'>PM</td></tr></table>";
		
		this.timeTag.innerHTML = tempHTML;
		this.timeTag.style.display = "block";
		return true;
	}
	
	this.setMeridiem = function(meridiem) {
		this.meridiem = meridiem;
		this.pickHour();
	}
	
	this.pickHour = function() {
		var tempHTML, i;
		
		tempHTML = "<center class='centercolor'>Hour</center><table class='timepick' cellpadding='0' cellspacing='0'><tr>";
		
		tempHTML += "<td class='timepick_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".setHour(0);'>12</td>";
		for(i = 1; i < 12; i ++) {
			if((i % 4) == 0) {
				tempHTML += "</tr><tr>";
			}
			tempHTML += "<td class='timepick_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".setHour(" + i + ");'>" + i + "</td>";
		}
		
		tempHTML += "</tr></table>";
		
		this.timeTag.innerHTML = tempHTML;
		return true;
	}

	this.setHour = function(hour) {
		this.hour = hour;
		this.pickMinute();
	}
	
	this.pickMinute = function() {
		var tempHTML, i;
		
		tempHTML = "<center class='centercolor'>Minute</center><table id='timepickm' class='timepick' cellpadding='0' cellspacing='0'><tr>";
		
		for(i = 0; i < 60; i ++) {
			if(i != 0 && (i % 10) == 0) {
				tempHTML += "</tr><tr>";
			}
			tempHTML += "<td class='timepick_link' onmouseover='colorToggle(this, \"link\", \"hover\");' onmouseout='colorToggle(this, \"hover\", \"link\");' onclick='" + this.thisId + ".setMinute(" + i + ");buysubmit();'>" + i + "</td>";
		}
		
		tempHTML += "</tr></table>";
		
		this.timeTag.innerHTML = tempHTML;
		return true;
	}
	
	this.setMinute = function(minute) {
		this.minute = minute;
		this.returnValue();
		return true;
	}
	
	this.returnValue = function() {
		var temp, tempDate;
		this.timeInp.value = (this.meridiem * 12 + this.hour) + ":" + this.minute + ":00";
		this.timeTag.style.display = "none";
		
		temp = this.dateInp.value + " " + this.timeInp.value;

		tempDate = new Date(temp.parseDate());
		
		if(tempDate * 1 < this.fromDate * 1) {
			this.timeInp.value = this.fromDate.getHours() + ":" + this.fromDate.getMinutes() + ":00";
		} else if(tempDate * 1 > this.toDate * 1) {
			this.timeInp.value = this.toDate.getHours() + ":" + this.toDate.getMinutes() + ":00";
		}
		return true;
	}
}

