var ID  = (!document.all && document.getElementById);
var ALL = (document.all);

function show_panel(id) {
show_hide('dimensions');
show_hide('pressure');
show_hide('materials');

set_class('panel_tab1');
set_class('panel_tab2');
set_class('panel_tab3');
set_class('panel_tab4');

if (id == 'pages') {
	show_hide('dimensions', true);
	show_hide('pressure', true);
	show_hide('materials', true);
} else show_hide(id, true);

if (id == 'dimensions') 		set_class('panel_tab1', 'selected');
else if (id == 'pressure')		set_class('panel_tab2', 'selected');
else if (id == 'materials')		set_class('panel_tab3', 'selected');
else if (id == 'pages')		set_class('panel_tab4', 'selected');

}

//function to update div contents
function update_contents(item,content) {
	if (ID) {
		if (document.getElementById(item).innerHTML != content) document.getElementById(item).innerHTML = content;
	} else if (ALL) {
		if (document.all[item].innerHTML != content) document.all[item].innerHTML = content;
	}
}

function show_hide(id,mode) {
if (id) {
if (mode)  value = 'block';
if (!mode) value = 'none';
if (ID) {
	document.getElementById(id).style.display = value;
	return;
}
if (ALL) {
	document.all[id].style.display = value;
	return;
}
}
}

function set_class(id,thisclass) {
if (id) {
if (thisclass)  value = thisclass;
if (!thisclass) value = '';
if (ID) {
	document.getElementById(id).className = value;
	return;
}
if (ALL) {
	document.all[id].className = value;
	return;
}
}
}

//function to add content to a div
function add_content(item,content) {
	if (ID) {
		// save textares variables
		var textareas = document.getElementById(item).getElementsByTagName("textarea"); 
		var savedvalues = new Array();
		for (var i = 0; i < textareas.length; i ++) {
			savedvalues[i] = textareas[i].value;
		}
		//collect div info
		newcontent = document.getElementById(item).innerHTML.concat(content);
	} else if (ALL) {
		//collect div info
		newcontent = document.all[item].innerHTML.concat(content);
	}
	//update with new content
	update_contents(item, newcontent);
	
	if (ID) {
		// restore values
		for (var i = 0; i < textareas.length; i ++) {
			if (savedvalues[i]) textareas[i].value = savedvalues[i];
		}
	}
}

//function to open windows
function open_window(target,win_name,win_width,win_height) {
if (window.name == win_name) {
	window.location = target;
	return;
}
if (win_width > screen.width)   win_width = screen.width;
if (win_height > screen.height) win_height = screen.height;
var offset_left = (screen.width - win_width) / 2;
var offset_top  = (screen.height - win_height) / 2;
if (win_height > 100) {
win_height      = win_height - 100;
}
var win_cfg     = "status=1,scrollbars=1,resizable=1,width=" + win_width + ",height=" + win_height + ",left=" + offset_left + ",top=" + offset_top;
openwin         = window.open(target,win_name,win_cfg);
openwin.opener  = self; 
openwin.focus(win_name);
}

//function to confirm delete request
function confirmDelete(type) {
	if (!type) type = "item";
	return confirm("WARNING:" + '\n' + "All information related to this " + type + " will be removed from the system." + '\n' + "Are you sure you wish to continue?");
}

//function to make a url friendly string from input field
function make_url(inputField, outputField) {
var str;
if (ID) {
	str = document.getElementById(inputField).value;
} else if (ALL) {
	str = document.all[inputField].value;
}
str = str.toLowerCase();
str = str.replace(new RegExp("[!\"#£$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\\\\]", "g"), "");
str = str.replace(/ /g, "_");
if (ID) {
	document.getElementById(outputField).value = str;
} else if (ALL) {
	document.all[outputField].value = str;
}
}