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

function ClearAboutusData(){
	HideStatusDiv();
	
	SetTextEditor('rteBG', '');
	SetTextEditor('rteEN', '');
	
	document.getElementById('rteBG').focus();
	document.getElementById('rteBG').contentWindow.focus();
}

function ClearCategoryData(){
	HideStatusDiv();
	
	document.getElementById("categoryId").value = 0;
	document.getElementById("nameBG").value = '';
	document.getElementById("nameEN").value = '';
	document.getElementById("nameBG").focus();
	
	document.getElementById("submitCategory").value = 'добави';
	
	return true;
}

function ClearContactData(){
	HideStatusDiv();
	
	document.getElementById("contactId").value = 0;
	document.getElementById("nameBG").focus();
	
	document.getElementById("submitContact").value = 'добави';
	
	return true;
}

function ClearMessageData(){
	HideStatusDiv();
	
	document.getElementById("messageId").value = 0;
	
	return true;
}

function ClearNewsData(){
	HideStatusDiv();

	document.getElementById("newsId").value = 0;
	
	SetTextEditor('rteBG', '');
	SetTextEditor('rteEN', '');
	
	document.getElementById("submitNews").value = 'добави';
	document.getElementById("titleBG").focus();
	
	return true;
}

function ClearProductData(){
	HideStatusDiv();
	
	document.getElementById("productId").value = 0;
	document.getElementById("productNameBG").value = '';
	document.getElementById("productNameEN").value = '';
	document.getElementById("productNameBG").focus();
	
	document.getElementById("submitProduct").value = 'добави';
	
	return true;
}

function ClearSettingsData(){
	HideStatusDiv();
	document.getElementById("contactsPerPage").value = document.getElementById("contactsCount").value;
	document.getElementById("newsPerPage").value = document.getElementById("newsCount").value;
	document.getElementById("productsPerPage").value = document.getElementById("productsCount").value;
	document.getElementById("shopsPerPage").value = document.getElementById("shopsCount").value;
	SetFocus("contactsPerPage");
	return true;
}

function ClearShopData(){
	HideStatusDiv();

	document.getElementById("shopId").value = 0;
	
	document.getElementById("submitShop").value = 'добави';
	document.getElementById("nameBG").focus();
	
	return true;
}

function CorrectAboutusData(){
	var editorBG = document.getElementById('rteBG');
	var editorEN = document.getElementById('rteEN');
	
	document.getElementById("aboutusBG").value = editorBG.contentWindow.document.body.innerHTML.trim();
	document.getElementById("aboutusEN").value = editorEN.contentWindow.document.body.innerHTML.trim();
	
	if(editorBG.contentWindow.document.body.innerHTML.length == 0){
		DisplayErrors('моля попълнете за нас формата за българската версия<br/>');
		return false;
	}
	else{
		return true;
	}
}

function CorrectCategoryData(){
	var nameBG = document.getElementById("nameBG").value;
	
	var errors = '';
	
	var categoryId = document.getElementById("categoryId").value;
	var mainCategoryId = document.getElementById("mainCategories").value;
	
	if(categoryId == mainCategoryId && categoryId != 0){
		errors += 'главната и подкатегорията съвпадат<br/>';
	}
	
	if(nameBG.length == 0){
		errors += 'празно поле за името за българската версия на категорията<br/>';
	}
	
	if(errors.length == 0){
		return true;
	}
	else{
		DisplayErrors(errors);
		return false;
	}
}

function CorrectContactData(){
	var nameBG = document.getElementById("nameBG").value;
	var phone = document.getElementById('phone').value;
	
	var errors = '';
	
	if(nameBG.length == 0){
		errors += 'празно поле за името за българската версия на контакта<br/>';
	}

	if(phone.length == 0){
		errors += 'празно поле за телефона на контакта<br/>';
	}
	
	if(errors.length == 0){
		return true;
	}
	else{
		DisplayErrors(errors);
		return false;
	}
}

function CorrectMessageData(){
	var messageId = document.getElementById("messageId").value;
	
	return (messageId > 0);
}

function CorrectNewsData(){
	var titleBG = document.getElementById("titleBG").value;
	var editorBG = document.getElementById('rteBG');
	var editorEN = document.getElementById('rteEN');
	
	document.getElementById("bodyBG").value = editorBG.contentWindow.document.body.innerHTML;
	document.getElementById("bodyEN").value = editorEN.contentWindow.document.body.innerHTML;
	
	var errors = '';
	
	if(titleBG.length == 0){
		errors += 'празно поле за заглавието за българската версия на новината<br/>';
	}

	if(editorBG.contentWindow.document.body.innerHTML.length == 0){
		errors += 'празно поле за тялото за българската версия на новината<br/>';
	}
	
	if(errors.length == 0){
		return true;
	}
	else{
		DisplayErrors(errors);
		return false;
	}
}

function CorrectProductData(){
	var productId = document.getElementById("productId").value;
	var nameBG = document.getElementById("productNameBG").value.trim();
	var category = document.getElementById("productCategory").value;
	var descBG = document.getElementById("descBG").value.trim();
	
	var errors = '';
	
	if(nameBG.length == 0){
		errors += 'празно поле за името за българската версия на продукта<br/>';
	}
	
	if(descBG.length == 0){
		errors += 'празно поле за описанието за българската версия на продукта<br/>';
	}
	
	if(category == 0){
		errors += 'изберете категория<br/>';
	}
	
	if(errors.length == 0){
		return true;
	}
	else{
		DisplayErrors(errors);
		return false;
	}
}

function ValidateEmail(email){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

function CorrectSendMessageData(emptyFields, incorrectEmail){	
	var contactNames = document.getElementById("contactNames").value.trim();
	var contactEmail = document.getElementById("contactEmail").value;
	var contactMessage = document.getElementById("contactMessage").value.trim();
	
	var errors = '';
	
	if(contactNames.length == 0 || contactEmail.length == 0 || contactEmail.length == 0){
		errors += emptyFields + '\n';
	}

	if(!ValidateEmail(contactEmail)){
		errors += incorrectEmail + '\n';
	}

	if(errors.length == 0){
		return true;
	}
	else{
		alert(errors);
		return false;
	}
}

function CorrectSettingsData(){
	var contactsPerPage = parseInt(document.getElementById("contactsPerPage").value);
	var shopsPerPage = document.getElementById("shopsPerPage").value;
	var newsPerPage = document.getElementById("newsPerPage").value;
	var productsPerPage = document.getElementById("productsPerPage").value;
	
	var errors = '';
	
	if(contactsPerPage < 1 || contactsPerPage > 99){
		errors += 'въведете число между 1 и 99 за брой контакти на страница<br/>';
	}
	
	if(shopsPerPage < 1 || shopsPerPage > 99){
		errors += 'въведете число между 1 и 99 за брой магазини на страница<br/>';
	}
	
	if(newsPerPage < 1 || newsPerPage > 99){
		errors += 'въведете число между 1 и 99 за брой новини на страница<br/>';
	}
	
	if(productsPerPage < 1 || productsPerPage > 99){
		errors += 'въведете число между 1 и 99 за брой продукти на страница<br/>';
	}
	
	if(errors.length == 0){
		return true;
	}
	else{
		DisplayErrors(errors);
		return false;
	}
}

function CorrectShopData(){
	var nameBG = document.getElementById("nameBG").value;
	var cityBG = document.getElementById("cityBG").value;
	var addressBG = document.getElementById("addressBG").value;
	var phone = document.getElementById("phone").value;
	
	var errors = '';
	
	if(nameBG.length == 0){
		errors += 'празно поле за името за българската версия на магазина<br/>';
	}
	
	if(cityBG.length == 0){
		errors += 'празно поле за града за българската версия на магазина<br/>';
	}
	
	if(addressBG.length == 0){
		errors += 'празно поле за адреса за българската версия на магазина<br/>';
	}
	
	if(phone.length == 0){
		errors += 'празно поле за телефона за българската версия на магазина<br/>';
	}
	
	if(errors.length == 0){
		return true;
	}
	else{
		DisplayErrors(errors);
		return false;
	}
}

function DeleteCategory(categoryId, nameBG){
	if(confirm("Сигурен ли сте, че искате да изтриете: " + nameBG + "?")){
		document.location.href = "admin.php?cat=categories&categoryid=" + categoryId + "&act=delete";
	}
}


function DeleteContact(contactId, nameBG){
	if(confirm("Сигурен ли сте, че искате да изтриете: " + nameBG + "?")){
		document.location.href = "admin.php?cat=contacts&contactid=" + contactId + "&act=delete";
	}
}

function DeleteMessage(messageId, from){
	if(confirm("Сигурен ли сте, че искате да изтриете съобщението от " + from + "?")){
		document.location.href = "admin.php?cat=messages&messageid=" + messageId + "&act=delete";
	}
}

function DeleteNews(newsId, titleBG){
	if(confirm("Сигурен ли сте, че искате да изтриете: " + titleBG + "?")){
		document.location.href = "admin.php?cat=news&newsid=" + newsId + "&act=delete";
	}
}

function DeleteProduct(productId, nameBG){
	if(confirm("Сигурен ли сте, че искате да изтриете продукт: " + nameBG + "?")){
		document.location.href = "admin.php?cat=categories&productid=" + productId + "&act=delete";
	}
}

function DeleteShop(shopId, nameBG){
	if(confirm("Сигурен ли сте, че искате да изтриете магазин: " + nameBG + "?")){
		document.location.href = "admin.php?cat=shops&shopid=" + shopId + "&act=delete";
	}
}

function EditCategory(categoryId, mainCategoryId, nameBG, nameEN){
	HideStatusDiv();
	
	SetCategoryData(categoryId, mainCategoryId, nameBG, nameEN);

	return true;
}

function EditContact(contactId, nameBG, nameEN, positionBG, positionEN, phone, email, gsm){
	HideStatusDiv();
	
	SetContactData(contactId, nameBG, nameEN, positionBG, positionEN, phone, email, gsm);

	return true;
}

function EditNews(newsId, titleBG, titleEN, bodyBG, bodyEN){
	HideStatusDiv();
	
	SetNewsData(newsId, titleBG, titleEN, bodyBG, bodyEN);

	return true;
}

function EditProduct(productId, nameBG, nameEN, productCategory, descBG, descEN){
	HideStatusDiv();
	
	SetProductData(productId, nameBG, nameEN, productCategory, descBG, descEN)

	return true;
}

function EditShop(shopId, nameBG, nameEN, cityBG, cityEN, addressBG, addressEN, phone, email, mapLink){
	HideStatusDiv();
	
	SetShopData(shopId, nameBG, nameEN, cityBG, cityEN, addressBG, addressEN, phone, email, mapLink);

	return true;
}

function HideStatusDiv(){
	var errorDiv = document.getElementById("errors");
	errorDiv.style.display = "none";
	var successDiv = document.getElementById("success");
	successDiv.style.display = "none";
}

function LoadMessage(messageId, messageFrom, messageEmail, messageBody, messageDateTime){
	document.getElementById("messageId").value = messageId;
	document.getElementById("from").value = messageFrom;
	document.getElementById("email").value = messageEmail;
	document.getElementById("body").value = messageBody;
	document.getElementById("datetime").value = messageDateTime;
	
	return true;
}

function SetCategoryData(categoryId, mainCategoryId, nameBG, nameEN){
	SetFocus("nameBG");
	document.getElementById("submitCategory").value = 'редактирай';
	document.getElementById("categoryId").value = categoryId;
	document.getElementById("mainCategories").value= mainCategoryId;
	document.getElementById("nameBG").value = nameBG;
	document.getElementById("nameEN").value = nameEN;
}

function SetContactData(contactId, nameBG, nameEN, positionBG, positionEN, phone, email, gsm){
	SetFocus("nameBG");
	document.getElementById("submitContact").value = 'редактирай';
	document.getElementById("contactId").value = contactId;
	document.getElementById("nameBG").value = nameBG;
	document.getElementById("nameEN").value = nameEN;
	document.getElementById("positionBG").value = positionBG;
	document.getElementById("positionEN").value = positionEN;
	document.getElementById("phone").value = phone;
	document.getElementById("email").value = email;
	document.getElementById("gsm").value = gsm;
}

function SetNewsData(newsId, titleBG, titleEN, bodyBG, bodyEN){
	SetFocus("titleBG");
	document.getElementById("submitNews").value = 'редактирай';
	
	document.getElementById("newsId").value = newsId;
	document.getElementById("titleBG").value = titleBG;
	document.getElementById("titleEN").value = titleEN;
	
	SetTextEditor('rteBG', bodyBG);
	SetTextEditor('rteEN', bodyEN);
}

function SetProductData(productId, nameBG, nameEN, productCategory, descBG, descEN){
	SetFocus("productNameBG");
	document.getElementById("submitProduct").value = 'редактирай';
	document.getElementById("productId").value = productId;
	document.getElementById("productNameBG").value = nameBG;
	document.getElementById("productNameEN").value = nameEN;
	document.getElementById("productCategory").value = productCategory;
	document.getElementById("descBG").value = descBG;
	document.getElementById("descEN").value = descEN;
}

function SetShopData(shopId, nameBG, nameEN, cityBG, cityEN, addressBG, addressEN, phone, email, mapLink){
	SetFocus("nameBG");
	document.getElementById("submitShop").value = 'редактирай';
	document.getElementById("shopId").value = shopId;
	document.getElementById("nameBG").value = nameBG;
	document.getElementById("nameEN").value = nameEN;
	document.getElementById("cityBG").value = cityBG;
	document.getElementById("cityEN").value = cityEN;
	document.getElementById("addressBG").value = addressBG;
	document.getElementById("addressEN").value = addressEN;
	document.getElementById("phone").value = phone;
	document.getElementById("email").value = email;
	document.getElementById("mapLink").value = mapLink;
}

function SetTextEditor(fieldId, value){
	var editor = document.getElementById(fieldId);
	if(editor != undefined){
		editor.contentWindow.document.body.innerHTML = value;
	}
}

function ShowHide(editorId, titleId, type){
	var editorDiv = document.getElementById(editorId);
	if(editorDiv != undefined){
		if(editorDiv.style.display == "none"){
			editorDiv.style.display = "block";
			if(type == 0){
				SetFocus(titleId);
			}
			else{
				SetEditorFocus(titleId);
			}
		}
		else{
			editorDiv.style.display = "none";
		}
	}
	
	return true;
}

function SubmitNews(){
	var editorBG = document.getElementById('bodyBG');
	var editorEN = document.getElementById('bodyEN');
	var hiddenBG = document.getElementById('hiddenBG');
	var hiddenEN = document.getElementById('hiddenEN');
	
	hiddenBG.value = editorBG.contentWindow.document.body.innerHTML;
	hiddenEN.value = editorEN.contentWindow.document.body.innerHTML;
	
	return true;
}

function DisplayErrors(error){
	var errorDiv = document.getElementById("errors");
	errorDiv.innerHTML = error;
	errorDiv.style.display = "block";
}

function DisplaySuccess(success){
	var successDiv = document.getElementById("success");
	successDiv.innerHTML = success;
	successDiv.style.display = "block";
}

function SetEditorFocus(fieldId){
	SetFocus(fieldId);
	document.getElementById(fieldId).contentWindow.focus();
}

function SetSettingsData(contactsCount, newsCount, productsCount, shopsCount){
	document.getElementById("contactsPerPage").value = contactsCount; 
	document.getElementById("contactsCount").value = contactsCount;
	document.getElementById("newsPerPage").value = newsCount; 
	document.getElementById("newsCount").value = newsCount;
	document.getElementById("productsPerPage").value = productsCount;
	document.getElementById("productsCount").value = productsCount;
	document.getElementById("shopsPerPage").value = shopsCount; 
	document.getElementById("shopsCount").value = shopsCount;
	SetFocus("contactsPerPage");
	return true;
}

function SetFocus(fieldId){
	if(document.getElementById(fieldId) != undefined){
		document.getElementById(fieldId).focus();
	}
}

function RevealModal(divId){
	window.onscroll = function () { document.getElementById(divId).style.top = document.body.scrollTop; };
	document.getElementById(divId).style.display = "block";
	document.getElementById(divId).style.top = document.body.scrollTop;
}

function HideModal(divId){
	document.getElementById(divId).style.display = "none";
}

function SetDivDisplay(divId, imgId){
	document.getElementById(divId).style.display = "block";
	document.getElementById(imgId).src = "images/site/minus.gif";
}

function ShowImage(divId, imgSrc){
	RevealModal(divId);
	ToggleImage(imgSrc);
}

function Toggle(node){
	if (node.nextSibling.style.display == 'none'){
		if (node.childNodes.length > 0){
			if (node.childNodes.item(0).nodeName == "IMG"){
				node.childNodes.item(0).src = "images/site/minus.gif";
			}
		}
		node.nextSibling.style.display = 'block';
	}
	else{
		if (node.childNodes.length > 0){
			if (node.childNodes.item(0).nodeName == "IMG"){
				node.childNodes.item(0).src = "images/site/plus.gif";
			}
		}
		node.nextSibling.style.display = 'none';
	}

}

function ToggleDiv(divId, imgId){
	if(document.getElementById(divId).style.display == "none"){
		document.getElementById(divId).style.display = "block";
		document.getElementById(imgId).src = "images/site/minus.gif";
	}
	else{
		document.getElementById(divId).style.display = "none";
		document.getElementById(imgId).src = "images/site/plus.gif";
	}
}

function ToggleImage(imgSrc){
	document.getElementById('shopMap').src = imgSrc;
}
