/* 
	 NvMe.CA JavaScript Functions
	 
*/

var globalTimeOut;
var globalLinkDoWhat;
var xmlHttp;
var xmlSearch;
var xmlFlags;
var xmlRpic;
var xmlCaption;
var xmlVideoTitle;
var xmlVideoDesc;

/* 	when a selection is made on the search type dropdown
	this function changes the hidden input to match the selection
	
	@param1 	String		can be anything as long as theres a case for it in the switch
*/
function change_search_type(txt) {
	tbl = document.getElementById('search_what');
	val = document.getElementById('search_what_input');
	tbl.innerHTML = txt;

	switch (txt) {
		case "Articles":
			val.value = "1";
		break;
		case "Forum Threads":
			val.value = "2";
		break;		
		case "Videos":
			val.value = "3";
		break;	
		case "Blogs":
			val.value = "4";
		break;			
	}
	return;
}

/* create and return the xml object

	@return		Object		XML Object for either IE or FF
*/
function xml_obj()	{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// if running Internet Explorer
	if(window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	// if running Mozilla or other browsers
	else {
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	// return the created object or display an error message
	if (!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
	}
	else {
		return xmlHttp;
	}
}

// re-create a CAPTCHA image for the Register page upon request.
function reload_image() {
	xmlHttp = null;
	xmlHttp = xml_obj();
	// execute the quickstart.php page from the server
	xmlHttp.open("GET", "/include/_captcha.php?reload=1", true);
	// define the method to handle server responses
	xmlHttp.onreadystatechange = function() {
		// move forward only if the transaction has completed
		if (xmlHttp.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
			if (xmlHttp.status == 200) {
				img_src = xmlHttp.responseText;
				//alert(img_src);
				// update the client display using the data received from the server
				document.getElementById('img_captcha_container').innerHTML = img_src; 
				//alert(document.getElementById('img_captcha_container').innerHTML);
			}
			// a HTTP status different than 200 signals an error
			else {
				alert("There was a problem accessing the server: " + xmlHttp.statusText);
			}
		}
	}
	document.getElementById('img_captcha_container').innerHTML = '<b class="neckline"><i>Reloading...</b></i>';
	xmlHttp.send(null);
}

/* ajax operation for the search site function
*/
function perform_search() {
	xmlSearch = xml_obj();

	string = encodeURI(document.getElementById('search_string').value);
	type = document.getElementById('search_what_input').value;	
	
	if (string == "" || string == null) { 
		alert("Please enter a keyword."); 
		document.getElementById('search_string').focus(); 
	}
	else {
		xmlSearch.open("GET", "/_search_site/?query.nv,"+ type + "," + string, true);
		// define the method to handle server responses
		xmlSearch.onreadystatechange = function() {
			// move forward only if the transaction has completed
			if (xmlSearch.readyState == 4) {
			// status of 200 indicates the transaction completed successfully
				if (xmlSearch.status == 200) {
					var search_reply = xmlSearch.responseText;
					//alert (xmlSearch.responseText);
					document.getElementById('search_results').innerHTML = search_reply; 
				}
				// a HTTP status different than 200 signals an error
				else {
					document.getElementById('search_results').innerHTML = "<div class=\"windows neckline\" style=\"width: 99%;\">There was a problem accessing the server:"+xmlSearch.responseText+"</div>";
				}
			}
			
			document.getElementById('searching').style.display = 'none';
			document.getElementById('search_submit').disabled = false;
			document.search_frm.search_string.focus();
		}

		document.getElementById('searching').style.display = 'block';
		document.getElementById('search_submit').disabled = true;
		setTimeout(function() { xmlSearch.send(null); },2000);
	}
	
}
/* selects all checkboxes that have the name 'what'+[number]
	if checkbox is selected, it will be unselected
*/
function selectALL(what) {
	var x = 1;
	var chkname = what + x.toString();
	while (document.getElementById(chkname)) {
		document.getElementById(chkname).checked = document.getElementById(chkname).checked ? false : true;
		selectTD(document.getElementById(chkname));
		x++;
		chkname = what + x;
	}
	
}

// made obsolete by jquery, see include/jquery/_for_blog_options.php
function change_folder(showDiv,hideDivs) {
	document.getElementById(showDiv).style.display = 'block';
	// change visible content (con) to hidden (sd)
	
	var hd = new Array();
	hd = hideDivs.split(' ');
	var total = (hd.length * 1) - 1;
	
	var x = 0;
	while (x <= total) {
		var s = document.getElementById(hd[x]).style;
		s.display = "none";
		x++;
	}
}

// insert text at cursor function
function insertText(myField, myValue) {
	myField.focus();
	
	if (window.getSelection) {
		var start_pos = myField.selectionStart;
		var end_pos = myField.selectionEnd;
		var str = myField.value.substring(start_pos,end_pos);
		var sel_start = myField.value.substring(0,start_pos);
		var sel_end = myField.value.substring(end_pos,myField.value.length);
		var cursor_pos = start_pos + myValue.length;
		myField.value = sel_start + myValue + sel_end;
		myField.focus();
		myField.setSelectionRange(cursor_pos,cursor_pos);
		
	}
	else if (document.selection) {
		var sel = document.selection.createRange();
		sel.text = myValue;	
		myField.focus();
		sel.scrollIntoView();
	}
}
// reveal Loading div, and delay the submit by submitDelay miliseconds
function submitSearchDelay(showLoading,submitWhat,submitDelay) {
	document.getElementById(showLoading).style.display = 'block';
	if (globalTimeOut) { clearTimeOut(globalTimeOut); }
	globalTimeOut = setTimeout(function() { submitWhat.submit(); globalTimeOut = 0; }, submitDelay);
	return false;
}

/* the following insert_'s were made obsolete by wysiwyg */

// insert tab
function insert_tab(where) {
	insertText(where,'    ');
}

// insert image
function insert_image(where) {
	what = prompt("Please enter the URL of an image (http:// included)","http://");
	if (what) {
		insertText(where,'[img]' + what + '[/img]');
	}
}
// insert image
function insert_quote(where) {
	who = prompt("Who are you quoting? (leave blank for noone)",'');
	if (who) {
		what = prompt("What did " + who + " say?","");
		if (what) {
			insertText(where,'[quote="' + who + '"]' + what + '[/quote]');
		}
	}
	else {
		what = prompt("Enter to text to quote:",'');
		if (what) {
			insertText(where,'[quote]' + what + '[/quote]');
		}
	}
}
// insert image
function insert_url(where) {
	name = prompt("Please enter a name for the URL (leave blank for no url name)","Click Here");
	url = prompt("Please enter the URl (include http://):","http://");
	if (name && url) {
		insertText(where,'[url=\"' + name + '\"]' + url + '[/url]');
	}
	else if (url) {
		insertText(where,'[url]' + url + '[/url]');
	}
}
// insert HR
function insert_hr(where) {
	insertText(where,'\n[HR]\n');
}

/* insert emoticon 
 	syntax: 	insert_emo('SMILE',objForm)
	returns:	':SMILE:';
*/

function insert_emo(emo,where) {
	var emo = ":" + emo + ":";
	insertText(where,emo);
}

function insert_tag(tag,editorArea) {
	if (window.getSelection) {
		var str = editorArea.value.substring(editorArea.selectionStart,editorArea.selectionEnd);
		var sel_start = editorArea.value.substring(0,editorArea.selectionStart);
		var sel_end = editorArea.value.substring(editorArea.selectionEnd,editorArea.value.length);
		editorArea.value = sel_start + "[" + tag + "]" + str + "[/" + tag + "]" + sel_end;
		//alert(editorArea);
	}
	else if (document.selection) {
		var str = document.selection.createRange().text;
		editorArea.focus();
		var sel = document.selection.createRange();
		sel.text = "[" + tag + "]" + str + "[/" + tag + "]";
		//alert(editorArea);
	}
	return;
}

/* end of obsolete insert_'s */

// i need to get rid of this.. i don't like it
function verifyNotify(checkBox) {
	if (checkBox.notify.disabled == 1) {
		checkBox.notify.disabled = 0;
		checkBox.via.disabled = 0;		
	}
	else if (checkBox.comments.checked == 0) {
		checkBox.notify.checked = 0;
		checkBox.notify.disabled = 1;
		checkBox.via.disabled = 1;		
	}
	return;
}
function verifyNotifyThread(checkBox) {
	if (checkBox.notify.disabled == 1) {
		checkBox.notify.disabled = 0;
		checkBox.via.disabled = 0;		
	}
	else if (checkBox.locked.checked == 0) {
		checkBox.notify.checked = 0;
		checkBox.notify.disabled = 1;
		checkBox.via.disabled = 1;		
	}
	return;
}

// made obsolete by jquery see include/jquery/_for_blog_list.php
function toggleExpand(whichLayer,whichInfo,whichText) {
	var style2 = document.getElementById(whichLayer).style;
	var style3 = document.getElementById(whichInfo).style;	
	style2.display = style2.display ? "" : "block";
	style3.display = style2.display == "block" ? "none":"block";
	whichText.value = whichText.value == "+" ? "-" : "+";
}

function toggleDisplay(id,tog) {
	var st = document.getElementById(id).style;
	st.display = tog ? "block" : "none";
}

// show or hide an element based on their current state.
function toggleOnOff(id) {
	var st = document.getElementById(id).style;
	st.display = (st.display == "block") ? "none" : "block";
}

// when a table'd item is checked all cells on the row are colored depending on the state of the check
function selectTD(checkName) {
	var whatId = checkName.value;
	var whatType = checkName.name.split("_");

	var whichHeadline = whatType[0] + '_headline_' + whatId;
	var stHeadline = document.getElementById(whichHeadline).style;
	stHeadline.background = !checkName.checked ? "#FFFFFF" : "#F5F5F5";
	
	var whichAction = whatType[0] + '_action_' + whatId;
	var stAction = document.getElementById(whichAction).style;	
	stAction.background = !checkName.checked ? "#FFFFFF" : "#F5F5F5";
	
	var whichCheck = whatType[0] + '_check_' + whatId;
	var stCheck = document.getElementById(whichCheck).style;	
	stCheck.background = !checkName.checked ? "#FFFFFF" : "#F5F5F5";
	
	var whichFromMsg = whatType[0] + '_from_' + whatId;
	var whichFromDate = whatType[0] + '_date_' + whatId;
	
	if (document.getElementById(whichFromMsg)) {
		document.getElementById(whichFromMsg).style.background = !checkName.checked ? "#FFFFFF" : "#F5F5F5";
	}
	if (document.getElementById(whichFromDate)) {
		document.getElementById(whichFromDate).style.background = !checkName.checked ? "#FFFFFF" : "#F5F5F5";
	}
	
}

// automatically change color of cells if the checkbox state is checked upon page load.
function autoSelectTD(myForm) {
	var x = 0;
	var total = myForm.elements.length - 1;
	while (x <= total) {
		if ((myForm.elements[x].type == 'checkbox') && myForm.elements[x].checked) {
			selectTD(myForm.elements[x]);
		}
		x++;
	}
	
}

// swap the page to what the user clicked on and hide all other pages.
function changePage(toPage,allPages) {
	var list = new Array();
	list = allPages.split(' ');
	var totalPages = (list.length * 1) - 1;
	
	var x = 0;
	while (x <= totalPages) {
		var curPage = document.getElementById(list[x]).style;
		
		var count = x + 1;
		
		var otherPage = document.getElementById('page_link_' + count.toString());
		if (otherPage) {
			otherPage.innerHTML = count == toPage ? '[' + (x + 1) + ']' : (x + 1);
		}
		
		curPage.display = count == toPage ? "block" : "none";
		x++;
	}
}

// show the reply box in a blog artile.
function show_reply_box(postBox) {
	var pr = document.getElementById(postBox).style;
	
	pr.display = 'block';
	
	var c = document.getElementById('last_page').innerHTML;
	var splt = new Array();
	splt = c.split('_');
	changePage(splt[2],document.getElementById('list_pages').innerHTML);
}

function show_reply_box_quote(postBox,qWhat,quoteUser) {
	var pr = document.getElementById(postBox).style;
	
	setTimeout(function() { editor.body.innerHTML = '[QUOTE="' + quoteUser + '"]' + qWhat.innerHTML + '[/QUOTE]'; pr.display = 'block'; },1000);
	
	
	var c = document.getElementById('last_page').innerHTML;
	var splt = new Array();
	splt = c.split('_');
	changePage(splt[2],document.getElementById('list_pages').innerHTML);
}
function hide_reply_box(postBox,qWhat,qWhere,quoteUser) {
	var pr = document.getElementById(postBox).style;
	
	pr.display = 'none';
	var c = document.getElementById('last_page').innerHTML;
	var splt = new Array();
	splt = c.split('_');
	changePage(splt[2],document.getElementById('list_pages').innerHTML);
}

function links_toggle_edit(id) {
	var ll = document.getElementById('links_list').style;
	var le = document.getElementById('links_edit').style;
	
	if (ll.display == 'block') {
		le.display = 'block';
		ll.display = 'none';
		document.blogopt.linkdesc.value = document.getElementById('linkdesc_' + id).value;
		document.blogopt.linkalt.value = document.getElementById('linkalt_' + id).value;
		document.blogopt.linkname.value = document.getElementById('linkname_' + id).value;
		document.blogopt.linkurl.value = document.getElementById('linkurl_' + id).value;
		document.blogopt.linkid.value = id;
		document.blogopt.newwindow.checked = document.getElementById('newwindow_' + id).value == "1" ? true : false;
		//alert(document.blogopt.linkid.value);
	}
	else if (ll.display == 'none') {
		ll.display = 'block';
		le.display = 'none';
		document.blogopt.linkid.value = "";
	}
}
function links_toggle_delete(id) {
	var ll = document.getElementById('links_list').style;
	var le = document.getElementById('links_delete').style;
	if (id) { var ldc = document.getElementById('links_delete_change_' + id); }
	
	if (ll.display == 'block') {
		le.display = 'block';
		ll.display = 'none';
		//alert(document.blogopt.linkid.value);
		document.blogopt.links_delete_id.value = id;
	}
	else if (ll.display == 'none') {
		ll.display = 'block';
		le.display = 'none';
		//if (id) { ldc.innerHTML = 'Delete'; }
	}
}
function links_delete_confirm(id) {
	var ldc = document.getElementById('links_delete_change_' + id);
	if (ldc.innerHTML == 'Delete') {
		document.blogopt.links_delete_list.value = document.blogopt.links_delete_list.value + ' ' + id;
		ldc.innerHTML = 'UnDelete';
		document.getElementById('links_do_what').innerHTML = '<strong>don\'t</strong> ';
		links_toggle_delete(id);
		//alert(document.blogopt.links_delete_list.value);
		return true;
	}
	else if (ldc.innerHTML == 'UnDelete') {
		document.blogopt.links_delete_list.value = document.blogopt.links_delete_list.value.replace(' ' + id, '');
		ldc.innerHTML = 'Delete';
		document.getElementById('links_do_what').innerHTML = '';
		//alert(document.blogopt.links_delete_list.value);
		links_toggle_delete(id);
		return true;
	}
	
}
function links_toggle_add() {
	var ll = document.getElementById('links_list').style;
	var la = document.getElementById('links_add').style;
	
	if (ll.display == 'block') {
		la.display = 'block';
		ll.display = 'none';
		document.blogopt.linkaddconfirm.value = '1';
		document.blogopt.savechanges.value = '  Add Link & Save Changes  ';
	}
	else if (ll.display == 'none') {
		ll.display = 'block';
		la.display = 'none';
		document.blogopt.linkaddconfirm.value = '0';
		document.blogopt.savechanges.value = '  Save Changes  ';
	}
}

function rollover_swap(img_src,img_id) {
	document.getElementById(img_id).src = img_src;
	return;
}

function flags_show_hide(state,id) {
	document.getElementById('flags_hidden_' + id).style.display = !state ? 'none' : 'block';
	document.getElementById('flags_' + id).style.display = !state ? 'block' : 'none';
	document.getElementById('flags_status_saving_' + id).style.display = state ? 'none' : 'block';
	document.getElementById('flags_status_' + id).style.display = state ? 'block' : 'none';
}

function flags_save_changes(id) {
	xmlFlags = xml_obj();

	string = encodeURI(document.getElementById('flags_edit_' + id).value);
	if (string != null && string != "") {
		document.getElementById('flags_status_saving_' + id).style.display = 'block';
		document.getElementById('flags_status_' + id).style.display = 'none';		
		
		xmlFlags.open("GET", "/_flags_save_changes/?save.nv," + id + "," + string, true);
		
		// define the method to handle server responses
		xmlFlags.onreadystatechange = function() {
			// move forward only if the transaction has completed
			if (xmlFlags.readyState == 4) {
			// status of 200 indicates the transaction completed successfully
				if (xmlFlags.status == 200) {
					var reply = xmlFlags.responseText;
					if (reply.match(/OK/)) {
						flags = reply.split(/ /)[1];
						//alert (xmlFlags.responseText);
						document.getElementById('flags_' + id).innerHTML = flags; 
						document.getElementById('flags_edit_' + id).value = flags;
						flags_show_hide(0,id);
					}
					else {
						document.getElementById('flags_' + id).innerHTML = "<i>Error</i>"; 
						flags_show_hide(0,id);
					}
				}
				// a HTTP status different than 200 signals an error
				else {
					document.getElementById('flags_' + id).innerHTML = "<i>Error</i>";
					flags_show_hide(1,id);
				}
			}
		}
		setTimeout(function() { xmlFlags.send(null); },100);
	}
}

function next_rpic() {
	xmlRpic = xml_obj();
		
	xmlRpic.open("GET", "/_next_rpic/", true);
	document.getElementById('loading_link').style.display = 'block';
	// define the method to handle server responses
	xmlRpic.onreadystatechange = function() {
		// move forward only if the transaction has completed
		if (xmlRpic.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
			if (xmlRpic.status == 200) {
				var reply = xmlRpic.responseText;
				document.getElementById('rpic_image').innerHTML = reply;
				document.getElementById('rpic_caption').innerHTML = document.getElementById('rpic_img_tag').alt;
			}
			// a HTTP status different than 200 signals an error
			else {
				document.getElementById('rpic_caption').innerHTML = "<i>Error Getting rPic</i>";
			}
		}
		document.getElementById('loading_link').style.display = 'none';		
	}
	setTimeout(function() { xmlRpic.send(null); },2000);
}

function caption_show_hide(state,id) {
	document.getElementById('caption_hidden_' + id).style.display = !state ? 'none' : 'block';
	document.getElementById('caption_' + id).style.display = !state ? 'block' : 'none';
	document.getElementById('caption_status_saving_' + id).style.display = state ? 'none' : 'block';
	document.getElementById('caption_status_' + id).style.display = state ? 'block' : 'none';
}

function caption_save_changes(id) {
	xmlCaption = xml_obj();

	string = encodeURI(document.getElementById('caption_edit_' + id).value);
	document.getElementById('caption_status_saving_' + id).style.display = 'block';
	document.getElementById('caption_status_' + id).style.display = 'none';		
	
	xmlCaption.open("GET", "/_caption_save_changes/?save.nv," + id + "," + string, true);
	//alert("ID:"+id+" - "+string);
	// define the method to handle server responses
	xmlCaption.onreadystatechange = function() {
		// move forward only if the transaction has completed
		if (xmlCaption.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
			if (xmlCaption.status == 200) {
				var reply = xmlCaption.responseText;
				if (!reply.match(/ERR/)) {
					caption = reply;
					//alert (xmlCaption.responseText);
					document.getElementById('caption_' + id).innerHTML = caption; 
					document.getElementById('caption_edit_' + id).value = caption != "" ? caption : "";
					caption_show_hide(0,id);
				}
				else {
					document.getElementById('caption_' + id).innerHTML = "<i>Error</i> - " + reply; 
					caption_show_hide(0,id);
				}
			}
			// a HTTP status different than 200 signals an error
			else {
				document.getElementById('caption_' + id).innerHTML = "<i>Error</i> - " + reply;
				caption_show_hide(1,id);
			}
		}
	}
	setTimeout(function() { xmlCaption.send(null); },100);
}
function videotitle_show_hide(state,id) {
	document.getElementById('videotitle_hidden_' + id).style.display = !state ? 'none' : 'block';
	document.getElementById('videotitle_' + id).style.display = !state ? 'block' : 'none';
	document.getElementById('videotitle_status_saving_' + id).style.display = state ? 'none' : 'block';
	document.getElementById('videotitle_status_' + id).style.display = state ? 'block' : 'none';
}

function videotitle_save_changes(id) {
	xmlVideoTitle = xml_obj();

	string = encodeURI(document.getElementById('videotitle_edit_' + id).value);
	if (!string.replace(/^\s*|\s*$/g,'')) {
		alert("Please enter a valid video title");
		document.getElementById('videotitle_edit_' + id).focus();
		return 0;
	}
	document.getElementById('videotitle_status_saving_' + id).style.display = 'block';
	document.getElementById('videotitle_status_' + id).style.display = 'none';		
	
	xmlVideoTitle.open("GET", "/_videotitle_save_changes/?save.nv," + id + "," + string, true);
	//alert("ID:"+id+" - "+string);
	// define the method to handle server responses
	xmlVideoTitle.onreadystatechange = function() {
		// move forward only if the transaction has completed
		if (xmlVideoTitle.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
			if (xmlVideoTitle.status == 200) {
				var reply = xmlVideoTitle.responseText;
				if (!reply.match(/ERR/)) {
					title = reply;
					//alert (xmlVideoTitle.responseText);
					document.getElementById('videotitle_' + id).innerHTML = title; 
					document.getElementById('videotitle_edit_' + id).value = title;
					videotitle_show_hide(0,id);
				}
				else {
					document.getElementById('videotitle_' + id).innerHTML = "<i>Error</i> - " + reply; 
					videotitle_show_hide(0,id);s
				}
			}
			// a HTTP status different than 200 signals an error
			else {
				document.getElementById('videotitle_' + id).innerHTML = "<i>Error</i> - " + reply;
				videotitle_show_hide(1,id);
			}
		}
	}
	setTimeout(function() { xmlVideoTitle.send(null); },100);
}

function videodesc_show_hide(state,id) {
	document.getElementById('videodesc_hidden_' + id).style.display = !state ? 'none' : 'block';
	document.getElementById('videodesc_' + id).style.display = !state ? 'block' : 'none';
	document.getElementById('videodesc_status_saving_' + id).style.display = state ? 'none' : 'block';
	document.getElementById('videodesc_status_' + id).style.display = state ? 'block' : 'none';
}

function videodesc_save_changes(id) {
	xmlVideoDesc = xml_obj();

	string = encodeURI(document.getElementById('videodesc_edit_' + id).value);
	document.getElementById('videodesc_status_saving_' + id).style.display = 'block';
	document.getElementById('videodesc_status_' + id).style.display = 'none';		
	
	xmlVideoDesc.open("GET", "/_videodesc_save_changes/?save.nv," + id + "," + string, true);
	//alert("ID:"+id+" - "+string);
	// define the method to handle server responses
	xmlVideoDesc.onreadystatechange = function() {
		// move forward only if the transaction has completed
		if (xmlVideoDesc.readyState == 4) {
		// status of 200 indicates the transaction completed successfully
			if (xmlVideoDesc.status == 200) {
				var reply = xmlVideoDesc.responseText;
				if (!reply.match(/ERR/)) {
					desc = reply;
					//alert (xmlVideoDesc.responseText);
					document.getElementById('videodesc_' + id).innerHTML = desc; 
					document.getElementById('videodesc_edit_' + id).value = desc != "" ? desc : "";
					videodesc_show_hide(0,id);
				}
				else {
					document.getElementById('videodesc_' + id).innerHTML = "<i>Error</i> - " + reply; 
					videodesc_show_hide(0,id);
				}
			}
			// a HTTP status different than 200 signals an error
			else {
				document.getElementById('videodesc_' + id).innerHTML = "<i>Error</i> - " + reply;
				videodesc_show_hide(1,id);
			}
		}
	}
	setTimeout(function() { xmlVideoDesc.send(null); },100);
}