/*
 * $Id: validation.js 592047 2007-11-05 15:28:50Z musachy $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function clearErrorMessages(form) {
	clearErrorMessagesCSS(form);
}

function clearErrorMessagesCSS(form) {
    firstFieldErrorPosition = null;
    // clear out any rows with an "errorFor" attribute
    var spans = form.getElementsByTagName("span");
    var spansToClear = new Array();

    for(var i = 0; i < spans.length; i++) {
        var span = spans[i];
		var tmp = span.getAttribute("class");
		if (tmp == null){
			tmp = span.getAttribute("className");
		}
        if (tmp == "icon-msg") {
        	span.innerHTML = "";
        }
    }
    
}

function clearMyErrorMessages(form, fieldname) {
	clearErrorMessagesCSS(form, fieldname);
}

function clearMyErrorMessagesCSS(form, fieldname) {
    firstFieldErrorPosition = null;
    var element = document.getElementsByName(fieldname)[0];
	// get all spans within field divs
    var spans = element.parentNode.getElementsByTagName("span");
    var found = false;
    for(var i = 0; i < spans.length; i++) {
        var span = spans[i];
		
		var tmp = span.getAttribute("class");
		if (tmp == null){
			tmp = span.getAttribute("className");
		}
        if (tmp == "icon-msg") {
		
		
        /*if ((span.getAttribute("class") == "icon-msg") || (span.getAttribute("className") == "icon-msg")) {*/
			// error span found
        	span.innerHTML = "";
        	found = true;
        }
    }    
    if (!found) {
    	var spans = element.parentNode.parentNode.getElementsByTagName("span");
	    for(var i = 0; i < spans.length; i++) {
	        var span = spans[i];
	        
			var tmp = span.getAttribute("class");
			if (tmp == null){
				tmp = span.getAttribute("className");
			}
			if (tmp == "icon-msg") {
			
			/*if ((span.getAttribute("class") == "icon-msg") || (span.getAttribute("className") == "icon-msg")) {*/
	        	span.innerHTML = "";
	        }
	    }    
    }
    
}

function clearErrorDivs(form) {
    clearErrorDivsCSS(form);
}

function clearErrorDivsCSS(form) {
    // set all labels back to the normal class
    var divs = form.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        var div = divs[i];
        if (div) {
        	var currentClass = div.getAttribute("class");
			if (currentClass == null) {
				currentClass = div.getAttribute("className");
			}

            if(currentClass != null && currentClass.indexOf("error") > 0){
            	currentClass = currentClass.replace("error", "");
            	div.setAttribute("class", currentClass);
				div.setAttribute("className", currentClass); //ie hack cause ie does not support setAttribute
            }
        }
    }
}

function clearMyErrorDivs(form, fieldname) {
    clearErrorDivsCSS(form, fieldname);
}

function clearMyErrorDivsCSS(form, fieldname) {
    // set all labels back to the normal class
    var divs = form.getElementsByTagName("div");
    var element = document.getElementsByName(fieldname)[0];
    for (var i = 0; i < divs.length; i++) {
        var div = divs[i];
        if (div) {
        	var currentClass = div.getAttribute("class");
			if (currentClass == null) {
				currentClass = div.getAttribute("className");
			}
		
        	var isInside = (div == element.parentNode) || (div == element.parentNode.parentNode);
            if(isInside && currentClass != null && currentClass.indexOf("error") > 0){
            	currentClass = currentClass.replace("error", "");
            	div.setAttribute("class", currentClass);
				div.setAttribute("className", currentClass); //ie hack cause ie does not support setAttribute
            }
        }
    }
}

function addError(e, errorText) {
    addErrorCSS(e, errorText);
}

var firstFieldErrorPosition = null;
function addErrorCSS(e, errorText) {
    try {
        if (!e)
            return; //ignore errors for fields that are not in the form
        var elem = (e.type ? e : e[0]); //certain input types return node list, while we single first node. I.e. set of radio buttons.
        var enclosingDiv = elem.parentNode; // find wwgrp div/span

        //try to focus on first field
        var fieldPos = findFieldPosition(elem);
        if (fieldPos != null && (firstFieldErrorPosition == null || firstFieldErrorPosition > fieldPos)) {
            firstFieldErrorPosition = fieldPos;
        }

        if (!enclosingDiv) {
            return;
        }

		var oldErrorSpan = getErrorSpan(enclosingDiv);        
        if (oldErrorSpan == null) {
        	oldErrorSpan = getErrorSpan(enclosingDiv.parentNode);
        }
		
    	// set msg
    	if (oldErrorSpan != null) {
			var newErrorText = errorText;
    		if (oldErrorSpan.children.length > 0) {
				// add another error
				var indexP = oldErrorSpan.innerHTML.indexOf("<p>");
				if (indexP == -1) {
					indexP = oldErrorSpan.innerHTML.indexOf("<P>");
				}
				var indexPEnd = oldErrorSpan.innerHTML.indexOf("</p>");
				if (indexPEnd == -1) {
					indexPEnd = oldErrorSpan.innerHTML.indexOf("</P>");
				}
				if (indexP > -1 && indexPEnd > -1) {
					var newErrorText = oldErrorSpan.innerHTML.substring(indexP + 3, indexPEnd);
					if (newErrorText.indexOf(errorText) == -1) {
						// add error text
						newErrorText = newErrorText + ", " + errorText;
					}
				}
			}
			oldErrorSpan.innerHTML = "<div class='popup'><div class='bg-top'><p>" + newErrorText + "</p></div><div class='bg-btm'></div></div>";
    	}
    	// set class
    	var currentClass = getCurrentClass(enclosingDiv);
    	if (currentClass != null) {
			// remove checked
			var index = currentClass.indexOf("checked");
			if (index > -1) {
				var prefix = currentClass.substring(0, index);
				var suffix = currentClass.substring(index + 7, currentClass.length);
				currentClass = prefix + suffix;
			}
			
			// add error
			index = currentClass.indexOf("error");
			if (index == -1) {
				currentClass = currentClass + " error";
			}
			enclosingDiv.setAttribute("class", currentClass);
			enclosingDiv.setAttribute("className", currentClass); //ie hack cause ie does not support setAttribute
    	}

		// set parent's class
    	var currentClass = getCurrentClass(enclosingDiv.parentNode);	
		/*
    	if (currentClass != null && (currentClass.indexOf("input") > -1 || currentClass.indexOf("select") > -1 || currentClass.indexOf("holder") > -1)) {
		*/
		if (currentClass != null && (currentClass.indexOf("textarea") > -1 || currentClass.indexOf("size07") > -1)) {
			// remove checked
			var index = currentClass.indexOf("checked");
			if (index > -1) {
				var prefix = currentClass.substring(0, index);
				var suffix = currentClass.substring(index + 7, currentClass.length);
				currentClass = prefix + suffix;
			}
			
			// add error
			index = currentClass.indexOf("error");
			if (index == -1) {
				currentClass = currentClass + " error";
			}
			enclosingDiv.parentNode.setAttribute("class", currentClass);
			enclosingDiv.parentNode.setAttribute("className", currentClass); //ie hack cause ie does not support setAttribute
    	}
    } catch (err) {
        // alert("An exception occurred: " + err.name + ". Error message: " + err.message);
    }
}

function nothing() {
	// do nothing
}

function getErrorSpan(element) {
	var ar = element.children;
    for (var i = 0; i< ar.length; i++) {
    	var ch = ar[i];
		var tmp = ch.getAttribute("className");
		if (tmp == null){
			tmp = ch.getAttribute("class");
		}
		if (tmp != null && tmp.indexOf("icon-msg") > -1) {
    		return ch;
    	}
    }
    return null;
}

function getCurrentClass(element) {
	var currentClass = element.getAttribute("className");
	if (currentClass == null){ 
		currentClass = element.getAttribute("class");
	}
	if (currentClass == null){ 
		currentClass = element.className;
	}
	return currentClass;
}

function findErrorNode(element) {
	if (element == null || element.nextSibling == null) {
		return null;
	}
	return element.nextSibling.nextSibling;
}

function findWWGrpNode(elem) {
    while (elem.parentNode) {
        elem = elem.parentNode;
        
        if (elem.tagName)
            return elem;
    }
    return null;
}

function findWWCtrlNode(enclosingDiv) {
    for(var elem in enclosingDiv.getElementsByTagName("div")) {
        if (elem.className && elem.className.match(/(wwlbl|wwctrl)/))
            return elem
    }
    for(var elem in enclosingDiv.getElementsByTagName("span")) {
        if (elem.className && elem.className.match(/(wwlbl|wwctrl)/))
            return elem
    }
    return enclosingDiv.getElementsByTagName("span")[0];
}

//find field position in the form
function findFieldPosition(elem) {
    if (!elem.form) {
        alert("no form for " + elem);
    }
    
    var form = elem.form;
    for (i = 0; i < form.elements.length; i++) { 
        if (form.elements[i].name == elem.name) {
            return i;
        }
    }
    return null;
}

//focus first element
var StrutsUtils_showValidationErrors = StrutsUtils.showValidationErrors;
StrutsUtils.showValidationErrors = function(form, errors) {
    StrutsUtils_showValidationErrors(form, errors);
    //if (firstFieldErrorPosition != null && form.elements[firstFieldErrorPosition].focus) {
    //    form.elements[firstFieldErrorPosition].focus();
    //}
}
