/****************************************************************************
*                   Copyright (c) 2007 Mercator GeoSystems Ltd
* 
*                           Mercator GeoSystems Ltd
*                           10 St Quentin View
*                           Sheffield
*                           South Yorkshire
*                           S17 4PS
*                           ENGLAND
*
*                           Tel    +44 (0) 114 235 3409
*                           Fax    +44 (0) 114 235 3409
*                           E-Mail info@mercatorgeosystems.co.uk
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/

function showOptions(p_option, p_button)
{
  // get current state
  var curState = document.getElementById(p_option).style.display;
  // set new state
  //!!document.getElementById(p_option).style.display = (curState == 'none' ? '' : 'none');
  // Toggle DIV
  toggleSlide(p_option);
  // Set button text
  var button = document.getElementById(p_button);
  var label = button.innerHTML.substr(4);
  button.innerHTML = (curState == 'none' ? 'Hide' : 'Show') + label;
};

function showWaiting(p_show) {
  var waiting = document.getElementById('waiting');
  if (waiting) {
    if (p_show == true) {
      var img = document.getElementById('waitingImg');
      if (img) {
        img.style.left     = ((getWindowWidth()  / 2) - (126 / 2)) + 'px';
        img.style.top      = ((getWindowHeight() / 2) + getScrollXY()[1]) + 'px';
        img.style.zIndex   = 101;
        img.src            = img.src;
      }
      waiting.style.display='';
    } else {
      waiting.style.display='none';    
    }
  }
};

function onLoad() {
  // Hide any 'waiting' that  may be lingering!
  showWaiting(false);
  // get the form
  var frm = document.getElementById('dtForm');
  // got the form ok?
  if (frm) {
    // reset it
    // frm.reset();
    // update price
    updatePrice();
  }
  // Update buttons
  enableButtons();
};

function show_comment_form() {
  var url = "mgFeedback.php";
  window.open(url, "", "width=300,height=300,top=30,left=30,menubar=no,toolbar=no,location=no,directories=no,scrollbars=yes,statusbar=no,resizable=no");
};

function enableButtons()
{
  // enable the preview and buy now buttons only if a place name has been entered
  // get the form
  var frm = document.getElementById('dtForm');
  // got the form ok?
  if (frm) {
    // get value
    var place_name = frm.place_name.value;
    // enabled only if > 1 char (i.e B1 or more)
    var disabled = (place_name.length < 2);
    frm.preview.disabled = disabled;
    frm.buy.disabled     = disabled;
    // only enable sector list if 120 minutes or less
    var cutoff = frm.drivetime.options[frm.drivetime.selectedIndex].value;
    // if greatr than 120, and checked, then clear check
    if ((cutoff > 120) && (frm.include_sectors.checked == true)) {
      // clear any check mark
      frm.include_sectors.checked = false;
    }
    // enable / disable control
    frm.include_sectors.disabled = (cutoff > 120);
    // italic font to make it clearer it's disabled
    var sector_label = document.getElementById('sector_label');
    if (sector_label) {
      // change font style according to enabled or not
      sector_label.style.fontStyle = ((cutoff > 120) ? "italic" : "normal");
      // set text
      sector_label.innerHTML = ((cutoff > 120) ? 
                                "Include Postal Sector drive times table (disabled for cutoffs greater than 120 minutes)" :
                                "Include Postal Sector drive times table");
    }
  }
};

function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && (document.documentElement.clientWidth)) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && (document.body.clientWidth)) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
};


function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && (document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && (document.body.clientHeight)) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
};

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
