/**
 * pantatype.js
 * Collection of commonly used functions
 * 
 * @author Alex van Niel
 * @version 1.9.7 20090925
 * @copyright (c)2008-2009 Pantamedia BV
 * 
 */

if( typeof(Function.bind) == 'undefined' ){
/**
 * This function was inspired by Prototype.js
 * (c) 2005-2008 Sam Stephenson
 *
 * bind()
 * This function binds an object to a specific function
 * 
 */
	Function.prototype.bind = function() {
	  var __method = this;
	  var args= new Array();
	  for (var i=0; i < arguments.length; i++ ) {
	  	args.push(arguments[i]);
	  }
	  var object = args.shift();
	  return function(){
	  	return __method.apply(object, args);
	  };
	};
}

if( typeof(Array.indexOf) == 'undefined' ){
  Array.prototype.indexOf = function(elt /*, from*/){
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++){
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

if( typeof( String.padding ) == 'undefined' ){
	String.prototype.padding = function(p_sFillString,p_nMaxSize,p_bAlignRight){
		var sResult = new String();
		var sSource = this;
		var sFillString = new String();
		var nMaxSize = 0;
		var nAlign = 0; // 0 = left, 1 = right
	
		if( typeof(p_sFillString) != 'undefined' ) sFillString = new String(p_sFillString);
		if( typeof(p_nMaxSize) != 'undefined' ) nMaxSize = new Number(p_nMaxSize);
		if( (typeof(p_bAlignRight) != 'undefined') && p_bAlignRight ) nAlign = 1;
		
		if( !sFillString.length ){
			sResult = sSource;
		} else if( !nMaxSize || isNaN(nMaxSize) ){
			sResult = sSource;
		} else if( sSource.length >= nMaxSize){
			sResult = sSource;
		} else {
			if( nAlign == 1 ) sResult += sSource;
	
			var nFillLength = Math.floor((p_nMaxSize - sSource.length) / p_sFillString.length);
			for(var nIndex = 0; nIndex < nFillLength; nIndex++){
				sResult += p_sFillString;
			}
	
			if( nAlign == 0 ) sResult += sSource;
		}
	
		return sResult;
	};
}

function submitOnEnter(p_oForm){
	var bResult = true;
	var oEvent = null;
	var nKeyCode = null;
	
	if( typeof(arguments[0]) != 'undefined' ) oEvent = arguments[0];
	
	if( window.event ){
		nKeyCode = window.event.keyCode;
	} else if( oEvent ){
		if( oEvent.which ){
			nKeyCode = oEvent.which;
		} else {
			nKeyCode = oEvent.keyCode;
		}
	}
	
	if( nKeyCode == 13 ){
		p_oForm.submit();
		bResult = false;
	}
	
	return bResult;
}

/*
	Browser Detect Object
	(c)Peter-Paul Koch, adjusted by Alex van Niel

	converted from http://www.quirksmode.org/js/detect.html
	this object originally was immediately available when loaded
	it is converted into an object that has to be called with New

*/
function browserDetect(){
	this.dataBrowser = [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	];

	this.dataOS = [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	];
	
	this.browser = "";
	this.version = "";
	this.OS = "";
	
	this.init();
}

browserDetect.prototype.init = function () {
	this.browser = this.searchString(this.dataBrowser) || "an unknown browser";
	this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "an unknown version";
	this.OS = this.searchString(this.dataOS) || "an unknown OS";
};

browserDetect.prototype.searchString = function (data) {
	for (var i=0;i<data.length;i++)	{
		var dataString = data[i].string;
		var dataProp = data[i].prop;
		this.versionSearchString = data[i].versionSearch || data[i].identity;
		if (dataString) {
			if (dataString.indexOf(data[i].subString) != -1) return data[i].identity;
		} else if (dataProp) {
			return data[i].identity;
		}
	}
};

browserDetect.prototype.searchVersion = function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	if (index == -1) return;
	return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
};

function getQueryValue(p_sKeyName,p_xDefault){
	var xResult = null;
	var nPos = null;
	var sArgName = '';
	var sQueryString = window.location.search.substring(1);
	var aPairs = sQueryString.split("&");
	var sKeyName = p_sKeyName.toLowerCase();

	if( typeof(p_xDefault) != 'undefined' ) xResult = p_xDefault;

	for (var nI = 0; nI < aPairs.length; nI++){
		nPos = aPairs[nI].indexOf('=');
		if (nPos >= 0){
			sArgName = aPairs[nI].substring(0,nPos);
			sArgName = sArgName.toLowerCase(); 
			if( sArgName == sKeyName ){
				xResult = aPairs[nI].substring(nPos+1);;
				break;
			}
		}
	}

	return xResult;
}

/**
 * roundNumber()
 * Attempt at reliably rounding decimals
 * @param {Float}	p_fNumber	Float to be rounded
 * @param {Integer}	p_nLength	Integer indicating how many decimals in the result is needed 
 */
function roundNumber(p_fNumber,p_nLength){
	//make sure the arguments hold numbers, if not then 0
	if(isNaN(p_fNumber)) return 0;

	var nDefaultLength = 10;
	var nMinusOperator = (p_fNumber < 0 ? -1 : 1); //detect if number is negative
	var fNumber = Math.abs(p_fNumber); //convert number to positive if needed
	var nLength = (!isNaN(p_nLength) ? p_nLength : 0);

	//if desired length is smaller then the default length, calculate the difference, otherwise use default
	var nDiffLength = ( (nLength < nDefaultLength) ? nDefaultLength - nLength : nDefaultLength ) + 1;

	//do roundNumber nDiffLength times
	for(var nIndex = 0; nIndex < nDiffLength; nIndex++){
		//round fNumber to (defaultlength - current cycle) and store result in itself

		//fix a bug in javascript rounding characters between 8192 and 10484
		if( (fNumber > 8191)  && (fNumber < 10485) ){
			fNumber = fNumber - 5000;
			var fNumber = Math.round(fNumber * Math.pow(10,nDefaultLength - nIndex))/Math.pow(10,nDefaultLength - nIndex);
			fNumber = fNumber + 5000;
		} else {
			var fNumber = Math.round(fNumber * Math.pow(10,nDefaultLength - nIndex))/Math.pow(10,nDefaultLength - nIndex);
		}

	}

	//restore abs state of number
	fNumber *= nMinusOperator;
	
	return fNumber;
}

function formatPrijs(p_fBedrag,p_sNewSplit){
	var fBedrag = ( !isNaN(parseFloat(p_fBedrag)) ? parseFloat(p_fBedrag) : 0.00 );
	var sMinus = ( fBedrag < 0 ? '-' : '');
	var sResult = new String();
	var sNewSplit = new String();

	fBedrag = roundNumber(fBedrag,2);

	if(p_sNewSplit){
		sNewSplit = p_sNewSplit;
	} else {
		sNewSplit = ",";
	}

	fBedrag = Math.abs(fBedrag);
	fBedrag = parseInt((fBedrag + .005) * 100);
	fBedrag = fBedrag / 100;
	sResult = new String(fBedrag);
	if(sResult.indexOf(".") < 0){
		sResult += ".00";
	}
	if(sResult.indexOf(".") == (sResult.length - 2)){
		sResult += "0";
	}
	sResult = sMinus + sResult;
	sResult = sResult.replace(/\./gi,sNewSplit);

	return sResult;
}

/**
 * AJAX functions below
 */
function createXMLHttpRequest() {
	var oRequest = null;
	try {
		oRequest = new XMLHttpRequest();
	} catch (e) {
		try {
			oRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				oRequest = new ActiveXObject("Msxml3.XMLHTTP");
			} catch (e) {
				try {
					oRequest = new ActiveXObject("MicroSoft.XMLHTTP");
				} catch (e) {
					oRequest = null;
					//alert("Could not create XMLHttpRequest");
				}
			}
		}
	}
	return oRequest;
}

function sendXMLHttpRequest(p_sUrl,p_oOptions,p_sPostData) {
	var oRequest = null;
	var sUrl = p_sUrl;
	var sMethod = (p_sPostData) ? "POST" : "GET" ;
	var sPostData = (typeof(p_sPostData) != 'undefined') ? p_sPostData : null;

	var oOnSuccess = {callback: null, arguments: null};
	var oOnFailure = {callback: null, arguments: null};

	if( typeof(p_oOptions) != 'undefined' ){
		if( typeof(p_oOptions.callbackFunction) != 'undefined' ){	//old style with only success function
			oOnSuccess.callback = p_oOptions.callbackFunction;
			if( typeof(p_oOptions.callbackArguments) != 'undefined' ) oOnSuccess.arguments = p_oOptions.callbackArguments; 
		} else {
			if (typeof(p_oOptions.onsuccess) != 'undefined') {
				if( typeof(p_oOptions.onsuccess.callback) != 'undefined' ) oOnSuccess.callback = p_oOptions.onsuccess.callback;
				if( typeof(p_oOptions.onsuccess.arguments) != 'undefined' ) oOnSuccess.arguments = p_oOptions.onsuccess.arguments;
			}
			if (typeof(p_oOptions.onfailure) != 'undefined') {
				if( typeof(p_oOptions.onfailure.callback) != 'undefined' ) oOnFailure.callback = p_oOptions.onfailure.callback;
				if( typeof(p_oOptions.onfailure.arguments) != 'undefined' ) oOnFailure.arguments = p_oOptions.onfailure.arguments;
			}
		}
	}

	try {
		oRequest = createXMLHttpRequest();

		oRequest.open(sMethod, sUrl, true);

		oRequest.setRequestHeader('User-Agent','XMLHTTP/1.0');

		if( sPostData ){
			oRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		}

		oRequest.onreadystatechange = function () {
				var mxReadyState = oRequest.readyState;
				if( typeof(mxReadyState) == 'string' ) mxReadyState.toLowerCase();
				if( (mxReadyState != 4) && (mxReadyState != 'complete') ) return;

			    var nStatus = getXMLHttpRequestStatus(oRequest);
			    if( !nStatus || (nStatus >= 200 && nStatus < 300) ){
					if( typeof(oOnSuccess.callback) == 'function' ) oOnSuccess.callback(oRequest,oOnSuccess.arguments);
				} else {
					if( typeof(oOnFailure.callback) == 'function' ) oOnFailure.callback(oRequest,oOnFailure.arguments);
				}
			}

		oRequest.send(sPostData);
	} catch(e) {
		//alert("sendXMLHttpRequest - " + e.name + ", " + e.message);
		oRequest = null;
	}
	return oRequest;
}

function updateXMLHttpRequest(p_sUpdateTarget,p_sUrl,p_sPostData){
	var oUpdateFunction = function(p_oRequest){
		var oUpdateTarget = getDOMElementById(p_sUpdateTarget);
		oUpdateTarget.innerHTML = p_oRequest.responseText; 
	}

	oRequest = sendXMLHttpRequest(p_sUrl,{onsuccess: {callback: oUpdateFunction, arguments: null} },p_sPostData);
}

function getXMLHttpRequestStatus( oXMLHttpRequest ){
	try {
		return oXMLHttpRequest.status || 0;
	} catch (e) { return 0 }
}

/**
 * DOM Functions below
 */

/** 
 * 
 * getDOMElementById()
 * Returns an object by it's ID or false if the object was not found
 * 
 * @param {Object} p_oId String or Object pointing to the target element
 * @param {Object} p_oDocument node containing the getElementById function, if none specified, document (root) will be used
 * @return {mixed} Element or false
 */
function getDOMElementById(p_oId,p_oDocument){
	var mixResult = false;
	var oDocument = document;
	
	if( (typeof(p_oDocument) != 'undefined') ){
		oDocument = p_oDocument;
	}
	
	if( oDocument && oDocument.getElementById ){
		switch( typeof(p_oId) ){
			case 'string':
				mixResult = oDocument.getElementById(p_oId);
				break;
			case 'object':
				mixResult = p_oId;
				break;
			default:
				break
		}
	}
	
	return mixResult;
}

function getFormElementByType(p_sFormId,p_sTypeName,p_oDocument){
	var mixResult = false;
	var oDocument = document;
	var oForm = null;
	var nFormCount = 0;
	var aElements = new Array();
	
	if( (typeof(p_oDocument) != 'undefined') ){
		oDocument = p_oDocument;
	}
	
	if( oDocument && oDocument.getElementById ){
		oForm = oDocument.getElementById(p_sFormId);
		nFormCount = oForm.elements.length;
		if( oForm ){
			for(var nIndex = 0; nIndex < nFormCount; nIndex++){
				if( p_sTypeName == oForm.elements[nIndex].type ) aElements.push(oForm.elements[nIndex]);
			}

			if( aElements.length ) mixResult = aElements;
		}
	}
	
	return mixResult;
}

function doNothing(){
	//do nothing
}
/**
 * doFormSubmit()
 * Submits form data and jumps to a specified URL after a successfull submit
 * 
 * @param {String} p_sFormId		String holding the id of the form
 * @param {String} p_sUrl			String with URL used to jump to after a successfull submit
 * @param {Object} p_oValueCheck	Function used to check the values in the form
 * @requires Prototype Library
 * 
 */
function doFormSubmit(p_sFormId, p_sUrl, p_oValueCheck){
	var oElement = getDOMElementById(p_sFormId);

	if (p_oValueCheck() && oElement) {
		oElement.request({
			onSuccess: function(){
				window.location = p_sUrl;
			}
		});
	}
}

function isArray(p_oObject) {
	var bResult = false;

	if( p_oObject && (typeof(p_oObject) == 'object') ){
		bResult = ( (typeof(p_oObject.splice) != 'undefined') && (typeof(p_oObject.join) != 'undefined') );
	}
	
	return bResult;
}

function inArray(p_aHaystack,p_varNeedle,p_bStrict,p_bReturnIndex){
	var bFound = false;
	var bStrict = false;
	var bReturnIndex = false;
	var xResult = false;

	if( typeof(p_bStrict) != 'undefined' ) bStrict = p_bStrict;
	if( typeof(p_bReturnIndex) != 'undefined' ) bReturnIndex = p_bReturnIndex;

	for(var nIndex = 0, nLength = p_aHaystack.length; (nIndex<nLength) && !bFound; nIndex++){
		if( bStrict ){
			if(p_aHaystack[nIndex] === p_varNeedle) bFound = true;
		} else {
			if(p_aHaystack[nIndex] == p_varNeedle) bFound = true;
		}
		if( bReturnIndex && bFound ){
			xResult = nIndex;
		} else {
			xResult = bFound;
		}
	}

	return xResult;
}

//concat one or more arrays without loosing the original keys
function combineArray(){
	var aResult = new Array();
	var nKey = 0;

	for(var nId = 0; nId < arguments.length; nId++){														//walk along all arguments, this way unlimited number of arrays can be combined
		if( isArray(arguments[nId]) ){																		//make sure the current is an array
			for(nKey = 0; nKey < arguments[nId].length; nKey++){ 											//loop over array to be copied
				if( typeof(arguments[nId][nKey]) != 'undefined' ) aResult[nKey] = arguments[nId][nKey];
			}
		}
	}

	return aResult;
}

function createPopupWindow(p_sTitle,p_sWindowOptions,p_sUrl){
	var bResult = false;

	try{
		var oWindow = window.open(p_sUrl,p_sTitle,p_sWindowOptions);
		if(oWindow == null){
			alert('Er kon geen venster geopend worden.<br>');
		} else {
			bResult = true;
		}
	} catch(e) {
		alert('Er deed zich een onbekende fout voor.<br>');
		//e.message error
	}
	
	return bResult;
}

function setCookie( name, value, expires, path, domain, secure, safe ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	// if the expires variable is set, make the correct 
	// expires time, the current script below will set 
	// it for x number of days, to make it for hours, 
	// delete * 24, for minutes, delete * 60 * 24
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +( (safe)?escape( value ) : value )+
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name, unsafe ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				if ( (unsafe) ) {
					cookie_value = a_temp_cookie[1].replace(/^\s+|\s+$/g, '');
				} else {
					cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
				}
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//this function can be used to add events to for instance the window
//using 'load' as eventname and window as the object
function addEvent(p_oObject, p_sEventName, p_oFunction){ 
	var mixResult = false;

	if( p_oObject.addEventListener ){ 
		p_oObject.addEventListener(p_sEventName, p_oFunction, false); 
		mixResult = true; 
	} else if( p_oObject.attachEvent ){ 
		mixResult = p_oObject.attachEvent("on"+p_sEventName, p_oFunction); 
	}

	return mixResult;
}

function selectJump(p_oTarget,p_oSource,p_bReset){
	var oTarget = getDOMElementById(p_oTarget);
	var oSource = getDOMElementById(p_oSource);

	if(oSource && oTarget && oTarget.location){
		oTarget.location.href = oSource.value; //eval(p_sTarget+".location='"+oSource.value+"'");
		if (p_bReset) oSource.selectedIndex = 0;
	}
}

function confirmJump(p_sQuestion,p_sHyperlink){
	if(confirm(p_sQuestion)){
		location.href = p_sHyperlink;
	}
}

function changeNodeClass(p_oNode,p_sId){
	var oNode = null;

	if( p_oNode) oNode = getDOMElementById(p_oNode);
	if(oNode && p_sId.length) oNode.className = p_sId;

	return oNode;
}

function toggleDisplay(p_oNode,p_sDisplay){
	var oNode = null;

	if( (p_sDisplay == undefined) || !p_sDisplay.length ) p_sDisplay = "inline";
	if( p_oNode ) oNode = getDOMElementById(p_oNode);
	if( oNode && oNode.style ){
		var sDisplay = oNode.style.display;
		oNode.style.display = (sDisplay != "none") ? "none" : p_sDisplay;
	}

	return oNode;
}

function toggleVisibility(p_oNode,p_sVisibility){
	var oNode = null;

	if( (p_sVisibility == undefined) || !p_sVisibility.length ) p_sVisibility = "visible";
	if( p_oNode ) oNode = getDOMElementById(p_oNode);
	if( oNode && oNode.style ){
		var sVisibility = oNode.style.visibility;
		oNode.style.visibility = (sVisibility != "hidden") ? "hidden" : p_sVisibility;
	}
	
	return oNode;
}

function setVisibility(p_oNode,p_sVisibility){
	var oNode = null;

	if( (p_sVisibility == undefined) || !p_sVisibility.length ) p_sVisibility = "visible";
	if( p_oNode ) oNode = getDOMElementById(p_oNode);
	if( oNode && oNode.style ) oNode.style.visibility = p_sVisibility;
	
	return oNode;
}

function autoResizeFrame(p_oFrameElement,p_nDefaultHeight) {
	var nNewHeight = (!isNaN(p_nDefaultHeight)?p_nDefaultHeight:0) + 'px';
	var nOffset = 0;
	var oFrameElement = null;

	if(p_oFrameElement){
		if(typeof(p_oFrameElement) == 'object'){
			oFrameElement = p_oFrameElement;
		} else if(parent && parent.document && parent.document.getElementById){
			oFrameElement = parent.document.getElementById(p_oFrameElement);
		} else if(parent && parent.document && parent.document.all){
			oFrameElement = parent.document.all[p_oFrameElement];
		}
	}

	if(oFrameElement){
		oFrameElement.style.display = "block";
		
		if(document && document.body && document.body.offsetHeight){
			nNewHeight = document.body.offsetHeight;
		}

		oFrameElement.style.height = (nNewHeight+nOffset)+'px';
	}
}

function changeFrameContents(p_sTarget,p_sSource,p_nWidth,p_nHeight){
	var oFrame = getDOMElementById(p_sTarget);
	var oSource = getDOMElementById(p_sSource);
	var sUrl = '';

	if( ((oSource === null) || (oSource === undefined)) && p_sSource.length){
		//p_sSource is not a known element, so must be an url string
		sUrl = p_sSource;
	} else if(oSource && (oSource.value !== undefined) ){
		//p_sSource was found, using the node pointer
		sUrl = oSource.value;
	}

	if( oFrame && (oFrame.src !== undefined ) ){
		if(!isNaN(p_nWidth)){
			oFrame.width = p_nWidth;
			oFrame.style.width = p_nWidth + 'px';
		}
		if(!isNaN(p_nHeight)){
			oFrame.height = p_nHeight;
			oFrame.style.height = p_nHeight + 'px';
		}
		if(sUrl.length) oFrame.src = sUrl;
	}
}

/**
 * addDOMElement()
 * @param {Object} p_oArguments	This object consists of several members that make up the arguments for creating a DOM element
 * 
 * These arguments are:
 * @param {String} type			defining what kind of DOM element needs to be created
 * @param {Object} document		used to create the new DOM element, defaults to the document of the current window
 * @param {Object} parent		defines the object to which the newly created DOM element needs to be appended
 * @param {Object} attributes	should contain a list of attributes that need to be assigned to the new DOM element
 * @param {String} text			if param type is 'text', this parameter should contain the text to be added as a textNode
 * 
 */
function addDOMElement(p_oArguments){
	var oNewElement = null;

	//see if the new element needs to be created in document or somewhere else
	var oDocument = ( (typeof(p_oArguments.document) != 'undefined') && p_oArguments.document?p_oArguments.document:document);

	if(oDocument && p_oArguments.type){
		switch(p_oArguments.type){
			case 'text' :
				if( oDocument.createTextNode && (typeof(p_oArguments.text) != 'undefined') ){
					oNewElement = oDocument.createTextNode(p_oArguments.text);
				}
				break;
			default :
				if( oDocument.createElement ){
					oNewElement = oDocument.createElement(p_oArguments.type);
				}
				break;
		}
	} 

	if( oNewElement ){
		if ( p_oArguments.attributes && (typeof(p_oArguments.attributes) == 'object')) {
			setDOMAttributes(oNewElement, p_oArguments.attributes);
		}
		if( p_oArguments.parent && p_oArguments.parent.appendChild ){
			p_oArguments.parent.appendChild(oNewElement);
		}
	}

	return oNewElement;
}

function setDOMAttributes(p_oNode,p_oAttributes){
	if( !p_oNode ){										//check if p_oNode is not null of false, else return false
		return false;
	} else if( !p_oNode.setAttribute ){					//check if setAttribute is available
		return false;
	} else if( !p_oAttributes ){						//check if any attributes were supplied
		return false;
	} else if( (typeof(p_oAttributes) != 'object') ){	//see if these attributes are placed in an object
		return false;
	} else {
		for(var sKey in p_oAttributes){
			switch(sKey){
				case 'style' :
					//p_oNode.setAttribute("style",p_oAttributes[sKey]); //does nto work in IE
					p_oNode.style.cssText = p_oAttributes[sKey];
					break;
				case 'onmousedown' :
					p_oNode.onmousedown = p_oAttributes[sKey];
					break;
				case 'onmouseenter' :
					p_oNode.onmouseenter = p_oAttributes[sKey];
					break;
				case 'onmouseover' :
					p_oNode.onmouseover = p_oAttributes[sKey];
					break;
				case 'onmouseup'	:
					p_oNode.onmouseup = p_oAttributes[sKey];
					break;
				case 'onmousemove'	:
					p_oNode.onmousemove = p_oAttributes[sKey];
					break;
				case 'onmouseout'	:
					p_oNode.onmouseout = p_oAttributes[sKey];
					break;
				case 'onclick'	:
					p_oNode.onclick = p_oAttributes[sKey];
					break;
				case 'ondblclick'	:
					p_oNode.ondblclick = p_oAttributes[sKey];
					break;
				case 'onfocus'	:
					p_oNode.onfocus = p_oAttributes[sKey];
					break;
				case 'onblur'	:
					p_oNode.onblur = p_oAttributes[sKey];
					break;
				case 'onkeypress'	:
					p_oNode.onkeypress = p_oAttributes[sKey];
					break;
				case 'onkeydown'	:
					p_oNode.onkeydown = p_oAttributes[sKey];
					break;
				case 'onkeyup'	:
					p_oNode.onkeyup = p_oAttributes[sKey];
					break;
				case 'onsubmit'	:
					p_oNode.onsubmit = p_oAttributes[sKey];
					break;
				case 'onselect'	:
					p_oNode.onselect = p_oAttributes[sKey];
					break;
				case 'onchange'	:
					p_oNode.onchange = p_oAttributes[sKey];
					break;
				case 'checked' :
					p_oNode.checked = p_oAttributes[sKey];
					break;
				case 'className' :
					p_oNode.className = p_oAttributes[sKey];
					break;
				case 'src' :
					p_oNode.src = p_oAttributes[sKey];
					break;
				default :
					p_oNode.setAttribute(sKey,p_oAttributes[sKey]);
					break;
			}
		}
	}
}

//object to set attributes on a node with a delay
function delaySetAttributes(p_sTarget,p_oAttributes,p_nInterval,p_oAfterUpdate){
	this.m_sTarget = p_sTarget;
	this.m_oAttributes = p_oAttributes;
	this.m_nTimerID = null;
	this.m_nInterval = 1000;
	this.m_oAfterUpdate = null;
	if( (typeof(p_nInterval) != 'undefined') && p_nInterval ) this.m_nInterval = p_nInterval;
	if( (typeof(p_oAfterUpdate) == 'function') ) this.m_oAfterUpdate = p_oAfterUpdate;
}

delaySetAttributes.prototype.init = function(){
	this.m_nTimerID = window.setInterval(this.updateElement.bind(this),this.m_nInterval);
};

delaySetAttributes.prototype.updateElement = function(){
	var oNode = null;
	
	oNode = getDOMElementById(this.m_sTarget);
	if( oNode ){
		setDOMAttributes(oNode,this.m_oAttributes);
		window.clearInterval(this.m_nTimerID);
		if( typeof(this.m_oAfterUpdate) == 'function' ) this.m_oAfterUpdate();
	}
};

function clearNode(p_oNode){
	var bResult = false;
	var oNode = getDOMElementById(p_oNode);

	if( !oNode ){										//no node available
		return bResult;
	} else if( !oNode.hasChildNodes ){					//hasChildNodes() not available
		return bResult;
	} else {
		while(oNode.hasChildNodes()){					//loop while there are children to delete
			oNode.removeChild(oNode.lastChild);			//delete the last child
		}
		bResult = true;
	}

	return bResult;
}

function putStringInNode(p_sIdName,p_sString){
	var oNode = null;
	var sText = '';

	if( !p_sIdName ){
		return false;
	} else if( !p_sString ){
		return false;
	} else {
		oNode = getDOMElementById(p_sIdName);
		if( !oNode ){
			return false;
		} else if( !oNode.createTextNode || !oNode.appendChild ){
			return false;
		} else {
			clearNode(oNode);
			sText = new String(p_sString);
			oNode.appendChild(document.createTextNode(sText));
		}
	}
}

function getStringFromNode(p_sIdName){
	var oNode = null;
	var oChildNode = null;
	var sText = new String();
	var bResult = false;

	if( !p_sIdName ){
		return sText;
	} else {
		oNode = getDOMElementById(p_sIdName);
		if( !oNode ){
			return sText;
		} else if( !oNode.hasChildNodes || !oNode.childHasNodes() ){
			return sText;
		} else {
			oChildNode = oNode.firstChild;
	
			while(oChildNode && !bResult){
				//is the node a TextNode?
				if(oChildNode.nodeType == 3){
					sText = new String(oChildNode.nodeValue);
					bResult = true;
				}
				oChildNode = oChildNode.nextSibling;
			}
		}
	}
	
	return sText;
}

/* this function requires prototype library for $(), getInputs() and $F() */
function getRadioValue(p_oElement, p_sRadioGroup){
	var oElement = $(p_oElement);
	var bResult = false;

	if(p_oElement){
		if(oElement.type && (oElement.type.toLowerCase() == 'radio') ){
			var p_sRadioGroup = oElement.name;
			var oElement = oElement.form;
		} else if (oElement.tagName.toLowerCase() != 'form') {
			//nothing, bResult == false
		}
		bResult = $(oElement).getInputs('radio', p_sRadioGroup).find(function(p_oRadioElement){return p_oRadioElement.checked;});
	}

	return (bResult) ? $F(bResult) : null;
}

function setCheckBox(p_sIdName,p_bEnable,p_bUnCheck){
	var oNode = null;

	if(p_sIdName && (oNode = getDOMElementById(p_sIdName)) ){
		if( typeof(p_bUnCheck) != 'undefined'){
			oNode.checked = p_bUnCheck;
		}

		if(p_bEnable){
			if(oNode.getAttribute("disabled")){
				oNode.removeAttribute("disabled");
			}
		} else {
			if( typeof(p_bUnCheck) == 'undefined'){
				oNode.checked = 0;
			}
			oNode.setAttribute("disabled","true");
		}
	}
}

function toggleCheckBox(p_sIdName){
	var oNode =null;

	if(p_sIdName && (oNode = getDOMElementById(p_sIdName)) ){
		if(oNode.getAttribute("disabled")){
			oNode.removeAttribute("disabled");
		} else {
			oNode.checked = 0;
			oNode.setAttribute("disabled","true");
		}
	}
}

function validateForm(p_aControls){
	var bResult = false;

	if( typeof(p_aControls) != 'undefined' && typeof(p_aControls.length) != 'undefined' && p_aControls.length ){
		var nControlCount = p_aControls.length;
		var oControl = null;

		for(var nIndex = 0; nIndex < nControlCount; nIndex++){
			oControl = document.getElementById(p_aControls[nIndex].idname);
			if( !oControl || (typeof(oControl.value) == 'undefined') ){
				//programmer error
				alert('unexpected error occured');
				return false;
			} else if( oControl.value == '' || oControl.value == '0' || oControl == 0 ){
				alert(p_aControls[nIndex].message);
				oControl.focus();
				return false;
			} else {
				bResult = true;
			}
		}
	}

	return bResult;
}

