
function formatCurrency(s) {
	s = formatNumber(s);
	if (s.length>0) {
		s = "£" + s;
	}
	return s;
}

function formatNumber(s) {
	var a = "";
	s = numbersOnly(s);
	if (s.charAt(0) == "-") {
		a = "-";
		s = s.substring(1,s.length);
	}
	var t = s;
	s = "";	 
	dotIndex = t.indexOf(".");
	if (dotIndex > -1) {
		s = t.substring(dotIndex,t.length);
	} else {
		dotIndex = t.length;
	}
	//dotIndex  = 0;
	for(i=1;i<=dotIndex;i++) {
		s = t.charAt(dotIndex-i) + s;
		if ((i%3==0) && (i!=dotIndex)) {
			s =","+s;
		}
	}
	return a+s;
}

function numbersOnly(s) {
	s = s.replace(/[^0-9.\-]/g,"");
	//s = s.replace(/^(.*?\..*?)\..*/g,"$1");

	if (s != "0") {
		s = s.replace(/^0+/,"");
		s = s.replace(/^\-+/,"-");
		s = s.replace(/^(.+)[^0-9,](.+)/,"$1$2");		
	}	
	return s;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function openFoodFacts() { 
	window.open("http://www.rfbrookes.co.uk/Web/FoodFacts/FoodFacts.nsf/StartPage?OpenForm","start","toolbar=no,location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=600, height=350, screenX=0, screenY=0");
}



function MM_setTextOfLayer(objName,x,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function BW_centerLayers() { //v4.1.1
  if (document.layers || document.all || document.getElementById){
    var winWidth, winHeight, i, horz, vert, width, height, offsetX, offsetY, negX, negY, group, x, y, args;
    args = BW_centerLayers.arguments;

    onresize = BW_reload;


    winWidth = (document.all)?document.body.clientWidth:window.innerWidth;
    winHeight = (document.all)?document.body.clientHeight:window.innerHeight;

    for (i=0; i<(args.length-9); i+=10) {
      horz    = args[i+1];
      vert    = args[i+2];
      width   = parseInt(args[i+3]);
      height  = parseInt(args[i+4]);
      offsetX = parseInt(args[i+5]);
      offsetY = parseInt(args[i+6]);
      negX    = args[i+7];
      negY    = args[i+8];

      x = ((winWidth - width)/2) + offsetX;
      y = ((winHeight - height)/2) + offsetY;

      x = (negX=='false' && (x < 0))?0:x;
      y = (negY=='false' && (y < 0))?0:y;

      layerObj = (document.getElementById)?document.getElementById(args[i]):MM_findObj(args[i]);

      if (layerObj!=null) {
        layerObj = (layerObj.style)?layerObj.style:layerObj;
        layerObj.left = (horz=="true")?x:layerObj.left;
        layerObj.top = (vert=="true")?y:layerObj.top;
      }
    }
  }
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function BW_reload() {location.reload();}

var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;

function removeListValue(value,selectedObj) {
	var tmpArray = selectedObj.value.split(",");
	var newArray = Array();
	for (i=0;i<tmpArray.length;i++) {
		if (value != tmpArray[i]) { 
			newArray.push(tmpArray[i]);
		}
	}
	selectedObj.value = newArray.toString();
}

function addListValue(value,selectedObj) {
	var tmpArray = new Array();
	if (selectedObj.value != "") {
		tmpArray = selectedObj.value.split(",");
	}
	var found = false;
	for (i=0;i<tmpArray.length;i++) {
		if (value == tmpArray[i]) { 
			found = true;
		}
	}
	if (!found) {
		tmpArray.push(value);
		selectedObj.value = tmpArray.toString();
	}
}	

function toggleSelect(obj,id,selectedObj) {
	if (obj.selected) {
		obj.selected = false;
		obj.className="";
		removeListValue(id,selectedObj);
	}	else {
		obj.selected = true;
		obj.className="SelectedRow";
		addListValue(id,selectedObj);
	}
}

function toggleCheckBox(checkBoxObj,e) {
	var elem = (e.target) ? e.target : e.srcElement;
	if (elem.id == checkBoxObj.id) {
			return;
	}
	if (checkBoxObj.checked) {
		checkBoxObj.checked = false;
	}	else {
		checkBoxObj.checked = true;
	}
}

function toggleRow(row) {
	if (row.className == "SelectedRow") {
		row.className = row.oldClassName;
	} else {
		row.oldClassName = row.className;
		row.className = "SelectedRow";
	}
}

function doCustomTag(obj,startTag,stopTag) {
  if (obj) {
    obj.focus();
    if (ie4) {
      tr = document.selection.createRange();
      tr.text = startTag+tr.text+stopTag;
      tr.select();
    } else if (ns6) {
      var selectionStart = obj.selectionStart;
      var selectionEnd = obj.selectionEnd;
      selectedText = obj.value.substring(selectionStart,selectionEnd);
      obj.value = obj.value.substring(0, selectionStart) + startTag + selectedText +stopTag + obj.value.substring(selectionEnd);
      obj.setSelectionRange(selectionStart+startTag.length,selectionStart+startTag.length+selectedText.length);
    } else if (ns4) {
      obj.value += startTag+'<Type Here>'+stopTag;
      obj.setSelectionRange(obj.value.length-stopTag.length-11,obj.value.length-stopTag.length);
    }
    obj.focus();
  }
}
function previewText(textControl,divid) {
	s = textControl.value.replace(/\n/g,"<br />");
	MM_setTextOfLayer(divid,0,s);
}
function getSelectedText(obj) {
 if (obj) {
    if (ie4) {
      tr = document.selection.createRange();
      return tr.text;
    } else if (ns6) {
      var selectionStart = obj.selectionStart;
      var selectionEnd = obj.selectionEnd;
      return obj.value.substring(selectionStart,selectionEnd);
    }
  }
}
function overwriteWithText(obj,text) {
  if (obj) {
    if (ie4) {
      tr = document.selection.createRange();
      tr.text = text;
      tr.select();
    } else if (ns6) {
      var selectionStart = obj.selectionStart;
      var selectionEnd = obj.selectionEnd;
      selectedText = obj.value.substring(selectionStart,selectionEnd);
      obj.value = obj.value.substring(0, selectionStart) + text + obj.value.substring(selectionEnd);
      obj.setSelectionRange(selectionStart,selectionStart+text.length);
    } else if (ns4) {
      obj.value += startTag+'<Type Here>'+stopTag;
      obj.setSelectionRange(obj.value.length-stopTag.length-11,obj.value.length-stopTag.length);
    }
  }
  obj.focus();
}

function toLower(obj) {
  obj.value = obj.value.toLowerCase();
}

function toUpper(obj) {
  obj.value = obj.value.toUpperCase();
}

function showImageFile(img,fileObj,width) {
	var src = fileObj.value.replace(/\\\\/,"@");
	src = src.replace(/\\/g,"/");	
	src = src.replace(/\@/g,"///");		
	img.src = "file://"+src;
  img.width = width;
}
function showImageURL(img,src,width) {
  img.src = src;
  img.width = width;
}

function checkBoxes(control, name,state) {
  form = control.form;
  count = eval("form." + name + ".length");
  code = "form." + name + "[i].checked = state";
  for (i=0;i<count;i++)
  eval(code);
}

function jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function preloadImage() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImage.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function maxTextLength(obj,i) {
	if (obj.value.length > i) {
		obj.value=obj.value.substr(0,i);
		return false;
	}
	return true;
}

function disable(obj) {
		obj.disabled=true;		
}
function enable(obj) {
		obj.disabled=false;
}

function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;
	
	for( i = 0; i < cardNumber.length; ++i ) {
		ar[i] = parseInt(cardNumber.charAt(i));
	}
 // you have to start from the right, and work back.
 for( i = ar.length -2; i >= 0; i-=2 ) {
	 // every second digit starting with the right most (check digit)
	 // will be doubled, and summed with the skipped digits.
	 // if the double digit is > 9, ADD those individual digits together 
		ar[i] *= 2;							 
		if( ar[i] > 9 ) ar[i]-=9;
	}										 

	for( i = 0; i < ar.length; ++i ) {
		// if the sum is divisible by 10 mod10 succeeds		
		sum += ar[i];						 
	}
	return (((sum%10)==0)?true:false);	 	
}

var newwin;

function launchwin(winurl,winname,winfeatures)
{
  //This launches a new window and then
  //focuses it if window.focus() is supported.
  newwin = window.open(winurl,winname,winfeatures);
  if(javascript_version > 1.0)
  {
    //delay a bit here because IE4 encounters errors
    //when trying to focus a recently opened window
    setTimeout('newwin.focus();',250);
  }
}
function showHideLinkDialog(obj,insertTo,textObj) {
  obj = MM_findObj(obj);
  if (obj.style.visibility=="visible") {
    obj.style.visibility = "hidden";
  } else {
    obj.style.visibility = "visible";
    insertTo = MM_findObj(insertTo);
    insertTo.value = getSelectedText(textObj);
  }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

function ClockCal(controlName,defaultValue,onChange,startYear,stopYear) {


	this.pad = function(value,length) {
		zeros = "";
		i = 0;
		valueString = ""+value;
		while(i++<length-valueString.length) {
			zeros +="0";
		}
		return zeros+""+value;
	}

	this.setMonth = function(month) {
		month = parseFloat(month)-1;
		if (month < 0) {
			this.setYear(this.year-1);
			this.month=11;
			return
		}
		if (month > 11) {
			this.setYear(this.year+1);
			this.month=0;
			return
		}
		this.month=month;
	}

	this.setYear = function(year) {
			this.year = parseFloat(year);
	}

	this.getYear = function() {
		return this.year;
	}

	this.setDate = function(defaultValue) {
		if (defaultValue) {
			defaultValue = defaultValue.replace(/-/g,"");
			this.year = parseFloat(defaultValue.substring(0,4));
			this.setMonth(parseFloat(defaultValue.substring(4,6)));
			this.day = parseFloat(defaultValue.substring(6,9));
		}
	}

	this.getDate = function (defaultValue) {
		return this.pad(this.getYear(),4)+"-"+this.pad(this.getMonth(),2)+"-"+this.pad(this.getDay(),2)
	}

	this.getStartDayOffset = function() {
		var tmpdate = new Date(this.year,this.month,1);
		i = tmpdate.getDay();
		if (i == 0) {
			return -5;
		}
		return -i+2;
	}

	this.getDaysInMonth = function() {
		var tmpdate = new Date(this.year,this.month,32);
		return 32-tmpdate.getDate();
	}

	this.getDaysInLastMonth = function() {
		var tmpdate = new Date(this.year,this.month,0);
		return tmpdate.getDate();
	}

	this.displayDay = function(day,daysInMonth,daysInLastMonth) {
		if (day < 1) {
			return "<span class=\"CalDulled\">"+(daysInLastMonth+day)+"</span>";
		} else if (day > daysInMonth ) {
			return "<span class=\"CalDulled\">"+(-daysInMonth+day)+"</span>";
		}
		return day;
	}

	this.setDay = function(day) {
		day = parseInt(day);
		if (day < 1) {
			i = this.getDaysInLastMonth();
			this.setMonth(this.month);
			this.day = i+day;
			return;
		}
		i = this.getDaysInMonth();
		if (day > i) {
			this.setMonth(this.month+2);
			this.day = day-i;
			return;
		}
		this.day = day;
	}

	this.getMonth = function () {
		return parseInt(this.month)+1;
	}

	this.getDay = function () {
		return parseInt(this.day);
	}

	this.setAfterControl = function(afterControlName) {
		if (this.getDate() > afterControlName.getDate()) {
			afterControlName.setDate(this.getDate());
			afterControlName.update()
		}
	}

	this.makeMonthList = function () {
		var returnValue = "";
		returnValue = "<select id=\""+this.controlName+"Month\" onChange=\""+this.controlName+"Inst.setMonth(parseInt(this.options[this.selectedIndex].value)+1);"+this.controlName+"Inst.update('"+this.controlName+"');\">\n";
		for (i=0;i<12;i++) {
			returnValue += "<option value=\""+i+"\" "+(this.month==i?"selected":"")+">"+this.monthArray[i]+"</option>\n";
		}
		returnValue += "</select>\n";
		return returnValue;
	}
	this.makeYearList = function () {
		var returnValue = "";
		returnValue = "<select id=\""+this.controlName+"Year\" onChange=\""+this.controlName+"Inst.setYear(this.options[this.selectedIndex].value);"+this.controlName+"Inst.update('"+this.controlName+"');\">\n";
		for (i=this.startYear;i<=this.stopYear;i++) {
			returnValue += "<option value=\""+i+"\" "+(this.year==i?"selected":"")+">"+i+"</option>\n";
		}
		returnValue += "</select>\n";
		return returnValue;
	}
	this.setDisable = function(value) {
		this.disabled = value;
		this.update(true);
		this.monthControl.disabled=value;
		this.yearControl.disabled=value;
	}
	this.update = function(creating) {
		var daysInMonth = this.getDaysInMonth();
		var daysInLastMonth = this.getDaysInLastMonth();
		var dayCounter = this.getStartDayOffset();
		var calTable;
		calTable = "<input name=\""+this.controlName+"\" id=\""+this.controlName+"\" type=\"hidden\" value=\""+this.getDate()+"\">\n";
		calTable += "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td><table width=\"100%\"cellspacing=\"0\" cellpadding=\"0\" class=\"CalMonthYear\"><tr><td align=\"left\">"+this.makeMonthList()+"</td><td align=\"right\">"+this.makeYearList()+"</td></tr><tr></table></td></tr><td>\n";
		calTable += "<table cellspacing=\"0\" cellpadding=\"0\" class=\"CalTable\">\n";
		calTable += "<tr><th align=\"center\">Mo</th><th align=\"center\">Tu</th><th align=\"center\" >We</th><th align=\"center\" >Th</th><th align=\"center\" >Fr</th><th align=\"center\" >Sa</th><th align=\"center\" >Su</th></tr>";
		if (this.disabled) {
			for (e=0;e<6;e++){
				calTable += "<tr>\n";
				for (i=0;i<7;i++){
						calTable += "<td align=\"center\" "+(i>4?"class=\"CalWeekendDisabled\"":"class=\"CalDisabled\"")+">"+this.displayDay(dayCounter++,daysInMonth,daysInLastMonth)+"</td>\n";
				}
				calTable += "</tr>\n";
			}
		} else {
			for (e=0;e<6;e++){
				calTable += "<tr>\n";
				for (i=0;i<7;i++){
					calTable += "<td onClick=\""+this.controlName+"Inst.setDay("+dayCounter+");"+this.controlName+"Inst.update();\" onMouseOver=\"this.oldClassName=this.className;this.className='CalRollOver';\" onMouseOut=\"this.className=this.oldClassName;\" align=\"center\" "+(dayCounter==this.day?"class=\"CalSelectedDay\"":(i>4?"class=\"CalWeekend\"":""))+">"+this.displayDay(dayCounter++,daysInMonth,daysInLastMonth)+"</td>\n";
				}
				calTable += "</tr>\n";
			}
		}

		calTable += "</td></tr></table>";
		MM_setTextOfLayer(this.controlName+"Layer",'',calTable);
		this.monthControl = MM_findObj(this.controlName+"Month");
		this.yearControl = MM_findObj(this.controlName+"Year");		
		if (!creating) {
			
			eval(onChange);
			return;
		}
	}

	this.controlName = controlName;
	eval(this.controlName +"Inst = this");
	document.write("<div id=\""+this.controlName+"Layer\"></div>");
	this.date = new Date();
	this.monthArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	this.day=this.date.getDate();
	this.month=this.date.getMonth();
	this.year=this.date.getFullYear();
	this.setDate(defaultValue);
	this.startYear = startYear?startYear:1995;
	this.stopYear = stopYear?stopYear:2005;	
	this.onChange = onChange;
	this.disabled = false;
	this.update(true);
}
