/* ------------------------------------------------------------------------------------------ */
/* Function to insert string at cursor in textbox */
function insertAtCursor(field_id, string) {
	var content = document.getElementById(field_id);

	//IE support
	if (document.selection) {
		content.focus();
		sel = document.selection.createRange();
		sel.text = string;
	}

	//MOZILLA/NETSCAPE support

	
	else if (content.selectionStart || content.selectionStart == '0') {
	var startPos = content.selectionStart;
	var endPos = content.selectionEnd;
	content.value = content.value.substring(0, startPos)
					+ string
					+ content.value.substring(endPos, content.value.length);
	} else {
		content.value += string;
	}
}

/* ------------------------------------------------------------------------------------------ */
/* Function to insert an Image */
function insertIMG(field_id) {
	var imgURL = prompt ("Please enter external image URL: (You must host image yourself)","http://");
	
	if (imgURL != null) {
		//document.getElementById(field_id).value += "[img]" + imgURL + "[/img]";
		insertAtCursor( field_id, "[img]" + imgURL + "[/img]" );
	}
}

/* ------------------------------------------------------------------------------------------ */
/* Function to insert an link */
function insertURL(field_id) {
	var linkURL = prompt ("Please enter website URL:","http://");
	
	if (linkURL != null) {
		//document.getElementById(field_id).value += "[url]" + linkURL + "[/url]";
		insertAtCursor( field_id, "[url]" + linkURL + "[/url]" );
	}
}

/* ------------------------------------------------------------------------------------------ */
/* Function to insert Styles */
function insertStyle(style_type,field_id) {
	//document.getElementById(field_id).value += "[" + style_type + "]" + 'YOUR TEXT HERE' + "[/" + style_type + "]";
	insertAtCursor( field_id, "[" + style_type + "]" + 'YOUR TEXT HERE' + "[/" + style_type + "]" );
}

/* ------------------------------------------------------------------------------------------ */
/* Function to check for new posts in Forum */
function checkNewPosts(id, cat, subcat, type) {

	var attr = 'id=' + id + '&cat=' + cat + '&subcat=' + subcat + '&type=' + type;
	
	// Show loading icon
	document.getElementById('check_loading_' + id).style.display='block';
	
	// Clear our fetching variable
	var xmlhttp = false; 
	 
	// Check for Active X
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // Try the first kind of active x object 
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); // Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
		}
	}
	
	// If we were able to get a working active x object, start an XMLHttpRequest
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
		xmlhttp = new XMLHttpRequest();
	}
	
	// File that loads the Pool data   
	var file = '/modules/ajax/chkNewPosts.php?'; 
	
	// Get the file
	xmlhttp.open('GET', file + attr, true);
	xmlhttp.onreadystatechange=function() {
	
		// Check if it is ready to recieve data
		if (xmlhttp.readyState==4) {
		
			var content = xmlhttp.responseText; // The content data which has been retrieved
			
			if (content) { 	// Make sure there is something in the content variable
			
				document.getElementById('check_result_' + id).innerHTML = content;
				
				// Show loading icon
				document.getElementById('check_loading_' + id).style.display='none';
				
			}
		}
	}
	
	xmlhttp.send(null) // Nullify the XMLHttpRequest
	return;
}