

function onShareSubmitClick(){
	var errors = false;

	if (document.getElementById("share_email_from").value.length == 0){
		errors = true;
		setShareErrorMessage("share_email_from", "Email address is missing");
	} else {
		if (isValidEmail(document.getElementById("share_email_from").value)){
			setShareErrorMessage("share_email_from", "");
		} else {
			errors = true;
			setShareErrorMessage("share_email_from", "Email address is invalid");
		}
	}
	
	var toEmails = document.getElementById("share_email_to").value;
	
	toEmails = toEmails.replace(/\s/igm, "");	// Remove formatting and extra spaces
	
	if (toEmails){
		if (toEmails.indexOf(",") > 0){
			var toEmailsArr = toEmails.split(",");
			
			for (var i = 0; i < toEmailsArr.length; i++){
				// Check for blank line
				if (toEmailsArr[i].length > 0){
					if (!isValidEmail(toEmailsArr[i])){
						errors = true;
						setShareErrorMessage("share_email_to", "One or more emails are invalid");
						break;
					}
				}
			}
			
			if (!errors){
				setShareErrorMessage("share_email_to", "");
			}
		} else {
			if (isValidEmail(toEmails)){
				setShareErrorMessage("share_email_to", "");
			} else {
				errors = true;
				setShareErrorMessage("share_email_to", "Email address is invalid");
			}
		}
	} else {
		errors = true;
		setShareErrorMessage("share_email_to", "Email address is missing");
	}
	
	setShareStatusMessage("", "hidden");
	
	if (!errors){
		submitForm();
	}
	
	return false;
}


function submitForm(){
	var query = "";
	
	setShareStatusMessage("Processing...", "progress");
	
	query = getTextboxValueEncodedForURL("share_name") + "&";
	query += getTextboxValueEncodedForURL("share_email_from") + "&";
	query += getTextboxValueEncodedForURL("share_email_to") + "&";
	query += getTextboxValueEncodedForURL("share_message") + "&";
	query += getCheckboxValueEncodedForURL("share_copy_self") + "&";
	query += "title=" + encodeURIComponent(getTitle()) + "&";
	query += "url=" + encodeURIComponent(location.href) + "&";
	query += "type=" + encodeURIComponent(getType()) + "&";
	query += "share_submit=tenfour";
	
	
	setFormEnabledState(false);
	ajaxPost("/press/media_share.php", query, submitFormSuccess, submitFormFailure);
	
	
	function getTitle(){
		if (document.getElementById("video-title") != null){
			return document.getElementById("video-title").innerHTML;
		} else {
			return document.getElementById("audio-title").innerHTML;
		}
	}

	
	function getType(){
		if (document.getElementById("video-title") != null){
			return "video";
		} else {
			return "audio";
		}
	}
}


function submitFormSuccess(xmlhttp){
	var errors 		= (xmlhttp.responseXML.childNodes[0].getAttribute("errors") == "true");
	var serverError = (xmlhttp.responseXML.childNodes[0].getAttribute("serverError") == "true");
	
	setFormEnabledState(true);

	if (errors || serverError){
		if (serverError){
			setShareStatusMessage("Server error", "error");
		} else {
			if (xmlhttp.responseXML.childNodes[0].getAttribute("fromEmailMissing") == "true"){
				setShareErrorMessage("share_email_from", "Email address is missing");
			}
			
			if (xmlhttp.responseXML.childNodes[0].getAttribute("fromEmailInvalid") == "true"){
				setShareErrorMessage("share_email_from", "Email address is invalid");
			}
			
			if (xmlhttp.responseXML.childNodes[0].getAttribute("toEmailsMissing") == "true"){
				setShareErrorMessage("share_email_to", "Email address is missing");
			}
			
			if (xmlhttp.responseXML.childNodes[0].getAttribute("toEmailsInvalid") == "true"){
				setShareErrorMessage("share_email_to", "One or more emails are invalid");
			}
			
			setShareStatusMessage("Error, see above", "error");
		}
	} else {
		setShareStatusMessage("Message sent", "success");
	}
}


function submitFormFailure(){
	// Error from server
	setFormEnabledState(true);
	setShareStatusMessage("Comm. error", "error");
}


function setShareErrorMessage(elementName, message){
	var element = document.getElementById(elementName + "_error");
	
	if (message){
		element.style.display = "block";
		element.innerHTML = message;
	} else {
		element.style.display = "none";
	}
}


function setShareStatusMessage(message, className){
	var element = document.getElementById("share_status");
	
	if (message){
		element.style.display = "block";
		element.className = "share-" + className;
		element.innerHTML = message;
	} else {
		element.style.display = "none";
	}
}


function setFormEnabledState(isEnabled){
	document.getElementById("share_name").disabled = !isEnabled;
	document.getElementById("share_email_from").disabled = !isEnabled;
	document.getElementById("share_email_to").disabled = !isEnabled;
	document.getElementById("share_message").disabled = !isEnabled;
	document.getElementById("share_copy_self").disabled = !isEnabled;
	document.getElementById("share_submit").disabled = !isEnabled;
	
	if (isEnabled){
		document.body.style.cursor = "auto";
	} else {
		document.body.style.cursor = "progress";
	}
}