// JavaScript Document

var chatDiv;

function writePastorSide()
{
	var flashObject = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="544" height="104">'
    + '    <param name="movie" value="pastorSide_landscape.swf" />'
    + '    <param name="quality" value="high" />'
    + '    <param name="wmode" value="opaque" />'
    + '    <embed src="pastorSide_landscape.swf" quality="high" wmode="opaque" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="544" height="104"></embed>'
    + '    </object>'
	document.write(flashObject);
}


function writeMessageVid(file, lcid, ip, image)
{
	var flashObject = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="544" height="408">'
    + '    <param name="movie" value="player.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '" />'
    + '    <param name="quality" value="high" />'
    + '    <embed src="player.swf?video=' + file + '&lcid=' + lcid + '&ip=' + ip + '&image=' + image + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="544" height="408"></embed>'
    + '    </object>'
	document.write(flashObject);
}



function writeExperienceThumb(id, ip)
{
	var flashObject = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="200" height="96">'
    + '    <param name="movie" value="experienceThumb.swf?id=' + id + '" />'
    + '    <param name="quality" value="high" />'
    + '    <embed src="experienceThumb.swf?id=' + id + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="200" height="96"></embed>'
    + '    </object>'
	document.write(flashObject);
	//alert(flashObject)
}


function writeInternetHeader(type)
{
	var flashObject = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="998" height="73">'
	+ '    <param name="movie" value="internetHeader.swf?type=' + type + '" />'
	+ '    <param name="quality" value="high" />'
	+ '    <embed src="internetHeader.swf?type=' + type + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="998" height="73"></embed>'
 	+ '    </object>'
	document.write(flashObject);
}


function format(type,align)
{
	str = document.selection.createRange();
	if (str.htmlText != "")
	{
		switch (type)
		{
			case "Bold":
				str.execCommand("bold");
			break;
			
			case "Italic":
				str.execCommand("italic");
			break;
				
			case "InsertImage":	
				
				tmpFile = document.getElementById("pastorimages").value;

				if(align == "right")
				{
					newStr = "<img src='images/ipastor/" + tmpFile + "' align='right' />" + str.htmlText;
				}
				else if(align == "left")
				{
					newStr = "<img src='images/ipastor/" + tmpFile + "' align='left' />" + str.htmlText;
				}
				else 
				{
					newStr = "<img src='images/ipastor/" + tmpFile + "' />" + str.htmlText;
				}
				str.execCommand("Delete");
				str.pasteHTML(newStr);			
			break;
		}
	type = ""		
	}
	setHdnText();
}

function save(id){
	
	document.getElementById("text" + id).innerHTML = document.getElementById("textfield" + id).innerText; 
	document.getElementById("text" + id).style.display = "block"
	document.getElementById("textfield" + id).style.display = "none"
	document.getElementById("view" + id).value = "View HTML"	
	
	
	document.getElementById("hdnid").value = id;
	tmpString = document.getElementById("text" + id).innerHTML

	tmpText = tmpString.replace(/<strong>/gi,"<b>");
	tmpText = tmpText.replace(/<\/strong>/gi,"</b>");
	
	tmpText = tmpText.replace(/<em>/gi,"<i>");
	tmpText = tmpText.replace(/<\/em>/gi,"</i>");
							  
	document.getElementById("hdntext").value = tmpText;
	document.getElementById("textform").submit();
}


function makeActive(id){
	document.getElementById("hdnactiveid").value = id;
	document.getElementById("activeform").submit();
}



function swapView(id)
{
	
	if(document.getElementById("textfield" + id).style.display == "none" || document.getElementById("textfield" + id).style.display == "")
	{
		document.getElementById("textfield" + id).innerText = document.getElementById("text" + id).innerHTML;
		document.getElementById("text" + id).style.display = "none"
		document.getElementById("textfield" + id).style.display = "block"
		document.getElementById("view" + id).value = "View Normal"
	}
	else
	{
		document.getElementById("text" + id).innerHTML = document.getElementById("textfield" + id).innerText; 
		document.getElementById("text" + id).style.display = "block"
		document.getElementById("textfield" + id).style.display = "none"
		document.getElementById("view" + id).value = "View HTML"	
	}
	
}


function setHdnText()
{
	updateHTML(2);
	updateHTML(3);
}

function updateHTML(id)
{
	element = "text" + id;
	tmpElement = "textfield" + id;
	document.getElementById(tmpElement).innerText = formatHtml(cleanhtml(document.getElementById(element).innerHTML));
}

function updateDiv(id)
{
	element = "text" + id;
	tmpElement = "textfield" + id;
	document.getElementById(element).innerHTML = document.getElementById(tmpElement).innerText;
}

function formatHtml(input)
{
	var tmpHtml = input.replace(/<br>/gi, "<br>\n" );
	tmpHtml = tmpHtml.replace(/<br \/>/gi, "<br />\n" );
	tmpHtml = tmpHtml.replace(/<a /gi, "\n<a " );
	tmpHtml = tmpHtml.replace(/<\/a>/gi, "</a>\n" );
	
	return(tmpHtml);
}


function cleanhtml(temphtml)
{
	//temphtml = thehtml.innerHTML;

	// get the html tags and loop through them
	tempregexp = new RegExp("<[^>]+>", "g")
	results = temphtml.match(tempregexp);

	if (results != null)
	{
		for (i=0; i < results.length; i++) {
			original = results[i];
			
			// temporarily strip the already quoted attributes and loop through them
			stripquoted = results[i].replace(/ [^=]+= *"[^"]*"/g,"");
			tempregexp = new RegExp(" [^=]+=[^ |>]+", "g")
			unquoted = stripquoted.match(tempregexp);
			if (unquoted) {
				for (j=0; j < unquoted.length; j++) {
					// add quotes to unquoted attributes					
					addquotes = unquoted[j].replace(/( [^=]+=)([^ |>]+)/g, "$1\"$2\"");
					results[i] = results[i].replace(unquoted[j],addquotes);
				}
			}
			// convert tags to lowercase
			results[i] = results[i].replace(/<\/?[^>|^ ]+/, function(x) { return x.toLowerCase() })
			// convert attributes to lowercase
			results[i] = results[i].replace(/ [^=]+="/g, function(y) { return y.toLowerCase() })
			// finally replace the existing tag with the new tag
			temphtml = temphtml.replace(original, results[i]);
		}
	}
	// this strips out tbody tags.
	temphtml = temphtml.replace(/<\/?tbody>[^<]*/gi,"");
	temphtml = temphtml.replace(/http:\/\/shed\.seacoast\.org/gi,"");		
	temphtml = temphtml.replace(/<img([^>]+)>/gi, "<img$1 />");		
	temphtml = temphtml.replace(/<br>/gi, "<br />");	
	temphtml = temphtml.replace(/<hr>/gi, "<hr />");	
	return temphtml
}


function writeNewCrossPrayer(text)
{
	text = text.replace(/<script /gi,"");
	text = text.replace(/<\/script> /gi,"");
	text = text.replace(/<TEXTFORMAT LEADING=\"5\">/gi,"");
	text = text.replace(/<FONT FACE=\"Stanton ICG\" SIZE=\"18\" COLOR=\"#000000\" LETTERSPACING=\"0\" KERNING=\"0\">/gi,"");
	text = text.replace(/<\/FONT>/gi,"");						
	text = text.replace(/<\/TEXTFORMAT>/gi,"");	
	
	//alert("Please Clear your cache for the latest version --- " + text)
	
	
	document.getElementById("hdnprayertext").value = text;
	document.getElementById("newprayerfrm").submit();
}


//deprecated
function openExperience_old(type)
{
	var ScreenWidth=window.screen.width;
    var ScreenHeight=window.screen.height;	
    var movefromedge=0;
    placementx=(ScreenWidth/2)-((998)/2);
    placementy=(ScreenHeight/2)-((408+50)/2);


	tmpURL = "expWindow.asp?type=" + type
				
	window.open(tmpURL,"Experience","width=998,height=408,toolbar=0,location=0,directories=0,status=0,scrollbars=0,menubar=0,resizable=0,left="+placementx+",top="+placementy+",screenX="+placementx+",screenY="+placementy+",");
}


function openExperience(type)
{

		var content;
		
		switch (type)
		{
			case "cross":	
				content = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="998" height="408">'
				+ '    <param name="movie" value="theCross.swf?id=1548" />'
				+ '    <param name="quality" value="high" />'
				+ '    <embed src="theCross.swf?id=1548" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="998" height="408"></embed>'
				+ '    </object>'
			break;				
			
			case "candle":
				content = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="998" height="408">'
				+ '    <param name="movie" value="theCandle.swf?id=1549" />'
				+ '    <param name="quality" value="high" />'
				+ '    <embed src="theCandle.swf?id=1549" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="998" height="408"></embed>'
				+ '    </object>'
			break;
						
			case "prayer":
				content = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="998" height="408">'
				+ '    <param name="movie" value="communion.swf?id=1550" />'
				+ '    <param name="quality" value="high" />'
				+ '    <embed src="communion.swf?id=1550" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="998" height="408"></embed>'
				+ '    </object>'
			break;
				
			case "communion":
				content = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="998" height="408">'
				+ '    <param name="movie" value="communion.swf?id=1551" />'
				+ '    <param name="quality" value="high" />'
				+ '    <embed src="communion.swf?id=1551" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="998" height="408"></embed>'
				+ '    </object>'

			break;
			
			case "tithe":	
				content = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="998" height="408">'
				+ '    <param name="movie" value="communion.swf?id=1552" />'
				+ '    <param name="quality" value="high" />'
				+ '    <embed src="communion.swf?id=1552" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="998" height="408"></embed>'
				+ '    </object>'
			break;
		}
		
		document.getElementById("experienceDiv").innerHTML = content;
		document.getElementById("experienceDiv").style.display = "block";
		document.getElementById("expClose").style.display = "block";

}

function closeExperience()
{
	document.getElementById("experienceDiv").style.display = "none";
	document.getElementById("expClose").style.display = "none";
	document.getElementById("experienceDiv").innerHTML ="";
}

function showNotes()
{
	document.getElementById("notes").style.zIndex='5';
}

function closeNotes()
{
	document.getElementById("notes").style.zIndex='1';
}

function chat(type)
{

		var content;

		
		switch (type)
		{
			case "login":	
				chatDiv = document.getElementById("chatInner").innerHTML;
				content = '<form method="post" id="chatAuthForm" action="mc_auth.php">'
				+ '<div id="chatLogin"><span style="float:right;"><a href="javascript:chat();"><img src="images/closeWindowBtn.jpg" border="0"></a></span><br/><h3>Enter Chat:</h3>'
				+ '<label for="name">What is your Name:</label><input type="text" size="20" name="name" id="name"/><br /><br />'
				+ '<label for="location">Where are you from?:</label><input type="text" size="20" name="location" id="location" /><br />'
				+ '<br /><input type="button" id="join" name="join" value="Join the Chat!" onClick="chatAuth(); return false;" />'
				+ '</form></div>'
			break;				

			default:	
				content = chatDiv;
			break;

		}
		
		document.getElementById("chatInner").innerHTML = content;
}

function chatAuth() {
	var formName = document.getElementById("name").value;
	var formLocation = document.getElementById("location").value;
	$('chatAuthForm').request({
	  method: 'post',
	  parameters: { name: formName, location: formLocation },
	  onComplete: chat() 
	})
}

//start the auto-update of the service
//var serviceUpdater = new Ajax.PeriodicalUpdater('messageVideo', '/icampusdev/lightcast_embed.php', {
//  method: 'get',
//  frequency: 60,
//  evalScripts: true,
//  evalJS: true,
//});
//serviceUpdater.start();