var clientPC = navigator.userAgent.toLowerCase();
var is_gecko = ((clientPC.indexOf('gecko') != -1) && (clientPC.indexOf('spoofer') == -1) && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0') == -1));
var is_safari = ((clientPC.indexOf('AppleWebKit') != -1) && (clientPC.indexOf('spoofer') == -1));
var alreadySubmitted = 0;

function insertTags(tagOpen,tagClose,f){
	var sampleText = '';
	if(f == null){
		var textarea = document.post.message;
	}else{
		var textarea = f;
	}
	if(document.selection && !is_gecko){
		var theSelection = document.selection.createRange().text;
		var replaced = true;
		if(!theSelection){
			replaced = false;
			theSelection = sampleText;
		}
		textarea.focus();
		text = theSelection;
		if(theSelection.charAt(theSelection.length - 1) == ' '){
			theSelection = theSelection.substring(0, theSelection.length - 1);
			r = document.selection.createRange();
			r.text = tagOpen + theSelection + tagClose + ' ';
		}else{
			r = document.selection.createRange();
			r.text = tagOpen + theSelection + tagClose;
		}
		if(!replaced){
			r.moveStart('character', -text.length-tagClose.length);
			r.moveEnd('character', -tagClose.length);
		}
		r.select();
	}else if(textarea.selectionStart || textarea.selectionStart == '0'){
		var replaced = false;
		var startPos = textarea.selectionStart;
		var endPos = textarea.selectionEnd;
		if(endPos - startPos){
			replaced = true;
		}
		var scrollTop = textarea.scrollTop;
		var myText = (textarea.value).substring(startPos, endPos);
		if(!myText){
			myText = sampleText;
		}
		if(myText.charAt(myText.length - 1) == ' '){
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + ' ';
		}else{
			subst = tagOpen + myText + tagClose;
		}
		textarea.value = textarea.value.substring(0, startPos) + subst + textarea.value.substring(endPos, textarea.value.length);
		textarea.focus();
		if(replaced){
			var cPos = startPos + (tagOpen.length + myText.length + tagClose.length);
			textarea.selectionStart = cPos;
			textarea.selectionEnd = cPos;
		}else{
			textarea.selectionStart = startPos + tagOpen.length;
			textarea.selectionEnd = startPos + tagOpen.length + myText.length;
			textarea.scrollTop;
		}
	}else{
		var copy_alertText = alertText;
		var re1 = new RegExp("\\$1", "g");
		var re2 = new RegExp("\\$2", "g");
		copy_alertText = copy_alertText.replace(re1, sampleText);
		copy_alertText = copy_alertText.replace(re2, tagOpen + sampleText + tagClose);
		var text;
		if(sampleText){
			text = prompt(copy_alertText);
		}else{
			text = '';
		}
		if(!text){
			text = sampleText;
		}
		text = tagOpen + text + tagClose;
		textarea.value += "\\r\\n" + text;
		if(!is_safari){
			textarea.focus();
		}
	}
	if(textarea.createTextRange){
		textarea.caretPos = document.selection.createRange().duplicate();
	}
}

function checkForm( a ){
	if(document.getElementById('title')){
		if(a.title.value.length < 2){
			alert('You must enter a title.');
			return false;
		}
	}
	if(a.message.value.length < 2){
		alert('You must enter a message.');
		return false;
	}
	a.Submit.disabled = true;
	return true;
}

function saluteCheck(){
	if(!document.uploadSalute.nonPlainWhite.checked){
		alert('Paper salutes are required to be dark colored or lined. Checkbox must be checked regardless if you are not saluting with paper.');
		return false;
	}
}

function setAllChecked(sR){
	var form = document.forms[0];
	var formLen = form.elements.length;
	for(var i=0; i<formLen; i++){
		if(form.elements[i].name != 'checkall' && form.elements[i].type == 'checkbox'){
			form.elements[i].checked = form.checkall.checked;
			if(sR){
				selectRow(form.elements[i]);
			}
		}
	}
}

function stopSubmit(){
	if(alreadySubmitted == 1){
		return false;
	}
	alreadySubmitted = 1;
}

function disableEnterKey(){
	if(window.event.keyCode == 13){
		window.event.keyCode = 0;
	}
}

function GetElement(id){
	if(document.getElementById){
		return document.getElementById(id);
	}else if(document.all){
		return document.all[id];
	}else if(document.layers){
		return document.layers[id];
	}
	return false;
}

function drawColorBar(d,w,h){
	var cb=new Array();
	var cp=new Array('00','33','66','99','CC','FF');
	cb.push('<table cellpadding="0" cellspacing="1">');
	for(var r=0; r<=5; r++){
		if(d == 'h') cb.push('<tr>');
		for(var g=0; g<=5; g++){
			if(d == 'v') cb.push('<tr>');
			for(var b=0; b<=5; b++){
				cc=cp[r]+cp[g]+cp[b];
				cb.push('<td bgcolor="#'+cc+'"><a href="#" onClick="sc(\'#'+cc+'\');return false;"><img src="/images/spacer.gif" width="'+w+'" height="'+h+'" border="0"></a></td>');
			}
			if(d == 'v') cb.push('</tr>');
		}
		if(d == 'h') cb.push('</tr>');
	}
	cb.push('</table>');
	var o=cb.join('');
	return o;
}

function showSmilies(){
	smileyWin = window.open('', '', 'scrollbars=yes,status=no,width=420,height=450');
	smileyWin.location.href = '/showsmilies.php';
}

function enlargePhoto(a,w,h){
	var lP = 640;
	var tP = 480;
	if(!w || !h){
		var w = 400;
		var h = 400;
	}
	if(screen){
		lP = (screen.width - w) / 2;
		tP = (screen.height - h) / 2;
	}
	photoWin = window.open('','','scrollbars=0,status=0,left='+lP+',top='+tP+',width='+w+',height='+h);
	if(photoWin){
		photoWin.document.open();
		photoWin.document.write('<html><title>Image</title><body bgcolor="#000000" style="margin: 0px;padding: 0px;"><img src="'+a+'"></body></html>');
		photoWin.document.close();
	}else{
		alert('Your popup blocker may be preventing you from using this feature.');
	}
}

function Popup(width, height, url)
{
	var lP = 640;
	var tP = 480;
	if(screen){
		lP = (screen.width - width) / 2;
		tP = (screen.height - height) / 2;
	}
	PopupWin = window.open('','','scrollbars=0,left='+lP+',top='+tP+',width='+width+',height='+height);
	if(PopupWin){
		if(url.length == url.lastIndexOf('.jpg')){
			PopupWin.document.open();
			PopupWin.document.write('<html><title>Image</title><body bgcolor="#000000" style="margin: 0px;padding: 0px;"><img src="'+url+'"></body></html>');
			PopupWin.document.close();
		}else{
			PopupWin.document.open();
			PopupWin.document.write('<html><title>Please wait...</title><body bgcolor="#333366" style="margin: 0;padding: 5px;font-family: Arial, Helvetica, sans-serif;font-size: 14px;font-weight: bold;color: #FFFFFF;">Please wait...</body></html>');
			PopupWin.document.close();
			PopupWin.location.href = url;
		}
	}else{
		alert('Your popup blocker may be preventing you from using this feature.');
	}
}
		

function doAction( a,b,c ){
	var nw = 0;
	switch(a){
		case 'AB':w=400;h=100;t='/add-block.php?id='+b;break;
		case 'ALE':w=400;h=100;t='add.list.entry.php?user_id='+b;break;
		case 'CR':w=450;h=125;t='add.comment.reply.php?comment_id='+b;break;
		case 'DC':w=300;h=100;t='delete.comment.php?comment_id='+b;break;
		case 'ER':w=400;h=100;t='edit.reply.php?comment_id='+b;break;
		case 'EC':w=400;h=100;t='edit.comment.php?comment_id='+b;break;
		case 'AC':w=450;h=100;t='add.comment.php?user='+b;break;
		case 'SB':w=500;h=230;t='/mail/send.box.php?user='+b;break;
		case 'DAC':w=300;h=100;t='/delete.comments.php';break;
		case 'P':w=350;h=360;t='/preferences.php';break;
		default: return;
	}
	callAWindow(w+':'+h+':'+t,nw);
}

function callAWindow( op,nw ){
	if(!nw) var nw = 0;
	var ops = op.split(':');
	var lP = 0;var tP = 0;
	if(screen){ lP = (screen.width-ops[0])/2;tP = (screen.height-ops[1])/2;
	}else{ lP = 640;tP = 480;	}
	openDatHoe = window.open(ops[2],'','scrollbars='+nw+',status=0,left='+lP+',top='+tP+',width='+ops[0]+',height='+ops[1]);
	if(!openDatHoe){ alert('Your popup blocker may be preventing you from using this feature.'); }
}

function sendRequest(strMethod, strUrl, strData, fncCallBack)
{
	var request;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		request = new ActiveXObject('Microsoft.XMLHTTP');
	}else{
		return false;
	}
	
	if(request){
		request.onreadystatechange = function()
		{
			if(request.readyState == 4){
				if(request.status == 200){
					if(fncCallBack){
						fncCallBack(request.responseText);
					}else{
						return false;
					}
				}
			}
		}
		request.open(strMethod, strUrl, true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send(strData);
	}
}