
<!--

/* reload the page */
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);

/* search the dom for the job named */
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);
  }
  // alert('object ' + n + ' is ' + x);
  return x;
}

/* find then set a layers visibility */
function MM_showHideLayers() { //v6.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;
	    }
  }
}

// display popup info div at the position of the mouse pointer
function showResultDiv(div_id, state, e) {
    // find the div to move
    element = MM_findObj(div_id);
    if (document.all) {
        // the window.event is a mouse move
        element.style.left = window.event.x - 200;
        element.style.top = (document.body.scrollTop + window.event.y) - 250;
    }
    else {
        // e = mouse move event
        element.style.left = e.pageX - 200;
        element.style.top = e.pageY  - 200;
    }

    if (state == "show") {
      // now that the div has been moved into position show it.
      element.style.visibility = "visible";
    }else{
        element.style.visibility = "hidden";
    }
}

// display popup info div at the position of the mouse pointer
function positionDiv(div_id, state, e) {
    // find the div to move
    element = MM_findObj(div_id);
    if (document.all) {
        // the window.event is a mouse move
        element.style.left = window.event.x - 200;
        element.style.top = window.event.y;
    }
    else {
        // e = mouse move event
        element.style.left = e.pageX - 200;
        element.style.top = e.pageY;
    }


    if (state == "show") {
      // now that the div has been moved into position show it.
      element.style.visibility = "visible";
    }else{
        element.style.visibility = "hidden";
    }
}

// display popup info div at the position of the mouse pointer
function showLogoDiv(div_id, state, e) {
    // find the div to move
    element = MM_findObj(div_id);
    if (document.all) {
        // the window.event is a mouse move
        element.style.left = window.event.x + 50;
        element.style.top = window.event.y;
    }
    else {
        // e = mouse move event
        element.style.left = e.pageX + 50;
        element.style.top = e.pageY;
    }


    if (state == "show") {
      // now that the div has been moved into position show it.
      element.style.visibility = "visible";
    }else{
        element.style.visibility = "hidden";
    }
}

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 XM_images(n,o,d,l) { //v1.0
	this.obj=MM_findObj(n);this.n=n;this.u=this.obj.src;this.s=1;this.o=o;this.d=d;
	this.swap=function() {if(this.s==1){this.obj.src=this.o;this.s=2}}
	this.stick=function() {this.obj.src=this.d;this.s=3;XM_unStick(this.n)}
	this.restore=function(){if(this.s==2){this.obj.src=this.u;this.s=1}}
	if (document.location.href.indexOf(l)!=-1) this.stick()
}
function XM_unStick(n){ //v1.0
	for (var i=0;i<document.XM_img.length;i++)if (document.XM_img[i].n!=n)
	{document.XM_img[i].s=2;document.XM_img[i].restore()}
}
function XM_initImages() { //v1.0
	var a=XM_initImages.arguments;document.XM_img=new Array();for(i=0;i<a.length;i=i+4){
	document.XM_img[document.XM_img.length]=new XM_images(a[i],a[i+1],a[i+2],a[i+3])}
}

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_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];}
}
/**
 * Show/Hide an object
 */
function displayObject(name) {
  if ((obj = MM_findObj(name)) != null) {

    // toggle display of the object
    obj.style.display = (obj.style.display == "none") ? "" : "none";
  }

}

/* takes the highlighting of a field */
function blurField(field) {
	  // field.style.backgroundColor='cccccc';
	  field.className = 'field';
}

/* puts highlighting focus on a field */
function focusField(field) {
	  field.className = 'fieldHighlighted';
	  // field.style.backgroundColor = 'eeeeee';
	  field.focus();
}

function checkIfEnter(e,area){

	if (document.all) {
		if(window.event.keyCode == 13){
		  if(area=="login"){
			submitLoginForm();
			return true;
		  }else if(area =="search"){
			submitSearchForm();
			return true;
		  }else if(area =="quick_submit"){
			submitForm();
			return true;
		  }
		}
		return false;
	} else {
		if(e.keyCode == 13){
		  if(area=="login"){
			submitLoginForm();
			return true;
		  }else if(area =="search"){
			submitSearchForm();
			return true;
		  }else if(area =="quick_submit"){
			submitForm();
			return true;
		  }
		}
		return false;
	}
	return false;
}


function setSelectedRadio(buttonGroup, selectedButton) {

  // if the button group is an array (one button is not an array)
  // and has enough selectedButtons
	if (buttonGroup[selectedButton]) {

	  // set the selected button
	  buttonGroup[selectedButton].checked = true;

	  buttonGroup[selectedButton].focus();
	}
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function


function getNumOfDays(startDate,endDate){

    var dtCh= "-";
	var pos1=startDate.indexOf(dtCh);
	var pos2=startDate.indexOf(dtCh,pos1+1);
	var strStartDay=startDate.substring(0,pos1);
	var strStartMonth=startDate.substring(pos1+1,pos2);
	var strStartYear=startDate.substring(pos2+1);

	StartMonth= strStartMonth - 0 ;
	StartDay= strStartDay - 0;
	StartYear= strStartYear - 0;

	var endPos1=endDate.indexOf(dtCh);
	var endPos2=endDate.indexOf(dtCh,endPos1+1);
	var strEndDay=endDate.substring(0,endPos1);
	var strEndMonth=endDate.substring(endPos1+1,endPos2);
	var strEndYear=endDate.substring(endPos2+1);

	EndDay = strEndDay - 0;
	EndMonth= strEndMonth - 0;
	EndYear= strEndYear - 0;

    date2 = new Date(StartYear,StartMonth,StartDay);
    date1 = new Date(EndYear,EndMonth,EndDay);

    var difference =
        Date.UTC(EndYear,EndMonth,EndDay,0,0,0)
      - Date.UTC(StartYear,StartMonth,StartDay,0,0,0);

    return difference/1000/60/60/24;
}

function todayPlusDays(intDays){
    // get todays date
    date = new Date();

    strDate = date.toGMTString();

    // parse it
    strDate = Date.parse(strDate);

    strDate = parseInt(strDate, 10);
    // add
    strDate = strDate + intDays*(24*60*60*1000);
    newDate = new Date(strDate);

    // get the day month and years
    day      = newDate.getDate();
    month    = newDate.getMonth();
    year     = newDate.getYear();

    // add a month on
    month = month+1;

    // prepend 0 onto the day
    day = day < 9 ? '0' + day : day;

    // prepend the month
    month = month < 9 ? '0' + month : month;

    // prepend the year
    year = year < 1900 ? 1900 + year : year;

    new_str_date = day + '-' + month + '-' + year;

    return new_str_date;
}

function dateAddNumOfDays(dateValue,resultObject,numOfDays){
    var dtCh= "-";
	var pos1=dateValue.indexOf(dtCh);
	var pos2=dateValue.indexOf(dtCh,pos1+1);
	var strDay=dateValue.substring(0,pos1);
	var strMonth=dateValue.substring(pos1+1,pos2);
	var strYear=dateValue.substring(pos2+1);

	m = strMonth - 0 ;
	d = strDay - 0;
	y = strYear - 0;

	// get the milseconds of this date plus the number of days
    var result = (Date.UTC(strYear,(strMonth-0)-1,strDay,0,0,0)) + ((24*60*60*1000)*(numOfDays-0));
	newDate = new Date(result-0);

   // get the day month and years
    day      = newDate.getDate();
    month    = ((newDate.getMonth())-0)+1;
    year     = newDate.getYear();

  // prepend 0 onto the day
    day = day < 9 ? '0' + day : day;

    // prepend the month
    month = month < 9 ? '0' + month : month;

    // prepend the year
    year = year < 1900 ? 1900 + year : year;

    if( month == '00'){
	  month = '12';
	  year = (year-0) - 1;
	}

	if( month == '02'){
	  if(day-0 > 28){
		if((year-0)%4 == 0){
		  day = '29';
		}else{
		  day = '28';
		}
	  }
	}

    new_str_date = day + '-' + month + '-' + year;

	resultObject.value = new_str_date;
}


function dateMinusYearAndADay(dateValue,resultObject){

    var dtCh= "-";
	var pos1=dateValue.indexOf(dtCh);
	var pos2=dateValue.indexOf(dtCh,pos1+1);
	var strDay=dateValue.substring(0,pos1);
	var strMonth=dateValue.substring(pos1+1,pos2);
	var strYear=dateValue.substring(pos2+1);

	m = strMonth - 0 ;
	d = strDay - 0;
	y = strYear - 0;

    // check for a leap year
	if(m <= 2){
	  var tmpY = y % 4 ;
	}else{
	  var tmpY = ((y-0)+1) % 4 ;
	}
	if(tmpY == 0){
	  var numofdays = 365 ;
	}else{
	  var numofdays = 364 ;
	}

	// get the milseconds of this date plus 1 year and minus a day from it
    var result = (Date.UTC(strYear,(strMonth-0)-1,strDay,0,0,0)) + ((24*60*60*1000)*(numofdays-0));
	newDate = new Date(result-0);

   // get the day month and years
    day      = newDate.getDate();
    month    = ((newDate.getMonth())-0)+1;
    year     = newDate.getYear();

  // prepend 0 onto the day
    day = day < 9 ? '0' + day : day;

    // prepend the month
    month = month < 9 ? '0' + month : month;

    // prepend the year
    year = year < 1900 ? 1900 + year : year;

    if( month == '00'){
	  month = '12';
	  year = (year-0) - 1;
	}

	if( month == '02'){
	  if(day-0 > 28){
		if((year-0)%4 == 0){
		  day = '29';
		}else{
		  day = '28';
		}
	  }
	}

    new_str_date = day + '-' + month + '-' + year;

	resultObject.value = new_str_date;
}


/**
 * Set the field on the page
 */
function setField(field, value) {
		try {
				if ((obj = MM_findObj(field)) != null) {
						obj.innerHTML = value;
				}
		} catch (err) {
				alert('failed');
		}
}

function submitForm() {
	if (validateForm()) {
		document.form.submit();
		return true; // never get to here
	}

	return false;
}

function changeSearch(val){
	switch(val) {
			case 'reference_id':
		        document.search.fa.value = 's_i';
				break;
			case 'constituents':
		        document.search.fa.value = 's_constituents';
				break;
			case 'companies':
				document.search.fa.value = 's_companies';
				break;
			case 'licence':
				document.search.fa.value = 'l_s';
				document.search.search_type.value = 'licence';
				break;
			case 'fee':
				document.search.fa.value = 'f_s';
				document.search.search_type.value = 'fee';
				break;

      }
}

function submitSearchForm() {

	if (!document.search.q.value) {
			alert('Please enter something to search for.\n');
			return false;
	}

    // get the search type
    changeSearch(document.search.type.options[document.search.type.options.selectedIndex].value);

    // submit the form
    document.search.submit();
}

function formatPostcode(field){
  // check that val is greater than or equal to 5
  var val = field.value;

  if(val.length < 5) {

	// if not return false
	return false;

  } else {

	var res = '';
	var j = 0;

	// for each char in val
	for (var i=0; i<val.length; i++) {

	   // if the 5th char (i starts at zero)
	   if (i == 4 && val[i] != ' ') {

		// add a space
		  res = res + ' ';

		// increment the counter
		  j++;
	   }

	   // add the char to result
	   res = res + val[i];
	}

	// return the parsed result
	field.value = res;
	return true;
  }

  // return false something wrong with val
  return false;

}

function showRow(name) {
  var obj;
  if ((obj = MM_findObj(name)) != null) {
    obj.style.display = "";
  }
}

function hideRow(name) {
  var obj;
  if ((obj = MM_findObj(name)) != null) {
    obj.style.display = "none";
  }
}

function disableField(name) {
	field.disabled = 'disabled';
	field.className = 'fieldDisabled';
}

function enableField(field) {
	field.disabled = '';
	field.className = field;
}

function setLabel(name, value) {
	var obj;
	if ((obj = MM_findObj(name)) != null) {
		obj.innerHTML = value;
	}else{
	  alert('object not found');
	}
}

function getLabel(name) {
	var obj;
	if ((obj = MM_findObj(name)) != null) {
		return obj.innerHTML;
	}
	return false;
}
/**
 * Switch the expand button to down/up
 */
function toggleExpand(field,root) {

	obj = MM_findObj(field + "_expand");

    if (obj != null) {
		if (obj.src == root+'/images/tbc_buttons/expand_lbg.jpg') {
            obj.src = root+'/images/tbc_buttons/contract_lbg.jpg';
        } else {
            obj.src = root+'/images/tbc_buttons/expand_lbg.jpg';
        }
   }
}


function expandLayer(id) {
  element = MM_findObj(id);
  if (element != null) {
	  if (element.style.display == 'none') {
		  element.style.display = '';
	  } else {
		  element.style.display = 'none';
	  }
  }
}
/**
* check the date format
*/
function checkDateFormat(object,value){
	var selLength = value.length;

	if(selLength == 6){
	  var day = value.substring(0,2);
	  var month = value.substring(2,4);
	  var year = value.substring(4,6);
	  year = '20' + year;
	  object.value = day + '-' + month + '-' + year;
	}else if (selLength == 8 || selLength == 10){
	    var pos1 = 0;
		var pos2 = 0;
		pos1 = value.indexOf('/');
		if(pos1 > 0){
		  pos2 = value.indexOf('/',pos1+1);
		}else{
		  pos1 = value.indexOf('.');
		  if(pos1 > 0){
			pos2 = value.indexOf('.',pos1+1);
		  }
		}

		if(pos1 == 2 && pos2 == 5){
		  var day = value.substring(0,2);
		  var month = value.substring(3,5);
		  if(selLength == 8){
			var year = value.substring(6,8);
			year = '20' + year;
		  }else{
			var year = value.substring(6,10);
		  }
		  object.value = day + '-' + month + '-' + year;
		}else if (selLength == 8){
		  var day = value.substring(0,2);
		  var month = value.substring(2,4);
		  var year = value.substring(4,8);
		  //year = '20' + year;
		  object.value = day + '-' + month + '-' + year;
		}
	}else if (selLength == 1 || selLength == 2){
	  if(selLength == 1){
		var day = '0' + value;
	  }else{
		var day = value;
	  }

	  var date = new Date();
//	  var strDate = date.toGMTString();

      // get the day month and years
      var month    = date.getMonth()+1;
	  if(month-0 < 10){
		month = '0' + month;
	  }
      var year     = date.getYear();

	  object.value = day + '-' + month + '-' + year;
	}
}
//-->
