/** Form functions
 ** (c) 2008 Jeff Pickhardt
 **/

var FORM_LENGTH=200;

function browserWidth() {
	var returnWidth=0
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		returnWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		returnWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		returnWidth = document.body.clientWidth;
	}
	return returnWidth;
}

function openForm() {
	var form=$('form-holder');
	//form.style.left=(.5*(browserWidth()-FORM_LENGTH))+"px";
	//form.style.width=FORM_LENGTH+'px';
	form.style.display="block";
	openGreyBg();
}

function closeForm() {
	var form=$('form-holder');
	form.style.display="none";
	closeGreyBg();
	return false;
}

function openGreyBg() {
	var greyBg=$('greyBg');
	greyBg.style.display="block";
	var height1=document.body.offsetHeight;
	var height2=document.documentElement.clientHeight;
	if(height1<20 && height2<20) {
		greyBg.style.height="100%";
	} else {
		greyBg.style.height=
			(height1>height2) ?
				height1+20+'px' : height2+'px';	
	}
}

function closeGreyBg() {
	$('greyBg').style.display="none";
}

function sendMessage() {
	if(validate()) {
		return false;
	}
	showThinking();
	new Ajax.Request('cgi-bin/send-msg?ajax=true', {
		method: 'post',
		parameters: Form.serialize($('form-contact')),
		onSuccess: function(transport) {
				interpretResult(transport);
			}
		});
	return false;
}
function interpretResult(transport) {
	if (transport.responseText=='received') {
		showThanks();
	} else {
		showError();
	}
}
function showThinking() {
	$('form-contact').style.display = 'none';
	$('form-thinking').style.display = 'block';
}
function showThanks() {
	$('form-thinking').style.display = 'none';
	$('form-received').style.display = 'block';
}
function showError() {
	$('form-thinking').style.display = 'none';
	$('form-error').style.display = 'block';
	$('emailAddress').innerHTML='aqayumi@';
	$('emailAddress').innerHTML+='stanford.edu';
}
function isInvalidEmail(str) {
	var emailPattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; // credit to dustindiaz.com
	if (str.match(emailPattern)) { // regexp for (anything)@(anything).(anything)
		return false;
	}
	return true;
}
function isEmpty(str) {
	if (str == '') {
		return true;
	} else {
		return false;
	}
}
function markError(elem) {
	elem.addClassName('error');
	Effect.Shake(elem);
	foundErrors=true;
}
function validate() {
	var foundErrors=false;
	if (isEmpty($('form-name').value)) {
		markError($('form-name'));
		foundErrors=true;
	} else {
		$('form-name').removeClassName('error');
	}
	if (isEmpty($('form-email').value) || isInvalidEmail($('form-email').value)) {
		markError($('form-email'));
		foundErrors=true;
	} else {
		$('form-email').removeClassName('error');
	}
	if (isEmpty($('form-message').value)) {
		markError($('form-message'));
		foundErrors=true;
	} else {
		$('form-message').removeClassName('error');
	}
	return foundErrors;
}