TimePad = function()
{
    this.selectedHourCell 	= null,
    this.selectedMinuteCell = null,
    this.selectedAmPmCell 	= null,
    this.selectedTime       = null,
    this.buttonElement 	    = null,
    this.valueElement 	    = null,
    this.timePadDiv 		= null,
    this.stayOpen 		    = false,
    this.params 		    = null;
}

TimePad.prototype.showTimePad = function(buttonElement, valueElement, params)
{
	if(this.buttonElement == buttonElement && this.valueElement == valueElement && this.params == params)
	{
		/* this popUp was reopened, close it */
		this.hideTimePad();
		return false;
	}
	else if(this.buttonElement != null)
	{
		/* another popUp is open, close it and open this popup */
		this.hideTimePad();
	}
	
	this.buttonElement = buttonElement;
	this.valueElement = valueElement;
	this.params = params;
	
	if(!this.timePadDiv)
	{
		this.timePadDiv = document.createElement("DIV");
		with(this.timePadDiv)
		{
			id = "bdpLitePopUp";
			style.position = "absolute";
			style.visibility = "hidden";
			style.zIndex = "1000";
			onclick = this.ehPopUpClick;
		}
		document.body.appendChild(this.timePadDiv);
	}
	
	this.selectedTime = this.parseTime(this.valueElement.value);
	this.buildTimePad();
		
	this.timePadDiv.style.visibility = "visible";
	this.timePadDiv.style.top = (parseInt(this.valueElement.offsetHeight) + parseInt(this.findPosY(this.valueElement))) + 'px';
	this.timePadDiv.style.left = this.findPosX(this.valueElement) + 'px';
	document.onclick = this.ehDocumentClick;
	document.onkeydown = this.ehKeyPress;
};

TimePad.prototype.parseTime = function(valueString)
{
    if(!valueString || valueString == "")
	{
		return null;
	}
	
	var time = new Date();
	var hr, min;

	hr = parseInt(valueString.substr(0, valueString.indexOf(':')),10);
	min = parseInt(valueString.substr((valueString.indexOf(':') + 1), 3),10);
	
	if(!this.params.militaryTime)
	{
		if (valueString.indexOf("PM") > -1)
        {
            if (hr < 12)
            {
                hr = hr + 12;
            }
        }
        else
        {
            if (valueString.indexOf("AM") > -1)
            {
                if (hr == 12)
                {
                    hr = 0;
                }
            }
            else
            {
               return null;
            }
        }
	}

	
	time.setHours(hr);
	time.setMinutes(min);
	
	return time;
}

TimePad.prototype.ehDocumentClick = function()
{
	if(!timePad.stayOpen)
	{
		timePad.hideTimePad();
	}
	else
	{
		timePad.stayOpen = false;
	}
};

TimePad.prototype.ehPopUpClick = function(e)
{
	timePad.stayOpen = true;
};

TimePad.prototype.ehKeyPress= function(e)
{
	if(!e)
	{
		e = window.event;
	}
	
	if(e.keyCode == 9)
	{
		timePad.hideTimePad();
	}
};

TimePad.prototype.hideTimePad = function()
{
	this.timePadDiv.style.visibility = "hidden";
    this.selectedTime = null;
    this.selectedAmPmCell = null;
    this.selectedHourCell = null;
    this.selectedMinuteCell = null;
	this.buttonElement = null;
	this.valueElement = null;
	this.params = null;
	document.onclick = null;
	document.onkeydown = null;
};

TimePad.prototype.buildTimePad = function()
{
    var timePadFrame = window.document.createElement("table");
	timePadFrame.className = this.params.timePadCssClass;
	timePadFrame.style.cssText = this.params.timePadStyle;
	timePadFrame.setAttribute("border", "0");
	timePadFrame.setAttribute("cellpadding", "0");
	timePadFrame.setAttribute("cellspacing", "0");
	
	var timePadHeader = window.document.createElement("tr");
	timePadHeader.className = this.params.timePadHeaderCssClass;
	timePadHeader.style.cssText = this.params.timePadHeaderStyle;
	this.buildHeader(timePadHeader);
	
	var timePadHrMinCells = window.document.createElement("tr");
	this.buildHrMinCells(timePadHrMinCells);
	
	var timePadFooter = window.document.createElement("tr");
	this.buildFooter(timePadFooter);
	
	/*timePadFrame.insertAdjacentElement("beforeEnd", timePadHeader);
	timePadFrame.insertAdjacentElement("beforeEnd", timePadHrMinCells);
	timePadFrame.insertAdjacentElement("beforeEnd", timePadFooter);*/
	timePadFrame.appendChild(timePadHeader);
	timePadFrame.appendChild(timePadHrMinCells);
	timePadFrame.appendChild(timePadFooter);

    this.timePadDiv.innerHTML = timePadFrame.outerHTML;
};

TimePad.prototype.buildHeader = function(timePadHeader)
{
	var timePadHrTitle = window.document.createElement("td");
	timePadHrTitle.innerText = "Hours";              
	var timePadMinTitle = window.document.createElement("td");
	timePadMinTitle.innerText = "Minutes";
	        	
	//timePadHeader.insertAdjacentElement("beforeEnd", timePadHrTitle);
	//timePadHeader.insertAdjacentElement("beforeEnd", timePadMinTitle);
	timePadHeader.appendChild(timePadHrTitle);
	timePadHeader.appendChild(timePadMinTitle);
};

TimePad.prototype.buildHrMinCells = function(timePadHrMinCells)
{
	var hourCells = window.document.createElement("td");
	this.buildHourCells(hourCells);
	
	var minuteCells = window.document.createElement("td");
	this.buildMinuteCells(minuteCells);
	
	//timePadHrMinCells.insertAdjacentElement("beforeEnd", hourCells);
	//timePadHrMinCells.insertAdjacentElement("beforeEnd", minuteCells);
	timePadHrMinCells.appendChild(hourCells);
	timePadHrMinCells.appendChild(minuteCells);
};

TimePad.prototype.buildFooter = function(timePadFooter)
{
	var footerCell = window.document.createElement("td");
    if(!this.params.militaryTime)
	{
		var footerCell1 = window.document.createElement("td");
		this.buildAmPmButtons(footerCell1); 
		timePadFooter.appendChild(footerCell1);
	}
	else
	{
		footerCell.setAttribute("colspan", "2");
	}
              
    footerCell.align = "right";
	var updateButton = window.document.createElement("button");
	updateButton.setAttribute("type", "button");
	updateButton.setAttribute("value", "Update");
    updateButton.className = this.params.timePadCloseButtonCssClass;
    updateButton.style.cssText = this.params.timePadCloseButtonStyle;
	updateButton.onClick = "timePad.updateTime();";
    var cancelButton = window.document.createElement("button");
	cancelButton.setAttribute("type", "button");
	cancelButton.setAttribute("value", "Cancel");
	cancelButton.onClick = "timePad.hideTimePad();";
    cancelButton.className = this.params.timePadCloseButtonCssClass;
    cancelButton.style.cssText = this.params.timePadCloseButtonStyle;
	footerCell.appendChild(updateButton);
    footerCell.innerHtml = "&nbsp;";
	footerCell.appendChild(cancelButton);
	timePadFooter.appendChild(footerCell);	
};

TimePad.prototype.buildAmPmButtons = function(footerCell)
{
    var table = window.document.createElement("table");
    table.setAttribute("border", "0");
	table.setAttribute("cellpadding", "0");
	table.setAttribute("cellspacing", "0");
    table.className = this.params.timePadHrMinTableCssClass;
    table.style.cssText = this.params.timePadHrMinTableStyle;
    
	var row = window.document.createElement("tr");
	for(var x=0; x<2; x++)
	{
		var cell = window.document.createElement("td");
		cell.onmouseover = "timePad.amPmMouseOver(this);";
		cell.onmouseout = "timePad.amPmMouseOut(this);";
		cell.onClick = "timePad.selectAmPm(this);";
        cell.className = this.params.timePadDefaultTimeCssClass;
        cell.style.cssText = this.params.timePadDefaultTimeStyle;
        cell.style.fontSize = "8px";
		
		if(x == 0)
		{
			cell.id = "amButton";
			cell.innerText = "AM";
			
			if(this.selectedTime && this.selectedTime.getHours() <= 12)
			{
				this.selectAmPm(cell);
			}
		}
		else
		{
			cell.id = "pmButton";
			cell.innerText = "PM";
			
			if(this.selectedTime && this.selectedTime.getHours() > 12)
			{
				this.selectAmPm(cell);
			}
		}
		
		row.appendChild(cell);
	}
	table.appendChild(row);
	footerCell.appendChild(table);
};

TimePad.prototype.amPmMouseOver = function(cell)
{
	if(this.selectedAmPmCell)
	{
		var selAmPmCell = window.document.getElementById(this.selectedAmPmCell);
		
		if(selAmPmCell.innerText != cell.innerText)
		{
			cell.style.cssText = this.params.timePadMouseHoverStyle;
			cell.className = this.params.timePadMouseHoverCssClass;
			cell.style.fontSize = "8px";
		}
	}
	else
	{
		cell.style.cssText = this.params.timePadMouseHoverStyle;
		cell.className = this.params.timePadMouseHoverCssClass;
		cell.style.fontSize = "8px";
	}

};

TimePad.prototype.amPmMouseOut = function(cell)
{
	if(this.selectedAmPmCell)
	{
		var selAmPmCell = window.document.getElementById(this.selectedAmPmCell);
		
		if(selAmPmCell.innerText != cell.innerText)
		{
			cell.style.cssText = this.params.timePadDefaultTimeStyle;
			cell.className = this.params.timePadDefaultTimeCssClass;
			cell.style.fontSize = "8px";
		}
	}
	else
	{
		cell.style.cssText = this.params.timePadDefaultTimeStyle;
		cell.className = this.params.timePadDefaultTimeCssClass;
		cell.style.fontSize = "8px";
	}

};

TimePad.prototype.selectAmPm = function(cell)
{
	if(this.selectedAmPmCell)
	{
		var amPmCell = window.document.getElementById(this.selectedAmPmCell);
		amPmCell.style.cssText = this.params.timePadDefaultTimeStyle;
		amPmCell.className = this.params.timePadDefaultTimeCssClass;
		amPmCell.style.fontSize = "8px";
	}
	
	this.selectedAmPmCell = cell.id;
	cell.style.cssText = this.params.timePadSelectedTimeStyle;
	cell.className = this.params.timePadSelectedTimeCssClass;
    cell.style.fontSize = "8px";
};


TimePad.prototype.updateTime = function()
{
	var hr = window.document.getElementById(this.selectedHourCell).innerText;
	var min = window.document.getElementById(this.selectedMinuteCell).innerText;
    var amPm = ((!this.params.militaryTime)? window.document.getElementById(this.selectedAmPmCell).innerText : "");

	var newTimeString = window.document.getElementById(this.params.postBackFieldId).value =  this.valueElement.value = this.getFormatedTime(hr, min, amPm);
	this.selectedTime = this.parseTime(newTimeString);
	
	if(this.params.autoPostBack != false)
	{
	   
	   setTimeout("__doPostBack(\'" + this.params.timePickerID + "\',\'\')", 0);
	}
	this.hideTimePad();
}

TimePad.prototype.getFormatedTime = function(hour, minute, amPm)
{
   if(hour < 10)
   {
    hour = "0" + hour;
   }
   
   if(minute < 10)
   {
    minute = "0" + minute;
   }
   
   return hour + " : " + minute + " " + amPm.toUpperCase();
};

TimePad.prototype.buildHourCells = function(hourCells)
{
	var xCount,yCount;
	var hours;
	var hourTable = window.document.createElement("table");
	hourTable.setAttribute("border", "0");
	hourTable.setAttribute("cellpadding", "0");
	hourTable.setAttribute("cellspacing", "0");
    hourTable.className = this.params.timePadHrMinTableCssClass;
    hourTable.style.cssText = this.params.timePadHrMinTableStyle;
	
	if(this.params.militaryTime)
	{
		xCount = 4;
		yCount = 6;
		hours = 0;
	}
	else
	{
		xCount = 2;
		yCount = 6;
		hours = 1;
	}
	
	for(var y=0; y<yCount; y++)
	{
		var row = window.document.createElement("tr");
				
		for(var x=0; x<xCount; x++)
		{
			var cell = window.document.createElement("td");
			cell.onmouseover = "timePad.hourMouseOver(this)";
			cell.onmouseout = "timePad.hourMouseOut(this)";
			cell.onClick = "timePad.selectHour(this)";
            cell.className = this.params.timePadDefaultTimeCssClass;
            cell.style.cssText = this.params.timePadDefaultTimeStyle;
			cell.id = "hr" + hours;
						
			if(this.selectedTime)
			{
				var hr = this.selectedTime.getHours();
								
				if(!this.params.militaryTime)
				{
					if(parseInt(hr) == 0)
					{
					    hr = 12;
					}
					else
					{
					    if(parseInt(hr) > 12)
					    {
						    hr = parseInt(hr) - 12;
					    }
					}
				}
				
				if(hr == hours)
				{
					this.selectHour(cell);
				}
			}
			cell.innerText = hours++;
			
			row.appendChild(cell);
		}
		
		hourTable.appendChild(row);
	}

	hourCells.appendChild(hourTable);
};

TimePad.prototype.selectHour = function(cell)
{
	if(this.selectedHourCell)
	{
		var selHrCell = window.document.getElementById(this.selectedHourCell);
		selHrCell.style.cssText = this.params.timePadDefaultTimeStyle;
		selHrCell.className = this.params.timePadDefaultTimeCssClass;
	}
	
	this.selectedHourCell = cell.id;
	
	cell.style.cssText = this.params.timePadSelectedTimeStyle;
	cell.className = this.params.timePadSelectedTimeCssClass;
};

TimePad.prototype.hourMouseOver = function(cell)
{
	if(this.selectedHourCell)
	{
		var selHrCell = window.document.getElementById(this.selectedHourCell);
		
		if(selHrCell.innerText != cell.innerText)
		{
			cell.style.cssText = this.params.timePadMouseHoverStyle;
			cell.className = this.params.timePadMouseHoverCssClass;
		}
	}
	else
	{
		cell.style.cssText = this.params.timePadMouseHoverStyle;
		cell.className = this.params.timePadMouseHoverCssClass;
	}
};

TimePad.prototype.hourMouseOut = function(cell)
{
	if(this.selectedHourCell)
	{
		var selHrCell = window.document.getElementById(this.selectedHourCell);
		
		if(selHrCell.innerText != cell.innerText)
		{
			cell.style.cssText = this.params.timePadDefaultTimeStyle;
			cell.className = this.params.timePadDefaultTimeCssClass;
		}
	}
	else
	{
		cell.style.cssText = this.params.timePadDefaultTimeStyle;
		cell.className = this.params.timePadDefaultTimeCssClass;
	}
};

TimePad.prototype.buildMinuteCells = function(minuteCells)
{
	var minutes = 0;
	var minuteTable = window.document.createElement("table");
    minuteTable.setAttribute("border", "0");
	minuteTable.setAttribute("cellpadding", "0");
	minuteTable.setAttribute("cellspacing", "0");
    minuteTable.className = this.params.timePadHrMinTableCssClass;
    minuteTable.style.cssText = this.params.timePadHrMinTableStyle;
	
	for(var y=0; y<6; y++)
	{
		var row = window.document.createElement("tr");
				
		for(var x=0; x<10; x++)
		{
			var cell = window.document.createElement("td");
			cell.onmouseover = "timePad.minuteMouseOver(this)";
			cell.onmouseout = "timePad.minuteMouseOut(this)";
			cell.onClick = "timePad.selectMinute(this)";
			
            cell.className = this.params.timePadDefaultTimeCssClass;
            cell.style.cssText = this.params.timePadDefaultTimeStyle;
			cell.id = "min" + minutes;
			if(minutes%5 <= 0)
			{
			    cell.style.backgroundColor = "#ffff99";
			    cell.style.borderColor = "#ffff99";
			}
			
			
			if(this.selectedTime && (minutes == this.selectedTime.getMinutes()))
			{
				this.selectMinute(cell);
			}

			cell.innerText = minutes++;
			row.appendChild(cell);
		}
		
		minuteTable.appendChild(row);
	}

	minuteCells.appendChild(minuteTable);
};

TimePad.prototype.selectMinute = function(cell)
{
	if(this.selectedMinuteCell)
	{
		var minCell = window.document.getElementById(this.selectedMinuteCell);
		minCell.style.cssText = this.params.timePadDefaultTimeStyle;
		minCell.className = this.params.timePadDefaultTimeCssClass;
		if((parseInt(minCell.innerText) % 5) <= 0)
		{
			minCell.style.backgroundColor = "#ffff99";
			minCell.style.borderColor = "#ffff99";
	    }
	}
	
	this.selectedMinuteCell= cell.id;
	
	cell.style.cssText = this.params.timePadSelectedTimeStyle;
	cell.className = this.params.timePadSelectedTimeCssClass;
};

TimePad.prototype.minuteMouseOver = function(cell)
{
	if(this.selectedMinuteCell)
	{
		var minCell = window.document.getElementById(this.selectedMinuteCell);
		
		if(minCell.innerText != cell.innerText)
		{
			cell.style.cssText = this.params.timePadMouseHoverStyle;
			cell.className = this.params.timePadMouseHoverCssClass;
		}
	}
	else
	{
		cell.style.cssText = this.params.timePadMouseHoverStyle;
		cell.className = this.params.timePadMouseHoverCssClass;
	}
};

TimePad.prototype.minuteMouseOut = function(cell)
{
	if(this.selectedMinuteCell)
	{
		var minCell = window.document.getElementById(this.selectedMinuteCell);
		
		if(minCell.innerText != cell.innerText)
		{
			
	        cell.style.cssText = this.params.timePadDefaultTimeStyle;
			cell.className = this.params.timePadDefaultTimeCssClass;
			if((parseInt(cell.innerText) % 5) <= 0)
		    {
			 cell.style.backgroundColor = "#ffff99";
			 cell.style.borderColor = "#ffff99";
	        }
		}
	}
	else
	{
		
		cell.style.cssText = this.params.timePadDefaultTimeStyle;
		cell.className = this.params.timePadDefaultTimeCssClass;
		if((parseInt(cell.innerText) % 5) <= 0)
		{
			 cell.style.backgroundColor = "#ffff99";
			 cell.style.borderColor = "#ffff99";
	    }
	}
	
};


TimePad.prototype.buildStyleAttributes = function(style, cssClass)
{
	var atts = "";
	if(style.length > 0 || cssClass.length > 0)
	{
		if(style.length > 0)
		{
			atts += " style=\"" + style + "\"";
		}
		if(cssClass.length > 0)
		{
			atts += " class=\"" + cssClass + "\"";
		}
	}
	return atts;
};

TimePad.prototype.findPosX = function(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
};

TimePad.prototype.findPosY = function(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
};


