/* 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: 'Please provide your name',
						position: 'Please provide your position',
						email: 'Please enter a valid email address',
						postalcode: 'Please enter a valid postal code',
						company: 'Please provide your company name',
						telephone: 'Please provide your phone number',
						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('name'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 30 && this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'name'), 'name', FV.codes.name);
			}
		});
		this.YE.on(this.YD.get('position'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 30 && this.value.length > 0) {
				FV.showStatus(FV.validate('position', this.value, 'position'), 'position', FV.codes.position);
			}
		});
		this.YE.on(this.YD.get('company'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 30 && this.value.length > 0) {
				FV.showStatus(FV.validate('company', this.value, 'company'), 'company', FV.codes.company);
			}
		});
		this.YE.on(this.YD.get('telephone'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 30 && this.value.length > 0) {
				FV.showStatus(FV.validate('telephone', this.value, 'telephone'), 'telephone', FV.codes.telephone);
			}
		});
		this.YE.on(this.YD.get('email'), 'blur', function(event) {
			var e = event || window.event;
			if (e.keyCode != 30 && 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 != 30 && this.value.length > 0) {
				FV.showStatus(FV.validate('zip', this.value, 'postalcode'), 'postalcode', FV.codes.postalcode);
			}
		});
	},
	
	validate: function(t, v, n, sl) {
		switch(t) {
			case 'name':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'position':
				var r = new RegExp("^[a-zA-Z\-]","g");
				break;
			case 'company':
				var r = new RegExp("^[a-zA-Z0-9\-]","g");
				break;
			case 'telephone':
				var r = new RegExp("^[a-zA-Z0-9\-]","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 '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 'position':
						if (!this.validate('position', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.position)
							e++;
						}			
						break;
					case 'company':
						if (!this.validate('company', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.company)
							e++;
						}			
						break;
					case 'telephone':
						if (!this.validate('telephone', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.telephone)
							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 '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();
