/*
 * $Id: validation.js,v 1.4 2011/03/08 13:34:42 hcu Exp $
 *
 * 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 addClassName(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1))
		return;
	ele.className += (ele.className ? " " : "") + className;
}

function removeClassName(ele, className)
{
	if (!ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) == -1))
		return;
	ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
}

function clearErrors(form) {
    // set all labels back to the normal class
    
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++) {
      var e = elements[i];
      
      var container = $(e.id + "-container");
      var label = $(e.id + "-label");
        
    	if(container)	   	
    		removeClassName(container, 'validatorInvalidState');
    		
    	if(label)	   	
    		removeClassName(label, 'validatorInvalidState');
    		
    	if(e)	   	
    		removeClassName(e, 'validatorInvalidState');
    }
}

function clearActionErrors(errorMsgDiv) {
  $(errorMsgDiv).innerHTML = "";
  $(errorMsgDiv).style.display = "none";
  /*$(errorMsgDiv).setAttribute("style", "display:none;");*/
}

function addActionError(message, errorMsgDiv) {
  $(errorMsgDiv).innerHTML = $(errorMsgDiv).innerHTML + message + "<br/>";
}

/*
function addError(e, errorText) {
             
    try {
                  	
    	var container = $(e + ".container");    

   	  $(e + '.errorMessage').innerHTML = errorText; 
  	  addClassName(container, 'validatorInvalidState');    	
    	
    } catch (error) {
        alert(e + "  " + errorText + " " + error);
    }
}*/

function hightlightErrorFields(errors){
	if(errors.fieldErrors) {
    var containerId = new String();
    for(var fieldName in errors.fieldErrors) {              
      for(var i = 0; i < errors.fieldErrors[fieldName].length; i++) {                   
    		containerId = fieldName.replace(".", "-")+"-container";
    		containerId = containerId.replace(".", "-");
      	if($(containerId)){
      		$(containerId).addClassName("validatorInvalidState");
      	}
      	labelId = fieldName.replace(".", "-")+"-label";
    		labelId = labelId.replace(".", "-");
      	if($(labelId)){
      		$(labelId).addClassName("validatorInvalidState");
      	}
      	
      	fieldId = fieldName.replace(".", "-");
    		fieldId = fieldId.replace(".", "-");
      	if($(fieldId)){
      		$(fieldId).addClassName("validatorInvalidState");
      	}
      }
    }
  }
}

