TT.widget.Contact = {
	
	contactDivId: '',
	
	init: function (contactDivId) {
		this.contactDivId = contactDivId;
	},
	
	initPanel: function () {
		
		contactWin = new YAHOO.widget.Panel("contactPanel", {
			width:"600px",
			fixedcenter: false,
			constraintoviewport: true,
			underlay:"shadow",
			close:true,
			visible:false,
			draggable:false,
			modal: true,
			xy: [180,145]
		} );
		contactWin.render();
		
		// override the display setting from base.cfm
		YAHOO.util.Dom.get('contactPanel').style.display = 'block';
		
		// let the app know we can hide the busy window
		TT.customEvent.onCompleteAll.fire();
	},
	
	show: function (id) {
		// preselect the correct interest
		switch (id) {
			case 'offerings':
				YAHOO.util.Dom.get('interest').selectedIndex = 1;
				break;
			case 'partnerships':
				YAHOO.util.Dom.get('interest').selectedIndex = 2;
				break;
			case 'media':
				YAHOO.util.Dom.get('interest').selectedIndex = 3;
				break;
			default:
				YAHOO.util.Dom.get('interest').selectedIndex = 0;
				break;
		}
		
		contactWin.show();
	},
	
	hide: function () {
		contactWin.hide();
	},
	
	processForm: function () {
		// clear existing errors
		this.clearAllErrorClass();
		
		var error = false;
		
		// check for errors
		// interest
		if (YAHOO.util.Dom.get('interest').selectedIndex == 0) {
			this.addErrorClass('lbl_interest');
			error = true;
		}
		
		// first name
		if (YAHOO.util.Dom.get('fname').value.length < 1) {
			this.addErrorClass('lbl_fname');
			error = true;
		}
		// last name
		if (YAHOO.util.Dom.get('lname').value.length < 1) {
			this.addErrorClass('lbl_lname');
			error = true;
		}
		// email
		if (!YAHOO.util.Dom.get('email').value.match(/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/)) {
			this.addErrorClass('lbl_email');
			error = true;
		}
		// zip
		if (YAHOO.util.Dom.get('zip').value.length > 0 && !YAHOO.util.Dom.get('zip').value.match(/^[0-9]{5}$/) && !YAHOO.util.Dom.get('zip').value.match(/^[0-9]{5}-[0-9]{4}$/)) {
			this.addErrorClass('lbl_zip');
			error = true;
		}
		// comments
		if (YAHOO.util.Dom.get('comments').value.length < 1) {
			this.addErrorClass('lbl_comments');
			error = true;
		}
		
		// if no errors process the form
		if (!error) {
			// create a callback for the AJAX request
			var callback =
			{
			  success: TT.widget.Contact.successProcessForm
			}
			var postData = 'method=processContact' + 
				'&interest=' + escape(YAHOO.util.Dom.get('interest').options[YAHOO.util.Dom.get('interest').selectedIndex].innerHTML) + 
				'&fname=' + escape(YAHOO.util.Dom.get('fname').value) + 
				'&lname=' + escape(YAHOO.util.Dom.get('lname').value) + 
				'&email=' + escape(YAHOO.util.Dom.get('email').value) + 
				'&org=' + escape(YAHOO.util.Dom.get('org').value) + 
				'&jobtitle=' + escape(YAHOO.util.Dom.get('jobtitle').value) + 
				'&phone=' + escape(YAHOO.util.Dom.get('phone').value) + 
				'&address=' + escape(YAHOO.util.Dom.get('address').value) + 
				'&city=' + escape(YAHOO.util.Dom.get('city').value) + 
				'&state=' + escape(YAHOO.util.Dom.get('state').options[YAHOO.util.Dom.get('state').selectedIndex].innerHTML) + 
				'&zip=' + escape(YAHOO.util.Dom.get('zip').value) + 
				'&comments=' + escape(YAHOO.util.Dom.get('comments').value);
			
			YAHOO.util.Connect.asyncRequest('POST', TT.path + 'lib/cfc/AjaxFacade.cfc', callback, postData);
		}
		
		return false;
	},
	
	addErrorClass: function (id) {
		YAHOO.util.Dom.addClass(id, 'error');
	},
	
	clearErrorClass: function (id) {
		YAHOO.util.Dom.removeClass(id, 'error');
	},
	
	clearAllErrorClass: function () {
		YAHOO.util.Dom.getElementsByClassName(
			'error',
			'LABEL',
			this.contactDivId,
			function (el) {YAHOO.util.Dom.removeClass(el, 'error');}
		);
		
	},
	
	clearAndHideForm: function () {
		// loop inputs and clear values
		YAHOO.util.Dom.getElementsBy(function () { return true; }, 'INPUT', this.contactDivId, function (el) {el.value = '';});
		
		// reset dropdowns
		YAHOO.util.Dom.getElementsBy(function () { return true; }, 'SELECT', this.contactDivId, function (el) {el.selectedIndex = 0;});
		
		// loop comments and clear values
		YAHOO.util.Dom.getElementsBy(function () { return true; }, 'TEXTAREA', this.contactDivId, function (el) {el.value = '';});
		this.hide();
	},
	
	successProcessForm: function (o) {
		ret = eval('(' + o.responseText + ')')
		if (ret) {
			alert('Message has been sent.');
			TT.widget.Contact.clearAndHideForm();
			
		} else {
			alert('Unable to send message.  Please try again.');
		}
	}
}