var http_request = false;

function makePOSTRequest(url, parameters, fnname) {
//alert(parameters);
  http_request = false;
  if (window.XMLHttpRequest) {
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/html');
	}
  } else if (window.ActiveXObject) {
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }

	switch (fnname) {
		 case 'show':
				http_request.onreadystatechange = alertContents1;
				break;
		 case 'submitBookingForm':
				http_request.onreadystatechange = alertContents2;
				break;
		 case 'submitPricesForm':
				http_request.onreadystatechange = alertContents3;
				break;
	}
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents1() {
	if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
			result = http_request.responseText;
			if (document.getElementById('calendar')) document.getElementById('calendar').innerHTML = result;
	 } else {
		alert('There was a problem with the request.\n Please try again.');
	 }
  }
}

function alertContents2() {
	if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		result = http_request.responseText;
		// if result is like |1234|xxxxxxxxxxxx, a new row was inserted and the number 1234 is the new row id
//debugger;
		data = result.split('|');
		if (data[2]) {
			strReturn = data[2];
			// put id in the form's hidden input field
var item_id = data[1];		
			fnGetElementById('id').value = item_id;
			
//alert(fnGetElementById('id').value);

			// change the header from ADD to EDIT
			if (fnGetElementById('addoredit_in_header')) fnGetElementById('addoredit_in_header').innerHTML =
			'<h2 style="margin-bottom:10px; padding-bottom:0px;">EDIT</h2>';

		} else {
			strReturn = data[0];
		}
		// show result text in white on red background
		if (fnGetElementById('divERR')) fnGetElementById('divERR').innerHTML = strReturn;
		// make the calender jump to the month that was just added or edited

	 } else {
		alert('There was a problem with the request.\n Please try again.');
	 }
	showCalendar(fnGetElementById('startdate[year]').value,fnGetElementById('startdate[day]').value, fnGetElementById('startdate[month]').value);
	// show the calender
	show('divCalendar'); showImg('img_show_calendar', 'hide_calendar.gif')
  }
}

function alertContents3() {
	if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		result = http_request.responseText;
		alert(result);
		if (fnGetElementById('aaa')) fnGetElementById('aaa').innerHTML = result;
	 } else {
		alert('There was a problem with the request. Please try again.');
	 }
  }
}


function showCalendar(year, day, month) {
 http_request = false;
	var year = year;
	var day = day;
	var month = month;
	var poststr =  "year=" + year + "&month=" + month + "&day=" + day;
	makePOSTRequest('ajax/calendar.php', poststr, 'show');
}

// function submitBookingForm (poststr) now is function localFormSubmit() in itemedit.js