// --------------- DISPLAY/HIDE ELEMENTS --------------------------------- //
function display(id) {
	body=document.getElementById(id);
	if (body) {
		if (body.style.display == 'none') {
    		try {
    			body.style.display='table-row'; //for modern browsers
     		} catch(e) {
     		body.style.display = 'block'; //for IE
      		}
       	}
		else {
 			body.style.display = 'none';
		}
 	}
	return false;
}

function hide(id) {
	body=document.getElementById(id);
	body.style.display = 'none';
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank"
 }
}
window.onload = externalLinks;

function disabled() {
	alert('This feature is currently inactive. Please check back soon.');
}

// --------------- ADD/REMOVE FUNCTIONS --------------------------------- //
function add_field(element_name){
	var new_element = document.createElement('div');
	new_element.innerHTML = "<input type='text' class='norm_field' name='movement_array[]' size='50' maxlength='128' />";
	document.getElementById(element_name).appendChild(new_element);
}  
function add_editfield(element_name){
	var new_element = document.createElement('div');
	new_element.innerHTML = "<input type='text' class='edit_field' name='movement_newarray[]' size='50' maxlength='128' />";
	document.getElementById(element_name).appendChild(new_element);
}  
// --------------- XMLHTTP OBJECT DETECTION ------------------------------ //
//Detect appropriate xmlhttp objects
var xmlhttp = null;
if (window.XMLHttpRequest) {
	//assign native object for modern browsers (Mozilla, Safari, IE7, etc.)
	xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
	//assign the ActiveX object for IE5 and IE6
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

// --------------- XMLHTTP LOAD AUDIO ------------------------------------ //
function audio(filename, title) {
	var target = document.getElementById('audio_shell');
	xmlhttp.open("GET", 'includes/audiogallery.php?file=' + filename + '&title=' +title);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		target.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	return false;
}
// --------------- XMLHTTP DATA SUBMISSION ------------------------------- //

function request(task, item, id) {
	var target = document.getElementById(item);
	xmlhttp.open("GET", 'includes/request_' + task + '_editform.php?id='+id);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		target.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);

	//display the hidden shell 
	body = document.getElementById(item);
	if (body) {
		if (body.style.display == 'none') {
    		try {
    			body.style.display='table-row'; //for modern browsers
     		} catch(e) {
     		body.style.display = 'block'; //for IE
      		}
       	}
		else {
 			body.style.display = 'none';
		}
 	}
}
// --------------- XMLHTTP WRITE REFERENCE ---------------------------- //
function write_reference(item, id) {
	var target = document.getElementById(item);
	xmlhttp.open("GET", 'includes/request_recording_refs.php?id=' + id);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		target.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);

	//display the hidden shell 
	body = document.getElementById(item);
	if (body) {
		if (body.style.display == 'none') {
    		try {
    			body.style.display='table-row'; //for modern browsers
     		} catch(e) {
     		body.style.display = 'block'; //for IE
      		}
       	}
		else {
 			body.style.display = 'none';
		}
 	}
}
// --------------- XMLHTTP DELETE FILE ------------------------------ //
function del_object(request, object_id, type, target_id) {
	var target = document.getElementById(target_id);
	xmlhttp.open("GET", 'includes/delete_object.php?target=' + request + '&type=' + type + '&id=' + object_id);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		target.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	
	//display the hidden shell 
	body = document.getElementById(target_id);
	if (body) {
		if (body.style.display == 'none') {
    		try {
    			body.style.display='table-row'; //for modern browsers
     		} catch(e) {
     		body.style.display = 'block'; //for IE
      		}
       	}
		else {
 			body.style.display = 'none';
		}
 	}
}
// --------------- XMLHTTP DELETE DATABASE ROW ----------------------- //
function del_data(id, target) {
	var target = document.getElementById(target);
	xmlhttp.open("GET", 'includes/delete_data.php?id=' + id + '&target=' + target);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		target.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	
	//display the hidden shell 
	body = document.getElementById(target);
	if (body) {
		if (body.style.display == 'none') {
    		try {
    			body.style.display='table-row'; //for modern browsers
     		} catch(e) {
     		body.style.display = 'block'; //for IE
      		}
       	}
		else {
 			body.style.display = 'none';
		}
 	}
}
// --------------- EXTERNAL LINKING -------------------------------------- //
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank"
 }
}
window.onload = externalLinks;

// --------------- BBCODE SCRIPTING    ----------------------------------- //
// --------------- INSERT GENERAL TAGS ----------------------------------- //
function insert_tags(el, tag) {
	var selectedText = document.selection ? document.selection.createRange().text : el.value.substring(el.selectionStart,el.selectionEnd);// IE:Moz
	if(!selectedText.length){ 
		return;
	}//is something selected? return if not
	var newText = '[' + tag + ']' + selectedText + '[/' + tag + ']';
	if(document.selection){//IE & compatible
		document.selection.createRange().text=newText;
	} else {//Moz & compatible
		el.value = el.value.substring(0,el.selectionStart)+ newText +el.value.substring(el.selectionEnd,el.value.length);
	}
}

// --------------- INSERT HYPERLINK WITH ALIAS --------------------------- //
function insert_url_alias(el, tag) {
	var selectedText = document.selection ? document.selection.createRange().text : el.value.substring(el.selectionStart,el.selectionEnd);// IE:Moz
	if(!selectedText.length){ 
		selectedText = alert('Please type or highlight your link text before proceeding');
		return;
	}
	address = prompt('Please type your link address ("http://" will be entered automatically)','');
	regex = /.+\s/;
	var match = regex.test(selectedText);
	
	if(selectedText && address){
		//regex matching to test for whitespace
		if (regex.test(selectedText)){
			output = selectedText.replace(new RegExp('[\\s]+$', 'g'), '');//true, if whitespace should be added to end of selection
			var newText = '[' + tag + '=' + address + ']' + output + '[/' + tag + '] ';
			} else {
			output = selectedText.replace(new RegExp('[\\s]+$', 'g'), '');//false, if no whitespace at end
			var newText = '[' + tag + '=' + address + ']' + output + '[/' + tag + ']';
		}
		//return the changes
		if(document.selection){//IE & compatible
			document.selection.createRange().text=newText;
			} else {//Moz & compatible
			el.value = el.value.substring(0,el.selectionStart)+ newText +el.value.substring(el.selectionEnd,el.value.length);
		}
	}
}


