PRVD.APP.UC.DeliveryCalendar = function(htmlElementID)
{
	this.ElementID = htmlElementID;
	this.Element = document.getElementById(this.ElementID);
	this.IsDisabledForZip = false;
	this.CalendarElement = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendarelement")[0];
	this.CalendarPanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendarpopuppanel")[0];
	this.CalendarControl = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "deliverycalendar")[0];
    this.DeliveryDateText = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "deliverydateinputtextbox")[0];
    this.DeliveryDateHiddenText = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "deliverydatehiddentext")[0];
    this.DeliveryOptionsPanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "deliveryoptionspanel")[0];
    this.ClosePanelLink = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "ClosePanelButton")[0];
    this.CalendarButton = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button")[0];
    var elementID = this.CalendarElement.id.substring(0, this.CalendarElement.id.lastIndexOf('_')) + "_Calendar1";
    this.Calendar = window[elementID];
    this.ForceZipCollectionOnSundaySelection = PRVD.CT.CONFIG.ISZIPCODEREQUIREDONPRODUCTDETAILONLYFORVDAYDELIVERY; ;
    this.DeliveryInputDefaultValue = this.DeliveryDateText.value;
    this.DeliveryInputZipValue = "Please enter Zip Code above";
    this.FlexibleDeliveryPanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "pnlflexibledelivery")[0];
    var VdayDate = PRVD.CT.CONFIG.JSDATESTRINGTOTRIGGERSPECIALPANELINCALENDAR;

    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radflexibledate").length > 0)
        this.FlexibleDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radflexibledate")[0];
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasiondate").length > 0)
        this.OccassionDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasiondate")[0];
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasionorflexible").length > 0)
        this.FlexOrOccassionDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasionorflexible")[0];  
      
    var flexOrOccassionRadioSelected = false;
    var flexRadioSelected = false
    var occassionRadioSelected = false;
    var Vday = 'Mon Feb 14 2011'

    var editableFlagID = "isEditable_" + this.CalendarControl.id.substring(0, this.CalendarControl.id.lastIndexOf("_"));
    this.IsEditable = window[editableFlagID];
    
    if( this.IsEditable === undefined )
    {
		this.IsEditable = true;
    }
    
    /*public functions */
    
    this.SetInvisible = function()
    {
        this.Element.style.display = "none";
    };
    this.SetVisible = function()
    {
        this.Element.style.display = "inline";
    };
    
    this.RefreshDeliveryCalendar = function(customdateArray, disableddatesArray)
	{
        
		this.ChangeCalendarDeliveryDates(customdateArray);
		this.ChangeCalendarDisabledDates(disableddatesArray);
		this.Calendar.CellData=new Array();  
		this.Calendar.Render(); 
	};
	
	this.Reload = function(html)
	{
		  if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radflexibledate").length > 0)
		  {
                this.FlexibleDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radflexibledate")[0];
                flexRadioSelected = this.FlexibleDateRadioBtn.checked;
           }
            if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasiondate").length > 0)
            {
                this.OccassionDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasiondate")[0];
                occassionRadioSelected = this.OccassionDateRadioBtn.checked;
            }
            if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasionorflexible").length > 0)
            {
                this.FlexOrOccassionDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasionorflexible")[0];  
                flexOrOccassionRadioSelected = this.FlexOrOccassionDateRadioBtn.checked;
            }
	};
	
	this.GetSelectedDateFromCalendar = function()
	{
		if(this.DeliveryDateText.value == this.DeliveryInputDefaultValue || this.DeliveryDateText.value == this.DeliveryInputZipValue)
		    return "";
		else
		    return this.Calendar.FormatDate(this.Calendar.GetSelectedDate(), 'ddd, MMM dd, yyyy');	
	};
	this.DoesDateExistInCalendar = function(date)
	{
		return this.Calendar.CustomDays.Contains(new Date(date));
	};
	
	this.SetCalendarPositionRelativeToElement = function()
	{
		legendPanel = PRVD.CT.UT.GetElementsByPrvdName(this.CalendarPanel, 'LegendPanel')[0];
		if(legendPanel.childNodes && legendPanel.childNodes.length > 0 && legendPanel.childNodes[0].length > 0)
		{	
		    numSurcharges = (legendPanel.childNodes[0].childNodes.length + 1)/2;
		    var horizontalAdjustment = 185 + (20*numSurcharges);
		    if (this.FlexOrOccassionDateRadioBtn != null)
		    {
			    numSurcharges -= 1;
			    horizontalAdjustment = 285 + (20*numSurcharges);
		    }
		    positions = this.FindPosition(this.DeliveryDateText);
		    topPos = positions[1] - horizontalAdjustment;
    		
		    /* hack-solution added on 2/5/07	 */
		    if(navigator.userAgent.indexOf("Firefox") != -1) {
			    topPos -= 48;
		    }
    		
		    if( topPos < 0 ) {
		        topPos = 0;
		    }
		    this.CalendarPanel.style.top=topPos;		
		    this.CalendarPanel.style.posLeft=positions[0];
		    positions = null;
        }
	};
	
	 
	
	this.ReloadFlexPanel = function( html )
	{   
		if( this.FlexibleDeliveryPanel != undefined )
		{
			this.FlexibleDeliveryPanel.innerHTML = html;
			
			if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radflexibledate").length > 0)
			{
				this.FlexibleDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radflexibledate")[0];
				this.FlexibleDateRadioBtn.checked = flexRadioSelected;
		    }
			if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasiondate").length > 0)
			{
				this.OccassionDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasiondate")[0];
				this.OccassionDateRadioBtn.checked = occassionRadioSelected;
			}
			if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasionorflexible").length > 0)
			{
				this.FlexOrOccassionDateRadioBtn = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "radoccasionorflexible")[0];
				this.FlexOrOccassionDateRadioBtn.checked = flexOrOccassionRadioSelected;
			}
			 
			if( this.IsEditable )
			{
				PRVD.CT.EVENTS.Event.addListener(this.FlexibleDateRadioBtn, "click", this.OnRadioButtonClick, this, false);
				PRVD.CT.EVENTS.Event.addListener(this.OccassionDateRadioBtn, "click", this.OnRadioButtonClick, this, false);
				PRVD.CT.EVENTS.Event.addListener(this.FlexOrOccassionDateRadioBtn, "click", this.OnRadioButtonClick, this, false);
			}
		}
	};
	
	 this.IsVisible = function()
    {
        return this.Element.style.display != "none";
    }
 
	
 
	/* end public functions */
	
   
	this.ChangeCalendarDeliveryDates = function(dates)
	{
		var datelist = new Array(dates.length);
		for(i=0; i<dates.length; ++i)
		{
			datelist[i] = new Array(4);
			datelist[i][0] = dates[i].Date;
			datelist[i][1] = dates[i].CssClass;
			datelist[i][2] = dates[i].HoverCssClass;
			datelist[i][3] = dates[i].ActiveCssClass;
		}
		this.Calendar.CustomDays=new ComponentArt_CalendarDayCollection(datelist, this.Calendar);
	};
	this.ChangeCalendarDisabledDates = function(dates)
	{
		var datelist = new Array(dates.length);
		for(i=0; i<dates.length; ++i)
		{
			datelist[i] = dates[i].Date;
		}
		this.Calendar.DisabledDates=new ComponentArt_Calendar_DateCollection(datelist,this.Calendar)
	};
	
	this.FindPosition = function(obj)
	{
		curleft = 0;
		curtop = 0;
		if (obj.offsetParent)
		{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent)
			{
				curleft += 0;
				curtop += 0;
			}
		}
		return [curleft,curtop];
		curleft = null;
		curtop = null;
	};
	this.OnCalendarObjectChange = function(sender, eventArgs)
	{
	    this.ParentCustomJSObject.OnCalendarChange();
	};
	this.OnCalendarChange = function()
	{
		this.DeliveryDateText.value=this.Calendar.FormatDate(this.Calendar.GetSelectedDate(), 'ddd, MMM dd');
		this.DeliveryDateHiddenText.value=this.Calendar.FormatDate(this.Calendar.GetSelectedDate(), 'ddd, MMM dd, yyyy');
		this.HideCalendarPanel();
		
		if (this.FlexibleDateRadioBtn != null)
			this.FlexibleDateRadioBtn.checked = false;
		
		if (this.OccassionDateRadioBtn != null)	
			this.OccassionDateRadioBtn.checked = false;
			
		if (this.FlexOrOccassionDateRadioBtn != null)
			this.FlexOrOccassionDateRadioBtn.checked = false;
			
		if( this.ForceZipCollectionOnSundaySelection == "true" )
		{
			var selectedDate=new Date(eval('"'+this.DeliveryDateHiddenText.value+'"'))
			 
			if (selectedDate.toDateString() == VdayDate) 
			{
				/* unhide the zip step */
				var zipStep = document.getElementById('deliveryZipCode');
				if( zipStep != undefined )
					zipStep.style['display']='block';
				$("#deliveryZipCode").insertAfter("#DeliveryDate");
				var theTopOfZipContainer = $(".zipContainer:first").offset().top;
			    var theTopOfCurrentButton = $("#DeliveryDate").offset().top;
			    var theNewButtonLocationForBuyNow = theTopOfZipContainer - theTopOfCurrentButton;
				$("#OrderButtonDiv").css("top",theNewButtonLocationForBuyNow + "px");				
				
			}
			else
			{
				/* hide the zip step */
				var zipStep = document.getElementById('deliveryZipCode');
				if( zipStep != undefined )
					zipStep.style['display']='none';
				$("#OrderButtonDiv").css("top","5px");
			}
		}
	};
	
	this.OnRadioButtonClick = function(e, obj)
	{
		var objectClicked = (e.target === undefined) ? e.srcElement : e.target;
		var tempstring = objectClicked.attributes['clickvalue'].value;
		obj.DeliveryDateText.value = tempstring.split('$')[1];
		obj.DeliveryDateHiddenText.value = tempstring.split('$')[0];
		obj.HideCalendarPanel();
	};
					
	this.ShowCalendarPanel = function()
	{
	    /* enabled Calendar control before the Omniture stuff */
	    this.CalendarPanel.style.display = 'inline';
	    try
	    {
		    hideFormInputElements(".FlexCssClass");
	        /* checking if the omniture variable is not a null value */
	        if(s_gi != null)
	        {
		        var s=s_gi(s_account); 
		        s.linkTrackVars='prop1,prop11,prop12,prop13,prop14,channel,eVar4,events'; 
		        s.linkTrackEvents="event21";
		        s.events="event21";
		        s.prop1= s.prop11 + ":Calendar";
		        s.tl(this,'o','Calendar'); 
		   }
		}
		catch(e)
		{
		}
		
	};
	this.HideCalendarPanel = function()
	{
		this.CalendarPanel.style.display = 'none';
		showFormInputElements();
		
        // Free Shipping Test, remove when done
        var FSLinkElement = document.getElementById("FSLinkContainer");
        if( FSLinkElement != null )
        {
            FSLinkElement.style.display = "block";
        }
        // Free Shipping Test, remove when done
	};
	
	this.HandleDeliveryOptionsClick = function()
	{
		if(this.DeliveryOptionsPanel.style.display=='none')
		{
			this.DeliveryOptionsPanel.style.display='inline';
		}
		else
		{
			this.DeliveryOptionsPanel.style.visibility='none';
		}
	};
	this.HandleCloseClick = function(e, obj)
	{
	    obj.HideCalendarPanel();
	};
	this.HandleOpenClick = function(e, obj)
	{	 
	   obj.ShowCalendarPanel();
	};
	

 
 
	if( this.IsEditable )
	{
		 
	     
		PRVD.CT.EVENTS.Event.addListener(this.DeliveryDateText, "click", this.HandleOpenClick, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.ClosePanelLink, "click", this.HandleCloseClick, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.CalendarButton, "blur", this.HandleCloseClick, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.CalendarButton, "click", this.HandleOpenClick, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.FlexibleDateRadioBtn, "click", this.OnRadioButtonClick, this, false);
		PRVD.CT.EVENTS.Event.addListener(this.OccassionDateRadioBtn, "click", this.OnRadioButtonClick, this, false);
		PRVD.CT.EVENTS.Event.addListener(this.FlexOrOccassionDateRadioBtn, "click", this.OnRadioButtonClick, this, false);
			
	}
    this.Calendar.ClientSideOnSelectionChanged = this.OnCalendarObjectChange;
    this.Calendar.ParentCustomJSObject = this;    
};

 


   
   
