// COMMON JAVASCRIPT FUNCTIONS

function IsObject(obj) {
	if (document.all(obj) != null) {
		return true;
	}
	return false;
}

function GetObject(obj) {

	isNS4 = (document.layers) ? true : false;
	isIE4 = (document.all && !document.getElementById) ? true : false;
	isIE5 = (document.all && document.getElementById) ? true : false;
	isNS6 = (!document.all && document.getElementById) ? true : false;

	if (isNS4) {
	   elem = document.layers[obj];
	}
	else if (isIE4) {
	   elem = document.all[obj];
	}
	else if (isIE5 || isNS6) {
	   elem = document.getElementById(obj);
	}

	return elem;
}

function doDelete(id,url,qs) {
	var redirect = "";
	var action = "Delete";
	var del = window.confirm('Are you sure you want to delete this item?');
	if (del) {
		if (isEmpty(qs)) {
			redirect = url + '?ac=' + action + '&ai=' + id;
		}
		else {
			redirect = url + '?' + qs + '&ac=' + action + '&ai=' + id;
		}
		window.location = redirect;
	}
}

function doCategoryDelete(id,url,qs) {
	var redirect = "";
	var action = "DeleteCategory";
	var del = window.confirm('Are you sure you want to delete this category?');
	if (del) {
		if (isEmpty(qs)) {
			redirect = url + '?ac=' + action + '&ai=' + id;
		}
		else {
			redirect = url + '?' + qs + '&ac=' + action + '&ai=' + id;
		}
		window.location = redirect;
	}
}

function doFileDelete(id,file,url,qs) {
	var redirect = "";
	var action = "DeleteFile";
	var del = window.confirm('Are you sure you want to delete this file?');
	if (del) {
		if (isEmpty(qs)) {
			redirect = url + '?ac=' + action + '&ai=' + id + '&af=' + file;
		}
		else {
			redirect = url + '?' + qs + '&ac=' + action + '&ai=' + id + '&af=' + file;
		}
		window.location = redirect;
	}
}

function doCheckBox(action,id,url,qs) {
	var redirect = "";
	if (isEmpty(qs)) {
		redirect = url + '?ac=' + action + '&ai=' + id;
	}
	else {
		redirect = url + '?' + qs + '&ac=' + action + '&ai=' + id;
	}
	window.location = redirect;
}

function doPageMove(action,id,url,qs) {
	var redirect = "";
	if (isEmpty(qs)) {
		redirect = url + '?ac=' + action + '&ai=' + id;
	}
	else {
		redirect = url + '?' + qs + '&ac=' + action + '&ai=' + id;
	}
	window.location = redirect;
}

function doSelect(field,url,qs) {
	var prefix = "";
	var redirect = "";
	var x = GetObject(field).value;
	if (!isEmpty(qs)) {
		prefix = "&";
	}
	var redirect = url + '?' + qs + prefix + field + '=' + x;
	window.location = redirect;
}

function doNavSelect(field,pageurl,url) {
	var prefix = "";
	var redirect = "";
	var x = GetObject(field).value;
	var p = url.indexOf(field);
	var newurl = "";
	if (p > 0) {
		newurl = url.substring(0,p - 1);
	} else {
		if (p == 0) {
			newurl = "";
		}
		else {
			newurl = url
		}
	}
	if (newurl != "") {
		var prefix = "&";
	}
	redirect = pageurl + "?" + newurl + prefix + field + "=" + x;
	if (IsObject("pn") == true) {
		redirect = redirect + "&pn=" + GetObject("pn").value + "&ps=" + GetObject("ps").value;
	}
	window.location = redirect;
}

function doSelectOption(field,url,qs) {
	var prefix = "";
	var redirect = "";
	var x = GetObject(field).value;
	var pn = GetObject("pn").value;
	var ps = GetObject("ps").value;
	if (!isEmpty(qs)) {
		var prefix = "&";
	}
	redirect = url + "?" + qs + prefix + "pn=" + pn + "&ps=" + ps + "&sb=" + field + "&so=" + x;
	window.location = redirect;
}

function doNav(pageurl,url) {
	var prefix = "";
	var redirect = "";
	var pn = GetObject("pn").value;
	var ps = GetObject("ps").value;
	if (!isEmpty(url)) {
		var prefix = "&";
	}
	redirect = pageurl + "?" + url + prefix + "pn=" + pn + "&ps=" + ps;
	window.location = redirect;
}

function changePage() {
	if (isInteger(GetObject("page").value)) return true;
	return false;
}

function setFocus(field) {
	field.focus();
}

function formSave(form) {
	GetObject(form).submit();
}

function formToggle(state,field) {
	var checkboxes = eval(field)
	for (a=0; a < checkboxes.length; a++)
		checkboxes[a].checked = state
}

function appWindow(url) {
	if ((screen.Height >= 0) && (screen.Width >= 0))
	{
		var height = screen.Height - 80;
		var width = screen.Width - 10;
	}
	else if ((screen.availHeight >= 0) && (screen.availWidth >= 0))
	{
		var height = screen.availHeight - 50;
		var width = screen.availWidth - 10;
	}
	appWin = window.open(url, "Services", "toolbar=0,status=1,location=no,menubar=no,directories=no,scrollbars=yes,resizable=yes,screenX=0,screen=0,left=0,top=0,width=" + width + ",height=" + height);
}

function openWindow(url) {
	x = window.open(url,'PopupWindow','width=450,height=450,top=100,left=100,scrollbars=yes,resizable=yes');
}

function openPromoWindow(url) {
	x = window.open(url,'PromoWindow','width=466,height=520,top=80,left=80,scrollbars=yes,resizable=yes');
}

function openReportWindow(url) {
	x = window.open(url,'ReportWindow','width=800,height=600,top=10,left=10,scrollbars=yes,resizable=yes');
}

function openFormWindow(url) {
	x = window.open(url,'FormWindow','width=600,height=600,top=80,left=40,scrollbars=yes,resizable=yes,status=yes');
}

function confirmDelete() {
	return window.confirm('Are you sure you want to delete these items?');
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isWhitespace(s) {
	if (isEmpty(s)) return true;
	var i;
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

function isBlank(s) {
	if (!isEmpty(s)) return false;
	if (!isWhitespace(s)) return false;
	return true;
}

function isAlphaNumeric(s) {
	var i;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		var d = validchars.indexOf(c);
		if (d < 0) {
			return false;
		}
	}
	return true;
}

function validEmail(s) {

	if (isEmpty(s)) return false;
	if (isWhitespace(s)) return false;

	var i = 1;
	var sLength = s.length;

	while ((i < sLength) && (s.charAt(i) != "@"))
	{ i++
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}

	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

function validateInteger(s) {
	if (!isInteger(s.value)) {
		s.focus();
		s.select();
		alert("Please enter a number.");
		return false;
	}
	return true;
}

function validateDouble(s) {
	if (!isFloat(s.value)) {
		s.focus();
		s.select();
		alert("Please enter a number with two decimal places.");
		return false;
	}
	return true;
}

function checkDate(field) {

	var checkstr = "0123456789";
	var DateField = field;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	err = 0;

	DateValue = DateField.value;

	for (i = 0; i < DateValue.length; i++) {
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
			DateTemp = DateTemp + DateValue.substr(i,1);
		}
	}

	DateValue = DateTemp;

	if (DateValue.length == 6) {
		DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2);
	}

	if (DateValue.length != 8) {
		err = 19;
	}

	year = DateValue.substr(4,4);

	if (year == 0) {
		err = 20;
	}

	month = DateValue.substr(2,2);

	if ((month < 1) || (month > 12)) {
		err = 21;
	}

	day = DateValue.substr(0,2);
	if (day < 1) {
		err = 22;
	}

	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
		leap = 1;
	}

	if ((month == 2) && (leap == 1) && (day > 29)) {
		err = 23;
	}

	if ((month == 2) && (leap != 1) && (day > 28)) {
		err = 24;
	}

	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
		err = 25;
	}

	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
		err = 26;
	}

	if ((day == 0) && (month == 0) && (year == 00)) {
		err = 0; day = ""; month = ""; year = ""; seperator = "";
	}

	if (err == 0) {
		DateField.value = day + seperator + month + seperator + year;
	} else {
		alert("Date format is incorrect. Please enter another date.");
		DateField.select();
		DateField.focus();
		return false;
	}
}

function validateEntryForm() {

	var a2 = false;
	var a3 = false;
	var a4 = false;

	if (document.EntryForm.Answer1.options[0].selected) {
		document.EntryForm.Answer1.focus();
		alert("Please answer question #1");
		return false;
	}

	if (document.EntryForm.Answer2.options[0].selected) {
		a2 = true;
	}
	
	x = window.document.EntryForm.CustomAnswer2;
	if ((isBlank(x.value)) && (a2)) {
		document.EntryForm.Answer2.focus();
		alert("Please answer question #2");
		return false;
	}

	if (document.EntryForm.Answer3.options[0].selected) {
		a3 = true;
	}
	
	x = window.document.EntryForm.CustomAnswer3;
	if ((isBlank(x.value)) && (a3)) {
		document.EntryForm.Answer3.focus();
		alert("Please answer question #3");
		return false;
	}

	if (document.EntryForm.Answer4.options[0].selected) {
		a4 = true;
	}
	
	x = window.document.EntryForm.CustomAnswer4;
	if ((isBlank(x.value)) && (a4)) {
		document.EntryForm.Answer4.focus();
		alert("Please answer question #4");
		return false;
	}

	x = window.document.EntryForm.FirstName;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter your first name.");
		return false;
	}

	x = window.document.EntryForm.LastName;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter your last name.");
		return false;
	}

	x = window.document.EntryForm.AddressSuburb;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter your suburb.");
		return false;
	}

	x = window.document.EntryForm.AddressPostcode;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter your postcode.");
		return false;
	}

	x = window.document.EntryForm.Phone;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter your phone.");
		return false;
	}

	x = window.document.EntryForm.Email;
	if (!validEmail(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a valid email address (ie. info@mangoes.com.au).");
		return false;
	}
}

function validateFeedbackForm() {

	x = window.document.FeedbackForm.ContactName;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a contact name.");
		return false;
	}

	x = window.document.FeedbackForm.Email;
	if (!validEmail(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a valid email address (ie. info@mangoes.com.au).");
		return false;
	}
}

function validatePageForm() {

	x = window.document.PageForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

}

function validateCategoryForm() {

	x = window.document.CategoryForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

}

function validateArticleForm() {

	x = window.document.ArticleForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}

	x = window.document.ArticleForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

	x = window.document.ArticleForm.Author;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter an author.");
		return false;
	}

}

function validateEventForm() {

	x = window.document.EventForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}

	x = window.document.EventForm.Starts;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a start date.");
		return false;
	}
	
	x = window.document.EventForm.Ends;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter an end date.");
		return false;
	}

	x = window.document.EventForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

	x = window.document.EventForm.Type;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a course type.");
		return false;
	}
	
	x = window.document.EventForm.Author;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter an author.");
		return false;
	}

}

function validateDocumentForm() {

	x = window.document.DocumentForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

	x = window.document.DocumentForm.Author;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter an author.");
		return false;
	}

	x = window.document.DocumentForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}


}

function validateProviderForm() {

	x = window.document.ProviderForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}

	x = window.document.ProviderForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

//	x = window.document.ProviderForm.Author;
//	if (isBlank(x.value)) {
//		x.focus();
//		x.select();
//		alert("Please enter an author.");
//		return false;
//	}

}

function validateRecipeForm() {

	x = window.document.RecipeForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}

	x = window.document.RecipeForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

	x = window.document.RecipeForm.Author;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter an author.");
		return false;
	}

}

function validateImageForm() {

	x = window.document.ImageForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}

	x = window.document.ImageForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

	x = window.document.ImageForm.Author;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter an author.");
		return false;
	}

}

function validateLinkForm() {

	x = window.document.LinkForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a name.");
		return false;
	}

//
//   js comment out wb002
//
//	x = window.document.LinkForm.Web;
//	if (isBlank(x.value)) {
//		x.focus();
//		x.select();
//		alert("Please enter a hyperlink.");
//		return false;
//	}

}

function validateTrainingForm() {

	x = window.document.TrainingForm.PublicationDate;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a publication date.");
		return false;
	}

//	x = window.document.TrainingForm.Date1;
//	if (isBlank(x.value)) {
//		x.focus();
//		x.select();
//		alert("Please enter day 1.");
//		return false;
//	}
	
//	x = window.document.TrainingForm.Date2;
//	if (isBlank(x.value)) {
//		x.focus();
//		x.select();
//		alert("Please enter day 2.");
//		return false;
//	}

	x = window.document.TrainingForm.Name;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a course location.");
		return false;
	}

	x = window.document.TrainingForm.Contact;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a trainer.");
		return false;
	}

	x = window.document.TrainingForm.Phone;
	if (isBlank(x.value)) {
		x.focus();
		x.select();
		alert("Please enter a phone number.");
		return false;
	}

}
