var myGlobalHandlers = {
	onCreate: function(){
		Element.show('loading');
	},
	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('loading');
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);

// Define globals
Location.initialize(0, 'populateRegion'); // load regions

//************** AJAX HANDLERS FOR POPULATING DROP DOWNS ********************
function populateRegion(xmlReq) {
  var xpathHelper = new XpathHelper(xmlReq.responseText);
  locList = xpathHelper.query("//location");
  for(i=0;i<locList.length;i++) {
	appendToSelect($('selRegion'), locList[i].attributes[0].nodeValue, locList[i].attributes[1].nodeValue, sRegion);
  }
}
function populateCountries(xmlReq) {
  var xpathHelper = new XpathHelper(xmlReq.responseText);
  locList = xpathHelper.query("//location");
  for(i=0;i<locList.length;i++) {
	appendToSelect($('selCountry'), locList[i].attributes[0].nodeValue, locList[i].attributes[1].nodeValue, sCountry);
  }
  
  if( sCountry ) {  
    changeCountry();
	sCountry = '';
  }
}
function populateLocations(xmlReq) {
  var xpathHelper = new XpathHelper(xmlReq.responseText);
  locList = xpathHelper.query("//location");
  for(i=0;i<locList.length;i++) {
	appendToSelect($('selLocation'), locList[i].attributes[0].nodeValue, locList[i].attributes[1].nodeValue, sLocation);
  }
  
  if( sLocation ) {  
    changeContacts();
	sLocation = '';
  }
}
//***************************************************************************

//************** AJAX HANDLERS FOR CONTACT INFO ********************
function loadContacts(xmlReq) {
  $('contacts-1').innerHTML = "";
  $('contacts-2').innerHTML = "";
  $('contacts-3').innerHTML = "";
  var xpathHelper = new XpathHelper(xmlReq.responseText);
  cNodes = xpathHelper.query("//contact");
  if( (cNodes!=null) && (cNodes.length > 0) ) {
	cNodesLength = cNodes.length;
    for(i=0;i<cNodesLength;i++) {  // loop thru array of contact nodes
	  type = cNodes[i].attributes[0].nodeValue;  //getting contact type attribute
	  var contact = new Contact();
      cNodesChildLength = cNodes[i].childNodes.length;
	  for(j=0;j<cNodesChildLength;j++) {  //loop thru child nodes of this contact node
        node = cNodes[i].childNodes[j];
        if( (node.nodeType==1) ) { // denotes element node
		  text = (node.firstChild!=null) ? node.firstChild.nodeValue : '';
		  contact.setProperty(node.nodeName, text);
        }
      }
	  $('contacts-'+type).innerHTML += contact.toHtml();
	}
  }
}
//******************************************************************

//********************** FUNCTIONS TO UPDATE GUI **************************
function showLoading(xmlReq) {
	$('loading').style.display = 'block';
}
function hideLoading(xmlReq) {
	$('loading').style.display = 'none';
}
// empty select list content
function clearSelect(el) {
  while (el.length > 1) el.remove(1);
}
// add item to select element
function appendToSelect(el, value, content, selectedValue) {
  el.options[el.options.length] = new Option(content, value);
  if( selectedValue == value ) el.options[el.options.length-1].selected = true;
}
// fetches the location data for countries in the specified region and populates the country drop down
function changeRegion() {
  clearSelect($('selCountry'));  // reset the country drop down
  clearSelect($('selLocation'));  // reset the location drop down
  $('btnFind').disabled = true;  // disable find button
  if( $('selRegion').value ) {  // make sure region is selected
	Location.initialize($('selRegion').value, 'populateCountries');
	$('status').innerHTML      = 'Please select a Sub-Region'; 
	$('selCountry').disabled   = false;
	$('selLocation').disabled  = true;
  } else {
	$('status').innerHTML      = ''; 
	$('selCountry').disabled   = true;
	$('selLocation').disabled  = true;
  }
  $('map').style.display       = 'none';
  $('mapMsg').style.display    = 'none';
  $('mapToggle').style.display = 'block';
  return false;
}
// fetches the location data for locations in the specified country and populates the location drop down
function changeCountry() {
  clearSelect($('selLocation'));
  $('btnFind').disabled = false;
  if( $('selCountry').value ) {
	Location.initialize($('selCountry').value, 'populateLocations');
	$('status').innerHTML     = 'Please select a Location or click on Find';
	$('selLocation').disabled = false;
  } else {
	$('status').innerHTML     = '';
	$('selLocation').disabled = true;
  }
}
function start(region) {   
  $('selRegion').value = region;
  changeRegion();
}
function showSearchBy() {
  crumbArr = new Array();
  if( ($('selRegion').value!=null) && ($('selRegion').value!="") )     { crumbArr.push($('selRegion').options[$('selRegion').selectedIndex].text); }
  if( ($('selCountry').value!=null) && ($('selCountry').value!="") )   { crumbArr.push($('selCountry').options[$('selCountry').selectedIndex].text); }
  if( ($('selLocation').value!=null) && ($('selLocation').value!="") ) { crumbArr.push($('selLocation').options[$('selLocation').selectedIndex].text); }
  if( crumbArr.length>0 ) {
    var crumb = "";
    for(var i=0; i<crumbArr.length; i++) { 
      crumb += (i!=(crumbArr.length-1)) ? crumbArr[i]+" &gt; " : crumbArr[i].toUpperCase();
    }
    $('searchBy').style.display = 'block';
    $('searchBy').innerHTML = "&nbsp;Current filter: " + crumb;
  }
}
function init() {
  $('selCountry').disabled  = true;
  $('selLocation').disabled = true;
  $('btnFind').disabled     = true;
  if( sRegion ) {
	  start(sRegion);
	  sRegion = '';
  }
}
function changeContacts() {
  $('status').innerHTML = '';
  if( $('selCountry').value || $('selLocation').value ) {
	flag  = false;  // find contacts for location
	locId = $('selLocation').value;
	if(($('selCountry').value!='') && ($('selLocation').value=='')) {
	  flag = true;  // find contacts for country
	  locId = $('selCountry').value;
	}
	Contact.initialize(locId, flag,'loadContacts');
	showSearchBy();
	$('map').style.display       = 'none';
	$('results').style.display   = 'block'; //new Effect.Appear("results");
	$('mapToggle').style.display = 'block';
  }
}
//*************************************************************************

// Define Behavior rules
var myrules = {
  '#selRegion' : function(el) {
    el.onchange = function() { changeRegion(); return false; }
  },
  '#selCountry' : function(el) {
    el.onchange = function() { changeCountry(); return false; }
  },
  '#selLocation' : function(el) {
    el.onchange = function() { $('btnFind').disabled = false; return false; }
  },
  '#btnFind' : function(el) {
    el.onclick = function() { changeContacts(); return false; }
  },
  '#americas' : function(el) {
    el.onclick = function() { start(1); return false; }
  },
  '#emea1' : function(el) {
    el.onclick = function() { start(2); return false; }
  },
  '#emea2' : function(el) {
    el.onclick = function() { start(2); return false; }
  },
  '#apac' : function(el) {
    el.onclick = function() { start(3); return false; }
  },
  '#mapToggle a' : function(el) {
    el.onclick = function() {
	  new Effect.Appear("map");
	  $('mapMsg').style.display    = 'block';
      $('results').style.display   = 'none';
      $('mapToggle').style.display = 'none';
	  $('selRegion').value         = '';
      clearSelect($('selCountry'));
	  clearSelect($('selLocation'));
	  $('selCountry').disabled     = true;
      $('selLocation').disabled    = true;
	  $('btnFind').disabled        = true;
	  $('status').innerHTML        = '';
      return false;
    }
  }
};

Behaviour.register(myrules);

Event.observe(window, 'load', init, false);
Event.observe(window, 'unload', Event.unloadCache, false);

// DREAMWEAVER FUNCTIONS FOR MAP ROLLOVERS
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); return x;
}
function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}