/*
Copyright Cayenne Web Development 2007
All rights reserved
*/

function LinkHighlight () {
	listItems=document.getElementById("navMain").getElementsByTagName("ul")[0].getElementsByTagName("li");
	for (var i=0; i<listItems.length; i++) {
		if (urlTest(location.href, listItems[i].getElementsByTagName("a")[0].href, 0)) {
			listItems[i].className=listItems[i].className + "current";
		}
	}
}

function urlTest(url1, url2, level) {
	var dirPattern = "";
	for (var i=0; i<Math.abs(level); i++) { dirPattern = dirPattern + "[^/]*/";}
	if (level == 0) return (url1 == url2);
	if (level > 0) {
		var pattern = new RegExp ("([a-zA-Z:]+//[a-zA-Z0-9.:]*)(/)(" + dirPattern + ")");
	} else {
		var pattern = new RegExp ("([a-zA-Z:]+//[a-zA-Z0-9.:]*)(.*)(" + dirPattern + "[^/]*/)([^/]*$)");
	}
	var parts1 = url1.match(pattern); 	var parts2 = url2.match(pattern);
	return (parts1 && parts2 && parts1[1] == parts2[1] && parts1[3] == parts2[3])
}




// For keyword search for brand, promoter, etc

function validateGenericSearch(form) {
	if (!validateTextBox(form.searchTerm,'Please enter a search term')) return false;
}



// /brand/add/step2.php; /brand/edit/step3.php;

function validateAddEditBrandForm(form, edit) {
	if (!validateTextBox(form.Name, 'Please enter the name of the Brand')) return false;
	if (!edit && !validateTextBox(form.Logo, 'Please select a logo for the Brand')) return false;
	if (!validateCheckBox(form.confirm, 'Please confirm that these details are accurate and have been checked with the promoter')) return false;
	return true;
}


// /promoter/edit/step2.php; /promoter/add/step1.php

function validateAddEditPromoterForm(form, edit) {
	if (!validateTextBox(form.Name, 'Please enter the name of the Promoter')) return false;
	if (!validateTextBox(form.Address, 'Please enter the address of the Promoter')) return false;
	if (!edit && !validateTextBox(form.Logo, 'Please select a logo for the Promoter')) return false;
	if (!validateCheckBox(form.confirm, 'Please confirm that these details are accurate and have been checked with the promoter')) return false;
	return true;
}


function validateAddEditPromotionForm(form) {
	if (!validateTextBox(form.Title, 'Please enter the title of the Promotion')) return false;
	if (!validateTextBox(form.ClosingDate, 'Please enter the closing date of the Promotion')) return false;
	if (!validateTextBox(form.WinnersDate, 'Please enter the data that the Winners List will be available')) return false;
	if (!validateTextBox(form.prize1, 'Please enter details for at least one prize') || !validateTextBox(form.quantity1, 'Please enter the details for at least one prize')) return false;
	if (!validateCheckBox(form.confirm, 'Please confirm that these details are accurate and have been checked with the promoter')) return false;
	return false;
}


function validateAddEditRmh(form) {
	if (!validateTextBox(form.Name, 'Please enter the name of the RMH')) return false;
	if (!validateTextBox(form.Address, 'Please enter the address of the RMH')) return false;
	if (!validateTextBox(form.Telephone, 'Please enter the RMH\'s telephone number')) return false;
	return true;
}


function validateEditUser(form) {
	if (!validateTextBox(form.Firstname, 'Please enter the First Name of the user')) return false;
	if (!validateTextBox(form.Lastname, 'Please enter the Last Name of the user')) return false;
	if (!validateTextBox(form.Username, 'Please enter the Username of the user')) return false;
	if (!validateTextBox(form.Email, 'Please enter the user\'s email address')) return false;
	if (form.PasswordNew.value != form.PasswordNew2.value) {
		alert ('To reset the password, please make sure that both new password boxes match');
		form.PasswordNew.focus();
		return false;
	}
	if (!validateTextBox(form.Email, 'Please enter an email address of the user')) return false;
	return true;
}


function validateAddUser(form) {
	if (!validateTextBox(form.Firstname, 'Please enter the First Name of the user')) return false;
	if (!validateTextBox(form.Lastname, 'Please enter the Last Name of the user')) return false;
	if (!validateTextBox(form.Username, 'Please enter the Username of the user')) return false;
	if (!validateTextBox(form.Email, 'Please enter the user\'s email address')) return false;
	if (!validateTextBox(form.PasswordNew, 'Please enter a password')) return false;
	if (form.PasswordNew.value != form.PasswordNew2.value) {
		alert ('Please make sure that both password boxes are identical');
		form.PasswordNew.focus();
		return false;
	}
	if (!validateTextBox(form.Email, 'Please enter an email address of the user')) return false;
	return true;
}

// Create a new prize box (/promotion/add/step3.php)
function addPrizeBox(id) {
	var container = document.getElementById(id).getElementsByTagName("ol")[0];
	var prizes = container.getElementsByTagName("li");
	var num = prizes.length + 1;
	var li = document.createElement("li");
	container.appendChild(li);
	li.innerHTML = '<label for="quantity' + num + '">QTY</label><input id="quantity' + num + '" type="text" name="quantity[]" value="" size="2"/> X <label for="prize' + num + '">Prize</label><input id="prize' + num + '" type="text" name="prize[]" value=""  size="30" />';
	return false
}


// Form validation functions


function validateSelect(box, message) {
	valid = false;
	if (box.selectedIndex && box.selectedIndex != 0) valid = true;
	if (!valid && message != "") {
		alert (message);
		box.focus();
	}
	return valid;
}

function validateRadio(button, message) {
	valid = false;
	for (i=0; i < button.length; i++) if (button[i].checked) valid = true;
	if (!valid && message != "") {
		alert (message);
		button[0].focus();
	}
	return valid;
}


function validateTextBox(box, message) {
	valid = false;
	if (box.value != "") valid = true;
	if (!valid && message != "") {
		alert (message);
		box.focus();
	}
	return valid;
}

function validateCheckBox(box, message) {
	valid = false;
	if (box.checked) valid = true;
	if (!valid && message != "") {
		alert (message);
		box.focus();
	}
	return valid;
}

