/* Form Validation */

function FV() {}

FV.prototype = {

	YD: YAHOO.util.Dom,
	YE: YAHOO.util.Event,
	YA: YAHOO.util.Anim,
	YC: YAHOO.util.Connect,
	strength: -1,
	uidcount: 0,
	uids: {},
	codes: { 	password: 'Please enter a valid password',
						retypepassword: 'Passwords do not match',
						retypepassword2: 'Invalid password',
						name: 'Bitte gültigen Namen angeben',
						email: 'Bitte gültige Email Adresse eingeben',
						telefon: 'Bitte gültige Telefonnummer eingeben',
						strasse: 'Bitte gültige Strasse eingeben',
						plz: 'Bitte gültige Postleitzahl eingeben',
						stadt: 'Bitte gültige Stadt angeben',
						land: 'Bitte gültigs Land angeben',
						message: 'Bitte geben Sie eine Nachricht ein',
						userid: 'Sorry that user ID is not available',
						userid2: 'Sorry that user ID is too short',
						gender: 'Please select a gender',
						country: 'Please select a country'	},
	allow_pw_validation: true,
	
	init: function() {
		this.YE.on(this.YD.get('password'), 'keyup', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.validate('password_st', this.value, 'password');
			}
		});
		this.YE.on(this.YD.get('password'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('password', this.value, 'password'), 'password', FV.codes.password);
			}
		});
		this.YE.on(this.YD.get('password'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16) && this.value.length > 0) {
				FV.validate('password', this.value, 'password', true);
			}
		});
		this.YE.on(this.YD.get('retypepassword'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.validate('password2', this.value, 'retypepassword');
			}
		});
		this.YE.on(this.YD.get('retypepassword'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16) && this.value.length > 0) {
				FV.validate('password2', this.value, 'retypepassword');
			}
		});
		this.YE.on(this.YD.get('name'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'name'), 'name', FV.codes.name);
			}
		});
		this.YE.on(this.YD.get('email'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('email', this.value, 'email'), 'email', FV.codes.email);
			}
		});
		this.YE.on(this.YD.get('telefon'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('telefon', this.value, 'telefon'), 'telefon', FV.codes.telefon);
			}
		});
		this.YE.on(this.YD.get('strasse'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('strasse', this.value, 'strasse'), 'strasse', FV.codes.strasse);
			}
		});
		this.YE.on(this.YD.get('plz'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('plz', this.value, 'plz'), 'plz', FV.codes.plz);
			}
		});
		this.YE.on(this.YD.get('stadt'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('stadt', this.value, 'stadt'), 'stadt', FV.codes.stadt);
			}
		});
		this.YE.on(this.YD.get('land'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('land', this.value, 'land'), 'land', FV.codes.land);
			}
		});
		this.YE.on(this.YD.get('message'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('message', this.value, 'message'), 'message', FV.codes.message);
			}
		});
		this.YE.on(this.YD.get('userid'), 'blur', function(event) {
			clearTimeout(FV.ut);
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.validate('userid', this.value, 'userid');
			}
		});
		this.YE.on(this.YD.get('userid'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16) && this.value.length > 0) {
				clearTimeout(FV.ut);
				FV.ut = setTimeout('FV.validate(\'userid\', \''+this.value+'\', \'userid\');', 1000);
			}
		});
		this.YE.on(this.YD.get('gender'), 'change', function() {
			FV.validate('select', this.options.selectedIndex, 'gender', true);
		});
		this.YE.on(this.YD.get('gender'), 'blur', function() {
			FV.validate('select', this.options.selectedIndex, 'gender', true);
		});
		this.YE.on(this.YD.get('country'), 'change', function() {
			FV.validate('select', this.options.selectedIndex, 'country', true);
		});
		this.YE.on(this.YD.get('country'), 'blur', function() {
			FV.validate('select', this.options.selectedIndex, 'country', true);
		});
	},
	
	validate: function(t, v, n, sl) {
		switch(t) {
			case 'name':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'email':
				var r = new RegExp("[a-zA-Z0-9+%-._]+@[a-zA-Z0-9.\\-_]+\\.[a-zA-Z]{2,4}","g");
				break;
			case 'telefon':
				var r = new RegExp("^[0-9]","g");
				break;
			case 'strasse':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'plz':
				var r = new RegExp("^[0-9]","g");
				break;
			case 'stadt':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'land':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'message':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'password_st':
				var cb = {
					success: function(o) {
						FV.showStrength(o.responseText, n, v);
					},
					failure: function(o) {
						FV.showStatus(false, n, o.responseText);
					}
				};
				this.YC.asyncRequest('GET','includes/validate.php?pass='+encodeURIComponent(v), cb);
				break;
			case 'password':
				if (v.length >= 5) {
					var r = new RegExp("([^a-zA-Z]+)","g");
				}
				if (sl) {
					var p1 = this.YD.get(n).value;
					var p2 = this.YD.get('retypepassword').value;
					if (p1 != p2) {
						this.resetField('retypepassword');
					}
				}
				break;
			case 'password2':
				var p1 = this.YD.get('password').value;
				var p2 = this.YD.get(n).value;
				if (v.length >= 5) {
					var r = new RegExp("([^a-zA-Z]+)","g");
					if (r.exec(v)) {
						if (p1 == p2) {
							this.showStatus(true, n);
							return true;						 
						}else{
							this.showStatus(false, n, FV.codes.retypepassword);
							return false;
						}
					}else{
						this.showStatus(false, n, FV.codes.retypepassword2);
						return false;
					}
				}else{
					this.showStatus(false, n, FV.codes.retypepassword2);
					return false;
				}
				break;
			case 'userid':
				var r = new RegExp("^[a-zA-Z0-9_\S@\.]{5,}","g");
				if (r.exec(v)) {
					if (this.uidcount < 2) {
						if (!sl) { this.uidcount++; }
						this.uids[v] = -1;
						this.showStatus(false, n, this.codes.userid);
						return false;
					}else{
						if (this.uids[v] != -1 || !this.uids[v]) {
							this.uids[v] = true;
							this.showStatus(true, n);
							return true;
						}else{
							this.showStatus(false, n, this.codes.userid);
							return false;
						}
					}
				}else{
					this.uids[v] = -1;
					this.showStatus(false, n, this.codes.userid2);
					return false;
				}
				break;
			case 'select':
				if (this.YD.get(n).options[v].value != -1) {
					if (sl) {
						this.showStatus(true, n);
					}
					return true;
				}else{
					this.showStatus(false, n, this.codes.gender);
					return false;
				}
				break;

		}
		
		if (r) {
			if (r.exec(v)) {
				return true;
			}else{
				return false;
			}
		}

	},
	
	showStatus: function(s, id, m) {
		FV.YD.get('status-'+id).className = 'l';
		FV.YD.setStyle('status-'+id, 'opacity', 1);
		FV.YD.get('status-'+id).innerHTML = '';
//		FV.YD.get('lbl-'+id).className = '';
		FV.YD.get(id).className = '';
		clearTimeout(FV.YD.get('status-'+id).statim);
		FV.YD.get('status-'+id).statim = setTimeout(function() {
			if (s) {
				FV.YD.get('status-'+id).className = 's';
				if (FV.YD.get('strength-'+id)) {
					FV.YD.get('strength-'+id).style.display = 'block';
				}
			}else{
				FV.YD.setStyle('status-'+id, 'opacity', 1);
//				FV.YD.get('lbl-'+id).className = 'f';
				FV.YD.get('status-'+id).className = 'f';
				FV.YD.get(id).className = 'f';
				FV.YD.get('status-'+id).innerHTML = m;
			}
		}, 500);
		return s;
	},
	
	showStrength: function(s, id, v) {

		this.strength = (s != 'fail')?parseInt(s):-1;
		var m1 = this.YD.get('m1');
		var m2 = this.YD.get('m2');
		var m3 = this.YD.get('m3');
		
		if (this.strength > -1) {
			
			m1.innerHTML = '';
			m1.style.backgroundColor = '';
			m2.innerHTML = '';
			m2.style.backgroundColor = '';
			m3.innerHTML = '';
			m3.style.backgroundColor = '';
	
			switch(this.strength) {
				case 0:
				case 1:
				default: 
					m1.style.backgroundColor = '#ff3333';
					m1.innerHTML = 'Weak';
					break;
				case 2:
				case 3:
					m1.style.backgroundColor = '#ffcc33';
					m2.style.backgroundColor = '#ffcc33';
					m2.innerHTML = 'Fair';
					break;
				case 4:
					m1.style.backgroundColor = '#99ff66';
					m2.style.backgroundColor = '#99ff66';
					m3.style.backgroundColor = '#99ff66';
					m3.innerHTML = 'Strong';
					break;
			}
		//	this.showStatus(true, id);
		}else{
//			this.showStatus(false, id, this.codes.password);
	//		this.YD.get('strength-'+id).style.display = 'none';
			m1.style.backgroundColor = '';
			m2.style.backgroundColor = '';
			m3.style.backgroundColor = '';
			m2.innerHTML = 'Strength';
		}
},
		
	getType: function(form) {
		var e = 0;

		for(i=0;i<form.length;i++) {
				switch(form[i].id) {
					case 'name':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.name)
							e++;
						}			
						break;
					case 'email':
						if (!this.validate('email', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.email)
							e++;
						}						
						break;
					case 'telefon':
						if (!this.validate('telefon', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.telefon)
							e++;
						}						
						break;
					case 'strasse':
						if (!this.validate('strasse', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.strasse)
							e++;
						}						
						break;
					case 'plz':
						if (!this.validate('plz', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.plz)
							e++;
						}						
						break;
					case 'stadt':
						if (!this.validate('stadt', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.stadt)
							e++;
						}						
						break;
					case 'land':
						if (!this.validate('land', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.land)
							e++;
						}						
						break;
					case 'message':
						if (!this.validate('message', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.message)
							e++;
						}						
						break;
					case 'gender':
						if (!this.validate('select', form[i].options.selectedIndex, form[i].id)) {
							e++;
						}						
						break;
					case 'country':
						if (!this.validate('select', form[i].options.selectedIndex, form[i].id, true)) {
							e++;
						}						
						break;
					case 'userid':
						if (this.uidcount < 2) {
							this.uidcount--;
						}
						if (!this.validate('userid', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.userid)
							e++;
						}						
						break;
					case 'password':
						if (!this.validate('password', form[i].value, form[i].id)) {
//							this.validate('password', form[i].value, form[i].id);
							this.showStatus(false, 'password', FV.codes.password);
							e++;
						}else{
							this.showStatus(true, 'password', FV.codes.password);
						}
						break;
					case 'retypepassword':
						if (!this.validate('password2', form[i].value, form[i].id)) {
	//						this.validate('password2', form[i].value, form[i].id);
							e++;
						}
						break;
				}
		}
		
		if (e == 0) {
			this.YD.setStyle(this.YD.get('pg-error'), 'display', 'none');
			return true;
		}else{
			this.YD.setStyle(this.YD.get('pg-error'), 'display', 'block');
			return false;
		}

	},
	
	resetField: function(id) {
		var field = this.YD.get(id);
		var status = this.YD.get('status-'+ id);
		field.className = '';
		field.value = '';
		status.className = '';
		status.innerHTML = '';
	},

	submit: function(form) {
		return FV.getType(form);
	}
	
	
	
};

var FV = new FV();
FV.init();