//  *************************************************************************
// Common Javascript Routines Module
// Author : R Tostevin, 2005
//  *************************************************************************

//Initialise Window Variable so it is never undefined
// Note : popupWin=Main Popup window; popupWin2=Alternate to use
var popupWin = null;
var popupWin2 = null;

function openMediaFilePopUp(URL){	
	var winprops ="width=600,height=500,left=140,top=140,scrollbars=yes,resizable=yes";

	if (popupWin2 && !popupWin2.closed) {
		//Pop Up Already exists
		popupWin.focus();
	} else {
		//Pop Up does not yet exist so create & navigate
		popupWin= window.open("","",winprops);		
	}
	
	
	//popupWin.document.write("<div align=/"center/"><strong>Media is Loading Please wait ....</strong></div>");
	popupWin.document.write("<br><br><br><br><br><br><br><br><strong>Media is Loading Please wait ....</strong>");
	//popupWin.document.write("Media is Loading Please wait ....");
	
	navigatePopUp(URL,"",winprops,true);
}

function navigatePopUp(URL,AlternateName,WindowProperties,UseAltPopUp){
	//Displays & Navigates a Pop-Up to the passed URL
	// Note : Caption & Properties are optional
	
	var WinName;
	var WinProps;
	
	//Determine if some An Alternate Name has been passed
	WinName="";
	if (AlternateName != undefined) {
		WinName=AlternateName;
	}
	
	//Determine if some Windows Properties Text has been passed
	if (WindowProperties != undefined && WindowProperties != '') {
		WinProps=WindowProperties;
	} else {
		//Default Size & Position
		WinProps="width=600,height=500,left=140,top=140,scrollbars=yes,resizable=yes";
	}
	
	
	//Determine if the an alternate Window to current shold be used 
	if (UseAltPopUp==undefined || UseAltPopUp =='' ){
		UseAltPopUp=false;
	}
	
	
	//Show Pop-Up if req'd & Navigate to the URL 
	if (UseAltPopUp==true){
		if (popupWin && !popupWin.closed) {
			//Pop Up Already exists
			popupWin.focus();
			popupWin.location=URL;
		} else {
			//Pop Up does not yet exist so create & navigate		
			popupWin= window.open(URL,WinName,WinProps);		
		}
	
	} else {
		if (popupWin2 && !popupWin2.closed) {
			//Pop Up Already exists
			popupWin2.focus();
			popupWin2.location=URL;
		} else {
			//Pop Up does not yet exist so create & navigate
			popupWin2= window.open(URL,WinName,WinProps);		
		}
	}
}

function navigateBibleRef(ref){
	//Displays & Navigates a Pop-Up to a Bible Reference 
	/// Reference is in format :
	//		http://www.biblegateway.com/cgi-bin/bible?language=english&passage=Hebrews+12%3A1-3&version=NIV
	
	var url;
	var book;
	var chapter;
	var verses;
	var posn1;
	var posn2;
	var posn3;
	var basicURL = "http://www.biblegateway.com/cgi-bin/bible?language=english&passage=";
	var version = "&version=NIV";
	var WinProps="width=750,height=500,left=140,top=140,scrollbars=yes,resizable=yes";
	
	//Locate any Book Chapter
	posn1=ref.indexOf(" ");
	if (posn1>0){
		//Chapter has been supplied 
	
		//Locate any Verses		
		posn2=ref.indexOf(":");
		if (posn2>0) {
			//Verses have been supplied		
			verses=ref.substring(posn2+1,ref.length);
			}
		else{
			//No Verses have been supplied 
			verses="";
			
			//Set Chapter posn to string end
			posn2=ref.length;
		}
	
		//Set Book
		book=ref.substring(0,posn1);
		
		//Set Chapter
		chapter=ref.substring(posn1+1,posn2)	
		}
	else{
		// No Chapter so use ref as passed
		book=ref;
		chapter="";
		verses="";
	}
	
	//Form the URL
	if (chapter!=""){
		url=basicURL+book + "+" + chapter;
		if (verses!=""){
			url=url + "%3A" + verses
			}
		}
	else {
		url=basicURL+book;
	}
	url=url+version;
	
	
	//Show Pop-Up if req'd & Navigate to the URL 
	//if (popupWin && !popupWin.closed) {
	//	//Pop Up Already exists
	//	popupWin.focus()
	//	popupWin.navigate(url);
	//	}
	//else {
	//	//Pop Up does not yet exist so create & navigate
	//	popupWin= window.open(url, 'BibleReference', 'width=600,height=500,left=140,top=140,scrollbars=yes,resizable=yes')		
	//}
	navigatePopUp(url,'BibleReference',WinProps)
}

function navigateToURL(URL){
	window.location.href= URL;
}

function navigateiFrameToURL(iFrameName,URL){
	var element;
	
	element=document.getElementById(iFrameName); 	
	element.src=URL;
}

function toggleelementvisibility(elementname) { 
	// ** Function to Toggle teh Visible State of an element in the Document ***
	var element = document.getElementById(elementname); 
	var currentstate = element.style.visibility
	var show = (currentstate =="hidden"); 
	
	if (show) { 
		element.style.visibility = "visible";	
	} 
	else { 	
		element.style.visibility = "hidden"; 	
	}
} 

function showelement(elementname,visible) { 
	// ** Function to Show/Hide an element in the Document ***
	var element = document.getElementById(elementname); 
	var show = (visible ==true); 
	
	if (show) { 
		element.style.visibility = "visible";	
	} 
	else { 	
		element.style.visibility = "hidden"; 	
	}
} 

function CycleToElement(elementrootname,fromelementno,modulo,forwards) {
	// ** Function to Cycle in the direction indicated from one element to another using the passed modulo ***
	// ** returns the next element no in the cycle 	       												   ***
	var counter;
	var element;
	
	
	counter=fromelementno;
	
	
	do {
		
		switch (counter) {   
			case modulo :              
				if (forwards==true){
					counter=1;
				}
				else {
					counter -=1
				}
				break;    
			case 1 :
				if (forwards==false){
					counter=modulo;
				}
				else {
					counter +=1
				}
				break;
			default :
				if (forwards==true){
					counter +=1;
				}
				else {
					counter -=1
				}
				break;
		};
		
		element = document.getElementById(elementrootname+counter);
		
	} 
	while (element == undefined );
	
	return counter;

}

function setCookie(name, value, expire) {   
	// *** Function to set cookie values. Expiration date is optional ***
	var cookiestring;
	//document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
	
	cookiestring = name + "=" + escape(value);
	
	if (expire == null) {
	}
	else {
		cookiestring += "; expires=" + expire.toGMTString()
	}
	document.cookie = cookiestring
}


function getCookie(Name) {   
	// *** Function to retireve a cookie value ***
	var searchvalue = Name + "="   
	var end;
	var result;
	
	result="";
	
	// Check if there are any cookies
	if (document.cookie.length > 0) { 
		// Search for value      
		offset = document.cookie.indexOf(searchvalue);       
		if (offset != -1) {          
			offset += searchvalue.length;          
			// set index of beginning of value         
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value         
			if (end == -1){             
				end = document.cookie.length
			}
			//return unescape(document.cookie.substring(offset, end));
			result=unescape(document.cookie.substring(offset, end));   			    
		}
		
	//else 
	//	return "";
	}
	
	return result;
}

function setCellText(cellid,text){
	// *** Function to Set the Text for the Passed Cell id & Text ***
	var element1; 
	
	element1 = document.getElementById(cellid);
	element1.innerHTML = text;
}

function setPointer() {
   if (document.all)
       for (var i=0;i < document.all.length; i++)
            document.all(i).style.cursor = 'wait';
}

function resetPointer() {
   if (document.all)
       for (var i=0;i < document.all.length; i++)
            document.all(i).style.cursor = 'default';
}

function findElementPosX(obj){
	// Function to find the x position of an elemnt on a page 
	var curleft = 0;
	
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
				 curleft += obj.x;
	}
	return curleft;
}

function findElementPosY(obj){
	// Function to find the y position of an elemnt on a page 
	var curtop = 0;
	
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
} 

function setImageSource(imageName,imageURL){
	
	var img=document.getElementById(imageName);

	img.src = imageURL;
}