// today 
var today = new Date();
var Day = today.getDate();
var Month = today.getMonth();
var Year = today.getFullYear();

var next_month = 0;
var next_year = 0;
var pre_month = 0;
var pre_year = 0;

var sel_day = "";

var close_cal = false;

function resetVals(){
	Day = today.getDate();
	Month = today.getMonth();
	Year = today.getFullYear();	
}

// this function will print out the calender, please DON'T change it.
// vMonth is the month to display
// vYear is the year to display

function show_cal(vMonth, vYear){
	// removing old calender
	document.getElementById('calender_div').innerHTML = "";
	// creating the main table
	var main_table = document.createElement("TABLE");
	main_table.id = "cal_table";
	main_table.className = "cal_table";
	main_table.cellSpacing = "1";
	main_table.cellPadding = "1";
	
	var days_arr = new Array("S","M","T","W","T","F","S");
	var months_arr = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	
	// general variables
	var gtd = "";
	var gtr = "";
	
	var dateD = 0;
	
	// if the month and year is empty, use todays month and year
	if (vMonth.toString() == ""){
		vMonth = today.getMonth();
	}
	if (vYear.toString() == ""){
		vYear = today.getFullYear();
	}
	
	// getting the first day of the month vMonth
	var newday = new Date(vYear, vMonth, 1);
	var FirstDay = newday.getDay();
	
	// setting the number of the days for the month
	var numDays = daysInMonth(vMonth, vYear);
	
	
	var main_tbody = document.createElement("TBODY");
	main_table.appendChild(main_tbody);
	
	// next and previous months
	if (vMonth == 0) {
		pre_month = 12;
		pre_year = vYear - 1;
	}
	else {
		pre_month = vMonth - 1;
		pre_year = vYear;	
	}
	
	if (vMonth == 11) {
		next_month = 0;
		next_year = parseInt(vYear) + 1;
	}
	else {
		next_month = parseInt(vMonth) + 1;
		next_year = vYear;	
	}
	
	var top_row = document.createElement("TR");
	main_tbody.appendChild(top_row);
	
	var next_td = document.createElement("TD");
	if (vMonth == today.getMonth()){
		next_td.className = "nextTD";
		next_td.onclick = nextMonth;
	}
	else {
		next_td.className = "nextTD_blank";	
	}
	next_td.innerHTML = "&nbsp;"
	var month_dis = document.createElement("TD");
	month_dis.className = "monthDis";
	month_dis.colSpan = "5";
	month_dis.innerHTML = months_arr[vMonth] + " " + vYear;
	
	var pre_td = document.createElement("TD");
	if (pre_month == today.getMonth()) {
		pre_td.className = "preTD";
		pre_td.onclick = preMonth;
	}
	else {
		pre_td.className = "preTD_blank";	
	}
	pre_td.innerHTML = "&nbsp;"
	
	top_row.appendChild(pre_td);
	top_row.appendChild(month_dis);
	top_row.appendChild(next_td);
	
	
	var days_row = document.createElement("TR");	
	main_tbody.appendChild(days_row);
	
	for(i=0; i<7; i++){
		gtd = document.createElement("TD");
		gtd.className="day_head";
		gtd.innerHTML = days_arr[i];
		
		days_row.appendChild(gtd);
	}
	
	// print out the days calender
	for (i=0; i<(numDays + FirstDay); i++){
		dateD = i + 1 - FirstDay;
		
		if ((i % 7) == 0){
			gtr = document.createElement("TR");
			main_tbody.appendChild(gtr);
		}
		
		gtd = document.createElement("TD");
		gtd.id = "day_"+dateD+"_"+vMonth+"_"+vYear;
		
		if (i < FirstDay){
			gtd.className = "cal_norm";
			gtd.innerHTML = "&nbsp;";
		}
		else {
			gtd.className = "day_link";
			if ((dateD < today.getDate()) && (vMonth == today.getMonth())){
				gtd.className = "passed_day";
			}
			if (((i % 7) == 0) || ((i % 7) == 6)){
				gtd.className = "passed_day";	
			}
			
			if(gtd.className == "day_link"){
				gtd.onclick = assignDate;	
			}
			gtd.innerHTML = dateD;
		}
		
		gtr.appendChild(gtd);
	}
	
	while((i%7) != 0){
		gtd = document.createElement("TD");
		gtd.className = "cal_norm";
		gtd.innerHTML = "&nbsp;";
		gtr.appendChild(gtd);
		i++;
	}
	
	gtr = document.createElement("TR");
	gtd = document.createElement("TD");
	gtd.colSpan = "7";
	gtd.className = "closeTD";
	gtd.innerHTML = "Close";
	gtd.onclick = closeCal;
	
	gtr.appendChild(gtd);
	main_tbody.appendChild(gtr);
	
	document.getElementById('calender_div').appendChild(main_table);	
	
	highlight_day();
}

function nextMonth(){
	show_cal(next_month, next_year);
}
function preMonth(){
	show_cal(pre_month, pre_year);	
}

function highlight_day(){
	try {
		document.getElementById(sel_day).className = "day_link";
	}
	catch(e){
		var foo = 0;	
	}
	sel_day = "day_"+Day+"_"+Month+"_"+Year;
	try {
		document.getElementById(sel_day).className = "selected_day";
	}
	catch(e){
		var foo = 0;	
	}	
}

function select_day(day, mnth, yr){
	Day = parseInt(day);
	Month = parseInt(mnth);
	Year = parseInt(yr);
	
	var opt = "";
	var sel = "";
	
	highlight_day();
	
	sel = document.getElementById('pd1m');
	opt = sel.childNodes;
	for(i=0; i<opt.length; i++){
		if (parseInt(opt[i].innerHTML) == parseInt(Month)+1){
			sel.selectedIndex = i;
			i=1000;
		}
	}
	
	sel = document.getElementById('pd1d');
	opt = sel.childNodes;
	for(i=0; i<opt.length; i++){
		if (parseInt(opt[i].innerHTML) == parseInt(Day)){
			sel.selectedIndex = i;
			i=1000;
		}
	}
	
	sel = document.getElementById('pd1y');
	opt = sel.childNodes;
	for(i=0; i<opt.length; i++){
		if (parseInt(opt[i].innerHTML) == parseInt(Year)){
			sel.selectedIndex = i;
			i=1000;
		}
	}
	
	closeCal();
}

function assignDate(){
	var dt_arr = this.id.split("_");
	select_day(dt_arr[1], dt_arr[2], dt_arr[3]);
}

function closeCal(){
	document.getElementById('calender_div').style.display = "none";
}
function openCal(){
	document.getElementById('calender_div').style.display = "";
}

function bodyClose(){
	if (close_cal) {
		closeCal();
	}
	close_cal = false;
}

function start_cal(){
	resetVals();
	var today_day = new Array(5,4,3,9,8,7,6);
	var def_adv = Day+today_day[today.getDay()];
	var daymth = daysInMonth(Month, Year);
	if (def_adv > daymth){
		Month++;
		def_adv = def_adv % daymth;
		if (Month > 11){
			Month = 0;
			Year++;
		}
	}
	show_cal(Month, Year);
	
	try
	{
		if (prepop_year_cal > 0 && prepop_month_cal >= 0 && prepop_day_cal >= 0){ 
			select_day(prepop_day_cal, prepop_month_cal, prepop_year_cal); 
		} 
		else { 
			select_day(def_adv, Month, Year); 
		}
	}
	catch(e)
	{
		select_day(def_adv, Month, Year);	
	}
	
}

function daysInMonth(mth, yr){
	if ((mth == 0) || (mth == 2) || (mth == 4) || (mth == 6) || (mth == 7) || (mth == 9) || (mth == 11)){
		return 31;
	}
	else if((mth== 3) || (mth == 5) || (mth == 8) || (mth == 10)){
		return 30;
	}
	else {
		// checking if leap year
		if ((yr % 4) == 0){
			return 29;
		}
		else {
			return 28;
		}
	}
}

function changePay(){
	resetVals();
	var val = document.getElementById('PayPeriod').value;
	var buffer_days = 3;
	var daymth = daysInMonth(Month, Year);
	var show_bool = true;
	
	if (val == "16"){
		if (today.getDate() < 15 - buffer_days){
			Day = 15;
		}
		else if(today.getDate() < daymth - buffer_days + 1){
			Day = 1;
			Month++;
			if (Month > 11){
				Month = 0;
				Year++;
			}	
		}
		else {
			Day = 15;
			Month++;
			if (Month > 11){
				Month = 0;
				Year++;
			}
		}
	}
	
	else if(val == "32"){
		if (today.getDate() < 15 - buffer_days){
			Day = 15;
		}
		else if(today.getDate() < daymth - buffer_days){
			Day = daymth;
		}
		else {
			Day = 15;
			Month++;
			if (Month > 11){
				Month = 0;
				Year++;
			}
		}
	}
	
	else if(val == "64"){
		if (today.getDate() < 5 - buffer_days){
			Day = 5;
		}
		else if(today.getDate() < 20 - buffer_days){
			Day = 20;
		}
		else {
			Day = 5;
			Month++;
			if (Month > 11){
				Month = 0;
				Year++;
			}
		}
	}
	
	else if(val == "128"){
		if (today.getDate() < 7 - buffer_days){
			Day = 7;
		}
		else if(today.getDate() < 22 - buffer_days){
			Day = 22;
		}
		else {
			Day = 7;
			Month++;
			if (Month > 11){
				Month = 0;
				Year++;
			}
		}	
	}
	
	else if(val == "1"){
		if (today.getDate() < 10 - buffer_days){
			Day = 10;
		}
		else if(today.getDate() < 25 - buffer_days){
			Day = 25;
		}
		else {
			Day = 10;
			Month++;
			if (Month > 11){
				Month = 0;
				Year++;
			}
		}	
	}
	else if(val == "8"){
		Day = 1;
		Month++;
		if (Month > 11){
			Month = 0;
			Year++;
		}
	}
	else {
		start_cal();
		show_bool = false;
	}
	
	if (show_bool){
		show_cal(Month, Year);
		select_day(Day, Month, Year);
	}
}

