var CTC = {
	HAL:function(id) {
		new Effect.Highlight( $(id) );
		
		if( $( id + "_label" ) ) {
			$( id + "_label" ).style.color="red";
		}
		
		$(id).focus();
	},
	UNHAL:function(id) {
		if( $( id + "_label" ) ) {
			$( id + "_label" ).style.color="black";
		}
	},
	Contact : {
		Validate:function() {
			if( $("firstname").value.blank() ) {
				CTC.HAL("firstname");
				return false;
			}
			if( $("lastname").value.blank() ) {
				CTC.HAL("lastname");
				return false;
			}
			if( $("telephone").value.blank() ) {
				CTC.HAL("telephone");
				return false;
			}
			if( $("emailaddress").value.blank() ) {
				CTC.HAL("emailaddress");
				return false;
			}
			if( $("message").value.blank() ) {
				CTC.HAL("message");
				return false;
			}
			return true;
		}
	},
	Booking : {
		ChangeLocation:function() {
			if( $("locationId").value==0 ) {
				$("address_holder").hide();
			} else {
				$("address_holder").show();
			}
			var url = "index.cfm?action=LoadDatesFromLocation&id="+$("locationId").value;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:CTC.Booking.ChangeLocation_onSuccess, onFailure:errFunc});
		},
		ChangeLocation_onSuccess:function(t) {
			z = parseJSON( t.responseText );
			$( "approxDate" ).replace( z.newSelectHTML );
		},
		TogglePaypal:function() {
			if( $("paymentMethod").value=="CASH" ) {
				$("paypal").hide();
			} else {
				$("paypal").show();
			}
		},
		Validate:function() {
			if( $("firstname").value.blank() ) {
				CTC.HAL("firstname");
				return false;
			} else {
				CTC.UNHAL("firstname");
			}
			if( $("lastname").value.blank() ) {
				CTC.HAL("lastname");
				return false;
			} else {
				CTC.UNHAL("lastname");
			}
			if( $("telephone").value.blank() ) {
				CTC.HAL("telephone");
				return false;
			} else {
				CTC.UNHAL("telephone");
			}
			if( $("emailaddress").value.blank() ) {
				CTC.HAL("emailaddress");
				return false;
			} else {
				CTC.UNHAL("emailaddress");
			}
			if( $("locationId").value==0 ) {
				alert("Please tell us where your tree spent Christmas!");
				CTC.HAL("locationId");
				return false;
			} else {
				CTC.UNHAL("locationId");
			}
			if( $("address1").value.blank() ) {
				CTC.HAL("address1");
				return false;
			} else {
				CTC.UNHAL("address1");
			}
			if( $("approxDate").value==0 ) {
				alert("Please choose a date for us to collect your tree on!");
				CTC.HAL("approxDate");
				return false;
			} else {
				CTC.UNHAL("approxDate");
			}			
			if( $("btnSubmit") ) {
				$("btnSubmit").disabled = true;
			}
			return true;
		}
	}	
}

String.prototype.isEmail = function () { 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this); 
	return (matches != null && this == matches[0]); 
}

function isNumeric(x) { 
	var y=parseInt(x); 
	if (isNaN(y)) {
	   return false; 
	} else {
		return true;
	}
} 

function LoadingAjax(txt) {
	if(typeof(txt)=="undefined") {
		txt = "Loading...";
	}
	return "<div class='ajaxMsg' id='widgetAjaxMsg'> "+txt+"</div>";
}

function parseJSON( json ){
	var o = eval('(' + json + ')');
	if( o.redirectURL )
	{
		document.location = o.redirectURL;
		return;
	}
	if( $("message") != null ) new Element.remove( "message" );
	if( o.errMsg )
	{
		if( o.errMsgTitle == null ) o.errMsgTitle = "Error";
		if( o.errMsgTimer == null ) o.errMsgTimer = 5000;
		tw.ShowMessage( o.errMsgTitle, o.errMsg, "error", o.errMsgTimer, o.errMsgPosition );
		return o;
	}
	if( o.msgTitle || o.msg )
	{
		if( o.msgTimer == null ) o.msgTimer = 5000;
		tw.ShowMessage( o.msgTitle, o.msg, o.msgClass, o.msgTimer, o.msgPosition );
	}
	return o;
}

var errFunc = function(t) {
	var win = window.open("", "win", "width=1024,height=700,resizable=yes,scrollbars=yes,status=no"); // a window object
	win.document.open("text/html", "replace");
	win.document.write( "<html><body style='margin:0'><div style='border-bottom:1px solid #222;background:#666;padding:10px;'><h1 style='color:#FFF;margin:0;padding:0;'>Digital Crew Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}

var ClassAdder = {
	Apply:function(e) {
		$("mainNav").select("LI").each(
			function(name,index) {
				if(index<5) {
					name.addClassName("green");
				}
			}
		);
	}
}

document.observe("dom:loaded",ClassAdder.Apply);
