document.observe("dom:loaded", function() {
	$$('h4.contactar-instructor a').each(function(e) {
		e.observe('click', contactShow)
	});		
});

function contactShow(event) {
	var e = Event.element(event);
	event.stop();
		
	new Control.Modal.open(e, {
		overlayOpacity: 0.75,
		className: 'contactar',
		fade: true, 
		afterClose: function(){  
			Control.Modal.current.destroy();
			['contactar-header', 'contactar-content', 'contactar-footer'].each(function(e) {
				$(e).remove();
			});
		}  
	});	
}


function contactSubmit() {
	var ok = true;
	var value;
	$('error-container').update();
	['firstName', 'lastName', 'email', 'message'].each(function(e) {
		removeError(e);
		value = $F(e).stripTags().stripScripts().strip();
		if (value.empty()) {
			showError(e);
			ok = false;
		}
	});
	
	if (!$F('email').match(emailReg)) {
		showError('email');
		ok = false;
	}
	
	if (ok) {
		var f = $('contactar-form');
		new Ajax.Request(f.action, {
			method: 'post',
			parameters: f.serialize(),
			onSuccess: function(transport) {
				var result = transport.responseText.strip();
				if ('ok' == result) {
					$('contactar-content').update('Gracias por su mensaje. Sera contactada/o pronto.');
					new PeriodicalExecuter(function(pe) {
						pe.stop();
						Control.Modal.current.close();
					}, 5);
				}
				else {
					alert('Error intentando de enviar su menasje, favor intente de nuevo.');
				}
			}
		});
	}
	else {
		$('error-container').update('Por favor, introduzca los campos obligatorios.');
	}
}