// JavaScript Document

var Contact = Class.create({
	
	initialize : function ( element ) {
			
			if ( !$(element) ) return;
			
			this.element = $(element);
			
			var defaults = { file : '' };
			
			this.options = Object.extend( defaults, arguments[1] || {} );
			
			this.box = new Element("div",{id:"hcform"}).hide();
			this.name = new Element("input",{type:"text"});
			this.email = new Element("input",{type:"text"});
			this.message = new Element("textarea",{cols:30,rows:10});
			this.status = new Element("div").setStyle({textAlign:"left",padding:"4px",margin:"0 auto"});
			
			var tbl, tbody, tr, td, tmp;
			tbl = new Element("table");
			tbody = new Element("tbody");
			tbl.appendChild( tbody );
			
			tr = new Element("tr");
			tbody.appendChild( tr );
			tr.appendChild( new Element("th").update("Name:") );
			td = new Element("td");
			td.appendChild( this.name );
			tr.appendChild( td );
			
			tr = new Element("tr");
			tr.appendChild( new Element("th").update("Email:") );
			tbody.appendChild( tr );
			td = new Element("td");
			td.appendChild( this.email );
			tr.appendChild( td );
			
			tr = new Element("tr");
			tr.appendChild( new Element("th").update("Message:") );
			tbody.appendChild( tr );
			td = new Element("td");
			td.appendChild( this.message );
			tr.appendChild( td );
			
			tr = new Element("tr");
			tbody.appendChild( tr );
			this.sendButton = new Element("button",{type:"button"}).update("Send");
			this.sendButton.observe( "click", this.send.bind(this) );
			td = new Element("td");
			td.appendChild( this.sendButton );
			tr.appendChild( td );
			td = new Element("td").setStyle({textAlign:"right"});
			tmp = new Element("button",{type:"button"}).update("Close");
			tmp.observe("click", function(){ this.box.hide(); this.setStatus(); }.bind(this) );
			td.appendChild( tmp );
			tr.appendChild( td );
			
			this.box.appendChild( this.status );
			this.box.appendChild( tbl );
			this.element.parentNode.appendChild( this.box );
			this.element.observe( "click", function(){ this.box.toggle();
													   if ( this.box.visible() ) this.name.focus(); }.bind(this) );
			this.status.setStyle({width:(this.box.getWidth()-20)+"px"});
		},
	
	send : function () {
			
			if ( !this.options.file )
			{
				this.setStatus( {"file":"No url provided to send form"}, "error" );	
				return false;
			}
			
			var err = [];
			
			if ( this.name.value.match(/^\s*$/) )
				err.push( "name" );
			if ( this.email.value.match(/^\s*$/) || !(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(this.email.value)) )
				err.push( "email" );
			if ( this.message.value.match(/^\s*$/) )
				err.push( "message" );
			
			
			var par = "?name="+this.name.value
					 +"&email="+this.email.value
					 +"&message="+this.message.value;
			
			var opts = { method : "get"
						, parameters : par
						, onComplete : function ( req ) {
								var jsn = req.getHeader("X-JSON");
								
								jsn = jsn.evalJSON();
								
								if ( jsn.success )
								{
									this.setStatus({0:"Your email was successfully sent.<br/>"
												     +"You will be contacted within the next 2 "
													 +"business days about your inquiry.<br/><br/>"
													 +"If you need immediate assistance please call: "
													 +"406-493-2412"}, "success");
									this.name.value = '';
									this.email.value = '';
									this.message.value = '';
								} else
									this.setStatus( jsn.errors, "error" );
								
								this.sendButton.update("Send");
								this.sendButton.disabled = false;
								
							}.bind(this) };
			
			if ( err.length == 0 )
			{
				this.sendButton.disabled = true;
				this.sendButton.update( "Sending..." );
				
				new Ajax.Request( this.options.file, opts );
			} else {
				var tmp = {};
				for( var i=0; i<err.length; i++ )
					tmp[err[i]] = true;
				
				this.setStatus( tmp, "error" );
			}
				
		},
	
	setStatus : function ( obj, cn ) {
			
			this.status.className = cn;
			
			for( var x in this )
				if ( x.match(/name|email|message/) )
					this[x].parentNode.parentNode.className = "";
			
			var str = "";
			for ( var x in obj )
			{
				if ( cn == "error" && x.match(/name|email|message/) )
					this[x].parentNode.parentNode.className += cn;
				
				if ( typeof(obj[x]) == "string" )
					str += (x.match(/\d+/)?"":x+" = ")+obj[x]+"<br />";
			}
			
			if ( cn == "error" )
				str = "Please verify information in the form<br />"+str;
			
			if ( str ) this.status.show();
			else this.status.hide();
			
			this.status.update( str );
		}
	
}); // END class Contact