/*
   Copyright (c) 2007-2012, GlassDoor. All rights reserved.

   This software is an unpublished work subject to a confidentiality agreement
   and protected by copyright and trade secret law. Unauthorized copying,
   redistribution or other use of this work is prohibited. All copies must
   retain this copyright notice. Any use or exploitation of this work without
   authorization could subject the perpetrator to criminal and civil liability.

   The information in this software is subject to change without notice
   and should not be construed as a commitment by Glassdoor.

   The above copyright notice does not indicate actual or intended publication 
   of this source code.

   $Rev:: 3527                                           $: (current file revision)
   $Date:: 2008-03-20 12:22:19 -0700 (Thu, 20 Mar 2008)  $: (date of last file modification)
   $Author:: coyote                                      $: (person who last modified this file)
*/


/*--------------------------------------------------------------------------------
 * Javascript used by Struts Validation
 *--------------------------------------------------------------------------------*/

function clearActionErrors() {
	var		actionErrorBlock = document.getElementById('actionErrorBlock');

	if ((typeof actionErrorBlock === 'object') && (actionErrorBlock !== null)) {
		actionErrorBlock.style.display = 'none';
	}
}

function clearErrorMessages(form) {
	clearActionErrors();

	// clear out any rows with an 'errorFor' attribute
	var		divs = form.getElementsByTagName('div');
    var		elementsToDelete = [];
    var		i;

    for (i = 0; i < divs.length; i++) {
        var		thisDiv = divs[i];
        var		thisClassName = thisDiv.className;

        if (thisClassName == 'fieldError') {
            elementsToDelete.push(thisDiv);
        }
    }

    // now delete the paragraphsToDelete
	for (i = 0; i < elementsToDelete.length; i++) {
		var		r = elementsToDelete[i];
		var		parent = r.parentNode;

		try {
			parent.removeChild(r);
		}
		catch (e) {
			// ignore.
		}
	}
}

function clearErrorLabels(form) {
	clearActionErrors();

    // set all labels back to the previous class
    var labels = form.getElementsByTagName('label');

    for (var i = 0; i < labels.length; i++) {
		var		label = labels[i];

        if (label && label.className) {
            if ((label.className === 'labelError') || (label.className === 'labelErrorRight')) {
            	if (label.previousClassName) {
					label.className = label.previousClassName;
            	}
            	else {
					label.className = '';
				}
			}
		}
	}
}

function errorExists(errorBlock, errorText) {
	var		exists = false;
	var 	errorMessages = errorBlock.getElementsByTagName('div');
	
	for (var loop = 0; loop < errorMessages.length; loop++) {
		var		thisMessage = errorMessages[loop];
		var		paragraph = thisMessage.getElementsByTagName('p')[0];
		
		if (paragraph) {
			var		thisMessage = paragraph.innerHTML

			/*
			 * Trim the message before comparing.
			 */
			thisMessage = thisMessage.replace(/^\s+/, '');
			thisMessage = thisMessage.replace(/\s+$/, '');

			if (thisMessage === errorText) {
				exists = true;
				break;
			}
		}
	}

	return exists;
}

function flagLabel(field) {
    if (field.id) {
        var     fieldId = field.id;
        var     labels = document.getElementsByTagName('label');
        var     label = null;

        for (var loop = 0; loop < labels.length; loop++) {
            var     currLabel = labels[loop];

            if (currLabel && currLabel.htmlFor && (currLabel.htmlFor === fieldId)) {
                label = currLabel;
                break;
            }
        }

		if (label) {
		    var     alignRight = false;

		    if (label.className &&
		        (label.className === 'labelOptRight') || (label.className === 'labelReqRight')) {
		        alignRight = true;
			}

		    if (label.className) {
		        // Save the previous class name
		        label.previousClassName = label.className;
			}

		    if (alignRight) {
		        label.className = 'labelErrorRight';
			}
		    else {
		        label.className = 'labelError';
			}
		}
	}
}

function addError(field, errorText) {
    try {
        var		ctrlDiv = field.parentNode; // wwctrl_ div or span
        var		enclosingDiv = ctrlDiv.parentNode; // wwgrp_ div
        var		actionErrorBlock = document.getElementById('actionErrorBlock');

		if ((typeof actionErrorBlock === 'object') && (actionErrorBlock !== null)) {
			actionErrorBlock.style.display = 'block';
		}

		if (!ctrlDiv ||
			(ctrlDiv.nodeName !== 'DIV' && ctrlDiv.nodeName !== 'SPAN' && ctrlDiv.nodeName !== 'SELECT') ||
			!enclosingDiv || enclosingDiv.nodeName !== 'DIV') {
			Logger.warn("[gd-struts-validation.js / addError()] improper node structure found for field name '" + field.name + "'");
			return;
		}

        flagLabel(field);

		var		errorBlock = null;
		var		internalDivs = enclosingDiv.getElementsByTagName('div');

		if ((internalDivs !== null) && (internalDivs.length > 0)) {
			for (var loop = 0; loop < internalDivs.length; loop++) {
				var		className = internalDivs[loop].className;

				if (className === 'fieldError') {
					// Found the error block!
					errorBlock = internalDivs[loop];
					break;
				}
			}
		}

		if (!errorBlock) {
			var 	firstDiv = enclosingDiv.getElementsByTagName('div')[0]; // either wwctrl_ or wwlbl_
			var		errorMessageHTML =
						"<div class='errorMessage'>\n" +
						"    <p class='stopIcon'>" +
						"        " + errorText + "\n" +
                        "    </p>" +
						"</div>\n" +
						"<div class='clear'></div>";

			if (!firstDiv) {
				firstDiv = enclosingDiv.getElementsByTagName('span')[0];
			}

	        errorBlock = document.createElement('div');

			errorBlock.className = 'fieldError';

			errorBlock.innerHTML = errorMessageHTML;

	        enclosingDiv.insertBefore(errorBlock, firstDiv);
		}
		else {
			if (!errorExists(errorBlock, errorText)) {
		        var		clearDiv = document.createElement('div');
				var		errorMessageHtml = "<p>" + errorText + "</p>\n";
		        var		errorMessage = document.createElement('div');

				errorMessage.className = 'errorMessage';
				errorMessage.innerHTML = errorMessageHtml;
	
				clearDiv.className = 'clear';

				errorBlock.appendChild(errorMessage);
				errorBlock.appendChild(clearDiv);
			}
		}
    } 
    catch ( exceptionMsg ) {
        alert(exceptionMsg);
    }
}

