isEmailOpen = false;
isArticleShareOpen = false;
isSending = false;
var statusId;
var actionIndicator = new Array()
actionIndicator[0] = ".";
actionIndicator[1] = ". .";
actionIndicator[2] = ". . .";
var curIndication;
function closeArticleShare(){
	document.getElementById("shareArticle").style.display = "none";
	isArticleShareOpen = false;
}
function closeEmailShare(){
	document.getElementById("shareEmail").style.display = "none";
	isEmailOpen = false;
}
function openArticleShare(){
	if(!isArticleShareOpen){
		document.getElementById("shareArticle").style.display = "block";
		isArticleShareOpen=true;
		closeEmailShare();
	}else{
		closeArticleShare();
	}
}
function openStatus(){
	document.getElementById("email_status").style.display = "block";
}
function closeStatus(){
	document.getElementById("email_status").style.display = "none";
	isSending=false;
	//reset data for next email
	resetEmailForm();
	if(document.getElementById("status").firstChild != null) document.getElementById("status").removeChild(document.getElementById("status").firstChild);
}
function openEmailShare(){
	if(isSending){
		return;
	}
	if(!isEmailOpen){
		document.getElementById("shareEmail").style.display = "block";
		isEmailOpen=true;
		closeArticleShare();
	}else{
		closeEmailShare();
	}
}
function sendEmail(){
	//get relevant data
	var toEmail = document.getElementById("toEmail").value;
	var fromEmail = document.getElementById("fromEmail").value;
	var comments = document.getElementById("comments").value;
	var emailHeadline = document.getElementById("emailHeadline").value;
	var emailStrapline = document.getElementById("emailStrapline").value;	
	//regex for email
	var emailexp = /^[A-Za-z_0-9'\.\-]+@[A-Za-z_0-9'\.\-]+(\.\w+)+$/;
	//validate data
	var failedValidation = false;
	if(!emailexp.test(toEmail)){
		document.getElementById("errorToEmail").style.display = "block";
		failedValidation = true;
	}else{
		document.getElementById("errorToEmail").style.display = "none";
	}
	if(!emailexp.test(fromEmail)){
		document.getElementById("errorFromEmail").style.display = "block";
		failedValidation = true;
	}else{
		document.getElementById("errorToEmail").style.display = "none";
	}
	//dont send the email if the data failed validation
	if(failedValidation){
		return false;
	}
	//construct email submit url
	var emailUrl = "/article-email?toEmail=" + toEmail
					+ "&fromEmail=" + fromEmail
					+ "&articleUrl=" + encodeURIComponent(parent.document.URL)
					+ "&comments=" + comments 
					+ "&emailHeadline=" + emailHeadline 
					+ "&emailStrapline=" + emailStrapline;
	//send the async email
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				alert("Your browser does not support AJAX!");
			}catch (e){
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			//tell user email was sent
			if(xmlHttp.responseText.match("success=true")){
				emailSent();
			}else{
				emailFailed();
			}
		}
	}
	xmlHttp.open("GET",emailUrl,true); //true means asynchronous
	xmlHttp.send(null);
	isSending=true;
	//give them status
	emailSendStatus();
	return false; // so the form doesnt actually try to submit anything
}
function emailSendStatus(){
	closeEmailShare();
	if(document.getElementById("status").firstChild != null) document.getElementById("status").removeChild(document.getElementById("status").firstChild);
	document.getElementById("status").appendChild(document.createTextNode("Sending",0));
	openStatus();
	//animate status
	curIndication=0;
	statusId = setInterval("animateStatus()", 1000);
}
function animateStatus(){
	if(document.getElementById("status").firstChild != null) document.getElementById("status").removeChild(document.getElementById("status").firstChild);
	document.getElementById("status").appendChild(document.createTextNode("Sending " + actionIndicator[curIndication]),0);
	curIndication++;
	if(curIndication == 3) curIndication = 0;
}

function emailSent(){
	//stop status animation
	clearInterval(statusId);
	
	//let user know the email was sent
	if(document.getElementById("status").firstChild != null) document.getElementById("status").removeChild(document.getElementById("status").firstChild);
	document.getElementById("status").appendChild(document.createTextNode("E-Mail Sent!!"),0);
	setTimeout("closeStatus()",5000);
}

function emailFailed(){
	//stop status animation
	clearInterval(statusId);
	
	//let user know the email failed to send
	if(document.getElementById("status").firstChild != null) document.getElementById("status").removeChild(document.getElementById("status").firstChild);
	document.getElementById("status").appendChild(document.createTextNode("Send Failed!!"),0);
	setTimeout("closeStatus()",5000);
}

function isValidEmail(str) {
	var emailexp = /^[A-Za-z_0-9'\.\-]+@[A-Za-z_0-9'\.\-]+(\.\w+)+$/;
	var vcheck = isValid(emailexp, str);
	return vcheck;
}

function checkEmptyToEmail(){
	if(document.getElementById("toEmail").value == ""){
		document.getElementById("toEmail").value = "Your Friend's Email Address";
	}
}

function checkEmptyFromEmail(){
	if(document.getElementById("fromEmail").value == ""){
		document.getElementById("fromEmail").value = "Your Email Address";
	}
}

function clearToEmail(){
	if(document.getElementById("toEmail").value == "Your Friend's Email Address"){
		document.getElementById("toEmail").value = "";
	}
}

function clearFromEmail(){
	if(document.getElementById("fromEmail").value == "Your Email Address"){
		document.getElementById("fromEmail").value = "";
	}
}

function resetEmailForm(){
	document.getElementById("toEmail").value = "Your Friend's Email Address";
	document.getElementById("fromEmail").value = "Your Email Address";
	document.getElementById("comments").value = "";
}