//Listing Component
//GLOBALS
var row_selected=false;
var resize_start_x=false;
var resize_column=false;
var resize_started=false;
var old_width=0;
var listing_prefix="";
var old_widths=new Array();
var num_cols=0;
var header_widths=new Array();
var component_selected=false;

function listing_locate(e) {
	locate(e);

	//window.status = "("+x_loc+","+y_loc+")";
	if (resize_started) do_resize();
}
//document.onmousemove=listing_locate;
//document.onmouseup=end_resize;
add_event_handler("document", "onmousemove", "listing_locate")
add_event_handler("document", "onmouseup", "end_resize")

function header_pressed(header) {
	header.old_class_name=header.className;
	header.className="listing-header-pressed";
}

function header_depressed(header) {
	depress_header(header);
}

function depress_header(header) {
	if (header.old_class_name) header.className=header.old_class_name;
}

function start_resize(col_num) {
	//if only one column 
	if (!document.all[listing_prefix+"header_1"]) return;
	resize_started=true;
	resize_start_x=x_loc;
	resize_column=col_num;
	old_width=document.all[listing_prefix+"header_"+resize_column].style.pixelWidth;
}

function do_resize() {
	//if only one column 
	if (!document.all[listing_prefix+"header_1"]) return;
	
	new_width=old_width+x_loc-resize_start_x;
	
	if (resize_column==old_widths.length) mod_index=resize_column-1;
	else mod_index=resize_column+1;
	mod_header=document.all[listing_prefix+"header_"+mod_index];
	this_header=document.all[listing_prefix+"header_"+resize_column];
	
	difference = new_width-this_header.style.pixelWidth;
	other_width = mod_header.style.pixelWidth - difference;
	
	if (new_width>7 && other_width>7) {
		all_widths=get_all_widths(resize_column, new_width);
	}
}

var initial_width=0;
function init_columns(prefix) {
	//if only one column
	scope_it(prefix);
	if (!document.all[listing_prefix+"header_1"]) return;
	all_widths=init_widths();
}

function scope_it(prefix) {
	listing_prefix=prefix;
	header_widths=eval(listing_prefix+"_header_widths");
}

function init_widths() {
	var the_widths=new Array();
	
	total_width=document.all[listing_prefix+"listing_region"].clientWidth;
			
	num_cols=0;
	for(var a=0; header=document.all[listing_prefix+"header_"+a]; a++) num_cols++;
	
	total_width=total_width - 3 * (num_cols-1);//subtract out the dividers
	//each_width=total_width/num_cols;
	
	for(var a = 0; a < num_cols; a++) {
		header=document.all[listing_prefix+"header_"+a];
		
		if (a != num_cols-1) divider=document.all[listing_prefix+"divider_"+a];
						 
		the_widths[a]=header.style.pixelWidth;
		if (a != 0) the_widths[a]=the_widths[a]+3;
	}
	
	//everything is fine
	old_widths=the_widths;
	return the_widths;
}

var item_for_tool_tip=false;
var timer=false;
var tool_tip_open=false;

function start_tool_tip(action, item, prefix) {
	timer=setTimeout("tool_tip()",1000);
	item_for_tool_tip=item;
}

function end_tool_tip() {
	if (timer) clearTimeout(timer);
	if (tool_tip_open) destroy_rcm();
	item_for_tool_tip=false;
	tool_tip_open=false;
}


function tool_tip() {
	if (item_for_tool_tip && item_for_tool_tip.scrollWidth > item_for_tool_tip.clientWidth+3)	{
		tool_tip_open=true;
		the_text=item_for_tool_tip.innerHTML;
		display_rcm("<table cellpadding=0 cellspacing=0 border=0 width=100 height=50 bgcolor=fffff0><tr><td valign=top>"+the_text+"&nbsp;&nbsp;</td></tr></table>",0,0);
	}
}

function get_all_widths(col_num, col_width) {
	var the_widths=old_widths;
	var difference=0;
	//define things
	if (col_num==the_widths.length) mod_index=col_num-1;
	else mod_index=col_num+1;
	mod_header=document.all[listing_prefix+"header_"+mod_index];
	this_header=document.all[listing_prefix+"header_"+col_num];
	
	//set array stuff
	the_widths[col_num]=col_width;
		
	//fix problems
	difference=col_width-this_header.style.pixelWidth;
	new_width = mod_header.style.pixelWidth - difference;
		
	//perform actions
	this_header.style.width=the_widths[col_num];
	mod_header.style.pixelWidth=new_width;
	the_widths[mod_index]=new_width;
	
	if (col_num != 0) the_widths[col_num]=the_widths[col_num]+3;
	if (mod_index != 0) the_widths[mod_index]=the_widths[mod_index]+3;
	
	//everything is fine
	old_widths=the_widths;
	return the_widths;
}

function set_all_widths(the_widths) {
	for(var a=0; a<the_widths.length; a++) {
		set_row_widths(a,the_widths[a]);
	}
	return false;
}

function set_row_widths(col_num,the_width,left) {
	for(var a=0; row=document.all[listing_prefix+"row_"+a]; a++) {
		the_object=document.all[listing_prefix+"row_"+a+"_col_"+col_num];
		the_object.style.pixelWidth=the_width;
	}
	return false;
}

function end_resize() {
	if (resize_started) {
		set_all_widths(old_widths);
		resize_started=false;
	}
}

function select_row(the_row, destroy_the_rcm) {
	if (row_selected) deselect_row(row_selected);
	row_selected=the_row;
	component_selected=listing_prefix;
	the_row.old_class=the_row.className;
	the_row.className="listing-row-selected";
	
	if (destroy_the_rcm) destroy_rcm();
}

function deselect_row(the_row) {
	component_selected=false;
	if (the_row && the_row.old_class) the_row.className=the_row.old_class;
}

function sort_by(col_num) {
	eval(listing_prefix+"sort_by("+col_num+")");
}

//To get netscape to sort by headers, we must do this crap
function capture_me(the_element,the_index) {
	the_element.index_num=the_index;
	the_element.captureEvents(Event.MOUSEUP);
	the_element.onmouseup=call_sort_by;
}
				
function call_sort_by() {
	sort_by(this.index_num);
}

function right_click_menu(row)  {
	if (row.RCM_length==0) return;
	end_tool_tip();
	var new_html="<table cellpadding=0 cellspacing=0 border=0 class=right-click-menu>";
	
	for (i=0; i<row.RCM_length; i++ ) {
		row_properties=eval("row.RCM_prop_"+i).split("|");
		onmouseover=" onMouseOver=\"this.className='right-click-menu-row-on'\" ";
		onmouseout=" onMouseOut=\"this.className='right-click-menu-row'\" ";
		//will neen an extra case to handle javascript links
		var default_style="";
		if (row.double_click==row_properties[1]) {
			default_style = " style=\"font-weight: bold;\"";
		}
		if (-1 != row_properties[1].indexOf("javascript:")) onclick=" onClick=\""+get_javascript_link(row_properties[1],row.extended_menu_parameters)+"\"";
		else {
			row_properties[1] += ((!row_properties[1].match(/\?/)) ? "?" : "&");
			onclick=" onClick=\"document.all['"+listing_prefix+"right_click_menu'].style.visibility='hidden'; document.location.href='"+row_properties[1]+row.extended_menu_parameters+"';\"";
		}
		var row_name = row_properties[0].replace(/ /g, "&nbsp;");
		new_html+="<tr class=right-click-menu-row "+onmouseover+onclick+onmouseout+"><td"+default_style+">"+row_name+"</td></tr>";
	}
	new_html+="</table>";
	
	display_rcm(new_html,0,0);
}

function display_rcm(new_html,offset_x,offset_y) {
	document.all[listing_prefix+"right_click_menu"].style.left=x_loc + offset_x;
	document.all[listing_prefix+"right_click_menu"].style.top=y_loc + offset_y;

	x_err = (x_loc+offset_x) - document.all[listing_prefix+"right_click_menu"].getBoundingClientRect().left;
	document.all[listing_prefix+"right_click_menu"].style.pixelLeft += x_err
	
	y_err = (y_loc+offset_y) - document.all[listing_prefix+"right_click_menu"].getBoundingClientRect().top;
	document.all[listing_prefix+"right_click_menu"].style.pixelTop += y_err
	
	document.all[listing_prefix+"right_click_menu"].innerHTML=new_html;
	document.all[listing_prefix+"right_click_menu"].style.visibility="visible";
}

function destroy_rcm() {
	document.all[listing_prefix+"right_click_menu"].style.visibility="hidden";
}

function double_clk_action(the_row) {
	if (!the_row.double_click || the_row.double_click.length<2) return;
	if (-1 != the_row.double_click.indexOf("javascript:")) eval(get_javascript_link(the_row.double_click,the_row.extended_menu_parameters));
	else {
		the_row.double_click += ((!the_row.double_click.match(/\?/)) ? "?" : "&");
		document.location.href = the_row.double_click + the_row.extended_menu_parameters;
	}
}

function IE_menu_button(the_link, button_name, form_name, multiplicity_flag, row_name) {
	// begin multiplicity code
	var hidden = eval("document."+form_name+"."+listing_prefix+"MultiplicityCheckboxSelectedRows");
	var multiplicity_args = ""
	
	if (hidden && hidden.value) {
		// do a safe split...
		var selected_rows = ((hidden.value.indexOf(";") != -1) ? hidden.value.split(";") : new Array(hidden.value));
		
		multiplicity_args = "multiplicity_count="+selected_rows.length;
		for (var i=0; i<selected_rows.length; i++) {
			var cb = eval("document."+form_name+"."+selected_rows[i]);
			multiplicity_args += "&multiplicity_value" + i + "=" + escape(cb.value);
		}
		if (!multiplicity_flag && (selected_rows.length > 1)) {
			alert("You have selected multiple rows and have chosen an action which does not support multiple rows.\nPlease only select one row for this action.");
			return;
		}
	}

	if (multiplicity_flag && hidden && hidden.value) {
		//alert("Welcome to Multiplicity! (selected_rows = "+selected_rows.length+")");
		if (selected_rows.length > 1) {
			// determine if this action is enabled on every row selected
			var enabled_findings = new Array();
			for (var i=0; i<selected_rows.length; i++) {
				//var my_row_num = selected_rows[i].charAt(selected_rows[i].length-1); // the last character
				var temp_parts = selected_rows[i].split("Checkbox");
				var my_row_num = temp_parts[1];
				var my_row = document.getElementById(listing_prefix+"row_"+my_row_num);

				var enabled = false;
				for (var j=0; j<my_row.RCM_length; j++) {
					if (eval("my_row.RCM_prop_"+j).substring(0, button_name.length) == button_name) {
						enabled = true;
						break;
					}
				}
				enabled_findings[enabled_findings.length] = enabled;
			}
			
			var enabled = true;
			for (var i=0; i<enabled_findings.length; i++) {
				if (!enabled_findings[i]) {
					enabled = false;
					break;
				}
			}

			if (enabled) {
				var my_form = eval("document."+form_name);
				
				if (-1 != the_link.indexOf("javascript:")) {
					js_code=the_link;
					eval(js_code);
				}
				else {
					the_link += ((the_link.match(/\?/)) ? "&" : "?");
					my_form.action = the_link + my_row.extended_menu_parameters;
					my_form.submit();
				}
			}
			else {
				alert("This action may not be performed on these entries.\nNot all selected rows support this action.");
			}
		}
		else {
			if (selected_rows[0] == "") {
				alert("Please select a "+row_name+" first");
				return false;
			}
			else {
				var my_row_num = selected_rows[0].charAt(selected_rows[0].length-1); // the last character
				var my_row = document.getElementById(listing_prefix+"row_"+my_row_num);

				var enabled = false;
				for (var i=0; i<my_row.RCM_length; i++) {
					if (eval("my_row.RCM_prop_"+i).substring(0, button_name.length) == button_name) {
						enabled = true;
						break;
					}
				}
				
				if (enabled) {
					if (-1 != the_link.indexOf("javascript:")) {
						js_code=the_link;
						eval(js_code);
					}
					else {
						the_link += ((the_link.match(/\?/)) ? "&" : "?");
						var my_form = eval("document."+form_name);
						my_form.action = the_link + my_row.extended_menu_parameters;
						my_form.submit();
					}
				}
				else {
					alert("This action may not be performed on this entry.");
				}
			}
		}
	} // end multiplicity code
	else {
		if (!row_selected || component_selected != listing_prefix) {
			alert("Please select a "+row_name+" first");
			return false;
		}
		var enabled=false;
		for (var i=0; i < row_selected.RCM_length; i++) {
			if (eval("row_selected.RCM_prop_"+i).substring(0, button_name.length) == button_name) {
				enabled=true;
				break;
			}
		}
		if (enabled && (-1 != the_link.indexOf("javascript:"))) {
			js_code=get_javascript_link(the_link,row_selected.extended_menu_parameters);
			eval(js_code);
		}
		else if (enabled){
			the_link += ((!the_link.match(/\?/)) ? "?" : "&");
			document.location.href = the_link+row_selected.extended_menu_parameters;
		}
		else {
			alert("This action may not be performed on this entry.");
		}
	}
}

function others_menu_button(the_link) {
	
}

function get_javascript_link(the_link,the_parameters) {
	//find the function call 
	j_code=the_link.substr(11+the_link.indexOf("javascript:"));
		
	//break up the parameters and pass them to the function
	all_parameters=the_parameters.split("&");
	for (var i=0; i<all_parameters.length; i++) {
		if (-1 == all_parameters[i].indexOf("=")) continue;
		func_parameters=all_parameters[i].split("=");
		j_code=ereg_replace(func_parameters[0],"'"+url_decode(func_parameters[1])+"'",j_code,true);
	}
	return j_code;
}

// begin multiplicity javascript
function listing_component_multiplicity_checkall(flag, prefix, numRows) {
	for (var i=0; i<numRows; i++) {
		var cb = eval("document."+prefix+i);
		if (cb) { 
			cb.checked = flag;
			listing_component_multiplicity_setHidden(flag, prefix, cb.name);
		}
	}
}



function listing_component_multiplicity_setHidden(flag, prefix, value) {
	var hidden_str = "document."+prefix+"SelectedRows";
	var hidden = eval(hidden_str);
	
	if (flag) { //add
		var seperator = ((hidden.value.length == 0) ? "" : ";");
		hidden.value += seperator + value;
	}
	else { // remove
		var new_rows = seperator = "";
		var selected_rows = hidden.value.split(";");
		for (var i=0; i<selected_rows.length; i++) {
			if (selected_rows[i] != value) {
				new_rows += seperator + selected_rows[i];
				seperator = ";";
			}
		}
		
		hidden.value = new_rows;
	}
}
// end multiplicity javascript

