//alert('enter arrayExtension.js');

//---- array prototypes --------------------------------------------------------
Array.prototype.insertLeg = function(l){
	this.push(l);
	this.sort(function(a,b){return(a.name<b.name?-1:1);});
}
Array.prototype.findLegIndex = function(leg){
	var id = ( getObjectClass (leg) == '_trip' ? leg.id : leg );
	for ( var i = 0; i < this.length; ++i )
		if ( this[i].id == id )
			return i;
	return -1;
}
Array.prototype.findLeg = function(id){
	var index = this.findLegIndex(id);
	if ( index == -1 ) return false;
	return this[index];
}
Array.prototype.removeLeg = function(leg){
	var index = this.findLegIndex(leg);
	if ( index >= 0 )
		this.splice(index,1);
}

// load array from server

Array.prototype.loadFromServer = function( url, param ) {
	this.onSuccess = 0;
	this.loader = new ajaxLoader();
	this.loader.loadArray( this, url, param );
}

Array.prototype.onLoad = function( callback ) {
	this.onSuccess = callback;
	if ( this.length )
		this.onSuccess(this);
}

Array.prototype.newData = function(data,meta) {
	for ( var i = 0; i < data.length; ++i ) 
		this.push ( data[i] );
	this.meta = meta;
	if ( this.onSuccess )
		this.onSuccess(this);
}

// make a select element

Array.prototype.selectContainer = false;
Array.prototype.onChangeCall = function(){};
Array.prototype.selected = 0;
Array.prototype.selectedID = -1;

Array.prototype.makeSelector = function( onChangeCall, selectedID, container ) {
	this.onChangeCall = ( onChangeCall || this.onChangeCall );
	this.selectedID = ( selectedID || this.selectedID );
	this.selectContainer = ( container || this.selectContainer );
	this.selected = -1
	html  = '<select class="Select" ' + 
			'onchange="' + this.onChangeCall + '(this.selectedIndex)" >';
	for ( var i = 0; i < this.length; ++i ) {
		var selected = ( this.selectedID == this[i].id ? 'selected="selected"' : '' );
		if ( selected.length > 0 ) {
			this.selected = i;
//			alert(this.selected + onChangeCall);
		}
		html += '<option value="' + this[i].id + '" ' + selected + '>'
				+ this[i].name + '</option>';
	}
	html += "</select>";
	if ( this.selectContainer ) {
		delete _$(this.selectContainer).innerHTML;
		_$(this.selectContainer).innerHTML = html;
	}
	else
		return html;
}

Array.prototype.deleteData = function() {
	if ( this.loader ) {
		this.loader.destruct();
		delete this.loader;
	}
	while ( this.length )
		delete this.pop();
	if ( this.selectContainer ) {
		delete _$(this.selectContainer).innerHTML;
		_$(this.selectContainer).innerHTML = "";
	}
}
//alert('exit arrayExtension.js');
