/* <![CDATA[ */
function JPCalendar(){

    this.monthNames=new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    this.dayNames=new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
    
    this.updateCalendarLink;
    this.selectMonthControl="";
    this.selectYearControl="";
    this.displayMonthControl="";
    this.displayYearControl="";
    this.calendarDivName="";
    this.parentControl="";
    this.calendarDisplayDiv="";
    this.noneSelectedText="None Selected";
    this.displayDateSeperator=" - ";
    this.showHover=false;
    this.calendarPrefix="";
    this.popupOffset=25;
    this.todayTemplate="";
    this.blankDayTemplate="";
    this.emptyDayTemplate="";
    this.eventDayTemplate="";
    this.displayTemplate="";
    this.footerTemplate="";
    this.headerTemplate="";
    this.weekendTemplate="";
    this.selectedDayTemplate="";
    this.currentDay=1;
    this.currentMonth=0;
    this.currentYear=1;
    this.dateOffset=1;
    this.monthDisplayOffset=1;
    this.dateOutputType=0;
    this.hiddenParent="";    
    this.displayType=0;
    this.isShowing=false;
    this.events=new Array();
    this.selectedDays=new Array();
    this.isMultiSelect=false;
    this.monthsToDisplay=1;
    this.startYear=2000;
    this.endYear=2030;
    
    this.startUp=function(){
        if(!this.currentYear){
            this.currentYear==1;this.currentMonth==0;this.currentDay==1;
        }
        if(this.currentYear==1){
            this.currentDate=new Date();
            this.currentYear=this.currentDate.getFullYear();
            this.currentMonth=this.currentDate.getMonth();
            this.currentDay=this.currentDate.getDate();
        }else{        
            this.currentDate=new Date(this.currentYear,this.currentMonth,this.currentDay);
        }
        this.currentYearSelected=this.currentYear;
        this.currentMonthSelected=this.currentMonth;
        this.getDaysInMonth(this.currentMonth,this.currentYear);
        
        //if(this.isMultiSelect==true){
        //    this.toggleSelectedDay(this.currentDay,this.currentMonth,this.currentYear);
        //}       
        if(this.selectYearControl==""){
	        this.selectYearControl=this.calendarPrefix+"SelectYear";
	    }
        if(this.selectMonthControl==""){
	        this.selectMonthControl=this.calendarPrefix+"SelectMonth";
	    }	    
        if(this.calendarDisplayDiv==""){
	        this.calendarDisplayDiv=this.calendarPrefix+"Display";
	    }
        if(this.parentControl==""){
	        this.parentControl=this.calendarPrefix+"DisplayDate";
	    }
        if(this.displayMonthControl==""){
            this.displayMonthControl=this.calendarPrefix+"DisplayMonth";
        }
        if(this.displayYearControl==""){
            this.displayYearControl=this.calendarPrefix+"DisplayYear";
        }        
        if(this.calendarDivName==""){
            this.calendarDivName=this.calendarPrefix+"Calendar";
        }        
        
        if(this.hiddenParent && this.hiddenParent!=""){
		    var objHidden = document.getElementById(this.hiddenParent)
		    if(objHidden){
			    this.currentDate=this.getDate(objHidden.value);
			    this.currentDay=this.currentDate.getDate();
			    this.currentMonth=this.currentDate.getMonth();
			    this.currentYear=this.currentDate.getFullYear();
			    this.currentYearSelected=this.currentYear;
			    this.currentMonthSelected=this.currentMonth;
		    }
        }
        this.displayDays();
        this.displayCurrentDate();
    }
    this.displayCurrentDate=function(){
        if(this.isMultiSelect==true){
            var output="";
            if(this.selectedDays.length==0){
                output=this.noneSelectedText;
            }else{
                for(x=0;x<this.selectedDays.length;x++){
                    if(output.length>0){
                        output+=this.displayDateSeperator;
                    }
                    output+=this.displayDate(new Date(this.selectedDays[x].year,this.selectedDays[x].month,this.selectedDays[x].day));
                }
            }
            this.setControlValue(this.parentControl,output);
        }else{
            this.setControlValue(this.parentControl,this.displayDate(this.currentDate));
        }
    }
    this.setDate=function(newDate){
        if(newDate){
            this.currentDate=newDate;
            this.currentYear=this.currentDate.getFullYear();
            this.currentMonth=this.currentDate.getMonth();
            this.currentDay=this.currentDate.getDate();
            this.currentYearSelected=this.currentYear;
            this.currentMonthSelected=this.currentMonth;
            this.getDaysInMonth(11,this.currentYear);
            this.displayCurrentDate();
            
            this.showHeader();
            this.displayDays();
        }
    }
    this.showHeader=function(){
        var yearDiv=document.getElementById(this.selectYearControl);
        var monthDiv=document.getElementById(this.selectMonthControl);
        
        if(yearDiv){
            if(!yearDiv.options[0]){
                for(i=this.startYear;i<this.endYear+1;i++){
                    yearDiv.options[i-this.startYear] = new Option(i,i);
                }
            }
        
            for(x=0;x<yearDiv.options.length;x++){
                if(parseInt(yearDiv.options[x].value)==this.currentYearSelected){
                    yearDiv.options[x].selected=true;
                    yearDiv.selectedIndex=x;
                    break;
                }
            }
        }       
        if(monthDiv){
            if(!monthDiv.options[0]){
                for(i=0;i<12;i++){
                    monthDiv.options[i] = new Option(this.monthNames[i],i);
                }            
            }
           monthDiv.selectedIndex=this.currentMonthSelected;
        }
    }
    this.showCalendar=function(){
        if(this.isShowing==false){
            var dateDiv=document.getElementById(this.parentControl);

		    if(dateDiv){
		        if(dateDiv.value){
			        this.currentDate=this.getDate(dateDiv.value);
			    }else{
			        this.currentDate=this.getDate(dateDiv.innerHTML);
			    }
			    this.currentDay=this.currentDate.getDate();
			    this.currentMonth=this.currentDate.getMonth();
			    this.currentYear=this.currentDate.getFullYear();
			    this.currentYearSelected=this.currentYear;
			    this.currentMonthSelected=this.currentMonth;
		    }
            this.showMe();
        }else{
            this.hideMe();
        }

    }
    this.yearChange=function(newYear){
        this.currentYearSelected=parseInt(newYear);
        this.displayDays();
    }
    this.monthChange=function(newMonth){
        this.currentMonthSelected=parseInt(newMonth);
        this.displayDays();
    }
    this.updateHiddenValues=function(){

        this.setControlValue(this.calendarPrefix+"ListDays",this.selectedDaysToString());
        this.setControlValue(this.calendarPrefix+"Year",this.currentYear);
        this.setControlValue(this.calendarPrefix+"Month",this.currentMonth+1);
        this.setControlValue(this.calendarPrefix+"Day",this.currentDay);
        this.setControlValue(this.parentControl,this.displayDate(this.currentDate))
        
    }
    this.selectDay=function(newDay,newMonth,newYear){
        this.currentDate=new Date(newYear,newMonth,newDay);        
        this.currentYear=newYear;this.currentMonth=newMonth;this.currentDay=newDay;        
        if(this.displayType==3){
            this.currentYearSelected=newYear;this.currentMonthSelected=newMonth;
        }
       
        this.updateHiddenValues();
        
        if(this.updateCalendarLink){
            this.updateCalendarLink();
        }        
        if(this.isMultiSelect==true){
            this.toggleSelectedDay(newDay,newMonth,newYear);
        } 
        if(this.displayType>2 && this.isMultiSelect==false){
            this.displayCurrentDate();
            this.hideMe()
            
        }else{
            this.displayCurrentDate();
            this.displayDays();
        }
    }
    this.previousMonth=function(){
        var newMonth=this.currentMonthSelected-1
        if(newMonth<0){
            newMonth=11;
            this.currentYearSelected-=1;
        }
        this.currentMonthSelected=newMonth;
        this.displayDays();
    }
    this.nextMonth=function(){
        var newMonth=this.currentMonthSelected+1
        if(newMonth>11){
            newMonth=0;
            this.currentYearSelected+=1;
        }
        this.currentMonthSelected=newMonth;
        this.displayDays();
    }
    this.previousYear=function(){
        this.currentYearSelected-=1;this.displayDays();
    }
    this.nextYear=function (){
        this.currentYearSelected+=1;this.displayDays();
    }
    this.displayDays=function(){
        this.setControlValue(this.displayMonthControl,this.monthNames[(this.currentMonthSelected)+this.monthDisplayOffset]);
        this.setControlValue(this.displayYearControl,this.currentYearSelected);          
        
        var calendarDiv=document.getElementById(this.calendarDivName);
        
        if(calendarDiv){            
            if(this.displayTyp<3){
                calendarDiv.className="calendar";
            }
            calendarDiv.innerHTML=this.headerTemplate;

            if(this.displayType==0 || this.displayType==3){
            
                var temp="";
                temp=this.displayTemplate;
                temp=this.updateDisplay(this.currentMonthSelected,this.currentYearSelected,temp);
                temp=this.replaceAll(temp,"$DISPLAYMONTH$",this.monthNames[this.currentMonthSelected+this.monthDisplayOffset]);
                temp=this.replaceAll(temp,"$DISPLAYYEAR$",this.currentYearSelected);
                calendarDiv.innerHTML+=temp;                                                       
            }else if(this.displayType==1 || this.displayType==4){
                var temp="";var x=0;var total=(this.monthsToDisplay*2)+1;
                var tempDate=new Date(this.currentYearSelected,this.currentMonthSelected,1);
                tempDate.setMonth(tempDate.getMonth() - this.monthsToDisplay);
                
                for(x=0;x<total;x++){
                    temp=this.displayTemplate;
                    temp=this.updateDisplay(tempDate.getMonth(),tempDate.getFullYear(),temp);
                    temp=this.replaceAll(temp,"$DISPLAYMONTH$",this.monthNames[tempDate.getMonth()+this.monthDisplayOffset]);
                    temp=this.replaceAll(temp,"$DISPLAYYEAR$",tempDate.getFullYear());
                    calendarDiv.innerHTML+=temp;
                    tempDate.setMonth(tempDate.getMonth()+1);
                }                
            }else if(this.displayType==2 || this.displayType==5){
                var temp="";var x=0;                
                for(x=0;x<12;x++){
                    temp=this.displayTemplate;
                    temp=this.updateDisplay(x,this.currentYearSelected,temp);
                    temp=this.replaceAll(temp,"$DISPLAYMONTH$",this.monthNames[x+this.monthDisplayOffset]);
                    temp=this.replaceAll(temp,"$DISPLAYYEAR$",this.currentYearSelected);
                    calendarDiv.innerHTML+=temp;                    
                }                
            }
            calendarDiv.innerHTML+=this.footerTemplate;        
        }
        this.showHeader();
    }
    this.updateDisplay=function(month,year,divText){
        
        var displayText = ""; 
        var tempValue="";
        var dataIndex=false;               
        var dayCounter=0;var daysInMonth=31;var isSelectedDay=false;
        var firstDate=new Date(year,month,1);        
        var tempDate=new Date(year,month,1);
        var dayType=0;        
        if(this.currentMonthSelected==1){
            daysInMonth=this.getDaysInFebruary(year);
        }else{
            daysInMonth=this.getDaysInMonth(month,year);
        }
        var x=0;
        for(x=1;x<43;x++){
            if(dayCounter==0){
                if(firstDate.getDay()==x || x>6){
                    dayCounter=1;
                }else{
                    displayText+=this.blankDayTemplate;
                }
            }
            if(dayCounter>0){
                if(dayCounter>daysInMonth){
                    displayText+=this.blankDayTemplate;
                }else{
                    tempValue=this.emptyDayTemplate
                    tempDate=new Date(year,month,dayCounter);
                    dayType=tempDate.getDay();
                    
                    if(dayType==0 || dayType==6){
                        if(this.weekendTemplate!=""){
                            tempValue=this.weekendTemplate;
                        }
                    }                    
                    dataIndex=this.checkDayForEvents(dayCounter,month,year);
                    
                    if(this.isMultiSelect==true){
                        isSelectedDay=this.checkIfSelectedDay(dayCounter,month,year);
                    }
                    if(dataIndex == true){
                        tempValue=this.eventDayTemplate;
                        tempValue=this.replaceAll(tempValue,"$EVENT$",(dayCounter+","+month+","+year));
                    }else{
                        toDay=new Date();                                                                                         
                        if(dayCounter==toDay.getDate() && toDay.getMonth()==month && toDay.getFullYear()==year){
                            tempValue=this.todayTemplate;
                        }
                        if(this.isMultiSelect==true){
                            if(isSelectedDay==true){
                                tempValue=this.selectedDayTemplate;
                            }                            
                        }else{
                            if(this.currentDay==dayCounter && this.currentMonth==month && this.currentYear==year){
                                tempValue=this.selectedDayTemplate;
                            }                            
                        }
                    }
                    if(tempValue==""){
                        tempValue=this.emptyDayTemplate;
                    }
                    tempValue=this.replaceAll(tempValue,"$DATE$",(dayCounter+","+month+","+year));
                    tempValue=this.replaceAll(tempValue,"$INDEX$",dayCounter);
                    displayText+=tempValue;
                    dayCounter+=1;
                }
            }
        }       
        displayText=this.replaceAll(divText,"$DISPLAY$",displayText);  
        return displayText;
    }
    this.getDaysInFebruary=function (year){return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}
    this.getDaysInMonth=function(n,year){
        n+=1;	    
	    var days=31;
	    if (n==4 || n==6 || n==9 || n==11) {days=30}
        return days
    }
    this.showMe=function(){   
        if(this.displayType<3){
            return;
        }
        var parentElement=document.getElementById(this.parentControl);
        var calendarDiv=document.getElementById(this.calendarDivName);
  
        if(calendarDiv && parentElement){
            this.displayDays();  
                  
            var pos = this.findPosition(parentElement);
            calendarDiv.style.left=pos[0]+'px';
            calendarDiv.style.top=pos[1]+'px';
            calendarDiv.style.zIndex = 1000;
            calendarDiv.className="calendaron";
            this.isShowing=true;
            //this.addEvent(document,"mousedown",this.mouseUpHandlder,true);            
        }
    }
    this.hideMe=function(){
        if(this.displayType<3){return;}
        	
        var calendarDiv=document.getElementById(this.calendarDivName);
        if(calendarDiv){
            calendarDiv.innerHTML="";
            //this.removeEvent(document,"mousedown",this.mouseUpHandlder,true);
            calendarDiv.className="calendaroff";
            this.isShowing=false;    
        }
    }
     this.mouseUpHandlder=function(e) {
        if (!e) e = window.event;
        var elementName
	    if(e.srcElement){    	
		    elementName = e.srcElement.id.toUpperCase();
	    }
	    else{
		    elementName = e.target.id.toUpperCase();
	    }
	    if(elementName.substring(0,this.calendarPrefix.length) == this.calendarPrefix){
		    return;
	    }	
	    this.hideMe();
    }    
    this.addEvent=function(obj, evType, fn, useCapture){
      if (obj.addEventListener){
        obj.addEventListener(evType, fn, useCapture);
        return true;
      }else if(obj.attachEvent){
        var r=obj.attachEvent("on"+evType, fn);
        return r;
      }
    }
    this.removeEvent=function(obj, evType, fn, useCapture) {
      if (obj.removeEventListener){
        obj.removeEventListener(evType, fn, useCapture);
        return true;
      } else if (obj.detachEvent){
        var r=obj.detachEvent("on"+evType, fn);
        return r;
      }
    }
    this.findPosition=function(obj){
	    var curleft=0;var curtop=0;
	    if (obj.offsetParent) {
		    curleft=obj.offsetLeft
		    curtop=obj.offsetTop
		    while(obj=obj.offsetParent) {
			    curleft+=obj.offsetLeft
			    curtop+=obj.offsetTop
		    }
	    }
        curtop+=this.popupOffset;
	    return [curleft,curtop];
    }
    this.getDate=function(value){
        var newDate=new Date();
        var day;var month;var year;
        if(value && value!=''){
            var temp = new Array();
            temp = value.split('/');
            if(temp.length==3){
                year=parseInt(temp[2]);
                month=parseInt(temp[1]); 
                day=parseInt(temp[0]);
            }
            else{
			    temp=value.split(' ');
			    if(temp.length==3){
                    year=parseInt(temp[2]);
                    month=findMonth(temp[1]);
                    day=parseInt(temp[0]);                    			
			    }
                if(temp.length==4){
                    year=parseInt(temp[3]);
                    month=findMonth(temp[2]);
                    day=parseInt(temp[1]);
                }            			            
            }
        }      
        if(day && month && year && day>0 && day<32 && month>0 && month<13){
            newDate = new Date(year,month-1,day);
            return newDate;
        }else{
            return this.currentDate;
        }    
    }
    this.findMonth=function(month){
        var x=0;
	    for(x=0;x<this.monthNames.length;x++){
		    if(this.monthNames[x].toLowerCase()==month.toLowerCase()){
		      if(x>11){x-=12};
		      return x;
		    }
	    } 
	    return 0;
    }
    this.displayDate=function (date){
        if(this.dateOutputType==0){
            return date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear();
        }
        if(this.dateOutputType==1){
            return date.getDate()+" "+this.monthNames[date.getMonth()+12]+" "+date.getFullYear();
        }    
        if(this.dateOutputType==2){
            return date.getDate()+" "+this.monthNames[date.getMonth()]+" "+date.getFullYear();
        }    
        if(this.dateOutputType==3){
            return this.dayNames[date.getDay()+7]+" "+date.getDate()+" "+this.monthNames[date.getMonth()+12]+" "+date.getFullYear();
        }    
        if(this.dateOutputType==4){
            return this.dayNames[date.getDay()]+" "+date.getDate()+" "+this.monthNames[date.getMonth()]+" "+date.getFullYear();
        }   
    }
    this.replaceAll=function(checkMe,repMe,repWith){
        var temp=checkMe;
        var i=temp.indexOf(repMe);
        while(i > -1){
            temp=temp.replace(repMe, repWith);
            i=temp.indexOf(repMe);
        }
        return temp; 
    }
    function CalendarDays(data){
	    this.day=0;
	    this.month=0;
	    this.year=0;
    
        if(data && data!=""){
	        var list=data.split(",");
	        if(list.length==3){
		        this.day=parseInt(list[0]);
		        this.month=parseInt(list[1])-1;
		        this.year=parseInt(list[2]);    		    
	        }
	    }
    }
     this.createCalendarSelectedDays=function(data){
        var itemDays=new Array();
        if(data && data!=""){
            var listItems=data.split("|");
            var x=0;
            for(x=0; x<listItems.length; x++){
                if(listItems[x]!="" && listItems[x].length>2){
                    itemDays[itemDays.length]=new CalendarDays(listItems[x])                
                }
            }
        }
        
        this.selectedDays=itemDays;        
    }       
     this.createCalendarData=function(data){
     if(data!="")
     {
        this.events=JSON.parse(data);
     }
        else{this.events=new Array();
        }
    }
    this.toggleSelectedDay=function(day,month,year){
        var newSelectedDay=new CalendarDays();
        var isSelected=-1;
        
        newSelectedDay.day=day;
        newSelectedDay.month=month;
        newSelectedDay.year=year;
            
        if(this.selectedDays.length==0){
            this.selectedDays[0]=newSelectedDay;
            return;
        }
        var x=0;
        for(x=0;x<this.selectedDays.length;x++){
            if(this.selectedDays[x].day==day && this.selectedDays[x].month == month && this.selectedDays[x].year==year){
                isSelected=x;
            }
        }
        if(isSelected>-1){
            this.selectedDays.splice(isSelected,1);
        }else{
            this.selectedDays[this.selectedDays.length]=newSelectedDay;
        }
        this.updateHiddenValues();
    }
    this.checkIfSelectedDay=function(day,month,year){
        if(this.selectedDays.length==0){
            return -1;
        }
        var x=0;
        for(x=0;x<this.selectedDays.length;x++){
            if(this.selectedDays[x].Day==day && this.selectedDays[x].Month==month && this.selectedDays[x].Year==year){
                return true;
            }
        }
        return false;
    }
    this.selectedDaysToString=function(){
        var returnValue="";
        if(this.selectedDays.length==0){
            return "";
        }else{
            var x=0;
            for(x=0;x<this.selectedDays.length;x++){
                if (returnValue.length>0){
                    returnValue+="|";
                }
                returnValue+=this.selectedDays[x].Day+","+(this.selectedDays[x].Month+1)+","+this.selectedDays[x].Year;
            }    
            return returnValue;
        }
    }
    this.checkDayForEvents=function(day,month,year){
        if(this.events.length==0){
            return -1;
        }
        var x=0;
        for(x=0;x<this.events.length;x++){
            if(this.events[x].Day==day && this.events[x].Month==month + 1 && this.events[x].Year==year){
                return true;
            }
        }
        return false;
    }
    this.getEventsForDay=function(day,month,year){
        var returnData=new Array();
        
        if(this.events.length==0){
            return returnData;
        }
        
        var x=0;
        for(x=0;x<this.events.length;x++){
            if(this.events[x].Day==day && this.events[x].Month==month + 1 && this.events[x].Year==year){
                returnData[returnData.length]=this.events[x];
            }
        }
        return returnData;
    }
    this.setControlValue=function(controlName,newValue){
        var control=document.getElementById(controlName);
        
        if(control){
            if(control.nodeName=="DIV"){
                control.innerHTML=newValue;
                return;   
            }
            if(control.nodeName=="INPUT"){
                control.value=newValue;                
            }
        }   
    }
    this.setDivValue=function(controlName,newValue){
        var control=document.getElementById(controlName);        
        if(control && control.innerHTML){
            control.innerHTML=newValue;
        }   
    }
}
/* ]]> */