/*
* etnoFramework - PHP framework  Copyright (C) 2009 Radek Krawiec
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
* Redistributions of files must retain the above copyright notice.
*/

/**
* This file contains Javascript function used by validators.
*/

function validateNotEmpty (formId, fieldId)
{
	var field = document.forms[formId][fieldId];
	if (field != null)
	{
		if (field.value != '')
		{
			return true;
		}
		return false;
	}
	return true;
}

function validateRegex (formId,fieldId,regex)
{
	var field = document.forms[formId][fieldId];
	if (field != null)
	{
		return field.value.match(regex)==null?false:true; 
	}
	return true;
}

function validateObjectRegex (formId,fieldId,regex)
{
	var field = document.forms[formId][fieldId];
	if (field != null)
	{
		//Chinese character range
		var rg = new RegExp("^[\u4E00-\uFA29]*$");
		return rg.test(field.value); 
	}
	return true;
}

function validateNotRegex (formId,fieldId,regex)
{
	return !validateRegex (formId,fieldId,regex);
}

function addErrorBottom (formName, fieldName, msg, cssClass, cssStyle, isFieldHidden)
{
	var field = document.forms[formName][fieldName];
	if (field != null)
	{
		/*
		 * It's assumed that the field is placed as a separate row
		 * in td and td elements. Unless it is a hidden field,
		 * in which case it will not be in any tr/td tags.
		 */
		if (!isFieldHidden)
		{
			var prevElem = field.parentNode.parentNode;
			var table = prevElem.parentNode;
		}
		else
		{
			var table = field.parentNode;
			var prevElem = field;
		}
		
		errorRow = document.createElement('tr');
		errorRow.id = 'errorFor_'+fieldName;
		errorCell = document.createElement('td');
		firstCell = document.createElement('td');
		errorSpan = document.createElement('span');
		errorSpan.className = cssClass;
		errorSpan.innerHTML = msg;
		errorCell.appendChild(errorSpan);
		errorRow.appendChild(firstCell);
		errorRow.appendChild(errorCell);
		insertAfter(table,errorRow,prevElem);
	}
}

function clearErrorBottom (formName, fieldName)
{
	var field = document.forms[formName][fieldName];
	var errorRow = document.getElementById('errorFor_'+fieldName);
	if (errorRow != null)
	{
		var parent = errorRow.parentNode;
		parent.removeChild(errorRow);
	}
}

function insertAfter(parent, newElement, referenceElement)
{
	parent.insertBefore(newElement, referenceElement.nextSibling);
} 