function checkAllSelect(postForm, postTarget) {
	var optionVar = postForm.elements[postTarget].options;
	var toReturn = "";
	
	if (optionVar.length > 0) {
		for (i=0; i<optionVar.length; i++) {
			toReturn += optionVar[i].value + "##";
			optionVar[i].selected = false;
		}
		
		optionVar[0].selected = true;
		optionVar[0].value = toReturn;
		
		return true;
	}
}

function confirmFileDelete(fileID) {
	if (confirm("Delete this file?")) {
		document.location = "remove_code.php?id=" + fileID;
	}
}

function copyToClipboard(textString) {
	window.clipboardData.setData('Text', textString);
	window.clipboardData.getData('Text');
}

function createMarker(point, html) {
	var marker = new GMarker(point);
	
	GEvent.addListener(marker, "click", function () {
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}

function createXMLHttpRequest() {
	var XMLRequest = false;
	
	// create object for IE7, Mozilla, Safari, etc. (native object)
	if (window.XMLHttpRequest) {
		XMLRequest = new XMLHttpRequest();
	}
	// create object for IE6, IE5, etc. (ActiveX object)
	else if (window.ActiveXObject) {
		var success = false;
		var progIDs = new Array(
			"Msxml2.XMLHTTP.6.0",
			"Msxml2.XMLHTTP.3.0",
			"Msxml2.XMLHTTP",
			"Microsoft.XMLHTTP"
		);
		
		for (i=0; i<progIDs.length && !success; i++) {
			try {
				XMLRequest = new ActiveXObject(progIDs[i]);
				success = true;
			}
			catch (failure) {
				
			}
		}
	}
	
	return XMLRequest;
}

function getDocumentHeight(documentName) {
	var heightVar = 0, sh, oh;
	
	if (documentName.height) {
		heightVar = documentName.height;
	}
	else if (documentName.body) {
		if (documentName.body.scrollHeight) {
			heightVar = sh = documentName.body.scrollHeight;
		}
		
		if (documentName.body.offsetHeight) {
			heightVar = oh = documentName.body.offsetHeight;
		}
		
		if (sh && oh) {
			heightVar = Math.max(sh, oh);
		}
	}
	
	return heightVar;
}

function getFrameHeight(frameName) {
	var frameWin = window.frames[frameName];
	var frameElement = document.getElementById ? document.getElementById(frameName) : document.all ? document.all[frameName] : null;
	
	if (frameWin && frameElement) {
		frameElement.style.height = "auto";
		var documentHeight = getDocumentHeight(frameWin.document);
		
		if (documentHeight) {
			frameElement.style.height = documentHeight + 20 + "px";
		}
	}
}

function jumpSelect(postField) {
	eval("document.location='" + postField.options[postField.selectedIndex].value + "'");
}

function moveOption(postDir, postForm, postTarget) {
	var optionVar = document.forms[postForm].elements[postTarget];
	
	if (optionVar.selectedIndex != -1 || optionVar.length == 0) {
		if ((postDir == "-1" && optionVar.selectedIndex > 0) || (postDir == "+1" && optionVar.selectedIndex < optionVar.length - 1)) {
			var optionSelected = optionVar.selectedIndex;
			var optionValue = optionVar[optionSelected + postDir].value;
			var optionText = optionVar[optionSelected + postDir].text;
			
			optionVar[optionSelected + postDir].value = optionVar[optionSelected].value;
			optionVar[optionSelected + postDir].text = optionVar[optionSelected].text;
			optionVar[optionSelected].value = optionValue;
			optionVar[optionSelected].text = optionText;
			optionVar.selectedIndex += postDir;
		}
	}
}

var populate = false;

function populateUsername(postForm, postTarget, postValue) {
	populate = !populate;
	
	if (populate) {
		postForm.elements[postTarget].value = postValue;
	}
	else {
		postForm.elements[postTarget].value = "";
	}
}

function popupWindow(popupURL, popupWidth, popupHeight) {
	var windowWidth = popupWidth;
	var windowHeight = popupHeight;
	
	var leftPosition = Math.ceil((screen.width - windowWidth) / 2);
	var topPosition = Math.ceil((screen.height - windowHeight) / 2);
	
	var tmpWindow = window.open(popupURL, "popup_window", "width=" + windowWidth + ", height=" + windowHeight + ", left=" + leftPosition + ", top=" + topPosition + ", channelmode=0, directories=0, fullscreen=0, location=0, menubar=0, resizable=1, scrollbars=1, status=0, toolbar=0");
	tmpWindow.focus();
}

function resizeImageOptions(postForm, postField) {
	var x = postField.value;
	var filter = /(.jpe|.jpeg|.jpg|.gif|.png)$/i;
	
	if (x) {
		if (filter.test(x)) {
			document.getElementById('upload_image_parameters').style.display = "none";
			document.getElementById('resize_image_options').style.display = "block";
			postForm.elements['resize'].focus();
			postForm.elements['resize'].selectedIndex = "0";
			resizeImageParameters(postForm, postForm.elements['resize']);
		}
		else {
			document.getElementById('upload_image_parameters').style.display = "block";
			document.getElementById('resize_image_options').style.display = "none";
		}
	}
}

function resizeImageParameters(postForm, postField) {
	var x = postField.selectedIndex;
	
	if (x == "0") {
		postForm.elements['resize_width'].value = "400";
		postForm.elements['resize_height'].value = "300";
		postForm.elements['resize_width'].disabled = false;
		postForm.elements['resize_height'].disabled = false;
	}
	else if (x == "1") {
		postForm.elements['resize_width'].value = "320";
		postForm.elements['resize_height'].value = "240";
		postForm.elements['resize_width'].disabled = false;
		postForm.elements['resize_height'].disabled = false;
	}
	else if (x == "2") {
		postForm.elements['resize_width'].value = "";
		postForm.elements['resize_height'].value = "";
		postForm.elements['resize_width'].disabled = true;
		postForm.elements['resize_height'].disabled = true;
	}
}

function setDateSelect(postForm, postMonth, postMonthValue, postDay, postDayValue, postYear, postYearValue) {
	var x = postForm.elements[postMonth];
	
	for (i=0; i<x.length; i++) {
		if (x.options[i].value == postMonthValue) {
			x.options[i].selected = true;
			break;
		}
	}
	
	var x = postForm.elements[postDay];
	
	for (i=0; i<x.length; i++) {
		if (x.options[i].value == postDayValue) {
			x.options[i].selected = true;
			break;
		}
	}
	
	var x = postForm.elements[postYear];
	
	for (i=0; i<x.length; i++) {
		if (x.options[i].value == postYearValue) {
			x.options[i].selected = true;
			break;
		}
	}
}

function setFieldFocus() {
	if (document.forms.length>0) {
		var field = document.forms[0];
		
		for (i=0; i<field.length; i++) {
			if (field.elements[i].type == "select-one" && field.elements[i].disabled == false) {
				field.elements[i].focus();
				break;
			}
			else if ((field.elements[i].type == "text" || field.elements[i].type == "textarea") && (field.elements[i].disabled == false && field.elements[i].readOnly == false)) {
				field.elements[i].focus();
				field.elements[i].select();
				break;
			}
		}
	}
}

function setFrameHeight(frameName) {
	if (parent == window) {
		return;
	}
	else {
		parent.getFrameHeight(frameName);
	}
}

function showFiles(menuId, menuCount) {
	for (i=1;i<=menuCount;i++) {
		document.getElementById("files" + i).style.display = "none";
		document.getElementById("tab" + i).style.backgroundColor = "#FFFFFF";
	}
	
	document.getElementById("files" + menuId).style.display = "block";
	document.getElementById("tab" + menuId).style.backgroundColor = "#E7E7E7";
}

function showMenu(menuId) {
	var menuVar = document.getElementById(menuId);
	
	if (menuVar != null) {
		if (menuVar.style.display == "none") {
			menuVar.style.display = "block";
		}
		else {
			menuVar.style.display = "none";
		}
	}
}
