/* CRM */
function offerDeleteConfirmation(Id){
	if(confirm('Czy na pewno?')){
		window.location=baseUrl+'crm/offers/delete/id/'+Id;
	}
}

/* FRONT */
function offerDeleteConfirmationFront(Id){
	if(confirm('Czy na pewno?')){
		window.location=baseUrl+'user/delete/offer/'+Id;
	}
}


/* credit rate */
function credit_rate(price)
{
	n = 360;
	r = 0.0672;
	m = 12;
	q = 1+(r/m);
				
	rata = (price * Math.pow(q, n) * ((q-1)/((Math.pow(q, n)-1))));
	rata = Math.round(rata);
				
	if (rata == 0 || rata == NaN) {rata = "brak danych";}
				
   	return rata;
}

/* deutsche bank tooltip */

$(function() {
    
	$('#deutschetip').tooltip({
	track: true,
	delay: 0,
	showURL: false,
	fade: 250,
	extraClass: "deutschetip"
	});

});

/* credit pop up div */

function createDiv(div_id) {
	
	var blanket = document.getElementById('blanket');
	blanket.style.display = 'block';
	
 	var divTag = document.getElementById(div_id);
 	divTag.style.display = 'block';
 	divTag.innerHTML = "<div style='float: right;'><a href='#' onclick=hideCreditPopUp('CreditPopUpDiv');><b>X</b></a></div><div style='float: left; font-size: 10px; font-weight: bold; color: #013299; padding-left: 5px;'><b></b></div><div style='float: left;'><iframe width='295' height='465' scrolling='no' src='https://dbdoradcy.pl/domoklik/' frameborder='0'>Niestety Twoja przeglądarka nie obsługuje ramek</iframe></div>";

}
function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	//popUpDiv_height=blanket_height/2-150;//150 is half popup's height
	popUpDiv_height = 200;
	popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-204;//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}
function showCreditPopUp(windowname) {
	
	createDiv(windowname);
	blanket_size(windowname);
	window_pos(windowname);
		
}
function hideCreditPopUp(windowname) {
	
	var el = document.getElementById(windowname);
 	el.style.display = 'none';
	var blanket = document.getElementById('blanket');
	blanket.style.display = 'none';

}

function setCookiesToBack(prod_id, href )
{
   var link = $("#searchListLinkToPageInput").val();
        $.ajax({
                  type: "POST",
                  url: ajaxUrl + 'json/search/set-cookies/id/' + prod_id + '?link=' + encodeURIComponent(link),
                  success: function() {
                        location.href=href;
                  }
                });
                return false;
}

function goBackToSearch(){
    if(BackToSearchSwitch){
        $('body').hide();
        location.href=BackToSearch; return false;
    }
}

function checkHash(){
    if(window.location.hash != '#1' && DoHashCheck){
        DoHashCheck = false;
        goBackToSearch();
    }
}


$(document).ready(function(){
    if(BackToSearchSwitch){
        window.location.hash = '1';
        DoHashCheck = true;
        setInterval("checkHash()", 100);

        loadListSearchResults(CurrentPage);

    }

});

/* ***************************    Lista listy wyszukiwania w widoku ofety*/

function loadListSearchResults(Page){

var Link = SearchResultsLinkShort +'page'+'/'+Page+'/';
$('#searchListResultArea').addClass('mapOpacity');
    $.ajax({
        url: ajaxUrl + 'json/search/get-offer-view-list',
        type: 'POST',
        data: "link="+encodeURIComponent(Link),
        success: function(result) {
                Data = json_parse(result);
                $('#searchListResultArea').html(Data);
                $('#searchListResultArea').removeClass('mapOpacity');
                CurrentPage = Page;
        }
    });

}