// date stuff used in cached pages
var dayofweek= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var 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");
var 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();
	var todaysDate="  "+dayofweek[day]+" "+dayofmonth[daym]+" "+months[month]+" "+year;
	return todaysDate;
}

// Search box validation used for both the search box and the main search page
function searchpagevalidate(ref) {
	if (ref.keyword.value != '')
		{
		return true;
		}
	else
		{
		alert('Need a keyword to \nsearch on!');
		return false;
		}		
}
function gotoarticle(sel)
	{
		artID=sel.options[sel.selectedIndex].value;
		if (artID.length > 0){
			aURL="index.cfm?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.cfm?articleid="+aid+"&ayear="+yer+"&amonth="+mnth+"&aday="+dy;
	window.location.href = aURL;
}

function genWindowOpen(url,wName,wWidth,wHeight)
{
	var remote = window.open(url,wName,'width='+wWidth+', height='+wHeight+',innerwidth='+wWidth+',innerheight='+wHeight+', alwaysLowered=0, alwaysRaised=1, channelmode=0, dependent=0, directories=0, fullscreen=0, hotkeys=1, location=0, menubar=1, resizable=1, scrollbars=1, status=0, titlebar=1, toolbar=0, z-lock=0');
	if (remote.opener == null) remote.opener = window;
	remote.opener.name = 'Right';
	remote.focus();
	return false;
}

//
// Used with the council tax template
//
function postit(){ //check postcode format is valid
test = document.details.postcode.value; size = test.length
if (size < 5 ){ //Code length rule
  alert(test + " address should be 5 characters or more");
  document.details.postcode.focus();
  return false;
  }
	return true;
}

// All the below remmed out by Mike as I now validate for address parameter > 4 chars

// test = document.details.postcode.value; size = test.length
// test = test.toUpperCase(); //Change to uppercase
// while (test.slice(0,1) == " ") //Strip leading spaces
//  {test = test.substr(1,size-1);size = test.length
//  }
// while(test.slice(size-1,size)== " ") //Strip trailing spaces
//  {test = test.substr(0,size-1);size = test.length
//  }
// document.details.postcode.value = test; //write back to form field
// if (size < 6 || size > 8){ //Code length rule
//  alert(test + " is not a valid postcode - wrong length");
//  document.details.postcode.focus();
//  return false;
//  }
// if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
//   alert(test + " is not a valid postcode - cannot start with a number");
//   document.details.postcode.focus();
//   return false;
//  }
// if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
//   alert(test + " is not a valid postcode - alpha character in wrong position");
//   document.details.postcode.focus();
//  return false;
//  }
// if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
// alert(test + " is not a valid postcode - number in wrong position");
//   document.details.postcode.focus();
//   return false;
//  }
// if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
//   alert(test + " is not a valid postcode - number in wrong position");
//   document.details.postcode.focus();
//   return false;
//  }
// if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
//   alert(test + " is not a valid postcode - no space or space in wrong position");
//   document.details.postcode.focus();
//   return false;
//   }
// count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
// if (count1 != count2){//only one space rule
//   alert(test + " is not a valid postcode - only one space allowed");
//   document.details.postcode.focus();
//   return false;
//  }
// alert("Postcode Format OK");

//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 = 'Unavailable';
	}else{
		Avail = ProductStatus[i].STK ;
	}
	f.Availability.value = Avail;
	if (Avail != 'Unavailable'){
		// 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;
		f.basketaction.style.display='block'
	}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";
		f.basketaction.style.display='none';
	}
} 	

function checkAvail(f){
	if(f.Availability.value == "" || f.Availability.value == "Unavailable"){
		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 = 13; // 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