
function toggleDiv( strContentDivId, strFormId, strImgId, strPicturePathOnSource, strPicturePathOffSource )
{
	if ( !strContentDivId )
	{
		return false;
	}

	if ( strImgId )
	{
		var openImg = new Image();
		openImg.src = strPicturePathOnSource;
		var objClosedImg = new Image();
		objClosedImg.src = strPicturePathOffSource;
		objImg = document.getElementById( strImgId );
	}

	var objContentDiv = document.getElementById( strContentDivId );

	if ( !objContentDiv )
	{
		return false;
	}

	if ( objContentDiv.style.display == 'none' )
	{
		objContentDiv.style.display = 'block';

		strStyleDisplayValue = 'block';

		if ( strImgId )
		{
			objImg.src = openImg.src;
		}

		// Start fck editor.
		if ( strFormId != null || strFormId != "" )
		{
	        switchEditors( strFormId );
		}
	}
	else
	{
		objContentDiv.style.display = 'none';

		strStyleDisplayValue = 'none';

		if ( strImgId )
		{
			objImg.src = objClosedImg.src;
		}
		
	}

	return true;
}

function validateForm( objForm )
{
	// We dont have any form object ? Sorry.
	if ( !objForm )
	{
		return false;
	}

	// First, look if the parameter is a string, if it is then load this element as a javascript object.
	if ( typeof( objForm ) == "string" )
	{
    	objForm = document.getElementById ( objForm );

		if ( !objForm && document.all )
		{
			objForm = document.all[objForm];
		}
	}

	// We dont have any elements under the form ? There's not an error but we dont need to process it.
	if ( objForm.elements.length <= 0 )
	{
		return true;
	}
	
	// Counter of form elemnts.	
	var intCounter = 0;
	// Boolean error per default.
	var boolError = false;
	// String of errors.
	var strErrorMsg = '';
	// Instantiate regular expressions engine.
	var regEx = new RegExp();

	// Loop all the elements on the form.
	for ( intCounter = 0; intCounter < objForm.elements.length; intCounter++ )
	{
		// If the element is required.
		// NOTE: "required" is an extra tag on the element.
		if ( 'true' == objForm.elements[intCounter].getAttribute( 'required' ) )
		{
			// Wich type is the current element.
			switch ( objForm.elements[intCounter].type )
			{
				case 'text':
				case 'textarea':
				case 'file':
					// For text, textarea or file elements on the form check if they has some value.
					if ( objForm.elements[intCounter].value == "" )
					{
						// Set errors token.
						boolError = true;
						
						// Add message to the errors string.
						// NOTE: "err_label" is an special tag on the input that allows you to add a custom error message.
						if ( strErrorMsg.indexOf( objForm.elements[intCounter].getAttribute( 'err_label' ) ) == -1 )
						{
							strErrorMsg += objForm.elements[intCounter].getAttribute( 'err_label' ) + '\n';
						}
					}

					// Try to validate some regular expression on for the current element.
					// NOTE: "check_characters" is an special tag on the input, a regular expression string.
					if ( '' != objForm.elements[intCounter].getAttribute( 'check_characters' ) )
					{
						regEx.compile( objForm.elements[intCounter].getAttribute( 'check_characters' ) );

						regExResults = regEx.exec( objForm.elements[intCounter].value );

						if ( null != regExResults )
						{
							// Set errors token.
							boolError = true;
							// Add error.
							strErrorMsg += 'The following characters are invalid for ' + objForm.elements[intCounter].getAttribute( 'item_label' ) + ': ' + objForm.elements[intCounter].value.charAt( regExResults.index ) + '\n';
						}
					}
					
					break;
						
				case 'select-one':
					// Anything selected on this drop-down. Ergghhh ...
					if ( objForm.elements[intCounter].value == "" )
					{
						// Set errors token.
						boolError = true;
						// Add error.
                   	    if ( strErrorMsg.indexOf( objForm.elements[intCounter].getAttribute( 'err_label' ) ) == -1 )
						{
							strErrorMsg += objForm.elements[intCounter].getAttribute( 'err_label' ) + '\n';
						}
					}
						
					break;
						
				case 'select-multiple':
					// No optiones selected.
					if ( objForm.elements[intCounter].selectedIndex == -1 )
					{
						// Set errors token.
						boolError = true;
						// Add error.
                	    if ( strErrorMsg.indexOf( objForm.elements[intCounter].getAttribute( 'err_label' ) ) == -1 )
						{
							strErrorMsg += objForm.elements[intCounter].getAttribute( 'err_label' ) + '\n';
						}
					}
					
					break;
					
				default:
					// There's not value here.
					if ( objForm.elements[intCounter].value.length == 0 )
					{
						// Set errors token.
						boolError = true;
						// Add error.
						if ( strErrorMsg.indexOf( objForm.elements[intCounter].getAttribute( 'err_label' ) ) == -1 )
						{
							strErrorMsg += objForm.elements[intCounter].getAttribute( 'err_label' ) + '\n';
						}
					}
					
					break;
			}
		}
	}

	// Show string of errors.
	if ( false != boolError && strErrorMsg != "" )
	{
		alert( 'The following errors were found:\n\n' + strErrorMsg + '\nPlease make the necessary changes and try again.' );
		
		return false;
	}

	return true;
}

function setRequiredElements( strContainerId, arrChildNodes, blnFunction )
{
	if ( !strContainerId )
	{
	    return false;
	}

	if ( "undefined" == typeof( blnFunction ) )
	{
		return false;
	}

	if ( !arrChildNodes )
	{
	    var arrChildNodes = document.getElementById( strContainerId ).childNodes;

	    if ( !arrChildNodes )
	    {
	        return false;
	    }
	}

	for ( var intChildCounter = 0; intChildCounter < arrChildNodes.length; intChildCounter++ )
	{
	    switch ( arrChildNodes[intChildCounter].nodeName )
	    {
	        case "INPUT":
	        case "SELECT":
	        case "TEXTAREA":
				if ( false == blnFunction )
				{
	                if ( "true" == arrChildNodes[intChildCounter].getAttribute( 'required' ) )
		            {
		                arrChildNodes[intChildCounter].setAttribute( 'required', false );
		            }
				}
				else if ( true == blnFunction )
				{
					if ( "false" == arrChildNodes[intChildCounter].getAttribute( 'required' ) )
	                {
	                    arrChildNodes[intChildCounter].setAttribute( 'required', true );
	                }
				}

	            break;

	        default:
	            setRequiredElements( strContainerId, arrChildNodes[intChildCounter].childNodes, blnFunction );

	            break;
		 }
	}

	return true;
}




var FCKeditorLoaded = false;
    
function FCKeditor_OnComplete( editorInstance ) 
{
	FCKeditorLoaded = true;
}

function switchEditors( ID ) 
{
	if( !FCKeditorLoaded ) 
	{
    	setTimeout( 'switchEditors(\'' + ID + '\')', 500 );

	    return;
	}

	DoSwitchEditors( document.getElementById( ID ) );
}

function DoSwitchEditors( oNode ) 
{
	var i;

	if ( oNode )
	{
		for ( i = 0; i < oNode.childNodes.length; i++ ) 
		{
        	childNode = oNode.childNodes.item( i );

	        editor = FCKeditorAPI.GetInstance( childNode.name );

    	    if ( editor && editor.EditorDocument && editor.EditMode == FCK_EDITMODE_WYSIWYG ) 
			{
				editor.SwitchEditMode();
				editor.SwitchEditMode();
	        }

    	    DoSwitchEditors( childNode );
		}
	}
}
