var Subscribe = Class.create();

Subscribe.prototype = {
	initialize : function(elm){
		this.formelm = $(elm);
		 $(elm).onsubmit = function (){
			Subscribe.validate();
			return false;
		}
	},
	succesCb : function(t){
		var serializer = new PHP_Serializer(); 
		var data = serializer.unserialize(t.responseText);
		var form = Subscribe.formelm;
		
		var formelm = form.elements;
		for(var i=0; i<form.elements.length; i++){
			e = form.elements[i];
			if(Object.isElement(e) == 1){
				e.parentNode.style.backgroundImage= "none";
			} else {
				for(j=0; j<e.length; j++){
					e.parentNode.style.backgroundImage= "none";
				}
			}
		}
		
		for(var i=0; i<data.length; i++){
			if(Object.isElement(form[data[i]]) == 1){
				Subscribe.highlightElm(form[data[i]]);
			} else {
				for(j=0; j<form[data[i]].length; j++){
					Subscribe.highlightElm(form[data[i]][j]);
				}
			}
		}
		
		$$('input, textarea').each(function(e){
			e.onfocus = function(){
				e.parentNode.style.backgroundImage= "none";
				new Effect.Morph(e,{style:'color:#008651;border:#008651'});
			}
	
			e.onblur = function(){
				e.parentNode.style.backgroundImage= "none";
				new Effect.Morph(e,{style:'color:#000;border:#aaa'});
			}
		});
		
		if(data.length > 0){
			new Element.show('Message');
			setTimeout("new Effect.ScrollTo('Message',{offset:-10,afterFinish:function(){new Effect.Highlight('Message',{duration:2})}})",500);

		} else {
			new Effect.Fade('Message');
			new Effect.Fade(Subscribe.formelm);
			Subscribe.formelm.submit();
		}		
		
		console.log(data);
	},
	failureCb : function(t){
		alert('Error ' + t.status + ' -- ' + t.statusText);
	},
	highlightElm : function(e){
		e.parentNode.style.backgroundImage= "url('/images/alert.gif')";
	},
	validate : function(){
		var aparams = Form.serialize(Subscribe.formelm);
		new Ajax.Request('/ajax.php?action=checkform&form=' +Subscribe.formelm.id,{method:'post',parameters:aparams, onSuccess : Subscribe.succesCb, onFailure : Subscribe.failureCb});
	}
}
