/*
    Copyright (C) 2008  Ivo Toby, Internet Voor Ondernemers, http://www.i-v-o.nl / http://syntacticsugar.nl 

    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 3 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, see <http://www.gnu.org/licenses/>.

	Crudder Version : 0.50b
 */

	crudderField.addMethods({
		
		// VALIDATIONS
		mandatory : function(value){
			value = value.replace(' ', '');
			if (value !== ''){
				return true;
			}
			return this.controller.lang.get('validationMandatory');
		},
		
		isemail : function(emailStr){
			var checkTLD=1;
			var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
			var emailPat=/^(.+)@(.+)$/;
			var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
			var validChars="\[^\\s" + specialChars + "\]";
			var quotedUser="(\"[^\"]*\")";
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
			var atom=validChars + '+';
			var word="(" + atom + "|" + quotedUser + ")";
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
			var matchArray=emailStr.match(emailPat);

			if (matchArray==null) {
				return this.controller.lang.get('validationEmail');
			}

			var user=matchArray[1];
			var domain=matchArray[2];

			for (i=0; i<user.length; i++) {
				if (user.charCodeAt(i)>127) {
					return this.controller.lang.get('validationEmail');
				}
			}

			for (i=0; i<domain.length; i++) {
				if (domain.charCodeAt(i)>127) {
					return this.controller.lang.get('validationEmail');
				}
			}
			
			if (user.match(userPat)==null) {
				return this.controller.lang.get('validationEmail');
			}

			var IPArray=domain.match(ipDomainPat);
			
			if (IPArray!=null) {
				for (var i=1;i<=4;i++) {
					if (IPArray[i]>255) {
						return this.controller.lang.get('validationEmail');
			   		}
				}
				return true;
			}
			
			 
			var atomPat=new RegExp("^" + atom + "$");
			var domArr=domain.split(".");
			var len=domArr.length;

			for (i=0;i<len;i++) {
				if (domArr[i].search(atomPat)==-1) {
					return this.controller.lang.get('validationEmail');
			   }
			}

			if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
				return this.controller.lang.get('validationEmail');
			}

			if (len<2) {
				return this.controller.lang.get('validationEmail');
			}
			
			return true;			
			
		}
	
	})