// JavaScript Document
function showHelp()
{
	document.getElementById("help").style.display = "Block";	
}

function hideHelp()
{
	document.getElementById("help").style.display = "none";	
}


function printtext(toPrintBlock){

	//Prints text versio of song to new window
	clearselectedsongs();
	popup = window.open('','popup','toolbar=no,menubar=no,scrollbars=yes,width=700');
	popup.document.open();
	popup.document.write("<html><head></head><body onload='print()'>");
	popup.document.write(toPrintBlock);
	popup.document.write("</body></html>");
	popup.document.close();
}

function SongText2(){
	var songcontents;
	popup = window.open('','popup','toolbar=no,menubar=no,scrollbars=yes,width=700');
	popup.document.open();
	popup.document.write("<html><head>");
	popup.document.write("</head><body onload='print()'>");
	songcontents = "<pre id='songBody'>" + document.getElementById("songBody").innerHTML + "</pre>";
	popup.document.write(songcontents);
	popup.document.write("</body></html>");
	popup.document.close();
	
}

function SongText(songID){						
	var key = getDDBoxValue(document.form.song_key);
	var winLocation = "/scripts/printsong.php?id="+songID+"&key="+key;
	popup = window.open(winLocation,'popup','toolbar=no,menubar=no,scrollbars=yes,width=700');
	popup.focus();
}

var selData = new Array();	
function addToPrint(id)
{
	
	modifySelection(id);
	
	if(!document.getElementById('cb_'+id).checked)
	{				
		if(inArray(selData,id) >= 0)
		{
			var index = inArray(selData,id);			
			selData.splice(index,1);
		}		
	}
	else
	{
		if( selData.length > 0)
		{ 
			if(inArray(selData,id) < 0)
			{
				var index = selData.length+1;
				selData.splice(index,1,id);
			}		
		}
		else
		{
			var index = selData.length+1;
			selData.splice(index,1,id);
		}
	}
	var selToPrint = '';
	value = selData.toString();
	Set_Cookie(value);
	
	for(var x=0; x<selData.length; x++)
	{								
		var objId = selData[x];																	
		selToPrint += document.getElementById('printblock_'+objId).innerHTML;						
	}
	document.getElementById('to_print').innerHTML = selToPrint;					
}



function addToDelete(id)
{
	if(!document.getElementById('cb_'+id).checked)
	{				
		if(inArray(selData,id) >= 0)
		{
			var index = inArray(selData,id);			
			selData.splice(index,1);
		}		
	}
	else
	{
		if( selData.length > 0)
		{ 
			if(inArray(selData,id) < 0)
			{
				var index = selData.length+1;
				selData.splice(index,1,id);
			}		
		}else{
			var index = selData.length+1;
			selData.splice(index,1,id);
		}
	}
	var selToPrint = '';
	//alert(selData.toString());
	value = selData.toString();
	Set_Cookie(value);	
}

function inArray(arr,str)
{	
	var found = -1;
	for(var i=0;i<=arr.length;i++)
	{		
		if( arr[i]==str )
		{		
			found = i;
			break;
		}		
	}
	return found;
}

function popup_player(id)
{
	window.open('player.php?song_id='+id,'player','width=330,height=350,top=200,left=300,resizable=no,scrollbars=yes,status=no;directories=no;menubar=no;');
}

function Set_Cookie(value)
{
	var today = new Date(); 
	var the_date = new Date("December 31, 2020"); 
	var the_cookie_date = the_date.toGMTString();  
	var the_cookie = "users_id="+value; 
	var the_cookie = the_cookie + ";expires=" + the_cookie_date;
	document.cookie = the_cookie
} 

function Get_Cookie(name)
{
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length)))
   		return null;
	if(start == -1)
		return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1)
		end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}

//===== ADDING REMOVING ITEMS FROM DD LIST =====

function clearList(list)
{
//Clears dd box for ddProvState
    var i = 0;
    var o = list.options;

    for (i = o.length; i >= 0; --i)
		o[i] = null;
    list.disabled = true;
    }


function addElement(list, text_in, value_in){
//Updates dd box for ddProvState

    var o = list.options;
    var nIdx;
	if (o.length < 0)// IE for Mac 4.5 sets length to -1 if list is empty
		nIdx = 0;
	else
		nIdx = o.length;

	o[nIdx] = new Option(text_in, value_in);
	list.disabled = false;
}

//end -----

function setSkeyDD(getText, getValues){
//Populates setkeyDD 
	var ddList = document.searchForm.skeydd;
	
	clearList(ddList)
	
	var arText = getText;
	var arValues = getValues;
	
	try{
		for (var i = 0; i < arText.length; i++){

			addElement(ddList, arText[i], arValues[i])
		}	// end for
	} catch (e) {
		
	}	// end try
}




//===== DD BOXES =====
function setDDBox(getFldName, getFldValue){
//Sets Combo box value to value	
	var fldName = eval(getFldName);
	var fldValue = getFldValue;
   	var i="";  
   	for (var i = 0; i < fldName.length; i++) {
         if (fldName.options[i].value == fldValue) {
			fldName.options[i].selected=true;     	   
		 }
   	}
}




function getDDBoxValue(fldName){
	
	//Gets Displayed value from DD
	var Indx = fldName.selectedIndex;
	return fldName.options[Indx].value;
}
//--- END DD BOXES ---


//===== RADIO =====
function setRadio(getFldName, getFldValue){
	var fldName = eval(getFldName);
	var fldValue = getFldValue;
	var i="";
	
	for (var i = 0; i < fldName.length; i++) {
		if (fldName[i].value == fldValue){
			fldName[i].checked = true;
		}
	}
}

function clearRadio(getFldName){
	var fldName = eval(getFldName);
	var i="";
	
	for (var i = 0; i < fldName.length; i++) {
		fldName[i].checked = false;
	}
}


function getRadio(getFldName){
	var fldName = eval(getFldName);
	var fldValue = null;
	
	for (var i = 0; i < fldName.length; i++) {
		if (fldName[i].checked == true){
			fldValue = fldName[i].value;
		}
	}
	return fldValue;
}

//--- END DD BOXES ---



function ShowHideUpload(getShow, getHide){
//Shows one element, hides another.  Used in update_songs.html	
	document.getElementById(getShow).style.display = 'block';
	document.getElementById(getHide).style.display = 'none';
}

//===== PRINTING PDF'S =====
function showPDF(){
	//Posts myForm to pop-up window for PDF
	//Used in songs and favorites
	popup = window.open('', 'popup', 'toolbar=yes,menubar=yes,scrollbars=yes,width=800');
	document.myForm.target = 'popup';
	document.myForm.submit();
	
}	
/*function showPDF1(){
//Posts myForm to pop-up window for PDF
//Used in songs and favorites
	
	popup = window.open('','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=700');
	//document.getElementById('task').value='createpdf1'
	document.myForm.target = 'popup'
	alert ("in pdf 1 "+document.getElementById('task').value)
	document.myForm.submit()

}	*/

function PDF_EventView(previewType, eventid){
	var actsn = Math.floor(Math.random()*111111111111);
	if(previewType == 1){
		popup = window.open('pdf/download.php?eventid='+eventid+'&actsn='+actsn+'&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 2){
		popup = window.open('pdf/download.php?eventid='+eventid+'&actsn='+actsn+'&action=fullpagechart&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 3){
		popup = window.open('pdf/download.php?eventid='+eventid+'&actsn='+actsn+'&action=lyricschart&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}	
	document.myForm.target = 'popup'
}

function PDF_PlaylistView(previewType, plid){
	var actsn = Math.floor(Math.random()*111111111111);
	if(previewType == 1){
		popup = window.open('pdf/download_pl.php?plid='+plid+'&actsn='+actsn+'&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 2){
		popup = window.open('pdf/download_pl.php?plid='+plid+'&actsn='+actsn+'&action=fullpagechart&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 3){
		popup = window.open('pdf/download_pl.php?plid='+plid+'&actsn='+actsn+'&action=lyricschart&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}
	document.myForm.target = 'popup'
}

function PDF_ListEvents1(){
	//Print list of songs with chords
	var frmObj = document.getElementById('eventlist');
	var isChk = false;
	for(var i = 0; i < frmObj.elements.length; i++){
		if(frmObj.elements[i].name == 'event_id[]' && frmObj.elements[i].checked == true){
			isChk = true;
		}
	}

	if(isChk == true){
		showEventPDF();
	} else {
		alert('Please select at least one event!!');
		return ;
	}
}

//===== PRINTING PDF'S =====
function showEventPDF(){
	//Posts myForm to pop-up window for PDF
	//Used in songs and favorites
	popup = window.open('', 'popup1', 'toolbar=yes,menubar=yes,scrollbars=yes,width=800,resizable=yes');
	var frmObj = document.getElementById('eventlist');
	frmObj.target = 'popup1';
	frmObj.action = 'pdf/download_events.php';
	frmObj.submit();
}	


function PDF_SongbookView(previewType, sbid){
	var actsn = Math.floor(Math.random()*111111111111);
	if(previewType == 1){
		popup = window.open('pdf/download_sb.php?sbid='+sbid+'&actsn='+actsn+'&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 2){
		popup = window.open('pdf/download_sb.php?sbid='+sbid+'&actsn='+actsn+'&action=fullpagechart&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 3){
		popup = window.open('pdf/download_sb.php?sbid='+sbid+'&actsn='+actsn+'&action=lyricschart&preview=true','popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}
	document.form.target = 'popup'
}

function PDF_ListSong(getShowChords){
	//Print list of songs with chords
	//Show Chords is either 'true' or 'false'
	var currTask = document.getElementById('currentTask');

	if(currTask == null){
		clearselectedsongs();
		if(selData.length < 1){
			alert("Select the songs you want to print.")
			return
		} 
	} else if(currTask.value != 'viewSelected'){
		clearselectedsongs();
		if(selData.length < 1){
			alert("Select the songs you want to print.")
			return
		} 
	}
	document.getElementById('task').value = 'createpdf';
	document.getElementById('showchords').value = getShowChords;
	showPDF();
}

function PDF_ListSong1(getShowChords)
{
	//Print list of songs with chords
	//Show Chords is either 'true' or 'false'
	var currTask = document.getElementById('currentTask');
	if(currTask == null){
		clearselectedsongs();
		if(selData.length < 1){
			alert("Select the songs you want to print.")
			return
		} 
	} else if(currTask.value != 'viewSelected'){
		clearselectedsongs();
		if(selData.length < 1){
			alert("Select the songs you want to print.")
			return
		} 
	}
	document.getElementById('task').value='createpdf1';
	document.getElementById('showchords').value=getShowChords;
	//alert("Page is under construction.")
	showPDF();
}



function PDF_SingleSong(getShowChords){
	//Print list of songs with chords
	var key = getDDBoxValue(document.form.song_key)

	//Show Chords is either 'true' or 'false'
	document.getElementById('task').value='createpdf';
	document.getElementById('showchords').value=getShowChords;
	document.getElementById('tranposekey').value=key;
	showPDF();

}

/* ----------------------------------------------------
START: THIS FUNCTION ADD FOR OPEN SONG_VIEW PDF PREVIEW
---------------------------------------------------- */
function NewPDF_SingleSong(getShowChords, id){

	//Print list of songs with chords
	var key = getDDBoxValue(document.form.song_key)

	//Show Chords is either 'true' or 'false'
	document.getElementById('page').value='song_view';
	document.getElementById('task').value='createpdf';
	document.getElementById('id').value = id;
	document.getElementById('showchords').value=getShowChords;
	document.getElementById('tranposekey').value=key;
	//URL = 'mypage.php?page=song_view'
	if(getShowChords == 'true'){
		popup1 = window.open('', 'popup1', 'toolbar=yes,menubar=yes,scrollbars=yes,width=800');
		document.myForm.target = 'popup1';
	} else {
/*		alert(document.myForm.action);
		alert(document.myForm.page.value);
		alert(document.myForm.task.value);
		alert(document.myForm.id.value);
		alert(document.myForm.showchords.value);
		alert(document.myForm.tranposekey.value);*/
		popup2 = window.open('', 'popup2', 'toolbar=yes,menubar=yes,scrollbars=yes,width=800');
		document.myForm.target = 'popup2';
	}
	document.myForm.submit();

}
/* ----------------------------------------------------
END: THIS FUNCTION ADD FOR OPEN SONG_VIEW PDF PREVIEW
---------------------------------------------------- */

//===== LISA AJAX STUFF =====
function XMLArray(url, getFunction){
	//Connects to Page(URL) and does function (getFunction)
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
	  xmlhttp=new XMLHttpRequest();
	  xmlhttp.onreadystatechange=eval(getFunction);
	  xmlhttp.open("GET", url, true);
	  xmlhttp.send(null);
	  
	  // code for IE
	}else if (window.ActiveXObject){
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	if (xmlhttp){
		xmlhttp.onreadystatechange=eval(getFunction);
		xmlhttp.open("GET",url,true);
		xmlhttp.send();
		}
	 }	// end if xmlhttprequest
	 
}

function CleanEmpty(getString){
//If string = " " set it to ""
	if(getString == " "){
		getString = ""
	}
	return getString
}



//----- Page specific funtions -----

function transposeUpdateSong(){
//Does song transposing for update_song.  Gets Plain text version of transposed song
// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
	{
	  // if "OK"
	  if (xmlhttp.status==200)
	  {
		 var response = xmlhttp.responseXML.documentElement;
		 var SongBody = response.getElementsByTagName("songbody_text")
		 var strTXT = SongBody[0].firstChild.nodeValue;
		 //var valSongBody = CleanEmpty(strTXT.replace("-amp-#146;", "'"));
		var arrString = strTXT.split('\n');
		var newStrTxt = '';
		for(var i = 0; i < arrString.length; i++){
			newStrTxt = newStrTxt + arrString[i].replace("-amp-#146;", "'") + "\n";
		}
		 var valSongBody = CleanEmpty(newStrTxt);

		//Set form field
		document.form.body.value = valSongBody;
		 
	  }
	  else
	  {
	  	 alert("Problem retrieving XML data:" + xmlhttp.statusText)
	  }	// end if status == 200
	 
	}	// end ready state
}



function transposeViewSong(){
//Does song transposing for update_song
// if xmlhttp shows "loaded"

	if (xmlhttp.readyState==4){
		
	  // if "OK"
	  if (xmlhttp.status==200){
		  	//docSongBody.innerHTML = xmlhttp.responseText;
			//return false;
  			var response = xmlhttp.responseXML.documentElement;
			var SongBody = response.getElementsByTagName("songbody_html");
			docSongBody = document.getElementById('songBody');

			if(SongBody[0].firstChild == null){
				docSongBody = document.getElementById('songBody');
				docSongBody.innerHTML = '';
			} else {
				var valSongBody = CleanEmpty(SongBody[0].firstChild.nodeValue);
				var xmlSongBody = SongBody[0];
				//Cleans up Receiving page
				docSongBody = document.getElementById('songBody');

				while (docSongBody.firstChild){
					//The list is LIVE so it will re-index each call
					docSongBody.removeChild(docSongBody.firstChild);
				};

				// Populate Receiving Page
				// Get children of xmlSongBody
				var xmlChildren = xmlSongBody.childNodes;

				// Go through xmlSongBody and transfer it to document
				for (var i = 0; i < xmlChildren.length; i++) {
					
					// If it's Span, create span and populate
					if(xmlChildren[i].nodeName == "span"){
					
						// Create Span Tag
						var docSpan = document.createElement("span");
						docSongBody.appendChild(docSpan);
					
						// Set Class
						docSongBody.lastChild.className='songLyricsKey';
									
						// Put text in
						var strHTML = xmlChildren[i].firstChild.nodeValue;
						var strHTML1 = strHTML.replace("-amp-#146;", "'");
						var strHTML2 = strHTML1.replace("-amp-amp;#039;", "'");
						strHTML2 = trim(strHTML2);
						//var docText = document.createTextNode(strHTML2.replace("-amp-#039;", "'")+"\r");
						var docText = document.createTextNode(strHTML2.replace("-amp-#039;", "'")+"\r");
						docSongBody.lastChild.appendChild(docText);
					}	// end if
				
					// If Text, 
					if(xmlChildren[i].nodeName == "#text"){
						//Put text in
						var strTXT = xmlChildren[i].nodeValue;
						var strTXT1 = strTXT.replace("-amp-#146;", "'");
						var strTXT2 = strTXT1.replace("-amp-amp;#039;", "'");
						//alert(strTXT2);
						var docText = document.createTextNode(strTXT2.replace("-amp-#039;", "'")+"\r");
						docSongBody.appendChild(docText);
					}
				}	// end for
				
				//var songBody = docSongBody.innerHTML;
				//docSongBody.innerHTML = songBody.replace('</span> ', '</span>');
			}
			
	  }else{
	  	alert("Problem retrieving XML data:" + xmlhttp.statusText);
	  }	// end if status == 200
		// end ready state
	}	else {
		//Cleans up Receiving page
		docSongBody = document.getElementById('songBody');
		docSongBody.innerHTML = 'Please wait...Processing !!!';
	}
}

//===== end lisa ajax =====
function modifySelection(id)
{
	if(document.getElementById('cb_'+id).checked == true)	
	{
		addToSelection(id);	
	}
	else
	{
		removeFromSelection(id);
	}
}

function addToSelection(id)
{
	var newstring = addSongIdToCookie(id);
	setCookie("selectedsongs", newstring, 1);
}

function removeFromSelection(id)
{
	var newstring = removeSongIdFromCookie(id);	
	setCookie("selectedsongs", newstring, 1);
}

function setCookie(cookie_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = cookie_name +  "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(cookie_name)
{
	if (document.cookie.length > 0)
 	{
		c_start = document.cookie.indexOf(cookie_name + "=");
		if (c_start != -1)
		{ 
			c_start = c_start + cookie_name.length + 1;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1)
			{
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function addSongIdToCookie(id)
{
	var currentcookievalue = getCookie("selectedsongs");
	var currentsongids = Array();
	if(currentcookievalue.length > 0)
	{
		currentsongids = currentcookievalue.split("|");
	}
	var newlength = currentsongids.push(id);
	
	var newcookievalue = currentsongids.join("|");
	
	return newcookievalue;
}

function removeSongIdFromCookie(id)
{
	var currentcookievalue = getCookie("selectedsongs");
	var currentsongids = Array();
	if(currentcookievalue.length > 0)
	{
		currentsongids = currentcookievalue.split("|");	
	}
	
	var newcookievalue = "";
	var i = 0;
	for(i=0;i<currentsongids.length;i++)
	{
		if(parseInt(currentsongids[i]) == parseInt(id))
		{
			//skip this value		
		}
		else
		{
			if(newcookievalue.length > 0)
			{
				newcookievalue = newcookievalue + "|" + parseInt(currentsongids[i]);
			}
			else
			{
				newcookievalue = newcookievalue + parseInt(currentsongids[i]);
			}
		}
	}
	return newcookievalue;
}

function checkselectedsongs()
{
	var currentcookievalue = getCookie("selectedsongs");
	
	if(currentcookievalue.length > 0)
	{
		var selectedsongids = currentcookievalue.split("|");
		
		if(selectedsongids.length > 0)
		{
			var i = 0;
			for(i=0;i<selectedsongids.length;i++)
			{
				if(document.getElementById('cb_' + selectedsongids[i]) != null)
				{
					document.getElementById('cb_' + selectedsongids[i]).checked = true;
				   //document.getElementById('cb_' + selectedsongids[i]).checked = false;
				}
			}
		}
	}
}

function clearselectedsongs()
{
	var newstring = "";
	setCookie("selectedsongs", newstring, 1);
}

function openHelpPopup(URL){
	var handelPopup;
	handelPopup = window.open('helpmain.php?page='+URL, '', 'resizable=no, menibar=no, scrollbars=yes');
	handelPopup.resizeTo(990, 750);
	handelPopup.moveTo(0, 0);
}

function openHomePopup(URL){
	var handelPopup;
	handelPopup = window.open('helpmain.php?page='+URL, '', 'resizable=no, menibar=no, scrollbars=yes');
	handelPopup.resizeTo(400, 400);
	handelPopup.moveTo(350, 300);
	handelPopup.focus();
}

function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
	 return false;
   } else {
 	 return true;
   }
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function setImage(frmEle, imageName){
	frmEle.src = 'themes/mp10/images/buttons/'+imageName;
}

function PDF_SingleSongView(previewType, song_id){
	var key = getDDBoxValue(document.form.song_key)
	
	var actsn = Math.floor(Math.random()*111111111111);
	if(previewType == 1){
		popup = window.open('pdf/download_song.php?song_id='+song_id+'&actsn='+actsn+'&chordshow=true&transposer_key='+key,'popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}else if(previewType == 2){
		popup = window.open('pdf/download_song.php?song_id='+song_id+'&actsn='+actsn+'&chordshow=false&transposer_key='+key,'popup','toolbar=yes,menubar=yes,scrollbars=yes,width=800,height=800,resizable=yes;');
	}	
	document.myForm.target = 'popup'
}