/*
--------------------------------------------------------------------------------
- Handige Algemene Functies
-
--------------------------------------------------------------------------------
*/

/*Redirection zonder alert if true*/
allowredirect = false;

// Inklappen en uitklappen van rijen die bij een kop horen
function ToggleNextRows(node, iRows, bScrollIntoView)
{
   // Doorloop de x volgende rijen en toggle deze...

   // De positieve variant pakt steeds de volgende...
   if (iRows > 0)
   {
      // Kijk of de onderliggende rijen al worden weergegeven of niet
      newStyle = (node.nextSibling.style.display == 'none') ? '' : 'none';

      sibling = node;
      for (i=1; i<=iRows; i++)
      {
           sibling = sibling.nextSibling;
           sibling.style.display = newStyle;
      }
      
      if ( (newStyle == '') && (bScrollIntoView) ) node.scrollIntoView( bScrollIntoView );
   }
   // De negatieve variant pakt steeds de vorige...
   else if (iRows < 0)
   {
      // Kijk of de onderliggende rijen al worden weergegeven of niet
      newStyle = (node.previousSibling.style.display == "none") ? "" : "none";

             iRows = -iRows;

      sibling = node.previousSibling;
      for (i=1; i<=iRows; i++)
      {
           sibling.style.display = newStyle;
           sibling = sibling.previousSibling;
      }
   }
}


// Handige helper functie om rows van de ene tabel naar de andere te verplaatsen
function MoveTableRow( sSourceTable, iSourceRow, sDestinationTable, iDestinationRow )
{
	// Zoek enkele elementen bij elkaar
	oSource = document.getElementById( sSourceTable );
	oTable = document.getElementById( sDestinationTable );
	
	if ( (oSource == null) || (oTable == null) )
	{
		return;
	}
	
	// De rows zitten binnen een tbody tag
	oRows = oSource.childNodes[0];


	// Verwijder de rij in de bron-tabel
	oRow = oRows.removeChild( oRows.childNodes[iSourceRow] );

	// en voeg 'm toe bij de doel-tabel
	oDestinedRow = oTable.insertRow(iDestinationRow);

	while (oRow.children.length > 0)
	{
		oDestinedRow.appendChild( oRow.childNodes[0] );
	}
}

var oPopup;
function InitPopup()
{
	oPopup = window.createPopup();
}
document.attachEvent('onreadystatechange', InitPopup);


function popupSource( sRequest, iWidth, iHeight)
{
    var objHTTP = new ActiveXObject('Microsoft.XMLHTTP');
    
    /* Haal de pagina op (,forceer een refresh door een random parameter) 
			en zet de source in een popup! */
    objHTTP.Open('GET', sRequest + '&random=' + Math.random(), false);
    objHTTP.Send();

    oPopup.document.body.innerHTML = '<DIV class="Popup">' + objHTTP.ResponseText + '</DIV>';
    if (iWidth == null) { iWidth = 1000; };
    if (iHeight == null) { iHeight = 500; };
    oPopup.show(100, 50, iWidth, iHeight);
}

function popupString( sHTMLString, sContext, iWidth, iHeight)
{
    oPopup.document.body.innerHTML = 
				'<HTML><BODY>' +
				'<LINK href="' + sContext + 'LabTrain.css" type="text/css" rel="stylesheet">' +
				'<DIV class="Popup">' + sHTMLString + '</DIV>' +
				'</BODY></HTML>';
    if (iWidth == null) { iWidth = 1000; };
    if (iHeight == null) { iHeight = 500; };
	oPopup.show(100, 50, iWidth, iHeight);
}


/* Deze functie maakt een nieuw venster aan en opent daar sLink */
function popupLink( sLink, sTarget, sStyle )
{
	if (sTarget == null)
	{
		sTarget = '_new';
	};
	
	if (sStyle == null)
	{
		sStyle = 'scrollbars=yes,resizable=yes';
	};
	
	popup = window.open(sLink, sTarget, sStyle)
}



/* --- Functies van de LabTrain ImageButton --- */
function imgClick( pImageTag )
{   
   if (pImageTag.className == 'imagebutton_disabled')
   {
      event.returnValue = false;
   }
   else
   {
		allowredirect = true;
   }
}

function imgButtonEnabled( sID )
{
	if ( document.getElementById( sID ).className == 'imagebutton_disabled' )
	{
	   return false;
	}
	else
    {
       return true;
    }
}


function JumpToTextbox( sID )
{
	tb = document.getElementById(sID);
	tb.focus();
	tb.createTextRange().select();
}

function getKey() 
{
	if (event.keyCode == 27)
	{
		parent.window.focus();
	}
}
document.attachEvent('onkeypress', getKey);


function CheckForm( sErrorPanel )
{
	sErrors = '';
	pErrorPanel = document.getElementById( sErrorPanel );
	if (pErrorPanel != null)
	{
		lstErrors = pErrorPanel.children;
		for (i=0; i < lstErrors.length; i++)
		{	
			if ( lstErrors[i].isvalid.toString() != 'true')
			{
				sErrors += '* ';
				if (lstErrors[i].id.indexOf('RequiredFieldValidator') == 0)
				{
					sErrors += 'Verplicht veld niet ingevuld: ';
				}
				sErrors += lstErrors[i].innerHTML + "\n";			
				
			}
		}
	}
	
	if (sErrors != '')
	{
		alert(sErrors);
		return false;
	}

	return true;
}


/* BEGIN Back knop disabling */

// Een backdoor om toch zonder postback te kunnen navigeren,
// deze allowredirect true maken indien er vrij genavigeerd mag worden

//window.onunload = ReplaceUrl;
function ReplaceUrl ()
{		
	// Indien we in een Modaldialog zitten of er nog geen history is
	// of we hebben aangegeven dat het nu even wel mag
	// of we sluiten het scherm dan hebben we geen problemen met een unload
	// en verlaten we deze functie.
	if ((window.history.length == 0) 
		|| allowredirect 
		|| Closing())
	{
		return;
	}
	
	// Indien we geen postback hebben navigeren we op een manier ongebruikelijke aan
	// LabTrain en moet het dus wel een Refresh of Backspace zijn. Foei! ;)
	if (iPostBacks == 0)
	{			
		// Problemen als mensen hardnekkig de backspace blijven gebruiken in het dagelijks gebruik.
		// en als men vanaf de site wil weg navigeren. dmv addressbar of favorieten.
		alert('Voor een juiste werking van LabTrain wordt het gebruik van de toetsen: backspace en F5(vernieuwen) afgeraden.');
		document.location = window.location;
	}
}

// Deze functie geeft aan of het scherm waarin we zijn gaat sluiten
function Closing()
{
	if(window.screenLeft < 10004)
	{
		return false;
	}
	else
	{
		return true;
	}
}
/* END Back knop disabling */


/* Zorg ervoor dat na een form-submit niet NOG een form-submit kan komen.
		In ASP.NET 2.0 is hiervoor een speciale overridable functie voor! */
iPostBacks = 0;

// Vervang doPostBack door onze eigen functie....
function BlockGUI(eventTarget, eventArgument)
{
	if (iPostBacks == 0)
	{
		__oldDoPostBack (eventTarget, eventArgument);
	}
	iPostBacks++;
}

try
{
	__oldDoPostBack = __doPostBack;
	__doPostBack = BlockGUI;
}
catch (er)
{}



/* Bovendien een aantal internet explorer acties die niet toegestaan zijn, afvangen */
function CancelThisEvent()
{
	return false;
}
//document.oncontextmenu = CancelThisEvent;
document.onstop = ResetPostback;

function ResetPostback()
{
	iPostBacks = 0;
}

function LabTrainHelp()
{
	ShowHelp();
	
	return false;
}
window.onhelp = LabTrainHelp;


/*function TryToLeavePage()
{
	if (iPostBacks == 0)
	{
		__doPostBack('_lbRefresh','');
	}
}
window.onunload = TryToLeavePage;*/


/* Deze functie is alleen bedoeld voor bij klanten 
Lelijke javascript errors worden zo niet getoond.
*/
function handleError() {
	//return true;
}

function CopyToClipBoard(text)
{
	oEdit = doc.getElementById('Header_tbRoundTripID');
	oEdit.innerText = text;
	Copied = oEdit.createTextRange();
	Copied.execCommand("Copy");
}