/*
*	the callback function for gathering data to quote 
*	quoteMessage(msgOwner, ID) : no return
*	
*	msgOwner: who are we quoting?  this is their username 
*	ID: the message ID that we're quoting from
*/
function quoteMessage(msgOwner, ID) {
	var msgContent = document.getElementById('tmpQuoteStore').innerHTML;
	
	if (msgOwner !== "") {
		msgOwner = "=" + msgOwner;
		if (ID !== "") {msgOwner += ";" + ID;}
	}
	
	document.getElementById('feedback').value += '[quote'+msgOwner+']' + msgContent + '[/quote]';
	document.getElementById('replyBlock').style.display='block';
	document.getElementById('replyButton').style.display='none';
	document.location = '#replyStart';
}
// *** // 



/*
*	display and populate the select box for forum searching (gets sel contents from JSON) 
*	showSearch( frame, selBox, JSONobject ) : no return
*	
*	frame: the search frame ID (to display) 
*	selBox: the select box ID (to populate with forum results) 
*	JSONobject: the JSON data (must come from trusted server!) 
*/
function showSearch( frame, selBox, JSONobject ) {
	if (JSONobject && document.getElementById && document.getElementById(selBox)
		&& document.getElementById(frame).style.display == 'none') {
		var sBoxObj;
		var newOption;
		
		try {
			sBoxObj = json_parse(JSONobject);
		} catch (err) {alert(err.name + ": " + err.message + "\n" + err.at + "  " + err.text);}
		
		for (var i = 0; i < sBoxObj.ID.length; ++i) {
			newOption = new Option( sBoxObj.name[i], sBoxObj.ID[i]);
			document.getElementById(selBox).options[i+1] = newOption;
		}
		
		document.getElementById(frame).style.display = 'block';
	}
}



/*
*	updates the user's color choice for posts
*	updateColor(color) : no return
*	
*	color: the color they chose (a string) 
*/
function updateColor(color) {document.previewMsg.fontcolor.value=color;}
// *** // 



/*
*	a visual alert that they must be logged in to use services 
*	alertSIG() : no return 
*/
function alertSIG() {alert("This feature is for GAC Members only!");return false;}
// *** // 



/*
*	inserts arbitrary values into a textbox at the cursor 
*	insertValue( field, startTag, endTag?, intermTag?, replicate? ) : no return
*	
*	field: the text field that we are inserting into 
*	startTag: the text that will be inserted before insertion point (possibly start tag)
*	endTag (optional): the tag that will be inserted after the selection 
*	intermTag (required if replicate): the tag to append to each new line
*	replicate (optional): replicate intermTag tag across new lines 
*/
function insertValue( field, startTag, endTag, intermTag, replicate ) {
	var selPos;
	var selectValue;
	
	if (!endTag) {endTag = "";}
	if (!intermTag) {intermTag = "";}
	
	if (document.selection) {
		field.focus();
		selPos = document.selection.createRange();
		var tmpSto = selPos.text;
		
		for (var newLinePos = tmpSto.length-1; replicate && newLinePos >= 0; ) {
			newLinePos = tmpSto.lastIndexOf('\n', (newLinePos > 0 ? newLinePos-1 : 0));
			
			if (newLinePos == -1) {break;}
			
			tmpSto = tmpSto.substring(0, newLinePos+1) + intermTag + tmpSto.substring(newLinePos+1);
		}
		
		selPos.text = startTag + intermTag + tmpSto + endTag;
	}
	else if ( field.selectionStart || field.selectionStart == "0" ) {
		selPos = field.selectionStart;
		var endPos = field.selectionEnd;
		selectValue = field.value.substring(selPos, endPos);
		
		for (var newLinePos = selectValue.length-1; replicate && newLinePos >= 0; ) {
			newLinePos = selectValue.lastIndexOf('\n', (newLinePos > 0 ? newLinePos-1 : 0));
			
			if (newLinePos == -1) {break;}
			
			selectValue = selectValue.substring(0, newLinePos+1) + intermTag + selectValue.substring(newLinePos+1);
		}
		
		field.value = field.value.substring(0, selPos) + startTag + intermTag + selectValue + endTag + field.value.substring(endPos);
	}
	else {
		field.value += startTag + intermTag + endTag;
	}
}

/* returns the selected text length in the given field */
function selectionSize( field ) {
	if (document.selection) {
		field.focus();
		selPos = document.selection.createRange();
		return selPos.text.length;
	}
	else if ( field.selectionStart || field.selectionStart == "0" ) {
		return (field.selectionEnd - field.selectionStart);
	}
	else {return 0;}
}
// *** // 




/*
*	routines needed to successfully display the new preview form
*	swapPreview() : no return
*/
function swapPreview(previewCheckbox, strPreviewArea, strWorkArea, objFeedback, strSigCheck, strSigValue) {
	if (strPreviewArea === "" || typeof strPreviewArea == 'undefined') {strPreviewArea = "msgBodyPreviewArea";}
	if (strWorkArea === "" || typeof strWorkArea == 'undefined') {strWorkArea = "msgBodyWorkArea";}
	if (objFeedback === "" || typeof objFeedback == 'undefined') {
		alert("Err: swapPreview :: undefined feedback field!");
	}
	if (strSigCheck === "" || typeof strSigCheck == 'undefined') {strSigCheck = "signature";}
	if (strSigValue === "" || typeof strSigValue == 'undefined') {strSigValue = "SIG";}
	
	if (previewCheckbox.checked) {
		document.getElementById(strPreviewArea).innerHTML='<i>Loading ... </i>'; 
		document.getElementById(strPreviewArea).style.display='block'; 
		document.getElementById(strWorkArea).style.display='none'; 
		loadXMLDoc('/updatePost.pl', strPreviewArea, 'POST', 
			'type=previewPost&message=' + escape(
				objFeedback.value 
				+ (
					(document.getElementById(strSigCheck) && document.getElementById(strSigCheck).checked)
					? ' [line] '+document.getElementById(strSigValue).value 
					: ''
				) 
			)
		);
	} else {
		document.getElementById(strPreviewArea).style.display='none';
		document.getElementById(strWorkArea).style.display='block';
	}
}
// *** // 



/*
*	routines needed to successfully display the preview screen 
*	previewIt() : no return
*/
function previewIt() {
	document.previewMsg.topicnum.value=document.forum.topicnum.value;
	document.previewMsg.submit.value=document.forum.submit.value;
	document.previewMsg.subject.value=document.forum.subject.value;
	document.previewMsg.name.value=document.forum.name.value;
	document.previewMsg.email.value=document.forum.email.value;
	document.previewMsg.website.value=document.forum.website.value;
	document.previewMsg.feedback.value=document.forum.feedback.value;
	document.previewMsg.SIG.value=document.forum.SIG.value;
	
	if (document.forum.signature.checked) {document.previewMsg.signature.value="true";}
	else {document.previewMsg.signature.value="false";}
	
	var config = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=525,height=250";
	var previewWindow = window.open("","previewWindow",config);
}
// *** // 



/*
*	check to make sure required fields are entered and (tentatively) acceptable before submitting post 
*	checkreq() : no return 
*/
function checkreq() {
	var website = document.forum.website.value;
	var feedback = "" + document.forum.feedback.value;
	if (document.forum.subject.value === "") {alert('Please Enter A Subject');return false;}
	if (document.forum.email.value.length > 0){
	if (document.forum.email.value.length < 6 || document.forum.email.value.indexOf('@') == -1 || document.forum.email.value.indexOf('.') == -1) {
	alert('If you are entering an E-mail address, please enter a valid one');return false;}}
	if (document.forum.feedback.value === "") {alert('The message body is required');return false;}
	if (document.forum.feedback.value.toLowerCase().indexOf('<font') >= 0)  {alert('You are not allowed to edit your font!');return false;}
	if (document.forum.feedback.value.toLowerCase().indexOf('<marquee') >= 0 || document.forum.feedback.value.indexOf('<div') >= 0)  {alert("'div' and 'marquee' tags are not allowed to be set!");return false;}
	//if (document.forum.name.value === "") {alert('Please Enter Your Name');return false;}
	if (document.forum.website.value.toLowerCase().indexOf('http://') == -1 && document.forum.website.value.length >= 7)  {document.forum.website.value="http://"+website;return true;}
}
// *** // 
