//This function opens a new window, first checking to see if one is already open
var gmyWin = null;

function myOpenWindow(strImage, intWidth, intHeight, strMessage, winObj)
{
  // this will hold our opened window
  var theWin; 
  var winName = 'myWin';

  // first check to see if the window already exists
  if (winObj != null)
  {
    //if the window exists, close it
    if (!winObj.closed) {
      	winObj.close();
    }
  }

  //open the window with the new parameters
  theWin = window.open('', winName, 'width=' + intWidth + ',height=' + intHeight + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizeable=yes');
  theWin.document.write('<html><head><style>body {background: #6d5e4e; color: #fff; font-family: arial; font-size: 0.9em; text-align: center;} a:link, a:visited {color: #ffa433; text-decoration: none;} a:hover, a:active {text-decoration: underline;} img {border: 1px solid #fff;}</style></head><body>' + '<p>' + strMessage + '</p><p><a href="javascript:window.close();">Close window</a></p><img src=\"' + strImage + '\" />' + '</body></html>');
  theWin.document.close();

  if (window.focus) {
  		theWin.focus();
	}

  return theWin;
}

//This function validates the booking
function validateBookingForm(objForm) {
	if (objForm.contact_name.value == "") {
		alert("Please enter a contact name.");
		objForm.contact_name.focus();
		return false;	
	}
	
	if (objForm.contact_city.value == "") {
		alert("Please enter your city.");
		objForm.contact_city.focus();
		return false;	
	}
	
	if ((objForm.contact_phone.value == "") && (objForm.contact_email.value == "")) {
		alert("Please enter a contact number or email address.");
		objForm.contact_phone.focus();
		return false;	
	}
	else if ((objForm.contact_phone.value != "") && (!checkPhoneNumberValid(objForm.contact_phone.value))) {
		alert("Please enter a valid contact number.");
		objForm.contact_phone.focus();
		return false;
	}
	else if ((objForm.contact_email.value != "") && (!checkEmailValid(objForm.contact_email.value))) {
		alert("Please enter a valid email address.");
		objForm.contact_email.focus();
		return false;
	}
	
	return true;
}

//Check if the phone number is valid
function checkPhoneNumberValid(number) {
	re = /^\+?\(?\d*\)?\s?\d*\s?\d*$/
	
	// Check that the value contains only numbers, one or more times
	if (!re.test(number)) {
		return false;
	}

	return true;
}

// Check email value
function checkEmailValid(email) {
	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/

	if (!re.test(email)) {
		return false;
	}

	return true;
}