function uploadProducts() {
	target = "/admin/productuploadform.php";
	win = window.open(target, "Products", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function uploadCategories() {
	target = "/admin/categoryuploadform.php";
	win = window.open(target, "Categories", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}


function exportCatalog() {
	target = "/cgi-bin/catalog_export.pl";
	win = window.open(target, "Catalog", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function importCatalog() {
	target = "/admin/cataloguploadform.php";
	win = window.open(target, "Categories", "toolbar=no,scrollbars=yes,width=400,height=300,resizable");
}

function setPromotion(code) {
	target = "/admin/setpromotion.php?code="+code;
	win = window.open(target, "Promotion", "toolbar=no,scrollbars=yes,width=300,height=200,resizable");
}

function setPromotions() {
	target = "/admin/promotionform.php";
	win = window.open(target, "Promotions", "toolbar=no,scrollbars=yes,width=300,height=200,resizable");
}

function getExtension(filename) {
	var tokens = filename.split(".");
	return(tokens[tokens.length-1]);
}

function isNumeric(str) {
	var validChars = "0123456789.";
	var isNumber=true;
	var cchar;
 
	// Foreach char in the string, check 
	// that it appears in validChars
	for (i=0; i<str.length; i++) { 
		cchar = str.charAt(i); 
		if (validChars.indexOf(cchar) == -1) {
			isNumber = false;
			break;
		}
	}

	return(isNumber);
}

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

function hasValue(val) {

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

	var s = trim(val);

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

	return(true);
}
   
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 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 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 = "/cgi-bin/logout.pl";
	}
}

function sendContact(form) {

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

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

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

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

function siteRegister(form) {

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

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

	if (!isEmail(form.email.value)) {
		alert("Your email address appears to be invalid, please re-enter it.");
		form.email.focus();
		return(false);
	}
	
	/*if (!hasValue(form.phone.value)) {
		alert("Please supply your phone number");
		form.phone.focus();
		return(false);
	}

	if (!hasValue(form.mobile.value)) {
		alert("Please supply your mobile phone number");
		form.mobile.focus();
		return(false);
	}*/
	
	if (!hasValue(form.address1.value)) {
		alert("Please supply your address");
		form.address1.focus();
		return(false);
	}
	
	form.submit();
}

function search(form) {
	if (!hasValue(form.terms.value)) {
		alert("Please supply a search term.");
		form.terms.focus();
		return(false);
	}

	form.submit();
}

