var daysInMonth = new Array();
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function popupImage(imageId) {
	target = "/popupImage.php?id="+imageId;	
	win = window.open(target, "viewImage", "toolbar=no,scrollbars=yes,width=350,height=300,resizable");
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '')
};

function hasValue(str) {
	if (str == null) {
		return(false);
	}

	tstr = trim(str);
	return(tstr.length > 0);
}

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 saveCompetition(form) {
	var cName = form.competitionName.value;

	if (!hasValue(cName)) {
		alert("Please supply a name for the competition.");
		form.competitionName.focus();
		return(false);	
	}

	form.submit();
}

function deleteCompetition(id) {
	if (confirm("Are you sure you wish to delete this competition?")) {
		location = "index.php?action=deleteCompetition&id=" + id;
	}
}

function editCompetition(id) {
	location = "index.php?action=competitionform&id=" + id;
}

function showRegisterForm() {
	target = "registerform.php";
	win = window.open(target, "Register", "toolbar=no,scrollbars=yes,width=350,height=450,resizable");
}

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 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 deletePromotion(id) {
	confirmMsg = "Are you sure you wish to delete this promotion?";

	if (confirm(confirmMsg)) {
		target="/admin/?action=deletePromotion&promotionId=" + id;
		location = target;
	}
}

function sectionForm(id) {
   target = "sectionform.php?id=" + id;
   win = window.open(target, "SectionForm", "toolbar=no,scrollbars=yes,width=400,height=250")
}

function addSectionAttachment(form) {
   sectionId = form.sectionId.value;
   target = "addsectionattachmentform.php?sectionId=" + sectionId;
   win = window.open(target, "AddAttachment", "toolbar=no,scrollbars=yes,width=400,height=250")
}

function addSectionImage(form) {
   sectionId = form.sectionId.value;
   target = "addsectionimageform.php?sectionId=" + sectionId;
   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 = "/admin/logout.php";
	}
}

function closeDialog(loc) {
	opener.document.location = loc;
	window.close();
}

function competition() {
	target = "/main/competition.php";
	win = window.open(target, "popup", "toolbar=no,scrollbars=yes,width=600,height=500,resizable");
}

function milliSecondsToDays(mSeconds) {
	return Math.ceil( mSeconds/(1000 * 60 * 60 * 24));
}

function isLeapYear(year) {
	// A year is a leap year if year % 4 == 0
	// BUT: this does not apply to years that fall on 100
	// unless the are a multiple of 400
	//
	if ( ((year%400) == 0) || (((year%4) == 0) && (year%100 != 0)) ) {
		return(true);
	}
	return(false);
}

function isValidDate(day, month, year) {
	if (month != 2) {
		return(day <= daysInMonth[month]);
	}

	if (isLeapYear(year)) {
		maxdays = 29;
	} else {
		maxdays = 28;
	}	

	return(day <= maxdays);
}



function checkBookingInfoMalton(form){
	if (!(checkBookingInfo(form))){
		interstitialShowHide('hide');
		return false;
		}
	else {
		return true;
	}
}

function checkBookingInfo(form) {
	var arrDate = form.arrDate.value;
	var arrMonth = form.arrMonth.value;
	var arrYear = form.arrYear.value;
	var depDate = form.depDate.value;
	var depMonth = form.depMonth.value;
	var depYear = form.depYear.value;
	var timeDiff;
	var dayDif;

	var adate = arrDate + "/" + arrMonth + "/" + arrYear;
	var ddate = depDate + "/" + depMonth + "/" + depYear;

	if ( adate == ddate) {
		alert("Your arrival and departure dates must be different.");
		return(false);
	}

	if (!isValidDate(arrDate, arrMonth, arrYear)) {
		alert("Your arrival date " + adate + " appears not to be valid. Please re-enter it.");
		return(false);
	}

	if (!isValidDate(depDate, depMonth, depYear)) {
		alert("Your departure date " + ddate + " appears not to be valid. Please re-enter it.");
		return(false);
	}

	today = new Date();
	arrivalDate = new Date(arrYear, (arrMonth - 1), arrDate);
	departureDate = new Date(depYear, (depMonth - 1), depDate);

	timeDiff = (arrivalDate.getTime() - today.getTime());
	dayDiff = milliSecondsToDays(timeDiff);
	
	if (dayDiff < 0) {
		alert("Sorry, your arrival date corresponds to a date in the past. Please re-enter your date.");
		return(false);
	}

	timeDiff = (departureDate.getTime() - arrivalDate.getTime());
	dayDiff = milliSecondsToDays(timeDiff);
	
	if (dayDiff < 0) {
		alert("Sorry, your departure date preceeds your arrival date. Please re-enter your dates.");
		return(false);
	}

	return(true);
}

function switchSite(form) {
	var siteId = form.site[form.site.selectedIndex].value;
	var dest = "switchsite.php?siteId=" + siteId;
	location = dest;
}
