/*
JavaScript Browser, OS, and Flash Version Detection Engine
Copyright © 2005 Phizz Graphic Design Studio
*/

var isIE = false;
var isNS = false;
var isFlash5 = false;
var isFlash6 = false;
var isFlash7 = false;

// Browser Detection
isIE = (navigator.appName.indexOf('Microsoft') == 0);
isNS = (navigator.appName.indexOf('Netscape') == 0);
isMac = (window.navigator.appVersion.indexOf("Mac") != -1);
isWindows = navigator.userAgent.indexOf("Windows 95") != -1 || navigator.userAgent.indexOf("Windows 98") != -1 || navigator.userAgent.indexOf("Windows NT") != -1;
isUNIX = (window.navigator.appVersion.indexOf("X11") != -1) || (window.navigator.appVersion.indexOf("Linux") != -1) || (window.navigator.appVersion.indexOf("SunOS") != -1) || (window.navigator.appVersion.indexOf("IRIX") != -1) || (window.navigator.appVersion.indexOf("HP-UX") != -1);
isMacIE = (isMac && isIE);

// Flash Version
if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]  && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
    if (navigator.plugins && navigator.plugins["Shockwave Flash"] && (versionIndex = navigator.plugins["Shockwave Flash"].description.indexOf(".")) != - 1) {
        var versionString = navigator.plugins["Shockwave Flash"].description.substring(versionIndex-1, versionIndex);
        versionIndex = parseInt(versionString);
        isFlash5 = (versionIndex >= 5);
        isFlash6 = (versionIndex >= 6);
        isFlash7 = (versionIndex >= 7);
    }
} else if (isIE && isWindows) {
    document.write('<script language="VBScript">\n');
    document.write('on error resume next\n');
    document.write('isFlash5 = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5")))\n');
    document.write('isFlash6 = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6")))\n');
    document.write('isFlash7 = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7")))\n');
    document.write('</script>\n');
}

function SubmitMailingList() {
    if (document.getElementById("txtMLName").value == "") {
        alert ("Please give a value for the Name field");
        document.getElementById("txtMLName").focus();
    } else if (document.getElementById("txtMLEmail").value == "") {
        alert ("Please give a value for the Email field");
        document.getElementById("txtMLEmail").focus();
    } else if (document.getElementById("txtMLEmail").value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != 0) {
        alert ("The email address you entered does not appear to be valid. Please check it and try again.");
        document.getElementById("txtMLEmail").focus();
    } else {
        window.location.href = "MailingList.aspx?action=add&name=" + escape(document.getElementById("txtMLName").value) + "&email=" + escape(document.getElementById("txtMLEmail").value);
    }
}

function WriteTitle() {
    if (isFlash6) {
        document.write(
            "<div style=\"width:780px;height:174px;margin-bottom:2px;\">"
            + "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"780\" height=\"174\" id=\"swfTitle\" align=\"middle\">"
            + "<param name=\"allowScriptAccess\" value=\"sameDomain\" />"
            + "<param name=\"movie\" value=\"title.swf\" />"
            + "<param name=\"loop\" value=\"false\" />"
            + "<param name=\"menu\" value=\"false\" />"
            + "<param name=\"quality\" value=\"high\" />"
            + "<param name=\"bgcolor\" value=\"#ffffff\" />"
            + "<embed src=\"title.swf\" loop=\"false\" menu=\"false\" quality=\"high\" bgcolor=\"#ffffff\" width=\"780\" height=\"174\" name=\"Header\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />"
            + "</object>"
            + "</div>"
        );
    } else {
        document.write("<img src=\"i/title.jpg\" width=780 height=174 />");
    }
}

function GetElement(id) {
	return document.getElementById(id);
}

function GetElements(name) {
	return document.getElementsByName(name);
}

function Trim(s) {
	s = s.replace(/\xA0+/g, "");
	return s.replace(/(^\s+)|(\s+$)/g, "");
}

function ValExists(id) {
	var obj = GetElement(id);
	if (Trim(obj.value).length == 0) {
		alert ("The " + obj.title + " field is required. Please enter a valid value.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function ValEquivalent(id1, id2) {
	var obj1 = GetElement(id1);
	var obj2 = GetElement(id2);
	if (Trim(obj1.value) != Trim(obj2.value)) {
		alert ("The " + obj1.title + " and " + obj2.title + " field values must be equivalent. Please check and try again.");
		obj1.focus();
		return false;
	} else {
		return true;
	}
}

function ValRegex(id, regExp, errorMessage, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	if (!regExp.test(obj.value)) {
		alert (errorMessage);
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function ValEmail(id, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	if (!IsEmail(obj.value)) {
		alert ("Please enter a valid email address in the " + obj.title + " field.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function IsEmail(s) {
	return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(s);
}

function ValPostcode(id, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	if (!IsPostcode(obj.value)) {
		alert ("Please enter a valid postcode in the " + obj.title + " field.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function IsPostcode(s) {
	return /^[A-Za-z][A-Za-z]?[0-9][A-Za-z0-9]? [0-9][A-Za-z][A-Za-z]$/.test(s);
}

function ValInteger(id, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	if (!IsInteger(obj.value)) {
		alert ("Please enter a valid whole number value in the " + obj.title + " field.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function IsInteger(s) {
	return /^-?\d+$/.test(s);
}

function ValDecimal(id, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	if (!IsDecimal(obj.value)) {
		alert ("Please enter a valid numeric value in the " + obj.title + " field.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function IsDecimal(s) {
	return /^-?\d+(\.\d+)?$/.test(s);
}

function ValDate(id, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	if (!IsDate(obj.value)) {
		alert ("Please enter a valid date value in the " + obj.title + " field, in the format [dd-Mmm-yyyy]. If unsure, please use the Date Picker.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function IsDate(s) {
	var isValid = true;
	var dateParts = (s.indexOf("-") > -1 ? s.split("-") : new Array());
	if (dateParts.length != 3) isValid = false;
	if (isValid) {
		var dayPart = dateParts[0]; if (dayPart.length == 2 && dayPart.substr(0,1) == "0") dayPart = dayPart.substr(1,1);
		var monthPart = dateParts[1].toLowerCase(); monthPart = monthPart.substr(0,1).toUpperCase() + monthPart.substr(1);
		var yearPart = dateParts[2];
		if (!/^[0-9]{1,2}$/.test(dayPart) || !/^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)$/.test(monthPart) || !/^[1-9]{1}[0-9]{3}$/.test(yearPart)) isValid = false;
		if (isValid) {
			dayPart = parseInt(dayPart);
			yearPart = parseInt(yearPart);
			if (dayPart < 1 || dayPart > 31 || (dayPart > 30 && /^(Apr|Jun|Sep|Nov)$/.test(monthPart)) || (dayPart > 29 && monthPart == "Feb") || (dayPart == 29 && monthPart == "Feb" && !IsLeapYear(yearPart))) isValid = false;
		}
	}
	return isValid;
}

function ValDateTime(id, allowNullValue) {
	var obj = GetElement(id);
	if (allowNullValue && obj.value.length == 0) return true;
	var parts = obj.value.split(" ");
	if ((parts.length == 2) && IsDate(parts[0]) && IsTime(parts[1])) {
		return true;
	} else {
		alert ("Please enter a valid date/time value in the " + obj.title + " field, in the format [dd-Mmm-yyyy hh:mm]. The time value must be between 00:00 and 23:59, with a preceding 0 if applicable. If unsure, please use the Date/Time Picker.");
		obj.focus();
		return false;
	}
}

function IsTime(s) {
	return /^([0-1][0-9]|2[0-3]):[0-5][0-9]$/.test(s);
}

function ValSelected(id) {
	var obj = GetElement(id);
	if (obj.selectedIndex == 0) {
		alert ("A value must be selected for the " + obj.title + " field.");
		obj.focus();
		return false;
	} else {
		return true;
	}
}

function ValChecked(name, customMessage) {
	var objs = GetElements(name);
	if (objs == null || objs.length == 0) return true;
	var checked = false;
	for (var i = 0; i < objs.length; i++) {
		if (objs[i].checked) {checked = true; break;}
	}
	if (!checked) {
		if (customMessage != null) {
			alert (customMessage);
		} else {
			alert ("A value must be selected for the " + objs[0].title + " field.");
		}
		objs[0].focus();
		return false;
	} else {
		return true;
	}
}

function ValNumberChecked(name, targetNumberChecked, rule) {
	var objs = GetElements(name);
	if (objs == null || objs.length == 0) return true;
	var numberChecked = 0;
	for (var i = 0; i < objs.length; i++) {
		if (objs[i].checked) {
			numberChecked++;
		}
	}
	if (rule == "exact" && numberChecked != targetNumberChecked) {
		alert ("Exactly " + targetNumberChecked + " values must be selected for the " + objs[0].title + " field.");
		objs[0].focus();
		return false;
	} else if (rule == "min" && numberChecked < targetNumberChecked) {
		alert ("A minimum of " + targetNumberChecked + " values must be selected for the " + objs[0].title + " field.");
		objs[0].focus();
		return false;
	} else if (rule == "max" && numberChecked > targetNumberChecked) {
		alert ("A maximum of " + targetNumberChecked + " values must be selected for the " + objs[0].title + " field.");
		objs[0].focus();
		return false;
	} else {
		return true;
	}
}

