/*
$Id: AutoComplete.js 6565 2007-03-28 10:16:53Z mtakag $
*/
/* Initialization */
var dayArrayShort = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
var monthArrayLong = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
function calender() {
  var myDate=new Date();
  myDate.setFullYear(myDate.getFullYear());
  return myDate.getFullYear();
}
/*
   from cal.js: Calendar Date Picker 
 */
var currentDate = new Date();
var currDate = parseInt(currentDate.getDate());
var currMonth = parseInt(currentDate.getMonth());
var currYear = parseInt(currentDate.getFullYear());
// minimum date = today
var minDate = parseInt(currentDate.getDate());
var minMonth = parseInt(currentDate.getMonth());
var minYear = parseInt(currentDate.getFullYear())-2;
// maximum date = today in one year
var maxDate = parseInt(currentDate.getDate());
var maxMonth = parseInt(currentDate.getMonth());
var maxYear = parseInt(currentDate.getFullYear())+1;
// flags to enable date limitation from min date to max date
var preventPreviousDate =true;
var preventFutureDate =true;
function setDates(stat){
if(stat=="1"){
preventPreviousDate =false;
preventFutureDate =false;
}
else{
preventPreviousDate =true;
preventFutureDate =true;

}
}


var scToday = false;
var scFutureDate = false;
var scPastDate = false;
var currentfield;
// also weekend days may be selected
var allowWeekend = true;
var datePickerDivID = "datepicker";
var iFrameDivID = "datepickeriframe";
var defaultDateSeparator = "/";		// common values would be "/" or "."
var defaultDateFormat = "dmy"	// valid values are "mdy", "dmy", and "ymd"
var dateSeparator = defaultDateSeparator;
var dateFormat = defaultDateFormat;
/********************* STATIC HTML TO BE WRITTEN ********************************/
// the calendar will be drawn as a table
// you can customize the table elements with a global CSS style sheet,
// or by hardcoding style and formatting elements below
var crlf = "\r\n";
var TABLE = "<div class='calendarContent'><div class='calTop'></div><div class='calMidd'><div class='calMidVer'><center><table cols=7 cellpadding=0 cellspacing=0 class='dpTable'>" + crlf;
var xTABLE = "</table></center></div></div><div class='calBot'></div><img class='closeBut' src='/assets/images/cal-close-button.png' alt='Close' title='Close' onclick=\"hideDatePicker();\"></div>" + crlf;
var TR = "<tr class='dpTR'>";
var TR_title = "<tr class='dpTitleTR'>";
var TR_days = "<tr class='dpDayTR'>";
var TR_todaybutton = "<tr class='dpTodayButtonTR'>";
var xTR = "</tr>" + crlf;
var TD = "<td class='dpTD'";	// leave this tag open, because we'll be adding an onClick event
var TD_title = "<td colspan=5 class='dpTitleText'>";
var TD_buttons = "<td class='button'>";
var TD_todaybutton = "<td colspan=7 class='dpTodayButtonTD'>";
var TD_days = "<td class='dpDayTD'>";
var TD_selected = "<td class='dpDayHighlightTD'";	// leave this tag open, because we'll be adding an onClick event
var xTD = "</td>" + crlf;
var DIV_title = "<div class='dpTitleText'>";
var DIV_selected = "<div class='dpDayHighlight'>";
var xDIV = "</div>";
var TD_notavail = "<td class='dpTD_notavail'>&nbsp;";
var TD_holiday = "<td class='dpHoliday'>";
var TD_EnHoliday = "<td class='dpTD_EnHoliday'";
var TD_EnHoliday_today = "<td class='dpTD_EnHoliday_today'";
var TD_closecal = "<td class='RighImage'>";
var TD_Today = "<td class='dpTD_today' ";
var TD_DisToday = "<td class='dpTD_disToday'>";
var dateDropDown;
var monthDropDown;
var yearDropDown;
/********************* STATIC HTML TO BE END ********************************/
function displayDatePicker(dateFieldName, dateDD, monthDD, yearDD) {	

	setDates(yearDD);
  BorderChange();
  monthDropDown =document.getElementById(dateDD);

	var minDt,maxDt;
	var targetDateField = document.getElementById(dateFieldName);
	if(targetDateField.stDate != "") {
		minDt = targetDateField.stDate
	}
	if(targetDateField.enDate != "") {
		maxDt = targetDateField.enDate
	}
	if(minDt)	{
		var arrMinDt= splitDateString(minDt);
		minDate = arrMinDt[0];
		minMonth= parseInt(arrMinDt[1] - 1);
		minYear = arrMinDt[2];		
		preventPreviousDate = true;
	}
	
	if(maxDt)	{		
		var arrMaxDt =splitDateString(maxDt);
		maxDate = arrMaxDt[0];
		maxMonth= parseInt(arrMaxDt[1] - 1);
		maxYear = arrMaxDt[2];
		preventFutureDate = true;
	}
  
	currentfield = dateFieldName;
	

  var parent = monthDropDown;
 var x = parent.offsetLeft;
 var y = parent.offsetTop + parent.offsetHeight;
 while (parent.offsetParent) {
    parent = parent.offsetParent;
    x += parent.offsetLeft;
    y += parent.offsetTop;
  }
	
  drawDatePicker(targetDateField, x, y);
} 
function drawDatePicker(targetDateField, x, y) { 
  var dt = new Date();
  if (!document.getElementById(datePickerDivID)) {
    // don't use innerHTML to update the body, because it can cause global variables
    // that are currently pointing to objects on the page to have bad references
    var newNode = document.createElement("div");
    newNode.setAttribute("id", datePickerDivID);
    newNode.setAttribute("class", "dpDiv");
    newNode.setAttribute("style", "visibility: hidden; width: 1px; height: 1px;");
    document.body.appendChild(newNode);
  }
  // move the datepicker div to the proper x,y coordinate and toggle the visiblity
  var pickerDiv = document.getElementById(datePickerDivID);
  pickerDiv.style.position = "absolute";
  pickerDiv.style.left = x + "px";
  pickerDiv.style.top = y + "px";
  pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
  pickerDiv.style.zIndex = 10000;
  if(preventPreviousDate == true) {
    checkPastDate(currDate,currMonth,currYear);
  }
  if(preventFutureDate == true) {
	 
    checkFutureDate(currDate,currMonth,currYear);	 
  }
//scPastDate = false;
  if(scPastDate == true) {
		refreshDatePicker(currentfield, minDate, minMonth,minYear);
  } else if(scFutureDate == true) {
	  if(preventPreviousDate == true)
		  refreshDatePicker(currentfield, minDate, minMonth,minYear);
	  else
  		refreshDatePicker(currentfield, maxDate, maxMonth,maxYear);
  } else {
		refreshDatePicker(currentfield, currDate, currMonth,currYear);
  }
   
} 
function refreshDatePicker(dateFieldName,cDate, cMonth,cYear) {
	 var thisDay = new Date();
	tempthisDay=thisDay;
  if ((cMonth >= 0) && (cYear > 0)) {
    thisDay = new Date(cYear, cMonth, 1);
	  var day = parseInt(cDate);
  } 
 
  // start generating the code for the calendar table
  var html = TABLE;
  // ----------- Commented  ------------------
  // html += topCloseButton; //row for close button on top right corner added
  // -----------------------------------------
  // this is the title bar, which displays the month and the buttons to
  // go back to a previous month or forward to the next month
  html += TR_title;
  if((preventPreviousDate == true) && (thisDay.getMonth() == minMonth) && (thisDay.getFullYear() == minYear)) {
  	html += TD_buttons + "" + xTD;
  } else {
  	html += TD_buttons + getButtonCode(dateFieldName, thisDay, -1, "/assets/images/cal-prev.gif") + xTD;
  } 
  html += TD_title + monthArrayLong[thisDay.getMonth()] + " " + thisDay.getFullYear() + xTD;
  if((preventFutureDate == true) && (thisDay.getMonth() == maxMonth) && (thisDay.getFullYear() == maxYear)) {
  	html += TD_buttons + xTD;
  } else {
    // ----------- Modified -------------------
  	html += TD_closecal + getButtonCode(dateFieldName, thisDay, 1, "/assets/images/cal-next.gif") + xTD;
    // --------------------------------------------------  
  }
  html += xTR;
 // this is the row that indicates which day of the week we're on
  html += TR_days;
  for(i = 0; i < dayArrayShort.length; i++)
  html += TD_days + dayArrayShort[i] + xTD;
  html += xTR;
  // now we'll start populating the table with days of the month
  html += TR;
  // first, the leading blanks
  for (i = 0; i < thisDay.getDay(); i++)
    html += TD_notavail + "" + xTD; 	
	var daySeq;
  // now, the days of the month
  do {
		dayNum = thisDay.getDate();
		dayNum = parseInt(dayNum);
		cMonth = parseInt(cMonth);
		cYear = parseInt(cYear);	
		//For saturday Sunday marking START
		daySeq = thisDay.getDay();
		//For saturday Sunday marking END
		checkPastDate(dayNum,cMonth,cYear);		
		checkToday(dayNum,cMonth,cYear);
		checkFutureDate(dayNum,cMonth,cYear)
		
		TD_onclick = " onclick=\"updateDateField('" + dateFieldName + "', '" + getDateString(thisDay) + "');\">";
		
    if((preventPreviousDate == true) && (preventFutureDate == true)) {
		if((scPastDate == true) && (scToday == true)) {	
				if(daySeq == 0 || daySeq == 6) {			
					html += TD_holiday + dayNum + xTD;
				} else {
				  html += TD_DisToday + dayNum + xTD;
				}
			} else if((scPastDate == true) && (scToday == false))	{
				
				
				if(daySeq == 0 || daySeq == 6) {
					if(allowWeekend)
						html += TD_EnHoliday + TD_onclick + dayNum + xTD;
					else
						html += TD_holiday + dayNum + xTD;
				}	else
					html += TD + TD_onclick + dayNum + xTD;
			} else if((scPastDate == false) && (scToday == true))	{
				 if(daySeq == 0 || daySeq == 6) {
					if(allowWeekend)
						html += TD_EnHoliday_today + TD_onclick + dayNum + xTD;
					else
						html += TD_holiday + dayNum + xTD;
				 } else
					html += TD_Today + TD_onclick + dayNum + xTD;
			}	else if((scFutureDate == true) && (scToday == true)) {			
				if(daySeq == 0 || daySeq == 6) {			
					html += TD_holiday + dayNum + xTD;
				} else {
				  html += TD_DisToday + dayNum + xTD;
				}
			} else if((scFutureDate == true) && (scToday == false))	{			
				if(daySeq == 0 || daySeq == 6) {			
					html += TD_holiday + dayNum + xTD;
				}	else {
				  html += TD_notavail + dayNum + xTD;
				}
			}	else if((scFutureDate == false) && (scToday == true))	{		 
				if(daySeq == 0 || daySeq == 6) {
					if(allowWeekend)
						html += TD_EnHoliday + TD_onclick + dayNum + xTD;
					else
						html += TD_holiday + dayNum + xTD;
				 } else
					html += TD_Today + TD_onclick + dayNum + xTD;
			}	else {
				if(daySeq == 0 || daySeq == 6) {			
					html += TD_holiday + dayNum + xTD;
				}	else{
				  html += TD_notavail + dayNum + xTD;
				}
				
			}	
		} else if((preventPreviousDate == true) && (preventFutureDate == false)) {	
			
		if((scPastDate == true) && (scToday == true))	{	
				if(daySeq == 0 || daySeq == 6) {			
          html += TD_holiday + dayNum + xTD;
        }	else {
          html += TD_DisToday + dayNum + xTD;
        }
      }	else if((scPastDate == true) && (scToday == false))	{	
        if(daySeq == 0 || daySeq == 6) {			
          html += TD_holiday + dayNum + xTD;
        }	else {
          html += TD_notavail + dayNum + xTD;
        }
      } else if((scPastDate == false) && (scToday == true))	{				
         if(daySeq == 0 || daySeq == 6) {
          if(allowWeekend)
            html += TD_EnHoliday + TD_onclick + dayNum + xTD;
          else
            html += TD_holiday + dayNum + xTD;
         } else
          html += TD_Today + TD_onclick + dayNum + xTD;
      }	else {
        if(daySeq == 0 || daySeq == 6) {
          if(allowWeekend)
            html += TD_EnHoliday + TD_onclick + dayNum + xTD;
          else
            html += TD_holiday + dayNum + xTD;
         }
         else
          html += TD + TD_onclick + dayNum + xTD;
      } 	
		} else if((preventPreviousDate == false) && (preventFutureDate == true)) {	
			
			if((scFutureDate == true) && (scToday == true))	{			
				if(daySeq == 0 || daySeq == 6) {			
					html += TD_holiday + dayNum + xTD;
				}	else{
					html += TD_DisToday + dayNum + xTD;
				}
			} else if((scFutureDate == true) && (scToday == false))	{			
				if(daySeq == 0 || daySeq == 6) {			
					html += TD_holiday + dayNum + xTD;
				}	else {
					html += TD_notavail + dayNum + xTD;
				}
			}	else if((scFutureDate == false) && (scToday == true))	{		 
				if(daySeq == 0 || daySeq == 6) {
					if(allowWeekend)
					  html += TD_EnHoliday + TD_onclick + dayNum + xTD;
					else
					  html += TD_holiday + dayNum + xTD;
				} else
					html += TD_Today + TD_onclick + dayNum + xTD;
			} else {
				if(daySeq == 0 || daySeq == 6) {
					if(allowWeekend)
						html += TD_EnHoliday + TD_onclick + dayNum + xTD;
					else
						html += TD_holiday + dayNum + xTD;
					} else
					  html += TD + TD_onclick + dayNum + xTD;
			}	
		}	else	{	
			if(scToday == true)	{
					html += TD_Today + TD_onclick + dayNum + xTD;
			}	else {
					html += TD + TD_onclick + dayNum + xTD;
			}
		}
		
		// if this is a Saturday, start a new row
		if (thisDay.getDay() == 6)
		  html += xTR + TR;
    
		// increment the day
		thisDay.setDate(thisDay.getDate() + 1);
	
  } while (thisDay.getDate() > 1)
  // fill in any trailing blanks
  if (thisDay.getDay() > 0) {
    for (i = 6; i > thisDay.getDay(); i--)
      html += TD_notavail + "" + xTD;	 
  }
  html += xTR;
 
  // and finally, close the table
  html += xTABLE;
  document.getElementById(datePickerDivID).innerHTML = html;
 
} 

function getButtonCode(dateFieldName, dateVal, adjust, imageSrc) {
  var newMonth = (dateVal.getMonth() + adjust) % 12;
  var newYear = dateVal.getFullYear() + parseInt((dateVal.getMonth() + adjust) / 12);
  if (newMonth < 0) {
    newMonth += 12;
    newYear += -1;
  }
  return "<input type='image' src="+ imageSrc + " onClick='refreshDatePicker(\"" + dateFieldName + "\",1 , " + newMonth + ", " + newYear + ");BorderChange();'></button>";
} // getButtonCode
function getDateString(dateVal) {
  var dayString = "00" + dateVal.getDate();
  var monthString = "00" + (dateVal.getMonth()+1);
  dayString = dayString.substring(dayString.length - 2);
  monthString = monthString.substring(monthString.length - 2);
  // MTI: return numeric values, as they are going to be splitted and used as selected index value for the day/month/year pulldowns
  return dateVal.getDate()+dateSeparator+(dateVal.getMonth()+1)+dateSeparator+dateVal.getFullYear();
 } // getDateString
 
function splitDateString(dateString) {
	  var dArray;
  if (dateString.indexOf("/") >= 0)
    dArray = dateString.split("/");
  else if (dateString.indexOf(".") >= 0)
    dArray = dateString.split(".");
  else if (dateString.indexOf("-") >= 0)
    dArray = dateString.split("-");
  else if (dateString.indexOf("\\") >= 0)
    dArray = dateString.split("\\");
  else
    dArray = false;
  return dArray;
  
} // splitDateString
function hideDatePicker() {		
	$("#datepicker").css("visibility","hidden");
} // hideDatePicker
function checkToday(chDate,chMonth,chYear) {
	if((chDate == currDate) && (chMonth == currMonth) && (chYear == currYear)) {
		scToday =true;		
	}	else {
		scToday =false;
	}
	return scToday;
} // checkToday
function checkFutureDate(chDate,chMonth,chYear) {	
	scFutureDate = false;
	var fdate,fmonth,fyear;
	//alert(maxDate + "  "+maxMonth + "  "+maxYear );
	if(chDate < maxDate)
		fdate = -1;
	else if(chDate == maxDate)
		fdate = 0;
	else
		fdate = 1;
	if(chMonth < maxMonth)
		fmonth = -1;
	else if(chMonth == maxMonth)
		fmonth = 0;
	else
		fmonth = 1;
	if(chYear < maxYear)
		fyear = -1;
	else if(chYear == maxYear)
		fyear = 0;
	else
		fyear = 1;
	var cmpString = fdate.toString() + fmonth.toString() + fyear.toString();
	switch (cmpString)	{
		case "-1-11":
		  scFutureDate = true;
		  break
		case "-101":
		  scFutureDate = true;
		  break
		case "-110":
		  scFutureDate = true;
		  break
		case "-111":
		  scFutureDate = true;
		  break
		case "0-11":
		  scFutureDate = true;
		  break
		case "001":
		  scFutureDate = true;
		  break
		case "010":
		  scFutureDate = true;
		  break
		case "011":
		  scFutureDate = true;
		  break
		case "1-11":
		  scFutureDate = true;
		  break
		case "100":
		  scFutureDate = true;
		  break
		case "101":
		  scFutureDate = true;
		  break
		case "110":
		  scFutureDate = true;
		  break
		case "111":
	 	  scFutureDate = true;
		  break
		default:
		  scFutureDate = false;
	}	
	return scFutureDate;
} // checkFutureDate
function checkPastDate(chDate,chMonth,chYear) {	
	minDate = parseInt(minDate);
	minMonth = parseInt(minMonth);
	minYear1 = parseInt(minYear)+2;
	
// alert(minDate + minMonth + minYear );
// alert(minDate + "  "+minMonth + "  "+minYear );
	scPastDate = false;
	var pdate = -2;
	var pmonth = -2;
	var pyear = -2;
	if(chDate < minDate)
		pdate = -1;
	else if(chDate == minDate)
		pdate = 0;
	else
		pdate = 1;
	if(chMonth < minMonth)
		pmonth = -1;
	else if(chMonth == minMonth)
		pmonth = 0;
	else
		pmonth = 1;
	if(chYear < minYear1)
		pyear = -1;
	else if(chYear == minYear1)
		pyear = 0;
	else
		pyear = 1;
	var cmpString = pdate.toString() + pmonth.toString() + pyear.toString();	
	
	switch (cmpString) {
		case "-1-1-1":
  		scPastDate = true;
  		break
		case "-1-10":
  		scPastDate = true;
  		break
		case "-10-1":
  		scPastDate = true;
  		break
		case "-100":
  		scPastDate = true;
  		break
		case "-11-1":
  		scPastDate = true;
  		break
		case "0-1-1":
  		scPastDate = true;
  		break
		case "0-10":
  		scPastDate = true;
  		break
		case "00-1":
  		scPastDate = true;
  		break
		case "01-1":
  		scPastDate = true;
  		break
		case "1-1-1":
  		scPastDate = true;
  		break
		case "1-10":
  		scPastDate = true;
  		break
		case "10-1":
  		scPastDate = true;
  		break
		case "11-1":
  		scPastDate = true;
  		break
		default:
  		scPastDate = false;
	}	
	return scPastDate;
} // checkPasteDate
function updateDateField(dateFieldName, dateString) {
  DateArray = new Array();
  var targetDateField = document.getElementById(dateFieldName);
  if (dateString)
    targetDateField.value = dateString;
	
  DateArray = dateString.split("/")
  DateOpt = new Option();
  MonthOpt = new Option();
  YearOpt = new Option();
  DateOpt.text = DateArray[0];
  MonthOpt.text = DateArray[1];
  YearOpt.text = DateArray[2];
  DateOpt.selected = true;
  MonthOpt.selected = true;
  YearOpt.selected = true;
  var date_st =(DateArray[0]);
	if(date_st< 10){
	date_st="0"+DateArray[0];
	}
	
	  var month_st=(DateArray[1]);
	  var year_st=(DateArray[2]);
	  var year_str_new= year_st.substr(0,4);
	var month_str_new;
	switch (month_st){
	case "1": 
	var month_str_new = "Jan";
	break;
	case "2": 
	var month_str_new = "Feb";
	break;
	case "3": 
	var month_str_new = "Mar";
	break;
	case "4": 
	var month_str_new = "Apr";
	break;
	case "5": 
	var month_str_new = "May";
	break;
	case "6": 
	var month_str_new = "Jun";
	break;
	case "7": 
	var month_str_new = "Jul";
	break;
	case "8": 
	var month_str_new = "Aug";
	break;
	case "9": 
	var month_str_new = "Sep";
	break;
	case "10": 
	var month_str_new = "Oct";
	break;
	case "11": 
	var month_str_new = "Nov";
	break;
	case "12": 
	var month_str_new = "Dec";
	break;
}
  	con = date_st+"-"+ month_str_new +"-"+ year_str_new;
	monthDropDown.value= con;
  	document.getElementById(datePickerDivID).style.visibility = "hidden";
 if ((dateString) && (typeof(datePickerClosed) == "function"))
    datePickerClosed(targetDateField);
  try{
		doDateDependentChanges();
	}	catch (err){
		//return false;
	}
} // updateDateField
//function to get maximum number of days in current month
function getMaxDaysMonth() {
	var returnvalue;
	switch (currMonth) {
		case 2:
			if((currYear % 4) == 0)	{
				returnvalue = 29;
			}	else {
				returnvalue = 28;
			}
		  break			 
		case 4:
			returnvalue = 30;
		  break
		case 6:
			returnvalue = 30;
		  break
    case 9:
			returnvalue = 30;
		  break
	  case 11:
			returnvalue = 30;
		  break
		case 1:
			returnvalue = 31;
		  break
	  case 3:
			returnvalue = 31;
		break
	  case 5:
			returnvalue = 31;
		  break
		case 7:
			returnvalue = 31;
		  break
		case 8:
			returnvalue = 31;
		  break
		case 10:
			returnvalue = 31;
		  break
		case 12:
			returnvalue = 31;
		  break
		
		default:
		  alert("Error month value not proper " + currMonth);
  		return false;
	}
	return returnvalue;
} // getMaxDaysMonth

function setCurrentDate(){
var m_names = new Array("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
if(curr_date <10){
curr_date="0"+curr_date;
}
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
if(curr_year <10 && curr_year.length==1 ){
curr_year="0"+curr_year;
}
var current_date = curr_date + "-" + m_names[curr_month] + "-" + curr_year;
}

$(document).ready(function(){
$("#monthDropDown").click(function(){
BorderChange();
})
});

function BorderChange() {
$('.dpDayTD:first').css("color","#999");
$('.dpDayTD:last').css("color","#999");
$(".dpDayTR td:first-child").css("border-left","1px solid #b1b1b1");
$(".dpTR td:first-child").css("border-left","1px solid #b1b1b1");
};

function changeEnable(){
	document.getElementById('retdate').disabled=false;
	document.getElementById('retlabel').style.color="black"; 
}
function changeDisable(){
	document.getElementById('retdate').disabled=true;
	document.getElementById('retlabel').style.color="gray"; 
}
function setCurrentDateId(idName){
var m_names = new Array("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
if(curr_date <10){
curr_date="0"+curr_date;
}
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
if(curr_year <10 && curr_year.length==1 ){
curr_year="0"+curr_year;
}
var current_date = curr_date + "-" + m_names[curr_month] + "-" + curr_year;
document.getElementById(idName).value=current_date;
}
