
function GetFormatInt(Number,Length) {
    var Ret = "";
    var NrString = Number.toString();
    for(var i = 0;i<Length-NrString.length;i++){
        Ret += "0";
    }
    return Ret+NrString;
}


function CaledarHandler() 
{
    var Pointer = this;
    this.Container = null;  
    var d = new Date();
    this.Changed = function() {};
    this.ShowYear = d.getFullYear();
    this.ShowMonth = d.getMonth()+1;  
        
    this.SelectedYear = d.getFullYear();
    this.SelectedMonth = d.getMonth();
    this.SelectedDay = d.getDate();
    
    this.Selected = function() {};
    this.Value = d.getFullYear() + "-"+d.getMonth()+"-"+d.getDate();
    this.Sender = null;
    this.Input = null;
    this.Show = function(Sender,Input,Changed) 
    {
        if(Changed != null)
            this.Changed = Changed;
        if(this.Container == null)
            this.CreateContainer();
        if(Sender != null && Sender != undefined) 
            this.Sender = Sender;
        if(Input != null && Input != undefined) 
            this.Input = Input;
        if(this.Sender != null)
        {                       
            if(navigator.appVersion.indexOf("Chrome") > -1)
                this.Container.style.top = GetObjectTop(this.Sender)+"px";
            else
                this.Container.style.top = GetObjectTop(this.Sender)+Window.ScrollY+"px";
            this.Container.style.left = GetObjectLeft(this.Sender)+this.Sender.offsetWidth+"px";
        }   
        LoadToPlace("Calendar/Calendar.php?showyear="+this.ShowYear+"&showmonth="+this.ShowMonth+"&syear="+this.SelectedYear+"&smonth="+this.SelectedMonth+"&sday="+this.SelectedDay,this.Container,function(){
            if(Pointer.Sender.tagName != "INPUT")
            {
                document.getElementById("CalendarCloser").style.display = "none";
            }
        });
          
    }
    
    this.SetValue = function(DateValue) {
        var date = DateValue.split("-"); 
        if(date.length == 3) 
        {    
            if(parseInt(date[0]) != "NaN" && parseInt(date[0]) > 0)        
                this.SelectedYear = date[0];
            if(parseInt(date[1]) != "NaN" && parseInt(date[1]) > 0 && parseInt(date[1]) < 12)
                this.SelectedMonth = date[1];
            if(parseInt(date[0]) != "NaN" && parseInt(date[0]) > 0 && parseInt(date[0]) < 31)
                this.SelectedDay = date[2];
              
            if(parseInt(date[0]) != "NaN" && parseInt(date[0]) > 0) 
                this.ShowYear = date[0];
            if(parseInt(date[1]) != "NaN" && parseInt(date[1]) > 0 && parseInt(date[1]) < 12)
                this.ShowMonth = date[1];  
        }
    }
    
    this.AddYear = function(Count) 
    {
        this.ShowYear = parseInt(this.ShowYear) + Count;
        this.Show();    
    }
    
    this.AddMonth = function(Count) 
    {
        this.ShowMonth = parseInt(this.ShowMonth) + Count; 
        if(this.ShowMonth > 12)
        {
            this.ShowYear++;
            this.ShowMonth -= 12;    
        }
        if(this.ShowMonth < 1)
        {
            this.ShowYear--;
            this.ShowMonth += 12;    
        }
            
        this.Show();  
    }
    
    this.CreateContainer = function() 
    {
        this.Container = document.createElement("div");    
        this.Container.style.position = "absolute"; 
        this.Container.style.top =  "0px"; 
        this.Container.style.left = (Window.Width - 200) / 2 + "px";  
        document.getElementsByTagName("body")[0].appendChild(this.Container);  
    }
    
    this.SetSelected = function(obj) 
    {
        var data    = obj.id.split("_");
        var date    = data[1].split("-"); 

        var LastSelected = document.getElementById("Calendar_"+this.SelectedYear+"-"+this.SelectedMonth+"-"+this.SelectedDay);
        if(LastSelected != null)
            LastSelected.setAttribute("class","Day"); 
            
        this.SelectedYear = date[0];
        this.SelectedMonth = date[1];
        this.SelectedDay = date[2];
       
        var NewSelected = document.getElementById("Calendar_"+this.SelectedYear+"-"+this.SelectedMonth+"-"+this.SelectedDay);
        if(NewSelected != null)
            NewSelected.setAttribute("class","Selected"); 
             
        this.Value = this.SelectedYear + "-"+GetFormatInt(this.SelectedMonth,2)+"-"+GetFormatInt(this.SelectedDay,2); 
        if(this.Input != null) {
            this.Input.value = this.Value;
        }
        if(this.Selected != null)
            this.Selected();
        if(this.Changed != null)
            this.Changed(); 
        if(this.Sender.tagName == "INPUT")
        {                                                                      
            Calendar.Close();  
        }                  
    }
    this.ShowHide = function() 
    {
        if(this.Container.style.display == "none") 
            this.Container.style.display = "block"; 
        else
            this.Container.style.display = "none";  
        
    }
    this.Close = function() {
        this.Container.parentNode.removeChild(this.Container);
        this.Container= null;
    }
}

var Calendar = new CaledarHandler();                             

