cal_lang="da";
if (cal_lang=="en")
{
	m_list = Array("January", "February", "March",
          "April",   "May",      "June",
		      "July",    "August",   "September",
		      "October", "November", "December");	  
	m_listShort = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
	day_list = ['mon','tue','wed','thu','fri','sat','sun'];
	day_listshort = ['mo','tu','we','th','fr','sa','su'];
	_weekname="week";
}
else if (cal_lang=="da")
{
	m_list = Array("Januar", "Februar", "Marts",
          "April",   "Maj",      "Juni",
		      "Juli",    "August",   "September",
		      "Oktober", "November", "December");
	m_listShort = ['jan','feb','mar','apr','maj','jun','jul','aug','sep','okt','nov','dec'];
	day_list = ['man','tirs','ons','tors','fre','l\u00F8r','s\u00F8n'];
	day_listshort = ['ma','ti','on','to','fr','l\u00F8','s\u00F8'];
	_weekname="uge";
}
var CalpathToImages = 'http://ecal.sitesuite.dk/js/popup_calendar/images/';	// Relative to your HTML file


function popcal()
{
	var myself=this;
	this.popupCalDiv=null;
	this.popupCalcurDay=0;
	this.popupCalcurMonth=0;
	this.popupCalcurYear=0;
	this.popupCalcurHour=0;
	this.popupCalcurMinute=0;
	this.popupCalcurDate = 0;
	this.popupCalFormat = "";
	
	this.popupCaluseTime=false;
	this.buttonObj=null;
	this.dayObj=null;
	this.monthObj=null;
	this.yearObj=null;
	this.hourObj=null;
	this.minuteObj=null;

	this.init_select=function(buttonObj,dayInput,monthInput,yearInput,hourInput,minuteInput)
	{
		this.buttonObj=buttonObj;
		this.dayObj=dayInput;
		this.monthObj=monthInput;
		this.yearObj=yearInput;
		this.hourObj=hourInput;
		this.minuteObj=minuteInput;
		this.popupCalcurDay = dayInput.options[dayInput.selectedIndex].value;
		this.popupCalcurMonth = monthInput.options[monthInput.selectedIndex].value-1;
		this.popupCalcurYear = yearInput.options[yearInput.selectedIndex].value;
		this.popupCalcurDate = new Date(this.popupCalcurYear,this.popupCalcurMonth,this.popupCalcurDay, 0,0,0,0);
		this.popupCaluseTime=false;
		if (hourInput)
		{
			this.popupCalcurHour = hourInput.options[hourInput.selectedIndex].value/1;
			this.popupCalcurMinute = minuteInput.options[minuteInput.selectedIndex].value/1;
			this.popupCaluseTime=true;
		}
		this.popupCalFormat="";
		this.display_popupCal();
	}
	
	this.init_input=function(inputField,format,buttonObj,displayTime)
	{
		var formatArray= Array();
		var sortArray= Array();
		var no, itemno;

		this.buttonObj=buttonObj;
		this.dayObj=null;
		this.monthObj=null;
		this.hourObj=null;
		this.minuteObj=null;
		this.yearObj=inputField;
		this.popupCalFormat=format;
		this.popupCaluseTime=displayTime;

		var datefield=inputField.value;
		if (datefield=="")
		{
			var Today = new Date();
			datefield=Today.getDate()+"-"+(Today.getMonth()+1)+"-"+Today.getFullYear();
		}
		var items = datefield.split(/[^0-9]/gi);
		formatArray['d'] = this.popupCalFormat.indexOf('dd');
		if(formatArray['d']==-1)formatArray['d'] = this.popupCalFormat.indexOf('d');
		formatArray['m'] = this.popupCalFormat.indexOf('mm');
		if(formatArray['m']==-1)formatArray['m'] = this.popupCalFormat.indexOf('m');
		formatArray['y'] = this.popupCalFormat.indexOf('yyyy');
		formatArray['h'] = this.popupCalFormat.indexOf('hh');
		formatArray['i'] = this.popupCalFormat.indexOf('ii');
		var no=0;
		for (var i in formatArray)
		{
			sortArray[no]=formatArray[i];
			no++;
		}
		sortArray.sort(this.popCalSort);
		for (no=0, itemno=0; no<sortArray.length; no++)
		{
			if (sortArray[no]<0)
				continue
			for (var i in formatArray)
				if (formatArray[i]==sortArray[no])
					formatArray[i]=items[itemno];
			itemno++;
		}
		this.popupCalcurDay = formatArray['d'];
		this.popupCalcurMonth = formatArray['m']-1;
		this.popupCalcurYear = formatArray['y'];
		this.popupCalcurDate = new Date(this.popupCalcurYear,this.popupCalcurMonth,this.popupCalcurDay, 0,0,0,0);
		if (this.popupCaluseTime)
		{
			this.popupCalcurHour = (formatArray['h']>=0 ? formatArray['h'] : 8);
			this.popupCalcurMinute = (formatArray['i']>=0 ? formatArray['i'] : 0);
		}

		this.display_popupCal();
	}

	this.display_popupCal=function()
	{
		this.popupCalDiv=document.getElementById('popupCalDiv');
		if (this.popupCalDiv==null)
		{
			this.popupCalDiv = document.createElement('DIV');
			this.popupCalDiv.id = 'popupCalDiv';
			document.body.appendChild(this.popupCalDiv);
		}
		this.popupCalDiv.style.visibility = 'visible';
		this.popupCalDiv.style.top = this.getPos(this.buttonObj,true)+this.buttonObj.offsetHeight+'px';
		this.popupCalDiv.style.left = this.getPos(this.buttonObj,false)+'px';

		this.writepopupCal();
		this.fixSelectLists(true);
		this.getPos(this.buttonObj,false);
	}

	// This function is from http://www.codeproject.com/csharp/gregorianwknum.asp
	this.getWeek = function (year,month,day)
	{
		day = (day/1)+1;	// Day starts on monday
		year = year /1;
	  month = month/1 + 1; //use 1-12
	  var a = Math.floor((14-(month))/12);
	  var y = year+4800-a;
	  var m = (month)+(12*a)-3;
	  var jd = day + Math.floor(((153*m)+2)/5) +
	               (365*y) + Math.floor(y/4) - Math.floor(y/100) +
	               Math.floor(y/400) - 32045;      // (gregorian calendar)
	  var d4 = (jd+31741-(jd%7))%146097%36524%1461;
	  var L = Math.floor(d4/1460);
	  var d1 = ((d4-L)%365)+L;
	  var NumberOfWeek = Math.floor(d1/7) + 1;
	  return NumberOfWeek;
	}

	this.popCalSort=function (a,b)
	{
		return a/1 - b/1;
	}

	this.writepopupCal= function ()
	{
	  var one_day = 1000 * 60 * 60 * 24;
		var Today = new Date();
		var Todaytxt = "";
		var y=this.popupCalcurYear;
		var m=this.popupCalcurMonth;  

		Todaytxt=day_list[Today.getDay()-1]+' '+Today.getDate()+". "+m_listShort[Today.getMonth()]+". "+Today.getFullYear();
	  var d = new Date(y,m,1, 0,0,0,0);
	  d = new Date(d.valueOf() - (one_day * 6));
	  while (d.getDay() != 1)
	  {
	     d = new Date(d.valueOf() + one_day);
	  }
	//popupCalDiv.innerHTML="";
		while (this.popupCalDiv.firstChild)
		{
			this.popupCalDiv.removeChild(this.popupCalDiv.firstChild);
		}
		var tablecell = document.createElement('TABLE');
		tablecell.width='100%';
		tablecell.height='100%';
		tablecell.cellSpacing=0;
		tablecell.cellPadding=0;
		this.popupCalDiv.appendChild(tablecell);
		row = tablecell.insertRow(-1);
		cell = row.insertCell(-1);
		cell.className="popupCal_toptd";

		var tmptable = document.createElement('TABLE');
		tmptable.width='100%';
		tmptable.height='100%';
		tmptable.cellSpacing=0;
		tmptable.cellPadding=0;
		cell.appendChild(tmptable);
		row = tmptable.insertRow(-1);

	/*****  NAV  *****/
		cell = row.insertCell(-1);
		cell.className="popupCal_leftarrow";
		var img=document.createElement('IMG');
		img.src=CalpathToImages + "leftyear.gif";
		img.onmouseover=function(){myself.popupCalHLArrow(this)};
		img.onmouseout=function(){myself.popupCalHLArrow(this)};
		img.onclick=function(){myself.popupCalYear(this)};
		cell.appendChild(img);

		txt=document.createTextNode(' ');
		cell.appendChild(txt);

		img=document.createElement('IMG');
		img.src=CalpathToImages + "left.gif";
		img.onmouseover=function(){myself.popupCalHLArrow(this)};
		img.onmouseout=function(){myself.popupCalHLArrow(this)};
		img.onclick=function(){myself.popupCalMonth(this)};
		cell.appendChild(img);

		cell = row.insertCell(-1);
		cell.className="popupCal_monthyear";
		cell.align='center';
		txt=document.createTextNode(m_list[m]+' '+y);
		cell.appendChild(txt);

		cell = row.insertCell(-1);
		cell.className="popupCal_rightarrow";
		cell.align='right';
		img=document.createElement('IMG');
		img.src=CalpathToImages + "right.gif";
		img.onmouseover=function(){myself.popupCalHLArrow(this)};
		img.onmouseout=function(){myself.popupCalHLArrow(this)};
		img.onclick=function(){myself.popupCalMonth(this)};
		cell.appendChild(img);

		txt=document.createTextNode(' ');
		cell.appendChild(txt);

		img=document.createElement('IMG');
		img.src=CalpathToImages + "rightyear.gif";
		img.onmouseover=function(){myself.popupCalHLArrow(this)};
		img.onmouseout=function(){myself.popupCalHLArrow(this)};
		img.onclick=function(){myself.popupCalYear(this)};
		cell.appendChild(img);

		txt=document.createTextNode(' ');
		cell.appendChild(txt);

		img=document.createElement('IMG');
		img.src=CalpathToImages + "close.gif";
		img.onmouseover=function(){myself.popupCalHLClose(this)};
		img.onmouseout=function(){myself.popupCalHLClose(this)};
		img.onclick=function(){myself.popupCalClose()};
		cell.appendChild(img);

		delete tmptable;

	/*****  today and time select  *****/

		row = tablecell.insertRow(-1);
		cell = row.insertCell(-1);
		cell.className="popupCal_ToDay";
		var btn=document.createElement('button');
		btn.onclick=function(){myself.popupCalDate(this)};
		btn.day=Today.getDate();
		btn.month=(Today.getMonth()+1);
		btn.year=Today.getFullYear();
		txt=document.createTextNode(Todaytxt);
		btn.appendChild(txt);
		cell.appendChild(btn);
		delete btn;
		if (this.popupCaluseTime)
		{
			txt=document.createTextNode(' ');
			cell.appendChild(txt);
			cell.appendChild(this.popupCalSelectHour());
			txt=document.createTextNode(' ');
			cell.appendChild(txt);
			cell.appendChild(this.popupCalSelectMinute());
		}

		row = tablecell.insertRow(-1);
		cell = row.insertCell(-1);

		tmptable = document.createElement('TABLE');
		tmptable.width='100%';
		tmptable.height='100%';
		tmptable.cellSpacing=1;
		tmptable.cellPadding=0;
		cell.appendChild(tmptable);
		row = tmptable.insertRow(-1);
		cell = row.insertCell(-1);
		cell.className="popupCal_week_col";
		txt=document.createTextNode(_weekname);
		cell.appendChild(txt);
		for (var i=0; i<7; i++)
		{
			cell = row.insertCell(-1);
			cell.className="popupCal_week_head";
			txt=(day_listshort[i]);
			txt=document.createTextNode((txt));
			cell.appendChild(txt);
		}

	/*****  day select  *****/

	  //DAYS
		var onejan = new Date(y,0,1);
	//	tod = new Date(y,0,4);
		var week= Math.ceil((((this.popupCalcurDate - onejan) / 86400000) + onejan.getDay())/7);

	  var day_count = 0;
	  while (day_count < 42)
	  {
	     day_count++

			if (d.getDay() == 1)
			{
				row = tmptable.insertRow(-1);
				week=this.getWeek(d.getFullYear(),d.getMonth(),d.getDate());
				cell = row.insertCell(-1);
				cell.className="popupCal_week_col";
				txt=document.createTextNode(week);
				cell.appendChild(txt);
			}
			if (d.getMonth()!=m)
			{
				cell = row.insertCell(-1);
				cell.className="popupCal_noDay";
				txt=document.createTextNode((d.getDate()));
				cell.appendChild(txt);
			}
			else
			{
				cell = row.insertCell(-1);
				cell.className=(d.getTime()!=this.popupCalcurDate.getTime() ? "popupCal_Day" : "popupCal_activeDay");
				cell.onmouseover=function(){myself.popupCalHLDay(this)};
				cell.onmouseout=function(){myself.popupCalHLDay(this)};
				cell.onclick=function(){myself.popupCalDate(this)};
				cell.day=d.getDate();
				cell.month=(d.getMonth()+1);
				cell.year=d.getFullYear();
				txt=document.createTextNode((d.getDate()));
				cell.appendChild(txt);

			  //next
		     //d = new Date(d.valueOf() + one_day); //replaced to stop double day in oct error
			}
		   d.setDate(d.getDate() + 1);
		  //if (d.getMonth() == next_month && d.getDay() == 1) {  break;  }
	  }

		delete tmptable;
		delete tablecell;
	}

	this.testBehind=function(elm)
	{
		var divtop=GetTagPixels(this.popupCalDiv, 'TOP');
		var divleft=GetTagPixels(this.popupCalDiv, 'LEFT');
		var divbottom=divtop+this.popupCalDiv.offsetHeight;
		var divright=divleft+this.popupCalDiv.offsetWidth;
		var elmtop=GetTagPixels(elm, 'TOP');
		var elmleft=GetTagPixels(elm, 'LEFT');
		var elmbottom=elmtop+elm.offsetHeight;
		var elmright=elmleft+elm.offsetWidth;

		return (elmtop<divbottom && elmbottom>divtop && elmleft<divright && elmright>divleft)
	}

	this.fixSelectLists=function(open)
	{
		var IEMS=navigator.appVersion.match(/(MSIE )(.*)?;/i);
		if (IEMS && parseFloat(IEMS[2])<7)
		{
			for (var i=0;i<document.forms.length;i++)
			{
				for(var j=0;j<document.forms[i].elements.length;j++)
				{
		      if (document.forms[i].elements[j].type.substr(0,6) == 'select')
					{
						if (this.testBehind(document.forms[i].elements[j]))
						document.forms[i].elements[j].style.visibility= (open ? "hidden" : "visible");
					}
				}
			}
		}
	}

	this.getPos=function(elm,top)
	{
		var pixpos; 

		if (top)
			pixpos=elm.offsetTop;
		else
			pixpos=elm.offsetLeft;
		while((elm = elm.offsetParent) != null)
		{
			if (top)
				pixpos+=elm.offsetTop;
			else
				pixpos+=elm.offsetLeft;
		}
		return pixpos;
	}

	this.popupCalHLArrow=function(elm)
	{
		var elmsrc=elm.src;
		if(elm.src.indexOf('_over')>=0)
			elmsrc=elmsrc.replace('_over','');
		else
		{
			var name=elmsrc.substring(0,elmsrc.lastIndexOf('.'));
			var ext=elmsrc.replace(name,'');
			elmsrc=name+'_over'+ext;
		}
		elm.src=elmsrc;
	}

	this.popupCalHLClose=function(elm)
	{
		if(elm.src.indexOf('over')>=0)
		{
				elm.src = CalpathToImages + 'close.gif';
		}
		else
		{
			elm.src = CalpathToImages + 'close_over.gif';
		}
	}

	this.popupCalClose=function()
	{
		this.popupCalDiv.style.visibility = 'hidden';
		this.fixSelectLists(false);
	}

	this.popupCalMonth=function(elm)
	{
		var down=(elm.src.indexOf('left')>=0);

		if (down)
		{
			this.popupCalcurMonth--;
			if (this.popupCalcurMonth<0)
			{
				this.popupCalcurMonth=11;
				this.popupCalcurYear--;
			}
		}
		else
		{
			this.popupCalcurMonth++;
			if (this.popupCalcurMonth>11)
			{
				this.popupCalcurMonth=0;
				this.popupCalcurYear++;
			}
		}
		this.writepopupCal();
	}

	this.popupCalSelectHour=function()
	{
		var optionelm, selected, txt;

		var selectelm=document.createElement('select');
		selectelm.id="popupCalHour";
		selectelm.name="popupCalHour";
		for(no=0;no<24;no++)
		{
			selected="";
			if (no==this.popupCalcurHour)
				selected=" selected";
			optionelm=document.createElement('option');
			optionelm.value=no;
			optionelm.selected=selected;
			txt=document.createTextNode((no<10 ? "0"+no : no));
			optionelm.appendChild(txt);
			selectelm.appendChild(optionelm);
		}

		return selectelm;
	}

	this.popupCalSelectMinute=function()
	{
		var optionelm, selected, txt;

		var selectelm=document.createElement('select');
		selectelm.id="popupCalMinute";
		selectelm.name="popupCalMinute";
		for(no=0, count=0;no<60;no+=5, count++)
		{
			selected="";
			if (no==this.popupCalcurMinute)
				selected=" selected";
			optionelm=document.createElement('option');
			optionelm.value=no;
			optionelm.selected=selected;
			txt=document.createTextNode((no<10 ? "0"+no : no));
			optionelm.appendChild(txt);
			selectelm.appendChild(optionelm);
		}

		return selectelm;
	}

	this.popupCalHLDay = function (elm)
	{
		if(elm.className.indexOf('HL')>=0)
			elm.className=elm.className.replace("HL","");
		else
			elm.className=elm.className+"HL";
	}

	this.popupCalYear=function(elm)
	{
		var down=(elm.src.indexOf('left')>=0);

		if (down)
			this.popupCalcurYear--;
		else
			this.popupCalcurYear++;
		this.writepopupCal();
	}

	this.popupCalDate = function (elm)
	{
		var d=elm.day;
		var m=elm.month;
		var y=elm.year;

		if (this.popupCalFormat)
		{
			this.popupCalFormat = this.popupCalFormat.replace('dd',(d<10 ? '0'+d : d));
			this.popupCalFormat = this.popupCalFormat.replace('mm',(m<10 ? '0'+m : m));
			this.popupCalFormat = this.popupCalFormat.replace('yyyy',y);
			this.popupCalFormat = this.popupCalFormat.replace('d',d/1);
			this.popupCalFormat = this.popupCalFormat.replace('m',m/1);
			if (this.popupCaluseTime)
			{
				sel=document.getElementById('popupCalHour');
				sel=sel.options[sel.selectedIndex].value;
				this.popupCalFormat = this.popupCalFormat.replace('hh',(sel<10 ? '0'+sel : sel));
				sel=document.getElementById('popupCalMinute');
				sel=sel.options[sel.selectedIndex].value;
				this.popupCalFormat = this.popupCalFormat.replace('ii',(sel<10 ? '0'+sel : sel));
			}
			this.yearObj.value=this.popupCalFormat;
		}
		else
		{
			for(no=0;no<this.dayObj.options.length;no++)
			{
				if(this.dayObj.options[no].value/1==d)
				{
					this.dayObj.selectedIndex=no;
					break;
				}
			}
			for(no=0;no<this.monthObj.options.length;no++)
			{
				if(this.monthObj.options[no].value/1==m)
				{
					this.monthObj.selectedIndex=no;
					break;
				}
			}
			for(no=0;no<this.yearObj.options.length;no++)
			{
				if(this.yearObj.options[no].value/1==y)
				{
					this.yearObj.selectedIndex=no;
					break;
				}
			}
			if (this.popupCaluseTime)
			{
				sel=document.getElementById('popupCalHour');
				sel=sel.options[sel.selectedIndex].value;
				for(no=0;no<this.hourObj.options.length;no++)
				{
					if(this.hourObj.options[no].value/1==sel)
					{
						this.hourObj.selectedIndex=no;
						break;
					}
				}
				sel=document.getElementById('popupCalMinute');
				sel=sel.options[sel.selectedIndex].value;
				for(no=0;no<this.minuteObj.options.length;no++)
				{
					if(this.minuteObj.options[no].value/1==sel)
					{
						this.minuteObj.selectedIndex=no;
						break;
					}
				}
			}
		}

		this.popupCalClose();
	}

}

/******************###########################******************/

var popCalObj=false;
function displayPopCalSelectBox(yearInput,monthInput,dayInput,hourInput,minuteInput,buttonObj)
{
	if (!popCalObj)
		popCalObj=new popcal();
	popCalObj.init_select(buttonObj,dayInput,monthInput,yearInput,hourInput,minuteInput);
}

function displayPopCal(inputField,format,buttonObj,displayTime)
{
	if (!popCalObj)
		popCalObj=new popcal();
	popCalObj.init_input(inputField,format,buttonObj,displayTime);
}

