/****************************************************************** 
 
*******************************************************************/
 

var sem = false, orgColor, but = new Array; // Globals which could not be avoided
//document.onkeydown = keyDownEvent; // capture events for CTRL combination strokes support

function prosUrl( curl, trgt )
{ // solve a dd url tag string and make this the current location
	var obj = getTrgtObj( trgt );
	obj.location = parseStr( curl, 1 );
}

function prosUrlPopup( curl )
{ // solve a dd url tag string and open a window for it
	popUrl = parseStr( curl, 1 );
	ddWin = window.open( popUrl,'ddPopupWin', 'width=700,height=400,resizable,scrollbars' );
	ddWin.focus();
}

function webEscapeVal( str )
{ // Thanks for escape
	return( escape( str ) );
}

function rollIn( obj, color )
{ // hoover
	obj.style.cursor = "hand";
	orgColor = obj.style.color;
	obj.style.color = color;
}
function rollOut( obj )
{ // and back orgColor is global
	obj.style.color = orgColor;
}


function setLvalue( tagStr )
{ // Assume a javascript assigment template and replace the lval = 
  // ... for window.document.my.lval = ...
	var lval = tagStr.substr( 0, tagStr.indexOf( '=', 0 ) );
	if( lval != '' )
	{
		tagStr = findAndRep( tagStr, lval, "window.document.my." + lval + ".value" );
	}
	return tagStr;
}


function calcOper( tagStr )
{ // Perform a evaluation of the passed dd javascript assignment template
	tagStr = findAndRep( tagStr, ' %', '%' ); // Unescape
	tagStr = findAndRep( tagStr, '% ', '%' );
	tagStr = parseStr( tagStr, 0 ); 
	tagStr = setLvalue( tagStr ); 
	eval( tagStr );
	return true;
}

function execBlur( oper, fld, frmt, cnd )
{ // evaluate the operation specified for field fld
  // the operation can be a format or a javascript assigment
  	if( !sem ) // make sure it is triggered only once
	{
		var OK = true;
		sem = true; // global
		if( frmt != '' && fld.value != '' )
		{
			OK = chkFrmt( fld, frmt );
		}
		if( oper != '' && OK )
		{
			var opers = oper.split(";");   
			for( var i=0; i < opers.length; i++ )
			{
				OK = calcOper( opers[ i ] );
			}
		}
		if( cnd != '' && fld.value != '' && OK )
		{
			chkCnd( fld, cnd );
		}
		sem = false;
	}
	else
	{
		sem = false;
	}
}

function clntSub( ddDiagTarget, trgt )
{ // Submit initiated from the client with support of targets ( submit another form then the one having the button defined )
  // The client submit button must have an associated hidden field ddDiagTarget so the receiver knows which button is pressed. 
  // For backwards compatibility a check in subButton is also made.
  	
  	var obj, subField;
	obj = getTrgtObj( trgt );
	
	load = document.getElementById('loading');
	if(load) {
		try {
			load.style.display = '';
		}
		catch(err){ }
	}
	document.body.style.cursor='wait';
	if( checkForm(obj.my, trgt ) )
	{
		/*
		 * submit HTML Editor content, if not exists, then error will be caught
		 */
		try {
			onsubmit_new();
		}
		catch(err){
			//alert(err);
		}
		
		subField = ( obj.my.elements[ 'DD_DIAG_TARGET' ] ) ? 'DD_DIAG_TARGET' : ( obj.my.elements[ 'subButton' ] ) ? 'subButton' : undef;
		if( obj.my.elements[ subField ] )
		{
			if( obj.my.elements[ subField ].type )
			{
				if( obj.my.elements[ subField ].type == 'hidden' )
				{
					obj.my.elements[ subField ].value = ddDiagTarget;
					obj.my.submit(); // post
				}
			}
		}
		else
		{
			// Do not no how to submit both DD_DIAG_TARGET and subButton are not defined.
			alert( 'Kan niet submitten zowel DD_DIAG_TARGET als subButton zijn niet gedefinieerd.');
		}
	}
}
		
function getTrgtObj( trgt )
{ // Get the obj of the target ( art!? )
	return( ( trgt ) ? ( ( trgt != 'parent' ) ? parent.frames[ trgt ] : this.parent ) : this.document );
}

function parseStr( tagStr, escFlag )
{ // Find and replace the %% tags and replace it with the value if they are form fields.
  // If the escFlag is set to 1 the found values will be web escaped
	escFlag = escFlag || 0;
	var obj = this.document, re = /%\w+%/g, res = tagStr.match( re ), fieldName, fieldObj;
	if( res )
	{ // yes found %tag(s)%
		for( var i = 0; i < res.length; i++ )
		{ // All found %%
			fieldName = res[ i ].substring( 1, res[ i ].length - 1 ); // Cut %%
			if( obj.my.elements[ fieldName ] )
			{ // Yes it is a form element 
				if( obj.my.elements[ fieldName ].type )
				{ // Yes it is a form element with a type
					if( obj.my.elements[ fieldName ].type == 'text' || obj.my.elements[ fieldName ].type == 'radio' )
					{
						fieldObj = obj.my.elements[ fieldName ];
					}
					else if( obj.my.elements[ fieldName ].type == 'select-one' || 
						obj.my.elements[ fieldName ].type == 'select-multiple' )
					{ // limited support for select multiple only return the first element checked will be returned
						for( var ii = 0; ii < obj.my.elements[ fieldName ].length; ii++ )
						{
							if( obj.my.elements[ fieldName ][ ii ].selected )
							{
								fieldObj = obj.my.elements[ fieldName ][ ii ];
							}
						}
					}
				}
				else if( obj.my.elements[ fieldName ].length )
				{ // Yes it is a form element with possible more values for now only radio is supported
					for( var ii = 0; ii < obj.my.elements[ fieldName ].length; ii++ )
					{
						if( obj.my.elements[ fieldName ][ ii ].type == 'radio' )
						{ 
							if( obj.my.elements[ fieldName ][ ii ].checked )
							{
								fieldObj = obj.my.elements[ fieldName ][ ii ];
								break;
							}
								
						}
					}
				}
				if( fieldObj )
				{
					tagStr = findAndRep( tagStr, res[ i ], ( escFlag == 1 ) ? escape( fieldObj.value ) : fieldObj.value );
				}
			}
		}
	}
	return( tagStr );
}

function findAndRep( str, strFind, strRep )
{ // Love regexp... thanks Larry
	return( str.replace( new RegExp( strFind, 'g' ), strRep ) );
}


/*function keyDownEvent( e ) 
{ // Bind the buttons left to right to CRTL 1 ... 9
	var whichKey = window.event.keyCode;
	var realKey = String.fromCharCode( whichKey );
	var ctrlPressed = window.event.ctrlKey;
	if( ctrlPressed )
	{
		if( ( whichKey >=49 && whichKey <= 59 ) || whichKey >= 97 && whichKey <= 107 )
		{
			i = ( whichKey >= 97 ) ? whichKey - 97 : whichKey - 49;
			if( i < but.length )
			{
				window.document.my.elements[ but[ i ] ].click(); // push it
			}
		}
	}
}   
*/
function setFocus( fName )
{ // Set the focus on the first or given form element
	var i, cPos = 0, flag = 0;
	if( fName != "" )
	{
		for( i=0; i<window.document.my.elements.length; i++ )
		{
			if( window.document.my.elements[ i ].name == fName )
			{
				cPos = i;// + 1;
			}
		} 
	}
	for( i = cPos; i < window.document.my.elements.length; i++ )
	{
			if( window.document.my.elements[ i ].type == "radio" ||
				window.document.my.elements[ i ].type == "text" ||
				window.document.my.elements[ i ].type == "password" ||
				window.document.my.elements[ i ].type == "textarea" ||
				window.document.my.elements[ i ].type == "select-one" ||
				window.document.my.elements[ i ].type == "checkbox" )
			{
			if( window.document.my.elements[ i ].name.indexOf( "foldercheck" ) < 0 &&			
					window.document.my.elements[ i ].name.indexOf( "foldertext" ) < 0 )			
			{ // Make an exeption for checkbox in the folder item
				window.document.my.elements[ i ].focus();
				flag = 1;
				break;
			}
		}
	}
	var x = 0;
	if( fName == "" )
	{
		for( i = 0; i < window.document.my.elements.length; i++ )
		{
			if( window.document.my.elements[ i ].type == "button" ||
				window.document.my.elements[ i ].type == "submit" )
			{
				if( flag == 0 )
				{
					window.document.my.elements[ i ].focus();
					flag = 1;
				}
				but[ x++ ] =  i // global array for CTRL button binding
			}
		}
	}
}

function checkForm( frm, trgt )
{ // Wrapper
	var chkMand = checkMandatory( trgt );
	var chkVali = validateForm(frm, false, true, false, true, 16);
	if (chkMand && chkVali) return true;
	return false ;
}

function checkMandatory( trgt )
{ // Check the comma separated list of fields in the hidden field obligate for values
	var fldName, fldObj, stat = true, errMsg = "", flag = 0, obj = getTrgtObj( trgt ), mVal = obj.my.obligate.value;
	if( mVal != "" )
	{
		mandFields = mVal.split(",");
		for( var i=0; i < mandFields.length; i++ )
		{      
			fldObj = obj.my[ mandFields[ i ] ];
			if( fldObj.value == "" )
			{
				fldName = ( fldObj.label ) ? ( fldObj.label != '' ) ? fldObj.label : fldObj.name : fldObj.name;
				errMsg = errMsg + "Leeg verplicht veld: " + fldName + "\n";
				stat = false;
				if( flag == 0 )
				{
					fldObj.focus();
					flag = 1;
				}
			} 
		}
	}
	if( !stat )
	{
		alert( errMsg );
	}
	return stat;
}	
/*
function chkCnd( fld, cnd )
{ // prepare and evaluate a given condition like > 20
	cnd = 'if (!(' + fld.value + cnd + ') ) { fld.focus(); alert( \'Ongeldige veldwaarde moet zijn: ' + cnd  + '\' ) }';
	ret =  eval ( cnd );
	return true;
}

function chkLngth( fld )
{ // Check the length of value against the maxlength
	var fldName;
	if( fld.value.length > fld.maxlength )
	{
		fldName = ( fld.label ) ? ( fld.label != '' ) ? fld.label : fld.name : fld.name;
		errMsg = "Veld: " + fldName + " moet een lengte hebben van: " + fld.maxlength;
		errMsg += " Huidige lengte is: " + fld.value.length;
		alert( errMsg );
		fld.focus();
	}
}
	
function chkFrmt( fld, frmt )
{ // Check the field value against the format specified as a format string
	var errMsg, i, fldName, val = fld.value, OK = true;
	fldName = ( fld.label ) ? ( fld.label != '' ) ? fld.label : fld.name : fld.name;
	if( ( val.length !=  frmt.length ) && 
		!( frmt == 'D' || frmt == 'F' || frmt == 'M' || frmt == 'HH:MI' || frmt =='D:MI' || evalRegExp( '\\/.*\\/', frmt )  ) )
	{
		OK = false;
		errMsg = "dd.err.Field; " + fldName + " (" + frmt + ")  moet een lengte hebben van: " + frmt.length;
		errMsg += " Huidige lengte is: " + val.length;
	}
	if( OK )
	{
		forloop :
		for( i=0; i<frmt.length; i++ )
		{
			switch( frmt.charAt( i ) )
			{
				case 'd' :
					if( !chkNum( val.charAt( i ) ) )
					{
						OK = false;
						errMsg = "Cijfer vereist op positie: " + i + " van veld: "; 
						errMsg += fldName + " (" + frmt + "). Huidige wrde is: " + val.charAt( i ); 
						break forloop;
					}
					else
					{
						break;
					}
				case 'a' :
					if( chkNum( val.charAt( i ) ) )
					{
						OK = false;
						errMsg = "Alfabetisch karakter vereist op deze positie: " + i + " van veld: " ;
						errMsg += fldName + " (" + frmt + "). Huidige wrde is: " + val.charAt( i ); 
						break forloop;
					}
					else
					{
						break;
					}
				case 'D' :
					if( frmt == 'D:MI' )
					{
						var re = /(^\d+):(\d{2})$/g, res = re.exec( val );
						if( res )
						{ 
							if( res[ 2 ] > 59 )
							{	
								OK = false;
								errMsg = "Ongeldige tijdswaarde: " + val + " voor veld: " + fldName + '\n';
								errMsg += "Minuten moeten tussen 0 en 59 liggen";
								break forloop;
							}
							else
							{ // Yes its good
								break forloop;
							}
						}
						else if( chkNum( val ) )
						{
							fld.value = val + ":00";
							break forloop;
						}
						else
						{
							OK = false;
							errMsg = "Ongeldige tijdswaarde: " + val + " voor veld: " + fldName + '\n';
							errMsg += "Moet formaat D:MI hebben";
							break forloop;
						}
					}
					else
					{
						if( !chkNum( val ) )
						{
							OK = false;
							errMsg = "Alleen cijfers toegestaan in veld: ";
							errMsg += fldName + " (" + frmt + "). Huidige wrde is: " + val; 
							break forloop;
						}
						else
						{
							break;
						}
					}
				case 'M' :
					var chk = val +"x";
					var re = /(^\d+)\.(\d{2})x/g, res = re.exec( chk );
					if( res )
					{
						break;
					}
					else if( chkNum( val ) )
					{
						fld.value = val + ".00";
						break forloop;
					}
					else
					{
						OK = false;
						errMsg = "Ongeldige valuta waarde: " + val + " voor veld " + fldName + '\n';
						break forloop;
					}
				case 'F' :
					var f = new String( parseFloat( val ) );
					if( isNaN( parseFloat( f ) ) || f.length != val.length )
					{
						OK = false;
						errMsg = "Drijvende komma vereist in veld: ";
						errMsg += fldName + " (" + frmt + "). Huidige wrde is: " + val; 
						break forloop;
					}
					else
					{
						break;
					}
				case 'H' :
						if( frmt == 'HH:MI' )
						{ // be sure it is time
							var re = /^(\d{1,2}):(\d{2})$/g, res = re.exec( val );
							if( res )
							{ 
								if( ( res[ 1 ] > 23 || res[ 2 ] > 59 ) && ( !( res[ 1 ] == 24 && res[ 2 ] == 0 ) ) )
								{	
									OK = false;
									errMsg = "Ongeldige tijdswaarde: " + val + " voor veld: " + fldName + '\n';
									errMsg += "Moet tussen 00:00 en 24:00 liggen";
									break forloop;
								}
								else
								{ // Yes its good
									break forloop;
								}
							}
							else if( chkNum( val ) )
							{
								if( ( val > 24 || val.length > 2 ) )
								{	
									OK = false;
									errMsg = "Ongeldige tijdswaarde: " + val + " voor veld: " + fldName + '\n';
									errMsg += "Moet tussen 00:00 en 24:00 liggen";
									break forloop;
								}
								else
								{
									fld.value = ( ( val <= 9 ) ? '0' + val : val ) + ":00";
									break forloop;
								}
							}
							else
							{
								OK = false;
								errMsg = "Ongeldige tijdswaarde: " + val + " voor veld: " + fldName + '\n';
								errMsg += "Moet formaat UU:MI hebben";
								break forloop;
							}
						}
				case '/' :
						if( evalRegExp( '\\/.*\\/', frmt ) )
						{ // Yes it is an regular expression
							if( evalRegExp( findAndRep( frmt.substring( 1, frmt.length - 1 ), '\\', '\\\\' ), val ) ) 
							{ // Cut out the / and escape the \ and evaluate
								break forloop;
							}
							else
							{
								OK = false;
								errMsg = "Ongeldige veldwaarde moet zijn: " + fldName;
								break forloop;
							}
						}
				default :
					if( ( frmt.charAt( i ) != val.charAt( i ) ) && ( frmt.charAt( i ) != 'c' ) )
					{
						OK = false;
						errMsg = "Karakter: " + frmt.charAt( i ) + " vereist op positie: " + i;
						errMsg += " in veld: " + fldName + " (" + frmt + "). Huidige wrde is: " + val.charAt( i ); 
						break forloop;
					}
			}
		}
	}
	if( !OK )
	{
		fld.focus();
		alert( errMsg );
	}
	return OK;
}

function evalRegExp( reStr, str )
{ // Evaluate a reg exp and return an array with the result
	return( ( new RegExp( reStr ) ).exec( str ) );
}

function chkNum( val )
{ // Check if val is a number
	var y, OK = true;
	for( y=0; y<val.length; y++ )
	{
		if ( isNaN( parseInt( val.charAt( y ), 10 ) ) )
		{
			OK = false;
			break;
		}
	}
	return OK
}
*/
function min2TimeFrmt( min )
{ // Return the given minutes in hour:min format with leading zeros if applicable
  // the hours are not limited to a day. 100:30 is a possible output
	var m = min % 60, h = ( min - m ) / 60;  // m is int minutes and h is int hours
	return( ( ( h == 0 ) ? '00' : ( h <= 9 ) ? '0' + h : h ) // string hours
	+ ':' + ( ( m == 0 ) ? '00' : ( m <= 9 ) ? '0' + m : m ) ); // string minutes
}

function timeFrmt2Min( timeFrmt )
{ // Return the given time in format hours:minutes in minutes
  // The fomat does not have to be HH:MI. 1000:999 is accepted
	var re = /(\d+):(\d+)/g, res = re.exec( timeFrmt );	
	return( ( res ) ? res[ 1 ] * 60 + parseInt( res[ 2 ], 10 ) : 0 );
}
// Extra functionality for DH Planning
function bereken(doorloopF,startF,eindF)
{
	var doorloop = document.all.item(doorloopF).value;
	var start = document.all.item(startF).value;
	if (doorloop != 0)
	{
		var jaar = start.substr(6);
		var maand = (start.substr(3,2)-1);
		var dag = start.substr(0,2);
		var myDate = new Date(jaar,maand,dag);
		var myInMilli = myDate.valueOf();
		var myDay = (1000*60*60*24);
		var myCorrectie = (Math.floor(doorloop/5)*7+(doorloop%5));
		//alert(myCorrectie);
		var myDate = new Date(myInMilli + myDay *(myCorrectie));
		var strWrk =myDate.toUTCString();
		var sMonth = "";
		var pJaar = 0;
		if (strWrk.substr(6,1)== " ")
		{
    			strEind = "0" + strWrk.substr(5,1)
    			sMonth = strWrk.substr(7,3)
    			pJaar = 11; 
    		}
    		else
    		{
    			strEind = strWrk.substr(5,2);
    			sMonth = strWrk.substr(8,3);
    			pJaar = 12;
    		}
		var strEind = strEind + "-"
		switch (sMonth)
		{
			case "Jan" :
				strEind = strEind + "01-";
				break;
			case "Feb" :
				strEind = strEind + "02-";
				break;
			case "Mar" :
				strEind = strEind + "03-";
				break;
			case "Apr" :
				strEind = strEind + "04-";
				break;
			case "May" :
				strEind = strEind + "05-";
				break;
			case "Jun" :
				strEind = strEind + "06-";
				break;
			case "Jul" :
				strEind = strEind + "07-";
				break;
			case "Aug" :
				strEind = strEind + "08-";
				break;
			case "Sep" :
				strEind = strEind + "09-";
				break;
			case "Oct" :
				strEind = strEind + "10-";
				break;
			case "Nov" :
				strEind = strEind + "11-";
				break;
			case "Dec" :
				strEind = strEind + "12-";
				break;
		}
		strEind = strEind + strWrk.substr(pJaar,4);
		document.all.item(eindF).value = strEind;
	}
	else
	{
		document.all.item(eindF).value = start;
	}
}

