//it formats the date!
function formatDate(theDay,theMonth,theYear,theFormat,theSpacer){
  var theNumber = new Array(theDay,theMonth,theYear)
  theFormat = theFormat.split(theSpacer)
  for(i=0;i<=2;i++){
    if(theFormat[i].length == 2 && theNumber[i].length == 1){
      theNumber[i] = '0' + theNumber[i]
    } else if(theFormat[i].length == 4){
      if(theNumber[i].length == 1){
        theNumber[i] = '200' + theNumber[i]
      } else if(theNumber[i].length == 2){
        theNumber[i] = '20' + theNumber[i]
      } else if(theNumber[i].length == 3){
        theNumber[i] = '2' + theNumber[i]
      }  
    }
  }
  return theNumber[0] + theSpacer + theNumber[1] + theSpacer + theNumber[2]
}
//for all A to Z pages
function doAToZSearch(urlToGoTo){
  top.location.href =urlToGoTo
}
function aToZOver(theId,urlToGoTo){
  eval('document.all.'+theId+'.style.borderColor = "#ff6600";');
  window.status = urlToGoTo
}
function aToZOut(theId){
  eval('document.all.'+theId+'.style.borderColor = "#003399";');
}
function writeAtoZTable(startUrl,endUrl){
  document.write('<table border="0" cellspacing="5" cellpadding="0">')
  document.write('<tr>')
  urlToGoTo = startUrl+'~NUMERIC~'+endUrl  
  document.write('<td align="center" id="atoznumeric" class="tableatoz" onclick="doAToZSearch(\''+urlToGoTo+'\')" onmouseover="aToZOver(\'atoznumeric\',\''+urlToGoTo+'\')" onmouseout="aToZOut(\'atoznumeric\')" colspan="2">')
  document.write('<span class="linkatoz">0-9</span>')
  document.write('</td>')
  for(i=65;i<=90;i++){
    urlToGoTo = startUrl+'&#0'+i+';'+endUrl
    document.write('<td align="center" class="tableatoz" id="atoz'+i+'" onclick="doAToZSearch(\''+urlToGoTo+'\')" onmouseover="aToZOver(\'atoz'+i+'\',\''+urlToGoTo+'\')" onmouseout="aToZOut(\'atoz'+i+'\')">')
    document.write('<span class="linkatoz">&#0'+i+';</span>')
    document.write('</td>')
    if(i==76){
      document.write('</tr>')
      document.write('<tr>')
    }
  }
  document.write('</tr>')
  document.write('</table>')
}
//

//for going to another location, 2 params, firstly a url and secondly a param which needs escaping
function getUrlWithEncodedParam(urlToGoTo,escapedParam){
  document.location.href = (urlToGoTo+escape(escapedParam));
}

//finds the value of the parameter passed to it or returns false if not there.
function getParam(paramName){
  var allParamsStr = '' 
  var paramValue = false  
  var start  = 0 
  var end = 0 
  allParamsStr = window.location.search 
  paramName = paramName.toLowerCase() 
  allParamsStr = allParamsStr.toLowerCase() 
  start = allParamsStr.indexOf(paramName + '=') 
  if(start!=1) { 
    start = allParamsStr.indexOf('&' + paramName + '=') 
    if(start==-1){
      return(paramValue)
    } 
    start += paramName.length + 2 
  } else { 
    start = paramName.length + 2 
  } 
  end = allParamsStr.indexOf('&', start + 1) 
  if(end==-1){end=allParamsStr.length} 
  paramValue = window.location.search.substring(start, end) 
  paramValue = unescape(paramValue) 
  return(paramValue)
}
//converts to one decimal places
function oneDP(theValue){
  if(isNaN(theValue)){
    return '-'
  } else {
    theValue = Math.round(theValue*10)/10
    theValue = theValue.toString()
    if(theValue.indexOf('.') == -1){
      theValue = theValue + '.0'
    }
    return theValue
  }
}
//puts thousand commas , in.
function thousandCommas(theNumber) {
  var fullNumber=String(theNumber)
  dpStr = false
  if(fullNumber.indexOf('.') != -1){
    dpStr = '.' + fullNumber.split('.')[1]
    fullNumber = fullNumber.split('.')[0]
  }
  var numLength=fullNumber.length-1
  var withCommas=''
  for (i=0; i<=numLength;i++) {
    withCommas+=fullNumber.charAt(i)
    if (((numLength-i)%3==0) && (i<numLength)){
      withCommas+=','
    }
  }
  if(dpStr){withCommas += dpStr}
  return withCommas
}

function abbNum(num) {
  scaleStr = ''
  if(isNaN(num)) {
    return '-'
  } else {
    if (Math.abs(num) >= 1000) {
      num = (num / 1000)
      scaleStr = 'k'
      if (Math.abs(num) >= 1000) {
        num = (num / 1000)
        scaleStr = 'm'
        if (Math.abs(num) >= 1000) {
          num = (num / 1000)
          scaleStr = 'bn'
        }
      }
    }
  }
  abbreviatedNum = twoDP(num);
  abbreviatedNum = abbreviatedNum+""+scaleStr;
  return abbreviatedNum;
}

//cross browser get and set properties functions.
function setDivProperty(divId,properyName,valueSetTo){//IE5/6 IE4  NS6/7 NS4
  if(document.getElementById){
    eval('document.getElementById(divId).style.' + properyName + '= "' + valueSetTo + '"')
  } else if(document.all){
    eval('document.all(divId).style.' + properyName + '= "' + valueSetTo + '"')
  } else if (document.layers){
    properyName = properyNameNS(properyName)//for differing names
    valueSetTo = valueSetToNS(valueSetTo)//for differing names
    eval('document.' + divId + '.' + properyName + '= "' + valueSetTo + '"')
  }
}
function getDivProperty(divId,properyName){//IE5/6 IE4  NS6/7 NS4
  if(document.getElementById){
    return eval('document.getElementById(divId).style.' + properyName)
  } else if(document.all){
    return eval('document.all(divId).style.' + properyName)
  } else if (document.layers){
    properyName = properyNameNS(properyName)//for differing names
    valueSetTo = valueSetToNS(valueSetTo)//for differing names
    return eval('document.' + divId + '.' + properyName)
  }
}
//required for the above for ns4.  additions will be needed as required.
function properyNameNS(properyName){
  if(properyName == 'height'){return 'clip.height'}
  else if(properyName == 'width'){return 'clip.width'}
  else {return properyName}
}
function valueSetToNS(valueSetTo){
  if(valueSetTo == 'visible'){return 'show'}
  else if(valueSetTo == 'hidden'){return 'hide'}
  else {return valueSetTo}
}
//gets the X and Y positions of the mouse relative to the div.  NS with position relative gives the position to the document.  i may rewrite these when i need them.
function obtainOffsetX(e){//IE5/6 IE4 NS6/7 NS4
  if(e.offsetX){
    return e.offsetX
  } else if(e.layerX){
    return e.layerX
  }
}
function obtainOffsetY(e){//IE5/6 IE4 NS6/7 NS4
  if(e.offsetY){
    return e.offsetY
  } else if(e.layerY){
    return e.layerY
  }
}
documentOffsetFix = 0
//Only required for IE.
if(document.all && window.navigator.userAgent.indexOf( 'Opera' ) == -1){
  documentOffsetFix = 2
}
//mouse co-ords to document.  simple.  [x,y]
function mouseCoordsToDocument(e){//works in everything.
  if( !e ) { e = window.event; }
  if( !e || ( typeof( e.pageX ) != 'number' && typeof( e.clientX ) != 'number' ) ){ var posX = 0; var posY = 0; }
  if( typeof( e.pageX ) == 'number' ){
    var posX = e.pageX; var posY = e.pageY;
  } else {
    var posX = e.clientX; var posY = e.clientY;
    if( !( ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) || ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) || window.navigator.vendor == 'KDE' ) ){
       if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) ){
         posX += document.documentElement.scrollLeft; posY += document.documentElement.scrollTop;
       } else if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) ){
         posX += document.body.scrollLeft; posY += document.body.scrollTop;
       }
    }
  }
  return [posX - documentOffsetFix,posY - documentOffsetFix];//I added documentOffsetFix to this standardfunction as IE is 2 out.
}
function truncateNames(compName){
  if(compName.indexOf(" PLC")!=-1){compName = compName.split(" PLC")[0]}
  if(compName.indexOf(" Group")!=-1){compName = compName.split(" Group")[0]}
  if(compName.indexOf(" (The)")!=-1){compName = compName.split(" (The)")[0]}
  document.write(compName)
}
function roundTo(value,precision){
  strValue = "" + value
  precision = parseInt(precision);
  whole = "" + Math.round(strValue * Math.pow(10, precision));
  decPoint = whole.length - precision;
  result = whole.substring(0, decPoint);
  if (result >= 1 || result <= -1){
    result += ".";
  } else {
    result += "0.";
  }
  result += whole.substring(decPoint, whole.length);
  return result;
}
