/*
File: js/functionlibrary.js	
Author: Nathan Davies
Created: 05/11/2007	 
Ammened: 
*/	

/*------------------------------------------------------------
	GENERIC AJAX FUNCTIONS
------------------------------------------------------------*/
function GetXmlHttpObject()
{ 
	if (window.XMLHttpRequest)
	{
		loXMLHTTP=new XMLHttpRequest() ;
	}
	else if (window.ActiveXObject)
	{
		loXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP") ;
	}	 
	if (loXMLHTTP==null)
	{
		alert ("Browser does not support HTTP Request")	;
	 	return	;
	}
	return loXMLHTTP ;
} 

function submitXMLHTTP(poXMLHTTP, pcIDToWriteTo, pcOpenMethod, pcURL, plOpen, pcSendData)
{																						
	poXMLHTTP.onreadystatechange = function() 
	{ 
		// use of ghost function enables the system to execute each item in the correct order 
		writeResponse(poXMLHTTP, pcIDToWriteTo); 
	};
	poXMLHTTP.open(pcOpenMethod,pcURL,plOpen) ;
	poXMLHTTP.send(pcSendData) ; 
} 

function writeResponse(poXMLHTTP, pcEmelentID) 
{ 	   
	if (poXMLHTTP.readyState==4 || poXMLHTTP.readyState=="complete")
	{ 
		if (poXMLHTTP.status == 200)
		{
			document.getElementById(pcEmelentID).innerHTML=poXMLHTTP.responseText ;  
		}
	} 
} 

/*------------------------------------------------------------
	MENU/DISPLAY HANDLING FUNCTIONS
------------------------------------------------------------*/	
function menuHandler(pnType, pnMenuID, pcWriteToID)
{												  
	if(trim(pcWriteToID) == "")
	{
		pcWriteToID = "mainContent" ; // default value
	}
	
	llClearSubMenu  = false ; // this is set to false for submenu items in the switch statement below
	
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcBaseURL = "pagedefinition/" ;
	lnIndex = 99 ; // default value	
	llMakeRecursiveCall = false ; 	 
	llRewriteURL = true ;
	
	// determine which URL we are loading to handle this menu selection
	switch(pnType)
	{
		case 1: // Main content  
			lcURL = lcBaseURL + "maincontent.php?page=" + pnMenuID ; 
			llClearSubMenu  = true ;
			break ;
		case 2: case 4: case 5:// Resources, Events and Training - writes a new menu into the sub menu container
			lcURL = lcBaseURL + "submenudefine.php?parent=" + pnMenuID ;  
			llClearSubMenu  = true ; 
			switch (pnType)
			{
				case 2:	   
					lnIndex = 0 ;
					break ;
				case 4:			 
					lnIndex = 1 ;
					break ;
				case 5:		   
					lnIndex = 2 ;
					break ;
			}
			llMakeRecursiveCall = true ;
			break ;
		case 3: // News - show a list of headlines or the full story
			lcURL = lcBaseURL + "news.php?story=" + pnMenuID ;
			llClearSubMenu  = true ;
			break ;
		case 6: // Forum
			lcURL = lcBaseURL + "forum.php?type=" + pnMenuID;
			llClearSubMenu  = true ;
			break ;
		case 7: // Contact Us - contains AJAX forms to enable people to contact us for different matters 
			lcURL = lcBaseURL + "contact.php" ;
			llClearSubMenu  = true ;
			break ;
		case 8: // event details - specific page as accessed from infoPanel, or from event listing, this will also give the listing	
			lcURL = lcBaseURL + "events.php?event=" + pnMenuID ;			
			break ;
		case 9: // articles - submenu of resources
			lcURL = lcBaseURL + "latestarticle.php" ;
			break ;
		case 10: // downloads - submenu of resources
			lcURL = lcBaseURL + "downloadcentre.php";
			break ;	
		case 11: case 12: case 13: case 21: // reviews - initial listing of reviews submitted - download(11), article(12), event(13) or training course(21) determined by the query string
			lcURL = lcBaseURL + "review.php?reviewtype=" + pnType + "&item=" + pnMenuID ; // QS tested in php to determine the output 
			llRewriteURL = false ;
			break ;	
		case 14: // event review landing page
			lcURL = lcBaseURL + "eventreview.php?event="  + pnMenuID ;
			break ;	
		case 15: // authors, list the biog then all articles plus synopsis written by that author
			lcURL = lcBaseURL + "author.php?author=" + pnMenuID ;
			break ;
		case 16: // article - full list or specific article
			lcURL = lcBaseURL + "article.php?article=" + pnMenuID ;
			break ;	 
		case 17: case 18: // booking events(17) and courses(18)
			laParameter	= pnMenuID.split('-') ;
			lnType		= laParameter[0] ;
			lnItem		= laParameter[1] ;
			lnFood		= laParameter[2] ;		
			ldStartDate	= laParameter[3];	
			lcURL = lcBaseURL + "booking.php?type=" + lnType + "&item=" + lnItem + "&food=" + lnFood + "&target=" + pcWriteToID +"&start=" + ldStartDate;		
			llRewriteURL = false ;
			break ;																  
		case 19: // training courses - lists and details 
			lcURL = lcBaseURL + "training.php?course=" + pnMenuID ;
			break ;
		case 20: // training reviews - list and details
			lcURL = lcBaseURL + "trainingreview.php?course=" + pnMenuID ;
			break ;
		case 22: // review submission, pnMenu indicates what is being reviewed x-y (x= type; 1=download, 2=event, 3=article, 4=training. y=itemID)
			laParameter	= pnMenuID.split('-') ;
			lnType		= laParameter[0] ;
			lnItem		= laParameter[1] ;
			lcURL = lcBaseURL + "reviewsubmission.php?type=" + lnType + "&item=" + lnItem + "&target=" + pcWriteToID  ;	   
			llRewriteURL = false ;
			break ;
		case 23: // generic contact form
			lcURL  = lcBaseURL + "contactform.php" ;
			break ;
		case 24: // My CL.O
			lcURL = lcBaseURL + "myclo.php?section=" + pnMenuID ; // required information is pulled from $_SESSION 
			llClearSubMenu  = true ;
			break ;		   
		case 25: // password reset
			lcURL = lcBaseURL + "passwordreset.php" ;
			llClearSubMenu  = true ;  
			break ;	  
		case 26: // welcome
			lcURL = lcBaseURL + "welcome.php" ;	
			llClearSubMenu  = true ;
			break ;
		case 27: // load the thread list for a forum		 
			lcURL = lcBaseURL + "thread.php?forum=" + pnMenuID ;
			break ;
		case 28: // load the posts for a particular thread		
			lcURL = lcBaseURL + "post.php?thread=" + pnMenuID ;	
			break ;												
		case 29: //	start a new thread/post
			laParameter	= pnMenuID.split('-') ;
			lnType		= laParameter[0] ;
			lnItem		= laParameter[1] ;
			lcURL = lcBaseURL + "forumsubmission.php?type=" + lnType + "&item=" + lnItem + "&target=" + pcWriteToID  ;	
			llRewriteURL = false ;
			break; 
		case 30: // subscribe to thread
			lcURL = "lib/threadsubscription.php?thread=" + pnMenuID ; 
			break ;	
		case 31: // define landing page
			lcURL = lcBaseURL + "landingpage.php?index=" + pnMenuID ;
			break; 
		case 32: // sub-sectios of 'My Account' 
			lcURL = lcBaseURL + "myclodetails.php?section=" + pnMenuID ;
			break ;
		case 33: // unregister
			lcURL = lcBaseURL + "unregister.php" ;		  
			break ;
		case 34: // speaker - biog plus all future events speaking at and downloads from past events
			lcURL = lcBaseURL + "speaker.php?speaker=" + pnMenuID ;
			break ;	
		case 35: // doanloads listing - the actual downloads
			lcURL = lcBaseURL + "downloads.php?typeID=" + pnMenuID ;
			break ;	
		case 36: case 37: // multi-booking events(36) and courses(37)
			laParameter	= pnMenuID.split('-') ;
			lnType		= laParameter[0] ;
			lnItem		= laParameter[1] ;
			lnFood		= laParameter[2] ;	
			llBookSelf	= laParameter[3] ;	  
			ldStartDate	= laParameter[4];
			lcURL = lcBaseURL + "booking_multi.php?type=" + lnType + "&item=" + lnItem + "&food=" + lnFood + "&target=" + pcWriteToID + "&bookself=" + llBookSelf +"&start=" + ldStartDate;		
			llRewriteURL = false ;
			break ;					
		case 38: // load the page displaying the links
			lcURL = lcBaseURL + "links.php" ;
			break ;				  
		case 39: // sermon outlines	
			laParameter	= pnMenuID.split('-') ;
			lnTopic		= laParameter[0] ; // if 0 then list the newest with links to the topic categories - possibly a cbo
			if(lnTopic == "X") // get it from a cbo control
			{
				lnTopic = document.getElementById('cbo_topic').value;
			}
			lnSermon	= laParameter[1] ; // if 0 then list all the sermon outlines available under topic
			lcURL = lcBaseURL + "sermon.php?topicID=" + lnTopic + "&sermonID=" + lnSermon ; 
			break ;
	}
	
	submitXMLHTTP(loXMLHTTP, pcWriteToID, "GET", lcURL, true, null) ;

	var laSubMenuID = new Array() ;
	laSubMenuID[0] = 'subMenu3' ;
	laSubMenuID[1] = 'subMenu5' ;
	//laSubMenuID[2] = 'subMenu6' ;  
	 
	for(i=0; i<=1; i = i+1)
	{
		if(i!=lnIndex)
		{
			if(llClearSubMenu == true)
			{
				lcSubMenuToCollapse =  laSubMenuID[i];
				writeBlank(lcSubMenuToCollapse); 
				setDisplayState(lcSubMenuToCollapse, 'none') ; 
			}
		}									   
		else
		{
			toggleDisplayState(laSubMenuID[i]) ;
		}
	}
	
	if(llMakeRecursiveCall==true)
	{
		menuHandler(31,pnMenuID,'mainContent') ;
	}

	if(llRewriteURL == true)
	{
		lcBaseURL = window.location.href ;
		lcBaseURL = lcBaseURL.substring(0, lcBaseURL.indexOf('#')) ;
		window.location.href = lcBaseURL + '#para1=' + pnType + '|para2=' + pnMenuID ;
	}
}

function setDisplayState(poObject, pcState)
{
	var loObject = document.getElementById(poObject); 
	loObject.style.display = pcState ;
}															

function toggleDisplayState(poObjectToDisplay)
{														 
	
	var loObject = document.getElementById(poObjectToDisplay);

	//test the current state of the object and toggle opposite state.
	if (loObject.style.display != '' && loObject.style.display != 'none') 
	{
		loObject.style.display = 'none';
	}
	else 
	{
		loObject.style.display = 'block';
	} 
} 


function writeBlank(pcContainerID)
{
	var loObject ;
	loObject = document.getElementById(pcContainerID) ;
	loObject.innerHTML = "" ; // simple reset as the link to display the code was never removed
}		

/*------------------------------------------------------------
	GENERAL VARIABLE MANIPULATION
------------------------------------------------------------*/
function trim(stringValue)
{
	return stringValue.replace(/(^\s*|\s*$)/, "");
}  

/*------------------------------------------------------------
	FORM VALIDATION
------------------------------------------------------------*/
function validateItem(pnType, pcItemID, pcTarget)
{
	var loXMLHTTP, lcURL, lcValue ;
	
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcValue	= document.getElementById(pcItemID).value ;
	lcURL	= "lib/verify.php?type=" + pnType + "&value=" + lcValue ;
	  
	submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURL, true, null) ;
}
/*------------------------------------------------------------
	FORM SUBMISSSION
------------------------------------------------------------*/
function handleForm(pcFormID, pcWriteToID, plUseDOM, pnType, pnItemID)
{
	// using the elements array in the dom we are able to build up a string of values to be passed to the server
	var lcQueryString = "" ;
	var lcValue ; 
	var llStandard = true ;

	if(plUseDOM)
	{
		var laFormElements = document.getElementById(pcFormID) ;
	
		for(lnArrayPntr = 0; lnArrayPntr < laFormElements.length; lnArrayPntr++)
		{
			lcValue = laFormElements.elements[lnArrayPntr].value ;
			lcValue = lcValue.replace(/\n/g, '<br />') ;
			lcQueryString = lcQueryString + "item" + lnArrayPntr + "=" + lcValue + "&" ;
		}
		
	}
	else
	{	 
		var laFormElements = pcFormID.split(',') ;
		for(lnArrayPntr = 0; lnArrayPntr < laFormElements.length; lnArrayPntr++)
		{
			lcElementID = laFormElements[lnArrayPntr] ;	
			lcValue = document.getElementById(lcElementID).value ;
			lcValue = lcValue.replace(/\n/g, '<br />') ;
			lcQueryString = lcQueryString + "item" + lnArrayPntr + "=" + lcValue + "&" ;
		}
	}
	switch(pnType)
	{
		case 1: case 2: case 3: case 4: // handle review form - 1=download, 2=event, 3=article, 4=training
			lcURL = "lib/reviewhandler.php?type=" + pnType + "&" + lcQueryString + "itemID=" + pnItemID ;
			break ;
		case 5: case 6: // handle booking form 5=event, 6=training
			lcURL = "lib/bookinghandler.php?type=" + pnType + "&" + lcQueryString + "itemID=" + pnItemID ;
			break ;	
		case 7: // general contact form	
			lcQueryString =	lcQueryString.substring(0, lcQueryString.length-1) ;
			lcURL = "lib/contacthandler.php?" + lcQueryString ;
			break ;
		case 8: case 9: // handle forum submission
			lcURL = "lib/forumhandler.php?type=" + pnType + "&" + lcQueryString + "itemID=" + pnItemID ; 
			llStandard = false ;
			break ;	 
		case 10: // handle password change
			lcURL = "lib/passwordchangehandler.php?" + lcQueryString + "userID=" + pnItemID ;
			break;	
		case 11: // handle personal details changes
			lcURL = "lib/mydetailshandler.php?" + lcQueryString + "userID=" + pnItemID ; 
			break ;	   
		case 12: case 13: // handle multi-bookings form 12=event, 13=training 	
		//update txt_count - item2 - in the QueryString to get the required values from the input
			lcElementID = laFormElements[2]	;
			lnCount = parseInt(document.getElementById(lcElementID).value) ;
			for(i=1; i<=lnCount; i++)
			{
				lcNameID	= "txt_name" + i.toString() ;
				lcFoodID	= "cbo_food" + i.toString() ;
				lcName		= document.getElementById(lcNameID).value ;
				if(document.getElementById(lcFoodID))
				{
					lcFood	= document.getElementById(lcFoodID).value ;
				}
				else	
				{
					lcFood	= "0";
				}
				lcQueryString = lcQueryString + lcNameID + "=" + lcName + "&" + lcFoodID + "=" + lcFood + "&";
			}
			lcURL = "lib/bookinghandler_multi.php?type=" + pnType + "&" + lcQueryString + "itemID=" + pnItemID + "&delegateCount=" + lnCount ;
			break ;	
		case 14: // move a thread to a new forum
			lcElementID = laFormElements[0] ;
			lnValue = parseInt(document.getElementById(lcElementID).value) ;
			lcNewForum = document.getElementById(lcElementID).options[lnValue].text ;
			lcURL = "lib/moveThread.php?" + lcQueryString + "threadID=" + pnItemID + "&newforum=" + lcNewForum;
			break ;	
		case 15: // edit a post
			lcURL = "lib/editPost.php?" + lcQueryString + "itemID=" + pnItemID ;
			break ;	
		case 16: // edit a forum - this is part of the admin area
			lcURL = "lib/admin_forumHandler.php?"+ lcQueryString + "itemID=" + pnItemID ;
			break ;	  
	 }
	document.getElementById(pcWriteToID).innerHTML='<div class="processingcentral"></div>' ;	
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ; 
	if(llStandard)
	{
		submitXMLHTTP(loXMLHTTP, pcWriteToID, "GET", lcURL, true, null) ;	
	}
	else
	{
		loXMLHTTP.onreadystatechange = function() 
		{ 
			// use of ghost function enables the system to execute each item in the correct order
			if (loXMLHTTP.readyState==4 || loXMLHTTP.readyState=="complete")
				{ 
					if (loXMLHTTP.status == 200)
					{
						document.location.href=loXMLHTTP.responseText ; 
						window.location.reload(true) ;
					}
			} 
		};
		loXMLHTTP.open("GET",lcURL,true) ;
		loXMLHTTP.send(null) ; 
	}
}	  

/*------------------------------------------------------------
	Interactive Features
------------------------------------------------------------*/
function changeVideo(pnVideoID, pcElementID)
{							   
	var loXMLHTTP, lcURL ;
	loXMLHTTP = GetXmlHttpObject() ;
	lcURL = "lib/videohandler.pnp?id=" + pnVideoID ;
	submitXMLHTTP(loXMLHTTP, pcElementID, "GET", lcURL, true, null) ;
}  

function loginHandler(pcEmail, pcPassword)
{																	 
	var loXMLHTTP, lcURL, lcEmail, lcPassword ;
	
	lcEmail		= document.getElementById(pcEmail).value ;
	lcPassword	= document.getElementById(pcPassword).value ;
		
	loXMLHTTP = GetXmlHttpObject() ;
	lcURL = "lib/loginhandler.php?id=" + lcEmail + "&description=" + lcPassword;
	submitXMLHTTP(loXMLHTTP, 'access', "GET", lcURL, true, null) ;
}

function logoutHandler()
{																	 
	var loXMLHTTP, lcURL ;
	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURL = "lib/logouthandler.php" ;
	submitXMLHTTP(loXMLHTTP, 'access', "GET", lcURL, true, null) ;	
	lcURL = window.location.href ;			 
	if(lcURL.indexOf("#")>=0)
	{
		lnPara1 = parseInt(lcURL.substring(lcURL.indexOf("=", 1)+1, lcURL.indexOf("|"))) ;
	}
	else
	{
		lnPara1 = 0 ;
	}
	if(lnPara1==24 || lnPara1==32) // refresh the page as we login out whilst on My Account
	{
		menuHandler(26,1,'mainContent');
	}
}		   

function reportAbuse(pnReviewType, pnReviewID, pcWriteTo)
{ 
	document.getElementById(pcWriteTo).innerHTML = '<div class="processingcentral"></div>' ;
	var loXMLHTTP, lcURL ;
	loXMLHTTP = GetXmlHttpObject() ;
	lcURL = "pagedefinition/reportabuse.php?reviewType=" + pnReviewType + "&item=" + pnReviewID;
	submitXMLHTTP(loXMLHTTP, pcWriteTo, "GET", lcURL, true, null) ;
}

function passwordReset(pnUser, pcTarget)
{
	document.getElementById(pcTarget).innerHTML = '<div class="processingcentral"></div>' ;
	var loXMLHTTP, lcURL ;
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcURL	= "lib/passwordreset.php?user=" + pnUser;
	  
	submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURL, true, null) ;
} 

function changeSubscriptionStauts(pnThreadID, pnUserID, pnIsSubscribed, pcTarget, pnOutputType)
{																				 
	document.getElementById(pcTarget).innerHTML = '<div class="processingcentral"></div>' ;
	var loXMLHTTP, lcURL ;										  
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcURL	= "lib/subscriptionhandler.php?thread=" + pnThreadID + "&user=" + pnUserID + "&currentstate=" + pnIsSubscribed + "&output=" + pnOutputType;
	  
	submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURL, true, null) ;	
}  

function cancelBooking(pnItemID, pnUserID, pnType, pcTarget)
{
	document.getElementById(pcTarget).innerHTML = '<div class="processingcentral"></div>' ;
	var loXMLHTTP, lcURL ;										  
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcURL	= "lib/cancelbooking.php?item=" + pnItemID + "&user=" + pnUserID + "&type=" + pnType ;
	  
	submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURL, true, null) ;	
} 

function churchDisplayHandler(pcTestValue, poObject)
{												   
	if(pcTestValue == "2") // display the other controls
	{							   
		setDisplayState(poObject, 'block') ;
	}
	else // hide the other controls
	{
		setDisplayState(poObject, 'none') ;
	}
}		   

function searchHandler(pcSearchFor)
{																		   
	document.getElementById('mainContent').innerHTML = '<div class="processingcentral"></div>' ;
	lcSearchTerm = document.getElementById(pcSearchFor).value ;
	var loXMLHTTP, lcURL ;										  
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcURL	= "lib/search.php?advanced=0&section=0&searchterm=" + lcSearchTerm ;
	  
	submitXMLHTTP(loXMLHTTP, 'mainContent', "GET", lcURL, true, null) ;		
}

function addDelegate(pnFood)
{  
	lnCount = parseInt(document.getElementById('txt_count').value) ;
	lnNewCount = lnCount + 1;
	lnNextCount = lnNewCount + 1 ;	
	lcTarget = "delegate_" + lnCount.toString() ;
	lcNewTarget = "delegate_" + lnNewCount.toString() ;
	lcNextTarget = "delegate_" + lnNextCount.toString() ;
	document.getElementById('txt_count').value = lnNewCount.toString() ; 
	lcNewestCBO = 'cbo_food' + lnCount ; 
	lcNewestTxt = 'txt_name' + lnCount ;   
	lnTabIndexCBO = 0 ; // default value for cases where no food
	if(document.getElementById(lcNewestCBO))
	{
		lnHighestTabIndex = document.getElementById(lcNewestCBO).tabIndex ;
		lnTabIndexCBO = lnHighestTabIndex + 2 ;
	}
	else
	{																	   
		lnHighestTabIndex = document.getElementById(lcNewestTxt).tabIndex ;
	}
	lnTabIndexTxt = lnHighestTabIndex + 1 ;
	

	document.getElementById(lcNewTarget).innerHTML='<div class="processingcentral"></div>' ;	

	var loXMLHTTP, lcURL ;										  
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcURL	= "pagedefinition/delegate.php?newtarget=" + lcNextTarget + "&food=" + pnFood + "&count=" + lnNewCount + "&ti1=" + lnTabIndexTxt + "&ti2=" + lnTabIndexCBO;
	  
	submitXMLHTTP(loXMLHTTP, lcNewTarget, "GET", lcURL, true, null) ;	
}

function removeDelegate()
{	   
	lnCount = parseInt(document.getElementById('txt_count').value) ;
	lnNewCount = lnCount - 1 ;					 
	document.getElementById('txt_count').value = lnNewCount.toString() ; 				  
	lcNewTarget = "delegate_" + lnCount.toString() ;
	document.getElementById(lcNewTarget).innerHTML = "" ;	
} 	

function addSpeaker()
{  
	lnCount = parseInt(document.getElementById('txt_count').value) ;
	lnNewCount = lnCount + 1;
	lnNextCount = lnNewCount + 1 ;	
	lcTarget = "speaker_" + lnCount.toString() ;
	lcNewTarget = "speaker_" + lnNewCount.toString() ;
	lcNextTarget = "speaker_" + lnNextCount.toString() ; 
	lcSpeakerExtra = "cnt_speakerExtra_" + lnNewCount.toString();
	document.getElementById('txt_count').value = lnNewCount.toString() ; 
	lcNewestCBO 	= 'cbo_speaker_' + lnCount ; 
	lcNewestTxt 	= 'txt_speakername_' + lnCount ;  
	lcNewestTxtArea	= 'txt_speaker_biog_' + lnCount ;
	lcNewestFile	= 'txt_imagefile_' + lnCount ;
	
	if(document.getElementById(lcNewestFile))
	{
		lnHighestTabIndex	= document.getElementById(lcNewestFile).tabIndex ;
		lnTabIndexFile		= lnHighestTabIndex + 4 ;
		lnTabIndexTxtArea	= lnHighestTabIndex + 4 ;
		lnTabIndexTxt		= lnHighestTabIndex + 4 ;
	}
	else
	{																	   
		lnHighestTabIndex = document.getElementById(lcNewestCBO).tabIndex ;
	}
	lnTabIndexCBO = lnHighestTabIndex + 1;
	document.getElementById(lcNewTarget).innerHTML='<div class="processingcentral"></div>' ;	

	var loXMLHTTP, lcURL ;										  
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcURL	= "pagedefinition/addspeaker.php?newtarget=" + lcNewTarget + "&nexttarget=" + lcNextTarget + "&extratarget=" + 
			"&count=" + lnNewCount + "&ti1=" + lnTabIndexCBO + "&ti2=" + lnTabIndexTxt +
			"&ti3=" + lnTabIndexTxtArea + "&ti4=" + lnTabIndexFile;;
	  
	submitXMLHTTP(loXMLHTTP, lcNewTarget, "GET", lcURL, true, null) ;	
}

function removeSpeaker()
{	   
	lnCount = parseInt(document.getElementById('txt_count').value) ;
	lnNewCount = lnCount - 1 ;					 
	document.getElementById('txt_count').value = lnNewCount.toString() ; 				  
	lcNewTarget = "speaker_" + lnCount.toString() ;
	document.getElementById(lcNewTarget).innerHTML = "" ;	
} 
	   
function insertAtCursor(myField, myValue) 
{
	
	var loTextArea = document.getElementById(myField) ;
	
	if (document.selection) 
	{
		//IE support
		myField.focus();
		sel		= document.selection.createRange(); 
		myValue	= myValue + sel.text;	
	}
	else if (loTextArea.selectionStart || loTextArea.selectionStart == '0') 	
	{																			
		//MOZILLA/NETSCAPE support 	
		var startPos		= loTextArea.selectionStart;
		var endPos			= loTextArea.selectionEnd;
		loTextArea.value	= loTextArea.value.substring(0, startPos) + myValue + loTextArea.value.substring(endPos, loTextArea.value.length);
	}
	else
	{
		loTextArea.value += loTextArea;
	}

}  

function wrapSelected(myField, openTag, closeTag)
{												
	var loTextArea = document.getElementById(myField) ;
	
	if (document.selection) 
	{
		//IE support
		myField.focus();
		sel		= document.selection.createRange(); 
		myValue	= myValue + sel.text;	
	}
	else if (loTextArea.selectionStart || loTextArea.selectionStart == '0') 	
	{																			
		//MOZILLA/NETSCAPE support 	
		var startPos		= loTextArea.selectionStart;
		var endPos			= loTextArea.selectionEnd;
		loTextArea.value	= loTextArea.value.substring(0, startPos) + openTag + loTextArea.value.substring((startPos), endPos) + closeTag + loTextArea.value.substring((endPos), loTextArea.value.length);
	}
	else
	{
		loTextArea.value += loTextArea;
	}

	
}

/*------------------------------------------------------------
	FORUM/REVIEW FUNCTIONS
------------------------------------------------------------*/	
function moderationControl(pnFunction, pnType, pnItemID, pcTarget)
{									   
	var loXMLHTTP, lcURL, llContinue ;
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	switch(pnFunction)
	{
		case 1: // delete post - this may delete a post or an entire thread - moderator only
			lcURL = "lib/deletePost.php?type=" + pnType + "&id=" + pnItemID ;	
			if(pnType==8)
			{
				llContinue = confirm("Delete entire thread?") ;
			}
			else
			{												   
				llContinue = confirm("Delete post?") ;
			}
			break ;
		case 2: // editPost - moderator only - even the first post in a thread
			lcURL = "pagedefinition/editPostForm.php?target=" + pcTarget + "&type=" + pnType + "&id=" + pnItemID ;		
			llContinue = true ;
			break ;
		case 3: // moveThread - may move an entire thread, but not an individual post - moderator only
			lcURL = lcURL = "pagedefinition/moveThreadForm.php?target=" + pcTarget + "&id=" + pnItemID ;	
			llContinue = true ;
			break ;
		case 4: // endAccount - nd a user account - set the isRegistered flag to 0 - moderator only
			lcURL = "lib/endAccount.php?type=" + pnType + "&id=" + pnItemID ; // php retreives the userID from the post/thread/review  
			llContinue = confirm("Cancel Member Account?") ;
			break ;			
	}	
	if(llContinue==true)
	{		   
		document.getElementById(pcTarget).innerHTML = '<div class="processingcentral"></div>' ;
		submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURL, true, null) ;	
	}
}

/*------------------------------------------------------------
	ADMIN AREA FUNCTIONS
------------------------------------------------------------*/
function adminHandler(pnMenuID, pcTarget)
{
	if(trim(pcTarget) == "")
	{
		pcTarget = "mainContent" ; // default value
	}
	
	// indicate processing
	document.getElementById(pcTarget).innerHTML='<div class="processingcentral"></div>' ;	
	
	// instantiate the XMLHTTP object
	loXMLHTTP = GetXmlHttpObject() ;
	
	lcBaseURL = "pagedefinition/" ;
	lcURLInfo = "pagedefinition/admin_info.php?id=" + pnMenuID ;
		
	// determine which URL we are loading to handle this menu selection
	switch(pnMenuID)
	{
		case 1: // News
			lcURL = lcBaseURL + "admin_news.php" ;
			break ;
		case 2: // Events
			lcURL = lcBaseURL + "admin_event.php" ;
			break ;
		case 3: // Training					 
			lcURL = lcBaseURL + "admin_training.php" ;		
			break ;
		case 4: // Forum					   
			lcURL = lcBaseURL + "admin_forum.php" ;		
			break ;  	
		case 5: // Downloads				
			lcURL = lcBaseURL + "admin_download.php" ;		
			break ;
		case 6: // Articles						
			lcURL = lcBaseURL + "admin_article.php" ;		
			break ;
		case 7: // Sermons									
			lcURL = lcBaseURL + "admin_sermon.php" ;		
			break ;
		case 8: // Links					  
			lcURL = lcBaseURL + "admin_link.php" ;		
			break ;
		case 9: // Reviews - events and training courses
			lcURL = lcBaseURL + "admin_eventreview.php" ;		
			break ;	 
		case 10: // administrative tasks
			lcURL =lcBaseURL + "admin_admin.php" ;
			break  ;  
	}
	submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURL, true, null) ;											 
}						   

function infoUpdater(pnMenuID)
{
	document.getElementById("information").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_info.php?id=" + pnMenuID ;
	submitXMLHTTP(loXMLHTTP, "information", "GET", lcURLInfo, true, null) ;	// update the information section
}

function editLink(pnLinkID)
{
	document.getElementById("linkcontrols").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_linkform.php?id=" + pnLinkID ;
	submitXMLHTTP(loXMLHTTP, "linkcontrols", "GET", lcURLInfo, true, null) ;	// update the information section	
}

function editForum(pnForumID)
{
	document.getElementById("forumcontrols").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_forumform.php?id=" + pnForumID ;
	submitXMLHTTP(loXMLHTTP, "forumcontrols", "GET", lcURLInfo, true, null) ;	// update the information section	
}

function getDownloadList(pnResourceType, pcTarget)
{
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_downloadList.php?typeID=" + pnResourceType ;
	submitXMLHTTP(loXMLHTTP, pcTarget, "GET", lcURLInfo, true, null) ;	// update the information section		
} 

function editDownload(pnTypeID, pnDownloadID)
{
	document.getElementById("downloadcontrols").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_downloadform.php?id=" + pnDownloadID + "&type=" + pnTypeID ;
	submitXMLHTTP(loXMLHTTP, "downloadcontrols", "GET", lcURLInfo, true, null) ;	// update the information section	
} 	

function editNews(pnNewsID)
{
	document.getElementById("newscontrols").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_newsform.php?id=" + pnNewsID ;
	submitXMLHTTP(loXMLHTTP, "newscontrols", "GET", lcURLInfo, true, null) ;	// update the information section	
} 	

function editEvent(pnEventID)
{
	document.getElementById("eventcontrols").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_eventform.php?id=" + pnEventID ;
	submitXMLHTTP(loXMLHTTP, "eventcontrols", "GET", lcURLInfo, true, null) ;	// update the information section	
} 
	   
function editEventReview(pnEventID)
{
	document.getElementById("eventreviewcontrols").innerHTML='<div class="processingcentral"></div>' ;	
	loXMLHTTP = GetXmlHttpObject() ;
	lcURLInfo = "pagedefinition/admin_eventreviewform.php?id=" + pnEventID ;
	submitXMLHTTP(loXMLHTTP, "eventreviewcontrols", "GET", lcURLInfo, true, null) ;	// update the information section	
} 

function conditionalDisplayHandler(pnItemValue, poObject)
{														
   if(pnItemValue == "1") // display the other controls
	{							   
		setDisplayState(poObject, 'block') ;
	}
	else // hide the other controls
	{
		setDisplayState(poObject, 'none') ;
	}
}

function conditionalDisplayHandlerCount(pnItemValue, poObject)
{														
	lnCount = parseInt(document.getElementById('txt_count').value) ;
	if(pnItemValue == "1") // display the other controls
	{							   
		setDisplayState(poObject, 'block') ;	 
		lnNewCount = lnCount + 1;
	}
	else // hide the other controls
	{
		setDisplayState(poObject, 'none') ;
		lnNewCount = lnCount - 1;
	}						
	
	lnNextCount = lnNewCount + 1 ;	
	lcTarget = "speaker_" + lnCount.toString() ;
	lcNewTarget = "speaker_" + lnNewCount.toString() ;
	lcNextTarget = "speaker_" + lnNextCount.toString() ;
	document.getElementById('txt_count').value = lnNewCount.toString() ; 
}

/*------------------------------------------------------------
	FILE FUNCTIONS
------------------------------------------------------------*/
function checkExt(pcSource, paValidTypes, pcButton, pcWriteTo)
{
	// pcSource - the ID for the file input control
	// paValidTypes - csv of the permitted file types
	// pcButton - the ID of the submit button - disabled if it is not valid or enabled if it is valid
	// pcWriteTo - the ID of the control to write any warning message to
	
	var lcFile			= document.getElementById(pcSource).value ;	
	var	lnFileLength	= parseInt(lcFile.length) - 3 ;
	var lcFileExt		= lcFile.substring(lnFileLength,lnFileLength + 3) ;
	var laAllowedTypes	= paValidTypes.split(',') ;
	var lnArrayLength	= laAllowedTypes.length // note JS arrays are 0 based  
	var llValidExt		= false ;
	
	for(i=0; i<=lnArrayLength-1; i = i+1)
	{
		if(lcFileExt.toLowerCase() == laAllowedTypes[i])
		{
			llValidExt = true ;
			break ; // we know it's valid so exit the loop						
		}
	}
																				
	if(llValidExt)
	{
		// ensure there are now warning
		document.getElementById(pcWriteTo).innerHTML = "" ;
		// ensure the button is available
		document.getElementById(pcButton).style.display="inline" ;	
	}			 
	else
	{									 
		// display warning
		document.getElementById(pcWriteTo).innerHTML = "<p class='warning'>only files of type " + paValidTypes + " are allowed</p>" ;
		// remove button
		document.getElementById(pcButton).style.display="none" ;
	}
}

function checkExt_multi(paToCheck, paBtnToChange, pcMainBtn, pcOutput, paAllowedTypes)
{
	// split the first two parameters into proper arrays
	var laToCheck		= paToCheck.split(',') ;
	var laBtnToChange	= paBtnToChange.split(',') ;
	var laAllowedType	= paAllowedTypes.split(',') ;	 
	var llValidExt		= false ;  
	var lnErrorCount	= 0 ;
	var lcSource ;
	var lcFile ;
	var	lnFileLength ;
	var lcFileExt ;
	
	for(j=0; j<=laToCheck.length-1; j = j+1)
	{
		lcSource			= laToCheck[j] ;
		lcFile				= document.getElementById(lcSource).value ;	
		lnFileLength		= parseInt(lcFile.length) - 3 ;
		lcFileExt			= lcFile.substring(lnFileLength,lnFileLength + 3) ;
		
		if(lnFileLength > 0) // only check if there is file selected, it is possible to laod an item without pictures
		{
			for(i=0; i<=laAllowedType.length-1; i = i+1)
			{
				if(lcFileExt.toLowerCase() == laAllowedType[i])
				{
					llValidExt = true ;
					break ; // we know it's valid so exit the loop						
				}
			}	
			
			if(llValidExt == false)
			{
				lnErrorCount	= lnErrorCount + 1 ;
				lcBtnToChange	= laBtnToChange[j] ; // note array order is important
				document.getElementById(lcBtnToChange).style.display = "none" ;
			}														   
			else
			{
				lcBtnToChange	= laBtnToChange[j] ; // note array order is important
				document.getElementById(lcBtnToChange).style.display = "inline" ;
			}
			llValidExt = false ; // reset to default
		}
	}
	
	if(lnErrorCount == 0)
	{
		document.getElementById(pcMainBtn).style.display	= "inline" ;	
		document.getElementById(pcOutput).innerHTML			= "" ;
	}
	else
	{														
		document.getElementById(pcMainBtn).style.display	= "none" ;
		document.getElementById(pcOutput).innerHTML			= "<p class='warning'>only files of type " + paAllowedTypes + " are allowed</p>" ;
	}
}			   

function startUpload(pcTarget)
{
	document.getElementById(pcTarget).innerHTML = '<div class="processingcentral"></div>' 	;
	return true;
}  

function stopUpload(pcTarget, pcMessage)
{				
	document.getElementById(pcTarget).innerHTML = pcMessage 	;
	return true;
}
