﻿// JScript File to handel radcontrol datepicker (Calendar)
/*********************************************************************/ 
//in order to use this jscript file, put in the aspx/ascx page:
//              * two datepicker
//              * define objDatePicker_1 and objDatePicker_2
//                 (** for example : objDatePicker_1 = "<%= RadDatePicker1.ClientID%>";)
//              *  optinal to define numOfDayToAdd. 
//                 the minimum days between the first date to the second date
//                 if numOfDayToAdd not define -> default value is 7 days
/*********************************************************************/      

var oIFrame = null;    //retains the current shown iframe
var isShow;     //tells if we should/shouldn't show the iframe under the popup
var prevId = "";    //retains the previous iframe that was shown
var bToggle = true; //tells to hide or show popup
var width;
//function OnDateSelected
//fired when a date is selected
//only when the event fired by objDatePicker_1 do this function
//what: 
//   after the first date was selected (objDatePicker_1)
//   set the second datepicker to a larger date (objDatePicker_2)
//   (objDatePicker_1 date + numOfDayToAdd <= objDatePicker_2)
//   optinal to define numOfDayToAdd. 
//                 the minimum days between the first date to the second date
//                 if numOfDayToAdd not define -> default value is 7 days
//notice: objDatePicker_1 and objDatePicker_2 are define where the two datepicker are placed   
//if they are not define - exist.
function OnDateSelected(datepickerInstance, args)
{
    if (isBrowserIE6())
    {
        if (oIFrame!= null)
            oIFrame.style.display = "none";
    }
    bToggle = false;
    
    if(  typeof(objDatePicker_1) == 'undefined'|| objDatePicker_1 == null ||
         typeof(objDatePicker_2) == 'undefined'|| objDatePicker_2 == null )
         {
              //objDatePicker_1 and objDatePicker_2 are not define              
              return;
         }
    if (datepickerInstance.ClientID != objDatePicker_1.ClientID && 
         datepickerInstance.ClientID != objDatePicker_2.ClientID)
        {
            //datepickerInstance is not objDatePicker_1 or objDatePicker_2
            return;
        }
    if(  datepickerInstance.ClientID == objDatePicker_1.ClientID )
    {
        //do it only if the sender is objDatePicker_1
        if(typeof(numOfDayToAdd) == 'undefined' || numOfDayToAdd == null)
        {
           numOfDayToAdd = 7;
        }
        var date = objDatePicker_1.GetDate();
        date.setDate(date.getDate() + numOfDayToAdd);
        objDatePicker_2.SetDate(date);
        objDatePicker_2.MinDate = date;
        objDatePicker_2.FocusedDate = date;
    }
   
}

//function TogglePopup
//open the popup when click on the dateInput
function TogglePopup(DatePickerId)
{         
    if (isBrowserIE6())
    {
        //modifications needed for the internet explorer 6.0 bug (comboboxes visible over div)
        var datePicker = eval(DatePickerId);

        var textBox = datePicker.GetTextBox();
        var position = datePicker.GetElementPosition(textBox);
        var dimensionText = datePicker.GetElementDimensions(textBox);

        var oCtrl1 = document.getElementById(DatePickerId+"_calendar");
        var oCtrl2 = document.getElementById(DatePickerId+"_wrapper");
        
        var dimensions = new Object(); 

        //hide the other datePicker iframe
        if (prevId!=(DatePickerId+"_frame"))
        {
            if (document.getElementById(prevId)!= null)
                document.getElementById(prevId).style.display = "none";
            bToggle = false;
        }
        
        if (document.getElementById(DatePickerId+"_frame") == null)
        {
            dimensions.x  = position.x;
            dimensions.y = position.y;
            dimensions.width = width;
            dimensions.height = oCtrl1.offsetParent.offsetParent.offsetParent.offsetParent.offsetParent.clientHeight + oCtrl2.offsetParent.clientHeight;
        
            //create an iframe element to hide the comboboxes
            oIFrame = document.createElement("iframe");
            oIFrame.id = DatePickerId+"_frame";
            oIFrame.style.position = "absolute";
            oIFrame.style.width = dimensions.width;
            oIFrame.style.height = dimensions.height - dimensionText.height;
            oIFrame.style.top = dimensions.y + dimensionText.height;
            oIFrame.style.left = dimensions.x;
            oIFrame.src = "";
            oIFrame.frameBorder = "0";
            oIFrame.style.display = "inline";
            document.body.appendChild(oIFrame);
            isShow = true;
        }
        else
        {
            //if the iframe is already created, just show it
            oIFrame = document.getElementById(DatePickerId+"_frame");
            oIFrame.style.display = "inline";
            isShow = true;
        }
        prevId = oIFrame.id;
    }  
    //toggle show/hide popup and iframe 
    if (bToggle == false)
    {
        bToggle = true;
        eval(DatePickerId).ShowPopup();
    }
    else
    {
        if (oIFrame != null)
            oIFrame.style.display = "none";
        eval(DatePickerId).HidePopup();
        bToggle = false;
    }
}

//function needed for internet explorer 6.0 bug (for the radDatePicker control)
document.onclick = function()
{
    if (isBrowserIE6())
    {
        //on click anywhere on the page, hide the iframe behind the datePicker control
        if (oIFrame != null)
            {
                if (isShow != true)
                {
                    //if is not the first time hide the iframe
                    //(the click apears even on the text box)
                    oIFrame.style.display = "none";
                    prevId ="";
                }
                else
                {
                    //if is the first time then do nothing
                    isShow = false;
                }
            }
    }
}

//check if broser is Internet Explorer version 6.0
function isBrowserIE6()
{
    if(navigator.appName.indexOf('Microsoft')>-1)
    {
        if(navigator.appVersion.indexOf('MSIE 6.0')>-1)
            return true;
    }
    return false;
}
