
/*
   Date format:

   dateformat     format
   ------------------------
      mdy       mm/dd/yyyy
      ymd       yyyy/mm/dd
      dmy       dd/mm/yyyy
 */

var dateformat = 'dmy';  //Take care. It has to be EXACTLY the same at datedialog-server.php, line 14
                         //                                            index.php,             line 12

function update_content(id,content){
     if (document.getElementById)
		 document.getElementById(id).innerHTML=content;
	 else if (document.layers)
		 document[id].innerHTML=content;
	 else if (document.all)
		 document.all[id].innerHTML=content;
}

function call_server(url, vars){
     var xml = null;
     try{
         xml = new ActiveXObject("Microsoft.XMLHTTP");
     }catch(exception){
         xml = new XMLHttpRequest();
     }

     if(xml!=null){
        xml.open("GET",url + vars, false);
        xml.send(null);
        if(xml.status == 404) alert("Error 404: Incorrect url.");
        return xml.responseText;
     }
     else
        alert("Your browser does not support XMLHTTP.")
}

function get_calendar(
                    m,y,
                    date_dialog_div,
                    selected_date_hidden_var,
                    selected_date_showed_div
                      ){

    var selectedday='';
    if(document.getElementById(selected_date_hidden_var))
       selectedday = document.getElementById(selected_date_hidden_var).value;
    var response = call_server( "datedialog-files/datedialog-server.php",
                                "?y="+y+
                                "&m="+m+
                                "&divname="+date_dialog_div+
                                "&hiddenvar="+selected_date_hidden_var+
                                "&showeddiv="+selected_date_showed_div+
                                "&selectedday="+selectedday
                              );
    var did = 'htmlcalendar'+date_dialog_div; 
    update_content(did,response);
}

function print_calendar(
                    date_dialog_div,
                    selected_date_hidden_var,
                    selected_date_showed_div
                       ){
    var y=document.getElementById("year"+date_dialog_div);
    var m=document.getElementById("month"+date_dialog_div);
    if( y && m ){
        var year=y.options[y.selectedIndex].value;
        var month=m.options[m.selectedIndex].value;
        get_calendar( month,year,
                      date_dialog_div,
                      selected_date_hidden_var,
                      selected_date_showed_div );
    }
}

function print_calendar2(
                    themonth,
                    theyear,
                    date_dialog_div,
                    selected_date_hidden_var,
                    selected_date_showed_div
                       ){
     get_calendar( themonth,
                   theyear,
                   date_dialog_div,
                   selected_date_hidden_var,
                   selected_date_showed_div );

     //Select month and year for comboboxes
    var y=document.getElementById("year"+date_dialog_div);
    var m=document.getElementById("month"+date_dialog_div);
    if( y && m ){
        //Select month
        for(var i=0; i<m.options.length; i++)
          if(m.options[i].value==themonth){
             m.options[i].selected=true;
             break;
          }

        //Select year
        for(var i=0; i<y.options.length; i++)
          if(y.options[i].value==theyear){
             y.options[i].selected=true;
             break;
          }
    }
}

function print_today(
                    theday,
                    themonth,
                    theyear,
                    date_dialog_div,
                    selected_date_hidden_var,
                    selected_date_showed_div
                       ){

     get_calendar( themonth,
                   theyear,
                   date_dialog_div,
                   selected_date_hidden_var,
                   selected_date_showed_div );

     //Select month and year for comboboxes
    var y=document.getElementById("year"+date_dialog_div);
    var m=document.getElementById("month"+date_dialog_div);
    if( y && m ){
        //Select month
        for(var i=0; i<m.options.length; i++)
          if(m.options[i].value==themonth){
             m.options[i].selected=true;
             break;
          }

        //Select year
        for(var i=0; i<y.options.length; i++)
          if(y.options[i].value==theyear){
             y.options[i].selected=true;
             break;
          }
    }

    if(themonth<10) themonth="0"+themonth;
    if(theday<10) theday="0"+theday;

    var thedate='';
    if(dateformat=='dmy')
       thedate=theyear+"/"+themonth+"/"+theday;
    else if(dateformat=='dmy')
       thedate=theyear+"/"+themonth+"/"+theday;
    else
       thedate=themonth+"/"+theday+"/"+theyear; //US format

    if(document.getElementById(selected_date_hidden_var))
       document.getElementById(selected_date_hidden_var).value=thedate
    update_content(selected_date_showed_div, thedate);
    close_datedialog(date_dialog_div);
}

function print_day( d,
                    date_dialog_div,
                    selected_date_hidden_var,
                    selected_date_showed_div  
                   ){
    var y=document.getElementById("year"+date_dialog_div);
    var m=document.getElementById("month"+date_dialog_div);
    if( y && m ){
        var year=y.options[y.selectedIndex].value;
        var month=m.options[m.selectedIndex].value;
        if(month<10) month="0"+month;
        if(d<10) d="0"+d;

        var thedate='';
        if(dateformat=='dmy')
           thedate=d+"/"+month+"/"+year;
        else if(dateformat=='dmy')
           thedate=d+"/"+month+"/"+year;
        else
           thedate=month+"/"+d+"/"+year; //US format

        if(document.getElementById(selected_date_hidden_var))
           document.getElementById(selected_date_hidden_var).value=thedate
        update_content(selected_date_showed_div, thedate);
        close_datedialog(date_dialog_div);
    }
}

function close_datedialog(date_dialog_div){
    if(document.getElementById(date_dialog_div))
       document.getElementById(date_dialog_div).style.display = 'none';
}

var last_div_showed='';

function show_datedialog(
                    date_dialog_div,
                    selected_date_hidden_var,
                    selected_date_showed_div   ){

    if(document.getElementById(date_dialog_div)){

       if(document.getElementById(date_dialog_div).style.display == 'block')
          document.getElementById(date_dialog_div).style.display = 'none';
       else{

          if(last_div_showed!='') close_datedialog(last_div_showed);

          var coordinates = getAnchorPosition(selected_date_showed_div);
          document.getElementById(date_dialog_div).style.left=coordinates.x + "px";
          coordinates.y+=17;
          document.getElementById(date_dialog_div).style.top=coordinates.y + "px"; 

          document.getElementById(date_dialog_div).style.zIndex = 200;

          document.getElementById(date_dialog_div).style.display = 'block';
          last_div_showed = date_dialog_div;
          print_calendar(
                       date_dialog_div,
                       selected_date_hidden_var,
                       selected_date_showed_div  );
       }
    }
}


