
//document.onclick = function(e) {
//	
//    e = e || event;
//    var target = e.target || e.srcElement;
//
//    var box = document.getElementById(containerId);
//    var link = document.getElementById(linkId);
//
//    do {
//        if (box.id == target.id || link.id == target.id) {
//            //alert('clsoe');
//            // Click occured inside the box, do nothing.
//            return
//        }
//        target = target.parentNode;
//    } while ( target);
//    // Click was outside the box, hide it.
//    box.style.display = "none";
//}

function initContractCalendar(valueInputId, calendarTableId)
{
	
	cellId = $(valueInputId).value;
	return updateCalendar(cellId, calendarTableId);
	
}


function selectDate(selectedCell, valueInputId, calendarTableId, calendarContainerId, dateTextId)
{
	
	var selectedDate = selectedCell.id.split('-');
    var selectedDateObj = new Date();
    
    selectedDateObj.setUTCMonth(selectedDate[1]-1);
    selectedDateObj.setUTCDate(selectedDate[2]);
    selectedDateObj.setUTCFullYear(selectedDate[0]);

    
    // update calendar display
    updateCalendar(selectedCell.id, calendarTableId);
    
    // update input element value
    $(valueInputId).value = selectedCell.id;

    setTimeout('hideElement("'+calendarContainerId+'");', 500);
    
    if(dateTextId != null)
    {
	    var selectedDate = new Date(selectedDate[0], selectedDate[1]-1, selectedDate[2]);
	    $(dateTextId).innerHTML =  formatDate(selectedDate, "EE d NNN yyyy");
    }
    
}


/**
 * Updates calender display highlighting cells.
 * 
 * @param id cell to highlight
 * @param tableId calendar element
 */
function updateCalendar(cellId, calendarTableId)
{
    
    // get calendar
    var table = $(calendarTableId);
    var cells = table.getElementsByTagName('td');
    
    // target cell
//    var cellId = $(date).value;
    
    // iterate, reset cells and highlight selected
    for(i=0; i < cells.length; i++)
    {
		cells[i].style.color = '';
    	cells[i].className = cells[i].className.replace('date_selected', '');
    	
    	if(cells[i].id == cellId)
    	{
    		if(cells[i].className.indexOf('date_today') != -1)
    		{
        		cells[i].style.color = '#fff';
    		}

    		cells[i].className = cells[i].className += ' date_selected';
    	}
    	
    }
	
}




function LZ(x){return(x<0||x>9?"":"0")+x}

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');


//------------------------------------------------------------------
//formatDate (date_object, format)
//Returns a date in the output format specified.
//The format string uses the same abbreviations as in getDateFromFormat()
//------------------------------------------------------------------
function formatDate(date,format) {
	format=format+"";
	var result="";
	var i_format=0;
	var c="";
	var token="";
	var y=date.getYear()+"";
	var M=date.getMonth()+1;
	var d=date.getDate();
	var E=date.getDay();
	var H=date.getHours();
	var m=date.getMinutes();
	var s=date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
	// Convert real date parts into formatted versions
	var value=new Object();
	if (y.length < 4) {y=""+(y-0+1900);}
	value["y"]=""+y;
	value["yyyy"]=y;
	value["yy"]=y.substring(2,4);
	value["M"]=M;
	value["MM"]=LZ(M);
	value["MMM"]=MONTH_NAMES[M-1];
	value["NNN"]=MONTH_NAMES[M+11];
	value["d"]=d;
	value["dd"]=LZ(d);
	value["E"]=DAY_NAMES[E+7];
	value["EE"]=DAY_NAMES[E];
	value["H"]=H;
	value["HH"]=LZ(H);
	if (H==0){value["h"]=12;}
	else if (H>12){value["h"]=H-12;}
	else {value["h"]=H;}
	value["hh"]=LZ(value["h"]);
	if (H>11){value["K"]=H-12;} else {value["K"]=H;}
	value["k"]=H+1;
	value["KK"]=LZ(value["K"]);
	value["kk"]=LZ(value["k"]);
	if (H > 11) { value["a"]="PM"; }
	else { value["a"]="AM"; }
	value["m"]=m;
	value["mm"]=LZ(m);
	value["s"]=s;
	value["ss"]=LZ(s);
	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		if (value[token] != null) { result=result + value[token]; }
		else { result=result + token; }
		}
	return result;
	}



