var delay = 1000;

function addSermonToOrder(limit) {
	var remaining = (limit - $('#sermonsToOrder').children().length);
	if (remaining > 0) { 
		$('#sermonsToOrder').append('<li id="sermon' + $("#sermonID").val() + '"><a href="javascript:removeSermonFromOrder(' + $("#sermonID").val() + ',' + limit + ')"><img src="http://cgg.org/images/grey_del.gif" border="0" hspace="4" alt="Remove this from the list" /></a><input type="hidden" name="sermons" value="' + $("#sermonID").val() + '" />' + $("#sermonTitle").val() + '</li>');
		if ((remaining - 1) == 0) {
			$('#tapesRemaining').html('<em style="color:#f00;">0 remaining</em>')
		} else {
			$('#tapesRemaining').html('<em>' + (remaining - 1) + ' remaining</em>')
		}
		
		$('#title').val('');
	} else {
		$('#title').attr("disabled", true)		
	}
}
function removeSermonFromOrder(ID,limit) {
		$('#sermon' + ID).remove();
		$('#title').removeAttr("disabled");
		var remaining = (limit - $('#sermonsToOrder').children().length);
		$('#tapesRemaining').html('<em>' + remaining + ' remaining</em>');
		
}
function bereanpopup() 
{
	if (getCookie("popup") != "popped") {
		var expire = new Date((new Date()).getTime() + 1000 * 24 * 3600000);
		setCookie("accept", "oreo", expire, "/", "", false); 
	
		if (getCookie("accept")) {
			if(typeof showAd != "undefined") {
				showAd();
				setCookie("popup", "popped", expire, "/", "", false);
			}
		}
	}
	return true;
}
function closeKeywords() {
	$("div#sermonKeywords").hide();
}
function closeShareThis() {
	$("div#akst_form").hide();
}
function closeDIV(DIVid) {
	$("div#" + DIVid).hide();
}
function continueContact() {
	var inputs = [];
	var selected = false;
	$("#active_continue").show();
	$("#continue_button").attr("disabled", true)
	
    $(":input", document.forms[1]).each(function() { 
		if (this.type != "checkbox" || (this.type == "checkbox" && this.checked == true)) {
			inputs.push(this.name + '=' + escape(this.value)); 
			if (this.type == "checkbox" && this.checked == true) { selected = true; };
		}});
	if (selected == true) {
		jQuery.ajax({type: "POST", data: inputs.join('&'), dataType: "html", url: "index.cfm?fuseaction=home.contact", timeout: 100000, success: function(r) {$('#contactform').html(r);}})
	} else {
		$("#contactInstruct").addClass("contactInstructRed");
	}
}

function emailPage() {
	$('div#responseContainer').html('');
	var tsTimeStamp= new Date().getTime();
	//var inputs = [];
    //$(':input', this).each(function() { {inputs.push(this.name + '=' + escape(this.value)); }})
	loading();
	jQuery.post("http://cgg.org/index.cfm?fuseaction=Email.processEmailToFriend", {toAddress: $("input[@name=toAddress]").val(), fromAddress: $("input[@name=fromAddress]").val(), efa: $("input[@name=efa]").val(), CT: $("input[@name=CT]").val(), k: $("input[@name=k]").val(), ID: $("input[@name=ID]").val(), time: tsTimeStamp, comments: escape($("textarea[@name=comments]").val())}, function(r) { $('div#responseContainer').html(r); });
	setTimeout("loaded()", delay);
	setCookie("email", $("input[@name=fromAddress]").val(), "", "/", "", false);
}
function getCookie(name) {
	var dc = document.cookie;
	
	// find beginning of cookie value in document.cookie
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else begin += 2;
	
	// find end of cookie value
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;
	
	// return cookie value
	return unescape(dc.substring(begin + prefix.length, end));
}
function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + '=' + escape(value) + ((expires) ? '; expires=' + expires.toGMTString() : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure' : '');
	document.cookie = curCookie;
}
function setClickPath() {
}

function loadPageJS()
{
	bereanpopup();
}
function loaded() {
	$("#ajaxWorkingEmail").removeClass("ac_loading");
}
function loading() {
	$("#ajaxWorkingEmail").addClass("ac_loading");
}
function chanout() {
}
function showAd() {
	document.getElementById('bereanad').style.visibility = 'visible';
}

function hideAd() {
	document.getElementById('bereanad').style.visibility = 'hidden';
}
function showEmailFriendAJAX() {
	var elem = document.getElementById('emailPageContainer');
	document.getElementById('responseContainer').innerHTML = '';
	elem.style.display = 'block';
    elem.style.visibility = 'visible';
	document.getElementById('toAddress').value = '';
	document.getElementById('emailComments').value = '';
	if (getCookie("email") != ("" || null))
	{
		document.getElementById('fromAddress').value = getCookie("email");
	}
	document.getElementById('toAddress').focus();

}
function hideEmailFriendAJAX() {
	var elem = document.getElementById('emailPageContainer');
	elem.style.display = 'none';
    elem.style.visibility = 'hidden';
}
function showKeywords() {
	$("div#sermonKeywords").show();
}
function toggleDescription() {
	if ($('#descriptionLink').html() == 'show') {
		$("div#sermonDescription").show();
		$('#descriptionLink').html('hide');
	}
	else {
		$("div#sermonDescription").hide();
		$('#descriptionLink').html('show');
	}
}
function toggleKeywords() {
	if ($('#keywordsLink').html() == 'show') {
		$("div#sermonKeywords").show();
		$('#keywordsLink').html('hide');
	}
	else {
		$("div#sermonKeywords").hide();
		$('#keywordsLink').html('show');
	}
}
function transcriptNotify() {
	var inputs = [];
    $(':input', this).each(function() { if (this.name == 'email' || this.name == "ID") {inputs.push(this.name + '=' + escape(this.value)); }})
	//jQuery.ajax({type: "GET", data: inputs.join('&'), dataType: "html", url: "index.cfm?fuseaction=Audio.transcriptNotify", timeout: 10000, error: function() {console.log("Failed to reach the server");}, success: function(r) {document.getElementById('TranscriptNotify').innerHTML = r;}})
	jQuery.post("http://cgg.org/index.cfm?fuseaction=Audio.transcriptNotify", {email: $("input[@name=email]").val(), ID: $("input[@name=ID]").val()}, function(r) { $('div#TranscriptNotify').html(r); });
}