var amura_calendar = function(id, allowedTimes, rails_text_id, allow_sunday, date_delay){
  allow_sunday = (typeof allow_sunday == 'undefined') ? "true" : allow_sunday;
  date_delay = (typeof date_delay == 'undefined') ? "1" : date_delay;
  
	jQuery(function ($) {
		var minDate, maxDate;
		$('input.date').each(function () {
			if ($(this).attr('name').indexOf('dob') === -1) {
				$(this).constrainedCalendar({
					defaultDateDelay: date_delay,
					allowDaysAhead: 365,
					allowToday: false,
					allowHolidays_US: false,
					allowWeekdays: [
             allow_sunday,
						 true,
						 true,
						 true,
						 true,
						 true,
						 true					]
									});
			} else {
				// DOB fields get a different format
				minDate = new Date((new Date()).getFullYear() - 110, 0, 1);
				maxDate = new Date((new Date()).getFullYear() - 12, 0, 1);
				$(this).constrainedCalendar({
					defaultDateDelay: 1,
					allowDaysAhead: 15,
					allowToday: false,
					allowHolidays_US: true,
					allowWeekdays: [true, true, true, true, true, true, true],
					displayDateFormat: 'MM/dd/yyyy',
					minDate: minDate,
					maxDate: maxDate,
					customDate: function (date) {
						return [1, ''];
					}
				});
			}
		});
	});
	
	//alert(document.body.innerHTML);
	document.write("<input id='timeSelect_text' type='hidden' value=''>");	
	document.write("<input id='final_datetime' type='hidden' value=''>");	
	if(allowedTimes == null || allowedTimes.length != 7){
		alert("Error for times");
	}else{
		var sun = getTimesArray(allowedTimes[0][0], allowedTimes[0][1]);
		var mon = getTimesArray(allowedTimes[1][0], allowedTimes[1][1]);
		var tue = getTimesArray(allowedTimes[2][0], allowedTimes[2][1]);
		var wed = getTimesArray(allowedTimes[3][0], allowedTimes[3][1]);
		var thu = getTimesArray(allowedTimes[4][0], allowedTimes[4][1]);
		var fri = getTimesArray(allowedTimes[5][0], allowedTimes[5][1]);
		var sat = getTimesArray(allowedTimes[6][0], allowedTimes[6][1]);
		var id = id;
		var rails_text_id = rails_text_id;
  }
	
  function getTimesArray(start, end){
		var arr = [];
		for(i=start;i<=end;i++){
			if(i <= 12){
				if(i == 12){
					var value = i + "PM";
				}else{
					var value = i + "AM";
				}
			}else{
				if(i == 24){
					var value = i - 12 + "AM";
				}else{
					var value = i - 12 + "PM";
				}
			}
			arr.push([value,i]);
		}
		return arr;
	}

  var getId = function () {
      return id;
  };

  var getRailsTextId = function () {
      return rails_text_id;
  };
  
  var getTimeArrayForDay = function (day) {
	if(day != null){
	  	if(day == "Mon"){
			return mon;	  		
	  	}
	  	if(day == "Tue"){
			return tue;	  		
	  	}
	  	if(day == "Wed"){
			return wed;	  		
	  	}
	  	if(day == "Thu"){
			return thu;	  		
	  	}
	  	if(day == "Fri"){
			return fri;	  		
	  	}
	  	if(day == "Sat"){
			return sat;	  		
	  	}
	  	if(day == "Sun"){
			return sun;	  		
	  	}
  	}
  	return null;
  };
  
  return {
			getId: getId,
			getTimeArrayForDay: getTimeArrayForDay,
			getRailsTextId: getRailsTextId
  };
};

function createTimeForCalendar() {
   var day = document.getElementById(cal.getId()).value.split(",")[0];
   if(day == null || day == ""){
	   popUpCal._clearDate(1);
	   return;
   }else{
   	day = document.getElementById(cal.getId()).value.split(",")[0];
		var arrayTimes = cal.getTimeArrayForDay(day);
		var timeOptions = "";
		if(arrayTimes != null){
			arrayTimes = arrayTimes.sort(sortNumber);
			for(var i=0;i<arrayTimes.length;i++){
				timeOptions += "<option value=" + arrayTimes[i][1] + ">" + arrayTimes[i][0] + "</option>";
			}
		}
		document.getElementById('timeSelect_text').value = arrayTimes[0][1];
	   document.getElementById('final_datetime').value = document.getElementById(cal.getId()).value + " " + arrayTimes[0][1] + ":00";
      document.getElementById(cal.getRailsTextId()).value = document.getElementById('final_datetime').value;
	      
		var x = document.getElementById("calendar_div");
		var y = getElementsByClass("calendar_header", x, null);
		var main_div = y[0];
		var time = document.createElement("div");
		time.setAttribute('id', 'calendarTime');
		time.setAttribute('class', 'calendarTime');
		time.innerHTML = "Time: <select id='timeSelect' class='calendar_newTime' onchange='TimeChanged(this.options[this.options.selectedIndex].value);'>" + timeOptions + "</select>";
		main_div.appendChild(time);
		setSelectValue(document.getElementById('timeSelect'), document.getElementById('timeSelect_text').value);
   }
}

function sortNumber(a,b)
{
	return a[1] - b[1];
}

function TimeChanged(value){
	document.getElementById('timeSelect_text').value = value;	
	document.getElementById('final_datetime').value = document.getElementById(cal.getId()).value + " " + value + ":00";	
	document.getElementById(cal.getRailsTextId()).value = document.getElementById('final_datetime').value;
}

function setSelectValue(selectElement, value){
	for(var i=0;i<selectElement.options.length;i++){
		if(selectElement.options[i].value == value){
			selectElement.selectedIndex = i;
		}
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

