var Borders = Class.create({
	initialize : function () {
		this.lcol  = $$('.leftColumn');
		this.rcol  = $$(".rightColumn");
	},
	
	addBorders : function (page) {
		if(page == "Home") (this.lcol[1].getHeight() > this.rcol[1].getHeight()) ?  this.rcol[1].setStyle({'height': this.lcol[1].getHeight() + 'px'}) : this.lcol[1].setStyle({'height': this.rcol[1].getHeight() + 'px'});
		else if(this.rcol.length > 1) {
			if(this.rcol[1].getHeight() > this.lcol[1].getHeight()) this.lcol[1].setStyle({'height': this.rcol[1].getHeight() + 'px'});
		}
		for(i=0;i<this.lcol.length;i++) {this.appendBorder(this.lcol[i], this.lcol[i].getHeight(), 'left');}
		for(i=0;i<this.rcol.length;i++) {this.appendBorder(this.rcol[i], this.lcol[i].getHeight(), 'right');}
		/*if(this.rcol.length == 2) {
			(this.lcol[1].getHeight() > this.rcol[1].getHeight()) ? $('main').setStyle({'height': this.lcol[1].getHeight() + 2 + 'px'}) : $('main').setStyle({'height': this.rcol[1].getHeight() + 2 + 'px'});
		}
		else {
			$('main').setStyle({'height': this.lcol[1].getHeight() + 2 + 'px'});
		}*/
	},
	
	appendBorder : function(elem, offset, pos) {
		eDims  = elem.getDimensions();
		border = document.createElement('div');
		if (pos == 'left')
			$(border).setStyle({'position': 'absolute', 'z-index': '1', 'left' : '13px', 'top': '2px', 'width': eDims.width+'px', 'height': eDims.height+'px', 'background': '#cacac9'});
		else
			$(border).setStyle({'position': 'absolute', 'z-index': '1', 'right' : '17px', 'top': '2px', 'width': eDims.width+'px', 'height': eDims.height+'px', 'background': '#cacac9'});
			
			
		$(elem).up().appendChild($(border));
	}
});

var LoginMenu = Class.create({
	initialize : function(options) {
		this.options = {
			timeout	: 200,
			button : $$('.navLogin')[0],
			menu : $('loginMenu')
		}
		Object.extend(this.options, options || {});
		
		this.closetimer = 0;

		this.options.button.onmouseover = (function(scope) {return function() {scope.showMenu();};})(this);		
		this.options.button.onmouseout  = (function(scope) {return function() {scope.hideMenu();};})(this);
		this.options.menu.onmouseover   = (function(scope) {return function() {scope.cancelHideMenu();};})(this);
		this.options.menu.onmouseout    = (function(scope) {return function() {scope.hideMenu();};})(this);
	},
	
	showMenu : function() {	
		// cancel close timer
		this.cancelHideMenu();
		this.options.menu.setStyle({'display':'block'});
	},
	
	// close showed layer
	hide : function(scope) {
		if(scope.options.menu) scope.options.menu.setStyle({'display':'none'});
	},
	
	// go close timer
	hideMenu : function() {
		scope = this;
		this.closetimer = window.setTimeout("scope.hide(scope)", this.options.timeout);
	},
	
	// cancel close timer
	cancelHideMenu : function() {
		if(this.closetimer) {
			window.clearTimeout(this.closetimer);
			this.closetimer = null;
		}
	}
});

var ContactForm = Class.create({
	initialize : function(obj) {
		this.obj = Element.extend(document.getElementById(obj));
		this.msgContainer = this.obj.up().up().down(0);
		
		this.url		= '/formHandler.php';
		this.defaults 	= ["First name", "Last name", "Phone number", "Email address", "Your comments"];
		this.errorMsg 	= 'Please correct the highlighted fields and submit again.'; // ERRORMSG - required
		this.successMsg = 'We have received your request and a representative will contact you shortly.'; // SUCCESSMSG - required
		this.el 		= this.obj.getElements();
		
		for(i=0;i<this.el.length-1;i++) {
			this.el[i].value = this.defaults[i];
			Event.observe(this.el[i], 'focus', (function(el, d, i) {return function() {if(el.value.toLowerCase() == d.toLowerCase()) {el.value = "";  el.setStyle({'color': '#000'});}};})(this.el[i], this.defaults[i], i));
			Event.observe(this.el[i], 'blur',  (function(el, d, i) {return function() {if(el.value == "") {el.value = d; el.setStyle({'color': '#ccc'});}};})(this.el[i], this.defaults[i], i));
		}
		
		$('submit').onmouseup = (function(scope) {return function() {scope.validate();};})(this);

	},
	
	validate : function(e) {
		haveError = false;
		for(i in this.el) this.el[i].error = false;
		
		//DEFINE REGULAR EXPRESSIONS
		nameExpr   = /^([a-zA-Z '-]+)$/i;
		emailExpr  = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z \.])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/i;
		phoneExpr  = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/i; 
		urlExpr    = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i;
		
		//Validate contact form fields
		
		//First, make sure none of the fields are populated with the defaults values
		for(i=0; i<this.el.length-1; i++) {
			if (this.el[i].value.toLowerCase() == this.defaults[i].toLowerCase()) this.el[i].error = true;
		}
		
		//Next, test the field values against the regex rules
		if (!nameExpr.test(this.el[0].value)) this.el[0].error = true;								// Validate first name
		if (!nameExpr.test(this.el[1].value)) this.el[1].error = true;								// Validate last name
		if(!this.el[2].value.blank()) { 															// Validate phone number
			if (!phoneExpr.test(this.formatPhoneNumber(this.el[2].value))) this.el[2].error = true;
		}
		if (!emailExpr.test(this.el[3].value)) this.el[3].error = true;								// Validate email address
			
		//loop through errors, if any
		for(i=0; i<this.el.length-1; i++) {
			if(this.el[i].error == true) {
				this.el[i].style.backgroundColor = "#FFFF99";
				haveError = true;
			}
			else {
				this.el[i].style.backgroundColor = "#ffffff";
			}
		}
		
		if(haveError) {
			if(!$$('.contactFormError').length) {
				msg = document.createElement("div");
				Element.extend(msg);
				msg.update(this.errorMsg);
				msg.className = "contactFormError";
				this.msgContainer.insert(msg);
				window.setTimeout('Element.remove(msg)',10000);
			}
			return false;
		}
		else {
			if($$('.contactFormError').length || $$('.contactFormSuccess').length) Element.remove(msg);
			this.submit();
		}
	},
	
	formatPhoneNumber : function(p) {
		filteredValues = "()- ";
		var formattedNumber = "";
		
		for (i=0; i<p.length; i++) {  // Search through string and append to unfiltered values to returnString.
			c = p.charAt(i);
			if (filteredValues.indexOf(c) == -1) formattedNumber += c;
		}
		
		return formattedNumber;
	},
	
	submit : function() {
		scope = this;
		params = this.obj.serialize();
			
		new Ajax.Request(this.url, {
			method: 'post',
			parameters: params,
			onComplete: function(transport) {
				msg = document.createElement("div");
				Element.extend(msg);
				
				if(transport.responseText.strip() == "success") {
					pageTracker._trackPageview('/contact-us/thanks/');
					for(i=0;i<scope.el.length-1;i++) {
						scope.el[i].setStyle({'color': '#ccc'});
						scope.el[i].value = scope.defaults[i];
					}
					msg.update(scope.successMsg);
					msg.className = "contactFormSuccess";
				}
				else {
					msg.update(transport.responseText.strip());
					msg.className = "contactFormError";
				}
				
				scope.msgContainer.insert(msg);
				window.setTimeout('Element.remove(msg)',15000);
			}
		});
	}
});
