function checkNav() {
  if (!document.getElementById) {
    clearInterval(domload)
    domload = null;
    return false; 
  }
  if (document.getElementById("menu") && !menuInit) {
    clearInterval(domload);
    $('menu').setStyle('opacity',0);
    domload = null;
    return true;
  } else {
    return false;
  }
}
var menuInit = false;
var domload = setInterval("checkNav()", 25);

window.addEvent('domready',function() {
  //Op de homepage is het logo geen link
  if (location.pathname == '/') {
    var logoimg = $('logolink').getChildren()[0];
    $('logolink').replaceWith(logoimg);
  }
 
  showMenu();
  makeAnchors();
  setHeight();
  setSideContentOpacity();

  if ($('newsname') && $('newsemail')) {

	  //alert('JA');
  
	  $('newsname').addEvent('focus', function() {
		if ($('newsname').value == 'Name') {
			$('newsname').value = '';
		}
	  });

	  $('newsname').addEvent('blur', function() {
		if ($('newsname').value == '') {
			$('newsname').value = 'Name';
		}
	  });

	  $('newsemail').addEvent('focus', function() {
		if ($('newsemail').value == 'E-mail') {
			$('newsemail').value = '';
		}
	  });

	  $('newsemail').addEvent('blur', function() {
		if ($('newsemail').value == '') {
			$('newsemail').value = 'E-mail';
		}
	  });
  }
  
  $('q').addEvent('focus', function() {
	if ($('q').value == 'Search') {
		$('q').value = '';
	}
  });

  $('q').addEvent('blur', function() {
	if ($('q').value == '') {
		$('q').value = 'Search';
	}
  });

});
/*----------------------------------------------------------------------------------------------------*/

function makeAnchors()
{
	var i = 1;
	if (location.href.contains('prismapro'))	{
		$$("h3").each(function(el) { el.id = "f" + i; i++ });
	}
}

/*----------------------------------------------------------------------------------------------------*/

function setHeight() {
  var contentHeight = $('main').getStyle('height').toInt();
  var minHeight = 200;
  if (contentHeight == minHeight){
	  $('main').setStyle('height', 'inherit');
  }
  if (contentHeight < minHeight) $('main').setStyle('height',minHeight+'px');
}

/*----------------------------------------------------------------------------------------------------*/
function showMenu() {
  myLocation = location.pathname;

  if (myLocation == '/software/login.asp') myLocation = '/software/';
  if (myLocation == '/options/login.asp') myLocation = '/options/';
  if (myLocation == '/options/index.asp') myLocation = '/options/';
  
  if (myLocation != '/' && myLocation != '/products/' && myLocation != '/ecs/' && myLocation != '/search/' && myLocation != '/downloads/' && myLocation != '/service/' && myLocation != '/chinese/') {
    var divId = myLocation.replace(/\//g,'');
    
    if (divId == 'news' || divId == 'careers' || divId == 'exhibitions' || divId == 'contact') divId = 'about'; 
	if (divId == 'options') divId = 'options';

    if ($$('#'+divId+' .submenu') != '') {
		$$('#'+divId+' .submenu').setProperty('id','active');

		// Create div-spacer, width the submenu's height
		var spacer = new Element('div');
		var subHeight = $('active').getStyle('height').toInt()+50;
		spacer.setStyle('height',subHeight+'px');
		var sidecontent = $$('#content .left')[0];
		if (sidecontent.getFirst()) spacer.injectBefore(sidecontent.getFirst());
    }
  }
  //getActiveMenu();
  $('menu').setStyle('opacity',1);
  menuInit = true;
}
/*----------------------------------------------------------------------------------------------------*/

function getActiveMenu() {
  var width = 0;
  var i, leftDist;
  $('active').getChildren().each(function(li) {
    i = li.getStyle('width').toInt()
    width = width + i;
  });
   
  $$('.submenu').each(function(hide) {
    if (hide.getProperty('id') != 'active') hide.setStyle('display','none');
  });
}
/*----------------------------------------------------------------------------------------------------*/

function setSideContentOpacity() {
  $$('.sidecontent').each(function(sideContent) {
    var height = sideContent.getChildren()[0].getStyle('height');
    var innerdiv = new Element('div',{
      'styles': {
        'height': height,
        'opacity': 0.7
      },
      'class': 'transbg'
    });
    sideContent.setStyle('height',height);
    innerdiv.injectInside(sideContent);
  });
}
/*----------------------------------------------------------------------------------------------------*/

function selectedLink(link) {
  $$('#productsubnav a').each(function(subLink) {
    subLink.removeClass('selected');
  });
  $(link).addClass('selected');
}

/*----------------------------------------------------------------------------------------------------*/

function toggleSub(aId) {
  var subHeight = 10;
  var sub = $(aId).getParent().getChildren()[1];
  if (sub.getStyle('display') == 'none') {
    sub.setStyle('display','block');
    subHeight = subHeight + sub.getStyle('height').toInt();
    $$('.sidecontent').getChildren()[0].each(function(div) {
      var divHeight = div.getStyle('height').toInt();
      div.setStyle('height',divHeight+subHeight+'px  ');
    });
    $$('.sidecontent')[0].setStyle('height',$$('.sidecontent')[0].getStyle('height').toInt()+subHeight+'px  ');
  } else {
    subHeight = subHeight + sub.getStyle('height').toInt();
    sub.setStyle('display','none');
    $$('.sidecontent').getChildren()[0].each(function(div) {
      var divHeight = div.getStyle('height').toInt();
      div.setStyle('height',divHeight-subHeight+'px  ');
    });
    $$('.sidecontent')[0].setStyle('height',$$('.sidecontent')[0].getStyle('height').toInt()-subHeight+'px  ');    
  }
}

/*----------------------------------------------------------------------------------------------------*/
function getProductData(prodId,dataDescr) {
  var myProduct = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&product='+prodId+'&'+dataDescr,{method:'get',update:$('main')});
  myProduct.request().chain(setHeight).chain(updateLinks);
}
/*----------------------------------------------------------------------------------------------------*/
function updateRegionInfo(regionId) {
  var region = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&region='+regionId,{method:'get',update:$('regioninfo')});
  if (regionId == "") {
	$("googlemap").setStyle("display", "block");
	$("regioninfo").setStyle("display", "none");
  } else {
	$("googlemap").setStyle("display", "none");
	$("regioninfo").setStyle("display", "block");
  }
  region.request().chain(setHeight);
}

/*function updateRegionInfo(regionId)
{
	window.location = "?addresses=1&continent=" + regionId;
}*/

/*----------------------------------------------------------------------------------------------------*/
function getProdsByCat(catId) {
	
	$('downloadproduct').innerHTML = '';
	$('downloadtype').innerHTML = '';
	
	if (catId != '') {
		var prodcat = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&prodbycat='+catId,{method:'get',update:$('downloadproduct')});
		prodcat.request();
	}

	getDownloadItems()
}

/*----------------------------------------------------------------------------------------------------*/
function showDownloadTypes(prodId) {

	$('downloadtype').innerHTML = '';
	
	if (prodId != '') {
		var downcat = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&downloadtypes=1',{method:'get',update:$('downloadtype')});
		downcat.request();
	}

	getDownloadItems()
}

/*----------------------------------------------------------------------------------------------------*/
function getDownloadItems() {

	var dCat  = $('groupfilter')   ? $('groupfilter').options[$('groupfilter').selectedIndex].text : 'empty';
	var dProd = $('productfilter') ? $('productfilter').value : 'empty';
	var dType = $('typefilter')	   ? $('typefilter').value	  : 'empty';

	if (dCat == 'Choose a productgroup...')	{
		dCat = 'empty';
	}

	var downloads = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&getdownloaditems=1&dcat=' + dCat + '&dprod=' + dProd + '&dtype=' + dType,{method:'get',update:$('downloaditems')});
	downloads.request();
}


/*----------------------------------------------------------------------------------------------------*/
function showDownTypes(catId) {
  var downType = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&downtypes='+catId,{method:'get',update:$('downtype')});
  downType.request();
}

/*----------------------------------------------------------------------------------------------------*/
function showDownloads(downType,prodId) {
  var downloads = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&getdownloads='+prodId+'&type='+downType,{method:'get',update:$('downloadresult')});
  downloads.request();
}

/*----------------------------------------------------------------------------------------------------*/
function updateDownCount(downType,userId,softwareId) {
  var count = new Ajax('/_private/ajax.asp?rnd='+$random(10,999)+'&updatedownload='+softwareId+'&user='+userId+'&type='+downType,{method:'get'});
  count.request();
}

function updateLinks()
{
	$$('a[rel=external]').each(function(extLink) {
		extLink.setProperty('target', '_blank');
	});
}


window.addEvent('domready',function() {
  // External links open in new window
  $$('a[rel=external]').each(function(extLink) {
    extLink.setProperty('target','_blank');
  });
  
  if($$('form')[0]) {
    var myForm = new Form($$('form')[0]);
  }

  if ($('newsform')) {
	var myNewsForm = new Form($('newsform'));
  }

});

var Form = new Class({
  initialize: function(frm) {
    this.form = frm;
    this.form.addEvent('submit',this.frmCheck);
  },
   
  frmCheck: function(ev) {

    var misFld = false;
    var e = new Event(ev);
    e.stop();

	if (this.newsname) {
		if (this.newsname.value == 'Name') {
			this.newsname.value = '';
		}
	}

	if (this.newsemail) {
		if (this.newsemail.value == 'E-mail') {
			this.newsemail.value = '';
		}
	}
    
    $$('.reqFld').each(function(fld) {
      fld.removeClass('missingField');
      if(fld.getValue() == '') {
        fld.addClass('missingField');
        misFld = true;
      }
    });
    if(misFld == true) {
      alert('Missing required fields.');
    } else {
	  this.submit();
    }
  }
});