var pop_window = null;
			
function winpop(page, name, x, y) {
	var this_x = (screen.width) ? (screen.width - x) / 2  : 100;
	var this_y = (screen.height) ? (screen.height - y) / 2  : 100;
	var options = "width=" + x + ",height=" + y + ",top=" + this_y + ",left=" + this_x + "," + "scrollbars=yes" + ",location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	pop_window = window.open(page, name, options);
	pop_window.focus();
}


function addcode(addcode) {
	field = document.parent.content;
	
	code = addcode;
	if (document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = code;
	} else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos)
		+ code
		+ field.value.substring(endPos, field.value.length);
	} else {
		field.value += code;
	}
}


function codeformat(value, type) {
	form = document.parent;
	
	addcode('[' + type + '=' + value + '][/' + type + ']');
	
	form.formatsize.selectedIndex = 0;
	form.formatfont.selectedIndex = 0;
	form.formatcolour.selectedIndex = 0;
}


function codeformatsingle(value, type) {
	form = document.parent;
	
	addcode('[' + type + '=' + value + ']');
	
	form.formatsize.selectedIndex = 0;
	form.formatfont.selectedIndex = 0;
	form.formatcolour.selectedIndex = 0;
}


function validate_length(element, valid_length) {
	
	our_element = document.getElementById(element);
	our_length = our_element.value.length;
	
	if (our_length < valid_length) {
		our_element.style.borderColor = '#f00';
	} else {
		our_element.style.borderColor = '#000';
	}
}


function validate(type) {
	
	if (type == 1) {
		if (document.parent.title.value == "") {
			alert("You must enter a title.");
			document.parent.title.focus();
			return false;
		}
	}
	if (document.parent.content.value == "") {
		alert("You must enter some content.");
		document.parent.content.focus();
		return false;
	}
	return true;
}


function ask_delete(url, message) {
	switch (message) {
		case 1:
			the_message = 'Are you sure?';
			break;
		case 2:
			the_message = 'Do you really want to delete this message?';
			break;
		case 3:
			the_message = 'Are you sure? This will delete all posts in this topic, too.';
			break;
	}
	
	if (confirm(the_message)) {
		window.location.href = url;
	}
}

/**
 * Select box validation for menu management
**/
function checkPageLink(ele) {
	
	if (!ele.selectedIndex) {
		var set = false;
	} else {
		var set = true;
	}
	
	link_custom.disabled = set;
	
	if (link_external)
		link_external.disabled = set;
}


/**
 * General Functions
 */
function func() {
	
}

func.prototype.formatImage = function(imageName) {
	
	return "<img src='" + imageName + "' alt='" + imageName + "' />";
}

var func = new func();

/**
 * Modifies a package's current special state
 */
function specialSwitch() {
	
	this.tick 		= base_uri + '/gfx/misc/tickbox.png';
	this.cross 		= base_uri + '/gfx/misc/crossbox.png';
	this.current	= [];
	this.total		= 0; 
}

specialSwitch.prototype.switcher = function(package_id, value) {
	
	if (!this.current[package_id]) {
		this.current[package_id] = value;
	} else {
		this.current[package_id] = value ? 0 : 1;
		value = this.current[package_id];
	}
	
	if (value && this.total == 3) {
		alert('You can only select 3 specials.');
		this.current[package_id] = 0;
		return;
	}
	
	var url = base_uri + '/requests.php';
	var params = 'type=special_switch&id=' + package_id + '&stat=' + value;
	var callAjax = new Ajax.Request(url, {method: 'get', parameters: params} );
	
	if (value) {
		this.total++;
		$('specialSwitch_' + package_id).innerHTML = func.formatImage(this.tick);
	} else {
		this.total--;
		$('specialSwitch_' + package_id).innerHTML = func.formatImage(this.cross);
	}
}
	
	




