var cart_link = "";
var forward_link = "";

function close_modal(reload, id)
{
	var divId = "";
	if (reload == true)
	{
		divId = "divPopupToCart";
	}
	else
	{
		divId = "divPopupNotExists";
	}
	
	if (typeof(id) != "undefined")
	{
		divId = id;
	}
	
	if (reload == true)
	{
		document.getElementById(divId).style.display = "none";
		location.href = cart_link + "&back=" + forward_link;
	}
	else
	{
		document.getElementById(divId).style.display = "none";
	}
}

function show_modal(link, back_link, id, price)
{
	var divId = "divPopupToCart";
	if (link == "")
		divId = "divPopupNotExists";
	
	if (typeof(id) != "undefined")
	{
		divId = id;
	}
	
	if (typeof(price) != "undefined")
	{
		document.getElementById("current_price").innerHTML = price + " руб.";
		document.getElementById("price").value = price + " руб.";
	}
	
	message_height = 200;
    client_height = getClientHeight();
    window_top = (client_height - message_height)/2;
    window_top = getBodyScrollTop() + parseInt(window_top);
    window_top += "px";
    
	cart_link = link;
	forward_link = back_link;
	if (link != "")
	{
		document.getElementById("cart_link").href = cart_link;
		document.getElementById("cart_link_more").href = cart_link + "&back=" + back_link;
	}
	document.getElementById(divId).style.display = "";
	document.getElementById(divId).style.top = window_top;
}

function show_modal_not_exists(product_id)
{
	divId = "divPopupNotExists";
	
	message_height = 400;
    client_height = getClientHeight();
    window_top = (client_height - message_height)/2;
    window_top = getBodyScrollTop() + parseInt(window_top) + 100;
    window_top += "px";
	
	document.getElementById("report_id").value = product_id;
	
	document.getElementById(divId).style.display = "";
	document.getElementById(divId).style.top = window_top;
}

function show_modal_credit(link)
{
	divId = "divPopupToCartCredit";
	
	message_height = 400;
	
	client_height = getClientHeight();
    window_top = (client_height - message_height)/2;
    window_top = getBodyScrollTop() + parseInt(window_top) + 100;
    window_top += "px";
	
	document.getElementById("cart_link_credit").href = link;

	document.getElementById(divId).style.display = "";
	document.getElementById(divId).style.top = window_top;
}

function close_modal_credit()
{
	divId = "divPopupToCartCredit";
	
	document.getElementById(divId).style.display = "none";
}

function getBodyScrollTop()
{
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

var Banners = Array();
var BannersPlaces = Array();

function showBanners()
{
	for (i = 0; i < Banners.length; i++)
	{
		Banners[i].write(BannersPlaces[i]);
	}
}

function sendMyPrice()
{
	var name 	 = document.getElementById("name");
	var proposal = document.getElementById("proposal");
	var argument = document.getElementById("argument");
	var email 	 = document.getElementById("email");
	var phone 	 = document.getElementById("phone");
	var card 	 = document.getElementById("card");
	
	var error 	   = false;
	var error_text = "";
	
	if (phone.value == "")
	{
		error = true;
		error_text = "Вы не ввели свой номер телефона!";
	}
	
	if (email.value == "")
	{
		error = true;
		error_text = "Вы не ввели свой E-mail адрес!";
	}
	/*
	if (!is_email(email.value))
	{
		error = true;
		error_text = "";
	}
	*/
	if (argument.value == "" || argument.value == "Ссылка на сайт конкурентов")
	{
		error = true;
		error_text = "Введите ссылку на сайт с примером\n меньшей цены!";
	}
	/*
	if (!is_argument(argument.value))
	{
		error = true;
		error_text = "";
	}
	*/
	if (proposal.value == "")
	{
		error = true;
		error_text = "Вы не предложили свою цену!";
	}
	
	if (name.value == "")
	{
		error = true;
		error_text = "Вы не ввели своё имя!";
	}
	
	if (error == true)
	{
		alert(error_text);
		return false;
	}
	else
	{
		return true;
	}
}

function argumentChange()
{
	if (document.getElementById("argument").value == "Ссылка на сайт конкурентов")
	{
		document.getElementById("argument").value = "";
		document.getElementById("argument").style.color = "";
	}
}

function argumentBlur()
{
	if (document.getElementById("argument").value == "")
	{
		document.getElementById("argument").value = "Ссылка на сайт конкурентов";
		document.getElementById("argument").style.color = "#888888";
	}
}

function changeCalc(calculator) {
	var buttons = document.getElementsByClassName('button');
	
	for (i = 0; i < buttons.length; i++) {
		buttons[i].className = 'button';
	}
	
	document.getElementById(calculator).className = 'button active';
    
    document.getElementById('calc_credit').style.display = 'none';
    document.getElementById('calc_rassr').style.display = 'none';

    document.getElementById('calc_' + calculator).style.display = 'block';
}

var creditPeriods = {
    6:  {
        min: 10,
        max: 50
    },
    8:  {
        min: 10,
        max: 50
    },
    10: {
        min: 10,
        max: 50
    },
    12: {
        min: 10,
        max: 50
    },
    18:  {
        min: 10,
        max: 50
    },
    24: {
        min: 10,
        max: 50
    },
    36: {
        min: 10,
        max: 50
    }
}

function fillCreditPeriod() {
    var options = '';
    
    document.getElementById('credit_period').innerHTML = '';
    
    for (month in creditPeriods) {
        options += '<option value="' + month + '">' + month + ' месяцев</option>';
    }
    
    document.getElementById('credit_period').innerHTML = options;
    
    fillCreditPercents(document.getElementById('credit_period').value);
}

function fillCreditPercents(period) {
    var options = '';
    
    document.getElementById('credit_percent').innerHTML = '';
    
    for (i = creditPeriods[period]['min']; i <= creditPeriods[period]['max']; i++) {
        options += '<option value="' + i + '">' + i + ' %</option>';
    }
    
    document.getElementById('credit_percent').innerHTML = options;
    
    calcCredit();
}

var rassrPeriods = {
    6:  {
        min: 10,
        max: 50
    },
    8:  {
        min: 25,
        max: 50
    },
    10: {
        min: 35,
        max: 50
    },
    12: {
        min: 50,
        max: 50
    }
}

function fillRassrPeriod() {
    var options = '';
    
    document.getElementById('rassr_period').innerHTML = '';
    
    for (month in rassrPeriods) {
        options += '<option value="' + month + '">' + month + ' месяцев</option>';
    }

    document.getElementById('rassr_period').innerHTML = options;
    
    fillRassrPercents(document.getElementById('rassr_period').value);
}

function fillRassrPercents(period) {
    var options = '';
    
    document.getElementById('rassr_percent').innerHTML = '';
    
    for (i = rassrPeriods[period]['min']; i <= rassrPeriods[period]['max']; i++) {
        options += '<option value="' + i + '">' + i + ' %</option>';
    }
    
    document.getElementById('rassr_percent').innerHTML = options;
    
    calcRassr();
}

function calcCredit() {
    var price = document.getElementById('credit_product_price').value;
    
    var firstPayPercent = document.getElementById('credit_percent').value/100;
    
    var firstPayValue = price*firstPayPercent;
    
    var period = document.getElementById('credit_period').value;
    
    pay = (price - price*firstPayPercent)/period + (price - price*firstPayPercent)*creditPercent/100;
    pay = priceRound(pay);
    
    firstPayValue = priceRound(firstPayValue);
    
    document.getElementById('credit_first_pay_value').innerHTML = firstPayValue;
    document.getElementById('credit_month_pay_value').innerHTML = pay;
}

var discountPrice;

function calcRassr() {
    var price = document.getElementById('rassr_product_price').value*rassrCoeff;
    
    var firstPayPercent = parseInt(document.getElementById('rassr_percent').value)/100;
    
    var period = document.getElementById('rassr_period').value;
    
    var riseInPriceCoeff = period*rassrPercent/100 - period*firstPayPercent*rassrPercent/100 + 1;
    
    discountPrice = parseInt(price/riseInPriceCoeff);
    
    var firstPayValue = priceRound(discountPrice*firstPayPercent);
    
    pay = (discountPrice - discountPrice*firstPayPercent)/period + (discountPrice - discountPrice*firstPayPercent)*rassrPercent/100;
    pay = priceRound(pay);

    firstPayValue = priceRound(firstPayValue);
    
    document.getElementById('rassr_first_pay_value').innerHTML = firstPayValue;
    document.getElementById('rassr_month_pay_value').innerHTML = pay;
}

function setFormValues() {
    if (document.getElementById('calc_credit').style.display == "none") {
        document.getElementById('delay_percentP').value = parseInt(document.getElementById('rassr_percent').value);
        document.getElementById('delay_price').value = discountPrice;
    } else {
        document.getElementById('delay_percentP').value = parseInt(document.getElementById('credit_percent').value);
        document.getElementById('delay_price').value = parseInt(document.getElementById('credit_product_price').value);
    }
}

function priceRound(value) {
    value = parseInt(value);

    if ((value%100) < 50) {
        value = value - (value%100);
    } else {
        value += 100 - (value%100);
    }
    
    return value;
}

function submitCreditForm() {
    setFormValues();
    document.getElementById('credit_form').submit();
}
