/*First DP start*/
var whatField = "formDate";

function dp(){
	document.getElementById("picker2").style.visibility = "hidden"; // Hide the second datepicker as soon as the first one is selected
	
	numDaysInMonth = numDays(new Date());
	firstDayOfMonth = firstDay(new Date());
	
	document.getElementById("formYear").innerHTML = returnMDY("year");
	document.getElementById("formMonth").selectedIndex = returnMDY("month");
	writeDays(numDaysInMonth, firstDayOfMonth);
	
	positionLayer("picker", "calendarButton", 0, 42);
	
	document.getElementById("picker").style.visibility = "visible";

}

function updateCalendar(my, plusminus){
	
	if (my == "year"){
		current = parseInt(document.getElementById("formYear").innerHTML);
		result = eval(current + plusminus + 1);
		document.getElementById("formYear").innerHTML = result;
	}
	
	updateYear = parseInt(document.getElementById("formYear").innerHTML);
	updateMonth = parseInt(document.getElementById("formMonth").value) - 1;
	
	numDaysInMonth = numDays(new Date(updateYear,updateMonth,1));
	firstDayOfMonth = firstDay(new Date(updateYear,updateMonth,1));
	//alert("Number of Days: " + numDaysInMonth);
	//alert("First Day of Month: " + firstDayOfMonth);
	
		
	writeDays(numDaysInMonth, firstDayOfMonth);

}


function insertDate(formDay) 
{
	month_val = document.getElementById("formMonth").value;
	month_val = convert_month(month_val);
	
	document.getElementById(whatField).value = formDay + "-" + month_val + "-" + document.getElementById("formYear").innerHTML;
	document.getElementById("picker").style.visibility = "hidden";
}

function returnMDY(mdy) {
	var dateToday = new Date();
	var year = dateToday.getFullYear();
	var month = dateToday.getMonth();
	var day = dateToday.getDate();
	
	
	switch(mdy){
		case "month":
		return month;
		break;
		
		case "day":
		return day;
		break;
		
		case "year":
		return year;
		break;
	}
	var dateToday = new Date();
}


function writeDays(numDaysInMonth, firstDayOfMonth) {
	var dayCounter = 1;
	
	for (i=1; i <43; i++){
		document.getElementById("bx"+i).innerHTML = "&nbsp;";
	}
	
	startNum = firstDayOfMonth;
	for (j=1; j<=numDaysInMonth; j++){
		document.getElementById("bx"+startNum).innerHTML = "<div align='center' onClick='insertDate(" + j + ")'>" + j + "</div>";
		startNum++;
	}
}


function firstDay(theDate) {
	var dateToday = theDate;
	var year = dateToday.getFullYear();
	var month = dateToday.getMonth();
	
	dateToday.setDate(1);
	
	return dateToday.getDay() + 1;
	
}

function numDays(theDate) {
	var dateToday = theDate;
	var month = dateToday.getMonth() + 1;
	var numDaysInMonth = 0;
	
	if ((month==4 || month==6 || month==9 || month==11) ) {
		return numDaysInMonth = 30;
	}
	else if (month==2){
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (isleap) {
			return numDaysInMonth = 29;
		}
		else {
			return numDaysInMonth = 28;	
		}
	}
	else {
		return numDaysInMonth = 31;
	}
	
}

/*
positionLayer() moves a layer to any specified point on the screen based on the location of any image.
The layer position can be adjusted up adn down or side to side with top and left.
currentMenu, and startingPoint are the id attributes of a the laeyr being moved and the image used a reference point.
*/
function positionLayer(currentMenu, startingPoint, top, left)
{
	menuLocator = document.images[startingPoint];
	menuStartleft = getRealLeft(menuLocator);
	menuStartTop = getRealTop(menuLocator);

	document.getElementById(currentMenu).style.top = (menuStartTop + top) + "px";
	document.getElementById(currentMenu).style.left = (menuStartleft + left) + "px";
}

//This Function gets a starting left point from which we set the hidden menus
function getRealLeft(el) 
{
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) 
	{
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}




//This Function gets a starting top point from which we set the hidden menus
function getRealTop(el) 
{
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) 
	{
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}
/*First DP end*/


/*Second DP start*/
var whatField2 = "formDate2";

function dp2()
{	
	
	document.getElementById("picker").style.visibility = "hidden"; // Hide the first datepicker as soon as the first one is selected
	
	numDaysInMonth2 = numDays2(new Date());

	firstDayOfMonth2 = firstDay2(new Date());

	document.getElementById("formYear2").innerHTML = returnMDY2("year");
	document.getElementById("formMonth2").selectedIndex = returnMDY2("month");
	
	writeDays2(numDaysInMonth2, firstDayOfMonth2);
	positionLayer2("picker2", "calendarButton2", 0, 42);
	document.getElementById("picker2").style.visibility = "visible";
}

function updateCalendar2(my2, plusminus2)
{
	
	if (my2 == "year")
	{
		current2 = parseInt(document.getElementById("formYear2").innerHTML);
		result2 = eval(current2 + plusminus2 + 1);
		document.getElementById("formYear2").innerHTML = result2;
	}
	
	updateYear2 = parseInt(document.getElementById("formYear2").innerHTML);
	updateMonth2 = parseInt(document.getElementById("formMonth2").value) - 1;
	
	numDaysInMonth2 = numDays2(new Date(updateYear2,updateMonth2,1));
	firstDayOfMonth2 = firstDay2(new Date(updateYear2,updateMonth2,1));
		
	writeDays2(numDaysInMonth2, firstDayOfMonth2);
}

function insertDate2(formDay)
{	
	month_val = document.getElementById("formMonth2").value;
	month_val = convert_month(month_val);
	
	document.getElementById(whatField2).value = formDay + "-" + month_val + "-" + document.getElementById("formYear2").innerHTML;
	document.getElementById("picker2").style.visibility = "hidden";	
}

function returnMDY2(mdy) 
{
	var dateToday = new Date();
	var year = dateToday.getFullYear();
	var month = dateToday.getMonth();
	var day = dateToday.getDate();
	
	
	switch(mdy){
		case "month":
		return month;
		break;
		
		case "day":
		return day;
		break;
		
		case "year":
		return year;
		break;
	}
	var dateToday = new Date();
}

function writeDays2(numDaysInMonth, firstDayOfMonth) 
{
	var dayCounter2 = 1;
	
	for (i=1; i <43; i++)
	{
		document.getElementById("bxx"+i).innerHTML = "&nbsp;";
	}
	
	startNum = firstDayOfMonth;
	for (j=1; j<=numDaysInMonth; j++)
	{
		document.getElementById("bxx"+startNum).innerHTML = "<div align='center' onClick='insertDate2(" + j + ")'>" + j + "</div>";
		startNum++;
	}
}

function firstDay2(theDate)
{
	var dateToday = theDate;
	var year = dateToday.getFullYear();
	var month = dateToday.getMonth();
	
	dateToday.setDate(1);
	
	return dateToday.getDay() + 1;
	
}

function numDays2(theDate) 
{
	var dateToday = theDate;
	var month = dateToday.getMonth() + 1;
	var numDaysInMonth = 0;
	
	if ((month==4 || month==6 || month==9 || month==11) ) 
	{
		return numDaysInMonth = 30;
	}
	else if (month==2)
	{
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (isleap) 
		{
			return numDaysInMonth = 29;
		}
		else 
		{
			return numDaysInMonth = 28;	
		}
	}
	else
	{
		return numDaysInMonth = 31;
	}
	
}

/*
positionLayer() moves a layer to any specified point on the screen based on the location of any image.
The layer position can be adjusted up adn down or side to side with top and left.
currentMenu, and startingPoint are the id attributes of a the laeyr being moved and the image used a reference point.
*/

function positionLayer2(currentMenu, startingPoint, top, left)
{
	menuLocator = document.images[startingPoint];
	menuStartleft = getRealLeft2(menuLocator);
	menuStartTop = getRealTop2(menuLocator);

	document.getElementById(currentMenu).style.top = (menuStartTop + top) + "px";
	document.getElementById(currentMenu).style.left = (menuStartleft + left) + "px";
}

//This Function gets a starting left point from which we set the hidden menus

function getRealLeft2(el) 
{
    xPos = el.offsetLeft;
    tempEl = el.offsetParent;
    while (tempEl != null) 
	{
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

//This Function gets a starting top point from which we set the hidden menus

function getRealTop2(el) 
{
    yPos = el.offsetTop;
    tempEl = el.offsetParent;
    while (tempEl != null) 
	{
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}
/*Second DP end*/



function convert_month(vali)
{
	ret_mon = "";
	if (vali == 1)
	{
		ret_mon = "Jan";
	}
	else if (vali == 2)
	{
		ret_mon = "Feb";
	}
	else if (vali == 3)
	{
		ret_mon = "Mar";
	}
	else if (vali == 4)
	{
		ret_mon = "Apr";
	}
	else if (vali == 5)
	{
		ret_mon = "May";
	}
	else if (vali == 6)
	{
		ret_mon = "Jun";
	}
	else if (vali == 7)
	{
		ret_mon = "Jul";
	}
	else if (vali == 8)
	{
		ret_mon = "Aug";
	}
	else if (vali == 9)
	{
		ret_mon = "Sep";
	}
	else if (vali == 10)
	{
		ret_mon = "Oct";
	}
	else if (vali == 11)
	{
		ret_mon = "Nov";
	}
	else if (vali == 12)
	{
		ret_mon = "Dec";
	}
	
	return ret_mon;
}
