﻿AvailabilityHandler = function(ciid,coid,firstDate)
{
	this.ciDt = null;
	this.coDt = null;
	this.ciFN = ciid;
	this.coFN = coid;
	this.fDt  = firstDate;	
    this.lDt  = new Date(firstDate.getFullYear()+1,firstDate.getMonth(),1);

	this.clearable = false;
	this.cimydd = $(ciid+"my");
	this.comydd = $(coid+"my");
	this.ciddd = $(ciid+"dy");
	this.coddd = $(coid+"dy");
	this.handlersNotSet = true;
};

/////////////////////////////////////////////
// INSTANCE FUNCTIONS
/////////////////////////////////////////////
AvailabilityHandler.prototype.setValidCIDate=function(dt,y,m,d,addOneLast)
{
    var cdt = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate());
	nowDiff = cdt.dayDiff(this.fDt);
	lstDiff = cdt.dayDiff(this.lDt)-1;
	oneLast = (addOneLast)?0:1;
	if((nowDiff>0)||(lstDiff<oneLast)){
		return true;
	}
	return false;
};

AvailabilityHandler.prototype.setValidCODate=function(dt,y,m,d)
{
	if(this.ciDt){
		return (this.setValidCIDate(dt,y,m,d,true)?true:(dt.dayDiff(this.ciDt)>-1));
	}else{
		return (this.setValidCIDate(dt,y,m,d,true)?true:(dt.dayDiff(this.fDt)>-1));
	}
};

AvailabilityHandler.prototype.updateCIDropdowns=function(){
	if(this.ciDt){
		AvailabilityHandler.setDropdown(this.cimydd,this.ciddd,this.ciDt,this.clearable);
	}
};

AvailabilityHandler.prototype.updateCODropdowns=function(){
	if(this.coDt){
		AvailabilityHandler.setDropdown(this.comydd,this.coddd,this.coDt,this.clearable);
	}
};

AvailabilityHandler.prototype.handleCIDChange=function(){
    if((""==this.ciddd.value)&&(""!=this.cimydd.value)){
        this.clearDates();
    }else{
        if(""==this.cimydd.value){
            this.cimydd.selectedIndex=1;
        }
        this.updateFromCIChange();
    }
};

AvailabilityHandler.prototype.handleCIYChange=function(){
    if((""==this.cimydd.value)&&(""!=this.ciddd.value)){
        this.clearDates();
    }else{
        if(""==this.ciddd.value){
            this.ciddd.selectedIndex=1;
        }
        this.updateFromCIChange();
    }
};

AvailabilityHandler.prototype.updateFromCIChange=function(){
	this.ciDt=AvailabilityHandler.getValidDate(this.cimydd.value,this.ciddd.value,this.fDt,this.lDt);
	this.updateCIDropdowns();
	if(this.coDt&&(this.ciDt.dayDiff(this.coDt)>0)){
	   //nothing
	}else{
		this.coDt=new Date(this.ciDt.getTime()+Date.DAY);
		this.updateCODropdowns();
	}
};

AvailabilityHandler.prototype.handleCOYChange=function(){
    if((""==this.comydd.value)&&(""!=this.coddd.value)){
        this.clearDates();
    }else{
        if(""==this.coddd.value){
            this.coddd.selectedIndex=1;
        }
        this.updateFromCOChange();
    }
};

AvailabilityHandler.prototype.handleCODChange=function(){
    if((""==this.coddd.value)&&(""!=this.comydd.value)){
		this.clearDates();
	}else{
	    if(""==this.comydd.value){
	        this.comydd.selectedIndex=1;
	    }
	    this.updateFromCOChange();
	}
};

AvailabilityHandler.prototype.updateFromCOChange=function(){
    if((""==this.cimydd.value)||(""==this.ciddd.value)){
    	this.ciDt=AvailabilityHandler.getValidDate(this.comydd.value,this.coddd.value,this.fDt,this.lDt);
   		this.coDt=AvailabilityHandler.getValidDate(this.comydd.value,this.coddd.value,new Date(this.ciDt.getTime()),this.lDt);
    }else{
    	this.coDt=AvailabilityHandler.getValidDate(this.comydd.value,this.coddd.value,new Date(this.ciDt.getTime()+Date.DAY),this.lDt);
    }
    
	this.updateCODropdowns();
	
    if((""==this.cimydd.value)||(""==this.ciddd.value)){
    	this.ciDt= new Date(this.coDt.getTime()-Date.DAY);
    	this.updateCIDropdowns();
    }
};

AvailabilityHandler.prototype.initialize=function(coDate){
	if(this.handlersNotSet){
		this.cimydd._ahThis = this;
		this.ciddd._ahThis = this;
		this.comydd._ahThis = this;
		this.coddd._ahThis = this;
		this.cimydd.onchange=function(){this._ahThis.handleCIYChange();};
		this.ciddd.onchange=function(){this._ahThis.handleCIDChange();};
		this.comydd.onchange=function(){this._ahThis.handleCOYChange();};
		this.coddd.onchange=function(){this._ahThis.handleCODChange();};
		handlersNotSet = false;
	}

	if(!this.clearable){
		this.ciDt = new Date(this.fDt.getFullYear(),this.fDt.getMonth(),this.fDt.getDate());    	
        	
	    if( coDate ){	
	        this.coDt = new Date(coDate.getTime());
		}else{
		    this.coDt = new Date(this.ciDt.getTime()+Date.DAY);
		}
				
		this.updateCIDropdowns();
		this.updateCODropdowns();
	}
};

AvailabilityHandler.prototype.clear=function()
{
    if(this.clearable)
    {
        this.ciDt = null;
        this.coDt = null;
    }
    else
    {
  		this.ciDt = new Date(this.fDt.getFullYear(),this.fDt.getMonth(),this.fDt.getDate());
		this.coDt = new Date(this.ciDt.getTime()+Date.DAY);
    }
    this.updateCIDropdowns();
    this.updateCODropdowns();
}

AvailabilityHandler.prototype.setCI=function(cal,dt){
	if(cal.dateClicked){
		this.ciDt=cal.date;
		this.updateCIDropdowns();
		if(this.coDt&&(this.ciDt.dayDiff(this.coDt)>0)){
			//nothing
		}else{
			this.coDt=new Date(this.ciDt.getTime()+Date.DAY);
			this.updateCODropdowns();
		}
		cal.hide();
	}
};

AvailabilityHandler.prototype.setCO=function(cal,dt){
	if(cal.dateClicked){
		this.coDt=cal.date;
		this.updateCODropdowns();
		this.updateFromCOChange();
		cal.hide();
	}
};

AvailabilityHandler.prototype.clearDates=function(){
	if(this.clearable){
		this.cimydd.selectedIndex=0;
		this.ciddd.selectedIndex=0;
		this.ciDt=null;
		this.comydd.selectedIndex=0;
		this.coddd.selectedIndex=0;
		this.coDt=null;
	}else{
		this.initialize();
	}
};

AvailabilityHandler.prototype.getEncodedCIDate=function(){
    mySetting = this.cimydd.options[this.cimydd.selectedIndex].value;
    if(mySetting==""){
        return "";
    }
    smySetting = mySetting.split(":");
    return smySetting[1]+":"+this.ciddd.options[this.ciddd.selectedIndex].value+":"+smySetting[0];
}

AvailabilityHandler.prototype.getEncodedCODate=function(){
    mySetting = this.comydd.options[this.comydd.selectedIndex].value;
    if(mySetting==""){
        return "";
    }
    smySetting = mySetting.split(":");
    return smySetting[1]+":"+this.coddd.options[this.coddd.selectedIndex].value+":"+smySetting[0];
}


///////////////////////////////////////////////
// STATIC FUNCTIONS
///////////////////////////////////////////////
AvailabilityHandler.setDropdown=function(dmy,dd,dt,clrble){
	dmy.value=dt.getFullYear()+":"+(dt.getMonth()+1);
	selIdx=dt.getDate()-1;
	if(clrble){
		selIdx=selIdx+1;
	}
	dd.selectedIndex = selIdx;
};

AvailabilityHandler.getValidDate=function(myv,dv,fd,ld){
	resmy = myv.split(":");
	checkDt = new Date(resmy[0],resmy[1]-1,1);
	maxMonthDays = checkDt.getMonthDays();
	if(maxMonthDays>=dv){
		checkDt = new Date(resmy[0],resmy[1]-1,dv);
	}else{
		checkDt=new Date(resmy[0],resmy[1]-1,maxMonthDays);
	}
	diffFd = checkDt.dayDiff(fd);
	diffLd = checkDt.dayDiff(ld);

	if(diffFd>0){
		return fd;
	}else if(diffLd<0){
		return ld;
	}else{
		return checkDt;
	}
};

////////////////////////////////////////////////
// POPUP CALENDAR
////////////////////////////////////////////////
function popupValidFutureOnly(dt,y,m,d){
    var now = new Date();
    var df = now.dayDiff(dt);
    if(df>-1){
        return false;
    }else{
        return true;
    }
//    return( (new Date()).dayDiff(dt) ) > -1 );
}

function popupCal(targId,imgId,futureOnly,dtFormat)
{
    var targ = $(targId);
    var imgIcon = $(imgId);
    if(targ&&imgIcon){
        var cal = new Calendar(0,targ.value,function(cal,dt){targ.value=dt;if(cal.dateClicked){cal.callCloseHandler();};},function(cal,dt){cal.hide();});
        if(dtFormat){
            cal.setDateFormat(dtFormat);
        }else{
            cal.setDateFormat("%m/%d/%Y");
        }
        cal.weekNumbers = false;
        cal.inpField = targ;
        cal.showOthers = true;
        if(futureOnly){
            cal.setDateStatusHandler(popupValidFutureOnly);
        }
        cal.create();
        cal.showAtElement(imgIcon,"bL");
    }
}

var addlPrintTags = new Hash();

function showPrintable(){
    var newUrl = window.location.href;
    if(newUrl.indexOf("?")==-1){
        newUrl = newUrl+"?printable=true&"+addlPrintTags.toQueryString();
    }else{
        newUrl = newUrl+"&printable=true&"+addlPrintTags.toQueryString();
    }
    window.open(newUrl,"_blank");
}

function doPopEmail()
{
    var winW = 0, winH = 0;
        
    if (document.body && document.body.offsetWidth) {
         winW = document.body.offsetWidth;
         winH = document.body.offsetHeight;
    }
    
    if (document.compatMode=='CSS1Compat' &&
        document.documentElement &&
        document.documentElement.offsetWidth ) {
        winW = document.documentElement.offsetWidth;
        winH = document.documentElement.offsetHeight;
    }
    
    if (window.innerWidth && window.innerHeight) {
        winW = window.innerWidth;
        winH = window.innerHeight;
    }       

    var w=380;
    var h=465;
    
    var left = (winW / 2) - (w/2);
    var top  = (winH / 2) - (h/2);

    var txtObj = document.getElementById('_st_sharedlg_location');
    if(txtObj)
    {   
        window.open(txtObj.value, "", "height=465,width=380,left=" + left + ",top=" + top + ",resizable=no,scrollbars=no");
    }
}         


function doPrint(){
    window.print();
}

function setPrintTags(ptag,pvalue){
    addlPrintTags[ptag]=pvalue;
}



