﻿GNgui.CrossRefrence = Class.create();
GNgui.CrossRefrence.prototype = {
	initialize: function (From, To) {
		this.valid = (From != null && To != null && From.tagName == 'SELECT' && To.tagName == 'SELECT')
		if (this.valid) {
			this.From = From;
			this.To = To;
			
			From.To = To;
			From.isFocus = true;
			From.onblur = function () {this.isFocus = false;}
			From.onfocus = function () {this.isFocus = true;}
			From.onscroll = function () {if (this.isFocus == true) this.To.scrollTop = this.scrollTop;}

			To.From = From;
			To.isFocus = false;
			To.onblur = function () {this.isFocus = false;}
			To.onfocus = function () {this.isFocus = true;}
			To.onscroll = function () {if (this.isFocus == true) this.From.scrollTop = this.scrollTop;}

			this.To.size = this.From.size;
		}
	},

	addItem: function (From, To, FromId, ToId) {
		if (this.valid) {
			if (FromId == null || isNaN(FromId)) FromId = 0;
			if (ToId == null || isNaN(ToId)) ToId = 0;
			FromId = parseInt(FromId);
			ToId = parseInt(ToId);

			this.From.options[this.From.options.length] = new Option(From, FromId);
			this.To.options[this.To.options.length] = new Option(To, ToId);
		}		
	},

	associate: function (callFunc, onError) {
		if (this.valid) {
			var pFrom = this.From.selectedIndex;
			var pTo = this.To.selectedIndex;

			if (pFrom > -1 && pTo > -1 && pFrom != pTo) {
				if (this.From.options[pFrom].value != 0 && this.To.options[pTo].value != 0) {
					var selected = this.To.options[pTo];
					if (this.To.options[pFrom].value != 0) {
						if (this.From.options[pTo].value != 0) {
							this.addItem ('', this.To.options[pFrom].text, 0, this.To.options[pFrom].value);
							this.To.options[pFrom] = new Option(selected.text, selected.value)
							this.To.options[pTo] = new Option('', 0)
						}
						else {
							this.addItem ('', this.To.options[pFrom].text, 0, this.To.options[pFrom].value);
							this.To.options[pFrom] = new Option (selected.text, selected.value)
							this.From.options.remove(pTo);
							this.To.options.remove(pTo);
						}
					}
					else {
						if (this.From.options[pTo].value != 0) {
							this.To.options[pTo] = new Option('', 0);
							this.To.options[pFrom] = new Option(selected.text, selected.value)
						}
						else {
							this.To.options[pFrom] = new Option (selected.text, selected.value)
							this.From.options.remove(pTo);
							this.To.options.remove(pTo);
						}
					}

					this.To.selectedIndex = this.From.selectedIndex;
					if (typeof callFunc == 'function') {
						callFunc();
					}
				}
			}
			else {
				if (onError != null) {
					alert(onError);
				}
			}
		}
	},

	dissociate: function (callFunc, onError) {
		if (this.valid) {
			var pFrom = this.From.selectedIndex;
			var pTo = this.To.selectedIndex;

			if (pTo > -1 && this.From.options[pTo].value > 0 && this.To.options[pTo].value > 0) {
				this.addItem ('', this.To.options[pTo].text, 0, this.To.options[pTo].value);
				this.To.options[pTo] = new Option('', 0);
				this.To.selectedIndex = this.To.options.length;

				if (typeof callFunc == 'function') {
					callFunc();
				}
			}
			else {
				if (onError != null) {
					alert(onError);
				}
			}
		}
	},

	getRelations: function () {
		var len = this.From.options.length;
		var out = '';

		for (var i = 0; i < len; i++) {
			if (this.From.options[i].value != 0 &&  this.To.options[i].value != 0) {
				out +=  this.From.options[i].value + ',' + this.To.options[i].value + ',';
			}
		}
		if (out.length >0) out = out.substr(0, out.length - 1);
		return out;
	}
}
