/*
* пробегаем по всем инпутам и если они имеют цифровое значение
* то суммируем его, после умножаем на тираж, прибавляем процент тиража
* и отображаем в блоке общей суммы сверху и снизу калькулятора
*/
function calcitems (rate){
	var summ = 0, total = 0;
	$("input").each(
		function(){
			if (!isNaN(this.value) && this.value != '' && this.checked){
				summ = summ + parseFloat(this.value);

			}
			if (!isNaN(jQuery('#suggest').attr('value')) && !isNaN(jQuery('#percent').attr('value')) && !isNaN(jQuery('#suggest').attr('value'))) {
				total = summ.toFixed(2) * parseInt(jQuery('#suggest').attr('value'));
//				total = ((total * parseInt(jQuery('#percent').attr('value')) / 100) + total).toFixed(2);
				total = (total + parseInt(jQuery('#percent').attr('value'))).toFixed(2);
				//total = ((total/5)*rate).toFixed(2);
				total = (total*rate).toFixed(2);
				jQuery('#totalprice').text(total + " грн.");
				jQuery('#totalprice2').text(total + " грн.");
			}
		}
	);
}

function calcandpostitems (){
	var ids = '', tir = '',percent = '', name = '', city = '', email = '', phone = '', message ='', seterror = 0;
	tir = $("#suggest").val();
	percent = $("#percent").val();
	name = $("#name").val();
	city = $("#city").val();
	email = $("#email").val();
	phone = $("#phone").val();
	message = $("#message").val();	
	$("input").each(
		function(){
			if (!isNaN(this.value) && this.value != '' && this.checked){
				if (ids == '') {
					ids = this.id;
				} else {
					ids = ids + ',' + this.id;
				}
			}
		}
	);
	$("#status").html("");
	if (ids == '') {
		$("#status").html("Вы не выбрали ниодной опции<br />");
		seterror = 1;	
	}
	if (name == '') {
		var htmlStr = $("#status").html();
		$("#status").html(htmlStr + "Вы не заполнили поле 'Имя'<br />");
		seterror = 1;
	} 
	if (email == '') {
		var htmlStr = $("#status").html();
		$("#status").html(htmlStr + "Вы не заполнили поле 'E-mail'<br />");
		seterror = 1;
	}
	if (city == '') {
		var htmlStr = $("#status").html();
		$("#status").html(htmlStr + "Вы не заполнили поле 'Город'<br />");
		seterror = 1;
	}
	
	if (seterror == 1) {
		$("#status").animate({opacity: "show"}, 300);
	} else {
		$("#status").animate({opacity: "hide"}, 300);
		$("#sendimg").fadeOut();
		$("#maincalcfrm").slideUp();
		$("#contacts").slideUp();
		$("#inproc").html("<img src='/img/icons/loader.gif' width='24' height='24' alt='loader' />");
		$.post("/calcback", {"arrayids": ids, "tir": tir,"percent": percent, "name": name, "city": city, "email": email, "phone": phone, "message": message}, onSuccess);
		
	}
}
function onSuccess(data)
{
	//$("#contacts").animate({opacity: "hide"}, 300);
	$("#inproc").fadeOut();
	//$("#calc").slideDown();
	$("#success").html(data);
	//$("#success").animate({opacity: "show"}, 300);
	$("#success").slideDown();
}

/*
* если выбран носитель CD диск, прячем всё что связано с DVD
*/
function hidedvd() {
	$("#cdmedia :input").removeAttr("disabled");
	$("#dvdmedia :input").removeAttr("checked");
//	$("#dvdmedia :input").attr({disabled: "disabled"});

	$("#typepack :input").removeAttr("disabled");
	$("input[@name=packpadvd]").removeAttr("checked");
	$("dt[@class=digidvd]").animate({opacity: "hide"}, 300);

	$("input[@name=packaddidcdvd]").removeAttr("checked");
	$("dt[@class=digiсdvd]").animate({opacity: "hide"}, 300);
	$("input[@name=packaddiddvd]").removeAttr("checked");
	$("dt[@class=digirdvd]").animate({opacity: "hide"}, 300);
	$("dt[@class=jdvd]").animate({opacity: "hide"}, 300);
	$("dt[@class=jcd]").animate({opacity: "show"}, 300);
	$("dt[@class=digicd]").animate({opacity: "show"}, 300);
	$("dt[@class=digiccd]").animate({opacity: "show"}, 300);
	$("dt[@class=digird]").animate({opacity: "show"}, 300);
	$("input[id*='addserv_']").removeAttr("disabled");
		
	$("dt[name=cd]").animate({opacity: "show"}, 300);
	$("dt[name=dvd]").animate({opacity: "hide"}, 300);
	
	$("input[name='vklitem']").removeAttr("checked");
	$("input[name='packpa']").removeAttr("checked");
	
}

/*
* если выбран носитель DVD диск, прячем всё что связано с CD
*/
function hidecd(){
	$("#dvdmedia :input").removeAttr("disabled");
	$("#cdmedia :input").removeAttr("checked");
//	$("#cdmedia :input").attr({disabled: "disabled"});

	$("#typepack :input").removeAttr("disabled");
	$("input[@name=packpacd]").removeAttr("checked");
	$("dt[@class=digicd]").animate({opacity: "hide"}, 300);

	$("input[@name=packaddidccd]").removeAttr("checked");
	$("dt[@class=digiсcd]").animate({opacity: "hide"}, 300);

	$("input[@name=packaddidcd]").removeAttr("checked");
	$("dt[@class=digirсd]").animate({opacity: "hide"}, 300);

	$("dt[@class=digidvd]").animate({opacity: "show"}, 300);
	$("dt[@class=digicdvd]").animate({opacity: "hide"}, 300);
	$("dt[@class=digirdvd]").animate({opacity: "hide"}, 300);
	
	$("dt[@class=jcd]").animate({opacity: "hide"}, 300);
	$("dt[@class=jdvd]").animate({opacity: "show"}, 300);
	
	$("input[id*='addserv_']").removeAttr("disabled");
	
	$("dt[name=cd]").animate({opacity: "hide"}, 300);
	$("dt[name=dvd]").animate({opacity: "show"}, 300);	
	
	$("input[name='vklitem']").removeAttr("checked");
	$("input[name='packpa']").removeAttr("checked");
	
		
}

function hidepack() {
	$('#digipack').animate({opacity: "hide"}, 300);
	$('#digiadds').animate({opacity: "hide"}, 300);
	$('#jewelbox').animate({opacity: "hide"}, 300);
	$('#jeweladds').animate({opacity: "hide"}, 300);
	$('#konvert').animate({opacity: "hide"}, 300);
	$('#konvertadds').animate({opacity: "hide"}, 300);
	$('#nonevkl').attr({checked: "checked"});
	$('#existvkl').attr({disabled: "disabled"});
	$('#vkl').animate({opacity: "hide"}, 300);
	$('#vkladds').animate({opacity: "hide"}, 300);
	$("#digipack :input").removeAttr("checked");
	$("#digiadds :input").removeAttr("checked");
	$("#jewelbox :input").removeAttr("checked");
	$("#jeweladds :input").removeAttr("checked");
	$("#konvert :input").removeAttr("checked");
	$("#konvertadds :input").removeAttr("checked");
	$("#vkl :input").removeAttr("checked");
	$("#vkladds :input").removeAttr("checked");

}

function digishow() {
	$('#jewelbox').animate({opacity: "hide"}, 300);
	$('#jeweladds').animate({opacity: "hide"}, 300);
	$('#konvert').animate({opacity: "hide"}, 300);
	$('#konvertadds').animate({opacity: "hide"}, 300);
	$('#digipack').animate({opacity: "show"}, 300);
	$('#digiadds').animate({opacity: "show"}, 300);

	$("#jewelbox :input").removeAttr("checked");
	$("#jeweladds :input").removeAttr("checked");
	$("#konvert :input").removeAttr("checked");
	$("#konvertadds :input").removeAttr("checked");

	//$('#existvkl').removeAttr("disabled");
	$('#nonevkl').attr({checked: "checked"});
	$('#existvkl').attr({disabled: "disabled"});
	$('#vkl').animate({opacity: "hide"}, 300);
	$('#vkladds').animate({opacity: "hide"}, 300);
	$("#vkl :input").removeAttr("checked");
	$("#vkladds :input").removeAttr("checked");
	
}

function jewelshow() {
	$('#digipack').animate({opacity: "hide"}, 300);
	$('#digiadds').animate({opacity: "hide"}, 300);
	$('#konvert').animate({opacity: "hide"}, 300);
	$('#konvertadds').animate({opacity: "hide"}, 300);
	$('#jewelbox').animate({opacity: "show"}, 300);
	$('#jeweladds').animate({opacity: "show"}, 300);
	$('#existvkl').removeAttr("disabled");
	$("#digipack :input").removeAttr("checked");
	$("#digiadds :input").removeAttr("checked");
	$("#konvert :input").removeAttr("checked");
	$("#konvertadds :input").removeAttr("checked");
}

function konvertshow() {
	$('#digipack').animate({opacity: "hide"}, 300);
	$('#digiadds').animate({opacity: "hide"}, 300);
	$('#jewelbox').animate({opacity: "hide"}, 300);
	$('#jeweladds').animate({opacity: "hide"}, 300);
	$('#konvert').animate({opacity: "show"}, 300);
	$('#konvertadds').animate({opacity: "show"}, 300);
	$('#nonevkl').attr({checked: "checked"});
	$('#existvkl').attr({disabled: "disabled"});
	$('#vkl').animate({opacity: "hide"}, 300);
	$('#vkladds').animate({opacity: "hide"}, 300);
	
	$("input[name='packpa']").removeAttr("checked");
	$("input[name='packaddidccd']").removeAttr("checked");
	$("#konvert :input").removeAttr("checked");
	$("#konvertadds :input").removeAttr("checked");
	$("#vkl :input").removeAttr("checked");
	$("#vkladds :input").removeAttr("checked");
	
}

function showvkl() {
	$('#vkl').animate({opacity: "show"}, 300);
	$('#vkladds').animate({opacity: "show"}, 300);
	$("input[name*='vklitem']").removeAttr("disabled");
	if ( $("#cdmedia > dt > input[id#='cd_']:checked").val() )
	{
		$("input[name*='vklitem']:parent").hide();
		$("dt[name=cd]").animate({opacity: "show"}, 300);
		$("dt[name=dvd]").animate({opacity: "hide"}, 300);
		if ( $("input[name=packpa]:checked").val() )
		{
			var type = $("input[name=packpa]:checked").attr("rel");
			$("dt[name=cd]").animate({opacity: "hide"}, 300);
			$("dt[name=cd][class='vklitems'][rel='"+type+"']").animate({opacity: "show"}, 300);
		}
	}
	else if ( $("#dvdmedia > dt > input[id#='dvd_']:checked").val() )
	{
		$("input[name*='vklitem']:parent").hide();
		$("dt[name=cd]").animate({opacity: "hide"}, 300);
		$("dt[name=dvd]").animate({opacity: "show"}, 300);
		if ( $("input[name=packpa]:checked").val() )
		{
			var type = $("input[name=packpa]:checked").attr("rel");
			$("dt[name=dvd]").animate({opacity: "hide"}, 300);
			$("dt[name=dvd][class='vklitems'][rel='"+type+"']").animate({opacity: "show"}, 300);
		}
		
	}
	
}

function hidevkl() {
	$('#vkl').animate({opacity: "hide"}, 300);
	$('#vkladds').animate({opacity: "hide"}, 300);
	$("input[name='vklitem']").removeAttr("checked");
}

function hideaddserv() {
	$("#addserv :input").removeAttr("checked");
	$("#addserv :input").attr({disabled: "disabled"});
}

function showaddserv() {
	$("#addserv :input").removeAttr("disabled");
}

function vklchange(type){
	$("input[name='vklitem']").removeAttr("checked");
	if ( $("#cdmedia > dt > input[id#='cd_']:checked").val() )
	{
		$("input[name*='vklitem']:parent").hide();
		$("dt[name=cd]").animate({opacity: "show"}, 300);
		$("dt[name=dvd]").animate({opacity: "hide"}, 300);
		if ( $("input[name=packpa]:checked").val() )
		{
			var type = $("input[name=packpa]:checked").attr("rel");
			$("dt[name=cd]").animate({opacity: "hide"}, 300);
			$("dt[name=cd][class='vklitems'][rel='"+type+"']").animate({opacity: "show"}, 300);
		}
	}
	else if ( $("#dvdmedia > dt > input[id#='dvd_']:checked").val() )
	{
		$("input[name*='vklitem']:parent").hide();
		$("dt[name=cd]").animate({opacity: "hide"}, 300);
		$("dt[name=dvd]").animate({opacity: "show"}, 300);
		if ( $("input[name=packpa]:checked").val() )
		{
			var type = $("input[name=packpa]:checked").attr("rel");
			$("dt[name=dvd]").animate({opacity: "hide"}, 300);
			$("dt[name=dvd][class='vklitems'][rel='"+type+"']").animate({opacity: "show"}, 300);
		}
		
	}
}