

// date stuff used in cached pages
dayofweek= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
dayofmonth= new Array("","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st");
months= new Array("January","February","March","April","May","June","July","August","September","October","November","December");

// returns todays date as a formated string
function theDate(){
	var mydate=new Date();
	var year=mydate.getYear();
	if (year < 1000) year+=1900;
	var day=mydate.getDay();
	var month=mydate.getMonth();
	var daym=mydate.getDate();
	todaysDate="  "+dayofweek[day]+" "+dayofmonth[daym]+" "+months[month]+" "+year;
	return todaysDate;
}

// returns todays date as a formated string
function theYear(){
	var mydate=new Date();
	var year=mydate.getYear();
	if (year < 1000) year+=1900;
	return year;
}
// Search box validation used for both the search box and the main search page
function searchpagevalidate(ref)
{
	var space_re = new RegExp('\\s+');
	var isEmpty = (ref.keyword.value.replace(space_re, '') == '') ? 1 : 0;
	if (!isEmpty)
		{
		return true;
		}
	else
		{
		alert('Need a keyword to search on!');
		return false;
		}		
}
function gotoarticle(sel)
	{
		artID=sel.options[sel.selectedIndex].value;
		if (artID.length > 0){
			aURL="index.aspx?articleid="+artID;
			window.location.href = aURL;
		}
	}
	
//scripts used in the forum template
// check all fields have been entered.
function  _forum_checkThreadForm(_forum_this){
	if  (_forum_this.forum_topic.value.length ==0){
		alert("You must enter a Title.");
		return false;
	}
	if  (_forum_this.forum_username.value.length ==0){
		alert("You must enter your name to post to the forum.");
		return false;
	}
	if  (_forum_this.forum_body.value.length ==0){
		alert("In order to post you need to enter text into the message.");
		return false;
	}
	return true;
} 
//variables and functions used in the calendar template
var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
function monthchange(sel){
	mnth=sel.options[sel.selectedIndex].value;
	aform=sel.form;
	if(aform.aday != null){
		if (mnth =='2'){
			theYear=aform.ayear.options[aform.ayear.selectedIndex].value;
			Days_in_Month[1] = ((theYear % 400 == 0) || ((theYear % 4 == 0) && (theYear % 100 !=0))) ? 29 : 28;
		}
		for(i=27; i < Days_in_Month[mnth-1]; i++){
			aform.aday.options[i].text=i+1;
		}
		for(i=Days_in_Month[mnth-1]; i < 31; i++){
			aform.aday.options[i].text='';
		}
		checkday(aform);
	}
	changer(sel);	
}

function checkday(aform){
	if (aform.aday.options[aform.aday.selectedIndex].text==''){
		aform.aday.options[eval(Days_in_Month[aform.amonth.selectedIndex]-1)].selected=true;
		aform.aday.focus();
	}
}

function changer(sel)
{
	aform=sel.form;
	dy=(aform.aday != null)?aform.aday.options[aform.aday.selectedIndex].value:1;
	mnth=aform.amonth.options[aform.amonth.selectedIndex].value;
	yer=aform.ayear.options[aform.ayear.selectedIndex].value;
	aid=aform.articleid.value;
	aURL="index.aspx?articleid="+aid+"&ayear="+yer+"&amonth="+mnth+"&aday="+dy;
	window.location.href = aURL;
}

//Functions Used by the commerce templates
function checkNum(id){	<!--- Check that the quantity is a positive integer --->
	var checkstr = /^[1-9][0-9]*$/;
	if  ( checkstr.test(id.value) ){ 
		id.value = id.value;	
	}else{
		alert ('You must specify a valid quantity');	
		id.value = 1;
	}
}

function showProductStatus(f,itemAttribs,ProductStatus){<!--- Search the stock array for  an itemAttribs and get it's price/availability info to the form --->
	qty = f.Quantity.value;
	if( (qty == '') || (isNaN(parseInt(qty))) ){
		qty = 0;  
	}else{
		qty = parseInt(qty);
		if (isNaN(qty)){
			qty = 1;
		}else if (qty < 0){
			qty = 0-qty;
		}
		f.Quantity.value = qty;
	}
	selectAttribs=""; <!--- Variable to hold Attributes selected by the user in a CSV string --->
	for(i = 0; i < f.length; i++)<!--- Scan the form and get product ProductStatus into PID --->
	{
		e = f.elements[i];
		if(e.name !=null && e.name.substr(0,7) == "Attrib_"){<!--- Get the selection from the form's DROP DOWN lists --->
			if(e.type == "select-one") selectAttribs=selectAttribs+e.options[e.selectedIndex].value+",";
			if(e.type == "hidden") selectAttribs=selectAttribs+e.value+",";
		}
	}
	selectAttribs = selectAttribs.substr(0,selectAttribs.length-1); 		<!--- Loose that final comma! --->
	for(i = 1; i < itemAttribs.length-1; i++){ 	<!--- Search the itemAttribs array for the Selected Attributes --->
		if(itemAttribs[i] == selectAttribs){break;}
	}<!--- If we get to here without a 'break' it's Out of Stock in that config. --->
	<!--- Put the details on to the form display ---> 
	if(ProductStatus[i].STK == 0 || ProductStatus[i].STK == -1){
		Avail = 'Out of Stock';
	}else{
		Avail = ProductStatus[i].STK ;
	}
	f.Availability.value = Avail;
	if (Avail != 'Out of Stock'){
		// the following may or may not exist
		if (f.PriceIncVAT != null)f.PriceIncVAT.value = formatNum(ProductStatus[i].PRCinc);
		if (f.PriceExVAT != null)f.PriceExVAT.value = formatNum(ProductStatus[i].PRCex);
		if (f.VAT != null)f.VAT.value = formatNum(ProductStatus[i].VAT);
		if (f.TotalIncVAT != null)f.TotalIncVAT.value = formatNum(f.PriceIncVAT.value * qty);
		//these always exist
		f.Code.value = ProductStatus[i].CID;
		f.ProductOptionID.value = ProductStatus[i].PID;
	}else{
		// the following may or may not exist
		if (f.PriceIncVAT != null)f.PriceIncVAT.value = "N/A";
		if (f.PriceExVAT != null)f.PriceExVAT.value = "N/A";
		if (f.VAT != null)f.VAT.value = "N/A";
		if (f.TotalIncVAT != null)f.TotalIncVAT.value = "N/A";
		//these always exist
		f.Code.value = "N/A"; 
		f.ProductOptionID.value = "N/A";
	}
} 	

function checkAvail(f){
	if(f.Availability.value="" || f.Availability.value == "Out of Stock"){
		alert("   Sorry, this item is not\navailable with the options\n    you have selected.");
		return false;
	}else{
		return true;
	}
}

function formatNum(value){
	value = "" + value <!--- convert value to string --->
	precision = 2; <!--- Decimal places --->
	width = 9; <!--- Formatted width in characters --->
	padding = "";
	for(p=0;p<width;p++) padding+=" "; <!--- Make a padding string --->
	var whole = "" + Math.round(value * Math.pow(10, precision));
	var decPoint = whole.length - precision;
	if(decPoint != 0){
		result = whole.substring(0, decPoint);
		result += ".";
		result += whole.substring(decPoint, whole.length);
	}else{
		result = whole;
	}
	result = padding+result;<!--- Left pad numbers so they line up --->
	result = result.substring(result.length-width, result.length);
	return result;
}
//Functions Used by the commerce templates end

// automatic print function
// (C) 2000 www.CodeLifter.com
// http://www.codelifter.com
// Free for all users, but leave in this  header

function printWindow(){
	bV = parseInt(navigator.appVersion)
	if (bV >= 4) window.print();
}

// Detect Client Browser type
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	  // NS/Opera version >= 3 check for Flash plugin in plugin array
	  if (navigator.plugins != null && navigator.plugins.length > 0) {
			if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
				  var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
						var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
						descArray = flashDescription.split(" ");
						tempArrayMajor = descArray[2].split(".");
						versionMajor = tempArrayMajor[0];
				if ( descArray[3] != "" ) {
						tempArrayMinor = descArray[3].split("r");
				} else {
						tempArrayMinor = descArray[4].split("r");
				}
						versionMinor = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
						flashVer = parseFloat(versionMajor + "." + versionMinor);
			} else {
				flashVer = -1;
			}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		flashVer = -1;
	}
	return flashVer;
}

// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 6r65 returns 6.65
// If called with reqMajorVer, reqMinorVer this function returns true if that version or greater is installed
function DetectFlashVer(reqMajorVer, reqMinorVer) 
{
	reqVer = parseFloat(reqMajorVer + "." + reqMinorVer);
	// loop backwards through the versions until we find the newest version      
	for (i=25;i>0;i--) {
			if (isIE && isWin && !isOpera) {;
				versionStr = VBGetSwfVer(i);
			} else {
				versionStr = JSGetSwfVer(i);
			}
			if (versionStr == -1) {
					return false;
			} else if (versionStr != 0) {
				if(isIE && isWin && !isOpera) {
						tempArray = versionStr.split(" ");
						tempString = tempArray[1];
						versionArray = tempString .split(",");

						versionMajor = versionArray[0];
						versionMinor   = versionArray[2];

						versionString = versionMajor + "." + versionMinor;
						versionNum = parseFloat(versionString);
				} else {
						versionNum = versionStr;
				}
				return (versionNum >= reqVer ? true : false );
			}
	}

	return (reqVer ? false : 0.0);
}

// Set-up external links for XHTML where the target attribute has been deprecated
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "icm") {
				var href = anchor.getAttribute("href");
				anchor.setAttribute("rel",href);
				anchor.removeAttribute("href");
				anchor.target = "_blank"; 
				// Open the window and add an eventlistener to it
				anchor.onclick = function() {
						if (window.addEventListener) {
							// Mozilla
							var _window = window.open(this.getAttribute("rel"));
							// Flag to determine where the page should be refreshed from
							var blogListRefresh = false;
							if (this.getAttribute("rel").indexOf("Delete") > -1)
								blogListRefresh = true;
								
							_window.addEventListener('unload',function(){RefreshParent(blogListRefresh);},false);  
						}
						else {
							// IE
							// Flag to determine where the page should be refreshed from
							var blogListRefresh = false;
							if (this.getAttribute("rel").indexOf("Delete") > -1)
								blogListRefresh = true;
							var _window = window.open(this.getAttribute("rel"));
							_window.attachEvent('onunload',function(){RefreshParent(blogListRefresh);});
						}
				}
		}
	}
	var forms = document.getElementsByTagName("form");
	for (var j=0; j <forms.length; j++)
	{
		var form = forms[j];
		if (form.getAttribute("id") != null && form.getAttribute("id").indexOf("pollform") == 0) {
			form.target = "_blank";
		}
	}
}

function checkPersonalOptions (ref)
{
	// Set up vars
	var errors = 0; // How many errors that has occured
	var message = "The following errors have occured:\n\n"; // Message displayed within alert window

	if (ref.sectorid.value == "") { // Test sector option
		errors++;
		message += errors + ". Please choose a sector.\n";
	}
	if (ref.professionid.value == "") { // Test profession option
		errors++;
		message += errors + ". Please choose a profession.\n";
	}
	if (errors > 0) { // At least one error has occurred, show alert.
		alert (message);
		return false;
	}
	return true; // If we get here no errors have occurred
}


//toggle the display of the part browser
function PartBrowserToggle(obj)
{
	//get the partbrowser wrapper div
	var el = document.getElementById(obj);
	//get the element that caused the event
	var e = window.event;
	if ( el.style.display != 'none' )
	{
		el.style.display = 'none';
		if (e.srcElement.value)
		{
			e.srcElement.value = "Add to page";
		} 
	}
	else
	{
		el.style.display = '';
		
		if (e.srcElement.value)
		{
			e.srcElement.value = "Add to page";
		} 
	}
	
	
}

function toggle(obj) {
	var el = document.getElementById(obj);
	//var elExpandBtn     =   document.getElementById(obj + "-expand");
	//var elCollapseBtn   =   document.getElementById(obj + "-collapse");
    
	if ( el.style.display != 'none' )
	{
		el.style.display = 'none';
		//elExpandBtn.style.display = 'inline';
		//elCollapseBtn.style.display = 'none';
		//document.getElementById(obj + "-menutab").style.borderBottom = "10px solid #EDEDED";
		// write cookie
		//writeCookie(obj, "1", "999");
	}
	else
	{
		el.style.display = '';
		//elExpandBtn.style.display = 'none';
		//elCollapseBtn.style.display = 'inline';
		//document.getElementById(obj + "-menutab").style.borderBottom = "0";
		//write cookie
		//writeCookie(obj, "0", "0");
	}
}

function writeCookie(name, value, days)
{
  if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	}
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
  return null;
}

function closePanel(objid)
{
	try
	{
		var obj = document.getElementById(objid);
		if (obj.style.display!='none')
		{
			obj.style.display='none';
			document.getElementById(objid + "-menutab").style.borderBottom = "10px solid #EDEDED";
			var expand = document.getElementById(objid + '-expand');
			var collapse = document.getElementById(objid + '-collapse');
			expand.style.display='block';
			collapse.style.display='none';
		}
	}
	catch(e)
	{
	}
}
// Catch the zoomRating function hook to update the Average rating element
function zoomVoteUpdate(newVoteCount) {
	var avRating = document.getElementById('votetotal');
	if (avRating) {
		if (newVoteCount == 0) {
			avRating.firstChild.data = "0 Votes";
		}
		if (newVoteCount == 1) {
			avRating.firstChild.data = "1 Vote";
		}
		if (newVoteCount > 1) {
			avRating.firstChild.data = newVoteCount + " Votes";
		}
	}
}

function zoomoutevent(ratings) {
	var results = ratings.split("|");
	for(i = 0; i < results.length; i++) {
		if (i < 5) {
			var itemsplit = results[i].split(",");
			var star = document.getElementById(itemsplit[0]);
			star.src=itemsplit[1];
		}
	}
}
