/**
 *	@pack: false
 **/
function captchaReload() {
	if ($('captchaImg')) {
		$('captchaImg').src='/captcha/generate?'+ Math.random();
	}
}

function getSelectValue(form_obj, field_name) {
	var elm = form_obj.elements[field_name];

	if (elm == null || elm.options == null)
		return "";

	return elm.options[elm.selectedIndex].value;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}

function selectByValue(form_obj, field_name, value, ignore_case) {
	if (!form_obj || !form_obj.elements[field_name])
		return;

	var sel = form_obj.elements[field_name];

	var found = false;
	for (var i=0; i<sel.options.length; i++) {
		var option = sel.options[i];

		if (option.value == value || (ignore_case && option.value.toLowerCase() == value.toLowerCase())) {
			option.selected = true;
			found = true;
		} else
			option.selected = false;
	}

	return found;
}

function attachFormAction(form, callBackAction, onSuccessCallBack, preSubmit) {
	form = $(form);
	if (form) {
		form.onsubmit = function() {
			if (preSubmit) {
				if (preSubmit(form) == false) {
					return false;
				}
			}
			//callBackAction(this, onSuccessCallBack, preSubmit);
			callBackAction(this, onSuccessCallBack);
			return false;
		}
	}
	
	return;
}

function hasMCE(id) {
	if (tinyMCE.getInstanceById(id) == null) {
		return false;
	}
	
	return tinyMCE.getInstanceById(id);
}

//Add a new field to indicate that this form was submitted by javascript
function attachJsIndicator(form) {
	form = $(form);
	if (!form) {
		return;
	}
	
	var jsIndicator = form['is_js'];
	if (!jsIndicator) {
		jsIndicator = document.createElement('input');
		jsIndicator.type = 'hidden';
		jsIndicator.name = 'is_js';
		jsIndicator.value = 'true';
		
		form.appendChild(jsIndicator);
	}
	
	return true;
}

function submitForm(form, callBack, preSubmit) {
	form = $(form);
	if (!form) {
		alert('Form is invalid');
		return false;
	}
	
	if (!form.action) {
		alert('Form ' + form.id + ' does not contain an action');
		return false;
	}
	
	attachJsIndicator(form);
	
	if (preSubmit) {
		preSubmit(form);
	}
	
	var ajax = new AjaxProcessing(form, form.action);
	ajax.messageOnLoading('Submitting form');
	ajax.messageOnSuccess('Form submitted');
	ajax.messageOnFailure('Submission failed');
	
	if (callBack) {
		ajax.onSuccess('justcallback', function(result) {
			if (callBack) {
				callBack(form, result);
			}
		});
	}
	ajax.process();
};

var Core = {};
Core.Autocompleter = Class.create(Ajax.Autocompleter, {
	initialize: function(element, url, options) {
		//
		var choices = $('autocomplete_choices');
		if (!choices) {
			choices = document.createElement('div');
			choices.id = 'autocomplete_choices';
			choices.className = 'autocomplete';
			document.body.appendChild(choices);
		}
		
		if (options.updateFor) {
			options.afterUpdateElement = function(text, li) {
				$(options.updateFor).value = li.id;
			};
		}
		
		this.baseInitialize(element, 'autocomplete_choices', options);
		this.options.asynchronous  = true;
		this.options.onComplete    = this.onComplete.bind(this);
		this.options.defaultParams = this.options.parameters || null;
		this.url                   = url;
	},
	startIndicator: function() {
		//
		var className = this.element.className;
		if (className.indexOf('busy') < 0) {
			this.element.className = className + ' busy';
		}
	},
	
	stopIndicator: function() {
		this.element.className = this.element.className.replace('busy', '');
	}
});


function nextTab() {
	if (tabPane) {
		var tabsCount = tabPane.getTabsCount();
		var currentIndex = tabPane.getSelectedIndex();
		if (currentIndex < tabsCount - 1) {
			tabPane.setSelectedIndex(++currentIndex);
		} else {
			tabPane.setSelectedIndex(0);
		}
	}
}

function previousTab() {
	if (tabPane) {
		var tabsCount = tabPane.getTabsCount();
		var currentIndex = tabPane.getSelectedIndex();
		if (currentIndex > 0) {
			tabPane.setSelectedIndex(--currentIndex);
		} else {
			tabPane.setSelectedIndex(tabsCount - 1);
		}
	}
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
