/**
 * RSS Reader edit control script
 *
 * @copyright (c) 2004-2006, SWsoft
 */
function SbRssEditControl(selectionObject) {
	this._inputObject = null;
	this._predefinedObject = null;
	this._selectionObject = selectionObject;
	this._objectsList = new Array();
	this._selectedObject = null;
	this._customChannelObject = null;
	var editor = this;
	this._selectionObject.addOnHighlightEvent(function(channelObject) {return editor.onHighlighItem(channelObject);});
	this._selectionObject.addOnResetEvent(function(channelObject) {return editor.onResetItem(channelObject);});
	this._selectionObject.addOnSelectEvent(function(channelObject) {return editor.onSelectItem(channelObject);});
	self.sbEditorObject.addOnSubmitEvent(function() {return editor.onSubmitForm();});
	addEvent(self, 'load', function (event) { editor._endInitialization(); });
}

SbRssEditControl.prototype.setPredefinedObject = function(objectId) {
	this._predefinedObject = self.document.getElementById(objectId);
	if (null != this._selectedObject) {
		this._updateInputObject(this._selectedObject);
	}
}

SbRssEditControl.prototype.setInputObject = function(inputId) {
	this._inputObject = self.document.getElementById(inputId);
	if (null != this._selectedObject) {
		this._updateInputObject(this._selectedObject);
	}
}

SbRssEditControl.prototype.setSelectedObject = function(itemObjectId) {
	this._selectedObject = self.document.getElementById(itemObjectId);
}

SbRssEditControl.prototype.setCustomObject = function(itemObjectId) {
	this._customChannelObject = self.document.getElementById(itemObjectId);
}

SbRssEditControl.prototype.registerObject = function(itemObjectId, rss, code, itemObjectSourceId) {
	var itemObject = self.document.getElementById(itemObjectId);
	var itemObjectSource = self.document.getElementById(itemObjectSourceId);
	itemObjectSource.className = 'sb-rss-channel';
	itemObject.rss = rss;
	itemObject.code = code;
	if (document.all) {
		setObjectSize(itemObjectSource, 128, 40);
	} else {
		setObjectSize(itemObjectSource, 116, 30);
	}
	itemObject.sourceObject = itemObjectSource;
	this._objectsList.push(itemObject);
	this._selectionObject.registerObject(itemObject);
}

SbRssEditControl.prototype._endInitialization = function() {
	if (null != this._selectedObject) {
		this._selectionObject.select(this._selectedObject);
	}
}

SbRssEditControl.prototype.onHighlighItem = function(channelObject) {
	//channelObject.sourceObject.className = 'sb-rss-channel-sorted';
	this._updateInputObject(channelObject);
	return true;
}

SbRssEditControl.prototype.onResetItem = function(channelObject) {
	//channelObject.sourceObject.className = 'sb-rss-channel';
	if (null != this._selectedObject) {
		this._updateInputObject(this._selectedObject);
	}
	return true;
}

SbRssEditControl.prototype.onSelectItem = function(channelObject) {
	//channelObject.sourceObject.className = sb-rss-channel-sorted';
	this._selectedObject = channelObject;
	this._updateInputObject(this._selectedObject);
	return true;
}

SbRssEditControl.prototype.onSubmitForm = function() {
	var result = true;
	if ((this._selectedObject == this._customChannelObject) && !this._validateCustomRss()) {
		result = false;
	}
	if (result) {
		self.sbEditorObject.addHidden('rss-channel-code', this._selectedObject.code);
	}
	return result;
}

SbRssEditControl.prototype._validateCustomRss = function() {
	var result = true;
	var validator = new Validator();
	if (validator.isEmpty(this._inputObject.value)) {
		alert(SbGetLocaleByKey('emptyCustomChannelUrl'));
		result = false;
	} else {
		if (!validator.isHttpUrl(this._inputObject.value)) {
			alert(SbGetLocaleByKey('customChannelIsNotValidUrl'));
			result = false;
		}
	}
	return result;
}

SbRssEditControl.prototype._updateInputObject = function(channelObject) {
	if ((null != this._inputObject) && (null != this._predefinedObject)) {
		if (channelObject == this._customChannelObject) {
			this._predefinedObject.style.visibility = 'hidden';
			this._predefinedObject.style.display = 'none';
			this._inputObject.style.visibility = 'visible';
			this._inputObject.style.display = 'block';
			this._inputObject.focus();
		} else {
			this._predefinedObject.innerHTML = _htmlSpecialChars(channelObject.rss);
			this._inputObject.style.visibility = 'hidden';
			this._inputObject.style.display = 'none';
			this._predefinedObject.style.visibility = 'visible';
			this._predefinedObject.style.display = 'block';
		}
	}
}

function _htmlSpecialChars(str) {
	try {
		str = str.replace(/&/ig, '&amp;');
		str = str.replace(/\?/ig, '&#63;');
		str = str.replace(/</ig, '&lt;');
		str = str.replace(/>/ig, '&gt;');
		str = str.replace(/\'/ig, '&#39;');
		str = str.replace(/\"/ig, '&#34;');
	} catch(e){ }

	return str;
}