// Initialize variables
var submitCount = 0;



/// Functions for BlueButton stuff

function blueButtonsOnload()
{
   // Find all buttons with class BlueButton, and apply click events to them.
   var blueButtonElements = getElementsByClassName( document, '*', 'BlueButton' );

   for( i=0; i<blueButtonElements.length; i++ )
   {
     blueButtonElements[ i ].style.cursor = 'pointer';
     blueButtonElements[ i ].onclick = function()
     {
       performActionOnParentTag( this, 'FORM', 'submit' );
     }
   }
}

function performActionOnParentTag( objElement, strTagName, strAction )
{
   if ( objElement != null )
   {
     if ( objElement.parentNode != null && 
         objElement.parentNode.tagName == strTagName )
     {
       switch( strAction )
       {
         case 'submit':
            objElement.parentNode.submit();
            break;
       }
     }
     else
     {
       performActionOnParentTag( objElement.parentNode, strTagName, strAction );
     }
   }
}



/// Functions for Google skyscraper positioning

function googleSkyscraperOnload()
{
   // Positioning of Google Skyscraper
   var eWrapper = document.getElementById( 'Wrapper' );
   var wrapperLeftPosition = getElementLeft( 'Wrapper' );
   var eSkyscraper = document.getElementById( 'GoogleAdmanagerSkyscraper' );
   var skyscraperLeftPosition = wrapperLeftPosition + eWrapper.clientWidth;

   if ( skyscraperLeftPosition + 120 <= getViewportWidth() )
   {
     eSkyscraper.className = '';
     ShowSkyscraper();
     eSkyscraper.className = '';
   }
}


// Track resizes
window.onresize = function()
{

   var eSkyscraper = document.getElementById( 'GoogleAdmanagerSkyscraper' );
   if( eSkyscraper != null )
   {
      eSkyscraper.style.left = getElementLeft( 'Wrapper' ) + 'px';
      ShowSkyscraper();
   }
}

// Show skyscraper
function ShowSkyscraper()
{

   var eWrapper = document.getElementById( 'Wrapper' );
   var eSkyscraper = document.getElementById( 'GoogleAdmanagerSkyscraper' );
   if( eSkyscraper != null )
   {
      var wrapperLeftPosition = getElementLeft( 'Wrapper' );
      var skyscraperLeftPosition = wrapperLeftPosition + eWrapper.clientWidth;
      eSkyscraper.style.left = ( skyscraperLeftPosition + 3 ) + 'px';
   }
}

// Get element left
function getElementLeft( elementId )
{

   var element;
   if( document.getElementById )
   {
      var element = document.getElementById( elementId );
   }
   else if ( document.all )
   {
      var element = document.all[ elementId ];
   }
   
   xPos = element.offsetLeft;
   parentElement = element.parentNode;
   while ( parentElement.tagName != "BODY" && parentElement != null )
   {
      xPos += parentElement.offsetLeft;
      parentElement = parentElement.parentNode;
   }

   return xPos;
}

// Get viewport width
function getViewportWidth()
{
   if ( window.innerWidth != window.undefined )
      return window.innerWidth;

   if ( document.compatMode == 'CSS1Compat' )
      return document.documentElement.clientWidth;

   if ( document.body )
      return document.body.clientWidth; 

   return window.undefined; 
}



/// Whitespace functions

// whitespace characters
var WhitespaceChars = " \t\n\r";

// Strip whitespace from the left
function TrimLeft( s )
{
	// Search through string's characters one by one.
	var returnString = "";
   var length = s.length;
	var c;
   for( var i = 0; i < length; i++ )
   {   
     // Check that current character isn't whitespace.
     c = s.charAt( i );
     if ( WhitespaceChars.indexOf( c ) == -1 ) 
		{
			returnString = s.substring( i, s.length - i );
			break;
		}			
   }
   
   return returnString;
}

// Strip whitespace from the right
function TrimRight( s )
{
	// Search through string's characters one by one.
	var returnString = "";
	var c;
   for( var i = s.length; i >= 0 ; i-- )
   {   
     // Check that current character isn't whitespace.
     c = s.charAt( i );
     if ( WhitespaceChars.indexOf( c ) != -1 ) 
		{
			returnString = s.substring( 0, i );
			break;
		}			
   }
   
   return returnString;
}

// Strip Whitespaces around
function Trim( s )
{
	return TrimRight( TrimLeft( s ) );
}



// GetElementsByClassName
function getElementsByClassName( oElm, strTagName, strClassName ){
   var arrElements = ( strTagName == "*" && oElm.all )? oElm.all : oElm.getElementsByTagName( strTagName );
   var arrReturnElements = new Array();
   strClassName = strClassName.replace( /\-/g, "\\-" );
   var oRegExp = new RegExp( "(^|\\s)" + strClassName + "(\\s|$)" );
   var oElement;

   for( var i=0; i<arrElements.length; i++ )
   {
     oElement = arrElements[i];     
     if ( oRegExp.test( oElement.className ) )
     {
         arrReturnElements.push( oElement );
     }   
   }
   return ( arrReturnElements )
}



/// Bladeren popup
var eBladPopup;
function openBladWindow( pLocation )
{
   if ( eBladPopup != null )
   {
      if ( !eBladPopup.closed )
         eBladPopup.focus();
   }

   eBladPopup = window.open( pLocation, 
      "bladPopup", 
      "top=" + ((screen.availHeight/2) - (678 / 2)) + 
      ",left=" + ((screen.availWidth/2) - (955 / 2)) + 
      ",width=955,height=678,resizable=0,toolbar=0,scrollbars=0,location=0,status=0,menubar=0" );

   return void(0);
}

var newPopup = null;
function openPopup( pLocation, pHeight, pWidth )
{
   if ( pHeight == null )
     var pHeight = 550;

   if ( pWidth == null )
     var pWidth = 500;

   if ( newPopup && !newPopup.closed )
     newPopup.focus();

   else
     newPopup = window.open( pLocation, "popup", 
                "top=" + ( ( screen.availHeight / 2 ) - ( pHeight / 2 ) ) + 
                ",left=" + ( ( screen.availWidth / 2 ) - ( pWidth / 2 ) ) + 
                ",width="+pWidth+",height="+pHeight+",resizable=0,toolbar=0," +
                "scrollbars=1,location=0,status=0,menubar=0" );

   return void(0);
}



/// Image Popup


function ImagePopup( image, label ){
   imageObject = new Image();
   imageObject.src = (image);
   CheckImage( image, label );
}

function CheckImage( image, label ){
   if( imageObject.width != 0 && imageObject.height != 0 )
   {
     OpenImagePopup(image,label);
   }
   else
   {
     command = "CheckImage( '" + image + "', '" + label + "' )";
     interval = setTimeout( command, 20 );
   }
}

function OpenImagePopup( image, label )
{

   var imageWidth = imageObject.width + 4;
   var imageHeight = imageObject.height + 4;

   if( !label )
     label = image;

   var imagePopupParameters = "width=" + imageWidth + ",height = " + imageHeight;
   imagePopupParameters += ",left=" + ( ( screen.width - imageWidth ) / 2 );
   imagePopupParameters += ",top=" + ( ( screen.height - imageHeight ) / 2 );
   // imagePopupParameters += ",scrollbars=1";

   newwindow = window.open( "", "imagePopupWindow", imagePopupParameters );

   newwindow.document.clear();
   newwindow.document.write("<html>\n\t<head>\n\t\t<title>" + label + "</title>\n");
   newwindow.document.write("\t\t<meta http-equiv=\"imagetoolbar\" content=\"no\">\n");
   newwindow.document.write("\t</head>\n\n\t<body style=\"margin:0;padding:0;\" onBlur=\"window.close();\"' scroll=\"auto\">\n"); 
   newwindow.document.write("\t\t<img src=\"" + image + "\" border=\"0\" onclick=\"javascript:window.close()\">\n"); 
   newwindow.document.write("\t</body>\n</html>\n"); 

   if ( newwindow.document.focus )
     newwindow.document.focus();

   newwindow.document.close();

}



// Mijn Elektuur form

function usernameOnload( username, defaultText)
{
   var usernameField = document.getElementById( 'username' );
   
   if ( usernameField != null )
   {
     if ( username == "" )
       username = defaultText;
     if ( usernameField.value == "" )
     {
       usernameField.value = username;

       usernameField.onfocus = function(){
         if ( this.value == defaultText )
            this.value = '';}

       usernameField.onblur = function()
       {
         if ( this.value == '' )
            this.value = username;
       }
     }
   }
}



// Misc form functions

function SetFormAction( sFormId, sFormAction )
{
   var eForm = document.getElementById( sFormId );
   var sCurrentFormAction = eForm.action;
   var sNewFormAction = "";
   
   // Strip current "(completed)" part from action
   var sCurrentFormActionSplit = sCurrentFormAction.split( '-(' );
   if ( sCurrentFormActionSplit.length > 1 )
      sCurrentFormActionSplit[ sCurrentFormActionSplit.length - 1 ] = sCurrentFormActionSplit[ sCurrentFormActionSplit.length - 1 ].split( ')' )[ 1 ];

   for ( var i=0; i<sCurrentFormActionSplit.length; i++ )
   {
      if ( i == sCurrentFormActionSplit.length - 1 )
         sNewFormAction += sCurrentFormActionSplit[ i ];
      else
         sNewFormAction += "-(" + sCurrentFormActionSplit[ i ];
   }
   if ( sNewFormAction.substring( 0, 2 ) == "-(" )
      sNewFormAction = sNewFormAction.substring( 2 );
   
   // Add new part to action
   var sNewFormActionSplit = sNewFormAction.split( '.' );
   sNewFormAction = "";
   for ( var i=0; i<sNewFormActionSplit.length; i++ )
   {
      sNewFormAction += "." + sNewFormActionSplit[ i ];
      if ( i == sNewFormActionSplit.length - 3 )
         sNewFormAction += "-(" + sFormAction.replace( / /g, '-' ) + ")";
   }
   sNewFormAction = sNewFormAction.substring( 1 );
   
   eForm.action = sNewFormAction;
}