/* 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: { 	pass: error_1,
						ver_pass: error_2,
						ver_pass2: error_3,
						first_name: error_4,
						last_name: error_5,
						email: error_6,
						postalcode: 'Please enter a valid postal code',
						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('pass'), 'keyup', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.validate('pass_st', this.value, 'pass');
			}
		});
		this.YE.on(this.YD.get('pass'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('pass', this.value, 'pass'), 'pass', FV.codes.pass);
			}
		});
		this.YE.on(this.YD.get('pass'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16) && this.value.length > 0) {
				FV.validate('pass', this.value, 'pass', true);
			}
		});
		this.YE.on(this.YD.get('ver_pass'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.validate('pass2', this.value, 'ver_pass');
			}
		});
		this.YE.on(this.YD.get('ver_pass'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16) && this.value.length > 0) {
				FV.validate('pass2', this.value, 'ver_pass');
			}
		});
		this.YE.on(this.YD.get('first_name'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'first_name'), 'first_name', FV.codes.first_name);
			}
		});
		this.YE.on(this.YD.get('last_name'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'last_name'), 'last_name', FV.codes.last_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('postalcode'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 9 && this.value.length > 0) {
				FV.showStatus(FV.validate('zip', this.value, 'postalcode'), 'postalcode', FV.codes.postalcode);
			}
		});
		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 'zip':
				var r = new RegExp("[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? *[0-9][ABD-HJLN-UW-Z]{2}", "i");
				break;
			case 'pass_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',site_root+'/validate.php?pass='+encodeURIComponent(v), cb);
				break;
			case 'pass':
				if (v.length >= 5) {
					var r = new RegExp("([a-zA-Z0-9+%-._]+)","g");
				}
				if (sl) {
					var p1 = this.YD.get(n).value;
					var p2 = this.YD.get('ver_pass').value;
					if (p1 != p2) {
						this.resetField('ver_pass');
					}
				}
				break;
			case 'pass2':
				var p1 = this.YD.get('pass').value;
				var p2 = this.YD.get(n).value;
				if (v.length >= 5) {
					var r = new RegExp("([a-zA-Z0-9+%-._]+)","g");
					if (r.exec(v)) {
						if (p1 == p2) {
							this.showStatus(true, n);
							return true;
						}else{
							this.showStatus(false, n, FV.codes.ver_pass);
							return false;
						}
					}else{
						this.showStatus(false, n, FV.codes.ver_pass2);
						return false;
					}
				}else{
					this.showStatus(false, n, FV.codes.ver_pass2);
					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) {
        return true;
		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.pass);
	//		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 'first_name':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.first_name)
							e++;
						}
						break;
					case 'last_name':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.last_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 '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 'postalcode':
						if (!this.validate('zip', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.postalcode)
							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 'pass':
						if (!this.validate('pass', form[i].value, form[i].id)) {
//							this.validate('pass', form[i].value, form[i].id);
							this.showStatus(false, 'pass', FV.codes.pass);
							e++;
						}else{
							this.showStatus(true, 'pass', FV.codes.pass);
						}
						break;
					case 'ver_pass':
						if (!this.validate('pass2', form[i].value, form[i].id)) {
	//						this.validate('pass2', 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();
