
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
};

function isNumeric(str) {
	var validChars = "0123456789.";
	var isNumber=true;
	var cchar;
 
	for (i=0; i<str.length; i++) { 
		cchar = str.charAt(i); 
		if (validChars.indexOf(cchar) == -1) {
         isNumber = false;
			break;
		}
	}

	return(isNumber);
}
   
function isEmail(s) {
	var i = 1;
	var sLength = s.length;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@")) { 
		i++; 
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) {
		return false;
	} else {
		i += 2;
	}

	// look for .
	while ((i < sLength) && (s.charAt(i) != ".")) { 
		i++; 
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
		return false;
	} else {
		return true;
	}
}

function hasValue(val) {
    if (val == null) {
        return(false);
    }

    var s = trim(val);

    if (s == "") {
        return(false);
    }

    return(true);

}

function showRegisterForm(form) {
	var target = "/response.php?action=registerform";
	win = window.open(target, "Register", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function checkRegistration(form) {
	var email = form.email.value;

	if (!hasValue(email)) {
		alert("Please enter your email address");
		form.email.focus();
		return;
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return;
	}

	form.submit();
}

function memberRegister(form) {

	var account_number = trim(form.account_number.value);
	var fname = trim(form.fname.value);
	var sname = trim(form.sname.value);
	var company_name = trim(form.company_name.value);
	var address1 = trim(form.address1.value);
	var email_1 = trim(form.email_1.value);
	var email_2 = trim(form.email_2.value);
	var countyIndex = form.county.selectedIndex;

	if (account_number.length != 8) {
		alert("Please provide your 8 digit customer account number.");
		form.account_number.focus();
		return;
	}

	if (!isNumeric(account_number)) {
		alert("Your account number appears to be invalid. Note that this must be an 8 digit number.");
		form.account_number.focus();
		return;
	}

	if (fname == "") {
		alert("Please provide your first name.");
		form.fname.focus();
		return;
	}

	if (sname == "") {
		alert("Please provide your surname.");
		form.sname.focus();
		return;
	}

	if (company_name == "") {
		alert("Please provide your company name.");
		form.company_name.focus();
		return;
	}

	if (address1 == "") {
		alert("Please provide your company address.");
		form.address1.focus();
		return;
	}

	if (countyIndex <= 0) {
		alert("Please select your county");
		return;
	}

	if (email_1 == "") {
		alert("Please provide your email address.");
		form.email_1.focus();
		return;
	}

	if (email_2 == "") {
		alert("Please re-enter your email address.");
		form.email_2.focus();
		return;
	}

	if (email_1 != email_2) {
		alert("Note that your email address has been re-entered differently.\nPlease enter the same email address in both fields.");
		form.email_1.focus();
		return;
	}

	if (!isEmail(email_1)) {
		str = "Your address " + email_1 + " does not look like a valid email adddress.\nPlease re-enter your email address.";
		alert(str);
		form.email_1.focus();
		return;
	}

	form.submit();
}

function terms() {
	target = "/terms.html";
	win = window.open(target, "TermsAndConditions", "toolbar=no,scrollbars=yes,width=400,height=450")
}

function siteSearch(form) {
	//alert("Search");
	form.submit();
}

// Returns a random positive integer > 0 <= upperBound
function randomInt(upperBound) {
	return( Math.round((Math.random()*upperBound) + 0.5) );
}

function randomBanner() {
	var idx = randomInt(5);
	var imageName = "images/banner" + idx + ".jpg";
	return("<img alt='Tower Interactive' src='" + imageName + "'/>");
}

function loginhelp() {
	msg = "For assistance logging in, please contact webmaster@tower.ie.\nThank You";
	alert(msg);
}

function login(form) {
   // Log in admin users
   user = form.adminuser.value;
   passwd = form.adminpasswd.value;

   if ( (user == null) || (user.length == 0) ) {
      alert("Please Supply Your Username.");
      form.adminuser.focus();
      return(false);
   }

   if ( (passwd == null) || (passwd.length == 0) ) {
      alert("Please Supply a Password.");
      form.adminpasswd.focus();
      return(false);
   }

   form.submit();
}

function moveSection(id) {
   target="movesectionform.php?sid=" + id;
   win = window.open(target, "MoveSection", "toolbar=no,scrollbars=yes,width=300,height=250")
}

function deleteSection(id, parent_id, cnt) {
   nonEmptyMsg = "This section contains other subsections.\nYou must delete these before proceeding.";
   confirmMsg = "Are you sure you wish to delete this section?";

   if (cnt > 0) {
      alert(nonEmptyMsg);
      return(false);
   }

   if (confirm(confirmMsg)) {
      target="deletesection.php?sid=" + id + "&parent_id=" + parent_id;
      win = window.open(target, "DeleteSection", "toolbar=no,scrollbars=yes,width=300,height=150")
   }
}

function sectionForm(id) {
   target = "sectionform.php?id=" + id;
   win = window.open(target, "SectionForm", "toolbar=no,scrollbars=yes,width=400,height=250")
}

function addSectionAttachment(form) {
   section_id = form.section_id.value;
   target = "addsectionattachmentform.php?section_id=" + section_id;
   win = window.open(target, "AddAttachment", "toolbar=no,scrollbars=yes,width=400,height=250")
}

function addSectionImage(form) {
   section_id = form.section_id.value;
   target = "addsectionimageform.php?section_id=" + section_id;
   win = window.open(target, "AddImage", "toolbar=no,scrollbars=yes,width=400,height=250")
}

function popup(link) {
	win = window.open(link, "popup")
}

function setColour(id,ON){
	var el = document.getElementById(id);
	if(ON){
		el.style.backgroundColor='#C6CFDC';
	} else {
		el.style.backgroundColor='transparent';
	}
}

function adminLogout() {
	if (confirm("Are you sure you wish to logout?")) {
		location = "logout.php";
	}
}

function checkSignup(form) {
	var signup_name = form.signup_name.value;
	var signup_email = form.signup_email.value;

	if (!hasValue(signup_name)) {
		alert("Please supply your name.");
		form.signup_name.focus();
		return(false);
	}

	if (!hasValue(signup_email)) {
		alert("Please supply your email address.");
		form.signup_email.focus();
		return(false);
	}

	if (!isEmail(signup_email)) {
		alert("Your email address appears not to be valid, please re-enter it.");
		form.signup_email.focus();
		return(false);
	}

	var signup_name_enc = escape(signup_name);
	var signup_email_enc = escape(signup_email);

	var target = "/signup.php?name="+signup_name_enc+"&email="+signup_email_enc;
	var win=window.open(target, "signup", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function resetPassword(form) {
	var email = form.email.value;

	if (!hasValue(email)) {
		alert("Please enter your email address in order to register with HR.");
		form.email.focus();
		return;
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return;
	}

	var email_enc = escape(email);
	var target = "/response.php?action=pwreset&email="+email_enc;
	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function hr_register(form) {
	var email = form.email.value;

	if (!hasValue(email)) {
		alert("Please enter your email address in order to register with HR.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	var email_enc = escape(email);
	var target = "/response.php?action=hr_register&email="+email_enc;
	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function register(form) {
	var email = form.email.value;

	if (!hasValue(email)) {
		alert("Please enter your email address in order to register for notifications.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	var email_enc = escape(email);
	var target = "/response.php?action=register&email="+email_enc;
	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function requestBrochure(form) {
	var email = form.email.value;
	var address = form.address.value;

	if (!hasValue(email)) {
		alert("Please enter your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	if (!hasValue(address)) {
		alert("Please enter your postal address.");
		form.address.focus();
		return(false);
	}

	var email_enc = escape(email);
	var address_enc = escape(address);

	var target = "/response.php?action=brochure&email="+email_enc+"&address="+address_enc;
	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function requestDvd(form) {
	var email = form.email.value;
	var address = form.address.value;

	if (!hasValue(email)) {
		alert("Please enter your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	if (!hasValue(address)) {
		alert("Please enter your postal address.");
		form.address.focus();
		return(false);
	}

	var email_enc = escape(email);
	var address_enc = escape(address);

	var target = "/response.php?action=dvd&email="+email_enc+"&address="+address_enc;
	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function sendContact(form) {
	var cname = form.cname.value;
	var phone = form.phone.value;
	var email = form.email.value;
	var comment = form.comment.value;
	var venueName;


	if (!hasValue(cname)) {
		alert("Please enter your name.");
		form.cname.focus();
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please enter your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	for (i=0; i<form.venue.length; i++) {
		if (form.venue[i].checked) {
			venueName = form.venue[i].value;
		}
	}


	var cname_enc = escape(cname);
	var phone_enc = escape(phone);
	var email_enc = escape(email);
	var comment_enc = escape(comment);
	var venue_enc = escape(venueName);

	var target = "/response.php?action=contact&cname="+cname_enc+"&phone="+phone_enc+"&email="+email_enc+"&phone="+phone_enc+"&venue="+venue_enc+"&comment="+comment;

	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function sendReservation(form) {
	var cname = form.cname.value;
	var phone = form.phone.value;
	var email = form.email.value;
	var rdate = form.rdate.value;
	var comment = form.comment.value;
	var venueName;


	if (!hasValue(cname)) {
		alert("Please enter your name.");
		form.cname.focus();
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please enter your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	if (!hasValue(rdate)) {
		alert("Please enter your required date.");
		form.rdate.focus();
		return(false);
	}

	for (i=0; i<form.venue.length; i++) {
		if (form.venue[i].checked) {
			venueName = form.venue[i].value;
		}
	}


	var cname_enc = escape(cname);
	var phone_enc = escape(phone);
	var email_enc = escape(email);
	var comment_enc = escape(comment);
	var venue_enc = escape(venueName);
	var rdate = escape(rdate);

	var target = "/response.php?action=reservation&cname="+cname_enc+"&phone="+phone_enc+"&email="+email_enc+"&phone="+phone_enc+"&venue="+venue_enc+"&rdate="+rdate+"&comment="+comment;

	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function sendVoucherForm(form) {
	var cname = form.cname.value;
	var phone = form.phone.value;
	var email = form.email.value;
	var vname = form.vname.value;
	var vvalue = form.vvalue.value;

	var venueName;


	if (!hasValue(cname)) {
		alert("Please enter your name.");
		form.cname.focus();
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please enter your email address.");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	if (!hasValue(vname)) {
		alert("Please enter the name you wish to appear on your voucher.");
		form.vname.focus();
		return(false);
	}

	if (!hasValue(vvalue)) {
		alert("Please enter the value you require for your voucher.");
		form.vname.focus();
		return(false);
	}

	for (i=0; i<form.venue.length; i++) {
		if (form.venue[i].checked) {
			venueName = form.venue[i].value;
		}
	}


	var cname_enc = escape(cname);
	var phone_enc = escape(phone);
	var email_enc = escape(email);
	var vname = escape(vname);
	var vvalue = escape(vvalue);
	var venue_enc = escape(venueName);

	var target = "/response.php?action=voucher&cname="+cname_enc+"&phone="+phone_enc+"&email="+email_enc+"&venue="+venue_enc+"&vname="+vname+"&vvalue="+vvalue;

	var win=window.open(target, "Response", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function search(form) {
	var terms = form.terms.value;

	if (!hasValue(terms)) {
		alert("Please enter a term to search for.");
		form.terms.focus();
		return(false);
	}

	form.submit();
}

function jobSearch(form) {
	var terms = form.terms.value;

	if (!hasValue(terms)) {
		alert("Please enter a term to search for.");
		form.terms.focus();
		return(false);
	}

	form.submit();
}

function registerProfile(form) {
	var minPasswordLength = 6;

	var pname = form.pname.value;
	var email = form.email.value;
	var password1 = form.password1.value;
	var password2 = form.password2.value;

	if (!hasValue(pname)) {
		alert("Please enter your name");
		form.pname.focus();
		return(false);
	}

	if (!hasValue(email)) {
		alert("Please enter your email address");
		form.email.focus();
		return(false);
	}

	if (!isEmail(email)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}

	if (!hasValue(password1)) {
		alert("Please enter your password.");
		form.password1.focus();
		return(false);
	}

	if (password1.length < minPasswordLength) {
		alert("Please note, your password must be at least " + minPasswordLength + " characters.\nPlease re-enter your password.");
		form.password1.focus();
		return(false);
	}

	if (!hasValue(password2)) {
		alert("Please confirm your password.");
		form.password2.focus();
		return(false);
	}

	if (password1 != password2) {
		alert("Your passwords do not match, please re-enter your password");
		form.password2.focus();
		return(false);
	}
	
	form.submit();
}

function saveProfile(form) {
	form.submit();
}
