/**
 * Znajduje współrzędne obiektu
 * @param obj Object obiekt
 */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

/**
 * Wyświetla / chowa element
 * @param el_id string id elementu
 */
function hideShowElement(el_id){
	Effect.toggle($(el_id),'blind',{duration:0.5});
} 

/**
 * Przesuwanie kalendarium na stronie głównej
 * @param size int ilość pikseli
 */
function moveEvents(size){
	if (size < 0){
		if (currentEvent == eventsCount){
			return;
		}else{
			currentEvent++;
		}
	}else if (size > 0){
		if (currentEvent == showEvents){
			return;
		}else{
			currentEvent--;
		}
	}
	
	var size__ = 0;
	size__ = (currentEvent - showEvents) * 74 * (-1);
	
	//new Effect.Move('events_moving', { x: 0, y: size, transition: Effect.Transitions.sinoidal });
	
	new Effect.Morph('events_moving', {
		  style: "position: relative; left: 0px; top: "+size__+"px;", // CSS Properties
		  duration: 0.8 // Core Effect properties
		});
}

/**
 * Ocenianie zdjęć -> podświetlanie gwiazdek
 */
function selectedActiveImage(id_, max_, img_path){
	$('selecting_rank').onmouseout = function() {
		var w = 1;
		for(w ; w <= max_ ; w++){
			document.getElementById('star_'+w).style.backgroundImage = 'url('+img_path+'gray_star.png)'
		}
	}

	var i = 1;
	for(i ; i <= id_ ; i++){
		document.getElementById('star_'+i).style.backgroundImage = 'url('+img_path+'orange_star.png)';
	}
	for(i ; i <= max_ ; i++){
		document.getElementById('star_'+i).style.backgroundImage = 'url('+img_path+'gray_star.png)'
	}
}

/**
 * Dodanie oceny 
 * @param note_ int ocena (1-5)
 * @param url_notes_add string adres do oceniania
 */
function addNote(note_, url_notes_add){
	var note_for_article = document.getElementById('NoteNote');
	note_for_article.value = note_;
	new Ajax.Updater('note_info', url_notes_add, {method: 'post', parameters: Form.serializeElements(Form.getElements('add_note_form'))});
}

/**
 * Dodanie komentarza
 * @param url_comments_add string adres do dodawania komentarzy
 */
function addComment(url_comments_add){
	var content = $('CommentContent').value;
	
	if (content != ''){
		var updater = new Ajax.Request(url_comments_add, {method: 'post',
			parameters: Form.serializeElements( Form.getElements('add_comment_form') ),
			onSuccess: function() {
				$('request').innerHTML = 'Dziękujemy za dodanie komentarza.';
				$('add_comment_div').style.display = '';
				$('add_comment_sending').style.display = 'none';
				$('CommentContent').value = '';
				if ($('CommentNickName')){
					$('CommentNickName').value = '';
				}
				//location.reload(true);
				new Ajax.Updater('comment_info',url_update_note_comment_info , {method: 'post', parameters: Form.serializeElements(Form.getElements('add_comment_form')), onComplete:function(){hide_show_element('request');}});
				
				//uzupelnienie wartosci o nastepny komentarz
				var comment_span = $('comments_count');
				comment_span.innerHTML = parseInt(parseInt(comment_span.innerHTML)+1);
			},
			onLoading: function() {
				$('request').innerHTML = '';
				$('add_comment_div').style.display = 'none';
				$('add_comment_sending').style.display = '';
			},
			onFailure :function() {
				$('request').innerHTML = 'Wystąpił błąd. Komentarz nie został dodany.';
				$('add_comment_div').style.display = '';
				$('add_comment_sending').style.display = 'none';
			}
		});
		
		
	}else{
		alert ('Wpisz komentarz!');
	}
}

/**
 * Wyświetla kalendarzyk do kalendarium
 */
function showEventCalendar(){
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	if (IE6){
		var main_div = $('events_calendar_arrow').parentNode;	
		var position = findPos(main_div);	
		var calendar_div = $('EventsCalendar');
		calendar_div.style.left = position[0]+'px';
		calendar_div.style.top = (position[1]+21)+'px';
		calendar_div.style.position = 'absolute';
		if (calendar_div.style.display == ''){
			calendar_div.style.display = 'none';
		}
		displayElement(calendar_div);
	}
}

/**
 * Wyświetla / ukrywa element
 * @param element Object obiekt do wyświetlenia
 */
function displayElement(element){
	if (element){
		if (element.style.display == 'none'){
			element.style.display = 'block';
			lastShowedDiv = element.id;
		}else{
			element.style.display = 'none';
		}
	}
}

/**
 * Wysłanie formularza
 * @param url string adres dla formularza
 * @param form_id string id formularza
 */
function submitForm(url, form_id){
	$(form_id).action = url;
	$(form_id).submit();
}

/**
 * Wydrukowanie artkyułu
 * @param article_id int id artykułu
 * @param page int numer strony
 */
function printArticle(article_id, page){
	window.open(article_print_page_url+article_id, 'article_print_page_'+article_id+'/'+page, 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no, width=710, height=600');
}

/**
 * Wydrukowanie produktu
 * @param product_id int id produktu
 */
function printProduct(product_id){
    window.open(product_print_page_url+product_id, 'product_print_page_'+product_id, 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no, width=780, height=600');
}

/**
 * Wydrukowanie ogłoszenia
 * @param advertisement_id int id ogłoszenia
 */
function printAdvertisement(advertisement_id){
    window.open(advertisement_print_page_url+advertisement_id, 'product_print_page_'+advertisement_id, 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no, width=780, height=600');
}

/**
 * Wydrukowanie firmy
 * @param product_id int id produktu
 */
function printEntry(entry_id){
    window.open(entry_print_page_url+entry_id, 'product_print_page_'+entry_id, 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no, width=780, height=600');
}

/**
 * Wydrukowanie oferty pracy
 * @param product_id int id produktu
 */
function printOffer(offer_id){
    window.open(offer_print_page_url+offer_id, 'offer_print_page_'+offer_id, 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no, width=780, height=600');
}

/**
 * Zmiana strony przy pomocy selecta 'na skroty'
 */
function changePagePage(){
	if ($('PagePage').selectedIndex != 0){
		document.location.href = $('PagePage').options[$('PagePage').selectedIndex].value;
	}
}

/**
 * Wyczyszczenie inputa
 * @param input_id string id inputa
 * @param start_string string początkowa wartość inputa (aby nie czyścić wpisanych danych)
 */
function clearInput(input_id, start_string){
	if ($(input_id).value == start_string){
		$(input_id).value = '';
	}
}

/**
 * Zmiana zdjęcia w galerii produktu
 * @param id int numer zdjęcia
 */
function changeProductPhoto(id){
	//podmiana lightboxa
	//$('lighbox_href').href=pictures_url+all_photos[id]+'_wm';
	
	$('lighbox_href').href=all_photos_wm[all_photos[id]];

	var counted_desc = all_photos.length;

	var link_to_write = '';

	for (var i = 0; i < counted_desc; i++){
		if(i != id){
			//link_to_write=link_to_write+' '+'<a id="lighbox_href" rel="lightbox[showcases]" href="'+pictures_url+all_photos[i]+'" title="'+all_photos_desc[i]+'"></a>';
			link_to_write=link_to_write+' '+'<a id="lighbox_href" rel="lightbox[showcases]" href="'+all_photos_wm[all_photos[i]]+'"></a>';
		}
	}

	$('other_lightbox_links').innerHTML=link_to_write;
	
	$('product_photo_img').src = preLoad[id];
}

/**
 * Wyświetla wideoprezentację produktu
 * @param product_id int id produktu
 * @param main_url string link do strony głównej
 */
function showProductVideo(product_id, main_url){
	window.open(main_url+'videos/show_popup/'+product_id, '','width=439,height=330,resizable=no,scrollbars=no,toolbar=no');
}

/**
 * Kliknięcie w menu na stronie firmy
 * @param number int numer item'u
 */
function changeEntryMenu(number){
	$('entry_menu_item_'+current_entry_menu_item).className = 'entry_menu_item_no_selected';
	$('entry_menu_item_'+number).className = 'entry_menu_item_selected';
	$('entry_menu_content_'+current_entry_menu_item).style.display = 'none';
	$('entry_menu_content_'+number).style.display = 'block';
	
	current_entry_menu_item = number;
}

/**
 * Wyświetlanie produktów w wizytówce firmy
 * Pobiera kolejne podstrony Ajaxem
 * @param url string adres
 */
function changeEntryProduct(url){
	var pars = Form.serializeElements(Form.getElements('ProductsEntryList'));
	$('entry_products').innerHTML = 'Pobieranie danych. Proszę czekać...';
	new Ajax.Updater('entry_products', url, {method: 'post', parameters: {rates: pars}, evalScripts: true});
}

/**
 * Dodaje mapkę Googla
 * @param lat
 * @param lon
 * @param txt
 */
function add_marker(lat,lon,txt){  
	
	var marker = new GMarker(new GLatLng(lat,lon),{title: txt});  
	marker.txt=txt;  
	map.addOverlay(marker);  
	GEvent.addListener(marker,"click",function() {  
		marker.openInfoWindowHtml(marker.txt);  
	});  
	           
	return marker;  
}

/**
 * Wyświetla mapę firmy
 * @param outlet_id int id outletu
 */
function showEntryMap(outlet_id){
	window.open(entries_show_map_url+'/'+outlet_id, '','width=800,height=600,resizable=no,scrollbars=no,toolbar=no');
}

/**
 * Wybór branży w wyszukiwarce pracy
 * @param id int id branży
 */
function selectCraft(id){
	$('SearchCraftId').value = id;
	$('SearchCraftName').value = $('craft_'+id).innerHTML;
	displayElement($('select_craft'));
}

/**
 * Wybór regionu w wyszukiwarce pracy
 * @param id int id województwa
 */
function selectRegion(id){
	$('SearchRegionId').value = id;
	$('SearchRegionName').value = $('region_'+id).innerHTML;
	displayElement($('select_region'));
}

/**
 * Wysyła formularz wyszukiwania prac ze sprawdzeniem selectów
 * @param url string url do wyszukiwarki
 */
function submitJobofferFormWithCheck(url){
	if ($('SearchCraftId').value != '' || $('SearchRegionId').value != ''){
		url = url + '/1';
	}
	
	if ($('SearchCraftId').value != ''){
		url = url + '/' + $('SearchCraftId').value;
	}else if ($('SearchRegionId').value != ''){
		url = url + '/0';
	}
	if ($('SearchRegionId').value != ''){
		url = url + '/' + $('SearchRegionId').value;
	}
	submitForm(url, "JobofferSearch");
}

/**
 * Wyświetlenie / schowanie diva z sortowaniem
 */
function displaySortBy(){
	var sort_by_menu = $('sort_by_menu');
	if (sort_by_menu.style.display == 'none'){
		$('sort_by').style.overflow = 'visible';
	}else{
		$('sort_by').style.overflow = 'hidden';
	}
	displayElement(sort_by_menu);
}

/**
 * Zaznacza/odznacza wszystkie wiadomości
 * @param messages array lista wszystkich id wiadomości
 * @param status bool zaznaczyc / odznaczyć
 */
function messageSelectAll(messages, status){
	var message_length = messages.length;
	
	for (var i = 0 ; i < message_length; i++){
		$('Message'+messages[i]).checked = status;
	}
}

/**
 * Usunięcie zaznaczonych wiadomości
 */
function deleteSelectedMessages(status){
	$('StatusStatus').value = status;
	$('MessagesChangeStatus').submit();
}

/**
 * Dodanie nowego pliku do bloga
 * Wyświetla nowy input
 */
function blogAddNextFile(){
	$('add_files').appendChild(blog_add_one_file_div.cloneNode(true));
}

/**
 * Wybór województwa w wyszukiwarce pracy
 * @param id int id województwa
 */
function selectProvince2(id){
	$('SearchProvinceId').value = id;
	$('SearchProvinceName').innerHTML = $('province_'+id).innerHTML;
	displayElement($('select_province'));
}

/**
 * Usunięcie awatara
 */
function deleteAvatar(){
	if (confirm('Czy napewno chcesz usunąć?')){
		$('UserDeleteAvatar').value = 1;
		$('user_profile_no_avatar').style.display = 'block';
		$('user_profile_avatar').style.display = 'none';
	}
}

/**
 * Funkcja imitująca selecta
 * @param value mixed wartość wybrana
 * @param div_input_id string diva imitującego inputa (nazwa wybranej opcji)
 * @param prefix string prefiks id diva z wartosćią
 * @param input_id string id inputa (ukrytego)
 * @param select_div_id string id selecta diva
 */
function select(value, div_input_id, prefix, input_id, select_div_id){
	$(input_id).value = value;
	$(div_input_id).innerHTML = $(prefix+value).innerHTML;
	$(select_div_id).style.display = 'none';
}

/**
 * Sprawdza czy podano wszystkie dane w formularzu edycji profilu
 */
function validateUserProfileForm(){
	if ($('UserFirstName').value == ''){
		alert('Proszę podać imię');
		$('UserFirstName').focus();
		return false;
	}
	
	if ($('UserSurname').value == ''){
		alert('Proszę podać nazwisko');
		$('UserSurname').focus();
		return false;
	}
	/*
	if ($('UserPosition').value == ''){
		alert('Proszę podać opis zawodu');
		$('UserPosition').focus();
		return false;
	}
	
	if ($('EntryNameName').value == ''){
		alert('Proszę podać firmę');
		$('EntryNameName').focus();
		return false;
	}
	
	if ($('UserProvinceId').value == 0){
		alert('Proszę wybrać lokalizację');
		return false;
	}
	
	if ($('UserCity').value == ''){
		alert('Proszę podać miejscowość');
		$('UserCity').focus();
		return false;
	}
	
	if ($('UserCraftId1').value == 0 && $('UserCraftId2').value == 0 && $('UserCraftId3').value == 0){
		alert('Proszę wybrać co najmniej jedną branżę');
		return false;
	}	
	*/

	if($('UserShowNick').checked == true){
		if($('UserNick').value == ''){
			alert('Po zaznaczeniu opcji "pokaż jako nazwę profilu", pole "Pseudonim/nick" music być wypełnione');
			return false;
		}
	}
	
	return true;
}

/**
 * Dodanie doświadcznie zawodowego
 * @param url string url dla ajaxa
 * @param div_id string id diva ze wszystkimi historiami pracy
 */
function addNextJobHistory(url, div_id){
	current_job_history++;
	var div = document.createElement('div');
	div.id = 'job_history_'+current_job_history;	
	$(div_id).appendChild(div);
	new Ajax.Updater('job_history_'+current_job_history, url+'/'+current_job_history, {evalScripts: true});
}

/**
 * Dodanie edukacji
 * @param url string url dla ajaxa
 * @param div_id string id diva ze wszystkimi edukacjami
 */
function addEducation(url, div_id){
	current_education++;
	var div = document.createElement('div');
	div.id = 'education_history_'+current_education;	
	$(div_id).appendChild(div);
	new Ajax.Updater('education_history_'+current_education, url+'/'+current_education, {evalScripts: true});
}

/**
 * Sprawdza dlugosc tekstu wprowadzonego do textarea
 * @param text_area_id string id textare'i
 * @param max_length int maksymalna ilość znaków
 * @param length_span string id spana w którego wsadzamy aktualną ilość znaków
 */
function checkDescriptionLength(text_area_id, max_length, length_span){
	if ($(text_area_id).value.length > max_length){
		$(text_area_id).value = ($(text_area_id).value).substr(0, max_length);
	}
	$(length_span).innerHTML = $(text_area_id).value.length;
}

/**
 * Dodanie telefonu w wizytówce firmy
 * @param alert_no_more_phones string tekst alertu
 */
function addPhone(alert_no_more_phones){
	if (max_phones == 0 || max_phones == current_phone){
		alert(alert_no_more_phones);
	}else{
		current_phone++;
		var div = document.createElement('div');
		div.id = 'phone'+current_phone;
		div.className = 'entry_phone';
		div.innerHTML = "<div class='float_left' style='margin-right: 5px;'><input id='"+current_phone+"Phone' name='data[Entry][phones][]' class='input small_account_input'/></div><span class='submit_button' style='float: left; clear: none;' onclick=\"deleteItemImIn(this, 'DIV');current_phone--;\">usuń</span><div class='break_5px'></div>";
		
		$('entry_phones').appendChild(div);
	}
}

/**
 * Dodanie faxu w wizytówce firmy
 * @param alert_no_more_faxes string tekst alertu
 */
function addFax(alert_no_more_faxes){
	if (max_faxes == 0 || max_faxes == current_fax){
		alert(alert_no_more_faxes);
	}else{
		current_fax++;
		var div = document.createElement('div');
		div.id = 'fax'+current_fax;
		div.className = 'entry_fax';
		
		div.innerHTML = "<div class='float_left' style='margin-right: 5px;'><input id='"+current_fax+"Fax' name='data[Entry][faxes][]' class='input small_account_input'/></div><span class='submit_button' style='float: left; clear: none;' onclick=\"deleteItemImIn(this, 'DIV');current_fax--;\">usuń</span><div class='break_5px'></div>";
		
		$('entry_faxes').appendChild(div);
	}
}

/**
 * Dodanie emailu w wizytówce firmy
 * @param alert_no_more_emails string tekst alertu
 */
function addEmail(alert_no_more_emails){
	if (max_emails == 0 || max_emails == current_email){
		alert(alert_no_more_emails);
	}else{
		var div = document.createElement('div');
		div.id = 'email'+(current_email+1);
		div.className = 'entry_email';
		
		var innerHTML = '';
		
		innerHTML = "<div class='float_left' style='margin-right:5px; padding-top: 2px;'><input type='radio' name='data[Email][contact_email]' title='Zaznacz jeśli chcesz aby na ten mail przychodziły pytania z serwisu' ";
		if (current_email == 0){
			innerHTML += " checked='checked' ";
		}
		innerHTML += " value='"+current_email+"'/></div><div class='float_left' style='margin-right: 5px;'><input id='"+current_email+"Email' name='data[Entry][emails][]' class='input user_account_input'/></div><span class='submit_button' style='float: left; clear: none;' onclick=\"deleteItemImIn(this, 'DIV');current_email--;\">usuń</span><div class='break_5px'></div>";
		
		div.innerHTML = innerHTML;
		$('entry_emails').appendChild(div);
		
		current_email++;
	}
}

/**
 * Dodanie strony www w wizytówce firmy
 * @param alert_no_more_emails string tekst alertu
 */
function addWww(alert_no_more_www){
	if (max_www == 0 || max_www == current_www){
		alert(alert_no_more_www);
	}else{
		current_www++;
		var div = document.createElement('div');
		div.id = 'email'+current_www;
		div.className = 'entry_www';
		
		div.innerHTML = "<div class='float_left' style='margin-right: 5px;'><input id='"+current_www+"Www' name='data[Entry][www][]' class='input user_account_input'/></div><span class='submit_button' style='float: left; clear: none;' onclick=\"deleteItemImIn(this, 'DIV');current_www--;\">usuń</span><div class='break_5px'></div>";
		
		$('entry_www').appendChild(div);
	}
}

/**
 * Dodanie odziału firmy
 * @param url string url dla ajaxa
 * @param div_id string id diva ze wszystkimi oddziałami
 */
function addOutlet(url, div_id){
	if (max_outlets == 0 || max_outlets == current_outlet){
		alert(alert_no_more_outlets);
	}else{
		var div = document.createElement('div');
		div.id = 'entry_outlet_'+current_outlet;	
		$(div_id).appendChild(div);
		new Ajax.Updater('entry_outlet_'+current_outlet, url+'/'+current_outlet, {evalScripts: true});
		current_outlet++;
		
		var div_break = document.createElement('div');
		div_break.className = 'break_20px';
		$(div_id).appendChild(div_break);
	}
}

/**
 * Usunięcie oddziału firmy
 * @param id int id oddziału
 */
function deleteOutlet(id){
	if (confirm('Czy napewno chcesz usunąć ten oddział?')){
		var div = document.createElement('div');
		div.innerHTML = "<input type='hidden' name='data[DeleteOutlet][]' value='"+id+"'/>";
		$('deleted_outlets').appendChild(div);
		var div_to_delete = $('entry_outlet_'+id);
		div_to_delete.parentNode.removeChild(div_to_delete);
	}
}

function deleteEntryFile(file_id){
	if (confirm('Czy napewno chcesz usunąć ten plik?')){
		var container = $('current_files_to_delete');
		var new_div = document.createElement('div');
		new_div.id = 'delete_file_'+file_id;
		new_div.innerHTML = "<input type='hidden' name='data[DeleteFile][]' value='"+file_id+"'/>";
		container.appendChild(new_div);
		var div_to_delete = $('file_'+file_id);
		div_to_delete.parentNode.removeChild(div_to_delete);
	}
}

function change_category(poziom, selected_div, category_id, is_leaf){

	selected_div.className = 'add_product_selected';
	
	if (selected_categories[poziom] != null){
		$('category_'+selected_categories[poziom]).className = 'add_product_not_selected';
	}
	
	selected_categories[poziom] = category_id;
	
	if (is_leaf == 1){
		$('ProductProductCategoryId').value = category_id;
		find_num(poziom);
		selected_categories[poziom+1] = null;
		if ($('ProductCategoryName')){
			$('ProductCategoryName').value = $('category_'+category_id).innerHTML;
		}
		if ($('ProductCategoryName')){
			if ($('change_category')){
				$('change_category').style.display = 'none';
			}else if($('categories_info')){
				$('categories_info').style.display = 'none';
			}
		}
		$('add_product_submit').style.display = 'block';
	}else{
		selected_categories[poziom+1] = null;
		find_num(poziom);
		current_category = poziom + 1;
		var div = $('categories_info');
		
		var new_div = document.createElement('div');
		new_div.id = 'product_category_'+current_category;
		new_div.className = 'add_product_select_category';
		
		if (current_category % 3 != 0){
			new_div.style.marginRight = '9px';
		}
		
		new_div.innerHTML = "<table class='table_no_border' style='width:100%;height:100%;'><tr><td class='no_border' style='text-align:center;vertical-align:middle;'><img src='"+url_img_webroot+"ajax-loader.gif'/><br/>Trwa pobieranie danych.</td></tr></table>";
		
		div.appendChild(new_div);
		
		$('ProductProductCategoryId').value = '';
		if ($('ProductCategoryName')){
			$('ProductCategoryName').value = '';
		}
		$('add_product_submit').style.display = 'none';
		
		new Ajax.Updater('product_category_'+current_category, url_products_find_category+category_id+'/'+poziom);
	}
}

function find_num(number){
	
	if (!is_ie){
		number = number + 2;
	}

	var ilosc = $('categories_info').childNodes.length;	

	if (number < $('categories_info').childNodes.length){
		for(var j = $('categories_info').childNodes.length; j > number; j--){
			$('categories_info').removeChild($('categories_info').childNodes[j-1]);
		}
	}
}

function checkProduct(){
	
	if ($('ProductName').value == ''){
		alert(alert_no_product_name);
		return false;
	}
	
	if ($('ProductProductCategoryId').value == ''){
		alert(alert_no_product_category);
		return false;
	}	
	return true;
}

/**
 * Dodanie atrybutu do produktu
 */
function addProductAttribute(){
	var div = document.createElement('div');
	
	var span = document.createElement('span');
	span.innerHTML = 'Nazwa atrybutu:&nbsp;&nbsp;';
	
	div.appendChild(span);
	div.id = current_attribute;
	div.className = 'other_attribute';
	
	var input_1 = document.createElement('input');
	input_1.id = current_attribute+'AttributeName';
	input_1.name = 'data[Product][attribute_name][]';
	input_1.className = 'input message_input';
	
	div.appendChild(input_1);
	
	var span = document.createElement('span');
	span.innerHTML = '&nbsp;&nbsp;&nbsp;Wartość:&nbsp;&nbsp;';
	
	div.appendChild(span);
	
	var input_2 = document.createElement('input');
	input_2.id = current_attribute+'AttributeValue';
	input_2.name = 'data[Product][attribute_value][]';
	input_2.className = 'input small_account_input';
	input_2.size = 10;
	
	div.appendChild(input_2);
	
	$('other_attributes').appendChild(div);
	
	current_attribute++;
	
	var break_div = document.createElement('div');
	break_div.className = 'break_5px';
	$('other_attributes').appendChild(break_div);
}

/**
 * Dodanie pliku do firmy / wizytowki (formularza do dodania)
 * @param url string adres dla ajaxa
 */
function addFile(url){
	current_file++;
	var div = document.createElement('div');
	div.id = 'file_file_'+current_file;	
	$('add_files').appendChild(div);
	new Ajax.Updater('file_file_'+current_file, url+'/'+current_file, {evalScripts: true});
}

//zapisuje informacje o produkcie 
function SubmitFormProduct(someId, someId2, url_product_photos_update_gallery){
	/*if ($('ProductDescription').value.length > description_length){
		alert ('Za długi opis produktu ('+$('ProductDescription').value.length+').Można wpisać '+description_length+' znaków');
		return false;
	}*/
	if ($(someId)){
		var url = url_product_photos_update_gallery;
		var pars = escape(Sortable.serialize(someId));
		pars += '&'+Form.serializeElements(Form.getElements(someId2));
		var target = 'null_container';
		var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: {rates: pars}, asynchronous: false});
	}
	return false;
}

/**
 * Walidacja formularza z dodaniem oferty pracy
 */
function validateJobofferEditForm(){
	tinyMCE.triggerSave(true, true);
	
	if ($('JobofferPosition').value == ''){
		alert('Proszę podać nazwę stanowiska');
		return false;
	}
	
	if ($('JobofferRegionId').value == 0){
		alert('Proszę wybrać lokalizację');
		return false;
	}
	
	if ($('JobofferCraftId').value == 0){
		alert('Proszę wybrać branżę');
		return false;
	}
	
	if ($('cities_autocomplete_input2').value == ''){
		alert('Proszę podać miasto');
		$('cities_autocomplete_input2').focus();
		return false;
	}
	
	if ($('JobofferModified').value == ''){
		alert('Proszę wybrać datę aktywności');
		$('JobofferModified').focus();
		return false;
	}
	
	if ($('JobofferPositionDescription').value == ''){
		alert('Proszę podać opis stanowiska');
		$('JobofferPositionDescription').focus();
		return false;
	}
	
	if ($('JobofferObligations').value == ''){
		alert('Proszę podać zakres obowiązków');
		$('JobofferObligations').focus();
		return false;
	}
	
	if ($('JobofferRequirements').value == ''){
		alert('Proszę podać wymagania');
		$('JobofferRequirements').focus();
		return false;
	}
	
	return true;
}
function selectBookmarkFiltr(url, value){
	$('SearchTypeId').value = value;
	submitForm(url, 'BookmarksSearch');
	return false;
}

/**
 * Walidacja formularza z rejestracją użytkownika
 */
function validateNewUserForm(){	
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if ($('UserFirstName').value == ''){
		alert('Prosze podać imię');
		$('UserFirstName').focus();
		return false;
	}
	
	if ($('UserSurname').value == ''){
		alert('Prosze podać nazwisko');
		$('UserSurname').focus();
		return false;
	}
	
	if ($('UserUsername2').value == ''){
		alert('Proszę podać e-mail');
		$('UserUsername2').focus();
		return false;
	}
	
	if ($('UserPassword1').value == '' || $('UserPassword1').value.length <6){
		alert('Hasło musi składać się z przynajmniej 6 znaków');
		$('UserPassword1').focus();
		return false;
	}
	
	if ($('UserPassword1').value != $('UserPassword2').value){
		alert('Podane hasło są różne');
		return false;
	}
	
	if (regex.test($('UserUsername2').value) == false){
	    alert('Podaj poprawny email');
	    $('UserUsername2').focus();
	    return false;
	}
	
	if ($('UserEntryAdd').checked){
		if ($('EntryName').value == ''){
			alert('Proszę podać nazwę firmy');
			$('EntryName').focus();
			return false;
		}
		if ($('EntryPhone').value == ''){
			alert('Proszę podać numer telefonu');
			$('EntryPhone').focus();
			return false;
		}
		if ($('EntryStreet').value == ''){
			alert('Proszę podać ulicę');
			$('EntryStreet').focus();
			return false;
		}
		if ($('EntryNumber').value == ''){
			alert('Proszę podać numer budynku');
			$('EntryNumber').focus();
			return false;
		}
		if ($('EntryCity').value == ''){
			alert('Proszę podać miejscowość');
			$('EntryCity').focus();
			return false;
		}
		if ($('EntryPostcode').value == ''){
			alert('Proszę podać kod pocztowy');
			$('EntryPostcode').focus();
			return false;
		}
		if($('EntryProvinceId').value == '' || $('EntryProvinceId').value == 0){
			alert('Proszę wybrać województwo');
			return false;
		}
		if($('EntryCategoryId').value == '' || $('EntryCategoryId').value == 0){
			alert('Proszę wybrać kategorię');
			return false;
		}		
		if($('EntryEntryTypeOfBusiness').value == '' || $('EntryEntryTypeOfBusiness').value == 0){
			alert('Proszę wybrać typ działalności');
			return false;
		}
	}
	
	if ($('UserAccept').checked == false){
		alert('Proszę zaakceptować regulamin');
		return false;
	}
	
	return true;
}

/**
 * Walidacja formularza z propozycją aktualności
 */
function validateEventsAddNews(){
	if ($('EventName').value == ''){
		alert('Proszę podać nazwę aktualności');
		$('EventName').focus();
		return false;
	}
	
	if ($('EventCategoryId').value == '' || $('EventCategoryId').value == 0){
		alert('Proszę wybrac kategorię');
		return false;
	}
	
	if ($('EventContent').value == '' && $('EventLink').value == ''){
		alert('Proszę podać opis aktualności lub link');
		$('EventContent').focus();
		return false;
	}
	
	return true;
}

/**
 * Walidacja formularza z propozycją wydarzenia
 */
function validateEventsAddEvent(){
	if ($('EventName').value == ''){
		alert('Proszę podać nazwę wydarzenia');
		$('EventName').focus();
		return false;
	}
	
	if ($('EventDate').value == ''){
		alert('Proszę wybrać datę wydarzenia');
		$('EventDate').focus();
		return false;
	}
	
	if ($('EventContent').value == '' && $('EventLink').value == ''){
		alert('Proszę podać opis wydarzenia lub link');
		return false;
	}
	
	return true;
}

/**
 * Walidacja formularza z edycją referencji 
 */
function validEntryReferencesEditForm(){
	if ($('EntryReferenceName').value == ''){
		alert('Proszę podać firmę');
		$('EntryReferenceName').focus();
		return false;
	}
	
	return true;
}

/**
 * Aktualzuje liste wpisow w widoku firmy
 * @param url string adres dla Ajaxa
 * @param div_id string id diva do ktorego wsadzimy wynik
 */
function updateEntryList(url, div_id){
	$(div_id).innerHTML = 'Pobieranie danych. Proszę czekać...';
	new Ajax.Updater(div_id, url, {evalScripts: true});
}

/**
 * Usunięcie doświadczenia zawodowego
 * @param job_history_id int id doświadczenia
 */
function deleteUserHistory(job_history_id){
	(($('job_history_'+job_history_id)).parentNode).removeChild($('job_history_'+job_history_id));
	
	var div = document.createElement('DIV');
	div.innerHTML = "<input type='hidden' name='data[DeleteUserHistory][]' value='"+job_history_id+"'/>";
	
	$('data_to_delete').appendChild(div);
}

/**
 * Usunięcie edukacji z historii
 * @param education_id int id edukacji
 */
function deleteEducation(education_id){
	(($('education_history_'+education_id)).parentNode).removeChild($('education_history_'+education_id));
	var div = document.createElement('DIV');
	div.innerHTML = "<input type='hidden' name='data[DeleteEducation][]' value='"+education_id+"'/>";
	
	$('data_to_delete').appendChild(div);
}

/**
 * Usunięcie zdjęcia z ogłoszenia
 * @param id int id zdjęcia
 */
function deleteAdvertPhoto(id){
	var div = document.createElement('DIV');
	div.innerHTML = "<input type='hidden' name='data[DeletePhoto][]' value='"+id+"'/>";
	
	$('data_to_delete').appendChild(div);
}

/**
 * Walidacja formularza dodawnia / edycji ogloszenia
 */
function checkAdvertForm(){
	if ($('AdvertisementName').value == ''){
		alert('Proszę podać nazwę');
		$('AdvertisementName').focus();
		return false;
	}
	
	if ($('ProductProductCategoryId').value == ''){
		alert('Proszę wybrać kategorię produktu');
		return false;
	}
	
	if ($('TypeId').value == 0){
		alert('Proszę wybrać typ ogłoszenia');
		return false;
	}
	
	if ($('AdvertisementProvinceId').value == 0){
		alert('Proszę wybrać region');
		return false;
	}
	
	if ($('cities_autocomplete_input2').value == ''){
		alert('Proszę podać miasto');
		$('cities_autocomplete_input2').focus();
		return false;
	}
	
	return true;
}

/**
 * Wyszukiwarka
 * @param search_type int po czym szukać
 */
function searchByType(search_type){
	$('SearchType').value = search_type;
	document.search_big_name_form.submit();
}

function showJobofferSearch(){
	if ($('UserShowJoboffers').checked == true){
		$('searching_joboffers').style.display = 'block';
	}else{
		$('searching_joboffers').style.display = 'none';
	}
}

/**
 * Usunięcie wideo z produktu / firmy
 * @param div_id string id diva do usunięcia
 */
function deleteVideo(div_id){
	if (confirm('Czy napewno chcesz usunąć?')){
		var container = $('current_files_to_delete');
		var new_div = document.createElement('div');
		new_div.id = 'delete_video';
		new_div.innerHTML = "<input type='hidden' name='data[DeleteVideo][]' value='1'/>";
		container.appendChild(new_div);
		var div_to_delete = $(div_id);
		div_to_delete.parentNode.removeChild(div_to_delete);
	}
}

/**
 * Walidacja formularza dodawania / edycji projektów
 */
function validateProjectForm(){
	if ($('ProjectName').value == ''){
		alert('Proszę podać nazwę');
		$('ProjectName').focus();
		return false;
	}
	
	if ($('ProjectDescription').value == ''){
		alert('Proszę wprowadzić opis');
		$('ProjectDescription').focus();
		return false;
	}
	
	if ($('ProjectAuthors').value == ''){
		alert('Proszę podać autorów');
		$('ProjectAuthors').focus();
		return false;
	}
	
	return true;
}
//-------------------Partnerzy---------------------------
	function stopPartners(){
	  clearTimeout(partner_timer_one);
	  clearTimeout(partner_timer_two);
	}

	function startPartners(){
		partner_timer_one = setTimeout("showNextPartner()",1000);
		partner_timer_two = setTimeout("modifyCpPartner()",4000);
	}	

	function stopCountingPartner(td_sel, pict_index, type_){	
	  clearTimeout(partner_timer_one);
	  clearTimeout(partner_timer_two);	
	}	

	function modifyCpPartner(){
		  var img_change_partners = document.getElementById('pcp_'+current_div_partners);
		  Effect.Fade(img_change_partners,{duration:0.5});
		  
		  partner_timer_one = setTimeout("showNextPartner()",1000);
		  partner_timer_two = setTimeout("modifyCpPartner()",4000);
	}

	function showNextPartner(){
	  var img_change_partners = document.getElementById('pcp_'+current_div_partners);
	  
	  if(current_index_partners_ >= counted_partners - 1){
	 	 current_index_partners_ = 0;
	  }else{
	 	 current_index_partners_ = current_index_partners_ + 1;
	  }
	  
	  img_change_partners.innerHTML = partners_array[current_index_partners_];
	  
	  Effect.Appear(img_change_partners,{duration:0.5});
	  
	  if(current_div_partners == partners_licznik * max_partners_on_page){
		  current_div_partners = partners_licznik;
	  }else{
		  current_div_partners = current_div_partners + partners_licznik;
	  }

	}
//-------------------Partnerzy---------------------------
	
/**
 * Zgłaszanie komentarzu do moderacji
 */
function modComment(model, id){
	if (confirm('Czy napewno chcesz zgłosić ten komentarz?')){
		new Ajax.Request(url_mod_comments, {method: 'post', parameters: {model: model, id: id}, onSuccess:function(){alert('Dziękujemy za zgłoszenie');}});
	}
}

/**
 * Walidacja formularza dodawania grupy
 * @return
 */
function validateForumGroupEditForm(){
	if ($('GroupName').value == ''){
		alert('Proszę podać nazwę grupy');
		$('GroupName').focus();
		return false;
	}
	
	if ($('GroupCategoryId').value == 0){
		alert('Proszę wybrać kategorię');
		return false;
	}
	
	return true;
}

function checkEducationAcademyName(key){
	var value = $('UserEducation'+key+'AcademyId').value;
	
	if (value == -1){
		$('user_education_academny_name_'+key).style.display = 'block';
	}else{
		$('user_education_academny_name_'+key).style.display = 'none';
	}
}

function showEntryAccountDetails(id){
	window.open(entry_account_types_about_type_url+'/'+id, '','width=620,height=400,resizable=no,scrollbars=yes,toolbar=no');
}

function runArticleVideo(file_name){
	/*flowplayer('movie_'+file_name, flow_player_link,  { 		         
	    clip: 
	        { 
	            url: video_main_link+file_name,  
	            autoPlay: false,
	            autoBuffering: true,
	            scaling: "fit"
	        }
	});*/
	$('movie_'+file_name).innerHTML = "<object width=\"385\" height=\"240\" data=\""+flow_player_link+"\" type=\"application/x-shockwave-flash\"><param name=\"movie\" value=\""+flow_player_link+"\" /><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param value=\"high\" name=\"quality\"/><param value=\"#000000\" name=\"bgcolor\"/><param name=\"flashvars\" value='config={\"clip\":{\"url\":\""+video_main_link+file_name+"\",\"autoPlay\": false, \"autoBuffering\": false, \"scaling\": \"fit\"}}' /></object>";
}

function show_hidden_description(el_id){
	
	if(showed_description != 0 && showed_description != el_id){
		Effect.Fade(showed_description, { duration: 0.2 });
	}
	
	Effect.toggle($(el_id),'appear',{duration:0.3});
	
	showed_description = el_id;
}

function valid_contact_form(){
	var textarea_content = $('EntryAskContent').value;
	var address = $('EntryAskMail').value;

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	
	if(reg.test(address) == false || textarea_content == '') {
		if (reg.test(address) == false){
			alert('Podaj prawidłowy adres e-mail');
			$('EntryAskMail').focus();
		}else{
			alert('Proszę podać treść zapytania');
			$('EntryAskContent').focus();
		}
		return false;
	}else{
		$('ContactForm').submit();
	}
}


function insert_values_search(){

	var perscount = eval(document.getElementById("ArticlePhrase"));	
	var defperscount = "teksty, ludzie, firmy, produkty ...";
	if(perscount.value == ''){
		perscount.value = defperscount;
	}

}

function clear_value(id, phrase){

	var input_element = eval(document.getElementById(id));

	if( input_element.value == phrase ){
		input_element.value = '';
	}
}

/**
 * Walidacja formularza ustawień konta
 */
function validateUserPropertiesForm(){
	if ($('UserOldPswd').value != ''){
		if ($('UserPassword1').value != $('UserPassword2').value){
			alert('Podane hasła są różne');
			return false;
		}
	}
	
	if ($('UserEntryAdd') && $('UserEntryAdd').checked == true){
		if ($('EntryName').value == ''){
			alert('Proszę podać nazwę firmy');
			$('EntryName').focus();
			return false;
		}
		if ($('EntryPhone').value == ''){
			alert('Proszę podać numer telefonu');
			$('EntryPhone').focus();
			return false;
		}
		if ($('EntryStreet').value == ''){
			alert('Proszę podać ulicę');
			$('EntryStreet').focus();
			return false;
		}
		if ($('EntryNumber').value == ''){
			alert('Proszę podać numer budynku');
			$('EntryNumber').focus();
			return false;
		}
		if ($('EntryCity').value == ''){
			alert('Proszę podać miejscowość');
			$('EntryCity').focus();
			return false;
		}
		if ($('EntryPostcode').value == ''){
			alert('Proszę podać kod pocztowy');
			$('EntryPostcode').focus();
			return false;
		}
		if($('EntryProvinceId').value == '' || $('EntryProvinceId').value == 0){
			alert('Proszę wybrać województwo');
			return false;
		}
		if($('EntryCategoryId').value == '' || $('EntryCategoryId').value == 0){
			alert('Proszę wybrać kategorię');
			return false;
		}
		if($('EntryEntryTypeOfBusiness').value == '' || $('EntryEntryTypeOfBusiness').value == 0){
			alert('Proszę wybrać typ działalności');
			return false;
		}
	}
	
	return true;
}

/**
 * Walidacja formularza pobierania oferty
 */
function validateGetOfferForm(){
	if ($('GetOfferEMail').value == ''){
		alert('Proszę podać adres e-mail');
		$('GetOfferEMail').focus();
		return false;
	}
	
	return true;
}

/**
 * Usunięcie awatara grupy
 */
function deleteGroupAvatar(){
	$('current_avatar').innerHTML = $('avatar_after_delete').innerHTML;
	$('delete_group_avatar').style.display = 'none';
	
	var container = $('delete_group_avatar_container');
	container.innerHTML = "<input type='hidden' name='data[Avatar][delete]' value='1'/>";
}

/**
 * Chowa selecty
 */
function hideSelectedDivs(){
	var divs = document.getElementsByClassName('select');
	var divs_length = divs.length;
	for (i = 0; i < divs_length; i++){
		var div = divs[i];
		if (div.tagName == 'DIV'){
			if (div.id != lastShowedDiv){
				div.style.display = 'none';
			}
		}
	}

	var divs = document.getElementsByClassName('joboffers_select');
	var divs_length = divs.length;
	for (i = 0; i < divs_length; i++){
		var div = divs[i];
		if (div.tagName == 'DIV'){
			if (div.id != lastShowedDiv){
				div.style.display = 'none';
			}
		}
	}

	lastShowedDiv = '';
}

/**
 * Podgląd pisanego posta na forum
 */
function previewPost(){
	if ($('preview_blog_div').style.display == 'none'){
		var position = findPos($('preview_post'));
		var preview_blog_div = $('preview_blog_div');
		preview_blog_div.style.left = (position[0] - 450)+'px';
		preview_blog_div.style.top = (position[1] + 30)+'px';
		preview_blog_div.style.display = 'block';
		$('preview_blog_td').style.display = 'none';
		$('preview_blog_wait').style.display = 'block';
		var pars = Form.serializeElements(Form.getElements('ForumPostsAddForm'));
		new Ajax.Updater('preview_blog_td', preview_post_url, {method: 'post', parameters: {rates: pars}, evalScripts: true, onComplete: function(){
				$('preview_blog_wait').style.display = 'none';
				$('preview_blog_td').style.display = 'block';
			}
		});
	}else{
		$('preview_blog_div').style.display = 'none';
	}
}
/**
 * Umożliwienie wyboru punktu na mapie
 */
function selecteEtryOutletFromMap(outlet_id, x_id, y_id, zoom_id){
	window.open(entry_oulets_select_from_map_url+outlet_id+'/'+x_id+'/'+y_id+'/'+zoom_id, '','width=510,height=400,resizable=yes,scrollbars=yes,toolbar=no');
}

/**
 * Walidacja formularza edycji 
 */
function validateEntryEditForm(){
	if ($('EntryName').value == ''){
		alert('Proszę podać nazwę firmy');
		$('EntryName').focus();
		return false;
	}
	if ($('OutletStreet').value == ''){
		alert('Proszę podać adres firmy');
		$('OutletStreet').focus();
		return false;
	}
	if ($('OutletCity').value == ''){
		alert('Proszę podać miejscowość');
		$('OutletCity').focus();
		return false;
	}
	if ($('OutletPostcode').value == ''){
		alert('Proszę podać kod pocztowy');
		$('OutletPostcode').focus();
		return false;
	}
	if ($('EntryProvinceId').value == 0){
		alert('Proszę wybrać województwo');
		return false;
	}
	if ($('EntryCategoryId').value == 0){
		alert('Proszę wybrać kategorię');
		return false;
	}
	return true;
}

/**
 * Zaproponowanie dodania bloga jako esperckiego
 * @param blog_id int id wpisu
 */
function addBlogToExpert(blog_id){
	if (confirm('Czy napewno chcesz zaproponować ten wpis jako ekspercki?')){
		new Ajax.Request(url_blogs_add_to_expert+blog_id, {onComplete:function(){
					alert('Dziękujemy za zgłoszenie');
					$('add_to_expert').style.display = 'none';
				}
			}
		);
	}
}

/**
 * Dodanie kolejnego usera do listy zaproszonych
 */
function addNextUserToSigned(){
	current_signed_user++;
	
	if ($('user_to_sign_'+current_signed_user)){
		$('user_to_sign_'+current_signed_user).style.display = '';
	}else{
		alert('Nie możesz zaprosić więcej użytkowników');
	}
}

/**
 * Walidacja formularza dodawania wątku
 */
function validateFormTopicsAddForm(){
	if ($('TopicTitle').value ==''){
		alert('Proszę podać tytuł wątku');
		$('TopicTitle').focus();
		return false;
	}
	
	return true;
}

/**
 * Walidacja formularza edycji postu
 */
function validateFormPostEdit(){	
	if ($('PostEditedReason')){
		if ($('PostEditedReason').value ==''){
			alert('Proszę podać powód edycji');
			$('PostEditedReason').focus();
			return false;
		}
	}
	return true;
}

/**
 * Dodanie nowej strony do wpisu na blogu
 */
function addBlogPage(){
	$('BlogAddPage').value = 1;
	$('page_form').submit();
}

function showRightSelect(select, div){
	if ($(select).style.display == 'none'){
		Position.clone($(div), $(select), {
			setHeight: false, 
			setWidth: false			
		});
		$(select).style.display = 'block';
		lastShowedDiv = select;
	}else{
		$(select).style.display = 'none';
	}
}

function runnSkyscraper(){
	$('skyscraper').style.display = 'block';
	Position.clone($('content_for_layout'), $('skyscraper'), {
		setHeight: false, 
		setWidth: false,
		offsetLeft: $('content_for_layout').getWidth() + 10		
	});

	Event.observe(window, 'resize', runSkyscraper);
}

