document.observe('dom:loaded', function() {

	$$('#card-form input').each(function(elm) {
		elm.observe('focus', function(event) {
			if(elm.title == elm.value) {
				elm.value = '';
			}
			elm.up().addClassName('focus');
			}).observe('blur', function (event) {
				if(elm.value == '') {
					elm.value = elm.title;
				}
				elm.up().removeClassName('focus');
			});
		});

		var prenume = new LiveValidation('prenume', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			prenume.add( Validate.Presence, { failureMessage: "Completeaza prenumele !" } );
			prenume.add( Validate.Format, { pattern: /(^[a-zA-Z-\.\, ]+$)/, failureMessage: "Prenume invalid !" } );

		var nume = new LiveValidation('nume', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			nume.add( Validate.Presence, { failureMessage: "Completeaza numele !" } );
			nume.add( Validate.Format, { pattern: /(^[a-zA-Z-\.\, ]+$)/, failureMessage: "Nume invalid !" } );

/*		var cnp = new LiveValidation('cnp', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			cnp.add( Validate.Presence, { failureMessage: "Completeaza CNP !" });
			cnp.add( Validate.Numericality, { notANumberMessage: "CNP invalid !" });
			cnp.add( Validate.Length, { is: 13, wrongLengthMessage: "CNP-ul necesita 13 caractere !" } );
			cnp.add( Validate.Custom, { against: function (value, args) {
				var date	= DateFormat.parse(value.substring(5, 7) + '-' + value.substring(3, 5) + '-' + value.substring(1, 3));
				var now		= new Date();

				var age		= now.getFullYear() - date.getFullYear();
				var months	= now.getMonth()	- date.getMonth();
				var days	= now.getDate()		- date.getDate();			

				if (months < 0) {
					age -= 1;
					} else if (months == 0) {

						if (days < 0) {
							age -= 1;
							} else if (days > 0) {
								age += 1;
								}

						} else {
							age += 1;
							}	

				return (18 <= age && age <= 24);
				}, failureMessage: 'Trebuie sa ai intre 18 si 24 de ani !' } );*/
			
			var cnp = new LiveValidation('cnp', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
		cnp.add( Validate.Presence, { failureMessage: "completati CNP" });
		cnp.add( Validate.Numericality, { notANumberMessage: "CNP invalid" });
		cnp.add( Validate.Length, { is: 13, wrongLengthMessage: "CNP-ul necesita 13 caractere" } );
		cnp.add( Validate.Custom, { against: function (value, args) {
			if (!DateFormat.parse(value.substring(5, 7) + '-' + value.substring(3, 5) + '-19' + value.substring(1, 3))) {
				return false;
				}
			return true;
			}, failureMessage: 'CNP invalid' } );
		
		
		cnp.add( Validate.Custom, { against: function (value) {
													   
													   
			constanta=new String("279146358279");
			suma=0;
			for(i=0; i<constanta.length; i++) {
			suma=suma+value.charAt(i)*constanta.charAt(i);
			}
			rest=suma%11;
			if((value != 0 && rest <10 && rest == value.charAt(12)) || (value != 0 && rest == 10 && value.charAt(12) == 1) )
			{
				return true;
				}

			return false;
			}, failureMessage: 'CNP invalid' } );
		
		cnp.add( Validate.Custom, { against: function (value, args) {
			var cnpdate	= DateFormat.parse(value.substring(5, 7) + '-' + value.substring(3, 5) + '-19' + value.substring(1, 3));
			var now		= new Date();
			var age		= now.getFullYear() - cnpdate.getFullYear();
			var months	= now.getMonth() - cnpdate.getMonth();
			var days	= now.getDate() - cnpdate.getDate();

			if (age < 18) {
				return false;
				} else if (age == 18) {
					if (months < 0) {
						return false;
						} else if (months == 0) {
							if (days < 0) {
								return false;
								}
							}
					}

			return true;
			}, failureMessage: 'trebuie sa ai peste 18 ani' } );
		
		
		cnp.add( Validate.Custom, { against: function (value, args) {

			var cnpdate	= DateFormat.parse(value.substring(5, 7) + '-' + value.substring(3, 5) + '-19' + value

.substring(1, 3));

			var now		= new Date();

			var age		= now.getFullYear() - cnpdate.getFullYear();

			var months	= now.getMonth() - cnpdate.getMonth();

			var days	= now.getDate() - cnpdate.getDate();

			if (age > 25) {

				return false;

				} else if (age == 25) {

					if (months > 0) {

						return false;

						} else if (months == 0) {

							if (days >= 0) {

								return false;

								}

							}

					}

			return true;

			}, failureMessage: 'trebuie sa ai mai putin de  25 ani' } );




		var email = new LiveValidation('email', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			email.add( Validate.Presence, { failureMessage: "Completeaza emailul !" } );
			email.add( Validate.Email, { failureMessage: "Email invalid !" } );

		var telefon = new LiveValidation('telefon', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			telefon.add( Validate.Presence, { failureMessage: "Completeaza telefonul !" } );
			telefon.add( Validate.Numericality, { notANumberMessage: "Telefon invalid !" });
			telefon.add( Validate.Length, { is: 10, wrongLengthMessage: "Telefonul necesita 10 caractere !" } );
			telefon.add( Validate.Custom, { against: function(value, args) {
			    
			    var firstTwo = value.substring(0, 2);
			    if( firstTwo != '07') {
			         return false;
                }
                
                return true;
                
			    }, failureMessage: 'Telefon invalid!'});
	

		var judet = new LiveValidation('id_judet', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			judet.add( Validate.Presence, { failureMessage: "Alege judetul !" } );
		
		var filiala = new LiveValidation('id_filiala', { onValid: function() { this.addFieldClass(); }, onInvalid: function() { this.addFieldClass(); } });
			filiala.add( Validate.Presence, { failureMessage: "Alege filiala !" } );
	});


