
function autoSaveEntry(){
    //LoadAnim3();
    var journaltitle = document.getElementById('ctl00_ContentPlaceHolder1_JTitle').value;
    var journaltext = document.getElementById('ctl00_ContentPlaceHolder1_JEntryTxt').value;
    var acctid = document.getElementById('ctl00_ContentPlaceHolder1_acctuid').value;
    var journalpicture = document.getElementById('ctl00_ContentPlaceHolder1_JPIct').attributes['src'].value;
    var jnpictdesc = document.getElementById('ctl00_ContentPlaceHolder1_PictureDescription').value;
    var journaldate = document.getElementById('ctl00_ContentPlaceHolder1_SelectDate').value;
    
    if(journaltext != "No journal entry exists" && journaltext != "" && journaltitle != ""){
        PageMethods.SaveJournalEntry(journaldate, journaltitle, journaltext, journalpicture, jnpictdesc, acctid, PM4Succeded, PM4Failed);
    }
    
    return true;
}

function LoadAnim3(){
    resetimgsrc = document.getElementById('journalsave').attributes['src'].value;
    document.getElementById('journalsave').attributes['src'].value = "http://www.readthescriptures.com/images/loaders/timer.gif";
    postBackHiddenField('ctl00_ContentPlaceHolder1_HiddenFieldPostBack');
}

function PM4Succeded(result) {
    document.getElementById('journalsave').attributes['src'].value = resetimgsrc;
    postBackHiddenField('ctl00_ContentPlaceHolder1_HiddenFieldPostBack');
}

function PM4Failed(result) {
    document.getElementById('journalsave').attributes['src'].value=resetimgsrc;
    postBackHiddenField('ctl00_ContentPlaceHolder1_HiddenFieldPostBack');
}

var resetimg = "";
var resetimgsrc = "";
var hselectelement = "";
var highlightcolor = "Red";

//This function is used to call the partial page render on an Update panel
function postBackHiddenField(hiddenFieldID) {
    var hiddenField = $get(hiddenFieldID);
    if (hiddenField) {
        hiddenField.value = (new Date()).getTime();
        loaderenabled=false;
        __doPostBack(hiddenFieldID,'');
        loaderenabled=true;
    }
}

function DeleteVerseHighlights(e,f){
    resetimg = f;
    HighlightSelectionClear();
    var accntid = document.getElementById('ctl00_ContentPlaceHolder1_AcctID');
    LoadingAnim2(f);
    PageMethods.highlightRemove(e, accntid.value, PM2Succeded, PM2Failed);
}

function PM2Succeded(result) {
    document.getElementById(resetimg).attributes['src'].value=resetimgsrc;
    postBackHiddenField('ctl00_ContentPlaceHolder1_HiddenFieldPostBack');
}

function PM2Failed(result) {
    alert(result);
    document.getElementById(resetimg).attributes['src'].value=resetimgsrc;
}

/*<><><><><><><><><><><><><><><> Start of Highlighting operations <><><><><><><><><><><><><>*/

document.onmouseup = getActiveText;
if (!document.all) document.captureEvents(Event.MOUSEUP);

var highlighttext = "";

function getActiveText(e) { 
    try{
        if(document.selection){
            highlighttext = document.selection.createRange().text;
            return true;
        }else if (window.getSelection){
            highlighttext = window.getSelection();
            return true;
        }else{
            return false;
        }
    }catch(err){}
}

function setselectedelem(e,f){
   resetimg = f;
   hselectelement = e;
   var accntid = $get('ctl00_ContentPlaceHolder1_AcctID');
   var htxt = highlighttext.toString();
   var elem = $get("span_"+e);
   if(highlighttext != "" && hselectelement != "" && accntid.value != ""){
    LoadingAnim2(f);
    if (window.getSelection) {
       if(elem.textContent.search(highlighttext) > -1){
            PageMethods.AddVerseHighlights(hselectelement, htxt, highlightcolor, accntid.value, PM1Succeded, PM1Failed);
       }else{
         alert("Please select text within a single verse.");
       }
    }else if (document.selection) {
       if(elem.innerText.search(highlighttext) > -1){
            PageMethods.AddVerseHighlights(hselectelement, htxt, highlightcolor, accntid.value, PM1Succeded, PM1Failed);
       }else{
         alert("Please select text within a single verse.");
       }
   }
  }
}

function HighlightSelectionClear() { 
    if (document.selection) 
        document.selection.empty(); 
    else if (window.getSelection) 
        window.getSelection().removeAllRanges(); 
} 

function PM1Succeded(result) {
    document.getElementById(resetimg).attributes['src'].value=resetimgsrc;
    postBackHiddenField('ctl00_ContentPlaceHolder1_HiddenFieldPostBack');
    HighlightSelectionClear();
}

function PM1Failed(result) {
    document.getElementById(resetimg).attributes['src'].value=resetimgsrc;
    HighlightSelectionClear();
}

// Apply formatting to the text selection
function formatselectedText() {
    var sText
    if (document.selection) 
        sText = document.selection.createRange(); 
    else if (window.getSelection)
        sText = window.getSelection(); 
  
    if (sText.text != ""){
      sText.execCommand("BGColor",false,highlightcolor);
    }
    HighlightSelectionClear();
}

function LoadingAnim2(areaid){
    resetimgsrc = document.getElementById(areaid).attributes['src'].value;
    document.getElementById(areaid).attributes['src'].value="http://www.readthescriptures.com/images/loaders/ajax-loader.gif";
}

var tooglesize = true;

function SRTSC(){
    try{
        var tlink = document.getElementById('inlink');
        if(tooglesize){
            document.getElementById('ctl00_ContentPlaceHolder1_scripturearea').className="scriptureareaold";
            tlink.innerText="Decrease Font";
            tooglesize = false;
        }else{
            document.getElementById('ctl00_ContentPlaceHolder1_scripturearea').className="scripturearea";
            tlink.innerText="Increase Font";
            tooglesize = true;  
        }
        PageMethods.SetProfileVal('Screen', tooglesize?"1":"2", PM3Succeded, PM3Failed);
    }catch(err){}
}

function PM3Succeded(result) {

}

function PM3Failed(result) {

}

function SaveVerseNotes(){
    getElementbyType();
    if(txtcollect.length > 0)
        XMLVerseNotes();
}

function XMLVerseNotes(){
    var accntid = document.getElementById('ctl00_ContentPlaceHolder1_AcctID');
    var increm=0;
    var increm2=0;
	while (txtcollect[increm]){
		PageMethods.SaveReadingNotes(txtcollect[increm].id, txtcollect[increm].attributes['sid'].value, txtcollect[increm].value, accntid.value, PageMethodSucceded, PageMethodFailed);
		increm++;
	}
}

function getElementbyType(){
	txtcollect = new Array();
	var tinc=0;
	var txttags=document.getElementsByTagName('textarea');
	
	for (i=0; i<txttags.length; i++){
	    var noteimgdis = document.getElementById(txttags[i].id.replace("note","img"));
		if (txttags[i].value != ""){
			txtcollect[tinc++]=txttags[i];
			noteimgdis.attributes['src'].value='http://www.readthescriptures.com/images/note_2.jpg';
		}else{
		    try{
		        noteimgdis.attributes['src'].value='http://www.readthescriptures.com/images/note_1.jpg';
		    }catch(err){}
		}
	}
}

function PageMethodSucceded(result) {
    //Scroll top into view
    //window.scrollBy(0,nPos);
}

function PageMethodFailed(result) {
    alert(result);
    document.getElementById("ctl00_ContentPlaceHolder1_ModalPopupExtender1").hide();
}

// -----------------------------------------------------------------------------

function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	try{
	    var fieldWidth =  parseInt(field.offsetWidth);
	    var charcnt = field.value.length;        

	    // trim the extra text
	    if (charcnt > maxlimit) { 
		    field.value = field.value.substring(0, maxlimit);
	    }else { 
		    // progress bar percentage
		    var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
		    document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
		    document.getElementById(counter).innerHTML=percentage+"%"
		    // color correction on style from CCFFF -> CC0000
		    setcolor(document.getElementById(counter),percentage,"background-color");
	    }
	}catch(error){}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}


// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// the version of javascript supported
var jsVersion = 1.0;
// -----------------------------------------------------------------------------

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isFFox = (navigator.userAgent.indexOf("Firefox")!=-1) ? true : false;

jsVersion = 1.1;
// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 

// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
 	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
   	// loop backwards through the versions until we find the newest version	
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) { 
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString .split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);
        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );	
			}
		}
	}	
	return (reqVer ? false : 0.0);
}

function CreateControl(DivID, MOVIE, WIDTH, HEIGHT, BGCOLOR ){
  var d = document.getElementById(DivID);
  d.innerHTML = 
    '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" + width=' + WIDTH + ' height=' + HEIGHT + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
    + '<param name="movie" value="' + MOVIE + '" />'
    + '<param name="quality" value="high" />'
    + '<param name="bgcolor" value=' + BGCOLOR + ' />'
    + '<embed src="' + MOVIE + '" quality="high" bgcolor=' + BGCOLOR 
    + ' width=' + WIDTH + ' height=' + HEIGHT + ' align="middle"'
    + ' play="true"'
    + ' loop="false"'
    + ' quality="high"'
    + ' allowScriptAccess="sameDomain"'
    + ' type="application/x-shockwave-flash"'
    + ' pluginspage="http://www.macromedia.com/go/getflashplayer" >'
    + '<\/embed>'
    + '<\/object>';
}

function CreatePControl(DivID, MOVIE, WIDTH, HEIGHT, BGCOLOR, PARAMVAL){
  var d = document.getElementById(DivID);
  d.innerHTML = 
    '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" + width=' + WIDTH + ' height=' + HEIGHT + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
    + '<param name="movie" value=' + MOVIE + ' />'
    + '<param name="quality" value="high" />'
    + '<param name="bgcolor" value=' + BGCOLOR + ' />'
    + '<param name="FlashVars" value="imgcnt=' + PARAMVAL + '" />'
    + '<embed src=' + MOVIE + ' quality="high" bgcolor=' + BGCOLOR 
    + ' width=' + WIDTH + ' height=' + HEIGHT + ' align="middle"'
    + ' flashvars="imgcnt=' + PARAMVAL + '"'
    + ' play="true"'
    + ' loop="false"'
    + ' quality="high"'
    + ' allowScriptAccess="sameDomain"'
    + ' type="application/x-shockwave-flash"'
    + ' pluginspage="http://www.macromedia.com/go/getflashplayer" >'
    + '<\/embed>'
    + '<\/object>';
}

var enablepersist="off"; //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="yes"; //Collapse previously open content when opening present? (yes/no)

function getElementbyClass(classname){
	ccollect=new Array();
	var inc=0;
	var alltags=document.all? document.all : document.getElementsByTagName("*");
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==classname){
			ccollect[inc++]=alltags[i];
		}
	}
}

function contractcontent(omit){
	var inc=0;
	while (ccollect[inc]){
		if (ccollect[inc].id!=omit){
			ccollect[inc].style.display="none";
		}
		inc++;
	}
}

function expandcontent(cid){

	if (typeof ccollect!="undefined"){
		if (collapseprevious=="yes"){
			contractcontent(cid);
		}
		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none";
		var clickelem = document.getElementById(cid);
	}
}

function revivecontent(){
    contractcontent("omitnothing")
    selectedItem=getselectedItem()
    selectedComponents=selectedItem.split("|")
    for (i=0; i<selectedComponents.length-1; i++)
        document.getElementById(selectedComponents[i]).style.display="block"
}

function getselectedItem(){
	if (get_cookie(window.location.pathname) != ""){
		selectedItem=get_cookie(window.location.pathname);
		return selectedItem;
	}
	else{
		return "";
	}
}

function get_cookie(Name) { 
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) { 
            offset += search.length
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
                returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

function saveswitchstate(){
	var inc=0, selectedItem="";
	while (ccollect[inc]){
		if (ccollect[inc].style.display=="block"){
			selectedItem+=ccollect[inc].id+"|";
		}
		inc++;
	}
	document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
	uniqueidn=window.location.pathname + "firsttimeload";
	getElementbyClass("switchcontent");
	
	if (enablepersist=="on" && typeof ccollect!="undefined"){
		document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"; 
		firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0; //check if this is 1st page load
		if (!firsttimeload){
			revivecontent();
		}
	}
}

if (window.addEventListener)
	window.addEventListener("load", do_onload, false);
else if (window.attachEvent)
	window.attachEvent("onload", do_onload);
else if (document.getElementById)
	window.onload=do_onload;

if (enablepersist=="on" && document.getElementById){
	window.onunload=saveswitchstate;
}

function changeImages() {
    if (document.images && !selectionmade) {
	    for (var i=0; i<changeImages.arguments.length; i+=2) {
		    document.getElementById(changeImages.arguments[i]).src = changeImages.arguments[i+1];
	    }
    }
}

function changeClass(elem){
    getElementbyClass("picborderselect");
    var inc=0;
	while (ccollect[inc]){
	    ccollect[inc].className="picborder";
		inc++;
	}
	document.getElementById(elem).className = "picborderselect";
}