// JavaScript Document
var setToCalId=null;
var setToComponentId = null;
var MonthName=["January", "February", "March", "April", "May", "June","July","August", "September", "October", "November", "December"];
var next_day = true;
var start_date = new Date();
var currhour = new Date();
var hour = currhour.getHours();
var end_date = new Date();
var today_date = new Date();
var curr_month = today_date.getMonth();
var curr_year = today_date.getFullYear();
var curr_cal_date = new Date(today_date.getFullYear(), today_date.getMonth());
if (next_day) {
  start_date.setDate(start_date.getDate()+0);
}  
end_date.setDate(start_date.getDate()+60);
var days_count
days_count = GetMonDays(start_date) - start_date.getDate() + 1;
days_count = 0

function sel_date(sday) {
 var elems = document.forms[0].elements;
 var j = 0;
	/*for(var i=0; i<elems.length; i++){
		var curr = elems[i];
		if(setToComponentId != null){
		if(curr.id.indexOf(setToComponentId)!=-1){
			curr.value = sday + '-' + (curr_cal_date.getMonth()+ 1) + '-' + curr_cal_date.getFullYear();
			break;
		}
		}
	}*/ 

  document.getElementById(setToComponentId).value = sday + '-' + (curr_cal_date.getMonth()+ 1) + '-' + curr_cal_date.getFullYear();
  close_cal();
  //document.getElementById(setToComponentId).value = curr.value;// sday + '-' + (curr_cal_date.getMonth()+ 1) + '-' + curr_cal_date.getFullYear();
  document.getElementById(setToCalId).style.display = "none";
  days_count = 0;  
  curr_cal_date = new Date(today_date.getFullYear(), today_date.getMonth()); 
  if (next_day) {
    start_date.setDate(start_date.getDate()+0);
  }  
  end_date.setDate(start_date.getDate()+60);
}

 function incr_month() {
	if(days_count==185){
		alert('Please click On Previous');
	}
	else{
	   if (days_count <185) {
		 curr_cal_date = new Date(curr_cal_date.getFullYear(), curr_cal_date.getMonth() + 1);
		 //days_count = days_count + GetMonDays(curr_cal_date)
		 show_cal(setToComponentId,setToCalId)
	   }
	}
 }
 
 function dec_month() {
   mydate = new Date();
   if ((curr_cal_date.getFullYear() > mydate.getFullYear())) {  
      days_count = days_count - cur_month_day
	  mywdate = new Date();
	  mywdate.setDate(curr_cal_date.getFullYear(), curr_cal_date.getMonth() - 1)
	  days_count = days_count - GetMonDays(mywdate)
	  curr_cal_date = new Date(curr_cal_date.getFullYear(), curr_cal_date.getMonth() - 1);
	   show_cal(setToComponentId,setToCalId)
	}
   else {
     if (curr_cal_date.getMonth() > mydate.getMonth() ) {  
	   days_count = days_count - cur_month_day
	   mywdate = new Date();
	    mywdate.setDate(curr_cal_date.getFullYear(), curr_cal_date.getMonth() - 1)
	    days_count = days_count - GetMonDays(mywdate)
	   if (days_count < 0) {
	    days_count = 0
	  }
	   curr_cal_date = new Date(curr_cal_date.getFullYear(), curr_cal_date.getMonth() - 1);	
	   show_cal(setToComponentId,setToCalId)
	 }
   }
 }
 function close_cal() {
   document.getElementById(setToCalId).style.display = "none";
   while((el=document.getElementsByName('_hvrShm1')).length){el[0].parentNode.removeChild(el[0]);}
   days_count = 0
   curr_cal_date = new Date(today_date.getFullYear(), today_date.getMonth()); 
   if (next_day) {
    start_date.setDate(start_date.getDate()+0);
   }  
   end_date.setDate(start_date.getDate()+60);
 }
var cur_month_day = 0

function show_cal(objctid,calId) {
	if(setToComponentId != null ){
	 document.getElementById(setToCalId).style.display = "none";
	   days_count = 0
	}
	setToCalId = calId;
	setToComponentId = objctid;
	var elems = document.forms[0].elements;
	var j = 0;
	for(var i=0; i<elems.length; i++){
		var curr = elems[i];
		if(setToComponentId != null){
		if(curr.id.indexOf(setToComponentId)!=-1){
			curr.value = "";
		}
		}
	}
  cur_month_day = 0 
  mon_first_date = new Date(curr_cal_date.getFullYear(), curr_cal_date.getMonth());
  vFirstDay = mon_first_date.getDay();
  vDayCount = 0;

  vCalData= "<table id='calendar' width='280' cellspacing='0' cellpadding='0' border='0'>";
  vCalData = vCalData +"<caption><a href='javascript:dec_month();' class='nav'>&nbsp;&laquo;&nbsp;</a>" + MonthName[curr_cal_date.getMonth()]+ " " +curr_cal_date.getFullYear()+  "<a href='javascript:incr_month();' class='nav'>&nbsp;&raquo;&nbsp;<a href='javascript:close_cal();' class='nav' align='right'> &times; </a></caption>";
  vCalData = vCalData +"<tbody><tr><th title='Sunday'>S</th><th title='Monday'>M</th><th title='Tueday'>T</th><th title='Wednesday'>W</th><th title='Thursday'>T</th><th title='Friday'>F</th><th title='Satday'>S</th><tr>"
  for (i=0;i<vFirstDay;i++)	{
	vCalData = vCalData + "<td>&nbsp;</td>"
	vDayCount = vDayCount + 1;
  }
  var _limit = GetMonDays(curr_cal_date);
  for (j=1; j <= _limit; j++) {
    if ((vDayCount%7==0)) {
	  vCalData=vCalData+"</tr>\n<tr>";
	}
	if (start_date.getMonth() ==  curr_cal_date.getMonth()) {
	  if (start_date.getDate() <= j) { 
	    vCalData = vCalData + "<td><a href='javascript:sel_date(" + j+ ")'>" + j +  "</a></td>"
	    days_count++;
		cur_month_day++;
	  }
	  else if (start_date.getDate() == j) {
		 	if (hour < 13) {
			 vCalData = vCalData + "<td><a href='javascript:sel_date(" + j+ ")'>" + j +  "</a></td>"
	         days_count++;
		     cur_month_day++;
 	   
	  	} else {
	      vCalData = vCalData + "<td>" + j +  "</td>"
		   
	    }
	  }
	  else {
	    vCalData = vCalData + "<td>" + j +  "</td>"
	  }
	}
	else {
	  if (days_count < 185) {
	    vCalData = vCalData + "<td><a href='javascript:sel_date(" + j+ ")'>" + j +  "</a></td>"
	    days_count++;
		cur_month_day++;
	  }
	  else {
	    vCalData = vCalData + "<td>" + j +  "</td>"
	  }	
	} //ele
	vDayCount = vDayCount + 1;	
  }
  mmx = vDayCount % 7;
  emp_cell = 7 - mmx;
  if (emp_cell < 7) {  
    for (i=0;i<emp_cell;i++) {
      vCalData = vCalData + "<td>&nbsp;</td>"
	}
  } 
	vCalData = vCalData + "</tr></table>";	
	document.getElementById(setToCalId).innerHTML = vCalData;
	document.getElementById(setToCalId).style.display = "block";
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		var divNode = document.getElementById(setToCalId);
		divNode.insertAdjacentHTML("afterEnd", '<IFRAME frameBorder="10" scrolling="no" id="_hvrShm1" name="_hvrShm" style="position:absolute; z-index:5; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0); " />'); 
		var iframeShim = document.getElementById("_hvrShm");
		iframeShim.style.top = divNode.offsetTop; 
		iframeShim.style.left = divNode.offsetLeft; 
		iframeShim.style.width = divNode.offsetWidth; 
		iframeShim.style.height = divNode.offsetHeight;
	}
}

function GetMonDays(a) {
  var DaysInMonth=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if (IsLeapYear(a))
	{
		DaysInMonth[1]=29;
	}	
	return DaysInMonth[a.getMonth()];
}

function IsLeapYear(a)
{
	if ((a.getFullYear()%4)==0)
	{
		if ((a.getFullYear()%100==0) && (a.getFullYear()%400)!=0){
			return false;
		}
		else{
			return true;
		}
	}
	else{
		return false;
	}
}