var totalMenuItems;
var activeMainId;
var totalShieldItems = 20;
var leftPos = 0;
var shieldOffsetTop = 20;
var curShieldId = '';

function showShield(id)
{
	
	//if(curShieldId != $(id).attr('id'))
	//{
	curShieldId = $(id).attr('id');


	var position = $(id).attr('className').split(' ')[1];	
	switch(position)
	{
	case 'keten_left':
		leftPos = 0;
		break;
	case 'keten_right':
		leftPos = 58;
		break;
	default:
		leftPos = 30;
	}
	topPos =  $(id).position();
	
	var item = '#'+id.id+'_shield';
	hideAllShield();
	$(item).css({'display' : 'block'});
	$(item).css({'z-index' : '15000'});
	$(item).css({'left' : leftPos});
	$(item).css({'top' : topPos.top - shieldOffsetTop});
	//}
}
function hideAllShield()
{	
	
	$('.keten_shield').css({'display' : 'none'});
}

function showActive(){
	var item = $('#menu_'+lang+'_btn_'+activeMainId+'_active');
	$(item).css({'display' : 'block'});
	$(item).css({'z-index' : '9998'});
}

$(document).ready(function()
{	
	if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6) {
	  try {
	    document.execCommand("BackgroundImageCache", false, true);
	  } catch(err) {}
	}
	
	$(".keten_item").mouseenter(function()
	{
		showShield(this);
	});
	
	$(".keten_shield").mouseleave(function()
	{	
		hideAllShield();
	});

	$(".keten_container").mouseleave(function()
	{	
		curShieldId = '';
	});
	
	for (var i = 1; i <= totalShieldItems; i++)
	{
		$("#keten_item_"+i+"_shield").click(function()
		{
			var id = $(this).attr('id').substring(11).split('_shield');
			var link = $('#keten_item_'+id[0]).children('a').attr('href');
			//location.href = link;
		});
	}
	
	var itemArray = new Array('cacao','hout','thee','natuursteen','soja','toerisme','katoen','kweekvis','electronica','specerijen');
	totalMenuItems = itemArray.length;
	var item = indexInArray(itemArray,itemActive); // replace toerisme by active page
	activeMainId = item + 1;
	
	showActive();
	
	for (var i = 1; i <= totalMenuItems; i++)
	{
		$('#menu_'+lang+'_btn_'+i).mouseover(function()
		{
			showMenu(this);
		});
		$('#menu_'+lang+'_btn_'+i+'_active').mouseout(function()
		{
			hideAllMenuItems();
		});
		$('#menu_'+lang+'_btn_'+i+'_active').click(function()
		{
			var id = $(this).attr('id').substring(12).split('_active');
			var link = $('#menu_'+lang+'_btn_'+id[0]).children('a').attr('href');
			location.href = link; 
		});
	}
});

function hideAllMenuItems()
{
	for (var i = 1; i <= totalMenuItems; i++)
	{
		if(activeMainId != i)
		{
			$('#menu_'+lang+'_btn_'+i+'_active').css({'display' : 'none'});
		}
		
	}
}
function showMenu(id) {
	var item = '#'+id.id+'_active';
	hideAllMenuItems();
	$(item).css({'display' : 'block'});
	$(item).css({'z-index' : '9999'});
}
function showActive(){
	var item = $('#menu_'+lang+'_btn_'+activeMainId+'_active');
	$(item).css({'display' : 'block'});
	$(item).css({'z-index' : '9998'});
}

attachRollOverEvent = function(image){
	
	$(image).mouseover(function(){
		$(this).css("left","-160px");
	});
	$(image).mouseout(function(){
		$(this).css("left","0px");
	});
}



function clearDefault(el)
{
  if (el.defaultValue == el.value) el.value = '';
}

function setDefault(el)
{
  if (el.value == '') el.value = el.defaultValue;
}

function indexInArray(theArray, theValue)
{
	var arLength = theArray.length;
	for(var i = 0; i < arLength; i++)
	{
		if (theArray[i] == theValue)
		{
			return i;
		}
	}
	return -1;
}




function e(id) { return document.getElementById(id) }

/* Contactform functions */

function toggleErrorLabel(label,state) {
	if(state == 'on') {
		document.getElementById(label).className = "errorlabel";
	}else{
		document.getElementById(label).className = "normallabel";
	}
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function ValidateForm(form) {

	var error = 0;
	var errorMessage = 'Je hebt dit formulier niet volledig ingevuld:\n\n';
		
	if(typeof(form.required)=='undefined') {
		return true;
	}
		
	
	var fieldnames = form.required.value.split(',');
	
	for(key in fieldnames) {
		if(e(fieldnames[key])) {
			type = e(fieldnames[key]).type
			label = e('label_'+fieldnames[key]).innerHTML;
			label = strip_tags(label);
			//label = fieldnames[key];
			if(type=='text' || type=='textarea') {
				if(e(fieldnames[key]).value == "") { 
					errorMessage += '- '+label+' is niet ingevuld\n';
					error = 1;
					toggleErrorLabel('label_'+fieldnames[key],'on');
				}else{
					toggleErrorLabel('label_'+fieldnames[key]);
				}
			} else if(type=='select-one') {
				if(e(fieldnames[key]).options[e(fieldnames[key]).selectedIndex].value=='' || e(fieldnames[key]).options[e(fieldnames[key]).selectedIndex].value=='0') {
					errorMessage += '- '+label+' is niet gekozen\n';
					error = 1;
					toggleErrorLabel('label_'+fieldnames[key],'on');
				}
			} else {
				alert(type)
			}
		}
	
	}

	if(error){
		alert(errorMessage);
		return false; 
	}else{
		return true;
	}
}

function strip_tags(str, allowed_tags) { // needed for validateform!
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Marc Palau
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Eric Nagel
    // +      input by: Bobby Drake
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
    // *     example 4: strip_tags('1 < 5 5 > 1');
    // *     returns 4: '1 < 5 5 > 1'
 
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
 
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

function openSite(URLStr)
{
    var maxx = screen.width;
    var maxy = screen.height;
    windowprops = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=100,height=100,left=100,top=100";
    window.open(URLStr+"&maxx="+maxx+"&maxy="+maxy,"",windowprops);
}
