	/* PCM is acronym for Popup Calendar Manager */

	function PCM(Calendar, refTextbox, VisibilityList, countrycode, sourceTextBox, freeSelection){	
		this.MainTable=Calendar;				            // The baseobject of the callendar. All childs belong to this.
		this.textBox=refTextbox;				                // Name of the textbox object, if these are separated with a '|' then we are talking about several dropdown boxes instead of a text box
		this.VisibleObjects = VisibilityList;	        // Array of names for the objects (dropdownlists) that should be hidden		
		this.Language = countrycode;			        // From which site the call comes
		this.date=new Date;						                // initDate
		this.cutoffDate=new Date;				            // initStoredDate
		this.sourceTextBox=sourceTextBox;		// if this!=null it provides the cutoff value for the calendar
        this.freeSelection=freeSelection;            // determins if there should be grayed days or not.
		this.date.setFullYear(2006,05,01);
		if(refTextbox.split('|').length==1){
			this.outputFunction = this.handleTextboxOutput;
			this.inputFunction = this.handleTextboxInput;
		}
		else if(refTextbox.split("|").length == 2){
			this.outputFunction = this.handle2DropdownOutput;
			this.inputFunction = this.handle2DropdownInput;
		}
	}

	PCM.prototype.handleTextboxOutput = function(year, month, day){
		if(this.Language=='NO'){
			this.getTextBox().innerText=day + "." + month + "." + year;
			this.getTextBox().focus();
		}
        else if(this.Language=='FI'){
			this.getTextBox().innerText=day + "." + month + "." + year;
			this.getTextBox().focus();
        }
		else{
			this.getTextBox().innerText=year + "-" + month + "-" + day;
			this.getTextBox().focus();
		}
	}

	PCM.prototype.handleTextboxInput = function(){
		var sDate=this.getTextBox().value;
		if (this.Language=='NO'){
			var parts=sDate.split('.');
			if(parts.length==3)
				sDate=parts[2] + '-' + parts[1] + '-' + parts[0];
		}
        else if (this.Language=='FI'){
			var parts=sDate.split('.');
			if(parts.length==3)
				sDate=parts[2] + '-' + parts[1] + '-' + parts[0];
		}

		return this.validateDateFormat(sDate);
	}

	PCM.prototype.handle2DropdownOutput = function(year, month, day){
		var drops = this.textBox.split('|');
		getObj(drops[0]).value=parseInt(parseFloat(day),10);
		var mon = getObj(drops[1]);
		for(var i=0;i<mon.length;i++){
			mon.selectedIndex=i;
			if(mon.value.split('|')[1]==year+'-'+month)
				return;
			else if(mon.value==year+'-'+month)
				return;
		}
					
	}
	PCM.prototype.handle2DropdownInput = function(){
		var yearmonth = getObj(this.textBox.split("|")[1]).value.split("|")[1];
		if (yearmonth == undefined) yearmonth = getObj(this.textBox.split("|")[1]).value;
		var thisyear=yearmonth.split('-')[0];
		var thismonth=yearmonth.split('-')[1];
		var thisday=getObj(this.textBox.split("|")[0]).value;
        var sDate=thisyear+'-'+thismonth+'-'+thisday;
		return this.validateDateFormat(sDate);		

	}
	
	PCM.prototype.GetDepartureDate = function(){
        var inp = this.inputFunction();
	    var parts=inp.split('-');
	    var d = new Date;
	    d.setUTCFullYear(parts[0],parseInt(parts[1],10)-1,parts[2]);
        return d;
	}
	
	PCM.prototype.validateDateFormat = function(date){
	    var parts = date.split('-');
	    if(parts.length==3){
            if(parseInt(parts[0],10)>=1000 && 
            parseInt(parts[1],10)>=0 && parseInt(parts[1],10)<=12 &&
            parseInt(parts[2],10)>=0 && parseInt(parts[2],10)<=31){
                return date;
            }
	    }
            var d= new Date();
	        var out=d.getFullYear()+'-'+this.Pad(d.getMonth())+'-'+this.Pad(d.getDay());
	        return out;
	}

    PCM.prototype.Pad = function(inp){
        return int = (inp < 10) ? '0' + inp : inp;
    }
	
	PCM.prototype.getMainTable = function(){
		return document.getElementById(this.MainTable);
	}
	
	PCM.prototype.getTextBox = function(){
		return getObj(this.textBox);
	}

	PCM.prototype.getReferenceElement = function(){
		if(this.textBox.split('|').length==1){
			return getObj(this.textBox);
		}
		else if(this.textBox.split("|").length == 2){
			return  getObj(this.textBox.split("|")[0]);
		}
		else return null;
	}

	PCM.prototype.getSourceTextBox = function(){
		if(this.sourceTextBox==null)
			return null;
		return getObj(this.sourceTextBox);
	}

	PCM.prototype.getCalendarObj = function(weekname,parent){
		var getWeekObjCounter = 0 ;
		if (parent==null)
			parent=this.getMainTable();
		
		for(getWeekObjCounter=0;getWeekObjCounter<parent.childNodes.length;getWeekObjCounter++){
			obj=parent.childNodes[getWeekObjCounter];
			if(obj.id != undefined)
				if(obj.id.indexOf(weekname)>=0)
					return obj;
		}
		
		// If not found then iterate all childs
		for(getWeekObjCounter=0;getWeekObjCounter<parent.childNodes.length;getWeekObjCounter++){
			obj=parent.childNodes[getWeekObjCounter];
			newobj=this.getCalendarObj(weekname,obj);
			if(newobj!=null)
				return newobj;
		}
		return null;			
	}

	PCM.prototype.getDayObj = function (week,dayName){
		var getDayObjCounter = 0 ;
		for(getDayObjCounter=0;getDayObjCounter<week.childNodes.length;getDayObjCounter++){
			obj=week.childNodes[getDayObjCounter];
            if(obj.innerHTML != undefined)
    			if(obj.innerHTML.indexOf(dayName)>=0)
	    			return obj;
//			if(obj.nodeValue.indexOf(dayName)>=0)
		}
		return null;	
	}

	PCM.prototype.setText = function (obj,text,className){
		var obj=obj.childNodes[0];
		obj.value=text;
		obj.className=className;
	}
	
	PCM.prototype.getText = function (obj){
		var obj=obj.childNodes[0];
		return obj.value;
	}

	PCM.prototype.getClassName = function (obj){
		var obj=obj.childNodes[0];
		return obj.className
	}
	PCM.prototype.setClassName = function (obj,text){
		var obj=obj.childNodes[0];
		obj.className=text;
	}

	PCM.prototype.renderWeeks = function (month){
		daysInMonth=this.getDaysInMonth(month);
		startWeek=this.getWeek(month.getFullYear(),month.getMonth(),1);	
				
		thisDate=new Date;
		thisDate.setFullYear(month.getFullYear(),month.getMonth(),1);
		startday=thisDate.getDay();
		if(startday==0)
			startday=7;

		weekcounter=1;
		counter=1;
		var o = 1;
		while(weekcounter<=6){
			weekobj=this.getCalendarObj('Week'+weekcounter);

			//clear before
			for(o=1;o<startday;o++){
				obj=this.getDayObj(weekobj,'Day'+o);
				if(obj!=null){
					this.setText(obj,'','calDayEmpty');
				}			
			}										

			for(o=startday;o<=7 && counter<=daysInMonth;o++){
				obj=this.getDayObj(weekobj,'Day'+o);
				if(obj!=null){
					thisDate=new Date;
					thisDate.setFullYear(month.getFullYear(),month.getMonth(),counter);
					if(this.checkIfEarly(thisDate)){
						this.setText(obj,counter.toString(),'calDayGray');}
					else
						this.setText(obj,counter.toString(),'calDay');
				}
				counter++;
			}
			
			weekNumber=this.getDayObj(weekobj,'WeekNumber');
			if(weekNumber!=null){
				if(o==1)		// If no empty row then print week number
					this.setText(weekNumber,'','calDay');
				else
					this.setText(weekNumber,startWeek,'calDayWeek');
			}
			//clear after
			for(;o<=7;o++){
				obj=this.getDayObj(weekobj,'Day'+o);
				if(obj!=null){
					this.setText(obj,'','calDayEmpty');
				}							
			}									
			startday=1;
			weekcounter++;
			startWeek++;
		}
		this.updateSelections();
	}
		
	PCM.prototype.updateSelections = function(){
		weekcounter=1;
		var same=false;
		var sDate=this.inputFunction();
		var parts=sDate.split('-');
		if(parts.length==3)
			if(parts[0]==this.date.getFullYear() && parts[1]==this.date.getMonth()+1)
				same=true;

		while(weekcounter<=6){
			weekobj=this.getCalendarObj('Week'+weekcounter);

			for(var o=1;o<=7;o++){
				obj=this.getDayObj(weekobj,'Day'+o);
				if(obj!=null){
					cName=this.getClassName(obj);
					if (cName!='calDayEmpty' && cName!='calDayGray'){
						if(this.getText(obj)==this.date.getDate() && same)
							this.setClassName(obj,'calDaySelected');
						else 
							this.setClassName(obj,'calDay');
					}
				}
			}			
			weekcounter++;
		}
	}

	PCM.prototype.checkIfEarly = function (date){
        if(this.freeSelection)
            return false;
	
		if(date<this.cutoffDate)
			return true;
		else
			return false;
	}
	
	PCM.prototype.renderMonthName = function(date){
		var thisdate=new Date;
		thisdate.setFullYear(date.getFullYear(),date.getMonth(),date.getDate());
		obj=this.getCalendarObj('lblFromMonth');
		
		var offs=thisdate.getMonth();
		if(offs<0) offs=11;
		if(obj.innerText != undefined)	
			obj.innerText=MonthNames[offs] + ' ' + thisdate.getFullYear();
		else if(obj.textContent != undefined)
			obj.textContent=MonthNames[offs] + ' ' + thisdate.getFullYear();
	}
		
	PCM.prototype.getWeek = function(year,month,day){
		month += 1; 
		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;      
	    
		var d4 = (jd+31741-(jd%7))%146097%36524%1461;
		var L = Math.floor(d4/1460);
		var d1 = ((d4-L)%365)+L;
		NumberOfWeek = Math.floor(d1/7) + 1;
		return NumberOfWeek;        
	}

	PCM.prototype.TestFunction = function (e, cutoffDate){
		if (!e) var e = window.event;
		e.returnValue=false;
		e.cancelBubble=true;
		if(this.getMainTable().style.visibility == 'visible')
			return;
			
	//	this.date.setFullYear(2006,05,01);

		var time=this.inputFunction().split('-');
		if (time!=null && time!="")
			this.date.setFullYear(parseInt(parseFloat(time[0]),10),parseInt(parseFloat(time[1]),10)-1,parseInt(parseFloat(time[2]),10));
		else{
		    this.date=new Date();
        }

		if(cutoffDate!=null){
			var parts=cutoffDate.split('-');
			if(parts.length==3)
				if (IsNumeric(parts[0]) && IsNumeric(parts[1]) && IsNumeric(parts[2]))
					this.cutoffDate.setFullYear(parts[0],parts[1]-1,parts[2]);
		}
		else if(this.getSourceTextBox()!=null){
			var parts=this.getSourceTextBox().value.split('-');
			if(parts.length==3)
				if (IsNumeric(parts[0]) && IsNumeric(parts[1]) && IsNumeric(parts[2]))
					this.cutoffDate.setFullYear(parts[0],parts[1]-1,parts[2]);			
		}
		obj=this.getTextBox();
		if(obj!=null){
			var parts=obj.value.split('-');
			if(parts.length==3)
				if (IsNumeric(parts[0]) && IsNumeric(parts[1]) && IsNumeric(parts[2]))
					this.date.setFullYear(parts[0],parts[1]-1,parts[2]);				
		}
		if(this.checkIfEarly(this.date)){
			this.date.setFullYear(this.cutoffDate.getFullYear(),this.cutoffDate.getMonth(),this.cutoffDate.getDate());
		}

		this.PositionElement() ;
		// Hide visibility list objects
		for(ctrl in this.VisibleObjects){
			obj=getObj(this.VisibleObjects[ctrl]);
			if(obj != null)
				obj.style.visibility = 'hidden' ;
		}

		var Main = this.getMainTable();
		Main.style.visibility='visible';
		var date=new Date;
		
		obj=this.getCalendarObj('Week1');
		if(obj!=null)
			this.renderWeeks(this.date);
			
		this.renderMonthName(this.date);

		// Setup eventhandlers
		var myref=this;		

		obj=this.getCalendarObj('CalPilForw');
		obj.onclick = function(){myref.addMonth(1)};
		obj=this.getCalendarObj('CalPilBack');
        obj.onclick = function(){myref.addMonth(-1)};
		obj=this.getCalendarObj('calClose');
        obj.onclick=function (){myref.closeDown()};
		obj=this.getCalendarObj('PupupCalendar');
        obj.onclick=function (e){myref.clickUpdateDate(e);};

		
	}
	
	PCM.prototype.clickUpdateDate = function(e){
		if (!e) var e = window.event;
    
        var uniName=null;
        if(e.target != undefined)
            uniName=e.target;
        else if(e.srcElement != undefined)
            uniName=e.srcElement;
            
        if(uniName != null){
            if(uniName.name != undefined){
		        if(uniName.name.indexOf('Day')>=0){			
			        year=this.date.getFullYear();
			        month=this.date.getMonth()+1;
			        if(month<10)
				        month='0'+month;
			        day=uniName.value;
			        cl=uniName.className;
			        if (day!='' && cl!='calDayGray'){
				        if(day<10)
					        day='0'+day;
				        this.date.setDate(day);
				        this.updateSelections();
				        this.outputFunction(year, month, day);
			        }
			        this.closeDown();
		        }
	        }
	    }  
	}
	
	PCM.prototype.closeDown = function (e){
		if (!e) var e = window.event;
		if(this.getMainTable() != null){
    		this.getMainTable().style.visibility='hidden';
		}
		//Show the hidden ones
		for(ctrl in this.VisibleObjects){
			obj=getObj(this.VisibleObjects[ctrl]);
			if(obj != null)
				obj.style.visibility = 'visible' ;
		}

		return;
	}
	
	PCM.prototype.addMonth = function(numbers){
		this.date.setMonth(this.date.getMonth()+numbers);
		this.renderWeeks(this.date);
		this.renderMonthName(this.date);
	}
	
	PCM.prototype.getDaysInMonth = function(month){
		var date = new Date;
		date.setFullYear(month.getFullYear(),month.getMonth(),month.getDate());
		date.setDate(31);
		if(date.getDate()==31) return 31;
		else return (31-date.getDate());
	}
	
	PCM.prototype.GetLeftPosition = function(currentObj){
		var leftPos = 0;
		
		while (currentObj != null && currentObj.tagName != "BODY")
		{
			leftPos += currentObj.offsetLeft;
			currentObj = currentObj.offsetParent;        
		}

		return leftPos;
	}

	PCM.prototype.GetTopPosition = function(currentObj){
		var topPos = 0;

        while (currentObj != null && currentObj.tagName != "BODY")					
        {
			topPos += currentObj.offsetTop;
			currentObj = currentObj.offsetParent;
		}

		return topPos;
	}
	
	PCM.prototype.PositionElement = function(){
		var calendar = this.getMainTable(); 

		if( calendar != null ){
			calendar.style.top = this.GetTopPosition(this.getReferenceElement())+16+'px';
			calendar.style.left = this.GetLeftPosition(this.getReferenceElement())+'px';
        
		}
	}
	
	
	/********************/
	
    function updateDateDiff(obj1,obj2,destinationObj,countrycode){
		obj1=getObj(obj1);
		obj2=getObj(obj2);
		destinationObj=getObj(destinationObj);
		
		var date=getDateDiff(obj1,obj2,countrycode);
		if (destinationObj.innerText != undefined)
			destinationObj.innerText = date;
		else if (destinationObj.innerHTML != undefined)
			destinationObj.innerHTML = date;			
	}

    function updateDateDiffByPCM(obj1,obj2,destinationObj,countrycode){
		destinationObj=getObj(destinationObj);
		
		var date=getDateDiffByPCM(obj1,obj2,countrycode);
		if (destinationObj.innerText != undefined)
			destinationObj.innerText = date;
		else if (destinationObj.innerHTML != undefined)
			destinationObj.innerHTML = date;			
	}
	
    function getDateDiffByPCM(obj1,obj2,countrycode){
        
        var fromdate = new Date();
        var todate = new Date();
        
		var time=obj1.inputFunction().split('-');
		if (time!=null && time!="")
			fromdate.setFullYear(parseInt(parseFloat(time[0]),10),parseInt(parseFloat(time[1]),10)-1,parseInt(parseFloat(time[2]),10));
        
        time=obj2.inputFunction().split('-');
		if (time!=null && time!="")
			todate.setFullYear(parseInt(parseFloat(time[0]),10),parseInt(parseFloat(time[1]),10)-1,parseInt(parseFloat(time[2]),10));
        
		return Math.ceil((todate-fromdate)/(1000*24*60*60));			

	}

	
	/**********************/
