/*********************************
Mon Calendar make by Programmer on Koh Lanta Krabi Thailand
with javascript & Dom Tecnology.
By Autthaporn Nualwut---admin@lantainfo.com
**********************************/
function createDate(p,dateDefault,idPosition){
	var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var arrMonth=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var strSelect='';
	var new_select=null,new_input=null,option=null;
	var currentDate=new Date();
	currentDate.setDate(currentDate.getDate()+dateDefault);
	Cyear=currentDate.getFullYear();//define current year for add in combo year
	Cdate=currentDate.getDate();
	Cmonth=currentDate.getMonth();
	Syear=currentDate.getFullYear();
	maxday=MonthDays[Cmonth];
	if(Cmonth==1&&(Cyear%400==0||(Cyear%4==0&&Cyear%100!=0))) maxday++;
	strDate=Cdate.toString();
	phpMonth=Cmonth+1;
	strMonth=phpMonth.toString();
	if(strDate.length==1) strDate='0'+strDate;
	if(strMonth.length==1) strMonth='0'+strMonth;
	//if set position make new object by dom : not set position make new object by java
	if(idPosition){
		var inDate=document.getElementById(idPosition);
		//-----new combo date with dom-----
		new_select=document.createElement('select');
		new_select.name=p+'_day';
		new_select.id=p+'_day';
		new_select.style.fontFamily='Verdana, Arial, Helvetica, sans-serif';
		new_select.style.fontSize='11px';
		new_select.onchange=new Function('setdate("'+p+'")');
		for(var i=0; i<maxday; i++){
			option=document.createElement('option');
			option.appendChild(document.createTextNode(i+1));
			option.value=i+1;
			if(Cdate==i+1) option.selected=1;
			new_select.appendChild(option);
		}
		inDate.appendChild(new_select);
		inDate.appendChild(document.createTextNode(' '));
		//-----new combo month with dom-----
		new_select=document.createElement('select');
		new_select.name=p+'_month';
		new_select.id=p+'_month';
		new_select.style.fontFamily='Verdana, Arial, Helvetica, sans-serif';
		new_select.style.fontSize='11px';
		new_select.onchange=new Function('setday("'+p+'")');
		for(var i=0;i<12;i++){
			option=document.createElement('option');
			option.appendChild(document.createTextNode(arrMonth[i]));
			option.value=i+1;
			if(Cmonth==i) option.selected=1;
			new_select.appendChild(option);
		}
		inDate.appendChild(new_select);
		inDate.appendChild(document.createTextNode(' '));
		//-----new combo year with dom-----
		new_select=document.createElement('select');
		new_select.name=p+'_year';
		new_select.id=p+'_year';
		new_select.style.fontFamily='Verdana, Arial, Helvetica, sans-serif';
		new_select.style.fontSize='11px';
		new_select.onchange=new Function('setday("'+p+'")');
		for(var i=-1;i<5;i++){
			option=document.createElement('option');
			option.appendChild(document.createTextNode(Cyear+i));
			option.value=Cyear+i;
			if(Cyear+i==Syear) option.selected=1;
			new_select.appendChild(option);
		}
		inDate.appendChild(new_select);
		//----new text date with dom----
		new_input=document.createElement('input');
		new_input.type='hidden';
		new_input.name=p;
		new_input.id=p;
		new_input.value=Syear+'-'+strMonth+'-'+strDate;
		inDate.appendChild(new_input);
	}else{//not set position
    	//----new combo date with java----
		document.writeln('<select id="' + p + '_day" onchange="setdate(\'' + p + '\')" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px">');
		for(var i=0; i<maxday; i++){
			if(i+1==Cdate) strSelect=' selected'; else strSelect='';
    		document.writeln('<option value="' +eval(i+1)+ '"'+strSelect+'>' + eval(i+1) + '</option>');
		}
		document.writeln('</select>');
    	//----new combo month with java----
		document.writeln('<select id="' + p + '_month" onchange="setday(\'' + p + '\')" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px">');
		for(var i=0;i<12;i++){
			if(i==Cmonth) strSelect=' selected'; else strSelect='';
    		document.writeln('<option value="' +eval(i+1)+ '"'+strSelect+'>' + arrMonth[i] + '</option>');
		}
		document.writeln('</select>');
    	//----new combo year with java----
		document.writeln('<select id="' + p + '_year" onchange="setday(\'' + p + '\')" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px">');
		for(var i=-1;i<5;i++){
			if(Cyear+i==Cyear) strSelect=' selected'; else strSelect='';
    		document.writeln('<option value="' +eval(Cyear+i)+ '"'+strSelect+'>' + eval(Cyear+i) + '</option>');
		}
		document.writeln('</select>');
		//----new text date with java----
		document.writeln('<input type="hidden" id="'+p+'" name="'+p+'" value="'+Cyear+'-'+strMonth+'-'+strDate+'">');
	}//end if set position
}

function setdate(p){
	var d=document;
	var txtdate=d.getElementById(p);
	var strday=d.getElementById(p+"_day"),strmonth=d.getElementById(p+"_month"),stryear=d.getElementById(p+"_year");
	var day=strday.value;
	var month=strmonth.value;
	var year=stryear.value;
	if(day.length==1) day='0'+day;
	if(month.length==1) month='0'+month;
	txtdate.value=year+'-'+month+'-'+day;
}
function adddayList(p){
	var d=document;
	var strday=d.getElementById(p+"_day"),strmonth=d.getElementById(p+"_month"),stryear=d.getElementById(p+"_year");
	var day=strday.value;
	var month=strmonth.value;
	var year=stryear.value;
	var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var option=null;
	//clear day list
	while(strday.childNodes.length>0){
		strday.removeChild(strday.childNodes[0]);
	}
	var maxday=MonthDays[month-1];
	if(month==2&&year%4==0) maxday++;
	if(day>maxday) day=maxday;
	for(var i=0; i<maxday; i++){
		option=document.createElement("option");
		option.appendChild(document.createTextNode(i+1));
		option.value=i+1;
		if(day==i+1) option.selected=1;
		strday.appendChild(option);
	}
}
function setday(p){
	adddayList(p);
	setdate(p);
}
