var debug = false;
var ajaxDebug = false;

// ------------------------------ Code to manage relatd articles ----------------------------------
function changeArticle(index) {
	var maxIndex = 1 * document.getElementById(relatedCountID).innerHTML;
	if (index < maxIndex && index >= 0) {
		var relatedArticleTitle = document.getElementById("relatedArticle" + index + "Title").innerHTML;
		document.getElementById("relatedArticleTitle").innerHTML = relatedArticleTitle;
		
		var relatedArticleSynopsis = document.getElementById("relatedArticle" + index + "Synopsis").innerHTML;
		if (document.getElementById("relatedArticleSynopsis"))
			document.getElementById("relatedArticleSynopsis").innerHTML = relatedArticleSynopsis;
		
		//Set the article inddex field
		document.getElementById("relatedArticleIndex").innerHTML = index + 1;
		
	} else if (debug){
		alert("There is no article " + (index + 1));
	}
}

function nextArticle() {
	if (autoScroll) {
		clearInterval(autoScroll);
		autoScroll = null;
	}
	
	var index = (1 * document.getElementById("relatedArticleIndex").innerHTML);
	var maxIndex = (1 * document.getElementById(relatedCountID).innerHTML) -1;
	
	if (index > maxIndex)
		index = 0;
	changeArticle(index)

}

function previousArticle() {
	if (autoScroll) {
		clearInterval(autoScroll);
		autoScroll = null;
	}
	var index = (1 * document.getElementById("relatedArticleIndex").innerHTML) - 2;
	var maxIndex = (1 * document.getElementById(relatedCountID).innerHTML) -1;
	if (index < 0) 
		index = maxIndex;
	changeArticle(index)
}


function autoNext() {
	//goto the next index
	var index = (1 * document.getElementById("relatedArticleIndex").innerHTML);
	var maxIndex = (1 * document.getElementById(relatedCountID).innerHTML) -1;
	
	if (index > maxIndex)
		index = 0;
		
	changeArticle(index)
	
}

function focusText(field) {
	if (field.value.indexOf("optional") >= 0) {
		field.value = "";
	}
}

// ------------------------ Article Page Script -----------------------------
function showPlayer() {
	var playerDiv = document.getElementById("playerDiv");
	if (playerDiv!=null) {
		if (playerDiv.style.display == "none") {
			playerDiv.style.display = "";
		} else {
			playerDiv.style.display = "none";
		}
	}
}

// ------------------------ Share Article Code ------------------------------

var initialFromName = "Your Name";
var initialFromEmail = "Your email address";
var initialToName = "Your Friend's Name";
var initialToEmail = "Friend's email address";
var initialComments = "Have comments?";

function showShareArticle() {
	var theDiv = document.getElementById("shareArticle");
	
	//Align the top
	var top = getElementTop("shareButton");
	top += 27;
	theDiv.style.top = top + "px";
	
	//Set the left
	var width = document.body.clientWidth;
	var left = (width / 2) - 155;
	theDiv.style.left = left + "px";
	
	if (theDiv != null) {
		if (theDiv.style.display == "none") {
			theDiv.style.display = "";
		} else {
			theDiv.style.display = "none";
		}
	} else if (debug) {
		alert("theDiv is null");
	}
}

function validateFriendForm(theForm) {
	//validate fields
	var errors = new Array();
	var firstFieldInError = null;
	
	//Validate from name
	var error = validateRequired(theForm.fromName.value, "Your Name", initialFromName);
	if (error != null) {
		errors[errors.length] = error;
		firstFieldInError = theForm.fromName;
	} else {
		error = validateCleanText(theForm.fromName.value, "Your Name");
		if (error != null) {
			errors[errors.length] = error;
			firstFieldInError = theForm.fromName;
		}
	}
	
	//Validate from name
	var error = validateRequired(theForm.fromEmail.value, "Your Email Address", initialFromEmail);
	if (error != null) {
		errors[errors.length] = error;
		firstFieldInError = firstFieldInError!= null? firstFieldInError : theForm.fromEmail;
	} else {
		error = validateEmail(theForm.fromEmail.value, "Your Email Address");
		if (error != null) {
			errors[errors.length] = error;
			firstFieldInError = firstFieldInError!= null? firstFieldInError : theForm.fromEmail;
		}
	}
	
	//Validate from name
	var error = validateRequired(theForm.toName.value, "Your Friend's Name", initialToName);
	if (error != null) {
		errors[errors.length] = error;
		firstFieldInError = firstFieldInError != null ? firstFieldInError : theForm.toName;
	} else {
		error = validateCleanText(theForm.toName.value, "Your Friend's Name");
		if (error != null) {
			errors[errors.length] = error;
			firstFieldInError = firstFieldInError != null ? firstFieldInError : theForm.toName;
		}
	}
	
	//Validate to name
	var error = validateRequired(theForm.toEmail.value, "Your Friend's Email Address", initialToEmail);
	if (error != null) {
		errors[errors.length] = error;
		firstFieldInError = firstFieldInError!= null? firstFieldInError : theForm.toEmail;
	} else {
		error = validateEmail(theForm.toEmail.value, "Your Friend's Email Address");
		if (error != null) {
			errors[errors.length] = error;
			firstFieldInError = firstFieldInError!= null? firstFieldInError : theForm.toEmail;
		}
	}
	
	if (validateRequired(theForm.comments.value, "Comments", initialComments) == null) {
		error = validateCleanText(theForm.comments.value, "Comments");
		if (error != null) {
			errors[errors.length] = error;
			firstFieldInError = firstFieldInError!= null? firstFieldInError : theForm.comments;
		}
	}
	
	if (errors.length>0) {
		showErrors(errors);
		firstFieldInError.select();
		firstFieldInError.focus();
	}
	
	return errors.length == 0;
}

function sendToAFriend(theForm, labelID) {
	var valid = validateFriendForm(theForm);
	if (valid) {
		//Copy the article ID value
		theForm.articleId.value = document.getElementById(labelID).innerHTML;

		//If the form is valid then initiate the ajax call to send to a friend
		var client = new HttpClient(ajaxDebug);
		var formParams = postDataFromForm(theForm);
		var method = new GetMethod("ShareArticleEmailer.ashx?ajax=true" + formParams, sendToFriendSuccessCallback, sendToFriendErrorCallback);
		
		//Clean up the form (hide the fields)
		document.getElementById("formFields").style.display = "none";
		document.getElementById("sendToFriendCaption").innerHTML = "Sending Email, please wait";
		document.getElementById("sendToFriendCaption").style.display = "";
		document.getElementById("sendToAFriendButton").style.display = "none";
		
		
		//Execute the method
		client.executeMethod(method);
	}
}

function checkFromName(field) {
	if (field.value == initialFromName) {
		field.value = "";
	}
}

function checkFromEmail(field) {
	if (field.value == initialFromEmail) {
		field.value = "";
	}
}

function checkToName(field) {
	if (field.value == initialToName) {
		field.value = "";
	}
}

function checkToEmail(field) {
	if (field.value == initialToEmail) {
		field.value = "";
	}
}

function checkComments(field) {
	if (field.value == initialComments) {
		field.value = "";
	}
}

function sendToFriendSuccessCallback(resp) {
	if (ajaxDebug) {
		alert(resp.responseText);
	}
	
	var success = resp.responseXML.getElementsByTagName("Result");
	if (success.length == 1 && success[0].firstChild.nodeValue == "true") {
		document.getElementById("sendToFriendCaption").innerHTML = "Your email has been sent.";
		document.getElementById("sendAgainButton").style.display = "";
	} else {
		var errorText = "Your email could not be sent, the following errors were encountered:<br/><span class='commentsError'>";
		var errors = resp.responseXML.getElementsByTagName("Error");
		
		for (var i=0; i<errors.length; i++) {
			errorText += errors[i].firstChild.nodeValue;
			if (i < errors.length - 1) {
				errorText = errorText + "<br/>";
			}
			
		}
		errorText += "</span>";
		document.getElementById("sendToFriendCaption").innerHTML = errorText;
		document.getElementById("retrySendButton").style.display = "";	
	}
	
}

function sendToFriendErrorCallback(resp) {
	if(ajaxDebug) {
		alert(resp.responseText);
	}
	document.getElementById("sendToFriendCaption").innerHTML = "Your email could not be sent, press Retry to try again";
	document.getElementById("retrySendButton").style.display = "";
}

function resetSendToAFriendVisibility() {
	document.getElementById("retrySendButton").style.display = "none";
	document.getElementById("formFields").style.display = "";
	document.getElementById("sendToFriendCaption").style.display = "none";
	document.getElementById("sendToAFriendButton").style.display = "";
	document.getElementById("sendAgainButton").style.display = "none";
}

function restSendToAFriendForm() {
	document.forms.sendForm.reset();
	resetSendToAFriendVisibility();
}

function sendToAnotherFriend() {
	resetSendToAFriendVisibility();
	var theForm = document.forms.sendForm;
	theForm.toName.select();
	theForm.toName.focus();
	theForm.toName.value = initialToName;
	theForm.toEmail.value = initialToEmail;
}

// ------------------------ How you heard code ----------------------------
function howYouHeard() {
	var showDetails = document.getElementById("whereOther").checked;;
	var showRadio = document.getElementById("whereRadio").checked;
	
	document.getElementById("radioRow").style.display = showRadio ? "" : "none";
	document.getElementById("detailsRow").style.display = showDetails ? "" : "none";
}

function validateHowYouHeard(theForm) {
	var showDetails = document.getElementById("whereOther").checked;;
	var showRadio = document.getElementById("whereRadio").checked;
	
	var errors = new Array();
	var focusField = null;
	
	//Validate that at least one radio button is checked
	if (!showDetails && !showRadio && !document.getElementById("whereFlyer").checked &&
	 !document.getElementById("whereFriend").checked) {
		errors[errors.length] = "Please select how you heard about NotASermon.org";
	}
	
	//If they selected Radio then check that the radio station is specified
	if (showRadio && theForm.whichRadio.value == "") {
		errors[errors.length] = "Radio Station" + requiredText;
		focusField = theForm.whichRadio;
	}
	
	//Validate the other field if they selected other
	if (showDetails && validateRequired(theForm.otherDetails.value, "Details", "none")!=null) {
		errors[errors.length] = validateRequired(theForm.otherDetails.value, "Details", "none");
		focusField = theForm.otherDetails;
	}
	
	//If other details are supplied then validate that they are clean
	if (validateRequired(theForm.otherDetails.value, "Details", "none")==null) {
		var error = validateCleanText(theForm.otherDetails.value, "Details");
		if (error != null) {
			errors[errors.length] = error;
			focusField = theForm.otherDetails;
		}
	}
	
	var emailRequired = theForm.getNewsletter.checked;
	
	//Validate the email field if they entered anything
	if (validateRequired(theForm.email.value, "Email", "none")==null) {
		var error = validateEmail(theForm.email.value);
		if (error != null) {
			errors[errors.length] = error;
			focusField = theForm.email;
		}
	} else if (emailRequired) {
		errors[errors.length] = "Email is required if you would like to receive the newsletter";
		focusField = theForm.email;
	}
	
	//If comments are supplied then validate that they are clean
	if (validateRequired(theForm.comments.value, "comments", "none")==null) {
		var error = validateCleanText(theForm.comments.value, "Comments");
		if (error != null) {
			errors[errors.length] = error;
			focusField = theForm.comments;
		}
	}
	
	if (errors.length > 0) {
		showErrors(errors);
	}
	
	if (focusField != null) {
		focusField.focus();
	}
	
	return errors.length == 0;
	
}


// ------------------------ Comments Management Code ------------------------
function sendComments(formName){
	var theForm = document.forms[formName];
	var errors = new Array();
	var error = validateRequired(theForm.comment.value, "Comments", "none");
	
	if (error != null) {
		errors[errors.length] = error;
	} else {
		error = validateCleanText(theForm.comment.value, "Comments");
		if (error != null) {
			errors[errors.length] = error;
		}
	}
	
	error = validateRequired(theForm.name.value, "Your Name", "Your Name (optional)");
	//if error is null then there is text in there
	if (error==null) {
		error = validateCleanText(theForm.name.value, "Your Name");
		if (error != null) {
			errors[errors.length] = error;
		}
	}
	
	error = validateRequired(theForm.email.value, "Your Email", "Email Address (optional)");
	//if error is null then there is text in there
	if (error==null) {
		error = validateEmail(theForm.email.value, "Your Email");
		if (error != null) {
			errors[errors.length] = error;
		}
	}
	
	if (errors.length != 0) {
		showErrors(errors);
		return false;
	}
	
	//pass true in as a parameter to get debug
	var client = new HttpClient(ajaxDebug);
	var formParams = postDataFromForm(document.forms[formName]);
	var method = new GetMethod("PostComment.aspx?ajax=true" + formParams, commentSuccessCallback, commentErrorCallback);
	client.executeMethod(method);
}

function commentSuccessCallback(resp) {
	if (ajaxDebug) {
		alert(resp.responseText);
	}
	var success = resp.responseText.indexOf("SUCCESS")>0;
	if (success) {
		document.forms["commentsForm"].style.display = "none";
		document.getElementById("commentsSuccess").style.display = "";
		document.getElementById("submitComment").style.display = "none";
		document.getElementById("closeComment").style.display = "";
	}
}

function closeComments() {
	//Reset the comments form
	document.forms["commentsForm"].comment.value = "";
	document.getElementById("thoughts").style.visibility = "hidden";
	document.forms["commentsForm"].style.display = "";
	document.getElementById("commentsSuccess").style.display = "none";
	document.getElementById("submitComment").style.display = "";
	document.getElementById("closeComment").style.display = "none";
}

function toggleComments() {
	var thoughts = document.getElementById("thoughts");
	if (thoughts.style.visibility == "visible"){
		thoughts.style.visibility = "hidden";
	} else {
		thoughts.style.visibility = "visible";
	}
}

function commentErrorCallback() {
	document.getElementById("commentsError").innerHTML = "An error occured saving your comment, please try again<br>";
	document.getElementById("commentsError").style.display = "";
}
