var monthes = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var monthes_short = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');
var monthes_by_title = {'Jan':'0','Feb':'1','Mar':'2','Apr':'3','May':'4','Jun':'5','Jul':'6','Aug':'7','Sept':'8','Oct':'9','Nov':'10','Dec':'11'};
var monthes_digit = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
var daysOfWeek = new Array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
var daysOfWeekShort = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var daysLeap = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
var daysNotLeap = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var calendar = [];

function initCalendar()
{
	var calendarContainer = document.getElementById('schedule-popup');
	if(!calendarContainer) calendarContainer = document;
	var _inputs = getEls('input','calendar-input',calendarContainer);
	var _as = getEls('a','calendar-selector',calendarContainer);
	var _divs = getEls('div','calendar',calendarContainer);
	for(var i = 0; i < _inputs.length; i++)
	{
		if(_as[i] && _divs[i])
		{
			calendar[i] = new Calendar();
			calendar[i].init(_inputs[i],_as[i].firstChild,_divs[i]);
		}
	}
}

function getEls(tag,classn,container)
{
	var _return = [];
	if(container)
	{
		var els = container.getElementsByTagName(tag);
		for(var i = 0; i < els.length; i++)
		{
			if(els[i].className.indexOf(classn) != -1)
			{
				els[i]._left = getRealLeft(els[i]);
				els[i]._top = getRealTop(els[i]);
				_return[_return.length] = els[i];
			}
		}
	}
	return _return;
}

if (window.addEventListener){
	window.addEventListener("load", initCalendar, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", initCalendar);
}

Calendar = function(){

this.init = function(_input,_opener,_container){	
		
		this.baseDate = new Date();
		this.selectDate = this.baseDate;
		this.currentDate = new Date();
		this.calendarContainer = _container;
		//this.calendarOpener = _opener;
		this.calendarInput = _input;		
		//this.calendarOpener._this = this;
		//this.calendarOpener.onclick = ShowHideCalendar;
		//ShowHideCalendar;
		this.show();
};

this.show = function ()
{
	this._show();
	this._opened = true;	
	//this.calendarContainer.style.left = getRealLeft(this.calendarOpener) + 17 + 'px';
	//this.calendarContainer.style.top = getRealTop(this.calendarOpener) - 3 + 'px';
	this.calendarContainer.style.display = 'block';
};

this._show = function ()
{
	var calendarContainer = this.calendarContainer;
	if (calendarContainer)
	{
		calendarContainer.innerHTML = '';
		
		var node = document.createElement("strong");
		node.className = 'month';
		var monthesContainer = calendarContainer.appendChild(node);

		monthesContainer.innerHTML = monthes[this._getCurrentMonthNumber()] + ', ' + this._getYear();

		var node = document.createElement("ul");
		node.className = 'cal-nav';
		var navContainer = calendarContainer.appendChild(node);
		
		var node = document.createElement("ul");
		node.className = 'week';
		var daysOfWeekContainer = calendarContainer.appendChild(node);
		
		var node = document.createElement("ul");
		node.className = 'date';
		var daysContainer = calendarContainer.appendChild(node);
		
		/**********/
		var aNode = document.createElement("a");
		aNode.href = "javascript:;";
		aNode.className = 'btn-first';
		aNode.innerHTML = 'prev Year';
		aNode._this = this;
		aNode.onclick = _showPrevYear;
		
		var _li = document.createElement("li");
		_li.appendChild(aNode);
		var aElement = navContainer.appendChild(_li);
		/**********/		
		
		/*********************/
		var aNode = document.createElement("a");
		aNode.href = "javascript:;";
		aNode.className = 'btn-prev';
		aNode.innerHTML = monthes_short[this._getPrevMonthNumber()];
		aNode._this = this;
		aNode.onclick = _showPrevMonth;
		
		var _li = document.createElement("li");
		_li.appendChild(aNode);
		var aElement = navContainer.appendChild(_li);
		/*********************/
		
		/********************************/
		var _li = document.createElement("li");
		_li.className = 'today';
		_li.id = 'today-li';
		_li.innerHTML = 'Today';
		navContainer.appendChild(_li);
		/********************************/
		
		/*********************/
		var aNode = document.createElement("a");
		aNode.innerHTML = monthes_short[this._getNextMonthNumber()];
		aNode.href = "javascript:;";
		aNode.className = 'btn-next';
		aNode._this = this;
		aNode.onclick = _showNextMonth;
		
		var _li = document.createElement("li");
		_li.appendChild(aNode);
		var aElement = navContainer.appendChild(_li);
		/*********************/

		/**********/
		var aNode = document.createElement("a");
		aNode.href = "javascript:;";
		aNode.className = 'btn-last';
		aNode.innerHTML = 'next Year';
		aNode._this = this;
		aNode.onclick = _showNextYear;
		
		var _li = document.createElement("li");
		_li.appendChild(aNode);
		var aElement = navContainer.appendChild(_li);
		/**********/			
		
		for (var i = 0; i < 7; i++)
		{
			var node = document.createElement("li");
			if (i == 0 || i == 6) node.className = 'green';
			node.innerHTML = daysOfWeekShort[i];
			var dayOfWeekElement = daysOfWeekContainer.appendChild(node);
		}
		
		if (this._getYear() % 4 == 0)
		{
			var daysAmount = daysLeap[this._getCurrentMonthNumber()];
		}
		else
		{
			var daysAmount = daysNotLeap[this._getCurrentMonthNumber()];
		}
		
		var date = new Date(this.baseDate.toString());
		var currdate = this.currentDate;
		
		for (var day = 1; day <= daysAmount; day++)	
		{
			date.setDate(day);
			if (day == 1)
			{
				var upto = date.getDay();
				for (var i = 0; i < upto; i++)
				{
					var _li = document.createElement("li");
					_li.innerHTML = "&nbsp";
					var dayElement = daysContainer.appendChild(_li);
				}
			}
			
			var	_class = 'day';

			if(currdate.getFullYear() == date.getFullYear() && currdate.getMonth() == date.getMonth() && currdate.getDate() == date.getDate())
			{
				_class += ' today';
			}

			if(this.selectDate.getFullYear() == date.getFullYear() && this.selectDate.getMonth() == date.getMonth() && this.selectDate.getDate() == date.getDate())
			{
				_class += ' selected';
			}
			
			var aNode = document.createElement("a");
			aNode.className = _class;
			aNode.innerHTML = day;
			aNode._value = day;
			aNode.href = "javascript:;"
			aNode._this = this;
			aNode.onclick = calendarChoose;
			var _li = document.createElement("li");
			if ((i + day)%7 == 0) _li.className = 'green';
			if ((i + day)%7 == 1) _li.className = 'green';
			_li.appendChild(aNode);
			var aElement = daysContainer.appendChild(_li);
		}
		var node = document.createElement("a");
		node.className = 'btn-sel-date';
		node.href = '#';
		node.innerHTML = 'Select date';
		var daysContainer = calendarContainer.appendChild(node);
	}	
};

this.hide = function ()
{
	/*this.calendarContainer.style.display = 'none';
	this._opened = false;
	try{showSelectBoxes(this.calendarContainer)}catch (e){};*/
}

this._getYear = function ()
{
	return this.baseDate.getFullYear();
}

this._getShortYear = function ()
{
	var year = "" + this.baseDate.getFullYear();
	return year.substring(2,4);
}

this._getCurrentMonthNumber = function ()
{
	return this.baseDate.getMonth();
}

this._getPrevYear = function ()
{
	var number = this._getYear();
	if (number > 0) {
		return number - 1;
	}
	else {
		return 0;
	}
};

this._getNextYear = function ()
{
	var number = this._getYear();
	return number + 1;
}

this._showPrevYear = function ()
{
	number = this._getPrevYear();
	this.baseDate.setYear(number);
	this._show();
};

this._showNextYear = function ()
{
	number = this._getNextYear();
	this.baseDate.setYear(number);
	this._show();
};

this._getPrevMonthNumber = function ()
{
	var number = this._getCurrentMonthNumber();
	if (number > 0) {
		return number - 1;
	}
	else {
		return 11;
	}
};

this._getNextMonthNumber = function ()
{
	var number = this._getCurrentMonthNumber();
	if (number < 11) {
		return number + 1;
	}
	else {
		return 0;	
	}
};


	}

var calendarChoose = function ()
{
/*calendar hack w/jquery*/

    $(".selectTime").hide();
    $(".rbselectTime").hide();
    $('input[name="rb-time-of-day"]').removeAttr("checked"); 

	this._this.baseDate.setDate(this._value);
	this._this.calendarInput.value = this._this._getYear() + '-' + addZero(this._this._getCurrentMonthNumber()+1) + '-' + addZero(this._this.baseDate.getDate());
	if (document.getElementById('today-li')){
		document.getElementById('today-li').innerHTML = addZero(this._this.baseDate.getDate()); 
	}
	
	var currentTime = new Date();
    var calDate = new Date(this._this.baseDate.setDate(this._value)); 
    var dateDiff = calDate.getDate() - currentTime.getDate();

    var myDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
    var dayOfTheWeek = calDate.getDay();
    dayOfTheWeek = myDays[dayOfTheWeek];
    
    /*
    var m_names = new Array("January", "February", "March", 
        "April", "May", "June", "July", "August", "September", 
        "October", "November", "December");

    var curr_date = calDate.getDate();
    var curr_month = calDate.getMonth();
    var curr_year = calDate.getFullYear();
    
    var apptTime = dayOfTheWeek + ", " + m_names[curr_month] + " " + curr_date + ", " + curr_year;
    */
    
    if(dateDiff < 0 || dayOfTheWeek == "Sunday"){
        //invalid date
        $("#select-your-time-invalid").show();
        return;
        
    }else if(dateDiff == 0 || dateDiff == 1){
        //next/same day appt
        $("#select-your-time-same-day").show();
        return;
        
    }else if(dayOfTheWeek == "Friday" || dayOfTheWeek == "Saturday"){
        //Morning - Afternoon
        $("#select-your-time").show();
        
        $("#rb-morning-holder").show()
        $("#rb-afternoon-holder").show();
        
        return;
    }else{
        //Afternoon - Evening
        $("#select-your-time").show();
        
        $("#rb-afternoon-holder").show();
        $("#rb-evening-holder").show();
        return;
    }
   
   /*
    if(dateDiff < 0){
        //invalid date "Sorry, not available
    }else if(dateDiff == 0){
       // Same Day  alert("<h5>For next day appointments, please call the showroom direct to schedule a time.<br/>(888) 863-4966</h5>");
    
    }else if(dateDiff == 1){
        // 24 hours alert("<h5>For next day appointments, please call the showroom direct to schedule a time.<br/>(888) 863-4966</h5>");
    }else{
        //valid date
    }
    */
    
    
    /*
    if(currentTime > calDate){
        alert("invalid date");
    }else if(currentTime = nexDay){
        alert("<h5>For next day appointments, please call the showroom direct to schedule a time.<br/>(888) 863-4966</h5>");
    }else{
        alert("valid date");
    }*/
    /*
    }else if(currentTime = nexDay){
        alert("<h5>For next day appointments, please call the showroom direct to schedule a time.<br/>(888) 863-4966</h5>");
    }else{
       alert("select your date bitch"); 
    }
    */
    
	

	
	//alert(this._this.calendarInput.value);
	/*
	try
	{
		getHttpRequest('index.php?_date='+this._this._getYear()+'-'+(this._this._getCurrentMonthNumber()+1)+'-'+this._this.baseDate.getDate()+'&secid='+secid+'&catid='+catid+'&ajax','_conteiner=daytime','');
	}
	catch(e)
	{
		
	}
	*/
	
	//this._this.hide();
}

var _showPrevMonth = function ()
{
	number = this._this._getPrevMonthNumber();
	if (number == 11)
	{
		this._this.baseDate.setYear(this._this._getYear()  - 1);
	}
	this._this.baseDate.setMonth(number);
	this._this._show();
}
var _showPrevYear = function ()
{
	number = this._this._getPrevYear();
	this._this.baseDate.setYear(number);
	this._this._show();
}
var _showNextMonth = function ()
{
	number = this._this._getNextMonthNumber();
	if (number == 0)
	{
		this._this.baseDate.setYear(this._this._getYear() + 1);
	}
	this._this.baseDate.setMonth(number);
	this._this._show();
}
var _showNextYear = function ()
{
	number = this._this._getNextYear();
	this._this.baseDate.setYear(number);
	this._this._show();
}
var ShowHideCalendar = function ()
{
	if(this._this._opened)
	{
		this._this.hide();
	}
	else
	{
		this._this.show();
	}
}

function addZero(_text)
{
	_text = "" + _text;
	if(_text.length == 1)
	{
		return '0' + _text;
	}
	else
	{
		return _text;
	}
}

function getRealLeft(elem)
{
	var nLeft = 0;
	if(elem)
	{
		do
		{
			nLeft += elem.offsetLeft - elem.scrollLeft;
			elem = elem.offsetParent;
		}
		while(elem)
	}
	return nLeft;
}

function getRealTop(elem)
{
	var nTop = 0;
	if(elem)
	{
		do
		{
			nTop += elem.offsetTop - elem.scrollTop;
			elem = elem.offsetParent;
		}
		while(elem)
	}
	return nTop;
}

function hideAllCalendarPopups()
{
	for(var i = 0; i < calendar.length ; i++)
	{
		calendar[i].hide();
	}
}
