var tinyMCE=null;
    function getAjax2(file,params,ret_func) {
        var urlparam = "";
        keys = array_keys (params);
        for (k=0;k<keys.length;k++) {
            if (urlparam == "") urlparam = "?"; else urlparam = urlparam+"&";
            urlparam = urlparam+keys[k]+"="+params[keys[k]];
        }
        var url = BASEURL+"sub/"+file+urlparam;
        xmlHttp=GetXmlHttpObject(ret_func);
        xmlHttp.open("GET", url , true);
        xmlHttp.send(null);
    }
    
    function postAjax2(file,params,ret_func) {
        //alert(params);

        var http = new XMLHttpRequest();
        
        var url = BASEURL+"sub/"+file;
        http.open("POST", url, true);
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");
        http.onreadystatechange = //ret_func();
        function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                //alert(http.responseText) 
                ret_func(http.responseText);
                //document.getElementById('box_reflect').innerHTML=xmlHttp.responseText;  
            }
        }  
        http.send(params); 
    }
    var start;
    var tipus;
    function refresh_cal(){
        loadCalendarData(start, tipus);
    }
    
    function loadCalendarData(from, type){
        var p=new Array();
        p['from'] = from;
        start = from;
        p['type'] = type;
        tipus=type;
        //document.getElementById('Table_0').style.display = 'none'; 
        //document.getElementById('Table_0').style.zIndex = 0; 
        getAjax2("google_calendar.php",p, function(){
                            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
                                document.getElementById('naptar_layer').innerHTML = xmlHttp.responseText; 
                                document.getElementById('Table_0').style.zIndex = 150; 
                                document.getElementById('Table_0').style.display = 'block'; 
                            }
                            });
    }
    function loadCalendarDataMini(from, type, target){
        var p=new Array();
        p['from'] = from;
        start = from;
        p['type'] = type;
        tipus=type;
        //document.getElementById('Table_0').style.display = 'none'; 
        //document.getElementById('Table_0').style.zIndex = 0; 
        getAjax2("google_calendar.php",p, function(){
                            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
                                //alert(xmlHttp.responseText);
                                document.getElementById(target).innerHTML = xmlHttp.responseText; 
                            }
                            });
    }
    function hideCalendar(){
        document.getElementById('Table_0').style.zIndex = 0; 
        document.getElementById('Table_0').style.display = 'none'; 
    }
    function loadDetails(id, user_id){
        document.getElementById('naptar_details_layer').innerHTML = "<br /><br /><center><img src='"+BASEURL+"images/ajaxLoader.gif' alt='please Wait...' /></center><br /><br />"; 
        document.getElementById('Table_1').style.zIndex = 250; 
        document.getElementById('Table_1').style.display = 'block'; 
        var p=new Array();
        p['id'] = id;
        p['user_id'] = user_id;
        //document.getElementById('Table_1').style.display = 'none'; 
        //document.getElementById('Table_1').style.zIndex = 0; 
        getAjax2("google_calendar.php",p, function(){
                            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
                                document.getElementById('naptar_details_layer').innerHTML = xmlHttp.responseText; 
                            }
                            });
    }
    function addComment(f){
        var p=new Array();
        p = "id="+f.id.value;
        p += "&user_id="+f.user_id.value;
        p += "&add_comment="+f.add_comment.value;
        p += "&text="+f.text.value;
        postAjax2("google_calendar.php",p, function(ret){
                            if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
                                //alert( ret);
                                document.getElementById('naptar_details_layer').innerHTML = ret; 
                                document.getElementById('Table_1').style.zIndex = 250; 
                                document.getElementById('Table_1').style.display = 'block'; 
                            }
                            });
    }
    function hideDetails(){
        document.getElementById('Table_1').style.zIndex = 0; 
        document.getElementById('Table_1').style.display = 'none'; 
    }
    function moveFront(div){
        div.style.zIndex=250;
    }
    function moveBack(div){
        div.style.zIndex=div.getAttribute('origZ');
    }

    
    /* ADMIN FUNCTIONS */
    function deleteEvent(id, user_id){
        if (confirm('Are you sure to delete this event?')) {
            p = "id="+id;
            p += "&user_id="+user_id;
            p += "&edit=1";
            p += "&del_event=1";
            //document.getElementById('Table_1').style.display = 'none'; 
            //document.getElementById('Table_1').style.zIndex = 0; 
            postAjax2("google_calendar.php",p, function(ret){
                                    //document.getElementById('naptar_details_layer').innerHTML = xmlHttp.responseText; 
                                    //alert(ret);
                                    document.getElementById('Table_1').style.zIndex = 0; 
                                    document.getElementById('Table_1').style.display = 'none'; 
                                    //alert(start+'_'+type);
                                    //loadCalendarData(start, type);
                                    refresh_cal();
                                });
        }
    }
    function editEvent(id, user_id){
        p = "id="+id;
        p += "&user_id="+user_id;
        p += "&edit=1";
        p += "&edit_event=1";
        //document.getElementById('Table_1').style.display = 'none'; 
        //document.getElementById('Table_1').style.zIndex = 0; 
        postAjax2("google_calendar.php",p, function(ret){
                            document.getElementById('naptar_details_layer').innerHTML = ret; 
                            document.getElementById('Table_1').style.zIndex = 250; 
                            document.getElementById('Table_1').style.display = 'block'; 
                            $("#datepicker1").datepicker();
                            $("#datepicker2").datepicker();
                            //editorInit('base', 'exact', 'content');
                            });
    }
    function newEvent(startDate, user_id){
        p = "startDate="+startDate;
        p += "&user_id="+user_id;
        p += "&edit=1";
        p += "&new_event=1";
        //document.getElementById('Table_1').style.display = 'none'; 
        //document.getElementById('Table_1').style.zIndex = 0; 
        postAjax2("google_calendar.php",p, function(ret){
                            document.getElementById('naptar_details_layer').innerHTML = ret; 
                            document.getElementById('Table_1').style.zIndex = 250; 
                            document.getElementById('Table_1').style.display = 'block';
                            $("#datepicker1").datepicker();
                            $("#datepicker2").datepicker();
                            //editorInit('base', 'exact', 'content');
                            });
    }
    function saveEvent(f){
        p = "id="+f.id.value;
        p += "&user_id="+f.user_id.value;
        p += "&edit=1";
        p += "&save_event=1";
        p += "&calendar="+f.calendar.value;
        p += "&title="+f.title.value;
        p += "&location="+f.location.value;
        p += "&startDate="+f.startDate.value;
        p += "&startTime="+f.startTime.value;
        p += "&endDate="+f.endDate.value;
        p += "&endTime="+f.endTime.value;
        if (tinyMCE.editors.content) tinyMCE.editors.content.save();
        p += "&content="+f.content.value;
        postAjax2("google_calendar.php",p, function(ret){
                                alert( ret);
                                document.getElementById('naptar_details_layer').innerHTML = ret; 
                                document.getElementById('Table_1').style.zIndex = 0; 
                                document.getElementById('Table_1').style.display = 'none'; 
                                //alert(start+'_'+type);
                                //loadCalendarData(start, type);
                                refresh_cal();
                            });
    }
    
    