/* 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: { 	fname: 'Please enter first name',
				lname: 'Please enter last name',
				phone: 'Please enter phone number',
				email: 'Please enter valid email address',
				address: 'Please enter address',
				city: 'Please enter city',
				state: 'Please select state',
				zip: 'Please enter zip code',
				country: 'Please select country',
				island: 'Please select island',
				wedding_date: 'Please enter wedding date',
				guests: 'Please select number of guests', 
				security_code: 'Please enter security code'}, 
	allow_pw_validation: true,
	
	init: function() {
		
		this.YE.on(this.YD.get('fname'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'fname'), 'fname', FV.codes.fname);
			}
		});
		this.YE.on(this.YD.get('fname'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'fname'), 'fname', FV.codes.fname);
			//}
		});
		this.YE.on(this.YD.get('fname'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'fname'), 'fname', FV.codes.fname);
			}
		});
		
		this.YE.on(this.YD.get('lname'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'lname'), 'lname', FV.codes.lname);
			}
		});
		this.YE.on(this.YD.get('lname'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'lname'), 'lname', FV.codes.lname);
			//}
		});
		this.YE.on(this.YD.get('lname'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'lname'), 'lname', FV.codes.lname);
			}
		});
		
		this.YE.on(this.YD.get('phone'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'phone'), 'phone', FV.codes.phone);
			}
		});
		this.YE.on(this.YD.get('phone'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'phone'), 'phone', FV.codes.phone);
			//}
		});
		this.YE.on(this.YD.get('phone'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'phone'), 'phone', FV.codes.phone);
			}
		});
		
		this.YE.on(this.YD.get('email'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('email', this.value, 'email'), 'email', FV.codes.email);
			}
		});
		this.YE.on(this.YD.get('email'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('email', this.value, 'email'), 'email', FV.codes.email);
			//}
		});
		this.YE.on(this.YD.get('email'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('email', this.value, 'email'), 'email', FV.codes.email);
			}
		});
		
		this.YE.on(this.YD.get('address'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'address'), 'address', FV.codes.address);
			}
		});
		this.YE.on(this.YD.get('address'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'address'), 'address', FV.codes.address);
			//}
		});
		this.YE.on(this.YD.get('address'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'address'), 'address', FV.codes.address);
			}
		});
		
		this.YE.on(this.YD.get('city'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'city'), 'city', FV.codes.city);
			}
		});
		this.YE.on(this.YD.get('city'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'city'), 'city', FV.codes.city);
			//}
		});
		this.YE.on(this.YD.get('city'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'city'), 'city', FV.codes.city);
			}
		});
		
		this.YE.on(this.YD.get('state'), 'change', function() {
			FV.validate('select', this.options.selectedIndex, 'state', true);
		});
		this.YE.on(this.YD.get('state'), 'focus', function() {
			FV.validate('select', this.options.selectedIndex, 'state', true);
		});
		
		this.YE.on(this.YD.get('country'), 'change', function() {
			FV.validate('country', this.options.selectedIndex, 'country', true);
		});
		this.YE.on(this.YD.get('country'), 'focus', function() {
			FV.validate('country', this.options.selectedIndex, 'country', true);
		});
		
		this.YE.on(this.YD.get('island'), 'change', function() {
			FV.validate('island', this.options.selectedIndex, 'island', true);
		});
		this.YE.on(this.YD.get('island'), 'focus', function() {
			FV.validate('island', this.options.selectedIndex, 'island', true);
		});
		
		this.YE.on(this.YD.get('guests'), 'change', function() {
			FV.validate('guests', this.options.selectedIndex, 'guests', true);
		});
		this.YE.on(this.YD.get('guests'), 'focus', function() {
			FV.validate('guests', this.options.selectedIndex, 'guests', true);
		});
		
		this.YE.on(this.YD.get('zip'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'zip'), 'zip', FV.codes.zip);
			}
		});
		this.YE.on(this.YD.get('zip'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'zip'), 'zip', FV.codes.zip);
			//}
		});
		this.YE.on(this.YD.get('zip'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'zip'), 'zip', FV.codes.zip);
			}
		});
		
		this.YE.on(this.YD.get('wedding_date'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'wedding_date'), 'wedding_date', FV.codes.wedding_date);
			}
		});
		this.YE.on(this.YD.get('wedding_date'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'wedding_date'), 'wedding_date', FV.codes.wedding_date);
			//}
		});
		this.YE.on(this.YD.get('wedding_date'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'wedding_date'), 'wedding_date', FV.codes.wedding_date);
			}
		});
		
		this.YE.on(this.YD.get('security_code'), 'focus', function() {
			if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'security_code'), 'security_code', FV.codes.security_code);
			}
		});
		this.YE.on(this.YD.get('security_code'), 'blur', function() {
			//if (this.value.length > 0) {
				FV.showStatus(FV.validate('name', this.value, 'security_code'), 'security_code', FV.codes.security_code);
			//}
		});
		this.YE.on(this.YD.get('security_code'), 'keyup', function(event) {
			var e = event || window.event;
			if ((e.keyCode != 9 && e.keyCode != 16)) {
				FV.showStatus(FV.validate('name', this.value, 'security_code'), 'security_code', FV.codes.security_code);
			}
		});
		
	},
	
	validate: function(t, v, n, sl) {
		switch(t) {
			case 'name':
				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 'select':
				if (this.YD.get(n).options[v].value != "") {
					if (sl) {
						this.showStatus(true, n);
					}
					return true;
				}else{
					this.showStatus(false, n, this.codes.state);
					return false;
				}
				break;
			case 'guests':
				if (this.YD.get(n).options[v].value != "") {
					if (sl) {
						this.showStatus(true, n);
					}
					return true;
				}else{
					this.showStatus(false, n, this.codes.guests);
					return false;
				}
				break;
			case 'island':
				if (this.YD.get(n).options[v].value != "") {
					if (sl) {
						this.showStatus(true, n);
					}
					return true;
				}else{
					this.showStatus(false, n, this.codes.island);
					return false;
				}
				break;
			case 'country':
				if (this.YD.get(n).options[v].value != "") {
					if (sl) {
						this.showStatus(true, n);
					}
					return true;
				}else{
					this.showStatus(false, n, this.codes.country);
					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';
		// 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 'fname':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.fname)
							e++;
						}
						break;
					case 'lname':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.lname)
							e++;
						}
						break;
					case 'phone':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.phone)
							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 'address':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.address)
							e++;
						}
						break;
					case 'city':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.city)
							e++;
						}
						break;
					case 'state':
						if (!this.validate('select', form[i].options.selectedIndex, form[i].id, true)) {
							e++;
						}
						break;
					case 'zip':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.zip)
							e++;
						}
						break;
					case 'country':
						if (!this.validate('country', form[i].options.selectedIndex, form[i].id, true)) {
							e++;
						}
						break;
					case 'island':
						if (!this.validate('island', form[i].options.selectedIndex, form[i].id, true)) {
							e++;
						}
						break;
					case 'wedding_date':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.wedding_date)
							e++;
						}
						break;
					case 'guests':
						if (!this.validate('guests', form[i].options.selectedIndex, form[i].id, true)) {
							e++;
						}
						break;
					case 'security_code':
						if (!this.validate('name', form[i].value, form[i].id)) {
							this.showStatus(false, form[i].id, this.codes.security_code)
							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();