// JavaScript Document
function verseList_organizer(action, ref, tran) {
	
	_build_verseLists = function (ref) {
		var obj = getObjectPosition('vA_' + ref);
		var tmpDiv = document.createElement("Div");
			tmpDiv.style.position='absolute';
			tmpDiv.style.border='#8884AE solid 1px';
			tmpDiv.style.width = 538 + 'px';
			tmpDiv.style.height = 250 + 'px';
			tmpDiv.style.padding = 5 + 'px';
			tmpDiv.style.top = (obj.y + 30)+ 'px';
			tmpDiv.style.left = (obj.x + 1) + 'px';
			tmpDiv.style.backgroundColor = '#E7E3EF';
			tmpDiv.id = 'verse_listOrganizer';
			
			var innerTitle = document.createElement("Div");
				innerTitle.className = 'normal bold';
				innerTitle.innerHTML = 'verse list organizer';
				innerTitle.style.width = 533 + 'px';
				innerTitle.style.borderBottom = '1px solid #000000';
				innerTitle.style.paddingBottom = 5 + 'px';
			tmpDiv.appendChild(innerTitle);
			
			var innerDiv = document.createElement("Div");
				innerDiv.style.width = 533 + 'px';
				innerDiv.style.height = 220 + 'px';
				
				var curTbl = document.createElement("Table");
					curTbl.setAttribute('width', 100 + '%');
					curTbl.setAttribute('cellpadding', 0);
					curTbl.setAttribute('cellspacing', 0);
					curTbl.setAttribute('border', 0);
					
					var curTR1 = curTbl.insertRow(0);
					var curTD1 = curTR1.insertCell(0);
						curTD1.setAttribute('width', 40 + '%');
						curTD1.style.padding = '0px 12px';
							
						var curDiv1 = document.createElement("Div");
							curDiv1.className = 'medium bold';
							curDiv1.style.padding = '5px 0px';
							curDiv1.innerHTML = 'create new list';
						curTD1.appendChild(curDiv1);
							
						var curDiv2 = document.createElement("Div");
							curDiv2.style.paddingBottom = 2 + 'px';
								
							var curDiv2_input = document.createElement("Input");
								curDiv2_input.id = 'verseList_new';
								curDiv2_input.type = 'Text';
								curDiv2_input.className = 'small';
								curDiv2_input.style.height = 18 + 'px';
								curDiv2_input.style.width = 150 + 'px';
							curDiv2.appendChild(curDiv2_input);

							curDiv2.appendChild(document.createTextNode(' '));
							
							var curDiv2_btn = document.createElement("Button");
								curDiv2_btn.className = 'small bold purple_dark reverse';
								curDiv2_btn.onclick = new Function ( 'verseList_organizer(\'add_list\');' );
								curDiv2_btn.innerHTML = 'add';
							curDiv2.appendChild(curDiv2_btn);

						curTD1.appendChild(curDiv2);
						
					var curTD2 = curTR1.insertCell(1);
						curTD2.setAttribute('width', 60 + '%');
						curTD2.setAttribute('rowspan', 2);
						curTD2.setAttribute('align', 'right');
						curTD2.setAttribute('valign', 'top');
						curTD2.style.padding = '0px 12px';
						
						var curDiv1 = document.createElement("Div");
							curDiv1.setAttribute('align', 'left');
							curDiv1.className = 'medium bold';
							curDiv1.style.padding = '5px 0px';
							curDiv1.innerHTML = 'list display';
						curTD2.appendChild(curDiv1);
							
						var curDiv3 = document.createElement("Div");
							curDiv3.id = 'verseList_display';
							curDiv3.setAttribute('align', 'left');
							curDiv3.className = 'small';
							curDiv3.style.position = 'relative';
							curDiv3.style.overflow = 'auto';
							curDiv3.style.paddingLeft = 5 + 'px';
							curDiv3.style.paddingRight = 5 + 'px';
							curDiv3.style.border = '1px solid #B5B3CC';
							curDiv3.style.width = 285 + 'px';
							curDiv3.style.height = 190 + 'px';
							curDiv3.style.backgroundColor = '#FFFFFF';
						curTD2.appendChild(curDiv3);
						
					var curTR2 = curTbl.insertRow(1);
					var curTD1 = curTR2.insertCell(0);
						curTD1.setAttribute('width', 40 + '%');
						curTD1.setAttribute('valign', 'top');
						curTD1.style.padding = '0px 12px';

						var curDiv1 = document.createElement("Div");
							curDiv1.className = 'medium bold';
							curDiv1.style.padding = '5px 0px';
							curDiv1.style.borderTop = '1px solid #B5B3CC';
							curDiv1.style.marginTop = '5px';
							curDiv1.innerHTML = 'current verse lists';
						curTD1.appendChild(curDiv1);
							
						var curDiv2 = document.createElement("Div");
							curDiv2.className = 'small';
							curDiv2.style.paddingBottom = 5 + 'px';
								
							var curDiv2_select = document.createElement("Select");
								curDiv2_select.setAttribute('size', 7);
								curDiv2_select.id = 'verseList_select';
								curDiv2_select.className = 'medium';
								curDiv2_select.style.width = 198 + 'px';
								curDiv2_select.style.height = 114 + 'px';
							curDiv2.appendChild(curDiv2_select);

							curDiv2.appendChild(document.createElement('BR'));
							
							var curDiv2_btn = document.createElement("Button");
								curDiv2_btn.className = 'small bold purple_dark reverse';
								curDiv2_btn.style.marginTop = 3 + 'px';
								curDiv2_btn.style.width = 64 + 'px';
								curDiv2_btn.onclick = new Function ( 'verseList_organizer(\'dis_list\');' );
								curDiv2_btn.innerHTML = 'display';
							curDiv2.appendChild(curDiv2_btn);
					
							curDiv2.appendChild(document.createTextNode(' '));

							var curDiv2_btn = document.createElement("Button");
								curDiv2_btn.className = 'small bold purple_dark reverse';
								curDiv2_btn.style.marginTop = 3 + 'px';
								curDiv2_btn.style.width = 64 + 'px';
								curDiv2_btn.onclick = new Function ( 'verseList_organizer(\'def_list\');' );
								curDiv2_btn.innerHTML = 'default';
							curDiv2.appendChild(curDiv2_btn);

							curDiv2.appendChild(document.createTextNode(' '));

							var curDiv2_btn = document.createElement("Button");
								curDiv2_btn.className = 'small bold purple_dark reverse';
								curDiv2_btn.style.marginTop = 3 + 'px';
								curDiv2_btn.style.width = 64 + 'px';
								curDiv2_btn.onclick = new Function ( 'verseList_organizer(\'del_list\');' );
								curDiv2_btn.innerHTML = 'delete';
							curDiv2.appendChild(curDiv2_btn);

						curTD1.appendChild(curDiv2);

				innerDiv.appendChild(curTbl);
				
			tmpDiv.appendChild(innerDiv);
		document.body.appendChild(tmpDiv);
	}

	_get_listInfo = function(action) {
		var select_list = document.getElementById('verseList_select');

		var url = 'http://new.studylight.org/cgi-bin/desk/verselist_organizer.cgi?m=' + getCookie("memberData");
		on_load = function(action) {
			for( var i=select_list.options.length - 1 ; i >= 0; i-- ){ select_list.remove(i); }
			var xmlDocument = this.req.responseXML;
			var tmpinfo = xmlDocument.getElementsByTagName('results');
			var lists = tmpinfo[0].getElementsByTagName('list');
			for (i = 0; i < lists.length; i++) {
				var cur_option = document.createElement("Option");
					cur_option.text = lists[i].firstChild.data;
					cur_option.value = lists[i].getAttribute('number');
					cur_option.className = (lists[i].getAttribute('default') == 'y') ? 'purple_light' : '';
						
				try {
					select_list.add(cur_option, null);
				} catch(ex) {
					select_list.add(cur_option);
				}
			}
		}
		var http = new net.ContentLoader(url, on_load);
	}

	if (action == 'build') {
		if (document.getElementById('verse_listOrganizer') == null) {
			_build_verseLists(ref);
			_get_listInfo(action);
			return false;
		} else {
			document.body.removeChild(document.getElementById('verse_listOrganizer'));
			return false ;
		}
	}

	if (action != 'add_verse'){
		var curselect = document.getElementById('verseList_select');
		var curselect_list = (curselect.selectedIndex != -1) ? curselect.options[curselect.selectedIndex].value : (curselect.length > 0) ? curselect.options[0].value : null;
		var newlist = document.getElementById('verseList_new').value;
		if (action != 'remove_verse'){
			var display_div = document.getElementById('verseList_display');
				display_div.innerHTML = '';
		}
	}

	var url = 'http://new.studylight.org/cgi-bin/desk/verselist_organizer.cgi?m=' + getCookie("memberData");
		url = (action == 'remove_verse') ? url + '&a=remove_verse&n=' + ref.id + '&l=' + curselect_list : (action == 'add_verse') ? url + '&a=add_verse&n=' + ref + '&t=' + tran : (action == 'add_list') ? url + '&a=add_list&t=' + newlist : (action == 'del_list') ? url + '&a=del_list&n=' + curselect_list : (action == 'def_list') ? url + '&a=def_list&n=' + curselect_list : (action == 'dis_list') ? url + '&a=dis_list&n=' + curselect_list : '';

	on_load = function() {
		var xmlDocument = this.req.responseXML;
		var tmpinfo = xmlDocument.getElementsByTagName('results');

		if (action == 'remove_verse') {
			var answer = this.req.responseXML.getElementsByTagName('answer').item(0).firstChild.data;
			if (answer != null) {
				alert('Verse was removed from the \'' + answer + '\' list!');
				verseList_organizer('dis_list');
			} else {
				alert('There was a problem removing\nthis from your default list!\nPlease try again later!');	
			}
		} else if (action == 'add_verse') {
			var answer = this.req.responseXML.getElementsByTagName('answer').item(0).firstChild.data;
			if (answer != null) {
				if (answer.indexOf('- found') > -1) {
					alert('This verse is already on the \'' + answer.substring(0, answer.indexOf('- found')) + '\' list!');
				} else {
					alert('Verse was added to the \'' + answer + '\' list!');
				}
			} else {
				alert('There was a problem adding\nthis as your default list!\nPlease try again later!');	
			}
		} else if (action == 'add_list') {
			var answer = this.req.responseXML.getElementsByTagName('answer').item(0).firstChild.data;
			if (answer == 1) {
				alert('Your new list has been added!');
				_get_listInfo(action);
			} else if (answer == 2) {
				alert('You already have a verse list by this name!');
			} else {
				alert('There was a problem adding this list!\nPlease try again later!');	
			}
			document.getElementById('verseList_new').value = '';
		} else if (action == 'del_list') {
			var answer = this.req.responseXML.getElementsByTagName('answer').item(0).firstChild.data;
			if (answer == 1) {
				alert('Your list has been removed!');
				_get_listInfo(action);
			} else {
				alert('There was a problem removing this list!\nPlease try again later!');	
			}
		} else if (action == 'def_list') {
			var answer = this.req.responseXML.getElementsByTagName('answer').item(0).firstChild.data;
			if (answer == 1) {
				alert('This list has been set as the default!');
				_get_listInfo(action);
			} else {
				alert('There was a problem setting\nthis as your default list!\nPlease try again later!');	
			}

		} else if (action == 'dis_list') {
			var info = tmpinfo[0].getElementsByTagName('verses');
			for (i = 0; i < info.length; i++) {
				var ref = info[i].getAttribute('reference');
				var translation = info[i].getAttribute('translation');
				var bk = info[i].getAttribute('bk');
				var ch = info[i].getAttribute('ch');
				var vs = info[i].getAttribute('vs');
				var data = info[i].firstChild.data;
				
				var para = document.createElement("P");
					para.id = ref ;
					para.innerHTML = '<b>' + bk + ' ' + ch + ':' + vs + '</b> &#151; (' + translation + ') ' + data;
					
					var para_span = document.createElement("Span");
						para_span.id = 'del';
						para_span.style.display = 'none'
						para_span.className = 'clickable';
						
						var para_img = document.createElement("Img");
							para_img.className = 'purple_dark';
							para_img.src = '/images/delete.gif';
							para_img.height = 10;
							para_img.width = 10;
							para_img.border = 0;
							para_img.onclick = new Function ( 'verseList_organizer(\'remove_verse\', ' + this.parentNode.parentNode + ');' );
						para_span.appendChild(para_img);
					para.appendChild(para_span);

					para.onmouseover = function () {javascript:this.childNodes[2].style.display='inline';};
					para.onmouseout = function () {javascript:this.childNodes[2].style.display='none';};
					
				display_div.appendChild(para);
			}
		}
	}
	var http = new net.ContentLoader(url, on_load);
}

