/** 
 * @fileoverview 
 * <h3>SAF (Standard Action Forms) Validation</h3>
 * Client-side validation routines for all forms that have a <code>class</code>  
 * attribute of <code>saf</code>
 * (ex. <code>&lt;form id="example-form" action="." method="post"  
 * class="saf"&gt;</code>).
 *
 * @version 2.5
 *
 * @author Matt Dertinger
 * @author Yun Qian
 *
 * @see /c/saf.css for presentation rules
 *
 */
var safTimer = setTimeout('',2000);

/**
 * Construct a new Saf object.
 * @class This class represents an instance of a Saf.
 * @constructor
 *
 * @return A new instance of a Saf.
 *
 */

var Saf = {
	Version: '2.5',
	prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

Saf.Base = Class.create();
Saf.Base.prototype = {
	initialize: function(form, options) {
		this.init = true;
		this.hasErrors = false;
		this.f = $(form);
		this.submitButtons = new Array();
		Event.observe(this.f, 'submit', this.validateAll);
		//this.observe();
	},
	
	observe: function() {
		Form.getElements(this.f).each(function(item) {
			if(item.type == 'submit' || item.type == 'image'){
				this.submitButtons.push(item);
				
			}else{
				Event.observe(item, 'focus', this.focus.bindAsEventListener(this) );
				Event.observe(item, 'blur', this.blur.bindAsEventListener(this) );
				Event.observe(item, 'blur', this.validate.bindAsEventListener(this) );
			}
		}.bind(this) );
	},
	
	validate: function(evt) {
		/* Local Variable to store the object reference of the current field being validated */
		var fld = this.fld;
		var url = this.f.action;
		var pars = Form.serialize(this.f);
		var opt = {
			method: "post",
			postBody: "ajaxValidate=true&" + pars,
			onComplete: function(transport) {
				var json = eval('(' + transport.responseText + ')');
				var fldName = fld.name;
				if(fldName == 'prefix' || fldName =='suffix'){
					fldName = 'areacode';
				}
				if(fldName == 'verifyEmail'){
					fldName = 'email';
				}
				var jsonItem = json.messages.detect( function(item) {
					return (item.key == fldName);
				} );
			
				var response = (typeof jsonItem == "undefined" || jsonItem.value == "") ? "\u00A0" : jsonItem.value;
			
				this.isError = (response != "\u00A0") ? 1 : 0;
				var parentDL = $(fld).up('dl');
				if(fld.name == 'verifyEmail'){
					parentDL =	$('email').up('dl');
				}
				
				var dd = $A($(parentDL).getElementsByTagName('dd') ).detect(function(item) {
					return $(item).hasClassName('msg');
				} ).update(response);
			
			}
		}
		var xmlValidate = new Ajax.Request(url, opt);
	},
	
	focus: function(evt) {
		this.fld = Event.element(evt);
	/*	if (this.fld.value == this.fld.defaultValue) {
			this.fld.value = "";
		}
		var waitToValidate = (this.fld.type == "radio" || this.fld.type == "checkbox") ? 1 : 0;
		if (waitToValidate && this.fld.id.lastIndexOf("0") == -1) {
			clearTimeout(safTimer);
		} */
	},
	
	blur: function(evt) {
		this.fld = Event.element(evt);
	/*	if (this.fld.value == "") { 
			this.fld.value = this.fld.defaultValue; 
		} */
	},
	
	validateAll: function(evt) {
		Event.stop(evt);
		var f = Event.findElement(evt, 'form');
		var url = f.action;
		
		var pars = Form.serialize(f);
		/* Set error type for Omniture Form Analysis Plugin */
		var errorType;
		var opt = {
			method: "post",
			postBody: "ajaxValidate=true&" + pars,
			onComplete: function(transport) {
				$$('dd.msg').each(function(item){
					item.update("&#xA0;");
				});
				var json = eval('(' + transport.responseText + ')');
				json.messages.each( function(item) {
					var response = (typeof item.value == "undefined" || item.value == "") ? "\u00A0" : item.value;
					var isError = (response != "\u00A0") ? 1 : 0;
					if(isError){
						if($(item.key)){
							var parentDL = $(item.key).up('dl');
							var dd = parentDL.down('dd.msg').update(response);
							//$(parentDL).visualEffect('highlight', { delay: 1.0 } );
						}
					} 
					
				} );
				if (json.messages.pluck('value').toArray().compact().without("").size() <= 0) {
					safObj[f.id].submitButtons.each(function(item){
						item.disabled = true;
					});	
					
					f.submit();
				} 
			}
			
		}
		
		var xmlValidate = new Ajax.Request(url, opt);
		
	}
	
};

var safObj = new Array();	
function initSaf() {
	$A(document.forms).each(function(item) {
		var f = $(item);
		if (f.hasClassName('safajax') ) {
	 		safObj[f.id] = new Saf.Base(f);
			safObj[f.id].observe();
		}
	} );
	
}
