var TELIAPINK = "#ad005b";

function activateTab(tabParent, selectedTabId)
{		
	var tabsNum = (document.getElementById(tabParent).childNodes.length - 2)/2;	// -1 as there is a div for clearing the float
	var spacerTabsNum = tabsNum+1;
	
	// reset all spacerTabs
	for (a=0; a<spacerTabsNum; a++)
	{
		var spacerTab = document.getElementById("e_tabSpacer_" + a);
		spacerTab.className=  "e_tabSpacer";
	}
	
	for (a=0;a<tabsNum;a++)
	{	
		var tab = document.getElementById("Tab" + a);
		var tabContent = document.getElementById("tab_content_" + a);
		
		if (tab != null)
		{	
			var tabClasses = "e_tab";
			
			if (a!=selectedTabId)
			{	
				tab.className = tabClasses;
				
				if (tabContent!=null)
				{
					tabContent.className = "e_hidden";
				}
			} 
			else
			{	
				var spacerLeftClass = "e_tabSpacerLeftSelected";
				var spacerRightClass = "e_tabSpacerRightSelected";
				
				tabClasses = tabClasses + " e_tabSelected";
				var spacerLeft = document.getElementById("e_tabSpacer_" + a);
				var rightIndex = a+1;
				var spacerRight = document.getElementById("e_tabSpacer_" + rightIndex);
				
				if (a==0)
				{
					spacerLeftClass = "e_tabSpacerFirstLeftSelected";					
				}
				
				tab.className = tabClasses;
				spacerLeft.className = spacerLeftClass;
				spacerRight.className = spacerRightClass;
				
				//set content visible
				if (tabContent!=null)
				{
					tabContent.className = "e_visible";
				}
			}		
		}
	}
}

function enable(formElementID)
{
	document.getElementById(formElementID).disabled=false;
	var pbsInputRow = document.getElementById("PBSInputFields");
	
	var giroExpl = document.getElementById("GiroExplanation");
	var pbsExpl = document.getElementById("PBSExplanation");
	
	var regNrLabel = document.getElementById("regNrLabel");	             

	if (regNrLabel != null)
	{
		if (regNrLabel.className == "err")
		{
			regNrLabel.style.color = TELIAPINK;
		}
	}
	var accNrLabel = document.getElementById("accNrLabel");
	if (accNrLabel != null)
	{
		if (regNrLabel.className == "err")
		{
			accNrLabel.style.color = TELIAPINK;
		}
	}
	
	if (pbsInputRow != null)
	{	
		pbsInputRow.style.visibility = "visible";
		pbsInputRow.style.display = "";		
	}
	
	if (giroExpl != null)
	{	
		giroExpl.style.visibility = "hidden";
		giroExpl.style.display = "none";		
	}
	
	if (pbsExpl != null)
	{	
		pbsExpl.style.visibility = "visible";
		pbsExpl.style.display = "";		
	}
}

function disable(formElementID)
{
	var pbsInputRow = document.getElementById("PBSInputFields");
	
	var giroExpl = document.getElementById("GiroExplanation");
	var pbsExpl = document.getElementById("PBSExplanation");
	
	
	var element = document.getElementById(formElementID);
	if (element!=null)
	{
		element.disabled=true;
		element.value = "";
		
		var regNrLabel = document.getElementById("regNrLabel");
		if (regNrLabel != null)
		{
			regNrLabel.style.color = "black";
		}
		regNrLabel = document.getElementById("accNrLabel");
		if (regNrLabel != null)
		{
			regNrLabel.style.color = "black";
		}
	}
	
	if (pbsInputRow != null)
	{	
		pbsInputRow.style.visibility = "hidden";
		pbsInputRow.style.display = "none";
	}
	
	if (pbsExpl != null)
	{	
		pbsExpl.style.visibility = "hidden";
		pbsExpl.style.display = "none";		
	}
	
	if (giroExpl != null)
	{	
		giroExpl.style.visibility = "visible";
		giroExpl.style.display = "";		
	}
}
function initPaymentVisibility()
{	
	var element = document.getElementById("BS");
	if (element!=null)
	{
		if (!element.checked==true)
		{		
			disable('regNumberField');
			disable('accountNumberField');
		}
	}
}

function submitForm()
{
	submitForm("form");
}

function submitForm(formID)
{
	form = document.getElementById(formID);
	
	if (form!=null){		
		form.submit();
	}
}

function refreshProgressImage()
{
	var image = document.getElementById('progressImg');
	if (image != null)
	{
		image.src = "images/creditcardcheck/system_working.gif";
	}		
}

function creditCheck()
{	
	setTimeout("submitForm('userDataForm')",1500);
	setTimeout("refreshProgressImage()",1600);

	$('#progressLayer').click();
	
	//fix for IE where dropdowns are not banned to the background
	document.getElementById("housing").style.visibility = "hidden";
	document.getElementById("profession").style.visibility = "hidden";
	refreshProgressImage();
}

function show(elementID)
{
	var element = document.getElementById(elementID);
	if (element!=null)
	{
		element.style.visibility = "visible";
		element.style.display = "";	
	}
}

function hide(elementID)
{
	var element = document.getElementById(elementID);
	if (element!=null)
	{
		element.style.visibility = "hidden";
		element.style.display = "none";	
	}
}

// This is a workaround for IE where certain form elements stay on top (ignoring z-layer of overlaying elements)
function hideObtrusiveElements(formID)
{
	var form = document.getElementById(formID);
	if (form!=null)
	{
		var elements = form.elements;
	
		for(b=0; b<elements.length;b++)
		{
			if(elements[b].type.search("select[A-Za-z0-9]*")==0)
			{
				elements[b].style.visibility = "hidden";
				elements[b].style.display = "none";	
			}
		}
	}
}

function clickImageButton(imageButtonID)
{
	var button=document.getElementById(imageButtonID);
	button.click();
}

function toggleCartInfos(cartEntries, showSessionCartEntry)
{
	var moreSpan = document.getElementById( "CartRightMoreInfo" );
	var lessSpan = document.getElementById( "CartRightLessInfo" );
	if( moreSpan.style.display == '' )
	{
		moreSpan.style.visibility = 'hidden';
		moreSpan.style.display = 'none';
		lessSpan.style.visibility = 'visible';
		lessSpan.style.display = '';										
	}
	else
	{
		moreSpan.style.visibility = 'visible';
		moreSpan.style.display = '';
		lessSpan.style.visibility = 'hidden';
		lessSpan.style.display = 'none';
	}
	
	var element;
	for(var i=0; i<=cartEntries; i++)
	{	
		element = document.getElementById("entry_"+i);
		if (element!=null)
		{
			if( element.style.visibility == 'hidden' )
			{
				element.style.visibility='visible';
				element.style.display='';
			}
			else
			{
				element.style.visibility='hidden';
				element.style.display='none';
			}
		}
	}	
	
	if (showSessionCartEntry)
	{
		element = document.getElementById( "sessionCartRow" );
		if (element != null)
		{
			if( element.style.visibility == 'hidden' )
			{
				element.style.visibility='visible';
				element.style.display='';
			}
			else
			{
				element.style.visibility='hidden';
				element.style.display='none';
			}
		}
	}

	toggleCartOnServer('<%= request.getSession().getId() %>');
}

function hideAddonService()
{
	var moreSpan = document.getElementById( "AddOnsExpanded" );
	var lessSpan = document.getElementById( "AddOnsFolded" );
	var expanded = document.getElementById( "ExpandedAddOnServices" ).value = "false";
	var partListTable = document.getElementById( "partListTable" );
	
	for(a=0; a<partListTable.rows.length;a++)
	{
		var partListRow = document.getElementById( "partUIElementRow_" + a );
		if (partListRow != null)
		{			
			partListRow.style.visibility = 'hidden';
			partListRow.style.display = 'none';
			moreSpan.style.visibility = 'visible';
			moreSpan.style.display = '';
			lessSpan.style.visibility = 'hidden';
			lessSpan.style.display = 'none';					
		}
	}
}
 
function setAddonServices()
{
	var moreSpan = document.getElementById( "AddOnsExpanded" );
	var lessSpan = document.getElementById( "AddOnsFolded" );
	var expanded = document.getElementById( "ExpandedAddOnServices" ).value;
	var partListTable = document.getElementById( "partListTable" );
	
	for(a=0; a<partListTable.rows.length;a++)
	{
		var partListRow = document.getElementById( "partUIElementRow_" + a );
		if (partListRow != null)
		{
			if (expanded=="true")
			{
				partListRow.style.visibility = 'visible';
				partListRow.style.display = '';		
				moreSpan.style.visibility = 'hidden';
				moreSpan.style.display = 'none';
				lessSpan.style.visibility = 'visible';
				lessSpan.style.display = '';					
			}
			else
			{
				partListRow.style.visibility = 'hidden';
				partListRow.style.display = 'none';
				moreSpan.style.visibility = 'visible';
				moreSpan.style.display = '';
				lessSpan.style.visibility = 'hidden';
				lessSpan.style.display = 'none';	
			}			
		}
	}
}

function toggleAddonServices()
{
	var moreSpan = document.getElementById( "AddOnsExpanded" );
	var lessSpan = document.getElementById( "AddOnsFolded" );
	var expanded = document.getElementById( "ExpandedAddOnServices" ).value;
	var partListTable = document.getElementById( "partListTable" );
	
	if(expanded=="true")
	{
		document.getElementById( "ExpandedAddOnServices" ).value = "false";
	} 
	else	
	{
		document.getElementById( "ExpandedAddOnServices" ).value = "true";
	}
	
	setAddonServices();
}

function setPriceInfo()
{
	var moreSpan = document.getElementById( "PriceInfoExpanded" );
	var lessSpan = document.getElementById( "PriceInfoFolded" );
	var expanded = document.getElementById( "ExpandedPriceInfo" ).value;
		
	if (expanded=="true")
	{
		moreSpan.style.visibility = 'visible';
		moreSpan.style.display = '';
		lessSpan.style.visibility = 'hidden';
		lessSpan.style.display = 'none';					
	}
	else
	{		
		moreSpan.style.visibility = 'hidden';
		moreSpan.style.display = 'none';
		lessSpan.style.visibility = 'visible';
		lessSpan.style.display = '';
	}			
	
}

function togglePriceInfo()
{
	var expanded = document.getElementById( "ExpandedPriceInfo" ).value;
		
	if(expanded=="true")
	{
		document.getElementById( "ExpandedPriceInfo" ).value = "false";
	} 
	else	
	{
		document.getElementById( "ExpandedPriceInfo" ).value = "true";
	}
	
	setPriceInfo();
}

function hideLongDescription()
{
	var moreSpan = document.getElementById( "longDescriptionExpanded" );
	var lessSpan = document.getElementById( "longDescriptionFolded" );
	var expanded = document.getElementById( "expandedLongDescription" ).value = "false";
	var longDescriptionContent = document.getElementById( "longDescriptionContent" );

	longDescriptionContent.style.visibility = 'hidden';
	longDescriptionContent.style.display = 'none';
	moreSpan.style.visibility = 'visible';
	moreSpan.style.display = '';
	lessSpan.style.visibility = 'hidden';
	lessSpan.style.display = 'none';	
}

function setLongDescription()
{
	var moreSpan = document.getElementById( "longDescriptionExpanded" );
	var lessSpan = document.getElementById( "longDescriptionFolded" );
	var expanded = document.getElementById( "expandedLongDescription" ).value;
	var longDescriptionContent = document.getElementById( "longDescriptionContent" );
		
	if (expanded=="true")
	{
		longDescriptionContent.style.visibility = 'visible';
		longDescriptionContent.style.display = '';		
		moreSpan.style.visibility = 'hidden';
		moreSpan.style.display = 'none';
		lessSpan.style.visibility = 'visible';
		lessSpan.style.display = '';					
	}
	else
	{
		longDescriptionContent.style.visibility = 'hidden';
		longDescriptionContent.style.display = 'none';
		moreSpan.style.visibility = 'visible';
		moreSpan.style.display = '';
		lessSpan.style.visibility = 'hidden';
		lessSpan.style.display = 'none';	
	}			
	
}

function toggleLongDescription()
{
	var expanded = document.getElementById( "expandedLongDescription" ).value;
		
	if(expanded=="true")
	{
		document.getElementById( "expandedLongDescription" ).value = "false";
	} 
	else	
	{
		document.getElementById( "expandedLongDescription" ).value = "true";
	}
	
	setLongDescription();
}

function toggleVisibility(code)
{
	var contentFolded = document.getElementById(code + "cf");
	var contentExpanded = document.getElementById(code + "ce");
	var togglerFolded = document.getElementById(code + "tf" );
	var togglerExpanded = document.getElementById(code +"te" );
	
	var currentStyleOfExpandedContent = contentExpanded.style.visibility; 
		
	if(currentStyleOfExpandedContent=="hidden")
	{
		contentExpanded.style.visibility = 'visible';
		contentExpanded.style.display = '';
		if (contentFolded!=null)
		{
			contentFolded.style.visibility = 'hidden';
			contentFolded.style.display = 'none';	
		}
		
		if (togglerExpanded!=null)
		{
			togglerExpanded.style.visibility = 'visible';
			togglerExpanded.style.display = '';				
		}
		
		if (togglerFolded!=null)
		{
			togglerFolded.style.visibility = 'hidden';
			togglerFolded.style.display = 'none';
		}		
	} 
	else	
	{			
		contentExpanded.style.visibility = 'hidden';
		contentExpanded.style.display = 'none';
		
		if (contentFolded!=null)
		{
			contentFolded.style.visibility = 'visible';
			contentFolded.style.display = '';	
		}
		
		if (togglerExpanded!=null)
		{
			togglerExpanded.style.visibility = 'hidden';
			togglerExpanded.style.display = 'none';		
		}
		
		if (togglerFolded!=null)
		{
			togglerFolded.style.visibility = 'visible';
			togglerFolded.style.display = '';
		}		
	}
}

/*==========================*/
/*	Used for modal dialogs	*/
/*==========================*/
function setParagraph(paragraphID)
{
	var element = document.getElementById('paragraph');
	if (element!=null)
	{
		element.value=paragraphID;
	}
}

/*==================*/
/*	Popup window	*/
/*==================*/
function openUrl(url, windowName, windowParamz)
{
	var newWindow = window.open(url, windowName, windowParamz);
	newWindow.focus();
}

function openUrlCentered(url, width, height)
{
	var screenWidth = Math.max(screen.width-width, 0);
	var screenHeight = Math.max(screen.height-height-100, 0);
	var left = screenWidth/2;
	var top = screenHeight/2;
	var windowParams = 'width=' + width + ', height=' + height + ', left=' + left + ', top=' + top; 
	openUrl(url, '_blank', windowParams);
}

/*==========================*/
/*	Popup window for images */
/*==========================*/
function popupImageInNewWindow(url, title, windowparams, contentWidth, padding)
{
	var id = new Date().getMilliseconds();
	var w = window.open('',id,windowparams);	
	w.document.write('<html><head><title>' + title + '</title>');
	w.document.write('</head>');	
	w.document.write('<body style="margin: 0">');
	
	w.document.write('<table cellpadding="0" cellspacing="0">');
	w.document.write('<tr>');
	w.document.write('<td style="width: ' + contentWidth + 'px; text-align: center; padding: ' + padding + '">');
	
	if( document.all )
	{
		//only for ie
		 w.document.write('<div class="PngIMGRepDiv" style="width: 1px; height: 1px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader( src=' + url + ' ); cursor: pointer;" onclick="window.close()"></div>');
	}
	else
	{
		//standard for ff etc.
		w.document.write('<img src="' + url + '" id="image" onclick="window.close()"  />');		
	}
	
	w.document.write('</td>');
	w.document.write('</tr>');
	w.document.write('</table>');
	w.document.write('</body>');
	w.document.write('</html>');
	w.document.close();	
}

/*==========================*/
/*	Popup window for swf */
/*==========================*/
function popupFlashInNewWindow(mediaURL, title, windowparams, contentWidth, padding, codeBase, width, height, pluginsPage, bgColor )
{
	var id = new Date().getMilliseconds();
	var w = window.open('',id,windowparams);	
	w.document.write('<html><head><title>' + title + '</title>');
	w.document.write('</head>');	
	w.document.write('<body style="margin: 0">');
	
	w.document.write('<table cellpadding="0" cellspacing="0">');
	w.document.write('<tr>');
	w.document.write('<td style="width: ' + contentWidth + 'px; text-align: center; padding: ' + padding + '">');
	
	if (height=='0' || width=='0')
	{
		w.document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+codeBase+'">');
	}
	else
	{
		w.document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+codeBase+'" height="'+height+'" width="'+width+'">');	
	}
	w.document.write('<param name="movie" value="' + mediaURL + '"></param>');
	w.document.write('<param name="quality" value="high"></param>');
	w.document.write('<param name="bgcolor" value="'+bgColor+'"></param>');
	w.document.write('<param name="scale" value="showall"></param>');	
	if (height=='0' || width=='0')
	{	
		w.document.write('<embed src="' + mediaURL + '" quality="high" pluginspage="'+pluginsPage+'" type="application/x-shockwave-flash" scale="scale" bgcolor="'+bgColor+'"></embed>');
	}
	else
	{
		w.document.write('<embed src="' + mediaURL + '" quality="high" pluginspage="'+pluginsPage+'" type="application/x-shockwave-flash" scale="noscale" bgcolor="'+bgColor+'" height="'+height+'" width="'+width+'"></embed>');	
	}	
	w.document.write('</object>');
	
	w.document.write('</td>');
	w.document.write('</tr>');
	w.document.write('</table>');
	w.document.write('</body>');
	w.document.write('</html>');
	w.document.close();	
}

/*==========================*/
/*	Display of swf          */
/*==========================*/
function renderFlash(mediaURL, scale, width, height, codeBase, pluginsPage, bgColor, classId)
{
	document.write('<object classid="' + classId + '" codebase="'+codeBase+'" width="'+width+'" height="'+height+'">');
	document.write('<param name="movie" value="' + mediaURL + '"></param>');
	document.write('<param name="quality" value="high"></param>');
	document.write('<param name="bgcolor" value="'+bgColor+'"></param>');
	document.write('<param name="scale" value="' + scale + '"></param>');
	document.write('<param name="align" value="t"></param>');
	document.write('<param name="salign" value="t"></param>');
	document.write('<param name="wmode" value="opaque"></param>');
	document.write('<embed src="' + mediaURL + '" quality="high" pluginspage="'+pluginsPage+'" type="application/x-shockwave-flash" scale="' + scale + '" bgcolor="'+bgColor+'" width="'+width+'" height="'+height+'" align="t" salign="t" wmode="opaque"></embed>');
	document.write('</object>');		
}

/*==========================*/
/* Validation input fields  */
/*==========================*/
function isNumberKey(evt)
{	
	var isCtrC = isCTRL(evt);
	if (isCtrC=='ttt')
	{
		return true;
	}

	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
		
	return true;
}

function isCopyPasteKey(evt)
{	
	var isCtrC = isCTRL(evt);	
	if (isCtrC=='ttt')
	{
		return true;
	}	
	return false;
}

function enterPressedCheck(e)
{
	var key;
	if ( isNumberKey(e) )
	{
		if (!e)
		{
			e = window.event;
		}
		if (e.which)
		{
			key = e.which;
		}
		else if (e.keyCode)
		{
			key = e.keyCode;
		}
		if (key==13)
		{
			var enterKey = document.getElementById("enterKeyPressed");
			if (enterKey!=null)
			{
				enterKey.value = "true";
			}			
		}		
	}
	else
	{
		return false;
	}
}

function checkFocusNextElement(element)
{
	if (element.value.length==element.maxLength)
	{
		var currentIndex = element.tabIndex;
		var theForm = element.form;
		if (theForm!=undefined)
		{
			for (var i=0; i < theForm.elements.length; i++) {
				var nextElement = theForm.elements[i];
				if (nextElement.tabIndex==currentIndex+1)
				{
					nextElement.focus();
				}
			}
		}
	}
}

/* works only for IE*/
function isCTRL(e)
{
	var code = (document.all) ? event.keyCode : e.which;
	var ctrl = (document.all) ? event.ctrlKey : e.modifiers & Event.CONTROL_MASK;

	if (document.all)
	{
		if (ctrl && code==86) //CTRL+V
		{
			return 'ttt';
		}
		else if (ctrl && code==67) //CTRL+C (Copy)
		{	
			return 'ttt';
		}
	}
	else 
	{
		if (ctrl==2) //CTRL key
		{		
			return 'ttt';
		}
	}
	return 'fff';
}

/* does not work in FF, just works in IE with alert, when control loose the focus*/
var popup = "Sorry, right click is disabled.";
function noway(go)
{	
	if(document.all)
	{
		if (event.button == 2)
		{
			return false;
		}
	}
	if (document.layers) 
	{
		if (go.which == 3)
		{
			return false;
		}
	}
}
if (document.layers) 
{
	document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=noway;

function init()
{	
	$('.offer_option').click(function(event)
	{
		var selectedCol = ($(this).hasClass("left"))? "left" : (($(this).hasClass("right"))? "right" : "");
		
		var theOptionTable = $(this).parents("table:first");
		theOptionTable.attr("class", "e_offer_option_" + selectedCol);		
	});
	
	$('.showDialog').click(function(event)
	{
		event.preventDefault();
		var wrapperDiv = $(this).parent();
		var dialogContent = wrapperDiv.children('.dialogContent');
		
		var dialogClasses = dialogContent.attr('class');
		var width = dialogContent.css('width')=='auto'? 300 : dialogContent.css('width');
		var hiddenFooter = (dialogClasses.indexOf('hiddenFooter')>-1)? true : false;
		var hiddenTitleBar = (dialogClasses.indexOf('hiddenTitleBar')>-1)? true : false;
		
		if (wrapperDiv.is('.pageEnterDialog'))
		{
			wrapperDiv.children('.showDialog').unbind('click');
		}
		
		if (!(dialogClasses.indexOf('e_brightGray')>-1))
		{
			dialogClasses += ' e_pink_box';
		}
					
		var content = $('<div />');
		content.html(dialogContent.html());
		
		$dialog = content.dialog({modal:true,
			autoOpen:false,
			resizable:false,
			title:"",
			width:width,
			close: function(ev, ui) { $(this).remove(); }
			});
		
		$dialog.dialog('option', 'dialogClass', dialogClasses);
		
		$('.closeButton').click(function(event) {
			$dialog.dialog("close");
			$dialog.remove();
			return false;
		})
		
		if (hiddenTitleBar)
		{
			$('.ui-dialog-titlebar.ui-widget-header').remove();
		}
		
		if (!hiddenFooter)
		{
			uiDialogFooter = (this.uiDialogFooter = $('<div style="text-align: center; padding: 15px 0 0;"></div>'))
			.appendTo($dialog),
			
			uiDialogFooterClose = $('<a href="#"><img src="images/buttons/close.png" /></a>')
				.attr('role', 'button')
				.mousedown(function(ev) {
				ev.stopPropagation();
			})
			.click(function(event) {
				$dialog.dialog("close");
				$dialog.remove();
				return false;
			})
			.appendTo(uiDialogFooter)
		}
		
		$dialog.dialog("open");
		
	});
		
	$('.hourglassPart').live("click", function(e)
	{
		$(this).closest("div.partContainer").prepend('<div style="position: absolute; width: 665px; text-align: center;"><img src="images/hourglass.gif" style="margin-top: 15px" /></div>');
	});
	
	$(document).ready(function()
	{
		$('.pageEnterDialog').children('.showDialog').click();
		$('.defaultOffer').click();
	});
}

$(document).ready(function(){
	init();	
});