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.DeliveryInputDefaultValue = this.DeliveryDateText.value;
    this.DeliveryInputZipValue = "Please enter Zip Code above";
    this.FlexibleDeliveryPanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "pnlflexibledelivery")[0];
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zipcodeerrormessagepanel").length > 0)
        this.ZipCodeErrorMessagePanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zipcodeerrormessagepanel")[0];
    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];
    
     if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button_ziprequired").length > 0)
        this.CalendarButtonZipRequired = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button_ziprequired")[0];
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zippopuppanel").length > 0)
        this.ZipPanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zippopuppanel")[0]; 
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zip_button").length > 0)
        this.ZipButton = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zip_button")[0];
        
      
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zipcodeinput").length > 0)
        this.ZipCodeInput = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zipcodeinput")[0];
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "pnlzipSummary").length > 0)
        this.ZipSummary = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "pnlzipSummary")[0];
    
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "editZipButton").length > 0)
        this.EditZipLink = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "editZipButton")[0];  
     if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "enterZipLink").length > 0)
        this.EnterZipLink = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "enterZipLink")[0];    
   if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "skipZipEntryLink").length > 0)
        this.SkipZipEntryLink = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "skipZipEntryLink")[0];
   if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "pnlzipSummaryOptionalText").length > 0)
        this.SummaryTextPanel = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "pnlzipSummaryOptionalText")[0]; 
    
      
    if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button_zipoptional").length > 0)
        this.CalendarButtonZipOptional = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button_zipoptional")[0];
        
    this.LoadCalendarPanel = false;

    var editableFlagID = "isEditable_" + this.CalendarControl.id.substring(0, this.CalendarControl.id.lastIndexOf("_"));
    this.IsEditable = window[editableFlagID];
    
    if( this.IsEditable === undefined )
    {
		this.IsEditable = true;
    }
    this.SetDisabledForZip = function()
    {
        this.IsDisabledForZip = true;
        if(this.DeliveryDateText.value == this.DeliveryInputDefaultValue)
            this.DeliveryDateText.value = this.DeliveryInputZipValue;
    };
    this.SetEnabledForZip = function()
    {
        this.IsDisabledForZip = false;
        if(this.DeliveryDateText.value == this.DeliveryInputZipValue)
            this.DeliveryDateText.value = this.DeliveryInputDefaultValue;
        this.HideZipErrorPanel();
    };
    this.SetInvisible = function()
    {
        this.Element.style.display = "none";
    };
    this.SetVisible = function()
    {
        this.Element.style.display = "inline";
    };
    this.IsVisible = function()
    {
        return this.Element.style.display != "none";
    }
    this.ResetTextOnDeliveryInput = function()
    {
        if(this.IsDisabledForZip)
            this.DeliveryDateText.value = this.DeliveryInputZipValue;
         else
        this.DeliveryDateText.value = this.DeliveryInputDefaultValue;
    };
	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.RefreshDeliveryCalendar = function(customdateArray, disableddatesArray)
	{
		this.ChangeCalendarDeliveryDates(customdateArray);
		this.ChangeCalendarDisabledDates(disableddatesArray);
		this.Calendar.CellData=new Array();  
		this.Calendar.Render(); 
	};
	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.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;
	};
	
	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.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.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)
	{
	    if(obj.IsDisabledForZip)
	        obj.ShowZipErrorPanel();
	    else
	        obj.ShowCalendarPanel();
	};
	
	this.ShowZipErrorPanel = function()
	{
	    if(this.ZipCodeErrorMessagePanel)
	        this.ZipCodeErrorMessagePanel.style.display='inline';
	};
	this.HideZipErrorPanel = function()
	{
	    if(this.ZipCodeErrorMessagePanel)
	        this.ZipCodeErrorMessagePanel.style.display='none';
	};
	
	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];
			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];
			
			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.ReloadZipSummaryPanel = function( html )
	{  
		if( this.ZipSummary != undefined )
		{
			this.ZipSummary.innerHTML = html;
			this.ZipSummary.style.display = 'inline';	
			if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "editZipButton").length > 0)
			{
                this.EditZipLink = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "editZipButton")[0]; 			
			    if( this.IsEditable )
			    {
				    PRVD.CT.EVENTS.Event.addListener(this.EditZipLink, "click", this.HandleEditZipClick, this, false); 
			    }
			}
		
		 }
	};


	
	this.HideZipPanel = function()
	{   
	    this.ZipPanel.style.display = 'none';
	    
	};
	
	
	this.ShowZipPanel = function()
	{
	    this.ZipPanel.style.display = 'inline'; 
	    
	    try
	    { 
		    hideFormInputElements(".FlexCssClass,.zipEntry");	 
		    this.ZipCodeInput.style.visibility="";		           
		}
		catch(e)
		{
		}
	};	 
 
	
	this.ShowSummaryZipPanel = function()
	{    
	    
	    this.ZipSummary.style.display = 'inline';	
	};
	
	this.HideSummaryZipPanel = function()
	{ 
	    this.ZipSummary.style.display = 'none';
	};
	
	this.ShowSummaryTextPanel = function()
	{ 
	    this.SummaryTextPanel.style.display = 'block';
	};
	
	this.HideSummaryTextPanel = function()
	{
         this.SummaryTextPanel.style.display = 'none';
	};
	
		
	this.HandleOpenClickForZip = function(e, obj)
	{ 
	    obj.ShowZipPanel();
	    obj.HideZipErrorPanel();
	};
	
	
	this.HandleCloseClickForZip = function(e, obj)
	{ 
	    obj.ZipEntryControl.ZipCodeValue = obj.GetZipCode();
	    if(!obj.ZipEntryControl.IsValidZipEntered())
	        obj.ShowZipErrorPanel();	        
	    else
	    {   
	        obj.LoadCalendarPanel = true;
	        document.ProductDetailAppDS.DefaultValues.Rows[0].ShowSummaryZipPanel = true;
	        document.ProductDetailAppDS.DefaultValues.Rows[0].IsZipCodeRequiredToSeeCalendar = true;
	        obj.ZipCodeChangeEvent2.fire();   
	   }
	};
	 
	
		
	this.HandleSkipZipEntryLinkClick = function(e, obj)
	{ 	    
        obj.HideSummaryZipPanel();
        obj.ShowSummaryTextPanel();
        obj.ZipCodeInput.value = '';
	    obj.HideZipPanel();
	    obj.HideZipErrorPanel();
	    obj.ShowCalendarPanel();
	    
	};
	
	this.HandleEditZipClick = function(e, obj)
	{    
	    obj.HideCalendarPanel();
	    obj.ShowZipPanel();
	    obj.HideZipErrorPanel();
	};
	
	
	this.CloseZipEntryLinkClick = function(e, obj)
	{    
	   obj.HideZipPanel();
	   obj.HideZipErrorPanel();
	   obj.ShowCalendarPanel();	  
	   
	   if(this.ZipCodeInput.value != '')
	   {
	        obj.ShowSummaryZip();
	        obj.ShowSummaryZipPanel();
	        obj.HideZipOptionalEntryPanel();
	    }
	   else
	   {
	        obj.HideSummaryZipPanel();
	        obj.ShowZipOptionalEntryPanel();
	    }
	};
	
	this.GetZipCode = function()
	{
	  return this.ZipCodeInput.value;  
	}	
	    
	if( this.IsEditable )
	{
		if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button_ziprequired").length > 0)
	        PRVD.CT.EVENTS.Event.addListener(this.DeliveryDateText, "click", this.HandleOpenClickForZip, this, false); 
	    else if(PRVD.CT.UT.GetElementsByPrvdName(this.Element, "calendar_button_zipoptional").length > 0)
	        PRVD.CT.EVENTS.Event.addListener(this.DeliveryDateText, "click", this.HandleOpenClickForZip, this, false); 
	    else
		    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);
		
		PRVD.CT.EVENTS.Event.addListener(this.CalendarButtonZipRequired, "click", this.HandleOpenClickForZip, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.ZipButton, "click", this.HandleCloseClickForZip, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.EditZipLink, "click", this.HandleEditZipClick, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.CalendarButtonZipOptional, "click", this.HandleOpenClickForZip, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.EnterZipLink, "click", this.HandleEditZipClick, this, false); 
		PRVD.CT.EVENTS.Event.addListener(this.SkipZipEntryLink, "click", this.HandleSkipZipEntryLinkClick, this, false); 	
		
			
	}
    this.Calendar.ClientSideOnSelectionChanged = this.OnCalendarObjectChange;
    this.Calendar.ParentCustomJSObject = this;   
    this.ZipCodeChangeEvent2 = new PRVD.CT.EVENTS.CustomEvent(this.ElementID + "_ZipCodeChangeEvent2");
    
    
    
	// load the zip control if it exists
	ZipEntryControls = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "zipcodeinput");

	if( ZipEntryControls.length > 0 )
	{
	    this.ZipEntryControl = new PRVD.APP.UC.ZipCodeEntry2(ZipEntryControls[0].id);
	
	}
	
	
 		
    
};

PRVD.APP.UC.ZipCodeEntry2 = function(htmlElementID)
    { 
	    this.ElementID = htmlElementID;
	    this.Element = document.getElementById(this.ElementID);
	    this.ZipValidationRegex = new RegExp(/^[\d]{5}$/);
    	  
	    this.ZipCodeValue = document.getElementById(this.ElementID).value;
    	 
        this.IsValidZipEntered = function()
        { 
            return this.ZipValidationRegex.test(this.ZipCodeValue);
        };
    
	 
    };



   
   