/*******
** vp **	
*******/

window.onload = function() {
	activateMenu('mainNav');
	setSearchBox('searchArea');
	poplayer('popup');
	setParent();
	openPopup('add-existing-article');
	externalLinks();
	setmessageSwitch();
    InitSlideList();
	setAccordion('menulist');
    checkSplash();
}

function setParent() {
	if(parent.mainFrame){
		parent.frameUrl();
	}
}

function poplayer(naam) {
	var links = document.getElementsByTagName('div');
	for(var i=0; i<links.length; i++){
		if(links[i].className.indexOf(naam) != -1){
			triggers = links[i].getElementsByTagName('a');
			for(var j=0; j<triggers.length; j++){
				if(triggers[j].className == 'popaction'){
					triggers[j].onclick = function (){
						divjes = this.parentNode.getElementsByTagName('div');
						for(var k=0; k<divjes.length; k++){
							if(divjes[k].className == 'pop-layer'){
								if(divjes[k].style.display != 'block'){divjes[k].style.display = 'block';}
							};
						}return false;
					}
				}else if(triggers[j].className == 'popclose'){
					triggers[j].onclick = function (){
						this.parentNode.parentNode.style.display = 'none';
						return false;
					}
				}
			}
			triggers = links[i].getElementsByTagName('input');
			for(var j=0; j<triggers.length; j++){
				if(triggers[j].className == 'popclose'){
					triggers[j].onclick = function (){
						this.parentNode.parentNode.style.display = 'none';
						return false;
					}
				}
			}
		}
	}
}



var SCREEN_WIDTH = 910;
var navPosition;

function activateMenu(ID) {
	setAHack('main');
	setAHack('header');

	if(document.getElementById(ID)) {
		var navPosition = document.getElementById(ID).offsetLeft;
		var items = document.getElementById(ID).getElementsByTagName('li');
		for (var i=0; i<items.length; i++) {
			if(items[i].id) {
				if (document.getElementById(items[i].id + '-hover')) {
					items[i].menu = document.getElementById(items[i].id + '-hover');
					items[i].menu.style.left = getXPos(items[i]) + "px";
				}
				items[i].onmouseover = function (e) {
					if (self.opendropdown) closeOpen();
					if (this.childNodes[0].className != 'selected') {
						this.childNodes[0].className = 'over';
					}
					if (this.childNodes[0].className == 'selected') {
						this.childNodes[0].className = 'selected';
					}
					if (this.menu) {
						this.menu.style.visibility = 'visible';
					}
					self.opendropdown = this;
				};
				items[i].onmouseout = function (e) {closeNav(e) };
				
				if (document.getElementById(items[i].id + '-hover')) {
					items[i].menu.onmouseout = function (e) {closeNav(e)};
				}
			}
		}
	}
}


function getXPos(navItem) {
	var xCorrection = -1;
	var firstPos = navItem.offsetLeft;
	var subWidth = document.getElementById(navItem.id + '-hover').offsetWidth;
	if ((firstPos + navPosition + subWidth) > SCREEN_WIDTH) {
		firstPos += (navItem.offsetWidth - subWidth);
		xCorrection = 0;
	}

	return firstPos + xCorrection;
}

function closeNav(e) {
	var to = (e) ? e.relatedTarget : event.toElement;
	if (self.opendropdown) {
		if (to) {//Do not close in response to events within the menu itself
			var tg = to.tagName.toLowerCase();
			if (tg == "ul") return false;
			if (tg == "li") return false;
			if (tg == "a") return false;
			if (tg == "p") return false;
			if (tg == "strong") return false;
			if (tg == "div") return false;
			if (tg == "abbr") return false;
			if (tg == "span") return false;
			if (to == self.opendropdown.menu) return false;
		}
		closeOpen();
		return true;
	}
}

function closeOpen () {
	if (self.opendropdown.childNodes[0].className.indexOf('selected') == -1) {
		self.opendropdown.childNodes[0].className = '';
	}
	if (self.opendropdown.childNodes[0].className.indexOf('selected') != -1) {
		self.opendropdown.childNodes[0].className = 'selected';
	}
	if (self.opendropdown.menu) {
		self.opendropdown.menu.style.visibility = 'hidden';
	}
}



function setAHack(ID) {
	if (document.getElementById(ID)) {
		
		var c = document.getElementById(ID).getElementsByTagName('*');
		for	(var i=0; i<c.length; i++) {
			c[i].onmouseover = function (e) {
				closeNav('e');
			};
		}
	}
}


function setSearchBox(ID) {
	if(document.getElementById(ID)) {
		var entries = document.getElementById(ID).getElementsByTagName('input');
		for (var i=0; i<entries.length; i++) {
			if (entries[i].type == 'text') {
				entries[i].onfocus = function() {
					switch(this.id) {
						case 'z' : if(this.value == 'zoek op trefwoord') this.value = ''; break;
					}
				}
				entries[i].onblur = function () {
					if (this.value == '') {
						switch(this.id) {
							case 'z' : this.value = 'zoek op trefwoord'; break;
						}
					}
				}
			}
		}
	}
}

function openPopup(classnaam){
	items = document.getElementsByTagName('a');
	for(var i=0; i<items.length; i++){
		if(items[i].className == classnaam){
			items[i].onclick = function (){
				popUp(this.href,'fixed',678,1021);
				return false;
			}
		}
	}
}


function popUp(strURL,strType,strHeight,strWidth) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="elastic")
	{
	    if (!strHeight || !strWidth)
	    {
		strOptions="status,toolbar,menubar,scrollbars,resizable,location";
	    }
	    else
	    {
		strOptions="status,toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	    }
	}
	window.open(strURL, 'newWin', strOptions);
}

function setBond(value)
{
    var bondList = document.getElementById("bondList");
    
    var tbxBond = document.getElementById("tbxbond");
    tbxBond.innerHTML = '<a href="' + bondList.options[bondList.selectedIndex].value + '" onclick="popUp(this.href,\'elastic\');return false;">' + bondList.options[bondList.selectedIndex].title + '</a>';
}

function changeContribution(newText)
{
    var contrib = document.getElementById("contribution");
    contrib.innerHTML = newText;
}

function changeState(formName, type)
{
    if(type=="f")
    {
	document.forms[formName].aannrbg.value="";
	document.forms[formName].atnv.value="";
	//document.forms[formName].aannrbg.disabled=true;
	document.getElementById("rekeNumber").style.display="none";
    }
    else if(type=="w")
    {
        document.getElementById("welNietLid3").style.display="block";
	//document.forms[formName].lidnr.disabled=false;

        document.forms[formName].aanosltr.value="";
	//document.forms[formName].aanosltr.disabled=true;
        document.forms[formName].aanosvrv.value="";
	//document.forms[formName].aanosvrv.disabled=true;
        document.forms[formName].aannm.value="";
	//document.forms[formName].aannm.disabled=true;
	document.getElementById("welNietLid1").style.display="none";
        
        document.forms[formName].aanadphn.value="";
	//document.forms[formName].aanadphn.disabled=true;	
        document.forms[formName].aanadpth.value="";
	//document.forms[formName].aanadpth.disabled=true;
        document.forms[formName].aanstrnm.value="";
	//document.forms[formName].aanstrnm.disabled=true;
        document.forms[formName].aanpltnm.value="";
	//document.forms[formName].aanpltnm.disabled=true;
        document.forms[formName].aankdeml.value="";
	//document.forms[formName].aankdeml.disabled=true;
	document.getElementById("welNietLid2").style.display="none";
    }
    else if(type=="n")
    {
        document.forms[formName].lidnr.value="";
	//document.forms[formName].lidnr.disabled=true;
	document.getElementById("welNietLid3").style.display="none";

	document.getElementById("welNietLid1").style.display="block";
	//document.forms[formName].aanosltr.disabled=false;
	//document.forms[formName].aanosvrv.disabled=false;
	//document.forms[formName].aannm.disabled=false;

	document.getElementById("welNietLid2").style.display="block";
	//document.forms[formName].aanadphn.disabled=false;	
	//document.forms[formName].aanadpth.disabled=false;
	//document.forms[formName].aanstrnm.disabled=false;
	//document.forms[formName].aanpltnm.disabled=false;
	//document.forms[formName].aankdeml.disabled=false;
    }
    else if(type=="c")
    {
	if(document.forms[formName].aanoswrd[0].checked)
	{
            //document.forms[formName].atnv.disabled=true;
	    //document.forms[formName].aannrbg.disabled=true;
	}
    }
    else if (type=="")
    {
	document.getElementById("rekeNumber").style.display="block";
    }

    else if(type=="land")
    {
	if(document.forms[formName].lndnm.options[document.forms[formName].lndnm.selectedIndex].value == '9909d46a-0a3a-436b-8027-3bc81c511e13_Nederland')
	{
            document.forms[formName].strnm.className='tbreadonly';
            document.forms[formName].pltnm.className='tbreadonly';
            document.forms[formName].strnm.readOnly='readonly';
            document.forms[formName].pltnm.readOnly='readonly';
            document.forms[formName].strnm.value="";
            document.forms[formName].pltnm.value="";
	}
        else
        {
            document.forms[formName].strnm.className='tb';
            document.forms[formName].pltnm.className='tb';
            document.forms[formName].strnm.readOnly='';
            document.forms[formName].pltnm.readOnly='';
        }
    }
}

function showTab(num){
  for(i=0; i<4; i++){
    if(i == num) {
      document.getElementById(document.forms['gegevenswijzigen'].type[i].value).style.display="block";
    } else document.getElementById(document.forms['gegevenswijzigen'].type[i].value).style.display="none";
  }
}

function setFormAction2(action, formName, pageIsValid, id, anchorValue)
{
    //var pageIsValid = validatePostCodeCheck(formName);

    if (pageIsValid)
    {
        var form = document.forms[formName];

        if (typeof form != 'undefined')
        {
            form.action = action;
            var input = document.createElement("input");
            input.setAttribute("type", "hidden");
            input.setAttribute("name", "anchorValue");
            input.setAttribute("value", anchorValue);
            var node = document.getElementById(id);
            node.parentNode.appendChild(input);
            form.submit();
        }
    }
}

function setFormAction(action, formName, pageIsValid)
{
    //var pageIsValid = validatePostCodeCheck(formName);
    
    if (pageIsValid)
    {
        var form = document.forms[formName];

        if (typeof form != 'undefined')
        {
            form.action = action;
            form.submit();
        }
    }
}

function copyWerkgeversAdres(gekozenWaarde)
{
    var form = $("aanmelden").name;
    if(gekozenWaarde != null && gekozenWaarde != "")
    {
      optionValue = document.getElementById(gekozenWaarde).innerHTML;
      plaats = document.getElementById("pltnmorg").value;
      bedrijfsnaam = optionValue.substring(0, optionValue.indexOf("("));
      
      optionValue = optionValue.substring(optionValue.indexOf("(")+1, optionValue.length);
      straat = optionValue.substring(optionValue.indexOf("(")+1, optionValue.indexOf(","));
      temp = optionValue.substring(optionValue.indexOf(",")+2);
      marktSelectId = gekozenWaarde;
          
      if(temp.indexOf(" ") != -1)//is er een huisnummertoevoeging aanwezig?
      {
          huisnummer = temp.substring(0, temp.indexOf(" "));
          huisnummerToevoeging =  temp.substring(temp.indexOf(" ")+1, temp.indexOf(")"));
      }
      else//geen huisnummertoevoeging
      {
          huisnummer = temp.substring(0, temp.indexOf(")"));
          huisnummerToevoeging = "";
      }
      
      document.forms[form].strnmorg.value = straat;
      document.forms[form].psthnvst.value = huisnummer;
      document.forms[form].orgthvst.value = huisnummerToevoeging;
    }
    else if(gekozenWaarde == null || gekozenWaarde == "")
    {
      document.forms[form].strnmorg.value = "";
      document.forms[form].psthnvst.value = "";
      document.forms[form].orgthvst.value = "";
    }
    new Ajax.Request("/HandleMarktSelect", {
	method:'get',
	parameters: {action: 'getPostalCode', street: straat, houseNumber: huisnummer, houseNumberAtt: huisnummerToevoeging, city: plaats, marktSelectId: marktSelectId},
	onSuccess: function(transport) {
            var json = transport.responseText.evalJSON();
            var jsonpostcode = json.postcode;
            document.forms[form].orgnm.value = bedrijfsnaam.replace("&amp;", "&");
            document.forms[form].selectedmsid.value = marktSelectId;
            document.forms[form].selectedmsStraat.value = straat.replace("&amp;", "&");
            document.forms[form].selectedmsHuisnummer.value = huisnummer;
            document.forms[form].selectedmsToevoeging.value = huisnummerToevoeging.replace("&amp;", "&");
            if(jsonpostcode != "error")
            {
                document.forms[form].orgpc.value = json.postcode;
                document.forms[form].selectedmsPostcode.value = json.postcode;
                $("errorDiv").removeClassName("formItem");
                $("errorDiv").addClassName("hide");
            }
            else
            {
                $("errorDiv").removeClassName("hide");
                $("errorDiv").addClassName("formItem");
            }
	}
    });
}

function validatePostCodeCheck(formName)
{
    var isValid = true;

    if (!trim(document.forms[formName].adphn.value).match(huisnummerExpression)) {
        document.forms[formName].adphn.className = 'tb smallwt error';
        document.getElementById('error_adphn').className = 'formItem';
        isValid = false;
    } else {
        document.forms[formName].adphn.className = 'tb smallwt';
        document.getElementById('error_adphn').className = 'hide';
    }
    
    if ((document.forms[formName].lndnm.options[document.forms[formName].lndnm.selectedIndex].value == '139c6f1b-8b60-4cce-b813-edf7d27bb31b_Nederland' ||document.forms[formName].lndnm.options[document.forms[formName].lndnm.selectedIndex].value == '9909d46a-0a3a-436b-8027-3bc81c511e13_Nederland') && !trim(document.forms[formName].adppk.value).match(/^[1-9]\d{3}\s*[A-Za-z]{2}$/)) {
    	document.forms[formName].adppk.className = 'tb smallwt error';
        document.getElementById('error_adppk').className = 'formItem';
        isValid = false;
    } else {
        document.forms[formName].adppk.className = 'tb smallwt';
        document.getElementById('error_adppk').className = 'hide';
    }
        
    if (!isValid) {
        document.getElementById('message').className = 'message';
	    return false;
	} else {
        document.getElementById('message').className = 'hide';
        return true;
    }
    
}

function validatePostCodeCheckAanmelder(formName)
{
    var isValid = true;

    if (!trim(document.forms[formName].aanadphn.value).match(huisnummerExpression)) {
        document.forms[formName].aanadphn.className = 'tb smallwt error';
        document.getElementById('error_aanadphn').className = 'formItem';
        isValid = false;
    } else {
        document.forms[formName].aanadphn.className = 'tb smallwt';
        document.getElementById('error_aanadphn').className = 'hide';
    }

    if (!trim(document.forms[formName].aanadppk.value).match(/^[1-9]\d{3}\s*[A-Za-z]{2}$/)) {
    	document.forms[formName].aanadppk.className = 'tb smallwt error';
        document.getElementById('error_aanadppk').className = 'formItem';
        isValid = false;
    } else {
        document.forms[formName].aanadppk.className = 'tb smallwt';
        document.getElementById('error_aanadppk').className = 'hide';
    }

    if (!isValid) {
        document.getElementById('message').className = 'message';
	    return false;
	} else {
        document.getElementById('message').className = 'hide';
        return true;
    }

}

function validateSearchMarktSelect(formName)
{
    var isValid = true;

    //Naam
    if (document.forms[formName].orgnm.value.length <= 0) {
        document.forms[formName].orgnm.className = 'tb error';
        document.getElementById('error_orgnm').className = 'formItem';
        isValid = false;
    } else {
        document.forms[formName].orgnm.className = 'tb';
        document.getElementById('error_orgnm').className = 'hide';
    }

    //Plaats
    if (document.forms[formName].pltnmorg.value.length <= 0) {
    	document.forms[formName].pltnmorg.className = 'tb error';
        document.getElementById('error_pltnmorg').className = 'formItem';
        isValid = false;
    } else {
        document.forms[formName].pltnmorg.className = 'tb';
        document.getElementById('error_pltnmorg').className = 'hide';
    }
        
    if (!isValid) {
        document.getElementById('message').className = 'message';
	    return false;
	} else {
        document.getElementById('message').className = 'hide';
        return true;
    }
}

function ns_onclick (a, url, name, type, winopt)
{ 
    var ns_l="http://nl.sitestat.com/fnv/fnvbondgenoten/s?"; 
    ns_l+=name;ns_l+='&ns_type='+type+'&ns_action=view'; 
    ns_l+='&ns__t='+(new Date()).getTime();if(!url)url=a.href; 
    var ns_0=document.referrer;if(ns_0.lastIndexOf('/')==ns_0.length-1) 
    ns_0=ns_0.substring(ns_0.lastIndexOf('/'),0); 
    if (ns_0.length>0)ns_l+='&amp;ns_referrer='+escape(ns_0); 
    winopt=winopt||"";var 
    target=(a&&a.target&&a.target!="")?(a.target.substring(0,1)=="_")?a.target.substring(1):a.target:"self"; 
    var ns_i=new Image();if(target&&url){if(window[target]){window.ns_softclick_timer=function(target,url) 
    {return function(){ns_i.onload=ns_i.onerror=function(){return;}; 
    window[((window[target])?target:"self")].location.href=url;}}(target,url); 
    window.setTimeout('ns_softclick_timer()',5000);ns_i.onload=ns_i.onerror=window.ns_softclick_timer;}else{ 
    window.open(url,target,winopt);}}ns_i.src=ns_l;return false;
}

function externalLinks() {
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a");
	 for (var i=0; i<anchors.length; i++) { 
	   var anchor = anchors[i];
	   if (anchor.getAttribute("href") && 
		   anchor.getAttribute("rel") == "external")
		 anchor.target = "_blank";
	 }
}

function setmessageSwitch() {
	var layerID = 'layer-1';
	if (document.getElementById(layerID)) {
		messageOn('layer-1');
		var entries = document.getElementById(layerID).getElementsByTagName('a');
		for (var i=0; i<entries.length; i++) {
			if (entries[i].getAttribute("href") && entries[i].getAttribute("rel") == "internal") {
				messageOn('layer-1');}
			else {
				entries[i].onclick = function (){
				messageOff('layer-1');}			
			}
		}
	}	
}

function messageSwitch(divId, dispStyle) {
	if (/msie 6/i.test(navigator.userAgent)) {
		document.getElementById('fadelayerIE6').style.display = dispStyle;
	}
	document.getElementById(divId).style.display = dispStyle;
	document.getElementById('fadelayer').style.display = dispStyle;
	return false;
}

function messageOn(divId) {
	return messageSwitch(divId, 'block');
}

function messageOff(divId) {
	return messageSwitch(divId, 'none');
}


function resizeBox (ID, direction) {
	if (document.getElementById(ID)) {
		var el = document.getElementById(ID);
		var inc_value = 100;
		var current_height = parseInt(el.offsetHeight);
		var new_height = 0;
		current_height = current_height ? current_height : 100;
		
		//-----------------------------------------
		// If we're not going smaller than 100...
		//-----------------------------------------
	
		if (current_height >= 50) {
			if (direction == 'up') {
				new_height = current_height - inc_value;
			}
			else {
				new_height = current_height + inc_value;
			}
			
			if (new_height > 99) {
				el.style.height = new_height + 'px';
				//ipsclass.my_setcookie('ips_rte_height', new_height, 1);
			}
		}
	}
}

// Start onmouseover show accordion left column

function setAccordion(id) {
	if (document.getElementById(id)) {
		setInternalLinks(id);
		var entries = document.getElementById(id).getElementsByTagName('li');
		for (var i=0; i<entries.length; i++) {
			entries[i].onclick = function () {
				if (this.className == 'selected') {
					this.className = '';
				}
				else {
					closeAll(id, i);
					this.className = 'selected';
				}
			};
		}
	}
}


function setInternalLinks (id) {
	if (document.getElementById(id).getElementsByTagName('a')) {
		var linkItems = document.getElementById(id).getElementsByTagName('a');
		for (var i=0; i<linkItems.length; i++) {
			if (linkItems[i].className == 'main') {
				linkItems[i].href = 'javascript:;';
				linkItems[i].onfocus = function() {this.blur();};
			}
		}
	}
}

function closeAll(id, currentlySelected) {
	if (document.getElementById(id).getElementsByTagName('li')) {
		var entries = document.getElementById(id).getElementsByTagName('li');
		for (var i=0; i<entries.length; i++) {
			if (entries[i].className == 'selected' && i != currentlySelected) {
				entries[i].className = '';
			}
		}
	}
}
// End onmouseover show accordion left column

function openStaticPopup(type) {
    jQuery.openPopupLayer({
        name: "staticPopup",
        width: 916,
        url: "/?view=popup&type="+type+""
    });
}

function checkSplash(){
    var COOKIE_NAME = 'fnvbondgenoten_splash';
    var COOKIE_VALUE = 'true';

    // check if the browser accept cookies
    if(jaaulde.utils.cookies.test()){
        // only display the splash when the cookie does not exist.
        if(jaaulde.utils.cookies.get(COOKIE_NAME) == null){
            jaaulde.utils.cookies.set(COOKIE_NAME, COOKIE_VALUE, { path: '/' , hoursToLive: 24});
            jQuery('#splashTarget').show();
        }
    }
}

jQuery(document).ready(function(){
    jQuery.setupJMPopups({
        screenLockerBackground: "#003366",
        screenLockerOpacity: "0.7"
    });
    jQuery(".popupBranches").click(function (){
       openStaticPopup("branches");
       return false;
    });
    jQuery(".popupCompanies").click(function (){
       openStaticPopup("bedrijven");
       return false;
    });
    jQuery(".popupIam").click(function (){
       openStaticPopup("ikben");
       return false;
    });
    jQuery(".closeLink").click(function (){
        closePopupLayer("staticPopup");
    });
});