
Bluora.ExtendedUpdate = function (el,detail) {
	if (Bluora.typeOf(detail) == 'undefined') {
		detail = {};
	} else {
		if (el.id == '') {
			Bluora.log('Bluora.ExtendedUpdate == Element has no unique id');
		}
	}
	
	if (typeof detail.func != 'undefined' && el.id != '') {
		el.timeOutFunction = detail.func;
		window.setTimeout("$('"+el.id+"').timeOutFunction()", detail.timeout)
	}
	if (typeof detail.timeout != 'undefined' && el.id != '') {
		window.setTimeout("$('"+el.id+"').update('"+detail.update+"')", detail.timeout)
	}
};


Bluora.DIVupdate = function (el, text, detail) {
	el.innerHTML = text;
	Bluora.ExtendedUpdate(el,detail);
};

Bluora.LIupdate = function (el, text, detail) {
	el.innerHTML = text;
	Bluora.ExtendedUpdate(el,detail);
};
	
Bluora.INPUTupdate = function(el, text, detail) {
	if (typeof text == 'undefined') {
		text = '';
	}	
	el.value = text;
	if (typeof Bluora.INPUTdeactiviate != 'undefined') {
		Bluora.INPUTdeactiviate(el);
	}
	Bluora.ExtendedUpdate(el,detail);
};
	
Bluora.SELECTupdate= function(el, options_arrays) {
	el.options.length = 0; 
	if (options_arrays) {
		if (options_arrays.length > 0) { 
			for (var i = 0; i < options_arrays.length; i++) { 
				if (Bluora.typeOf(options_arrays[i]) == 'object') {
					el.options[i] = options_arrays[i];
				} else {
					el.options[i] = new Option(options_arrays[i][0], options_arrays[i][1], options_arrays[i][2], options_arrays[i][3]);
					if (options_arrays[i][2]) {
						el.options[i].selected = true;
					}
					if (options_arrays[i][4]) {
						el.options[i].setAttribute('style', options_arrays[i][4]);
					}
					if (options_arrays[i][5]) {
						el.options[i].setAttribute('class', options_arrays[i][5]);
					}
				}
			}
			if (Bluora.SELECTupdateRecordMovers) {
				Bluora.SELECTupdateRecordMovers(el);
			}
		}
	}
};
	
Bluora.TBODYupdate = function(el,rows) {
	while ($(el).rows.length> 0) {
		 $(el).deleteRow(0);
	}

	
	if (Bluora.typeOf(rows) != 'undefined' && rows != null) {
		if (Bluora.typeOf(rows) == 'array') {
			var html = '';
			rows.each(
				function (row) {
					html += '<tr'+Bluora.EvaluateAttributesArray(row[0])+'>';	
					row[1].each(
						function (col) {
							html += '<td'+Bluora.EvaluateAttributesArray(col[0])+'>'+col[1]+'</td>';
						}
					);
					html += '</tr>';
				}
			);
			new Insertion.Top(el, html);
		} else {
			Bluora.TABLEupdate(el, rows);
		}
	}
};

Bluora.TRupdate = function(el,columns) {
	Bluora.TRmakeEmpty(el);
	for (var c=0;c<columns.length;c++) {
		$(el).insertCell(c);
		$(el).cells[c].update(columns[c]);
	}
}

Bluora.TABLEmakeEmpty = function(el) {
	while ($(el).rows.length> 0) {
		 $(el).deleteRow(0);
	}
}

Bluora.TRmakeEmpty = function(el) {
	while ($(el).cells.length> 0) {
		 $(el).deleteCell(0);
	}
}

Bluora.TABLEupdate = function (el,html) {
	Bluora.TABLEmakeEmpty(el);
	var div = document.createElement('div');
 	div.innerHTML = '<table><tbody>' + html + '</tbody></table>';
	fragments = div.childNodes[0].childNodes;
	for(var i=0;i<fragments.length;i++){
		 $(el).appendChild(fragments[i]);
	}
}
	

Bluora.TDupdate = function (el, text, detail) {
	el.innerHTML = '';
	Element.insert(el,text);
	Bluora.ExtendedUpdate(el,detail);
};

var Extender = {
	update: function (element, html, detail) {
		element = $(element);
		var tag = element.tagName.toLowerCase();
		switch(tag) {
			case 'div':
				Bluora.DIVupdate(element, html, detail);
				break;
			case 'li':
				Bluora.LIupdate(element, html, detail);
				break;
			case 'input':
				Bluora.INPUTupdate(element, html, detail);
				break;
			case 'tbody':
				Bluora.TBODYupdate(element, html, detail);
				break;
			case 'td':
				Bluora.TDupdate(element, html, detail);
				break;
			case 'tr':
				Bluora.TRupdate(element, html, detail);
				break;
			case 'select':
				Bluora.SELECTupdate(element, html, detail);
				break;
			case 'span':
				Bluora.DIVupdate(element, html, detail);
				break;
			case 'table':
				Bluora.TABLEupdate(element, html, detail);
				break;
			case 'textarea': 
				Bluora.INPUTupdate(element, html, detail);
				break;
		}
		return element;
	},
	disable: function(element,makeDisabled) {
		element.disabled = makeDisabled;
		if (makeDisabled) {
			element.addClassName('disabledfield');
		} else {
			element.removeClassName('disabledfield');
		}
	},
	makeEmpty:  function (element) {
		element = $(element);
		var tag = element.tagName.toLowerCase();
		switch(tag) {
			case 'table':
				Bluora.TABLEmakeEmpty(element);
				break;
			case 'tr':
				Bluora.TRmakeEmpty(element);
				break;
		}
	},
	hideTimeout: function(element,timeout) {
		window.setTimeout("$('"+element.id+"').hide()", timeout);
	},
	showTimeout: function(element,timeout) {
		window.setTimeout("$('"+element.id+"').show()", timeout);
	}
}
Element.addMethods(['div','li','input','tbody','td','tr','select','span','table','textarea'],Extender);

var SELECTExtender = {
	hideMember: function(element,MemberID) {
		el = $(element);
		for (var i = 0;i < el.options.length;i++) {
			if (el.options[i].value == MemberID) {
				$(el.options[i]).disabled = true;
				$(el.options[i]).hide();
			//	el.options[i].value = ' - disabled';
			}
		}
	},
	removeMember: function(element,MemberID) {
		el = $(element);
		for (var i = 0;i < el.options.length;i++) {
			if (el.options[i].value == MemberID) {
				$(el.options[i]).remove();
			}
		}
	},
	showMember: function(element,MemberID) {
		el = $(element);
		for (var i = 0;i < el.options.length;i++) {
			if (el.options[i].value == MemberID) {
				$(el.options[i]).disabled = false;	
				$(el.options[i]).show();
				//el.options[i].value= el.options[i].value.replace(' - disabled','');
			}
		}
	},
	selectMember: function(element,MemberID) {
		el = $(element);
		for (var i = 0;i < el.options.length;i++) {
			if (el.options[i].value == MemberID) {
				$(el.options[i]).selected = true;
			} else {
				$(el.options[i]).selected = false;
			}
		}
	},
	countMembers: function(element,StartAt) {
		el = $(element);
		var member_count = 0;
		if (StartAt == '' || typeof StartAt == 'undefined') {
			var StartAt = 0;
		}
		for (var i = StartAt;i < el.options.length;i++) {
			if (el.options[i].style.display == '' || el.options[i].disabled == false) {
				member_count++;
			}
		}
		return member_count;
	}
}
Element.addMethods(['select'],SELECTExtender);

window.reloadPage = function() {
	window.location.hash = '';
	window.location.href = window.location.href.replace('#','');
	window.location.reload();
	//window.location.protocol+'//'+window.location.hostname+window.location.pathname+window.location.search;
}