// JavaScript Document //

//**** FOCUS CURSOR ON FIRST FIELD IF IT EXISTS ****//
function focusCursor(){
	//**** USE THE SECOND FORM (first form is LOGIN) ****//
	input_form = document.forms[1]
	if(input_form){
		if(input_form.elements.length >=3){
			input_obj = input_form.elements[3]
			if(input_obj){
				if(input_obj.type=="text"){
					input_obj.focus()
				}
			}
		}
	}
}

//**** CHANGE A CSS CLASS FROM ACTIVE TO INACTIVE (or vice versa) ****///
function classActivation(theID, new_status){

	theItem = document.getElementById(theID)
	if(theItem){
		theCSS = theItem.className
		startPos = theCSS.lastIndexOf("_")+1
		endPos = theCSS.length
		
		css_status = theCSS.substr(startPos, endPos)
		css_root = theCSS.substr(0, startPos)
		
		/* IF new_status IS NULL, SIMPLY REVERSE THE CURRENT STATUS */
		if(new_status.length==0||new_status==0){
			if(css_status=="active"){new_status = "inactive"}else{new_status = "active"}
		}else{
			new_status = new_status
		}
		
		//addSystemMessage("CSS Activation: " + theID + " is " + new_status)
		theItem.className = css_root + new_status
	}
}

function plusMinus(theID){

	if(document.getElementById(theID)){
		theItem = document.getElementById(theID)
		
		theImage = theItem.src
		startPos = theImage.lastIndexOf("/")+1
		endPos = theImage.length
		theImage = theImage.substr(startPos, endPos)
		
		if(theImage=="icon_plus.gif"){theItem.src="images/icon_minus.gif"}else{theItem.src="images/icon_plus.gif"}
	}
}

function imageOnOff(theID, new_status){
	
	if(document.getElementById(theID)){
		
		theItem = document.getElementById(theID)
	//	alert(theID)
	
		theImage = theItem.src
		
		/* DETERMINE IF THE IMAGE IS ON or OFF */
		startPos = theImage.lastIndexOf(".")-2
		endPos = theImage.lastIndexOf(".")
		statusPos = theImage.lastIndexOf("_")+1
		folderPos = theImage.lastIndexOf("/")+1
		imageLength = theImage.length
		
		imageRoot = theImage.slice(0, folderPos)
		imageName = theImage.slice(folderPos, statusPos)
		imageStatus = theImage.slice(startPos, endPos)
		imageType = theImage.slice(endPos, imageLength)
		
		if(new_status){
			if(imageStatus=="on"){new_status = "off"}else{new_status = "on"}
		}else{
			new_status = image_status
		}
		
		document.getElementById(theID).src = imageRoot + imageName + new_status + imageType
	}

}

function showHideElement(theID, ID_str){
	
	//**** HIDE ALL OTHER ELEMENTS IN THE SAME GROUP ****//
	if(ID_str!=null){
		hide_array = new Array()
		hide_array = ID_str.split(":")
		for(h=0;h<hide_array.length;h++){
			hide_element = document.getElementById(hide_array[h])
			if(hide_element&&hide_array[h]!=theID){
				hide_element.style.display = "none"
			}
		}
	}
	
	//**** SHOW THE SELECTED ELEMENT ****//
	if(document.getElementById(theID)){
		theDisplay = document.getElementById(theID).style.display
		if(theDisplay!=undefined&&theDisplay=="none"){
			document.getElementById(theID).style.display = ""
			imageOnOff("arrows_"+theID, "off")
		}else{
			document.getElementById(theID).style.display = "none"
			imageOnOff("arrows_"+theID, "on")
		}
	}
}
function showElement(the_ID){
	the_obj = document.getElementById(the_ID)
	
	if(the_obj){the_obj.style.display = "block"}	
}
function hideElement(the_ID){
	the_obj = document.getElementById(the_ID)
	
	if(the_obj){the_obj.style.display = "none"}	
}

function switchSetlistDisplay(the_ID){
	linear_obj = document.getElementById("linear_"+the_ID)
	stacked_obj = document.getElementById("stacked_"+the_ID)
//	setlist_layout_obj = document.getElementById("setlist_layout_"+the_ID)
	
	if(linear_obj&&stacked_obj){
		if(linear_obj.style.display=="none"){
			linear_obj.style.display="block"
			stacked_obj.style.display="none"
		//	setlist_layout_obj.value="linear"
		}else{
			linear_obj.style.display="none"
			stacked_obj.style.display="block"
		//	setlist_layout_obj.value="stacked"
		}
	}
}
	
function centerElement(positionType, windowDimension){
	var newLocation = 0
	var totalWidth, totalHeight
	
	// POSITION TYPE SHOULD BE 'x' OR 'y' //
	if(positionType&&windowDimension){
		totalWidth = screen.width
		totalHeight = screen.height
	//	alert("Screen: (" + totalWidth + " x " + totalHeight + ")")
		
		if(positionType=="x"){newLocation = (totalWidth/2)-(windowDimension/2)}
		if(positionType=="y"){newLocation = (totalHeight/2)-(windowDimension/2)}
	//	alert("Position: " + positionType + " is " + newLocation)
		
	}
	return newLocation;
}
function activateTab(tab_ID){
	
	for(s=0;s<10;s++){
		element_obj = document.getElementById('set_'+s)
		if(element_obj){element_obj.className = "setlist_group_inactive";}
		
		element_obj = document.getElementById('tab_set_'+s)
		if(element_obj){element_obj.className = "tab_inactive";}
	}
	classActivation('set_'+tab_ID, 'active')
	classActivation('tab_set_'+tab_ID, 'active')
	
	displaySongs('', '', tab_ID, '', '')
	
	active_song_details = 0
}
function filterField(the_ID){
//	alert("Filtering Dropdown")
	filter_obj = document.getElementById(the_ID)
	filtered_obj = document.getElementById(the_ID+"_filtered")
	
	if(filter_obj&&filtered_obj){
		filtered_obj.options.length = 0
		filter_obj.style.display = "none"
		filtered_obj.style.display = "block"
	
		var m = 0
		search_value = document.getElementById("filter_field").value
		matched_array = new Array()
		collection_obj = filter_obj.options
		item_count = filter_obj.options.length
		
	//	alert("Items: " + item_count)
		for(i=0;i<item_count;i++){
			item_text = collection_obj[i].text
			item_value = collection_obj[i].value
			search_result = item_text.search(search_value)
		//	alert(filtered_obj.options.length + ". " + item_value + ": " + search_value + " = " + item_text + " -- " + search_result)
		
			if(search_result>=0&&item_text.length>0&&item_value.length>0){
				if(m==0){filter_obj.value = item_value}
				filtered_obj.options[m] = new Option(item_text, item_value, false, false)
				m = m + 1
			}
		}
	}
}
function clearFilter(the_ID){

	filter_obj = document.getElementById(the_ID)
	filtered_obj = document.getElementById(the_ID+"_filtered")
	
	if(filter_obj&&filtered_obj){
		filter_obj.style.display = "block"
		filtered_obj.style.display = "none"
		filtered_obj.value = "cleared"
	}
}

function addListItem(from_list, to_list){

	from_list_obj = document.getElementById(from_list)
	to_list_obj = document.getElementById(to_list)
	
	if(from_list_obj&&to_list_obj){
	
		for (i=0;i<from_list_obj.options.length;i++){
			var current = from_list_obj.options[i];
			if(current.selected){
			the_value = from_list_obj.options[i].value
			the_text = from_list_obj.options[i].text
			}
		}
		
		if(the_value.length>0&&the_text.length>0){
			to_list_obj.options[to_list_obj.options.length] = new Option(the_text, the_value, false, false)
		}else{
			alert("There was a problem with your selection!")
		}
	}
	
}

function removeListItem(the_list){
	
	the_value_array = new Array()
	the_text_array = new Array()
	array_counter = 0
	the_list_obj = document.getElementById(the_list)
	
	if(the_list_obj){
	
		for (i=0;i<the_list_obj.options.length;i++){
			var current = the_list_obj.options[i];
			
			if(current.selected){
			//**** DON'T ADD TO TEMPORARY LIST ****//
			//	alert("Item [" +the_list_obj.options[i].text+ "] was removed")
			}else{
			the_value_array[array_counter] = the_list_obj.options[i].value
			the_text_array[array_counter] = the_list_obj.options[i].text
			array_counter = array_counter + 1
			}
		}
		the_list_obj.options.length = 0
	//	alert("Active list has been cleared!")
		
		for (i=0;i<the_value_array.length;i++){
	//		alert("New item [" +the_text_array[i]+ "] was added")
			the_list_obj.options[i] = new Option(the_text_array[i], the_value_array[i], false, false)
		}
	}
}

function filterField(the_ID){
//	alert("Filtering Dropdown")
	filter_obj = document.getElementById(the_ID)
	filtered_obj = document.getElementById(the_ID+"_filtered")
	
	if(filter_obj&&filtered_obj){
		filtered_obj.options.length = 0
		filter_obj.style.display = "none"
		filtered_obj.style.display = "block"
	
		var m = 0
		search_value = document.getElementById("filter_field").value
		matched_array = new Array()
		collection_obj = filter_obj.options
		item_count = filter_obj.options.length
		
	//	alert("Items: " + item_count)
		for(i=0;i<item_count;i++){
			item_text = collection_obj[i].text
			item_value = collection_obj[i].value
			search_result = item_text.search(search_value)
		//	alert(filtered_obj.options.length + ". " + item_value + ": " + search_value + " = " + item_text + " -- " + search_result)
		
			if(search_result>=0&&item_text.length>0&&item_value.length>0){
				if(m==0){filter_obj.value = item_value}
				filtered_obj.options[m] = new Option(item_text, item_value, false, false)
				m = m + 1
			}
		}
	}
}
function mobileSetlistCreator(){
	the_value= ""
	the_text= ""
	song_ID_obj = document.getElementById("song_ID")
	song_name_obj = document.getElementById("song_name")
	from_list_obj = document.getElementById("song_list_filtered")
	
	for (i=0;i<from_list_obj.options.length;i++){
		var current = from_list_obj.options[i];
		if(current.selected){
		the_value = from_list_obj.options[i].value
		the_text = from_list_obj.options[i].text
		}
	}
	song_ID_obj.value = the_value
	song_name_obj.value = the_text

}
function clearFilter(the_ID){

	filter_obj = document.getElementById(the_ID)
	filtered_obj = document.getElementById(the_ID+"_filtered")
	
	if(filter_obj&&filtered_obj){
		filter_obj.style.display = "block"
		filtered_obj.style.display = "none"
		filtered_obj.value = "cleared"
	}
}
function selectAllOptions(the_ID){
list_obj = document.getElementById(the_ID)
	if(list_obj){
		for(i=0;i<list_obj.options.length;i++){
			list_obj.options[i].selected = "selected"
		}
	}
}

function getElementsByClassName(cssName, htmltag){ 
	var arr = new Array(); 
	var elems = document.getElementsByTagName(htmltag);
	for ( var cls, i = 0; ( elem = elems[i] ); i++ ){
		if ( elem.className == cssName ){
			arr[arr.length] = elem;
		}
	}
	return arr;
}
