
//---------------------------------------------------------

var cachedElements = new Array();

function getElementById(elementId){
	if (!cachedElements[elementId]) {
		cachedElements[elementId] = document.getElementById(elementId);
	}
	return cachedElements[elementId];
}

//---------------------------------------------------------

function loadingText(){
	return '<img src="images/loading.gif" style="border: 0px;" /> Зареждане...';
}

//---------------------------------------------------------

function getMessagesBox(){
	return getElementById('messages');
}

function unsetMessages(){
	getMessagesBox().innerHTML = '&nbsp;';
	getMessagesBox().style.color = '';
}
//---------------------------------------------------------

function cancelForm(formCancelUrl){
	//document.ContainerForm.action = formCancelUrl;
	//document.ContainerForm.submit();
	document.location = formCancelUrl;
}
//---------------------------------------------------------

var lastPopUp;

function openPopUp(url, width, height){
	lastPopUp = window.open(url, 'nWin', 'toolbar=0, location=0,directories=0,status=0,menubar=0,resizable=1, width='+ width +',height='+ height+'');
	lastPopUp.focus();
}

function closePopUp(){
	if (lastPopUp){
		lastPopUp.close();
	}
}

//---------------------------------------------------------

function SubmitedForm(){
	this.fields = new Array();
	this.appendField = function(name, value){
		this.fields.push(new Array(name, value));
	}
}
//---------------------------------------------------------
function walkForm(form){
	var submit = new SubmitedForm();

	for (var x=0; x<form.elements.length; x++){
		var element = form.elements[x];
		if(element.name != '' && element.disabled != true){
			switch(element.tagName.toLowerCase()){
				case 'input':
					switch (element.type.toLowerCase()){
						case 'radio':
						case 'checkbox':
							if(element.checked){
								submit.appendField(element.name, element.value);
							}
						break;

						default:
							submit.appendField(element.name, element.value);
						break;
					}
				break;

				case 'select':
					var values = new Array();
					for(var y=0; y<element.options.length; y++){
						if (element.options[y].selected == true){
							if(element.options[y].value != ''){
								values.push(element.options[y].value);
							}else{
								values.push(element.options[y].text); // for IE
							}
						}
					}
					if(values.length > 0){
						submit.appendField(element.name, values);
					}
				break;

				case 'textarea':
					submit.appendField(element.name, element.value);
				break;
			}
		}
	}
	return submit;
}
//---------------------------------------------------------

function clearZero(field){
	if (field.value == 0){
		field.value = '';
	}
}

function bringBackZero(field){
	if (field.value == ''){
		field.value = 0;
	}
}

var searchTextField;
function checkSearchString(){
	if (!searchTextField){
		searchTextField = document.getElementById('searchTextField');
	}
	return (searchTextField.value != "" && searchTextField.value.replace(/\s+$/, ""));
}


function setCookie (name, value, expireDate, path, domain, secure){
	var cookieString = name + "=" + escape(value);

	if (typeof(expireDate) == "object"){
		cookieString += "; expires=" + expireDate.toGMTString();
	}
	if (path){
		cookieString += "; path=" + escape(path);
	}
	if ( domain ){
		cookieString += "; domain=" + escape(domain);
	}
	if ( secure ){
		cookieString += "; secure";
	}
	document.cookie = cookieString;
}


function getCookie (cookieName){
  var results = document.cookie.match( cookieName + '=(.*?)(;|$)' );

  if (results){
    return (unescape(results[1]));
  }else {
    return null;
  }
}

//---------------------------------------------------------
// Increase/Decrease Font Size Tool

var minFontSize = 6;
var maxFontSize = 16;
var textContainers = new Array();

function changeFontSize(containerId, direction){
	var textContainer = getElementById(containerId);

	if (typeof(textContainer) != 'object') {
		return false;
	}

	if (!textContainers[containerId]){
		textContainers[containerId] = new Array();
		textContainers[containerId]['home'] = 8;
		textContainers[containerId]['last'] = textContainers[containerId]['home'];
	}

	var newSize = textContainers[containerId]['last'];
	
	if (direction < 0){
		newSize -= (newSize -2 >= minFontSize) ? 2 : 0;

	}else if (direction > 0){
		newSize += (newSize +2 <= maxFontSize) ? 2 : 0;

	}else {
		newSize = textContainers[containerId]['home'];
	}

	textContainers[containerId]['last'] = newSize;
	textContainer.style.fontSize = newSize +'pt';

	return false;
}
//---------------------------------------------------------


var editorLastLang = 'BG';
var editorLastLinkObj;
//
function switchEditorLang(lang, linkObj){
	var nextLangEditor = getElementById(editorFieldName + lang);
	var lastLangEditor = getElementById(editorFieldName + editorLastLang);

	lastLangEditor.style.display = 'none';
	nextLangEditor.style.display = 'inline';

	if (typeof(linkObj) == 'object'){
		if (!editorLastLinkObj){
			editorLastLinkObj = getElementById('langLink_'+ fieldName + editorLastLang);
		}
		editorLastLinkObj.style.color = null;

		linkObj.style.color = 'red';
		editorLastLinkObj = linkObj;
	}

	editorLastLang = lang;

	return false;
}

var editorFieldName = 'Description';
//
function editorAction(action){
	switch (action){
		case 'reset':
			getElementById(editorFieldName + 'BG').value = '';
		break;

		case 'updateField':
			getElementById(editorFieldName + editorLastLang).value = getElementById(editorFieldName + 'BG').value;
		break;
	}
}

var langFields = new Array();
//
function switchLangField(property, lang, linkObj){
	if (!langFields[property]){
		langFields[property] = new Array();
		langFields[property]['lastLang'] = 'BG';
	}

	getElementById(property + langFields[property]['lastLang']).style.display = 'none';
	getElementById(property + lang).style.display = 'inline';

	if (typeof(linkObj) == 'object'){
		if (!langFields[property]['lastLinkObj']){
			langFields[property]['lastLinkObj'] = getElementById('langLink_'+ property + langFields[property]['lastLang']);
		}
		langFields[property]['lastLinkObj'].style.color = null;
	
		linkObj.style.color = 'red';
		langFields[property]['lastLinkObj'] = linkObj;
	}

	langFields[property]['lastLang'] = lang;

	return false;
}