/*
    $Revision: 1.3 $
*/
$(document).ready(function(){

	/*******
	** vp - Panel toggle  mijn dossier page - (c) - 0.0.1 **
	*******/
	$('input.sameAddress').each(function(i) {
	    if ($(this).is(':checked') ) {
	        $('div.sameAddress').filter(function(index) {
	            return index == i
	        }).hide();
	    }
	    else {
	        $('div.sameAddress').filter(function(index) {
	            return index == i
	        }).show();
	    }
	
		$(this).click(function(e) {
			if ($(this).is(':checked') ) {
				$('div.sameAddress').filter(function (index) {
					return index == i
				}).hide();
			}
			else {
				$('div.sameAddress').filter(function (index) {
					return index == i
				}).show();
			}
		});
	});
	
	/*******
	** vp - Panel toggle  mijn dossier page - (c) - 0.0.1 **
	*******/
	$('.panel').each(function () {
		$(this).find('h2').find('a').click(function(e) {
			$(this).blur();
			if ($(this).parent().parent().hasClass("openPanel")) {
				$(this).parent().parent().removeClass("openPanel")
			}
			else {
				//alert('ja');
				$(this).parent().parent().addClass("openPanel")
			}
			e.preventDefault();
		});
	});


	/*******
	** vp - forward dropdown homepage box - (c) - 0.0.1 **
	*******/
	$('#formDropdown1').submit(function(e) {
		$('#dropdown1 option:selected').each(function () {
			if ( $(this).val() != '') {
				document.location.href = $(this).val();
				//alert($(this).val());
				return true;
			}
			
			
		});
		
		e.preventDefault();
		
	});
	

	/*******
	** vp - print functionality - (c) - 0.0.1 **
	*******/
	$('.contentNav').find('li').each(function(){
		if ( $(this).hasClass('print')) {
			$(this).click(function() {
				window.print();
				return false;
			});
			$(this).toggle();
		}
	});

	
	/*******
	** vp - add target = _blank - (c) - 0.0.1 **
	*******/
	$('.pdffile').attr('target', '_blank');

	/*******
	** dk - add analytics to links
	*******/
	$('.pdffile').attr('onClick', '_gaq.push([\'_trackPageview\', \'' + $('.pdffile').attr('href') + '\']);');



	//pick one out of five stylesheets for the font size
	//and save in a cookie	//createCookie("fontsize","",-1); //delete cookie - only for testing, do not remove comment!
	var c = readCookie("fontsize");
	if(c) {
		setFont(c);
		makeControls(c);
	}
	else {
		createCookie("fontsize",3,"");
		makeControls(3);
	}
	
	/*******
	** vp - go to link functionality - (c) - 0.0.1 **
	*******/
	$('#gobutton').click(function(e) {
		return go(this.previousSibling.previousSibling.value);
	});

	addTracker();
});

function setPrintPopup(ID, page) {
    //var but = $(":asp('" + ID + "')");
    var but = $("*[id$='" + ID + "']")
    if (but.length) {
        but.click(function() {
            var win = window.open(page, "printpopup");
            if (win) {
                return false;
            }
            else {
                return true;
            }
        });
    }
}

function go(strValue) {
    window.location.href = strValue;
    return false;
}

	
function makeControls(c) {
	//draw the "Tekstgrootte" header and the plus and minus buttons
	var divTag = document.getElementById("textsize");	
	var ulTag = document.createElement('ul');
	var liTag = document.createElement('li');
	var liTag1 = document.createElement('li');
	var liTag2 = document.createElement('li');
	var aTag1 = document.createElement('a');
	var aTag2 = document.createElement('a');
	var liText = document.createTextNode('Tekstgrootte');
	var aText1 = document.createTextNode('-');
	var aText2 = document.createTextNode('+');
	
	liTag.appendChild(liText);
	aTag1.appendChild(aText1);
	aTag2.appendChild(aText2);
	
	liTag1.appendChild(aTag1);
	liTag2.appendChild(aTag2);
	ulTag.appendChild(liTag);
	ulTag.appendChild(liTag1);
	ulTag.appendChild(liTag2);
	divTag.appendChild(ulTag);
	
	aTag1.setAttribute("id","smaller");
	aTag2.setAttribute("id","larger");
	aTag1.setAttribute("title","smaller");
	aTag2.setAttribute("title","larger");
	
	if(c > 1) aTag1.setAttribute("href","#textsmaller");
	else aTag1.className = "inactive";
	
	if(c < 5) aTag2.setAttribute("href","#textlarger");
	else aTag2.className = "inactive";
	
	addListener(aTag1,-1);
	addListener(aTag2,1);
}

//the actual resizing (changing of stylesheet)

function resize(value) {
	//calculate the new size
	var c = readCookie("fontsize");
	if(c) {
		//get the number from the cookie and make it an int
		var oldSize = readCookie("fontsize") - 0;
	}
	else {
		//if something went wrong (e.g. IE6, in multipleIEs), at least the current font size is 3
		oldSize = 3;
	}
	//newsize is (oldsize + 1) or (oldsize + -1)
	var newSize = oldSize + value;
	if(0 < newSize && newSize < 6) {
		setFont(newSize);
		//test if we should deactivate a button (to prevent sizes bigger than 5 or smaller than 0) or reactivate one
		if(oldSize == 4 && newSize == 5) removeLink("larger");
		if(oldSize == 2 && newSize == 1) removeLink("smaller");
		if(oldSize == 5 && newSize == 4) addLink("larger",1);
		if(oldSize == 1 && newSize == 2) addLink("smaller",-1);
	}
}

function setFont(fontsize) {
    //get the link tag by id and change the href, update the cookie
    var l = document.getElementById("fontsize");
    l.href = "\/static\/css\/tekstgrootte" + fontsize + ".css";
    createCookie("fontsize", fontsize, "");
}

//functions for the links

function removeLink(id) {
	//deactivate a button to prevent sizes bigger than 5 or smaller than 0
	var el = document.getElementById(id);
	var nr = 1;
	if(id == "smaller") nr = -1;
	el.removeAttribute("href");
	el.className = "inactive";
}

function addLink(id,nr) {
	//activate a button if we need it again
	var el = document.getElementById(id);
	el.setAttribute("href","#text"+id);
	el.className = "";
}

function addListener(tag,param) {
	//add eventListener with the resize function and the correct param
	if (tag.addEventListener){
		tag.addEventListener('click',function(){resize(param);},false); 
	}
	else if (tag.attachEvent){
		tag.attachEvent('onclick',function(){resize(param);});
	}
}

//cookie functions

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
window.onload = function(){
	frameBreaker();
	pdf_downloads();        
	setIdenticalAddress('contentForm');
	setPrintPopup('ZorgverlenerForm_Z00S07Step_btnPrint', "/PrintZorgverlener.aspx");
	setPrintPopup('OrganisatieForm_O00S07Step_btnPrint', "/PrintOrganisatie.aspx");
	setPrintPopup('PasZorgverlenerForm_PZ00S08Step_btnPrint', "/PrintPasZorgverlener.aspx");
	setPrintPopup('MedewerkerpasForm_MP00S07Step_btnPrint', "/PrintMedewerkerPas.aspx");
	setPrintPopup('ServercertificaatForm_SC00S07Step_btnPrint', "/PrintServercertificaat.aspx");
	setPrintPopup('ServercertificaatForm_SC00S07Step_btnOpslaan', "/PrintPdfServercertificaat.aspx");
	setPrintPopup('GBZForm_GBZ00S08Step_btnPrint', "/PrintGBZAanvraag.aspx");
	setPrintPopup('SubsidieForm_SUB00S05Step_btnPrint', "/PrintSubsidieAanvraag.aspx");
	
	setPrintPopup('dashform_DashControleForm_DashControleControleStep_btnVolgende', "/PrintDash.aspx");
	
	
	setPrintButton('ZorgverlenerForm_Z00S00Step_btnPrint');
	setPrintButton('OrganisatieForm_O00S00Step_btnPrint');
	setPrintButton('PasZorgverlenerForm_PZ00S00Step_btnPrint');
	setPrintButton('MedewerkerpasForm_MP00S00Step_btnPrint');
	setPrintButton('ServercertificaatForm_SC00S00Step_btnPrint');
	setPrintButton('OrganisatieForm_Z00S00Step_btnPrint');
	setPrintButton('GBZForm_GBZ00S00Step_btnPrint');
	setPrintButton('SubsidieForm_SUB00S00Step_btnPrint');

}

function frameBreaker(){
	if (top.location!= self.location) {
		top.location = self.location.href
	}
}

function setIdenticalAddress(ID) {
	if (document.getElementById(ID)) {
		var entries = document.getElementById(ID).getElementsByTagName('div');
		for (var i=0; i<entries.length; i++) {
			if (entries[i].className.indexOf('extraFormNav') != -1) {
				var entry = entries[i].getElementsByTagName('input')[0];
				if (entry) {
					entry.onclick = function() {
						var target = document.getElementById(this.title);
						if (target.style.display == 'none') {
							target.style.display = 'block';
						}
						else {
							target.style.display = 'none';
						}
					}
					if (entry.checked) {
						document.getElementById(entry.title).style.display = 'none';	
					}
				}
			}
		}
	}
}




function pdf_downloads(){  //retrieve all links, open a new window when classname is 'document' of 'eformulier'
	if(document.getElementsByTagName('a')){
		var exlinks = document.getElementsByTagName('a');
		for(var i=0 ; i<exlinks.length ; i++){
			if(exlinks[i].className == 'document' || exlinks[i].className == 'eformulier'){
				exlinks[i].onclick = function(){
					window.open(this.href,'');
					return false;
				}
			}
		}
	}
}


function setPrintPopup(ID, page)
{
	if (document.getElementById(ID)) {
		var but = document.getElementById(ID);
		but.onclick = function(){
			var win = window.open(page, "printpopup");
			if(win)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}

function setPrintButton(ID)
{
    var buttonPrint = document.getElementById(ID)
    if (buttonPrint) 
    {
        ShowElement(ID);
        
        buttonPrint.onclick = function(){
			ShowPrintDialog();
			return false;
		}
    }
}


function ShowElement(ID)
{
    if (document.getElementById(ID)) 
    {
        document.getElementById(ID).style.display = 'block';
    }
}

function ShowPrintDialog()
{ 
	if (window.print) {
		window.print() ;  
	} else {
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}

function ClearRadios()
{
    if(document.getElementsByTagName('input'))
    {
        var inputs = document.getElementsByTagName('input');
		for(var i=0 ; i<inputs.length ; i++){
		    if(inputs[i].type == 'radio')
		    {
		        inputs[i].checked = false;
		    }
		}
    
    }

}

