//JS Functions for Many2Many relation via 2 listboxes
//


// constructor, must be called before using of M2MListboxes object
// Sample: var m2mtest = new M2MListboxes('ctr', 1, 'dst'); 
// this samle will produce the following popup url: M2M_popup.asp?ctr=1&dst=...
// Params:
//	ParentCtrlName - parent control name
//	ParentName - parent name
//	ChildName - child name
function M2MListboxes(ParentCtrlName, ParentName, ChildName) {
	this.ParentCtrlName = ParentCtrlName;
	this.ParentName = ParentName;
	this.ChildName = ChildName;
}

// method that binds HTML controls to M2MListboxes object, must be called before any event handler call but AFTER HTML controls definition
// Sample: m2mtest.bindControls(document.forms['test'].src, document.forms['test'].addAll, document.forms['test'].add, document.forms['test'].del, document.forms['test'].delAll, document.forms['test'].dst);
// Params:
//	src - source HTML select
//	addAll - HTML button that moves all records from source to destination HTML select
//	add - HTML button that moves all selected records from source to destination HTML select 
//	del - HTML button that moves all selected records from destination to source HTML select
//	delAll - HTML button that moves all records from destination to source HTML select
//	dst - destination HTML select
M2MListboxes.prototype.bindControls = function(src, addAll, add, del, delAll, dst) {
	this.src = src;
	this.addAll = addAll;
	this.add = add;
	this.del = del;
	this.delAll = delAll;
	this.dst = dst;

	this.updateButtonStates();
}

// event handler for form.onSubmit event
// Sample: <form name="test" onSubmit="return m2mtest.submit();">
M2MListboxes.prototype.submit = function() {
	var url = "popupSaveM2M.asp?" + this.ParentCtrlName +"=" + this.ParentName;
	for(var i = 0; i < this.dst.length; i++) {
		url += "&" + this.ChildName + "=" + this.dst.options[i].value
	}

        // VG 2005.09.05 : Changed Popup to iFrame
	//popup('saver', url);
	window['saver'].location = url;

	return false;
}

// method that updates button states, being called each time src/dst lists change, besides must be called from onChange handlers of source and destination HTML selects
// Sample: 
//	<select name="src" size="6" multiple onChange="m2mtest.updateButtonStates()">
//	<select name="dst" size="6" multiple onChange="m2mtest.updateButtonStates()">
M2MListboxes.prototype.updateButtonStates = function() {
	this.addAll.disabled = (this.src.options.length == 0);

	var addDisabled = true;
	for(var i = 0; i < this.src.length; i++) {
		if(this.src.options[i].selected) {
			addDisabled = false;
			break;
		}
	}
	this.add.disabled = addDisabled; 

	var delDisabled = true;
	for(var i = 0; i < this.dst.length; i++) {
		if(this.dst.options[i].selected) {
			delDisabled = false;
			break;
		}
	}
	this.del.disabled = delDisabled;

	this.delAll.disabled = (this.dst.options.length == 0);
}

// event handler for addAll button click
// Sample: <input type="button" name="addAll" value="All&gt;" onClick="m2mtest.addAllClick()">
M2MListboxes.prototype.addAllClick = function() {
	for(var i = 0; i < this.src.length; i++) {
		this.dst.options[this.dst.length] = new Option(this.src.options[i].text, this.src.options[i].value);
	}

	for(var i = this.src.length - 1; i >= 0 ; i--) {
		this.src.options[i] = null;
	}

	this.updateButtonStates();
}

// event handler for add button click
// Sample: <input type="button" name="add" value="&gt;&gt;" onClick="m2mtest.addClick()">
// this method can be called from src.onDblClick that allows to move selected item by double click
// Sample: <select name="src" size="6" multiple onChange="m2mtest.updateButtonStates()" onDblClick="m2mtest.addClick()">
M2MListboxes.prototype.addClick = function() {
	for(var i = 0; i < this.src.length; i++) {
		if(this.src.options[i].selected) this.dst.options[this.dst.length] = new Option(this.src.options[i].text, this.src.options[i].value);
	}

	for(var i = this.src.length - 1; i >= 0 ; i--) {
		if(this.src.options[i].selected) this.src.options[i] = null;
	}

	this.updateButtonStates();
}

// event handler for del button click
// Sample: <input type="button" name="del" value="&lt;&lt;" onClick="m2mtest.delClick()">
// this method can be called from dst.onDblClick that allows to move selected item by double click
// Sample: <select name="dst" size="6" multiple onChange="m2mtest.updateButtonStates()" onDblClick="m2mtest.delClick()">
M2MListboxes.prototype.delClick = function() {
	for(var i = 0; i < this.dst.length; i++) {
		if(this.dst.options[i].selected) this.src.options[this.src.length] = new Option(this.dst.options[i].text, this.dst.options[i].value);
	}

	for(var i = this.dst.length - 1; i >= 0 ; i--) {
		if(this.dst.options[i].selected) this.dst.options[i] = null;
	}

	this.updateButtonStates();
}

// event handler for delAll button click
// Sample: <input type="button" name="delAll" value="&lt;All" onClick="m2mtest.delAllClick()">
M2MListboxes.prototype.delAllClick = function() {
	for(var i = 0; i < this.dst.length; i++) {
		this.src.options[this.src.length] = new Option(this.dst.options[i].text, this.dst.options[i].value);
	}

	for(var i = this.dst.length - 1; i >= 0 ; i--) {
		this.dst.options[i] = null;
	}

	this.updateButtonStates();
}

// method that adds new option to the end of source HTML select
// Sample: m2mtest.addSrcOption('California', 'CA');
// Params:
//	text - label for ne option
//	value - value for new option
M2MListboxes.prototype.addSrcOption = function(text, value) {
	this.src.options[this.src.length] = new Option(text, value);

	this.updateButtonStates();
}

// method that removes an option from specified position of source HTML select
// Sample: m2mtest.delSrcOption(3);
// Params:
//	index - position of option to remove
M2MListboxes.prototype.delSrcOption = function(index) {
	this.src.options[index] = null;

	this.updateButtonStates();
}

// method that removes all options from source HTML select
// Sample: m2mtest.delAllSrcOptions();
M2MListboxes.prototype.delAllSrcOptions = function() {
	for(var i = this.src.length - 1; i >= 0 ; i--) {
		this.src.options[i] = null;
	}

	this.updateButtonStates();
}

// method that adds new option to the end of destination HTML select
// Sample: m2mtest.addDstOption('California', 'CA');
// Params:
//	text - label for ne option
//	value - value for new option
M2MListboxes.prototype.addDstOption = function(text, value) {
	this.dst.options[this.dst.length] = new Option(text, value);

	this.updateButtonStates();
}

// method that removes an option from specified position of destination HTML select
// Sample: m2mtest.delDstOption(3);
// Params:
//	index - position of option to remove
M2MListboxes.prototype.delDstOption = function(index) {
	this.dst.options[index] = null;

	this.updateButtonStates();
}

// method that removes all options from destination HTML select
// Sample: m2mtest.delAllDstOptions();
M2MListboxes.prototype.delAllDstOptions = function() {
	for(var i = this.dst.length - 1; i >= 0 ; i--) {
		this.dst.options[i] = null;
	}

	this.updateButtonStates();
}


function popup(name, file) {
   	width = Math.min(100, screen.width-40);
	height = Math.min(100, screen.height-60);

	wleft = (screen.width - width) / 2;
	wtop  = (screen.height - height) / 2 - 20;

	win = window.open(file, name, "width=" + width + ",height=" + height + ",left=" + wleft + ",top=" + wtop + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
	//win = window.open(file, name, "");
}
  
