/**************************************/
/*
Lloyd Engineering Ltd.
Copyright©Unite and Rule Ltd.
*/
/**************************************/

/**************************************/
/* Swap the active CSS */
function setActiveStyleSheet(title) {
	if (document.getElementsByTagName) {
		for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) a.disabled = true;
			if (a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

/**************************************/
/* Popup */
function popup (obj, iW, iH) {
	var winW = iW;
	var winH = iH;
	var winName = '_blank';
	var winL = (screen.width - winW) / 2;
	var winT = ((screen.height - winH) / 2) - 50;
	window.open(obj.href, winName, 'location=no, toolbar=no, status=no, directories=no, scrollbars=yes, menubar=no, resizable=no, left=' + winL + ', top=' + winT +', width=' + winW + ', height=' + winH);
}

/**************************************/
/* Turn off a menu */
function offMenu (obj) {
	document.getElementById(obj + 'Arrows').style.display = 'block';
	document.getElementById(obj).style.display = 'none';
}

/**************************************/
/* Change Menu */
function swapMenu (obj) {
	//check if hidden and show or opposite
	if (document.getElementById(obj).style.display == 'block') {
		document.getElementById(obj).style.display = 'none';
	} else {
		document.getElementById(obj).style.display = 'block';
	}
}

/**************************************/
/* Entry Validation */
function validateEntry(oForm) {
	var bValid = true;
/*
	//check for security code
	if (oForm.code.value.length < 4) {
		alert('Please enter the security code');
		return false;
	}
*/

	//check normal form
	if (oForm.forename.value == '' ||
/*		oForm.initial.value == '' ||*/
		oForm.surname.value == '') {
		alert('Please enter your forename and surnames.');
		return false;
	}
	if (oForm.houseName.value == '') {
		alert('Please enter a house name or number.');
		return false;
	}
	if (oForm.town.value == '') {
		alert('Please enter your town or city.');
		return false;
	}
	if (oForm.county.value == '') {
		alert('Please enter your county or province.');
		return false;
	}
	if (oForm.postcode.value == '') {
		alert('Please enter your postal or zip code.');
		return false;
	}
	if (oForm.country.value == '') {
		alert('Please specify your country.');
		return false;
	}
	if (oForm.telephone.value == '') {
		alert('Please enter your contact telephone number.');
		return false;
	}
	if (oForm.howHear.options[oForm.howHear.selectedIndex].text == 'Please Select') {
		alert('Please tell us how you heard about us.');
		return false;
	}

	//check email
	if (validateEmail(oForm.email) == false) {
		alert('Please enter a valid email address');
		return false;
	}
	if (oForm.email.value != oForm.emailConfirm.value) {
		alert('Your email address and confirmation do not match');
		return false;
	}

	//return correctly
//	if (bValid == false) {
//		alert('Please fill out all fields');
//		return false;
//	}

	return true;
}

/**************************************/
/* Work Validation */
function validateWork(oForm) {
	var bValid = true;

	//check normal form
	if (oForm.file.value == '') {
		alert('Please select an image file to upload.');
		return false;
	}
	if (oForm.title.value == '') {
		alert('Please specify a title for the piece.');
		return false;
	}
	if (isNaN(oForm.dimensionsH.value) || isNaN(oForm.dimensionsW.value)) {
		alert('Please specify the dimensions.');
		return false;
	}
	if (isNaN(oForm.dateCompletedDD.value) || isNaN(oForm.dateCompletedMM.value) || (isNaN(oForm.dateCompletedYYYY.value))) {
		alert('Please provide the date that the work was completed.');
		return false;
	}
	if (isNaN(oForm.price.value)) {
		alert('Please enter a numeric price as a whole number for the piece.  Example: enter £200.00 as 200');
		return false;
	}
	if (oForm.dimensionsH.value == '' ||
		oForm.dimensionsW.value == '') {
		alert('Please specify the piece dimensions.');
		return false;
	}
	if (oForm.dateCompletedDD.value == '' ||
		oForm.dateCompletedMM.value == '' ||
		oForm.dateCompletedYYYY.value == '') {
		alert('Please specify the date the piece was completed.');
		return false;
	}
	if (oForm.price.value == '') {
		alert('Please specify the price of the piece.');
		return false;
	}
	if (oForm.categoryMedium.options[oForm.categoryMedium.selectedIndex].text == 'Please Select') {
		alert('Please select the medium of the piece.');
		return false;
	}

	//check date piece created
	if (isDate(oForm.dateCompletedDD.value, oForm.dateCompletedMM.value, oForm.dateCompletedYYYY.value) == false) {
		alert('Please enter a valid completion date: DD/MM/YYYY');
		return false;
	}

	//check piece year
	if (oForm.dateCompletedYYYY.value < 2009) {
		alert('The piece must have been created after 31/12/2008');
		return false;
	}

	return true;
}

/**************************************/
/* validate the email field */
function validateEmail(field) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) { return false; }
		else { return true; }
	}
}

/* date validation */
function isDate(dd,mm,yyyy) {
   var d = new Date(mm + "/" + dd + "/" + yyyy);
   return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
}
