$('print_button').observe('click', printDirections);
$('email_button').observe('click', emailDirections);

$('email_form').observe('submit',emailDirections);

$('email').observe('blur',offEmail)
	.observe('focus',onEmail);

function alertme(){
	alert(this);
	return false;
}

function onEmail(){
	if(this.value=="email address" || this.value=="invalid email address"){
		this.value="";
	}
	this.style.color="#000";
	alertMsg('');
}
function offEmail(){
	if(this.value==''){
		this.value="email address";
		this.style.color="#7f9db9";
	}
}
function alertMsg(string){
	$('alert').update(string);
}

function emailDirections(e) {
	if (echeck(this.parentNode.getElementsByTagName('input')[0].value)) {
		if(SendDirections()) {
			alertMsg('directions sent');
			$('email').setStyle({display: 'none'})
		}
	}
	else {
		alertMsg('invalid email address');
	}
	Event.stop(e);
	return false;
}

function echeck(inputvalue) {
	var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)) return true;   
    else return false;
}


function SendDirections(){
	if (window.XMLHttpRequest) {// code for all new browsers
		phphttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {// code for IE5 and IE6
		phphttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (phphttp!=null) {
		//phphttp.onreadystatechange=stateChange;
		phphttp.open("POST","mail-directions.php",true); //following reqired for POST:
		phphttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		phphttp.send('email='+document.getElementById('email').value);
		return true;
	}
	else return false;
}

function printDirections(){
	if (CheckIsIE() == true){
		document.ifWorkspace.focus();
		document.ifWorkspace.print();
	}
	else{
		window.frames['ifWorkspace'].focus();
		window.frames['ifWorkspace'].print();
	}
}

function CheckIsIE()
{
	if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') return true;
	else return false;
}
