function convertLocalTime(ts,shortform,ifbr){
	var ifbr = (ifbr == null) ? 0 : ifbr;
	var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var d = new Date();
	d.setTime(ts*1000);
	var localts = '';
	if(d.getDate()<10){
		localts += "0";
	}
	localts += d.getDate();
	localts += "/";
	localts += months[d.getMonth()];
	localts += "/";
	localts += d.getFullYear();
	if(shortform!=1){
		if(ifbr==1){
			localts += "<br>";
		}
		localts += " ";
		if(d.getHours()<10){
			localts += "0";
		}
		localts += d.getHours();
		localts += ":";
		if(d.getMinutes()<10){
			localts += "0";
		}
		localts += d.getMinutes();
	}
	return localts;
}
function toggleCalendar(txtObj,targetid){	
	cObj = txtObj.myCalendar;
	if (!cObj) {
		cObj = new CalendarDisplay(txtObj);
		document.body.appendChild(cObj.cDiv);
		txtObj.myCalendar = cObj;
	}
	cObj.toggle(targetid);
}

CalendarDisplay = function(txtObj) {
	this.txtObj = txtObj;
	this.tBox = this.txtObj;
	this.cDiv = document.createElement('div');
	this.cDiv.style.position = 'absolute';
	this.cDiv.style.display = 'none';
}

CalendarDisplay.prototype.MONTHS_CALENDAR = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
CalendarDisplay.prototype.DAYS_2_CALENDAR = new Array("日", "一", "二", "三", "四", "五", "六");

CalendarDisplay.prototype.toggle = function(targetid) {
	if (this.cDiv.style.display == 'none') {
		this.adjustPosition();
		this.fillCalendar(this.grabDate(),targetid);
		this.cDiv.style.display = 'block';
	} else {
		this.cDiv.style.display = 'none';
	}
}

CalendarDisplay.prototype.grabDate = function() {
	var tempDate = new Date(this.tBox.value);
	if (!tempDate.getYear()) {
		tempDate = new Date();
	}
	return tempDate;
}

CalendarDisplay.prototype.fillCalendar = function(theDate,targetid) {
	if (this.cDiv.firstChild) {
		this.cDiv.removeChild(this.cDiv.firstChild);
	}
	this.adjustPosition();
	this.cDiv.appendChild(this.getCalendar(targetid,theDate));
}

CalendarDisplay.prototype.adjustPosition = function() {
	this.cDiv.style.top = this.tBox.offsetHeight + this.findPosY(this.tBox) + 'px';
	this.cDiv.style.left = this.findPosX(this.tBox) + 'px';
}

CalendarDisplay.prototype.getCalendar = function(targetid,theDate) {
	var theYear = theDate.getFullYear();
	var theMonth = theDate.getMonth();
	var theDay = theDate.getDate();

	var theTable = document.createElement('table');
	theTable.id = 'calendartable';
	var theTHead = theTable.createTHead();
	var theTBody = document.createElement('tbody');
	theTable.appendChild(theTBody);
	
	var monthRow = theTHead.insertRow(0);
	var navLeftCell = monthRow.insertCell(0);
	var monthCell = monthRow.insertCell(1);
	var navRightCell = monthRow.insertCell(2);
	monthCell.colSpan = 5;
	monthCell.appendChild(document.createTextNode(this.MONTHS_CALENDAR[theMonth] + ', ' + theYear));
	var leftLink = document.createElement('a');
	leftLink.href = '#';
	this.setCalendarPrevious(targetid,leftLink, this.txtObj, theYear, theMonth, theDay);
	leftLink.appendChild(document.createTextNode('-'));
	navLeftCell.appendChild(leftLink);
	var rightLink = document.createElement('a');
	rightLink.href = '#';
	this.setCalendarNext(targetid,rightLink, this.txtObj, theYear, theMonth, theDay);
	rightLink.appendChild(document.createTextNode('+'));
	navRightCell.appendChild(rightLink);
	
	var weeksRow = theTHead.insertRow(1);
	for (var i=0; i<7; i++) {
		var tempWeeksCell = weeksRow.insertCell(i);
		tempWeeksCell.appendChild(document.createTextNode(this.DAYS_2_CALENDAR[i]));
	}
	
	var temporaryDate1 = new Date(theYear, theMonth, 1);
	var startDayOfWeek = temporaryDate1.getDay();
	var temporaryDate2 = new Date(theYear, theMonth + 1, 0);
	var lastDateOfMonth = temporaryDate2.getDate();
	var dayCount = 1;
		
	for (var r=0; r<6; r++) {
		var tempDaysRow = theTable.tBodies[0].insertRow(r);
		tempDaysRow.className = 'dayrow';
		for (var c=0; c<7; c++) {
			var tempDaysCell = tempDaysRow.insertCell(c);
			var mysteryNode;
			if ((r > 0 || c >= startDayOfWeek) && dayCount <= lastDateOfMonth) {
				tempDaysCell.className = 'yestext';
				var mysteryNode = document.createElement('a');
				mysteryNode.href = '#';
				this.setCalendarClick(targetid,mysteryNode, this.txtObj, theYear, theMonth, dayCount);
				mysteryNode.appendChild(document.createTextNode(dayCount));
				dayCount++;
			} else {
				tempDaysCell.className = 'notext';
				mysteryNode = document.createTextNode('');
			}
			tempDaysCell.appendChild(mysteryNode);
		}
	}
	
	return theTable;
}
CalendarDisplay.prototype.setCalendarClick = function (targetid,node, theObj, theYear, theMonth, theDay) {
	node.onclick = function() {fillInFields(targetid,theObj, theYear, (theMonth + 1), theDay); return false;}
}
CalendarDisplay.prototype.setCalendarPrevious = function (targetid,node, theObj, theYear, theMonth, theDay) {
	node.onclick = function() {showPrevious(targetid,theObj, theYear, theMonth, theDay); return false;}
}
CalendarDisplay.prototype.setCalendarNext = function (targetid,node, theObj, theYear, theMonth, theDay) {
	node.onclick = function() {showNext(targetid,theObj, theYear, theMonth, theDay); return false;}
}
	

// http://www.quirksmode.org/js/findpos.html
CalendarDisplay.prototype.findPosX = function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

// http://www.quirksmode.org/js/findpos.html
CalendarDisplay.prototype.findPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent)	{
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function fillInFields(targetid,obj, year, month, day){
	months = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
	//obj.value = (day < 10 ? '0'+day : day) + ' ' + (months[month-1]) + ' ' + year;
	obj.value = year + '年' + (months[month-1] < 10 ? ' '+months[month-1] : months[month-1]) + '月' + (day < 10 ? ' '+day : day)+'日';
	cObj = obj.myCalendar;
	cObj.toggle(targetid);
	
	var d = Date.UTC(year,months[month-1]-1,day,0,0,0);
	d=(d-8*3600*1000)/1000;
	//document.write(d);
	document.getElementById(targetid).value=d;
	var today = new Date;
	var today_day=today.getDate();
	var today_month=today.getMonth();
	var today_year=today.getFullYear();
	
	var today_timestamp=Date.UTC(today_year,today_month,today_day,0,0,0)/1000;
	if(targetid=='ad_start_time'){
		if(today_timestamp>d){
			alert("請選擇"+today_year+"/"+(today_month+1)+"/"+today_day+"以後的時間");
			document.getElementById("ad_start_time_text").value="";
			document.getElementById("ad_start_time").value="";
			document.getElementById("ad_end_time_btn").disabled=true;
			document.getElementById("ad_end_time").value="";
			document.getElementById("ad_end_time_text").value="";
		}
		else{
			document.getElementById("ad_end_time_btn").disabled=false;
			document.getElementById("ad_end_time").value="";
			document.getElementById("ad_end_time_text").value="";
		}
	}
	else if(targetid=='ad_end_time'){
		var ad_start_timestamp=document.getElementById('ad_start_time').value;
		var ad_end_timestamp=document.getElementById('ad_end_time').value;
		if(parseInt(ad_start_timestamp,10)+86400*2>parseInt(ad_end_timestamp,10)){
			alert("請至少刊登3天");
			document.getElementById("ad_end_time_text").value="";
			document.getElementById("ad_end_time").value="";
		}
		else if(parseInt(ad_start_timestamp,10)+86400*30<parseInt(ad_end_timestamp,10)){
			alert("請至多刊登30天以內");
			document.getElementById("ad_end_time_text").value="";
			document.getElementById("ad_end_time").value="";
		}
	}
}

function showPrevious(targetid,obj, year, month, day)
{
	cObj = obj.myCalendar;
	var lastMonth = new Date(year, month - 1, day)
	cObj.fillCalendar(lastMonth,targetid);
}
function showNext(targetid,obj, year, month, day)
{
	cObj = obj.myCalendar;
	var nextMonth = new Date(year, month + 1, day)
	cObj.fillCalendar(nextMonth,targetid);
}
