function jpt_open_window (theURL,winName,features,popWidth,popHeight,winAlign,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v2.0
	var leftPos=0,topPos=0,autoCloseTimeoutHandle, ontopIntervalHandle, w = 480, h = 340;	
	if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
	if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;
	if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
		if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth; 
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0; 						
		features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
	if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";
	if (window["popupWindow"] == null) window["popupWindow"] = new Array();
	var wp = popupWindow.length;
	popupWindow[wp] = window.open(theURL,winName,features);
	if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;	
	if (document.all || document.layers || document.getElementById) {
		if (borderless && borderless != "") {popupWindow[wp].resizeTo(popWidth,popHeight); popupWindow[wp].moveTo(leftPos, topPos);}
		if (alwaysOnTop && alwaysOnTop != "") {
			ontopIntervalHandle = popupWindow[wp].setInterval("window.focus();", 50);
			popupWindow[wp].document.body.onload = function() {window.setInterval("window.focus();", 50);}; }
		if (autoCloseTime && autoCloseTime > 0) {
			popupWindow[wp].document.body.onbeforeunload = function() {
				if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);
				window.onbeforeunload = null;	}	
	 		autoCloseTimeoutHandle = window.setTimeout("popupWindow["+wp+"].close()", autoCloseTime * 1000); }
		window.onbeforeunload = function() {for (var i=0;i<popupWindow.length;i++) popupWindow[i].close();}; }	 
	document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}


function jpt_form_callback ( returnstring ) {
	var split_array = returnstring.split ( "~~" );
	var Name    = split_array[0];
	var Content = split_array[split_array.length - 1];
	
	
	// When the split array has two elements we are dealing with a standard element
	// When the split array has three elements we are dealing with an iframe element
	
	if ( split_array.length == 2 ) {
		document.getElementById(Name).innerHTML = Content;
		if ( document.getElementById(Name)['user_input'] )
			if ( document.getElementById(Name)['user_input'].value != "" )
				if ( document.getElementById(Name)['user_input'].focus )
					document.getElementById(Name)['user_input'].focus();
	} else if ( split_array.length == 3 ) {
		document.getElementById(Name).innerHTML = split_array[1];
		doc = document.getElementById(Name+'_iframe').contentDocument;
		setTimeout("",500);
		doc.designMode = "on";
		doc.open("text/html");
		doc.write('<html><body id="editable" style="Bitstream Vera, Vera, Trebuchet MS, Trebuchet, Tahoma, sans-serif; font-size: 13px;">');
		doc.write(Content);
		doc.write('</body></html>');
		doc.close();
	}
	
	var type = document.getElementById(Name)['type'].value;
	
	if ( type == 'tally_marks' && document.getElementById(Name+'_tally') ) {
		current_tally = document.getElementById(Name)['user_input'].value * 1;
		jpt_input_tally_display ( Name, current_tally );
	} else if ( type == 'histogram_fixed' || type == 'histogram_user' ) {
		jpt_input_histogram_update ( Name );
	}
	
	document.getElementById('loading_ident').style.visibility = "hidden";
}

function jpt_input_submit ( Name, Type ) {
	document.getElementById('loading_ident').style.visibility = "visible";
	
	var form_vars = "";
	for (var loop=0; loop < document.getElementById(Name).elements.length; loop++) {
		if ( document.getElementById(Name).elements[loop].type == "radio" ) {
			if ( document.getElementById(Name).elements[loop].checked ) {
				form_vars = form_vars + document.getElementById(Name).elements[loop].name + "~~"
							  	  	  + document.getElementById(Name).elements[loop].value + "~~";
			}
		} else {
			form_vars = form_vars + document.getElementById(Name).elements[loop].name + "~~"
							  	  + document.getElementById(Name).elements[loop].value + "~~";
		}
	}
	
	if ( Type == 'text_highlight' ) {
		if ( document.getElementById(Name+'_iframe') ) {
			form_vars = form_vars + "user_input~~"
				+ document.getElementById(Name+'_iframe').contentWindow.document.body.innerHTML + "~~";
		}
	} 
	
	jsrsExecute( "rs_jpt_input.php", jpt_form_callback, "", form_vars );
}

function jpt_input_iframe_execCommand (Name, command, param1, param2) {
	document.getElementById(Name).contentDocument.execCommand(command, param1, param2);
}



function jpt_multi_display_callback ( returnstring ) {
	split_array = returnstring.split ( "~~" );
	document.getElementById(split_array[0]).innerHTML = split_array[1];
	document.getElementById('loading_ident').style.visibility = "hidden";
}

function jpt_multi_display_jsrs ( Name ) {
	document.getElementById('loading_ident').style.visibility = "visible";
	
	var form_vars = "";
	for (var loop=0; loop < document.getElementById(Name).elements.length; loop++) {
		form_vars = form_vars + document.getElementById(Name).elements[loop].name + "~~"
							  + document.getElementById(Name).elements[loop].value + "~~";
	}
	
	jsrsExecute( "rs_multi_display.php", jpt_multi_display_callback, "", form_vars );
}

function jpt_input_tally_update ( form_id ) {
	var tally_start = document.getElementById(form_id)['tally_start'].value * 1;
	var tally_end   = document.getElementById(form_id)['tally_end'].value * 1;
	var tally_step  = document.getElementById(form_id)['tally_step'].value * 1;
	
	j=0;
	tally_divs = '';
	for ( i=tally_start ; i<=tally_end ; i+=tally_step ) {
		i = ( Math.round(i*100) )/100;
		
		tally_divs += '<div id="form_id_'+j+'_tally" style="width:100%;">';
		tally_divs += '<table><tr>';
		tally_divs += '		<td width="50" valign="middle" align"right"><img src="media/images/b_minus.jpg" onCLick="jpt_input_tally_change(\''+form_id+'\', \''+form_id+'_'+j+'\', \'minus\');"/> <img src="media/images/b_plus.jpg" onCLick="jpt_input_tally_change(\''+form_id+'\', \''+form_id+'_'+j+'\', \'plus\');"/></td>';
		tally_divs += '		<td width="50" align="right" valign="middle">'+i+':</td>';
		tally_divs += '		<td width="350" align="left" style="height:16px;" >';
		tally_divs += '			<div id="'+form_id+'_'+j+'_tally" style="width:100%;"></div>';
		tally_divs += '			<input type="hidden" name="'+form_id+'_'+j+'_hidden" value="0" />';
		tally_divs += '		</td>';
		tally_divs += '</tr></table>';
		tally_divs += '</div>';
		
		
		j++;
	}
	
	document.getElementById(form_id+'_tallies').innerHTML = tally_divs;
}

function jpt_input_tally_change ( form_id, tally_id, change ) {
	current_tally = document.getElementById(form_id)[tally_id+'_hidden'].value * 1;
	
	if ( change == "plus" )
		current_tally++;
	else
		current_tally--;
	
	document.getElementById(form_id)[tally_id+'_hidden'].value = current_tally;
	jpt_input_tally_display ( tally_id, current_tally );
}

function jpt_input_tally_display ( form_id, current_tally ) {
	var image_op = "";
	
	if ( current_tally > 0 ) {
		mod = current_tally % 5;
		full_tallys = (current_tally - mod) / 5;
		
		for ( i=0 ; i<full_tallys ; i++ ) {
			image_op = image_op + '<img src="media/images/tally_5.gif"/> ';
		}
		
		if ( mod > 0 )
			image_op = image_op + '<img src="media/images/tally_'+mod+'.gif"/> ';
	}
	
	document.getElementById(form_id+'_tally').innerHTML = image_op;
}



// Histogram functions	
var pageY;
var g_y_step;
var g_y_max;
var g_y_min;

var outer_working_layer;
var working_layer;

function cursor_cap ( layer_id ) {
	working_layer = layer_id;
	document.captureEvents(Event.MOUSEMOVE);
	//document.captureEvents(Event.MOUSEDOWN);
	//document.onmousedown=cursor_position_output;
	document.onmousemove=cursor_position_output;
	
}

function cursor_rel () {
	document.onmousemove=0;
	document.releaseEvents(Event.MOUSEMOVE);
	//document.releaseEvents(Event.MOUSEDOWN);
	return nd();
}

function cursor_position_output (e) {
	pageY = e.pageY;
	var backlayer_y = findPosY ( document.getElementById(working_layer) );
	var cursor_backlayer_difference = pageY - backlayer_y;
	var backlayer_height = document.getElementById(working_layer).offsetHeight;
	var perfect_height = backlayer_height - cursor_backlayer_difference;
	
	down_scale_factor = g_y_max / backlayer_height;
	up_scale_factor   = backlayer_height / g_y_max;
	perfect_height_scaled = perfect_height * down_scale_factor;
	
	if ( g_y_step < 1 ) {
		for ( i=g_y_min ; i<=g_y_max*(1/g_y_step) ; i=i+g_y_step )
			if (   perfect_height_scaled >  (g_y_step*i)-(g_y_step/2) 
				&& perfect_height_scaled <= (g_y_step*i)+(g_y_step/2) )
				new_height_scaled = y_step * i;
	} else {
		for ( i=g_y_min ; i<=g_y_max ; i=i+g_y_step )
			if (   perfect_height_scaled >  (g_y_step*i)-(g_y_step/2) 
				&& perfect_height_scaled <= (g_y_step*i)+(g_y_step/2) )
				new_height_scaled = g_y_step * i;
	}
	new_height = new_height_scaled * up_scale_factor;
	
	document.getElementById(working_layer+'_filler').style.height = new_height+"px";
	document.getElementById(outer_working_layer)[working_layer+'_hidden'].value = new_height;
	overlib(new_height_scaled, WIDTH, 20, MOUSEOFF);
}


function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
            curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
    	curtop += obj.y;
	return curtop;
}

function jpt_input_histogram_update ( layer_id ) {
	y_min    = document.getElementById(layer_id).y_min.value * 1;
	y_max    = document.getElementById(layer_id).y_max.value * 1;
	y_step   = document.getElementById(layer_id).y_step.value * 1;
	y_label  = document.getElementById(layer_id).y_label.value;
	x_min    = document.getElementById(layer_id).x_min.value * 1;
	x_max    = document.getElementById(layer_id).x_max.value * 1;
	x_step   = document.getElementById(layer_id).x_step.value * 1;
	x_label  = document.getElementById(layer_id).x_label.value;
	editable = document.getElementById(layer_id).editable.value * 1;
	center   = document.getElementById(layer_id).x_label_center.value * 1;
	
	g_y_step = y_step;
	g_y_max  = y_max;
	g_y_min  = y_min;
	outer_working_layer = layer_id;
	working_layer       = layer_id+'_histogram';

	histogram_draw ( layer_id, layer_id+'_histogram', editable, y_min, y_max, y_step, y_label, x_min, x_max, x_step, x_label, center );
}

function histogram_draw ( outer_layer_id, layer_id, editable, y_min, y_max, y_step, y_label, x_min, x_max, x_step, x_label, x_label_pos ) {
	number_bins = ((x_max - x_min)/x_step)+1;
	bin_width = Math.floor(450 / number_bins);
	
	y_steps = ((y_max - y_min)/y_step);
	y_col_height = Math.floor(200 / y_steps);
	
	chart_width  = number_bins * bin_width;
	chart_height = y_col_height * y_steps;
	
	j=0;
	bin_divs = "";
	x_axis = '';
	// Label are in the center of columns
	if ( x_label_pos == 0 ) {
		for ( i=x_min ; i<=x_max ; i+=x_step ) {
			i = ( Math.round(i*100) )/100;
			if ( editable == 1 )
				tmp = '<div id="'+layer_id+'_bin_'+j+'" class="histo_back" style="width:'+bin_width+'px; left:'+(bin_width*j)+'px; height:'+chart_height+'px;" onMouseDown="cursor_cap(\''+layer_id+'_bin_'+j+'\');" onMouseUp="cursor_rel();">';
			else
				tmp = '<div id="'+layer_id+'_bin_'+j+'" class="histo_back" style="width:'+bin_width+'px; left:'+(bin_width*j)+'px; height:'+chart_height+'px;">';
			
			if ( document.getElementById(outer_layer_id)[outer_layer_id+'_histogram_bin_'+j+'_hidden'] )
				tmp = tmp + '<div id="'+layer_id+'_bin_'+j+'_filler" class="histo_filler" style="width:'+bin_width+'px; height:'+(document.getElementById(outer_layer_id)[outer_layer_id+'_histogram_bin_'+j+'_hidden'].value)+'px;"></div>';
			else
				tmp = tmp + '<div id="'+layer_id+'_bin_'+j+'_filler" class="histo_filler" style="width:'+bin_width+'px;"></div>';
			
			tmp = tmp + '<div class="histo_x_number" style="width:'+bin_width+'px;">'+i+'</div>';
			
			tmp = tmp + '</div>';
			bin_divs = bin_divs + tmp;
			j++;
		}	
	// Labels are at either side of the columns
	} else {
		for ( i=x_min ; i<x_max ; i+=x_step ) {
			i = ( Math.round(i*100) )/100;
			if ( editable == 1 )
				tmp = '<div id="'+layer_id+'_bin_'+j+'" class="histo_back" style="width:'+bin_width+'px; left:'+((bin_width*j)+(bin_width/2))+'px; height:'+chart_height+'px;" onMouseDown="cursor_cap(\''+layer_id+'_bin_'+j+'\');" onMouseUp="cursor_rel();">';
			else
				tmp = '<div id="'+layer_id+'_bin_'+j+'" class="histo_back" style="width:'+bin_width+'px; left:'+((bin_width*j)+(bin_width/2))+'px; height:'+chart_height+'px;">';
			
			if ( document.getElementById(outer_layer_id)[outer_layer_id+'_histogram_bin_'+j+'_hidden'] )
				tmp = tmp + '<div id="'+layer_id+'_bin_'+j+'_filler" class="histo_filler" style="width:'+bin_width+'px; height:'+(document.getElementById(outer_layer_id)[outer_layer_id+'_histogram_bin_'+j+'_hidden'].value)+'px;"></div>';
			else
				tmp = tmp + '<div id="'+layer_id+'_bin_'+j+'_filler" class="histo_filler" style="width:'+bin_width+'px;"></div>';
			
			tmp = tmp + '</div>';
			bin_divs = bin_divs + tmp;
			j++;
		}
		
		j=0;
		for ( i=x_min ; i<=x_max ; i+=x_step ) {
			i = ( Math.round(i*100) )/100;
			x_axis = x_axis + '<div class="histo_x_number_off" style="left:'+((bin_width*j)-13+(bin_width/2))+'px;">'+i+'</div>';
			j++;
		}
		x_axis = x_axis + '<div class="histo_vert_line" style="left:'+(bin_width/2)+'px; height:'+chart_height+'px;"></div>';
	}
	
	
	bin_divs = bin_divs + '<div class="histo_y_0_line" style="height:'+(chart_height+10)+'px;"></div>';
	
	if ( y_steps < 10 )
		y_skipper = 1;
	else if ( y_steps < 20 )
		y_skipper = 2;
	else if ( y_step < 30 )
		y_skipper = 3;
	else
		y_skipper = 1;
	
	j=0;
	y_axis = '';
	y_skipper = 2;
	for ( i=y_max ; i>y_min ; i=i-(y_skipper*y_step) ) {
		y_axis = y_axis + '<div class="histo_horiz_line" style="top:'+((y_col_height*j)+(200-chart_height))+'px; height:'+y_col_height+'px; width:'+chart_width+'px;">';
		y_axis = y_axis + '<div class="histo_y_number">'+i+'</div>';
		y_axis = y_axis + '</div>';
		j+=(y_skipper*y_step);
	}
	
	y_axis = y_axis + '<div class="histo_x_0_line" style="top:200px; height:'+y_col_height+'px; width:'+(chart_width+10)+'px;"></div>';
	y_axis = y_axis + '<div class="histo_y_0_number">'+y_min+'</div>';
	
	x_label = '<div class="histo_x_label" style="left:'+(chart_width+10)+'px;">'+x_label+'</div>';
	y_label = '<div class="histo_y_label" style="bottom:'+(chart_height+7)+'px;">'+y_label+'</div>';
	
	chart_border = '<div class="histo_border" style="width:'+chart_width+'px; height:'+(chart_height-2)+'px;"></div>';
	
	document.getElementById(layer_id).innerHTML = chart_border + y_axis + x_axis + bin_divs + x_label + y_label;
}
