// Athlon specific javascripts



document.showElement = function (id) {
	if (document.getElementById(id).style.display == 'none') {
		document.getElementById(id).style.display='inline';
	}
	else {
		document.getElementById(id).style.display='none';
	}
}


document.getElementsByClassName = function (c) {

	var r = new Array()
	var j = 0
	var o = document.getElementsByTagName("*")

	for(i=0;i<o.length;i++)
	{
	if(o[i].className == c) {
	r[j]=o[i];j++;
	}
	}
	return r;
} 

//function adds 1px to the height of a element to overcome the rounded corner misplacement in IE6

document.fixCorners = function () {

	var CornerClass=new Array("a:contentblock","a:policy");
	
	for (y=0;y<CornerClass.length;y++) {
		var obj = document.getElementsByClassName(CornerClass[y].substring(2,CornerClass[y].length))
		if (obj != '') {	
			
			for(i=0;i<obj.length;i++) {
				if (obj[i].offsetHeight % 2 != 0) {
					if(CornerClass[y].substring(0,2) == "a:"){
						obj[i].style.paddingBottom="5px";
					}
					else if (CornerClass[y].substring(0,2) == "b:"){
						obj[i].style.paddingBottom="9px";
					}
				}
			}
		}
	}
	
	$('#content').supersleight({shim: '../skin/images/transparent.gif'});
}

//function to show the zoom image in the product detail page


function DimmedLayer (action,which,content) {

	//detect current scrollposition	and store it in a hidden field upon opening the popup
	if (action == "open") {
		document.getElementById('scrollposition').value=document.body.parentNode.scrollTop;
		if (document.getElementById("smallpdf") != null) {
			document.getElementById("smallpdf").style.visibility="hidden";
		} 
	}	
	
	//define initial variables to be used in the script
	
	var dimmedBox = document.getElementById(which);
	var dimmedBg = document.getElementById('dimmedBg');
	var viewportHeight='';

	//get viewfinder height for all browsers
	
	if (typeof window.innerWidth != 'undefined')
	{
		viewportHeight = window.innerHeight
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		viewportHeight = document.documentElement.clientHeight
	}
	else
	{
		viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
	}


	if (action == "open") {
		document.getElementById("dimmedBox").innerHTML=document.getElementById(content).innerHTML;
		var openIt = true;
		if(openIt){	
			//stretch the dimmed layer vertically and show it
			
			dimmedBg.style.height=document.body.parentNode.scrollHeight+"px";
			dimmedBg.style.display= "block";
			
			//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
			
			dimmedBox.style.marginLeft= "0px";
			
			//center the imagebox on the screen
			
			if ((viewportHeight-dimmedBox.offsetHeight)/2 > 0) {
				dimmedBox.style.marginTop = Math.round((viewportHeight-dimmedBox.offsetHeight)/2)+"px";			
			}
			else {
				dimmedBox.style.marginTop = "20px";
			}
			dimmedBox.style.marginLeft = (document.body.clientWidth-dimmedBox.offsetWidth)/2+"px";
		
			//scroll to the top of page to show the popup
			document.body.parentNode.scrollTop="0";
		}
		dimmedBg.style.height=document.body.parentNode.scrollHeight+"px";
		//dimmedBox.style.marginTop="20px";
		//hide selectboxes in IE6 - known IE bug
		
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) && openIt){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion>=6) {
				var sel = $("select");
				for(s=0;s<sel.length;s++){
					if($("select:eq(" + s + ")").css("visibility") != "hidden") {
						$("select:eq(" + s + ")").addClass("visiblityChanged");
						$("select:eq(" + s + ")").css("visibility", "hidden");
					}
				}
			 }
		}
	
	}
	else {
		dimmedBg.style.display= "none";
		
		//show selectboxes in IE6 - known IE bug
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion>=6) {
				var sel= $("select");
				for(s=0;s<sel.length;s++){
					if($("select:eq(" + s + ")").hasClass("visiblityChanged")) {
						$("select:eq(" + s + ")").removeClass("visiblityChanged");
						$("select:eq(" + s + ")").css("visibility", "inherit");
					}
				}
			 }
		}
		
		//hide the confirmBox using a negative left margin - IE6 won't show the transparent PNG with the display: none/block method
		
		dimmedBox.style.marginLeft= "-10000px";
		//document.body.style.overflow="auto";
		
		// Clear html
		document.getElementById("dimmedBox").innerHTML = "";
		
		//if the last scrollposition was stored in the hidden field scroll to that position
		
		if (document.getElementById('scrollposition').value != '') {
			document.body.parentNode.scrollTop=document.getElementById('scrollposition').value;
		}
		else {
			document.body.parentNode.scrollTop="0";
		}
	
		if (document.getElementById("smallpdf") != null) {
			document.getElementById("smallpdf").style.visibility="visible";
		}
		
	}
}

/**
 * @param callbacks	an associative array, eg. { "buttonId": function() { ... } }
 */
function ModalBox (action,which,content, callbacks) {

	//detect current scrollposition	and store it in a hidden field upon opening the popup
	if (action == "open") {
		document.getElementById('scrollposition').value=document.body.parentNode.scrollTop;
		if (document.getElementById("smallpdf") != null) {
			document.getElementById("smallpdf").style.visibility="hidden";
		} 
	}	
	
	//define initial variables to be used in the script
	
	var dimmedBox = document.getElementById(which);
	var dimmedBg = document.getElementById('modalBg');
	var viewportHeight='';

	//get viewfinder height for all browsers
	
	if (typeof window.innerWidth != 'undefined')
	{
		viewportHeight = window.innerHeight
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		viewportHeight = document.documentElement.clientHeight
	}
	else
	{
		viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
	}

	if (action == "open") {
		document.getElementById("dimmedBox").innerHTML=document.getElementById(content).innerHTML;
		
		for(var buttonId in callbacks) {
			$("#dimmedBox #" + buttonId).unbind("click");
			$("#dimmedBox #" + buttonId).click(callbacks[buttonId]);
		}
		
		var openIt = true;
		if(openIt){	
			//stretch the dimmed layer vertically and show it
			
			dimmedBg.style.height=document.body.parentNode.scrollHeight+"px";
			dimmedBg.style.display= "block";
			
			//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
			
			dimmedBox.style.marginLeft= "0px";
			
			//center the imagebox on the screen
			
			if ((viewportHeight-dimmedBox.offsetHeight)/2 > 0) {
				dimmedBox.style.marginTop = Math.round((viewportHeight-dimmedBox.offsetHeight)/2)+"px";			
			}
			else {
				dimmedBox.style.marginTop = "20px";
			}
			dimmedBox.style.marginLeft = (document.body.clientWidth-dimmedBox.offsetWidth)/2+"px";
		
			//scroll to the top of page to show the popup
			document.body.parentNode.scrollTop="0";
		}
		dimmedBg.style.height=document.body.parentNode.scrollHeight+"px";
		//dimmedBox.style.marginTop="20px";
		//hide selectboxes in IE6 - known IE bug
		
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) && openIt){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion>=6) {
				var sel = $("select");
				for(s=0;s<sel.length;s++){
					if($("select:eq(" + s + ")").css("visibility") != "hidden") {
						$("select:eq(" + s + ")").addClass("visiblityChanged");
						$("select:eq(" + s + ")").css("visibility", "hidden");
					}
				}
			 }
		}
	
	}
	else {
		dimmedBg.style.display= "none";
		
		//show selectboxes in IE6 - known IE bug
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion>=6) {
				var sel= $("select");
				for(s=0;s<sel.length;s++){
					if($("select:eq(" + s + ")").hasClass("visiblityChanged")) {
						$("select:eq(" + s + ")").removeClass("visiblityChanged");
						$("select:eq(" + s + ")").css("visibility", "inherit");
					}
				}
			 }
		}
		
		//hide the confirmBox using a negative left margin - IE6 won't show the transparent PNG with the display: none/block method
		
		dimmedBox.style.marginLeft= "-10000px";
		//document.body.style.overflow="auto";
		
		//if the last scrollposition was stored in the hidden field scroll to that position
		
		if (document.getElementById('scrollposition').value != '') {
			document.body.parentNode.scrollTop=document.getElementById('scrollposition').value;
		}
		else {
			document.body.parentNode.scrollTop="0";
		}
	
		if (document.getElementById("smallpdf") != null) {
			document.getElementById("smallpdf").style.visibility="visible";
		}
	}
}

function showTenderPopup(url) {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	  //Non-IE
	  myWidth = window.innerWidth;
	  myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  myWidth = document.documentElement.clientWidth;
	  myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  //IE 4 compatible
	  myWidth = document.body.clientWidth;
	  myHeight = document.body.clientHeight;
	}

	$("#PDF embed").remove();

	var embedHTML = '<embed id="pdfEmbed" src="' + url + '" type="application/pdf"></embed>';
	$("#PDF").append(embedHTML);

	$("#PDF embed").css("width", myWidth - 40);
	$("#PDF embed").css("height", myHeight - 40);

	DimmedLayer('open','dimmedBox','PDF');
}

function toggleAdvancedSearch(panelId) {
	var panel = $('#' + panelId);
	var anchor = $("#as_link");

	if(panel.is(":visible")) {
		panel.hide();
		anchor.removeClass("arrow_down").addClass("arrow");
	} else {
		panel.show();
		anchor.removeClass("arrow").addClass("arrow_down");
	}
}

function showEmailPopup(tenderId) {
	ModalBox('open','dimmedBox','confirmEmail',
			{ "ok": function() {
					ModalBox('close','dimmedBox','confirmEmail');
	
					var loadUrl = "../tenders/mail.do";					
					var emailTo = $("#emailTo").val();
					var emailBody = $("#emailBody").val();
					var userSessionToken = $("#userSessionToken").val();
					document.body.style.cursor="wait";
					$.post(
						loadUrl,
						{tenderId:tenderId, recipient:emailTo, message:emailBody, userSessionToken:userSessionToken},
						function(responseText){
			                document.body.style.cursor="default";		                
						},
						"json"
					);
				},
			"cancel": function() {
					ModalBox('close','dimmedBox','confirmEmail');
					}
			});
}

/**
The next method switches the tabs when they are clicked.
The styles are modified and the tab that is inactive is hidden.

Also the fields that exist in both tabs are disabled, since otherwise the
values of the inactive tab would be submitted with a post request as well.
This would lead to array values instead of simple values, which will give
problems in the type conversion when populating the backing bean.

For the active / inactive fields to work you need to specify named arrays
with the names 'disabledFields' and 'enabledFields';
These arrays contain the Id's of the elements that need be be enabled / disabled.

There are a few prerequesites to use this method:

Each tab has a name. 
The div to display and hide (with the tab contents) must have the 
same id + Div appended to it.
Each Li inside the tabMenu must have the same id + Li appended to it.

This is an example for your html tab menu:

<div id="tabContainer">
	<ul id="tabnav">
			<li class="${(contract.activeTab == 'existingDriver' ? 'tabActive' : 'tabInActive')}" id="existingDriverLi"><a onclick="javascript: switchToTab('existingDriver');">Bestaande bestuurder</a></li>
			<li class="${(contract.activeTab == 'newDriver' ? 'tabActive' : 'tabInActive')}" id="newDriverLi"><a onclick="javascript: switchToTab('newDriver');">Nieuwe bestuurder</a></li>
	</ul>
	</div>

Your form backing object needs to have an activeTab property in this case.
But you can use a different property if you like.

This is the javascript code that must be added to the page:

var tabMenuStore = {
	allExistingTabs: ['existingDriver', 'newDriver'],
	existingDriver: { 
						enabledFields: ['commentFieldExistingDriver','startDateExistingDriver'],
					  	disabledFields: ['commentFieldNewDriver','startDateNewDriver']
					},
	newDriver: {
						enabledFields: ['commentFieldNewDriver','startDateNewDriver'],
					  	disabledFields: ['commentFieldExistingDriver','startDateExistingDriver']
				}
};

The comment and the startDate fields exist on both tabs, so these fields can
not be active at the same time.

The menuStoreName can be defined when you want to use a different variable name
for the tabMenuStore, or when using multiple tab menus on a single screen.
*/
function switchToTab(tabName, menuStoreName) {
var variableName = 'tabMenuStore'; // default if none specified
if( menuStoreName != null ) {
	variableName = menuStoreName;
}
eval('var tabMenuStoreObj = ' + variableName);
var allTabs = tabMenuStoreObj.allExistingTabs;
// display the correct divs and change styles of tabMenu.
if( allTabs != null ) {
	for( i = 0; i < allTabs.length; ++i ) {
		var liElement = document.getElementById(allTabs[i] + 'Li');
		var divElement = document.getElementById(allTabs[i] + 'Div');
		if( tabName == allTabs[i] ) {
			liElement.className = 'tabActive';
			divElement.style.display = 'block';
		} else {
			liElement.className = 'tabInActive';
			divElement.style.display = 'none';
		}
	}
}

var tabFields = tabMenuStoreObj[tabName];
if( tabFields == null ) {
	return; // no enabled or disabled fields.
}

// enable all fields that are visible on multiple tabs.
var enabledTabFields = tabFields['enabledFields'];
if( enabledTabFields != null ) {
	for( i = 0; i < enabledTabFields.length; ++i ) {
		var fieldElement = document.getElementById(enabledTabFields[i]);
		fieldElement.disabled = false;
	}
}

// disable all fields that are visible on multiple tabs (to avoid array value submit).
var disabledTabFields = tabFields['disabledFields'];
if( disabledTabFields != null ) {
	for( i = 0; i < disabledTabFields.length; ++i ) {
		var fieldElement = document.getElementById(disabledTabFields[i]);
		fieldElement.disabled = true;
	}
}
}

/** Currently disabled because it slows down IE6 a lot. */
function fixIE6() {
	/*
	if(/MSIE 6/i.test(navigator.userAgent)) {	
		IE7.recalc();
	}
	*/
}
