// get browser
var agent       = navigator.userAgent.toLowerCase(); 
var isIE        = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
var isNetscape6 = (!document.all && !document.layers && navigator.appName == "Netscape" && parseInt(navigator.appVersion) >=5) ? true : false;
var isMac       = (agent.indexOf("mac") != -1) ? true : false;
var isIE4       = (agent.indexOf("msie") != -1 && agent.indexOf("4.0;") != -1) ? true : false;
var isNetscape  = (agent.indexOf("mozilla") != -1 && agent.indexOf("4.") != -1) ? true : false;
var win2000     = ((agent.indexOf("winnt") != -1) || (agent.indexOf("windows nt 5.0") != -1)) ? true : false; 
var winXP       = ((agent.indexOf("winnt") != -1) || (agent.indexOf("windows nt 5.1") != -1)) ? true : false; 
var isWin       = (agent.indexOf("windows") != -1) ? true : false;
var isMoz       = (agent.indexOf("gecko") != -1) ? true : false;

// popup functions
function open_popup (ref, url, width, height, scroll, menu, toolbar, location, status, resize) {
  window.open(url,
              ref,
              "menubar="    + ((menu) ? 'yes' : 'no')  + ", " +
              "scrollbars=" + ((scroll) ? 'yes' : 'no')  + ", " +
              "toolbar="    + ((toolbar) ? 'yes' : 'no')  + ", " +
              "status="     + ((status) ? 'yes' : 'no')  + ", " +
              "location="   + ((location) ? 'yes' : 'no')  + ", " +
              "resizable="  + ((resize) ? 'yes' : 'no')  + ", " +
              "width=" + width + ", height=" + height
             );
}


// synchronise drop menus
function synchronise_menus (drop_menu, form, menu_1, menu_2) {
  var value = drop_menu.options[drop_menu.selectedIndex].value;

  menu_1 = form[menu_1];
  for (i = 0; i < menu_1.options.length; i++) {
    if (menu_1.options[i].value == value) {
      menu_1.selectedIndex = i;
      break;
    }
  }

  menu_2 = form[menu_2];
  for (i = 0; i < menu_2.options.length; i++) {
    if (menu_2.options[i].value == value) {
      menu_2.selectedIndex = i;
      break;
    }
  }

  return value;
}

// find index of a value in drop menu
function find_drop_menu_index (drop_menu, value) {
  for (i = 0; i < drop_menu.options.length; i++) {
    if (drop_menu.options[i].value == value) {
      return i;
    }
  }
}

// print value of drop menu in cell
function print_menu_value (drop_menu, cell) {
  var value = drop_menu.options[drop_menu.selectedIndex].value;

  if (value) {
    document.getElementById(cell).innerHTML = value;
  } else {
    document.getElementById(cell).innerHTML = '-';
  }
}

// check for positive integer
Math.isPositiveInteger = function (value) {
  if (value == Math.abs(Math.floor(value)) && value > 0) {
    return true;
  } else {
    return false;
  }
}

// show other box on customer title
function change_customer_title (drop_menu) {
  title = drop_menu.options[drop_menu.selectedIndex].value;

  if (title == 'Other') {
    document.getElementById('customer_title_other_div').style.visibility = 'visible'
  } else {
    document.getElementById('customer_title_other_div').style.visibility = 'hidden'
  }
}


// make name non mandatory if company name entered
var company_name_mandatory = true;
var name_mandatory = true;

function check_customer_names_mandatory () {
  if (form.customer_last_name.value || form.customer_first_name.value) {
    company_name_mandatory = false;
    form.customer_company_name.className = 'nonMandatory';
    validate_field(true, 'customer_company_name_cell');

  } else {
    company_name_mandatory = true;
    form.customer_company_name.className = 'mandatory';
  }

  if (form.customer_company_name.value) {
    name_mandatory = false;

    form.customer_first_name.className = 'nonMandatory';
    validate_field(true, 'customer_first_name_cell');

    form.customer_last_name.className = 'nonMandatory';
    validate_field(true, 'customer_last_name_cell');
  } else {
    name_mandatory = true;

    form.customer_first_name.className = 'mandatory';
    form.customer_last_name.className = 'mandatory';
  }
}

// validate form field
formKeyColour = '#EDF4E3';
function validate_field (field, cell) {
  if (!field) {
    document.getElementById(cell).style.backgroundColor = '#EE5566';
    document.getElementById(cell).style.color           = 'white';

    return false;
  } else {
    document.getElementById(cell).style.backgroundColor = formKeyColour;
    document.getElementById(cell).style.color           = '#666666';

    return true;
  }
}

// rollover functions
var rollovers = new Array;

function preload_image (ref, image) {
  rollovers[ref]           = new Image;
  rollovers[ref].src       = image + "_a.gif";
  rollovers[ref + "2"]     = new Image;
  rollovers[ref + "2"].src = image + "_b.gif";
}

function roll_over (ref) {
  document.images[ref].src = rollovers[ref + "2"].src;
}

function roll_out (ref) {
  document.images[ref].src = rollovers[ref].src;
}


// launch fullscreen
function launch_fullscreen (url, ref) {
  var win = window.open(url, ref, 'toolbar=no, location=no, menubar=no, toolbar=no, status=yes, location=no, resizable=yes, scrollbars=yes');
  win.moveTo(0,0);
  win.resizeTo(screen.availWidth,screen.availHeight);
}


// capitalise string
function capitalise_string (string) {
  if (string) {
    var a = string.split(/\s+/g); // split the sentence into an array of words

    for (i = 0 ; i < a.length ; i ++ ) {
      var firstLetter = a[i].substring(0, 1).toUpperCase();
      var restOfWord = a[i].substring(1).toLowerCase();

      a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
    }

    string = a.join(' '); // join it back together
  }

  return string;
}


// IE hide/show elements functions
function hide_element (elmID) {
  if (isIE) {
	  for (i = 0; i < document.all.tags(elmID).length; i++)	{
  		obj = document.all.tags(elmID)[i];

		  if (! obj || ! obj.offsetParent) {
  			continue;
      }
	    obj.style.visibility = "hidden";
	  }
  }
}

function show_element (elmID) {
  if (isIE) {
	  for (i = 0; i < document.all.tags(elmID).length; i++)	{
		  obj = document.all.tags(elmID)[i];

		  if (! obj || ! obj.offsetParent) {
			  continue;
      }
		  obj.style.visibility = "visible";
	  }
  }
}


// progress bar
function set_progress_bar (percentage, message) {
  width = (percentage == 100) ? 'auto' : percentage + '%';

  document.getElementById('progress_bar').style.width   = width;
  document.getElementById('progress_bar').innerHTML     = percentage + '%';
  document.getElementById('progress_message').innerHTML = message;
}

