/*
## vBegin
## 
## Copyright 2008, 2009, 2010 ECLYDRE <ewm@eclydre.fr>
## This file is part of Eclydre Web Manager.
## 
## Eclydre Web Manager 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 3 of the License, or
## (at your option) any later version.
## 
## Eclydre Web Manager 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 Eclydre Web Manager.  If not, see <http://www.gnu.org/licenses/>.
## 
## This copyright notice MUST APPEAR in all copies of the script!  
## 
## Eclydre Web Manager v2.0.3
## Dateversion 2010-11-04
## File : ../core//js/jsfunc.validateform.js
## Changelogs : 
## v2.0.2 2010-11-04 Adding multiple required support
## v2.0.2 2010-09-24 Adding call to fonction on ereg
## v2.0.1 2010-09-14 Adding 'not matching' possiblilty on ereg
## ;
## vEnd
*/
/***************************************************************
*
*  Universal validate-form
*
* $Id: jsfunc.validateform.js,v 1.4 2004/04/19 15:25:53 typo3 Exp $
*
*
*
*  Copyright notice
*
*  (c) 1998-2003 Kasper Skaarhoj
*  All rights reserved
*
*  This script is part of the TYPO3 t3lib/ library provided by
*  Kasper Skaarhoj <kasper@typo3.com> together with TYPO3
*
*  Released under GNU/GPL (see license file in tslib/)
*
*  This script 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.
*
*  This copyright notice MUST APPEAR in all copies of this script
***************************************************************/


function validateForm(theFormname,theFieldlist,goodMess,badMess,emailMess)	{
	if (document[theFormname] && theFieldlist)	{
		var index=1;
		var theField = split(theFieldlist, ",", index);
		var msg="";
		var theEreg = '';
		var theEregMsg = '';
		var specialMode = '';
		var tOr = new Array();
		var tIndOr = new Array();
		var tIndOrValue = new Array();
		var indOr = 1;

		while (theField) {
			theEreg = '';
			specialMode = '';

				// Check special modes:
			if (theField == '_EREG')	{	// EREG mode: _EREG,[error msg],[JS ereg],[fieldname],[field Label]
				specialMode = theField;

				index++;
				theEregMsg = unescape(split(theFieldlist, ",", index));
				index++;
				theEreg = unescape(split(theFieldlist, ",", index));
			} else if (theField == '_EMAIL')	{
				specialMode = theField;
			}
			else if (theField == '_OR')	{
				specialMode = theField;
				index++;
				nbOr = split(theFieldlist, ",", index);
				tIndOr[indOr] = new Array();
				i = 0;
				while(i<nbOr)
				{
					index++;
					theOrField = split(theFieldlist, ",", index);
					tOr[theOrField] = indOr;
					tIndOr[indOr][i] = theOrField;
					tIndOrValue[indOr] = new Array();
					i++;
				}
				indOr++;
			}

				// Get real field name if special mode has been set:
			if (specialMode)	{
				index++;
				theField = split(theFieldlist, ",", index);
			}

			index++;
			theLabel = unescape(split(theFieldlist, ",", index));
			theField = unescape(theField);
			if (document[theFormname][theField])	{
				var fObj = document[theFormname][theField];
				var type=fObj.type;
				if (!fObj.type)	{
					type="radio";
				}
				var value="";
				switch(type)	{
					case "text":
					case "password":
					case "hidden":
					case "textarea":
						value = fObj.value;
					break;
					case "select-one":
						if (fObj.selectedIndex>=0)	{
							value = fObj.options[fObj.selectedIndex].value;
						}
					break;
					case "select-multiple":
						var l=fObj.length;
						for (a=0;a<l;a++)	{
							if (fObj.options[a].selected)	{
								 value+= fObj.options[a].value;
							}
						}
					break;
					case "checkbox":
						value = fObj.checked?fObj.value:0;
					break;
					case "radio":
						var l=fObj.length;
						for (a=0; a<l;a++)	{
							if (fObj[a].checked)	{
								value = fObj[a].value;
							}
						}
					break;
					default:
						value=1;
				}

				switch(specialMode)	{
					case "_EMAIL":
						var theRegEx_notValid = new RegExp("(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)", "gi");
						var theRegEx_isValid = new RegExp("^.+\@[a-zA-Z0-9\-\.]+[\.]+([a-zA-Z]{2,4}|[0-9]{1,3})$","");
						if (!theRegEx_isValid.test(value))	{	// This part was supposed to be a part of the condition: " || theRegEx_notValid.test(value)" - but I couldn't make it work (Mozilla Firefox, linux) - Anyone knows why?
							if(tOr[theField]) tIndOrValue[tOr[theField]][theField] = theLabel+' ('+(emailMess ? unescape(emailMess) : 'Email address not valid!')+')';
							else msg+="\n"+theLabel+' ('+(emailMess ? unescape(emailMess) : 'Email address not valid!')+')';
						}
					break;
					case "_EREG":
						//alert(value+" : "+theEreg);
						if(theEreg.substring(0, 9)=='function:')
						{
							// appel d'une fonction JS
							var theRegEx_function = theEreg.substring(9);
							eval("test = "+theRegEx_function+"(value)");
							if (!test)	{
								if(tOr[theField]) tIndOrValue[tOr[theField]][theField] = theLabel+' ('+theEregMsg+')';
								else msg+="\n"+theLabel+' ('+theEregMsg+')';
							}
						}
						else if(theEreg.substring(0, 1)=='!')
						{
							// mode inversé, la condition ne doit pas être rempli
							var theRegEx_isValid = new RegExp(theEreg.substring(1),"");
							if (theRegEx_isValid.test(value))	{
								if(tOr[theField]) tIndOrValue[tOr[theField]][theField] = theLabel+' ('+theEregMsg+')';
								else msg+="\n"+theLabel+' ('+theEregMsg+')';
							}
						}
						else
						{
							var theRegEx_isValid = new RegExp(theEreg,"");
							if (!theRegEx_isValid.test(value))	{
								if(tOr[theField]) tIndOrValue[tOr[theField]][theField] = " "+theLabel+' ('+theEregMsg+')';
								else msg+="\n"+theLabel+' ('+theEregMsg+')';
							}
						}
					break;
					default:
						if (!value ||value=='0')	{
							if(tOr[theField]) tIndOrValue[tOr[theField]][theField] = theLabel;
							else msg+="\n"+theLabel;
						}
				}
			}
			index++;
			theField = split(theFieldlist, ",", index);
		}
		/* traitement des required multiple (OR)*/ 
		for(i=1; i<indOr; i++)
		{
			nbItem = tIndOr[i].length;
			nbVide = 0;
			for(j=0; j<nbItem; j++)
			{
				theField = tIndOr[i][j];
				if( tIndOrValue[tOr[theField]][theField]) nbVide++;
			}
			if(nbVide == nbItem) 
			{
				msg += "\n[";
				for(j=0; j<nbItem; j++)
				{
					theField = tIndOr[i][j];
					msg += (j?"\n|| ":'')+tIndOrValue[tOr[theField]][theField];
				}
				msg += ']';
			}
		}
		/* */
		if (msg)	{
			var theBadMess = unescape(badMess);
			if (!theBadMess)	{
				theBadMess = "You must fill in these fields:";
			}
			theBadMess+="\n";
			alert(theBadMess+msg);
			return false;
		} else {
			var theGoodMess = unescape(goodMess);
			if (theGoodMess)	{
				alert(theGoodMess);
			}
			return true;
		}
	}
}
function split(theStr1, delim, index) {
	var theStr = ''+theStr1;
	var lengthOfDelim = delim.length;
	sPos = -lengthOfDelim;
	if (index<1) {index=1;}
	for (a=1; a<index; a++)	{
		sPos = theStr.indexOf(delim, sPos+lengthOfDelim);
		if (sPos==-1)	{return null;}
	}
	ePos = theStr.indexOf(delim, sPos+lengthOfDelim);
	if(ePos == -1)	{ePos = theStr.length;}
	return (theStr.substring(sPos+lengthOfDelim,ePos));
}

