function open_window(theURL,winName,features) {
	window.open(theURL,winName,features);
}

function change_picture(element_id) {
	popupidx = document.getElementById("selected_template").selectedIndex;
	popupary = document.getElementById("selected_template").options;
	document.getElementById("template_img").src="templates/"+popupary[popupidx].value;
}

function open_template_window() {
	open_window('templates.php','winTemplates','width=800,height=600,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}

function size_down() {
	current = document.getElementById("fontsize").value;
	current--;
	document.getElementById("fontsize").value = current;
}
function size_up() {
	current = document.getElementById("fontsize").value;
	current++;
	document.getElementById("fontsize").value = current;
}

function changeText(){
	document.getElementById("boldStuff").innerHTML = "Peter Gunn";
}

function change_signmaker_window(preview_image,id) {
	if (window.opener && !window.opener.closed) window.opener.opener_pull_tabs(preview_image,id);
	self.close();
}

function opener_pull_tabs(preview_image,id) {
	document.getElementById("template_img").src = "templates_empty/" + preview_image;

	var req = new DataRequestor();
	
	//req.setObjToReplace('insert_here');
	
	req.onload = function(data,obj) {
		document.getElementById("insert_here").innerHTML = data;
		tabber(1);
	}
	
	req.addArg(_GET,"id",id);
	req.getURL('getoptions.php');
	
	// make buttons pressable
	document.getElementById("preview_button").src = "images/preview.gif";
	document.getElementById("preview_action").href = "javascript:update_preview()";

	document.getElementById("download_button").src = "images/downloadpdf.gif";
	document.getElementById("download_action").href = "javascript:submit_form('pdf')";
}

function show_all(obj_name) {
	var out;

	var obj = document.getElementById(obj_name);

	for (propertyName in obj)
		out = out + propertyName + "\n";
		
	return out;	
}

function update_preview() {
	document.getElementById("preview_button").src = "images/preview_dim.gif";
	document.getElementById("generating_preview").src = "images/ajax-loader.gif";
	document.getElementById("generating_preview_text").innerHTML = 'Generating Preview';
	
	var req = new DataRequestor();
	
	//req.setObjToReplace('div2');
	
	req.onload = function(data,obj) {
		var img_src = data.rootElement.childElements[0].text;
		document.getElementById("template_img").src = img_src;
		document.getElementById("preview_button").src = "images/preview.gif";
		document.getElementById("generating_preview").src = "images/ajax-loader_dim.gif";
		document.getElementById("generating_preview_text").innerHTML = "";
		//document.getElementById("div2").innerHTML = htmlspecialchars(data.rootElement.childElements[0].text);
	}
	
	req.addArg(_POST,"id",get_form_value("template_id"));

	for (var i = 1;i < 32;i++)
		req.addArg(_POST,"text" + i,get_form_value("text" + i));
	
	req.getURL('makepreview.php',_RETURN_AS_REXML);
	//req.getURL('makepreview.php');
}

function get_form_value(id) {
	if (document.getElementById(id)) return document.getElementById(id).value;
	return " ";
}

function submit_form(thevalue) {
	document.formname.pushed_button.value = thevalue;
	document.formname.submit();
}

function tabber(num) {
	clear_tabs();
	clear_content();

	document.getElementById("tab" + num).className = "selected";
	document.getElementById("tabcontent" + num).className = "content_visible";
}

function clear_tabs() {
	for (var idx = 1; idx < 6; idx++) {
		var obj_name = "tab" + idx;
		if (document.getElementById(obj_name) != null) {
			document.getElementById(obj_name).className = "not_selected";
		}
	}
}

function clear_content() {
	for (var idx = 1; idx < 6; idx++) {
		var obj_name = "tabcontent" + idx;
		if (document.getElementById(obj_name) != null) {
			document.getElementById(obj_name).className = "content_invisible";
		}
	}
}

function htmlspecialchars(string) {
	string = string.toString();
	
	string = string.replace('/&/g', '&amp;');
	string = string.replace('/</g', '&lt;');
	string = string.replace('/>/g', '&gt;');
	string = string.replace('/"/g', '&quot;');
	
	return string;
}
