/**
 * find the position of an element on the page
 * to use for floating divs...
 */
function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/**
 * enable the next click to close any popups that we have open
 */
function setBodyClickHandler()
{
	document.onclick = function (event) {
		closePopups();
	}
}

/**
 * close any open popups...
 */
function closePopups()
{
	for (key in openPopups)
	{
		openPopups[key].className = "popup";
	}
	document.onclick = function(){};
	openPopups = new Array();
}

var openPopups = new Array();
/**
 * show a popup window...
 */
function showPopup(anchor, popupName)
{
	var popup = document.getElementById(popupName);
	if (openPopups.length > 0)
	{
		closePopups();
	}
	popup.className = "popupOver"
	position = findPos(anchor);
	//alert(position);
	popup.style.left = (position[0]+15)+"px";
	popup.style.top = position[1]+"px";
	openPopups[openPopups.length] = popup;
	setTimeout("setBodyClickHandler()", 1);
}

function toggleMoreInfo()
{
	var moreInfo = document.getElementById("moreinfo");
	if (moreInfo.style.display == "block")
	{
		moreInfo.style.display = "none";
	}
	else
	{
		moreInfo.style.display = "block";
	}
}


function validateQuestionnaire(theForm) {
	// make sure everything is checked
	valid = true;
	if (	(!theForm.q_existing[0].checked && !theForm.q_existing[1].checked) ||
			(!theForm.q_against_advice[1].checked && !theForm.q_against_advice[0].checked) ||
			(!theForm.q_waiting[1].checked && !theForm.q_waiting[0].checked) ||
			(!theForm.q_terminal[1].checked && !theForm.q_terminal[0].checked) ||
			(!theForm.q_pregnant[1].checked && !theForm.q_pregnant[0].checked) ||
			
			(!theForm.q_residents[1].checked && !theForm.q_residents[0].checked) ||
			(!theForm.q_location[1].checked && !theForm.q_location[0].checked) ||
			(!theForm.q_convictions[1].checked && !theForm.q_convictions[0].checked) ||
			(!theForm.q_previous_purchase[1].checked && !theForm.q_previous_purchase[0].checked) ||
			( theForm.q_previous_purchase[0].checked && !theForm.q_previous_decline[1].checked && !theForm.q_previous_decline[0].checked ) ||
			(!theForm.q_more_info[1].checked && !theForm.q_more_info[0].checked)
		)
	{
		alert("Please ensure all questions are answered");
		return false;
	}
	return true;
}

function validateDetails(theForm) {
	// okay, now check to ensure we have the contact information.
	if (theForm.brideFirstName.value == "")
	{
		alert("Please enter the bride's full name.");
		return false;
	}
	if (theForm.brideLastName.value == "")
	{
		alert("Please enter the bride's full name.");
		return false;
	}
	if (theForm.groomFirstName.value == "")
	{
		alert("Please enter the groom's full name.");
		return false;
	}
	if (theForm.groomLastName.value == "")
	{
		alert("Please enter the groom's full name.");
		return false;
	}
	if (theForm.weddingDate.value == "")
	{
		alert("Please enter the date for the wedding.");
		return false;
	}
	if (theForm.receptionDate.value == "")
	{
		alert("Please enter the date for the reception.");
		return false;
	}
	if (theForm.address.value == "")
	{
		alert("Please add the first line of your address");
		return false;
	}
	if (theForm.city.value == "")
	{
		alert("Please add your town or city");
		return false;
	}
	if (theForm.postcode.value == "")
	{
		alert("Please enter your post code");
		return false;
	}
	if (theForm.postcode.value.length < 3 || theForm.postcode.value.length > 4)
	{
		alert("Please re-enter your post code with at least 3 or 4 numbers");
		return false;
	}
	if (theForm.dayPhone.value == "")
	{
		alert("Please add your daytime telephone number");
		return false;
	}
	if (theForm.email.value.indexOf("@") == -1)
	{
		alert("Please enter your email address");
		return false;
	}
	if (theForm.email.value != theForm.confirmEmail.value)
	{
		alert("Please re-enter your email address");
		return false;
	}
	return true;
}

function validatePostage(theForm) {
	if ( (!theForm.mailDetails[0].checked && !theForm.mailDetails[1].checked) ) {
		alert("Please indicate whether you would like your certificate mailed to you.");
		return false;
	}
	return true;
}

function validatePDS(theForm) {
	if (theForm.productDisclosure.checked == false)
	{
		alert("Please check the box when you have read and agreed to your declaration.");
		return false;
	}
	return true;
}

function validatePayment(theForm) {
	if (theForm.cardName.value == "")
	{
		alert("Please enter the cardholder's name.");
		return false;
	}
	if (theForm.cardNo.value == "")
	{
		alert("Please enter your credit card number.");
		return false;
	}
	
	document.getElementById('process_btn').style.display = 'none'; 
	document.getElementById('process_txt').style.display = '';
	return true;
}

function populateFirstName(theInput) {
	var firstName = document.getElementById("contactFirstName");
	firstName.value = theInput.value;
}

function populateLastName(theInput) {
	var lastName = document.getElementById("contactLastName");
	lastName.value = theInput.value;
}