/* 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: { 	message: 'Please enter your message'}, 
	allow_pw_validation: true,
	
	init: function() {
		
		this.YE.on(this.YD.get('message'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'message'), 'message', FV.codes.message);
			}
		});
		this.YE.on(this.YD.get('message'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'message'), 'message', FV.codes.message);
			//}
		});
		this.YE.on(this.YD.get('message'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'message'), 'message', FV.codes.message);
			}
		});
		
	},
	
	validate: function(t, v, n, sl) {
		switch(t) {
			case 'name':
				var r = new RegExp("^[a-zA-Z0-9\-]","g");
				break;

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

	},
	
	showStatus: function(s, id, m) {
		//FV.YD.get('status-'+id).className = 'l';
		// Have a loading
		FV.YD.get('loading-'+id).style.display = '';
		
		FV.YD.setStyle('status-'+id, 'opacity', 1);
		FV.YD.get('status-'+id).innerHTML = '';
		FV.YD.get('warning-'+id).style.display = 'none';
		FV.YD.get('check-'+id).style.display = 'none';
		
//		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';
				// Show success image.
				FV.YD.get('check-'+id).style.display = '';
				// Hide warning image.
				FV.YD.get('warning-'+id).style.display = 'none';
				// Hide the loading image.
				FV.YD.get('loading-'+id).style.display = 'none';
				// Hide the status message.
				FV.YD.get('status-'+id).style.display = 'none';
				
				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';
				// Show the fail image.
				FV.YD.get('warning-'+id).style.display = '';
				// Hide the check image
				FV.YD.get('check-'+id).style.display = 'none';
				// Hide the loading image.
				FV.YD.get('loading-'+id).style.display = 'none';
				
				//FV.YD.get(id).className = 'f';
				FV.YD.get('status-'+id).style.display = '';
				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:
					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;
				default: 
			}
	//		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 'message':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.message)
							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();
