/*
 * @author Saikat Podder
 * @for 247webmonitoring.com
 * Offcourse the original file was provided by struts, had to modified it heavily.
 * Only the clearErrorLabels(form) function still has some very good similarity(abt 94%) with the original version
 * TODO to add errorFor attribute instead of id into the divs, since if multiple field errors are added to a particul;ar field
 * then getElementById would not be able to access them all
 */

function clearErrorMessages(form) {

    // get field table
//	alert("clearErrorMessages");
	var formElements = form.elements;
    for (var i = 0; i < formElements.length; i++)
    {
        var e = formElements[i];
        var elementContainer = e.parentNode;
//        alert(e.id);
        var errorMsgDiv = document.getElementById(e.id+"_errorMessage");
        while(errorMsgDiv)
        {
//        	if (errorMsgDiv)
//	        {
	//        	alert(errorMsgDiv);
	        	try
	        	{
	        		elementContainer.removeChild(errorMsgDiv)
	        	}
	        	catch(e)
	        	{
	        		alert(e);
	        	}
//	        }
	        errorMsgDiv = document.getElementById(e.id+"_errorMessage");
        }
    }
}

function clearErrorLabels(form) {
    // set all labels back to the normal class
//	alert("clearErrorLabels");
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++) {
        var e = elements[i];
        var cells = e.parentNode.parentNode.cells;
        if (cells && cells.length >= 2) {
            var label = cells[0];
            if (label) {
                label.setAttribute("class", "tdLabel");
                label.setAttribute("className", "tdLabel"); //ie hack cause ie does not support setAttribute
            }
        }
        //if the element is a checkbox, then will have to take special measures for that
        var chkbxLabel = document.getElementById(e.id+"_checkboxLabel");
        if (chkbxLabel)
        {
			chkbxLabel.className = "checkboxLabel";
        }
    }

}

function addError(e, errorText) {
    try {
        // clear out any rows with an "errorFor" of e.id
//    	alert("addError");
    	var elementContainer = e.parentNode;
//    	alert(elementContainer);
    	var errorMsgDiv = document.createElement("div");
    	
    	elementContainer.appendChild(errorMsgDiv);
    	
    	errorMsgDiv.id = e.id+"_errorMessage";
    	errorMsgDiv.setAttribute("class", "errorMessage");
    	errorMsgDiv.setAttribute("className", "errorMessage"); //ie hack cause ie does not support setAttribute
    	
    	errorMsgDiv.innerHTML = errorText;
    	
    	
        // updat the label too
        if (e.type.toLowerCase() == "checkbox")
        {
        	var label = document.getElementById(e.id+"_checkboxLabel");
        	label.setAttribute("class", "checkboxErrorLabel");
        	label.setAttribute("className", "checkboxErrorLabel"); //ie hack cause ie does not support setAttribute
        }
        else
        {
        	var label = elementContainer.parentNode.cells[0];//.getElementsByTagName("label")[0];
        	label.setAttribute("class", "errorLabel");
        	label.setAttribute("className", "errorLabel"); //ie hack cause ie does not support setAttribute
        }
        e.focus();
        
    } catch (e) {
        alert(e);
    }
}