// JavaScript Document

function myOpen(url,name) { //opens new recipe window
  window.open(url, name, 'toolbar=yes,location=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes,width=570,height=400')
}

function closeWindow() {
			window.opener=self;
			window.close();
}

function myOpen2(url,name) { //opens new window
  window.open(url, name, 'toolbar=yes,location=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes,width=570,height=500')
}

function myOpen3(url,name) { //opens sb download window
  window.open(url, name, 'toolbar=yes,location=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes,width=460,height=645')
}

function myOpen4(url,name) { //opens readership survey window
  window.open(url, name, 'toolbar=yes,location=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes,width=665,height=650')
}

function myOpen5(url,name) { //opens samperi design window
  window.open(url, name, 'toolbar=yes,location=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes,width=765,height=550')
}


function closeWindow() {
			window.opener=self;
			window.close();
}


function bmi_calculate() {
  formObj = document.forms['bmi_calculator'];
  /*
  if ((formObj.elements['bmi_height'] == null) || (formObj.elements['bmi_weight'] == null) || (formObj.elements['bmi_result'])) {
    alert('could not find the bmi input boxes');
    return;
  }
  */
  
  strErr = '';
  intHeightMin = 30;
  intHeightMax = 270;
  intWeightMin = 35;
  intWeightMax = 200;
  // try and coerce input to integers
  intHeight = parseInt( formObj.elements['bmi_height'].value );
  intWeight = parseInt( formObj.elements['bmi_weight'].value );
  // check coerced height is a number
  if (isNaN(intHeight)) {
    strErr += 'Please enter your height in centimetres.\n';
  }
  else {
    // check height is valid - intHeightMin < height < intHeightMax
    if ((intHeight < intHeightMin) || (intHeight > intHeightMax)) {
      strErr += 'Please enter your height in centimetres.\n';
    }
  }
  // check coerced weight is a number
  if (isNaN(intWeight)) {
    strErr += 'Please enter your weight in kilograms.\n';
  }
  else {
    // check weight is valid - intWeightMin < weight < intWeightMax
    if ((intWeight < intWeightMin) || (intWeight > intWeightMax)) {
      strErr += 'Please enter your weight in kilograms between ' + intWeightMin + 'kg and ' + intWeightMax + 'kg.' + '\n(If you fall outside this kg range please consult your GP for a health assessment.)\n';
    }
  }
  if (strErr != '') {
    // we got an error
    alert(strErr);
	formObj.elements['bmi_result'].value = '';
  }
  else {
    // we got no error - the math.round ( res * 10 ) / 10 gives us our one decimal place
	formObj.elements['bmi_result'].value = decimalPlaces((intWeight / Math.pow((intHeight/100), 2)), 1 );
  }
 
}

function decimalPlaces(fltNumber, intDecimalPlaces) {
  if (!isNaN(parseFloat(fltNumber))) {
    // make sure we get a valid floating point number
    return Math.round( fltNumber * Math.pow(10,intDecimalPlaces) ) / Math.pow(10,intDecimalPlaces);
  }
  return 0
}