var pics = [];
var currPic = 1;

function highlightCell(ref)
{
	//change back color (E1C1C5)
	//change link color (000000)
	document.getElementById('menuCell_' + ref).style.backgroundColor = '#E1C1C5';
	document.getElementById('menuLink_' + ref).style.color = '#000000';
}

function normaliseCell(ref)
{
	//change back color (6B112F)
	//change link color (BBD536)
	document.getElementById('menuCell_' + ref).style.backgroundColor = '#6B112F';
	document.getElementById('menuLink_' + ref).style.color = '#BBD536';
}

function changePic(change)
{
	if (Number(currPic) + Number(change) < 1)
		currPic = Number(pics.length);
	else
		if (Number(currPic) + Number(change) >= Number(pics.length))
			currPic = 0;

	currPic = Number(currPic) + Number(change);

	theImg = document.getElementById('bigPic');
	theAlt = document.getElementById('picAlt');
	theRef = document.getElementById('picRef');

	theImg.src = 'img/properties/large/' + pics[currPic][0];
	theImg.alt = pics[currPic][1];
	theImg.title = pics[currPic][1];

	theAlt.innerHTML = pics[currPic][1];
	theRef.innerHTML = (Number(currPic));

	return true;
}

function updateBigPic(src, alt, ref)
{
	theImg = document.getElementById('bigPic');
	theAlt = document.getElementById('picAlt');
	theRef = document.getElementById('picRef');

	theImg.src = 'img/properties/large/' + src;
	theImg.alt = alt;
	theImg.title = alt;

	theAlt.innerHTML = alt;
	theRef.innerHTML = ref;
	currPic = ref;

	return false;
}

function checkReg(form)
{
	if (!checkOptions('optTitle', ['Mr', 'Miss', 'Ms', 'Mrs', 'Dr'], 'Please select your title.'))
		return false;

	if (!checkField(form.txtFirstName, 'Please enter your first name.'))
		return false;

	if (!checkField(form.txtSurname, 'Please enter your surname.'))
		return false;

	if (!checkField(form.txtPropName, 'Please enter your house name / number.'))
		return false;

	if (!checkField(form.txtStreet, 'Please enter your street name.'))
		return false;

	if (!checkField(form.txtCity, 'Please enter your town.'))
		return false;

	if (!checkField(form.txtCounty, 'Please enter your county.'))
		return false;

	if (!checkField(form.txtPostCode, 'Please enter your post code.'))
		return false;

	if (!checkPhone([form.txtContactPhone, form.txtMobilePhone]))
		return false;

//	if (!checkOptions('optHowHeard', ['Internet', 'EveshamJournal', 'ValeMagazine', 'FiveAlive', 'ParishMagazine', 'Leaflet', 'WordOfMouth', 'Other'], 'Please indicate how you heard of Charles Leather Residential.'))
//		return false;

	if (form.selHowHeard.options[form.selHowHeard.selectedIndex].value == 'Other')
		if (!checkField(form.txtOther, 'Please explain how you heard of Charles Leather Residential.'))
			return false;

	if (!checkOptions('optUserType', ['Tenant', 'Landlord'], 'Please indicate whether you are a tenant or a landlord.'))
		return false;

	if (form.optUserType_Tenant.checked)
	{
		if (!checkOptions('optSmoker', ['Yes', 'No'], 'Please indicate whether you smoke or not.'))
			return false;

		if (!checkOptions('optPets', ['Yes', 'No'], 'Please indicate whether you have any pets.'))
			return false;

		if (!checkOptions('optChildren', ['Yes', 'No'], 'Please indicate whether you have any children.'))
				return false;
	}
	else
	{
		if (!checkOptions('optAllowSmokers', ['Yes', 'No'], 'Please indicate whether you will allow smokers or not.'))
			return false;

		if (!checkOptions('optAllowPets', ['Yes', 'No'], 'Please indicate whether you will allow tenants with pets.'))
			return false;

		if (!checkOptions('optAllowChildren', ['Yes', 'No'], 'Please indicate whether you will allow tenants with children.'))
			return false;
	}

	return true;
}

function showHideOther(val)
{
	if (val == 'Other')
		disp = '';
	else
		disp = 'none';

	document.getElementById('howHeardOther').style.display = disp;

	return true;
}

function showTenants()
{
	document.getElementById('optUserType_Tenant').checked = 'checked';

	TRs = document.getElementsByTagName('tr');

	for (x = 0; x < TRs.length; x++)
	{
		if (TRs[x].id)
			if (TRs[x].id.substr(0, 8) == 'landlord')
				TRs[x].style.display = 'none';
			else
				if (TRs[x].id.substr(0, 6) == 'tenant')
					TRs[x].style.display = '';
	}

	return true;
}

function showLandlords()
{
	document.getElementById('optUserType_Landlord').checked = 'checked';

	TRs = document.getElementsByTagName('tr');

	for (x = 0; x < TRs.length; x++)
	{
		if (TRs[x].id)
			if (TRs[x].id.substr(0, 8) == 'landlord')
				TRs[x].style.display = '';
			else
				if (TRs[x].id.substr(0, 6) == 'tenant')
					TRs[x].style.display = 'none';
	}

	return true;
}

function showHideAgent(val)
{
	if (val == 'Registered')
		disp = '';
	else
		disp = 'none';

	document.getElementById('regAgent').style.display = disp;

	return true;
}

function checkLogin(form)
{
	if (!checkText(form.txtUser, 'Please enter your username.'))
		return false;

	if (!checkText(form.txtPass, 'Please enter your password.'))
		return false;

	return true;
}

function getPageOffsetLeft (el)
{
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function getPageOffsetTop (el)
{
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function showPicDiv(img, e, width, height)
{
	div = document.getElementById('imgFloat');
	imgEl = document.getElementById('divImg');

	elLeft = getPageOffsetLeft(e);	//e.clientX;
	elTop = getPageOffsetTop(e);	//e.clientY;

	imgWidth = width;
	imgHeight = height;
	imgSrc = '../img/properties/large/' + img;

	imgEl.src = imgSrc;
	imgEl.style.width = imgWidth + 'px';
	imgEl.style.height = imgHeight + 'px';

	div.style.left = ((elLeft - imgWidth) + 50) + "px";
	div.style.top = (elTop - imgHeight) + "px";
	div.style.width = imgWidth;
	div.style.height = imgHeight;
	div.style.display = '';
}

function hidePicDiv()
{
	document.getElementById('imgFloat').style.display = 'none';
}

function showHidePropInfo(ref)
{
	img = document.getElementById('propExp_' + ref);
	row = document.getElementById('property_' + ref);

	if (row.style.display == 'none')
	{
		row.style.display = '';
		img.src = '../img/minus.gif';
		img.alt = 'Hide property information';
		img.title = 'Hide property information';
	}
	else
	{
		row.style.display = 'none';
		img.src = '../img/plus.gif';
		img.alt = 'View full property information';
		img.title = 'View full property information';
	}

	return;
}

function checkFile(form)
{
	fileName = document.getElementById('Logofile').value;
	ext = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();

	if (fileName == '')
	{
		alert('Please select a file to upload.');
		document.getElementById('Logofile').focus();
		return false;
	}

	if (ext != 'gif' && ext != 'jpg' && ext != 'jpeg' && ext != 'png')
	{
		alert('The file you have selected is not in a suitable format.\nImages must be saved as gif\'s or jpg\'s.');
		return false;
	}

//	if (!checkText(form.txtAltName, 'Please enter a description of this photo.'))
//		return false;

	return true;
}

function checkProp(form)
{
	if (!checkText(form.txtLocation, "Please enter the location of the property (e.g. Ashton Under Hill)"))
		return false;

	if (!checkText(form.txtAddress, "Please enter the first line of the property's address (e.g. 16 Cornfield Way)"))
		return false;

//	if (!checkText(form.txtPostCode, "Please enter the property\'s post code (e.g. WR11 7TA)"))
//		return false;

	if (!checkText(form.txtRent, "Please enter the rent for this property (e.g. 750)"))
		return false;

	if (isNaN(form.txtRent.value))
	{
		alert('Please enter the rent for this property. This must be a number (do not include the pound sign)');
		form.txtRent.focus();
		return false;
	}

	if (!form.chkTenStartBlank.checked)
		if (!checkDate(form.selTenStartDay.value, form.selTenStartMonth.value, form.selTenStartYear.value))
		{
			alert('Please enter the tenancy start date for this property. The date entered is not a valid date.');
			form.selTenStartDay.focus();
			return false;
		}

	if (!form.chkTenEndBlank.checked)
		if (!checkDate(form.selTenEndDay.value, form.selTenEndMonth.value, form.selTenEndYear.value))
		{
			alert('Please enter the tenancy end date for this property. The date entered is not a valid date.');
			form.selTenEndDay.focus();
			return false;
		}

	if (!form.chkInventoryBlank.checked)
		if (!checkDate(form.selInventoryDay.value, form.selInventoryMonth.value, form.selInventoryYear.value))
		{
			alert('Please enter the date of the next inventory check for this property. The date entered is not a valid date.');
			form.selInventoryDay.focus();
			return false;
		}

	if (!form.chkGasBlank.checked)
		if (!checkDate(form.selGasDay.value, form.selGasMonth.value, form.selGasYear.value))
		{
			alert('Please enter the date of the next gas inspection for this property. The date entered is not a valid date.');
			form.selGasDay.focus();
			return false;
		}

	if (!form.chkElecBlank.checked)
		if (!checkDate(form.selElecDay.value, form.selElecMonth.value, form.selElecYear.value))
		{
			alert('Please enter the date of the next electrical inspection for this property. The date entered is not a valid date.');
			form.selElecDay.focus();
			return false;
		}

	return true;
}

function checkLandlord(form)
{
	if (!checkText(form.txtTitle, 'Please enter the person\'s title.'))
		return false;

	if (!checkText(form.txtSurname, 'Please enter the person\'s surname.'))
		return false;

	if (!checkText(form.txtPropName, 'Please enter the property name / number.'))
		return false;

	if (!checkText(form.txtStreet, 'Please enter the street name.'))
		return false;

	if (!checkText(form.txtPostCode, 'Please enter the property\'s post code.'))
		return false;

	if (form.txtContactPhone.value == '' && form.txtMobilePhone.value == '' && form.txtEmail.value == '')
	{
		alert('Please provide at least one phone number or an email address for this person.');
		form.txtContactPhone.focus();
		return false;
	}

	if (form.txtEmail.value != '')
		if (!checkEmail(form.txtEmail, 'Please enter your email address.', 'Please enter your email address. The address entered is not a valid email address.'))
			return false;

	if (!checkText(form.txtPropLocation, 'Please enter the property\'s location.'))
		return false;

	if (!checkOptions('optFurnishing', ['Furnished', 'Unfurnished', 'WhiteGoods'], 'Please indicate whether the property is furnished or not.'))
		return false;

	return true;
}

function checkTenant(form)
{
	if (!checkText(form.txtTitle, 'Please enter the person\'s title.'))
		return false;

	if (!checkText(form.txtSurname, 'Please enter the person\'s surname.'))
		return false;

	if (!checkText(form.txtPropName, 'Please enter the property name / number.'))
		return false;

	if (!checkText(form.txtStreet, 'Please enter the street name.'))
		return false;

	if (!checkText(form.txtPostCode, 'Please enter the property\'s post code.'))
		return false;

	if (form.txtContactPhone.value == '' && form.txtMobilePhone.value == '' && form.txtEmail.value == '')
	{
		alert('Please provide at least one phone number or an email address for this person.');
		form.txtContactPhone.focus();
		return false;
	}

	if (!checkOptions('optFurnishing', ['DontMind', 'Furnished', 'Unfurnished', 'WhiteGoods'], 'Please indicate whether the property is furnished or not.'))
		return false;

	if (!checkText(form.txtVillagePref, 'Please enter the person\'s village preference.'))
		return false;

	if (!checkText(form.txtNumBeds, 'Please enter the number of bedrooms required.'))
		return false;

	if (!checkText(form.txtPrefRent, 'Please enter the preferred rent.'))
		return false;

	return true;
}
