jQuery.noConflict();

var add_URL = "/index.php?eID=labcalc&a=1";
var delete_URL = "/index.php?eID=labcalc&a=2";
var basketInfo_URL = "/index.php?type=1108";

var dostavka_type = 1;
var dostavka_summ = 0;
var dostavka_square = 0;


jQuery(document).ready(function() {

	jQuery('#aTagPvh').click(function() { openArticle(250); });
	jQuery('#aTagWpacket').click(function() { openArticle(251); });
	jQuery('#aTagVodoot').click(function() { openArticle(272); });
	jQuery('#aTagPdk').click(function() { openArticle(273); });
	jQuery('#aTagPdk1').click(function() { openArticle(274); });
	jQuery('#aTagPdk2').click(function() { openArticle(275); });
	jQuery('#aTagOtk').click(function() { openArticle(276); });


	
	jQuery('.dostavka-square').each(function() {
		dostavka_square += jQuery(this).val()-0;
	});
	recalcDostavka(dostavka_type);
	
	jQuery('#addToBasket').click(function() {
		var hash = {};
		hash = getFormHash('#mainForm');
		jQuery.post(add_URL, hash, function() {
			jQuery.post(basketInfo_URL, {}, function(data) {
				if(jQuery('.ul-okna-2').length > 0) {
					jQuery('.ul-okna-2 li:last').before(data);
				}
				else {
					jQuery('.tx-labcalc-pi1-basketinfo').html(data);
				}
			});
		});
		jQuery(this).slideUp('slow');
		
		
	});
	jQuery('.del').click(function() {
		square = jQuery(this).parent().children('.dostavka-square').attr('value');
		dostavka_square -= square;
		recalcDostavka(dostavka_type);

		product = jQuery(this).parent().children('.product-key').attr('value');
		price = jQuery(this).parent().children('.price').children('span').html();
		total_price = jQuery('#total-price').html();
		jQuery.post(delete_URL, { id: product });
		jQuery(this).parent().parent().parent().hide('slow');
		if((total_price-price) == 0) {
			product_another_url = jQuery('#product-another-url').attr('href');
			jQuery('.tx-labcalc-pi1').html('<h1>Ваш заказ пуст</h1><p><a href="'+product_another_url+'">Рассчитать новое окно</a></p>');
		} 
		jQuery('#total-price').html(total_price - price);
	});
	jQuery('.dostavkaType').click(function() {
		dostavka_type = jQuery(this).val();
		recalcDostavka(dostavka_type);
	});
	jQuery('#dostavkaKM').keyup(function() {
		recalcDostavka(dostavka_type);
	});
});

function recalcDostavka(d)
{
	var dostavka_add = 0;
	var dostavka_km = jQuery('#dostavkaKM').val();

	
	if(dostavka_square-0 > 0) {
		//dostavka_price = 80;
		//dostavka_km_price = 6;
		//dostavka_max_square = 35;

		total_price = jQuery('#total-price').text() - dostavka_summ;

		switch(d) {
			case '0':
				dostavka_summ = 0;
				break;
			case '1':
				dostavka_add = 0;
				dostavka_summ = Math.ceil( dostavka_square / 1000 / 1000 / dostavka_max_square ) * ( dostavka_price + dostavka_add );
				break;
			case '2':
				dostavka_add = dostavka_km * dostavka_km_price;
				dostavka_summ = Math.ceil( dostavka_square / 1000 / 1000 / dostavka_max_square ) * ( dostavka_price + dostavka_add );
				break;
			default:
				break;
		}
		
		

		total_price = total_price - 0 + dostavka_summ;
		
		jQuery('#total-price').text(total_price);
		//return dostavka_type;
	}
	else {
		//jQuery('#basket-dostavka').hide();
		total_price = jQuery('#total-price').text() - dostavka_summ;
		jQuery('#total-price').text(total_price);
	}
}

/*
document.write('<p>baseWprice</p>'); printMatrix2D(baseWprice);
document.write('mprice'); printMatrix2D(mprice);
document.write('baseWwidth'); printMatrix(baseWwidth);
document.write('baseWheight'); printMatrix(baseWheight);
document.write('stww = '+stww);
document.write('stwh = '+stwh);
*/

function printMatrix(obj) {
	document.write('<table>');
	for(x in obj) {
		if(typeof(obj[x])=='number') {
			document.write('<tr><td>'+x+'</td><td>'+obj[x]+'</td></tr>');
		}
	}
	document.write('</table>');
}

function printMatrix2D(obj) {
obj = transpMatrix(obj);
document.write('<table><tr>');
var i=0;
for(x in obj) {
	if(typeof(obj[x])=='object') {
		var j = 0;
		var temp = obj[x];
		for(y in obj[x]) {
			if(typeof(obj[x][y])=='number') {
				if(i==0 && j==0) {
					document.write('<td>&nbsp;</td>');
					for(z in temp) {
						if(typeof(temp[z])=='number') {
							document.write('<td>'+z+'</td>');
						}
					}
					document.write('</tr><tr>');
				}

				if(j==0) {
					document.write('<td>'+x+'</td>');
				}
				document.write('<td>'+obj[x][y]+'</td>');
				
				j++;
			}
		}
		i++;
	}
	document.write('</tr><tr>');
}
document.write('</tr></table>');

}

function transpMatrix(obj) {
	temp = new Array();
	for(x in obj) {
		if(typeof(obj[x])=='object') {
			for(y in obj[x]) {
				if(typeof(obj[x][y])=='number') {
					if(typeof(temp[y])!='object') {
						temp[y] = new Array();
					}
					temp[y][x] = obj[x][y];
				}
			}
		}
	}
	return temp;
}


function baseprice(pvh){
  if(pvh =='rehau'){
    if (wwidth<baseWwidth[0]){
		wwidth = baseWwidth[0];
    }
	else
	{
		if (wwidth>2*baseWwidth[stww-1]-baseWwidth[stww-2])
			wwidth = baseWwidth[stww-1];
		else
		{
			i=0;
			while (i<stww && baseWwidth[i]<=wwidth) i++;
			wwidth = baseWwidth[i-1];
		}
	}

	if (wheight<baseWheight[0])
		wheight = baseWheight[0];
	else
	{
		if (wheight>2*baseWheight[stwh-1]-baseWheight[stwh-2])
			wheight = baseWheight[stwh-1];
		else
		{
			i=0;
			while (i<stwh && baseWheight[i]<=wheight) i++;
			wheight = baseWheight[i-1];
		}
	}
  
  //alert('Высота='+wheight+' Ширина='+wwidth+' Цена'+baseWprice[wheight][wwidth]);
	return baseWprice[wheight][wwidth];
  } 
}

function mosquitoprice()
{
	if (mwidth*mheight==0) return 0;

	return mprice[mheight][mwidth];
}


function alter_pdk(pdk_type)	{
	pdk = document.form.pdk;
	len = pdk.length;
	if(len>0) pdk_size = pdk[pdk.selectedIndex].value;	
	else if(pdk_type=="dsp"&&pdk[pdk.selectedIndex].value=="250")
		pdk.selectedIndex = 1;
		
}




function calcprice() {
	if (document.form.wwidth.value<=0  ||
    document.form.wheight.value<=0 ||
    document.form.mwidth.selectedIndex==-1  ||
    document.form.mheight.selectedIndex==-1 ||
    document.form.wwidth.value-0>baseWwidth[stww-1] ||
    document.form.wheight.value-0>baseWheight[stwh-1] 
    ) {
		//window.alert(document.form.wwidth + ' ' + document.form.wheight + ' ' + baseWwidth[stww-1]+' '+baseWheight[stwh-1]);
		return 0;
	}

  wheight =1.0*document.form.wheight.value 
  wwidth = 1.0*document.form.wwidth.value
  wpacket= 1.0*document.form.wpacket[document.form.wpacket.selectedIndex].value;
  wcolor = 1.0*document.form.wcolor[document.form.wcolor.selectedIndex].value;
  vodoot = 1.0*document.form.vodoot[document.form.vodoot.selectedIndex].value;
  prf    = 1.0*document.form.prf[document.form.prf.selectedIndex].value;
  pvh='rehau';
  	pdk_size=document.form.pdk[document.form.pdk.selectedIndex].value;
	pdk_type=document.form.pdk_type_hidden.value;

  
	mosquito=(document.form.mosquito[document.form.mosquito.selectedIndex].value=="1");
	mwidth = 1.0*document.form.mwidth[document.form.mwidth.selectedIndex].value;
	mheight= 1.0*document.form.mheight[document.form.mheight.selectedIndex].value;

	btype = 1.0*document.form.btype[document.form.btype.selectedIndex].value;
  otktype = 1.0*document.form.otktype[document.form.otktype.selectedIndex].value;
  
  montage = document.form.montage[document.form.montage.selectedIndex].value;
  //dostavka = document.form.dostavka[document.form.dostavka.selectedIndex].value;
  //otk = document.form.otk[document.form.otk.selectedIndex].value;
  
  quantity = 1.0*document.form.quantity[document.form.quantity.selectedIndex].value;

  if(pdk_type=="pvh") {
		pdk_price=0;
		eval(pdk_price_pbh_eval);
	}
	if(pdk_type=="dsp") {
		pdk_price=0;
//		if(pdk_size=="250") {
//			document.form.pdk.selectedIndex = 1;/
//			pdk_size=document.form.pdk[document.form.pdk.selectedIndex].value;
//		}

		eval(pdk_price_dsp_eval);
	}

	eval(montage_price_eval);
	
	eval(otk_price_eval);
  
  	/*switch (btype) {
	case 0: dostavka_price = 0;    break;
	case 1: dostavka_price = 80;   break;
	}*/
  	S = (wwidth/1000)*(wheight/1000);
  
	if (otktype=="1") { 
		OTK = 0; }
	else {
		eval('OTK = '+otk_eval+';');
	}

		
	
	if (mosquito) MP = mosquitoprice(mwidth, mheight);
	else MP = 0;
  
	if (montage=="1") {
		eval('M ='+M_eval+';');
	}
	else {
		M = 0;
	}
	
	/*if (dostavka=="1") D = dostavka_price;
	else D = 0;*/
  
	eval('P1 ='+P1_eval+';');
  
  BP = baseprice(pvh)

  P0 = BP
  
  //P0 = P0*wcolor*wpacket*prf+(wwidth/1000+0.15)*vodoot+(wwidth/1000+0.15)*pdk_price;
  eval('P0 ='+P0_eval+';');

  
	//if (document.form.pvh[document.form.pvh.selectedIndex].value=="standart") P0 = P0*1;
	
  P0 = P0*1;//Стоимость окна скидка 0%
  P1 = P1*1;//Стоимость услуг сидка 0%

  P0 = Math.round(P0);
  P1 = Math.round(P1);
  document.form.price.value = P0*kurs + " р."
  document.form.price1.value = P1*kurs + " р."
  document.form.price2.value = (P0+P1)*quantity*kurs -0 + " р."

}

function getFormHash(formSelector)
{
	var hash = {};
	var inputs = {};
	jQuery(formSelector+' input').each(function() {
		name  = jQuery(this).attr('name');
		value = jQuery(this).val();
		type  = jQuery(this).attr('type');
		
		if(typeof(name)!=='undefined') {
			if(name.substr(name.length-2, 2)=='[]') {
				name = name.substr(0, name.length-2)
				if(typeof(inputs[name])=='undefined') {
					inputs[name]=0;
				}
				else {
					inputs[name]++;
				}
				if(type.toUpperCase() != 'CHECKBOX' || jQuery(this).attr('checked') == true) {
					hash[name+'['+inputs[name]+']'] = value;
				}
			}
			else {
				hash[name] = value;
			}
		}
	});

	jQuery(formSelector+' select').each(function() {
		name 	 = jQuery(this).attr('name');
		multiple = jQuery(this).attr('multiple');
		if(typeof(name)!=='undefined' ) {
			i = 0;
			jQuery(this).children('option').each(function() {
				if(jQuery(this).attr('selected')) {
					value = jQuery(this).val();
					if(multiple==true)
						hash[name+'['+i+']'] = value;
					else
						hash[name] = value;
					i++;
				}
			});
		}
	});

	jQuery(formSelector+' textarea').each(function() {
		name 		= jQuery(this).attr('name');
		value 		= jQuery(this).val();
		hash[name] 	= value;
	});

	return hash;
}

function openArticle(num) {
    window.open('/index.php?id=157&type=1109&c='+num, 'article', 'resizable=no,scrollbar=yes,status=no,width=300px,height=500px,top=100px,left=200px,toolbar=no,location=no');
}
