function initXmlHttp() {
  	try {
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
        xmlHttp.overrideMimeType("text/html");
       } catch (e) {
    	// Internet Explorer
    	try {
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     	} catch (e) {
      		try {
       			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (e) {
      			alert("Unsupported browser type. This page will not work!");
       			return;
      		}
    	}
  	}
  	return xmlHttp;
}

/**
 * Make an AJAX call, populating the given div with
 * the result from the given URL.
 * Use a callback parameter to allow an arbitary function callback.
 */
function ajaxCall(divId, url, callback) {
	 if(!callback) {
		 var callback;
	 }
	 var div = document.getElementById(divId);
    if (div == null) {
    	alert("Div '" + divId + "' does not exist");
    	return;
    }

	var xmlHttp = initXmlHttp();
  	prepareXmlHttp(xmlHttp, div, callback);
    xmlHttp.open("GET", url, true);
  	xmlHttp.send(null);
}

/**
 * Common function to prepare the xmlHttpRequest object
 * based on the browser type, ready for making AJAX calls.
 * Use a callback parameter to allow an arbitary function callback.
 */
function prepareXmlHttp(xmlHttp, div, callback) {
  	xmlHttp.onreadystatechange = function() {
    	if (xmlHttp.readyState == 4) {
      		div.innerHTML = xmlHttp.responseText;
      		if(callback) {
      			callback();
      		}
     	}
    }
}

/**
 * @return the selected value in the given select box
 */
function selected_value(s) {
	for (var i = 0; i < s.options.length; i++) {
		if (s.options[i].selected) return s.options[i].value;
	}
	return 0;
}

//function showStatusBox() {
//	document.getElementById("status_display").style.display="none";
//	document.getElementById("status_input").style.display="";
//	document.statusfm.status.focus();
//}

function setStatus() {
	ajaxCall("status_display", "/customer/status/set/?status=" + escape(document.statusfm.status.value));
	document.statusfm.status.value = "";
//	document.getElementById("status_display").style.display="";
//	document.getElementById("status_input").style.display="none";
	ajaxCall("comment_list", "/customer/comments/list/");
	return false;
}

/*********************************************
 * Calendar specific functions
 ********************************************/
function selectDay(iso, format, displayDiv, hidden_var) {
	document.getElementById("calendarBox").style.display="none";
	document.getElementById(hidden_var).value = iso;
	document.getElementById(displayDiv).innerHTML = format;
}

function showMonth(id, month) {
	showMonthFor(id, month, "display_date");
}

function showMonthFor(id, month, box) {
	var d = document.getElementById("month_" + id);
	if (d) {
		var n = document.getElementById("month_" + (id+1));
		if (n) n.style.display="none";
		n = document.getElementById("month_" + (id-1));
		if (n) n.style.display="none";
		d.style.display = "";
	} else {
		document.getElementById("calendarBox").innerHTML = "Loading...";
		ajaxCall("calendarBox", "/ajax/calendar?date=" + month + "&display_div=" + box);
	}
}

function showCalendarBackup(e) {
	showCalendarFor(e, "display_date", "id_target_date");
}
function showCalendar(event) {
	$(window).datepicker('dialog',new Date(),function(target_date,picker){
		document.getElementById("id_target_date").value = target_date;
		},{dateFormat: 'yy-mm-dd',altField:'#display_date'},event);
//	document.getElementById("display_date").innerHTML = target_date;
}

function showCalendarFor(e, box, hidden_var) {
	if (document.getElementById("calendarBox").style.display=="") {
		document.getElementById("calendarBox").style.display="none";
	} else {
		showDiv(e,"calendarBox",-20,-20);
		var date = "";
		if (document.getElementById(hidden_var))
			date = document.getElementById(hidden_var).value;
		ajaxCall("calendarBox", "/ajax/calendar/?date=" + date + "&display_div=" + box + "&hidden_var=" + hidden_var);
	}
}

function select_value(select_box, value) {
	for (var i = 0; i < select_box.length; i++) {
		if (select_box[i].value == value) {
			select_box[i].selected = true;
		 	break;
		}
	}
}

function deleteActivity(id,day) {
	ajaxCall("activityList", "/gym/activity/delete/" + day + "/" + id);
}

