//
//  This function finds the parent div 'formulier' and encapsulates it in a div class="formularError".
//  This will result in an error message at the top of the form.
//  input:
//      - formElement -> the element which contains (or doesn't contain) a error
//      - isFout -> boolean tests if the formelement is fout
//      - message -> the message to show when it's isFout (else the old message is cleared and the red box is removed)
//      - toonMessage -> if this is true then display the error message in the form...otherwise in an alertbox
//      - formid -> id of the form element
//

function setFormularError(formElement, isFout, message, toonMessage, formid)
{
    var parent;         // the parent
    var divClass = "formulier"; // the class we are searching for
    var errorHTMLTag = "* " + message; // the message set within a html tag for making red (see css)
    var fieldLabel;
    var errorElement;
    var divs;
    var isFound = false;
    var errorStar;
    var formTable;
    var formElementParent;
    var fieldLabelSpanElement;
    var errorMsgId;

    if (!formElement) {
        if(isFout) { alert(message); }
        return false;
    }
    parent = formElement;
    // if there is no parentnode, the element is a radiobutton,
    // so we have to get the first arrayelement to continue
    if (!parent.parentNode) parent = parent[0];
    var isFound = false;

    if(isFout)
    {
        if(toonMessage)
        {
            // reset all the label error classes and remove color '*'
            formTable = document.getElementById("formTable_" + formid);
            //formTable = formElement.parentNode.offsetParent;
            if(formTable != null) {
		            for (var j = 0; j < formTable.rows.length; j++ )
		            {
		                if (formTable.rows[j].cells.length != 0 && formTable.rows[j].cells[0].childNodes.length != 0)
		                {
		                    fieldLabelSpanElement = formTable.rows[j].cells[0].childNodes[0];
		                    if(fieldLabelSpanElement.className == "fout") {
			                    fieldLabelSpanElement.className = "";
			                  }

		                    if(fieldLabelSpanElement.childNodes && fieldLabelSpanElement.childNodes.length != 0 && fieldLabelSpanElement.childNodes.length > 1)
		                    {
		                        fieldLabelSpanElement.removeChild(fieldLabelSpanElement.childNodes[1]);
		                    }
		                }
		            }
						}

            // set the error message at the top of the form table
            div = document.getElementById("formulier_foutmelding_" + formid);
            if (div != null)
            {
                // set the message
                div.innerHTML = errorHTMLTag;
                isFound = true;

                // change the field label color and mark with an '*'
                if (formElement.id != "")
                {
                    errorMsgId = formElement.id;    //IE
                }
                else
                {
                    errorMsgId = formElement.name;  //Firefox
                }
                fieldLabel = document.getElementById(errorMsgId);
                fieldLabel.className = "fout";
                // create the star and containing span for the error message,
                // only if it doesn't already exist
                if(fieldLabel.childNodes.length < 2)
                {
                    errorStar = document.createElement("span");
                    errorStar.className = "formulier_star";
                    errorStar.innerHTML = " *";

                    // And add it to the original field label
                    fieldLabel.appendChild(errorStar);
                }
            }
            // if we don't find the formulier_foutmelding div, display error in an alert
            if(!isFound)
            {
                alert(message);
            }
        }
    }
    else
    {

        parent.parentNode.className = 'stub';

        if(toonMessage)
        {
            // reset all the label error classes and remove color '*'
            formTable = document.getElementById("formTable_" + formid);
            //formTable = formElement.parentNode.offsetParent;
            if(formTable != null) {
		            for (var j = 0; j < formTable.rows.length; j++ )
		            {
		                if (formTable.rows[j].cells.length != 0 && formTable.rows[j].cells[0].childNodes.length != 0)
		                {
		                    fieldLabelSpanElement = formTable.rows[j].cells[0].childNodes[0];
		                    if(fieldLabelSpanElement.className == "fout") {
			                    fieldLabelSpanElement.className = "";
			                  }
		                    if(fieldLabelSpanElement.childNodes && fieldLabelSpanElement.childNodes.length > 1)
		                    {
		                        fieldLabelSpanElement.removeChild(fieldLabelSpanElement.childNodes[1]);
		                    }
		                }
		            }
		        }

            // set the error message at the top of the form table
            div = document.getElementById("formulier_foutmelding_" + formid);
            if (div != null)
            {
                // set the message
                div.innerHTML = errorHTMLTag;
                isFound = true;

                // change the field label color and mark with an '*'
                if (formElement.id != "")
                {
                    errorMsgId = formElement.id;    //IE
                }
                else
                {
                    errorMsgId = formElement.name;  //Firefox
                }
                fieldLabel = document.getElementById(errorMsgId);
                fieldLabel.className = "fout";
                // create the star and containing span for the error message,
                // only if it doesn't already exist
                if(fieldLabel.childNodes.length < 2)
                {
                    errorStar = document.createElement("span");
                    errorStar.className = "formulier_star";
                    errorStar.innerHTML = " *";

                    // And add it to the original field label
                    fieldLabel.appendChild(errorStar);
                }
            }
            // if we don't find the formulier_foutmelding div, display error in an alert
            if(!isFound)
            {
                alert(message);
            }
        }
    }

}

