/**
 * Package: Spam Board 5
 * File: includes/js/js.js
 * Description: JavaScript functions used on various pages
 *
 * Copyright (C) 2007, 2009 Hannes Schueller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program (see LICENCE). If not,
 * see <http://www.gnu.org/licenses/>.
 **/

var _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

function encode64(input) {
	var output = '';
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;
	// handle UTF 8 encoding
	string = input.replace(/\r\n/g, '\n');
	var utftext = '';
	for (var j = 0; j < string.length; j++) {
		var c = string.charCodeAt(j);
		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	input = utftext;
	// actual base64 part
	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
		output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
	}
	return output;
}

function preview(sid) {
	if (typeof(document.getElementsByName('user')[0]) != 'undefined') {
		var user = document.getElementsByName('user')[0].value;
	} else { var user = ''; }
	var post = document.getElementsByName('post')[0].value;
	window.open('redirectors/preview.php?user=' + encode64(user) + '&post=' + encode64(post) + '&s=' + sid, 'previewwindow', 'width=600, height=450, scrollbars=yes');
}

function addSmiley(smiley) {
	// the textarea
	var obj = document.getElementsByName('postform')[0].post;
	if (typeof(obj.selectionStart) != 'undefined' && typeof(obj.setSelectionRange) != 'undefined') {
		// Gecko / Opera
		var start = obj.selectionStart;
		var end	  = obj.selectionEnd;
		obj.value = obj.value.substr(0, start) +  smiley + obj.value.substr(end, obj.value.length);
		obj.focus();
		var pos = start + smiley.length;
		obj.setSelectionRange(pos, pos);
	} else if (typeof(document.selection) != 'undefined') {
		// Internet Exploder (*choke*)
		obj.focus();
		document.selection.createRange().text = smiley;
	} else {
		// other browsers
		obj.value += smiley;
		obj.focus();
	}
}

function addCode(code) {
	// the textarea
	var obj = document.getElementsByName('postform')[0].post;
	if (typeof(obj.selectionStart) != 'undefined' && typeof(obj.setSelectionRange) != 'undefined') {
		// Gecko / Opera
		var start = obj.selectionStart;
		var end	  = obj.selectionEnd;
		obj.value = obj.value.substr(0, start) +  '[' + code + ']' + obj.value.substring(start, end) + '[/' + code + ']' + obj.value.substr(end, obj.value.length);
		obj.focus();
		var pos = start + code.length + 2;
		obj.setSelectionRange(pos, pos);
	} else if (typeof(document.selection) != 'undefined') {
		// Internet Exploder (*choke*)
		obj.focus();
		var str = document.selection.createRange().text;
		alert(str);
		document.selection.createRange().text = '[' + code + ']' + str + '[/' + code + ']';
	} else {
		// other browsers
		obj.value += '[' + code + '][/' + code + ']';
		obj.focus();
	}
}

function showAvatar(val) {
	document.images.avatarpic.src = val[val.selectedIndex].value;
}

function quote(id) {
	// the textarea
	var obj = document.getElementsByName('postform')[0].post;
	if (typeof(obj.selectionStart) != 'undefined' && typeof(obj.setSelectionRange) != 'undefined') {
		// Gecko / Opera
		var start = obj.selectionStart;
		var end	  = obj.selectionEnd;
		obj.value = obj.value.substr(0, start) + '[quote=' + id + ']' + post_quote[id] + '[/quote]' + obj.value.substr(end, obj.value.length);
		obj.focus();
		var pos = start + post_quote[id].length;
		obj.setSelectionRange(pos, pos);
	} else if (typeof(document.selection) != 'undefined') {
		// Internet Exploder (*choke*)
		obj.focus();
		document.selection.createRange().text = '[quote=' + id + ']' + post_quote[id] + '[/quote]';
	} else {
		// other browsers
		obj.value += '[quote=' + id + ']' + post_quote[id] + '[/quote]';
		obj.focus();
	}
}

function checkAll() {
	var mode;
	var checkbox = document.getElementById('mastercheckbox');
	if (checkbox.checked == true) {
		mode = 'check';
	} else {
		mode = 'uncheck';
	}
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++) {
		input = inputs[i];
		if (input.getAttribute('type', false).toUpperCase() == 'CHECKBOX' && input.getAttribute('id', false).toUpperCase() != 'MASTERCHECKBOX') {
			if (mode == 'check') {
				input.checked = true;
			} else {
				input.checked = false;
			}
		}
	}
}

