var xsize;
var ysize;
var tpaper;
var tprint;
var quantity;
var colors;
var fullcolors= new Array;
var shtrih1=new Array;
var shtrih2=new Array;
var shtrih3=new Array;
var shtrih4=new Array;
var shtrih50=new Array;
var shtrih50below=new Array;

function initvalue(){
	fullcolors[0]=new Array ('100000','0.99','1.17','1.32','1.43','1.20','1.35');
	fullcolors[1]=new Array ('4000','1.10','1.25','1.40','1.51','1.28','1.43');
	fullcolors[2]=new Array ('2500','1.16','1.31','1.47','1.58','1.34','1.49');
	fullcolors[3]=new Array ('1750','1.28','1.44','1.59','1.70','1.47','1.62');
	fullcolors[4]=new Array ('1250','1.34','1.50','1.66','1.76','1.53','1.68');
	fullcolors[5]=new Array ('900','1.40','1.58','1.74','1.85','1.61','1.76');
	fullcolors[6]=new Array ('700','1.45','1.65','1.80','1.91','1.67','1.83');
	fullcolors[7]=new Array ('550','1.50','1.70','1.85','1.96','1.73','1.88');
	fullcolors[8]=new Array ('450','1.55','1.74','1.89','2.00','1.76','1.92');
	fullcolors[9]=new Array ('350','1.90','2.12','2.27','2.38','2.14','2.30');
	fullcolors[10]=new Array ('250','2.20','2.66','2.81','2.92','2.68','2.84');
	fullcolors[11]=new Array ('175','2.50','3.02','3.17','3.28','3.04','3.20');
	fullcolors[12]=new Array ('125','3.00','3.47','3.62','3.73','3.49','3.68');
	
	shtrih1[0]=new Array('10000','1.40','1.62','1.77','1.88','1.65','1.80'); 
	shtrih1[1]=new Array('250','1.59','1.80','1.95','2.06','1.83','1.98');
	shtrih1[2]=new Array('150','1.79','1.98','2.13','2.24','2.01','2.16'); 

	shtrih2[0]=new Array('10000','1.50','1.71','1.86','1.97','1.74','1.89');
	shtrih2[1]=new Array('250','1.69','1.89','2.04','2.15','1.92','2.07');
	shtrih2[2]=new Array('150','1.90','2.07','2.22','2.33','2.10','2.25');

	shtrih3[0]=new Array('10000','1.59','1.80','1.95','2.06','1.83','1.98');
	shtrih3[1]=new Array('250','1.79','1.98','2.13','2.24','2.01','2.16');
	shtrih3[2]=new Array('150','2.00','2.16','2.31','2.42','2.19','2.34');

	shtrih4[0]=new Array('10000','1.69','1.89','2.04','2.15','1.92','2.07');
	shtrih4[1]=new Array('250','1.90','2.07','2.22','2.33','2.10','2.25');
	shtrih4[2]=new Array('150','2.10','2.34','2.49','2.60','2.37','2.52');
	
	shtrih50[0]=new Array('108','112.5','126','130.5','112.5');
	shtrih50[1]=new Array('135','139.5','157.5','162','139.5');
	shtrih50[2]=new Array('180','184.5','202.5','207','184.5');
	shtrih50[3]=new Array('198','202.5','220.5','225','202.5');
	
	shtrih50below[0]=new Array('89.1','93.6','11.6','117','93.6 ');
	shtrih50below[1]=new Array('108','112.5','130.5','135','112.5');
	shtrih50below[2]=new Array('126','130.5','148.5','135','130.5');
	shtrih50below[3]=new Array('144','148.5','16625','171','148.5');
}
function check(input) {
  var ok = true;
  var check1= new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
  for (var i = 0; i < input.length; i++) {
    var chr = input.charAt(i);
    var found = false;
    for (var j = 0; j <= check1.length; j++) {
      if (chr == check1[j]) found = true;
    }
    if (!found) ok = false;
  }
  return ok;
}
function verifyfields() {
	var lenpaper = 6;
	var form=document.calcul;
    var ind1;
	var ind2;
	var bool=0;
	if (!form.tprint[0].checked&&!form.tprint[1].checked){
		window.alert('Выберите тип печати');return false
	};
	if (form.tprint[0].checked){tprint=1}
	else if (form.tprint[1].checked){tprint=2}
	
    for (var i=0; i<lenpaper; i++){
        if(form.tpaper[i].checked){bool=1;tpaper=i+1;}
	}
	if (!bool){window.alert('Выберите тип бумаги');return false};
	
	if (!form.quantity.value||form.quantity.value==0||!check(form.quantity.value)) {window.alert('Введите количество этикеток');form.quantity.focus();form.quantity.select();return false}
	
	if (!form.xsize.value||form.xsize.value==0||!check(form.xsize.value)){window.alert('Введите правильный размер этикетки');form.xsize.focus();form.xsize.select();return false}
	if(!form.ysize.value||form.ysize.value==0||!check(form.ysize.value)){window.alert('Введите правильный размер этикетки');form.ysize.focus();form.ysize.select();return false}
	
	colors=form.colors.options[form.colors.selectedIndex].value;
	quantity=form.quantity.value;
	xsize=form.xsize.value;
	ysize=form.ysize.value;
	return 1;
}

function calculate() {
//	alert(' - '+tprint);
//	alert(' - '+tpaper);
//	alert(' - '+xsize+'X'+ysize);
//	alert(' - '+quantity);
//	alert('colors - '+colors);
	var form=document.calcul;
	var square;
	var pricepermeter;
	var price;
	square=xsize*ysize*quantity/1000000;
	if (square>5000){window.alert('Тираж имеет площадь более 5000кв.м.('+parseInt(square)+'). Расчет стоимости приблизителен. Для более точной информации обращайтесь к нашим менеджерам по телефонам или в офис.')}
//	alert('square: '+square);
	
	if (tprint==1){
		var i;
		for (i=fullcolors.length-1; i>=0; i--){
			if (square<=fullcolors[i][0]){
				//window.alert(fullcolors[i][0]+'---'+square);
				form.price.value=parseInt(fullcolors[i][tpaper]*square*100)/100;
				form.priceone.value=parseInt(fullcolors[i][tpaper]*square/quantity*10000000)/10000000;
				//form.square.value=parseInt(square*100)/100;	
				break
			}
		}
	} else if (square>75) {
		var shtrih=new Array;
		if (colors==1){shtrih=shtrih1}
		else if (colors==2){shtrih=shtrih2}
		else if (colors==3){shtrih=shtrih3}
		else {shtrih=shtrih4}
		var i;
		for (i=shtrih.length-1; i>=0; i--){
			if (square<shtrih[i][0]){
				if (square<600){
					form.price.value=parseInt(shtrih[i][tpaper]*square*100)/100;
					form.priceone.value=parseInt(shtrih[i][tpaper]*square/quantity*10000000)/10000000;
				} else {
					var koef=Math.pow(0.9,parseInt(Math.log(parseInt(square/300))/Math.log(2)));
					form.price.value=parseInt(shtrih[i][tpaper]*square*koef*100)/100;
					form.priceone.value=parseInt(shtrih[i][tpaper]*square*koef/quantity*10000000)/10000000;
				}
//				form.square.value=parseInt(square*100)/100;	
				break;
			}
		}
	} else if (square>=50){
//			form.square.value=parseInt(square*100)/100;
			form.price.value=shtrih50[colors-1][tpaper-1];
			form.priceone.value=parseInt(shtrih50[colors-1][tpaper-1]/quantity*10000000)/10000000;			
			//form.priceone.value=parseInt(shtrih50below[
	} else {
//			form.square.value=parseInt(square*100)/100;
			form.price.value=shtrih50below[colors-1][tpaper-1];
			form.priceone.value=parseInt(shtrih50below[colors-1][tpaper-1]/quantity*10000000)/10000000;
	}
	
}

initvalue();

