//Copyright (c) 2006 Frederick Leitner  $Revision: 0.1 $

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Options Variables
var webCal_showStatus; //  1 shows 'Loading...' , while waiting to update the calendar.  0 does not
var webCal_base_url; // the baseurl for which the calendar lives
var webCal_showArrows; // 1 shows previous and next arrows, 0 does not
var webCal_refreshDelay; // number of minutes to delay before updating calendar. 0 means no update



//Internal Variables
var webCal_req; 
var webCal_date_prev = "";
var webCal_date_next = "";


//helper functions to set the values of the options variable when
//calling show_minical from webCalendar.php
function webCal_set_refreshDelay(delay) {
    webCal_refreshDelay = delay;
}

function webCal_set_baseURL(url) {
    webCal_base_url = url;
}

function webCal_set_showArrows(show) {
    webCal_showArrows = show;
}

function webCal_set_showStatus(show) {
    webCal_showStatus = show;
}



//function to process the raw html received from minicalendar.php
//into a usable form

function webCal_process_Calendar(resp,pos){
    var title_cage = document.getElementById('webCal_title_cage');
    resp  = resp.substring(pos + 25);
    resp = resp.replace(/<\/body>/,'');
    resp = resp.replace(/<\/html>/,'');
    if (webCal_showArrows) {
	//		    var re = new RegExp("(<a title=\"Previous\".*$)|(<a title=\"Next\".*$)");
	var re = new RegExp('(<a title.*$)','mg');
	var pieces  =  resp.split(re);
	resp = pieces[0] + pieces[4];
	resp = resp.replace(/href=\"/g,'href="' + webCal_base_url); 
	webCal_date_prev=pieces[1].match(/date=\d*/);
	pieces[1] = pieces[1].replace(/src=\"/,'src="' + webCal_base_url); 
	pieces[1] = pieces[1].replace(/href=\"\S*?\"/,
				      'href="javascript:webCal_display_minical_prev()"' );
	webCal_date_next=pieces[3].match(/date=\d*/);
	pieces[3] = pieces[3].replace(/src=\"/,'src="' + webCal_base_url); 
	pieces[3] = pieces[3].replace(/href=\"\S*?\"/,
				      'href="javascript:webCal_display_minical_next()"' );
	resp = resp.replace(/<\/tbody>/,
			    '<tr id="arrows"><td colspan="2">'
			    + pieces[1] 
			    + '</td><td></td><td></td><td></td><td colspan="2">' 
			    + pieces[3] 
			    + '</td></tr></tbody>');
    } else {
	resp = resp.replace(/^<a title=\"Previous\".+$/m,'');
	resp = resp.replace(/^<a title=\"Next\".+$/m,'');
	resp = resp.replace(/href=\"/g,'href="' + webCal_base_url); 
    }
    if (title_cage != null) {  //user wants to put the month/year someplace else
 	resp = resp.replace(/<tr class="monthnav"><th colspan="7">/,'');
 	var temp_date  = resp.match(/^\w+\s+\d+<\/th><\/tr>$/m)[0];
	temp_date = temp_date.match(/^\w+\s+\d+/)[0];
 	title_cage.innerHTML  = temp_date;
 	resp = resp.replace(/^\w+\s+\d+<\/th><\/tr>$/m,'');
    } 
    resp = resp.replace(/&nbsp;/g,'');  //XML does not like  non-breaking spaces
    resp = resp.replace(/nulogin.php\?login=__public__&amp;return_path=/g,'');  //kludge to avoid a minical.php error... 
    resp = resp.replace(/Sun/,'S');
    resp = resp.replace(/Mon/,'M');
    resp = resp.replace(/Tue/,'T');
    resp = resp.replace(/Wed/,'W');
    resp = resp.replace(/Thu/,'R');
    resp = resp.replace(/Fri/,'F');
    resp = resp.replace(/Sat/,'S');
    resp = '<center>' + resp + '</center>';
    return resp;
}


//function to display the calendar
function webCal_processReqChange()  {
    // only if req shows "complete"
    if (webCal_req.readyState == 4) {
	// only if "OK"
        var cage = document.getElementById('webCal_cage');
	if (webCal_req.status == 200) {
	    var resp = webCal_req.responseText;
	    var pos = resp.search(/<body id=\"minicalendar\">/);
	    if (pos == -1 ) {
		cage.innerHTML = "Invalid Minical";
	    } else {
 	        cage.innerHTML = webCal_process_Calendar(resp,pos);
		if (webCal_refreshDelay > 0) {
		    setTimeout('webCal_display_minical_curr()',webCal_refreshDelay*60*1000);
		}
           }
	} else {
	    cage.innerHTML = webCal_req.statusText;
	}

    }
}



//internal functions for  updating which calendar is displayed
function webCal_display_minical_curr() {
    if (webCal_showStatus) {
	document.getElementById('webCal_cage').innerHTML="Loading...";
    }
    webCal_display_minical('');
}

function webCal_display_minical_prev() {
    if (webCal_showStatus) {
	document.getElementById('webCal_cage').innerHTML="Loading...";
    }
    webCal_display_minical(webCal_date_prev);
}

function webCal_display_minical_next() {
    if (webCal_showStatus) {
	document.getElementById('webCal_cage').innerHTML="Loading...";
    }
    webCal_display_minical(webCal_date_next);
}



//make the http request to minical and assign the handler
function webCal_display_minical(date) {     //for native XMLHttpRequest object 
    var url = webCal_base_url + 'minical.php' +'?' + date;
    if (window.XMLHttpRequest) { 
	weCal_cal = null;
	webCal_req = new XMLHttpRequest(); 
	webCal_req.onreadystatechange = webCal_processReqChange;
	webCal_req.open("GET",url,true);
	webCal_req.send(null); 
    } else if (window.ActiveXObject) {  //for IE/Windows ActiveX version 
	webCal_req = new ActiveXObject("Microsoft.XMLHTTP"); 
	if (webCal_req) { 
	    webCal_req.onreadystatechange = webCal_processReqChange; 
	    webCal_req.open("GET", url, true);
	    webCal_req.send(); 
	} 
    }
}


