// Switch images
function switchImg(targetId) {
	var main = document.getElementById("mainImg");
	main.src = '/image.php?id=' + targetId;
}


function checkForm() {
	// Check form exists
	if (document.forms['details']) {
		showInput(document.details.status.value);
	}
}


// Function to show the correct form elements
function showInput(opt) {

	if (document.forms['details']) {

		// Init for elements to access
		var landlord = document.getElementById('landlord');
		var tenant = document.getElementById('tenant');

		// Action based on selected...
		switch(opt) {

			// Show hospital field
			case 'landlord':
				// Disable unwanted elements
				document.details.hospital.disabled=false;
				landlord.style.display = "block";
				break;

			// Disable landlord stuff
			case 'tenant':
				document.details.hospital.disabled=true;
				landlord.style.display = "none";
				break;

			// Show all fields
			case 'both':
				document.details.hospital.disabled=false;
				landlord.style.display = "block";
				break;

			// Default, switch them off
			default:
				// Disable unwanted elements
				document.details.hospital.disabled=true;
				landlord.style.display = "none";
				break;

		}

	}

}


// Popup Calendar funtion
function openCal(sDate, targetId, openDateCoords) {
	// Use regex to check date string format
	var re = new RegExp("^[0-9]{4}", "g");
	var fD = re.test(sDate);

	// If no match then split date string...
	if (fD == false) {
		var tmpDate = new Date();
		sDate = tmpDate.getFullYear() + '-' + tmpDate.getMonth()+1 + '-' + tmpDate.getDate();
	}

	var dArray = sDate.split('-');
	dArray[1]--;		// Deduct 1 from month, 0-11 for JS

	// Init date object with current date (also date components)
	var theDate = new Date(dArray[0],dArray[1],dArray[2]);
	var year = theDate.getFullYear();
	var month = theDate.getMonth();	// Month is 0-11 so no need to deduct for previous month
	var day = theDate.getDate();
	var dayOfWeek = theDate.getDay();

	// Calculate the firs day of the currents date's month
	var firstDayOfMonth = new Date(year, month, 1).getDay();

	// Calculate number of days in the current month
	var daysInMonth = 32 - new Date(year, month, 32).getDate();

	// Init days of week array
	var dow = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	// Init months of year array
	var moy = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

	var pYear = year-1;
	var tMonth = month+1;		// No need to deduct as the getMonth() function works from 0-11
	var cal = '<a id="closeCal" onclick="closeCal(); return false;">x</a>';
	cal += '<div id="calHeader"><a id="pYear" onclick="openCal(\'' + pYear + '-' + tMonth + '-' + day + '\', \'' + targetId + '\', \'' + openDateCoords + '\'); return false;" title="Previous Year">&laquo;</a>';
	cal += '&nbsp;';
	cal += '<a id="pMonth" onclick="openCal(\'' + year + '-' + month + '-' + day + '\', \'' + targetId + '\', \'' + openDateCoords + '\'); return false;" title="Previous Month">&lt;</a>';

	// Display current Month and Year
	cal += '&nbsp;&nbsp;' + moy[month] + ', ' + year + '&nbsp;&nbsp;';

	var nYear = year+1;
	var tMonth = month+1;
	var nMonth = month+2		// Add two to get the real month value (1-12)
	cal += '<a id="nMonth" onclick="openCal(\'' + year + '-' + nMonth + '-' + day + '\', \'' + targetId + '\', \'' + openDateCoords + '\'); return false;" title="Next Month">&gt;</a>';
	cal += '&nbsp;';
	cal += '<a id="nYear" onclick="openCal(\'' + nYear + '-' + tMonth + '-' + day + '\', \'' + targetId + '\', \'' + openDateCoords + '\'); return false;" title="Next Year">&raquo;</a></div>';

	// Start new row
	cal += '<div class="clearMe"></div>';

	// Loop through days of the week and display
	for (var value in dow) {
		cal += '<div class="dayTitle">' + dow[value] + '</div>';
	}

	// Start new row
	cal += '<div class="clearMe"></div>';

	// Populate table with blank spaces for days last month
	if (firstDayOfMonth > 0) {
		var tmpCnt = firstDayOfMonth;
		// Loop over days of week to produce blanks at start of month
		while (tmpCnt > 0) {
			cal += '<div class="dayDays">&nbsp;</div>';
			tmpCnt--;
		}
	}

	// Initialise first day of month variable
	var currentDay = 1;
	var dayCount = firstDayOfMonth;
	// Loop through the month using number of days
	while (currentDay <= daysInMonth) {
		if (dayCount == 7) {
			dayCount = 0;
			cal += '<div class="clearMe"></div>';
		}
		var linkDate = year + '-';
		linkDate += month+1;
		linkDate += '-' + currentDay;
		// Work out whether to use normal style or selected style
		if (currentDay == day) {
			var linkStyle = 'theDay';
		} else {
			var linkStyle = 'dayDays';
		}
		cal += '<a class="' + linkStyle + '" onclick="sendVal(\'' + linkDate + '\', \'' + targetId + '\');">' + currentDay + '</a>';
		// Increment the counters
		currentDay++;
		dayCount++;
	}

	// See if day count ended before the week has finish
	if (dayCount < 7) {
		while (dayCount < 7) {
			cal += '<div class="dayDays">&nbsp;</div>';
			dayCount++;
		}
	}

	if (document.getElementById) {
		// Get id of element to write to
		var target = document.getElementById("calendar");
		// Also grab dummy fram to display underneath
		var dummy = document.getElementById("dummyFrame");
		// Grab object coords as array
		var theCoords = getCoords(openDateCoords);
		dummy.style.left = theCoords[0] + 'px';
		dummy.style.top = (theCoords[1] - 40) + 'px';
		dummy.style.width = target.offsetWidth + 'px';
		dummy.style.height = target.offsetHeight + 'px';
		dummy.style.visibility = 'visible';

		target.style.left = theCoords[0] + 'px';
		target.style.top = (theCoords[1] - 40) + 'px';
		target.innerHTML = (cal);
		target.style.visibility = 'visible';
	}
}


// Work our coords for the given object
function getCoords(theId) {
	obj = document.getElementById(theId);
	var cLeft = 0;
	var cTop = 0;
	// Only work with moderm DOM compliant browsers
	if (document.getElementById) {
		// Loop until we reach the top of the tree
		while (obj.offsetParent) {
			cLeft += obj.offsetLeft;
			cTop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	var tmp = new Array(cLeft, cTop);
	return tmp;
}


// Send date value back to form
function sendVal(theDate, targetId) {
	var targetElement = document.getElementById(targetId);
	targetElement.value = theDate;
	if (targetId == 'reminder_x') {
		var lab = document.getElementById('rLabel');
		lab.innerHTML = 'Reminder (' + theDate + ')';
	}
	closeCal();
}


// Close the calendar
function closeCal() {
	var target = document.getElementById("calendar");
	var dummy = document.getElementById("dummyFrame");
	target.innerHTML = '&nbsp;';
	target.style.visibility = 'hidden';
	dummy.style.visibility = 'hidden';
}


// Insert text at the cursor postion in a textarea
function insertAtCursor(field, value) {
	// Check for IE
	if (document.selection) {
		field.focus();
		var sel = document.selection.createRange();
		sel.text = value;

	// Check for Moz
	} else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);

	} else {
		field.value += value;
	}
}


function enterTxt(tag) {
	var theTxt = prompt('Please enter text:');
	// Check string has length
	if (theTxt.length > 0) {
		// Check for URL tag
		if (tag == 'link') {
			var link = prompt('Please enter valid web address:');
			insertAtCursor(document.content._any_content, '[' + tag + '=' + link + ']' + theTxt + '[/' + tag + ']');
		} else {
			insertAtCursor(document.content._any_content, '[' + tag + ']' + theTxt + '[/' + tag + ']');
		}
	}
}