var OP=(navigator.userAgent.indexOf('Opera')!=-1);
var IE=(navigator.userAgent.indexOf('MSIE')!=-1 && !OP);
var MZ=(navigator.userAgent.indexOf('Gecko')!=-1);
var IE6=(IE && navigator.userAgent.indexOf('MSIE 6')!=-1);

function getWindowW() { return (IE)?document.documentElement.clientWidth:window.innerWidth; }
function getWindowH() { return (IE)?document.documentElement.clientHeight:window.innerHeight; }
function getDocumentW() { return document.body.offsetWidth; }
function getDocumentH() { return document.body.offsetHeight; }
function getScrollX() { return (document.documentElement.scrollLeft)?document.documentElement.scrollLeft:document.body.scrollLeft; }
function getScrollY() { return (document.documentElement.scrollTop)?document.documentElement.scrollTop:document.body.scrollTop; }
function getScrollW() { return document.documentElement.scrollWidth; }
function getScrollH() { return document.documentElement.scrollHeight; }

var Monthes=new Array(
    'бокабрь', 
    'январь', 
    'февраль', 
    'март', 
    'апрель', 
    'май', 
    'июнь', 
    'июль', 
    'август', 
    'сентябрь', 
    'октябрь', 
    'ноябрь', 
    'декабрь'
);
var Steps=new Array(
    null,
    'Потери тепла в вашем доме',
    'Рассчитайте экономию',
    'Параметры дома',
    'Утепление кровли',
    'Утепление стен',
    'Утепление пола',
    'Рекомендации и расчеты',
    'Перейти на основной сайт'
);
var StepsH=new Array(
    0,
    574,
    390,
    460,
    560,
    500,
    430,
    570,
    null
);
var StepsNC=new Array(
    0,
    -174,
    0,
    0,
    -70,
    0,
    0,
    0,
    null
);

var Titles=new Array(
    null,
    'Потери тепла в вашем доме',
    'Выберите регион постройки',
    'Параметры дома',
    'Выбор толщины утеплителя <span class="small">(кровля)</span>',
    'Выбор толщины утеплителя <span class="small">(стены)</span>',
    'Выбор толщины утеплителя <span class="small">(пол)</span>',
    'Рекомендации и расчеты'
);
var Grids=[];
var SubSteps=[];
var stepCurrent=0;
var sData=[];
/*{
    'Region': 0,
    'HouseH': 0,
    'HouseHF': 0,
    'HouseW': 0,
    'HouseL': 0,
    'Temperature': 0,
    'Roof': '',
    'Roof_outdoor': 0,
    'Roof_indoor': 0,
    'RoofSize_outdoor': 0,
    'RoofSize_indoor': 0,
    'Wall': '',
    'Wall_vented': 0,
    'Wall_brick': 0,
    'WallSize_vented': 0,
    'WallSize_brick': 0,
    'Floor': '',
    'Floor_joist': 0,
    'Floor_cement': 0,
    'FloorSize_joist': 0,
    'FloorSize_cement': 0,
    'Power': '',
    'Power_electro': 0,
    'Power_gas': 0
};*/

function encodeStr(str) {
    var _r=encodeURI(str).replace(/\+/g,'%2B').replace(/\&/g,'%26');
    return _r;
}

var mapHintTimer=0;
var mapHintTimeout=50;
function mapHintShow(obj) {
    mapHintProlong();
    var hintObj=$('MapHint');
    $('MapHintText').innerHTML=obj.getAttribute('rtitle');
    hintObj.style.visibility='hidden';
    hintObj.style.display='';
    var baseL=obj.offsetLeft;
    var baseT=obj.offsetTop;
    var parentW=hintObj.parentNode.offsetWidth;
    
    var pTop=baseT-hintObj.offsetHeight;
    var pLeft=baseL+7-hintObj.offsetWidth/2;
    var aLeft=hintObj.offsetWidth/2-3;
    var lc=8;
    if (pLeft < -lc) { 
        aLeft-=(-lc - pLeft);
        pLeft=-lc; 
    }
    else if (pLeft > parentW-hintObj.offsetWidth+lc) {
        aLeft+=(parentW-hintObj.offsetWidth+lc - pLeft);
        pLeft = parentW-hintObj.offsetWidth+lc;
    }
    
    hintObj.style.left=pLeft+'px';
    hintObj.style.top=pTop+'px';
    $('MapHintArrow').style.left=aLeft+'px';
    
    hintObj.style.visibility='';
}
function mapHintProlong() {
    clearTimeout(mapHintTimer);
}
function mapHintHide() {
    mapHintProlong();
    mapHintTimer=setTimeout('mapHintClose()',mapHintTimeout);
}
function mapHintClose() {
    $('MapHint').style.display='none';
    $('MapHintText').innerHTML='';
}

function showStepType(stepName,type) {
    var oldType=sData[stepName];
    if (oldType!=type) {
        if ($(stepName+'Image')) { $(stepName+'Image').className=type; }
        if ($(stepName+'Line')) { $(stepName+'Line').className='line '+type; }
        for (i=0;i<SubSteps[stepName].length;i++) {
            $(stepName+'Button_'+SubSteps[stepName][i]).className='button';
            if ($(stepName+'Type_'+SubSteps[stepName][i])) {
                $(stepName+'Type_'+SubSteps[stepName][i]).style.display='none';
            }
            if ($(stepName+'Div_'+SubSteps[stepName][i])) {
                $(stepName+'Div_'+SubSteps[stepName][i]).style.display='none';
            }
            if ($('Final'+stepName+'Type_'+SubSteps[stepName][i])) {
                $('Final'+stepName+'Type_'+SubSteps[stepName][i]).style.display='none';
            }
        }
        if ($(stepName+'Button_'+type)) {
            $(stepName+'Button_'+type).className='button select';
        }
        if ($(stepName+'Type_'+type)) {
            $(stepName+'Type_'+type).style.display='';
        }
        if ($(stepName+'Div_'+type)) {
            $(stepName+'Div_'+type).style.display='';
        }
        if ($('Final'+stepName+'Type_'+type)) {
            $('Final'+stepName+'Type_'+type).style.display='';
        }
        sData[stepName]=type;
    }
}


function selectRegion(rid) {
    // помечаем выбранный регион
    var cities=$('MapCities').getElementsByTagName('img');
    for (i=0;i<cities.length;i++) {
        cities[i].className='';
    }
    
    // собираем инфу о регионе
    $('RegionTitle').innerHTML=Regions[rid].title;
    $('RegionTitle').nextSibling.style.backgroundImage='url("'+Regions[rid].map+'")';
    $('RegionContainer').style.display='none';
    var tempTable=$('RegionTemperatures');
    while (tempTable.rows[0]) { tempTable.deleteRow(0); }
    for (i=1;i<=12;i++) {
        tempTable.insertRow(i-1);
        tempTable.rows[i-1].insertCell(0);
        tempTable.rows[i-1].insertCell(1);
        tempTable.rows[i-1].cells[0].className='month_title';
        tempTable.rows[i-1].cells[0].innerHTML=Monthes[i];
        tempTable.rows[i-1].cells[1].innerHTML=Regions[rid].temps[i];
        if (Regions[rid].temps[i]-Math.ceil(Regions[rid].temps[i])==0) {
            tempTable.rows[i-1].cells[1].innerHTML+='.0';
        }
    }
    
    sData['Region']=rid;
    $('UMapCity_'+rid).className='select';
    
    if ($('MapContainer').className!='w66') {
        // двигаем карту
        mapMove();
    }
    else {
        $('RegionContainer').style.display='';
        stepCheck(1);
    }
    
}
var mapMoveTimout = 5; // 10
var mapMoveStep = 40; // 1
function mapMove() {
    var W=$('MapContainer').parentNode.offsetWidth*0.66;
    var w=$('MapContainer').offsetWidth-mapMoveStep;
    if (w>W) {
        $('MapContainer').style.width=w+'px';
        setTimeout('mapMove()',mapMoveTimout);
    }
    else {
        $('MapContainer').className='w66';
        $('MapContainer').style.width='';
        $('RegionContainer').style.display='';
        stepCheck(1);
    }
    
}

var navigatorMoveTimout = 20; // 10
var navigatorMoveStep = 10; // 1
function navigatorHide(num) {
    var curBot=-($('StepBack').offsetTop+$('StepBack').offsetHeight-$('StepBack').parentNode.offsetHeight);
    if (curBot > -40) {
        $('StepBack').style.bottom=$('StepNext').style.bottom=(curBot-navigatorMoveStep)+'px';
        setTimeout('navigatorHide('+num+')',navigatorMoveTimout);
    }
    else {
        stepShow(num,2);
    }
}
function navigatorShow(num) {
    var curBot=-($('StepBack').offsetTop+$('StepBack').offsetHeight-$('StepBack').parentNode.offsetHeight);
    if (curBot < 0) {
        $('StepBack').style.bottom=$('StepNext').style.bottom=(curBot+navigatorMoveStep)+'px';
        setTimeout('navigatorShow('+num+')',navigatorMoveTimout);
    }
    else {
        stepShow(num,5);
    }
}
function navigatorSet(num) {
    var backTxt=Steps[num-1];
    var nextTxt=Steps[num+1];
    if (backTxt) {
        $('StepBack').getElementsByTagName('span').item(1).innerHTML=backTxt;
        $('StepBack').style.visibility='';
    }
    else {
        $('StepBack').getElementsByTagName('span').item(1).innerHTML='';
        $('StepBack').style.visibility='hidden';
    }
    if (nextTxt) {
        var spans=$('StepNext').getElementsByTagName('span');
        if (num<7) {
            spans.item(1).innerHTML=nextTxt;
            spans.item(0).style.display='';
            $('StepNext').getElementsByTagName('a').item(0).style.display='none';
        }
        else {
            spans.item(0).style.display='none';
            $('StepNext').getElementsByTagName('a').item(0).style.display='';
            spans.item(3).innerHTML=nextTxt;
        }
        $('StepNext').style.visibility='';
    }
    else {
        $('StepNext').getElementsByTagName('span').item(1).innerHTML='';
        $('StepNext').style.visibility='hidden';
    }
    stepShow(num,4);
}

function stepShow(num,act) {
    if (act==undefined) { act=1; }
    switch (act) {
        case 1: { 
            if (stepInProcess) { return; }
            stepInProcess=true;
            navigatorHide(num); 
            break; 
        }
        case 2: { 
            if (!stepInProcess) { return; }
            if (num==7) {
                calculationProcess('show');
            }
            if (StepsH[num]>StepsH[stepCurrent]) {
                $('DivBody').style.height=StepsH[num]+'px';
                bodyMakeTop(num);
            }
            $('SPrint').style.display='none';
            $('DivNavigator').style.top=0+'px';
            stepMove(num);
            break; 
        }
        case 3: {
            if (!stepInProcess) { return; }
            stepSet(num);
            stepCheck(num);
            navigatorSet(num);
            break;
        }
        case 4: {
            if (!stepInProcess) { return; }
            if (StepsH[num]<StepsH[stepCurrent]) {
                $('DivBody').style.height=StepsH[num]+'px';
                bodyMakeTop(num);
            }
            $('DivNavigator').style.top=StepsNC[num]+'px';
            navigatorShow(num);
            break;
        }
        case 5: {
            if (!stepInProcess) { return; }
            stepCurrent=num;
            stepInProcess=false;
            if (num==7) {
                calculationProcess('send');
            }
            break;
        }
    }
    
}
var stepInProcess=false;
var stepMoveTimout = 5; // 5
var stepMoveStep = 80; //20
function stepMove(num) {
    var f=false;
    if (stepCurrent<num) {
        // движение вперед
        if ($('Step'+num).offsetLeft > 0) { f=true; }
        if (num > 1) {
            if ($('Step'+(num-1)).offsetLeft > -$('Step'+(num-1)).offsetWidth) { f=true; }
        }
        if (f){
            if ($('Step'+num).offsetLeft > 0) {
                var nl=$('Step'+num).offsetLeft-stepMoveStep;
                $('Step'+num).style.left=((nl>=0)?nl:0)+'px';
            }
            if (num > 1) {
                if ($('Step'+(num-1)).offsetLeft > -$('Step'+(num-1)).offsetWidth) { 
                    var nl=$('Step'+(num-1)).offsetLeft-stepMoveStep;
                    $('Step'+(num-1)).style.left=((nl>=-$('Step'+(num-1)).offsetWidth)?nl:-$('Step'+(num-1)).offsetWidth)+'px';
                }
            }
            setTimeout('stepMove('+num+')',stepMoveTimout);
        }
        else {
            if (num > 1) { $('Step'+(num-1)).style.left='-100%'; }
            stepShow(num,3)
        }
    }
    else {
        // движение назад
        if ($('Step'+num).offsetLeft < 0) { f=true; }
        if (num < Steps.length-2) {
            if ($('Step'+(num+1)).offsetLeft < $('Step'+(num+1)).offsetWidth) { f=true; }
        }
        if (f){
            if ($('Step'+num).offsetLeft < 0) {
                var nl=$('Step'+num).offsetLeft+stepMoveStep;
                $('Step'+num).style.left=((nl<=0)?nl:0)+'px';
            }
            if (num < Steps.length-2) {
                if ($('Step'+(num+1)).offsetLeft < $('Step'+(num+1)).offsetWidth) { 
                    var nl=$('Step'+(num+1)).offsetLeft+stepMoveStep;
                    $('Step'+(num+1)).style.left=((nl<=$('Step'+(num+1)).offsetWidth)?nl:$('Step'+(num+1)).offsetWidth)+'px';
                }
            }
            setTimeout('stepMove('+num+')',stepMoveTimout);
        }
        else {
            if (num < Steps.length-2) { $('Step'+(num+1)).style.left='100%'; }
            stepShow(num,3)
        }
    }
}
function stepCheck(num) {
    switch (num) {
        case 1: {
            $('StepNext').getElementsByTagName('span').item(0).className='active';
            break;
        }
        case 2: {
            if (sData['Region']>0) {
                $('StepNext').getElementsByTagName('span').item(0).className='active';
            }
            else {
                $('StepNext').getElementsByTagName('span').item(0).className='inactive';
            }
            break;
        }
        case 3: {
            $('StepNext').getElementsByTagName('span').item(0).className='active';
            break;
        }
        case 4: {
            $('StepNext').getElementsByTagName('span').item(0).className='active';
            break;
        }
        case 5: {
            $('StepNext').getElementsByTagName('span').item(0).className='active';
            break;
        }
        case 6: {
            $('StepNext').getElementsByTagName('span').item(0).className='active';
            break;
        }
        
    }
}
function stepSet(num) {
    $('StepCurrent').innerHTML=num;
    $('StepTitle').innerHTML=Titles[num];
}
function stepNext() {
    if ($('StepNext').getElementsByTagName('span').item(0).className!='active') { return; }
    if (stepCurrent<Steps.length-2) {
        stepShow(stepCurrent+1,1);
    }
}
function stepBack() {
    if (stepCurrent>1) {
        stepShow(stepCurrent-1,1);
    }
}



function setIsoverComment(sel,key) {
    var val=sel.value;
    $(key+'Comment').innerHTML=Isovers[val].comment;
    sData[key]=Number(val);
    if (sel.id==key) { if ($('Final'+key)) { $('Final'+key).value=val; } }
    else { $(key).value=val; }
}



function gridMake(gid) {
    // заполняем значения
    var i=Grids[gid].min;
    var scaleBox=$(gid+'Numbers');
    while (i<=Grids[gid].max) {
        var nc=document.createElement('div');
        nc.className='c';
        nc.innerHTML=i;
        scaleBox.appendChild(nc);
        if (i%Grids[gid].gridstep == 0) { i+=Grids[gid].gridstep; }
        else { i+=Grids[gid].gridstep-i%Grids[gid].gridstep; }
    }
    // выравниваем их по сетке
    var C = (scaleBox.offsetWidth-7)/(Grids[gid].max-Grids[gid].min);
    for (i=0;i<scaleBox.childNodes.length;i++) {
        var v=Number(scaleBox.childNodes[i].innerHTML);
        var w=Math.floor(scaleBox.childNodes[i].offsetWidth/2);
        scaleBox.childNodes[i].style.left=(C*(v-Grids[gid].min)-w+2)+'px';
    }
    if (Grids[gid].type=='select') {
        var i=Grids[gid].min;
        var selectBox=$(gid);
        var n=0;
        while (i<=Grids[gid].max) {
            selectBox.options[n]=new Option(i+' см',i);
            n++;
            i+=Grids[gid].step;
        }
    }
}
function gridSetMover(gid,val) {
    var scaleBox=$(gid+'Numbers');
    var C = (scaleBox.offsetWidth-7)/(Grids[gid].max-Grids[gid].min);
    
    if (Grids[gid].min > val) { val=Grids[gid].min; }
    if (Grids[gid].max < val) { val=Grids[gid].max; }
    if (val % Grids[gid].step != 0) {
        if (val % Grids[gid].step < Grids[gid].step/2) {
            val-=Grids[gid].step+val%Grids[gid].step;
        }
        else {
            val+=Grids[gid].step-val%Grids[gid].step;
        }
    }
    Grids[gid].value=Number(val);
    if (gid=='HouseH' || gid=='HouseHF') {
        if (Grids['HouseH'] && Grids['HouseHF']) {
            if (Grids['HouseH'].value > Grids['HouseHF'].value) {
                Grids['HouseH'].value = Grids['HouseHF'].value;
                if (gid=='HouseHF') { gridSetMover('HouseH',Grids['HouseH'].value); }
            }
        }
    }
    
    $(gid+'Mover').style.left=(C*(Grids[gid].value-Grids[gid].min)-7+2)+'px';
    if ($('Final'+gid+'Mover')) {
        $('Final'+gid+'Mover').style.left=(C*(Grids[gid].value-Grids[gid].min)-7+2)+'px';
    }
    
    gridSetValue(gid,Grids[gid].value);
}
function gridCheck(ev,obj) {
    var k = ev.keyCode;
    if (k==27 || k==13) { gridCheckValue(obj); }
    else if (k!=190 && k!=188) {
        var val=Number(obj.value.replace(/[^0-9\-\.\,]/g,'').replace(/\,/g,'.'));
        var gid=obj.id;
        if (k==38 || k==40) {
            if (k==38) { val+=Grids[gid].step; }
            if (k==40) { val-=Grids[gid].step; }
            if (Grids[gid].min > val) { val=Grids[gid].min; }
            if (Grids[gid].max < val) { val=Grids[gid].max; } 
            if (val % Grids[gid].step != 0) {
                if (val % Grids[gid].step < Grids[gid].step/2) {
                    val-=Grids[gid].step+val%Grids[gid].step;
                }
                else {
                    val+=Grids[gid].step-val%Grids[gid].step;
                }
            }
        }
        if (Grids[gid].min <= val && Grids[gid].max >= val) { gridSetMover(gid,val); }
        Grids[gid].value=val;
        gridSetValue(gid,val);
    }
}
function gridCheckValue(obj) {
    var val=Number(obj.value.replace(/[^0-9\-\.\,]/g,'').replace(/\,/g,'.'));
    var gid=obj.id;
    gridSetMover(gid,val);
    Grids[gid].value=val;
}
function gridSetValue(gid,val) {
    if (Grids[gid].postAction) { eval(Grids[gid].postAction); }
    var f=false;
    if (gid.indexOf('Final')===0) {
        gid=gid.substring(5);
        f=true;
    }
    sData[gid]=val;
    $(gid).value=String(val).replace(/\./g,',');
    if (f) { gridSetMover(gid,val); }
    if ($('Final'+gid)) {
        $('Final'+gid).value=String(val).replace(/\./g,',');
    }
    if (Grids[gid].addAction) { eval(Grids[gid].addAction); }
    
}


var gridDragName='';
var gridDragObj='';
var gridDrag_lastX, gridDrag_lastY, gridDrag_dragging, gridDrag_dx;
var gridDragOldMouseMove=function(e){};
var gridDragOldMouseUp=function(e){};

function gridBeginDrag(e,gid) {
    var mouseKey=(IE)?event.button:e.which;
    if (mouseKey!=1) { return; }
    
    gridDragObj=$(gid+'Mover'); 
	gridDragName=gid;
	gridDrag_begindrag(e);
}

function gridEndDrag(e) {
    if (gridDragName=='') { return false; }
    
    var pps=($(gridDragName+'Scale').offsetWidth-7)/(Grids[gridDragName].max-Grids[gridDragName].min)*Grids[gridDragName].step;
    steps=Math.round(gridDrag_dx/pps);
    gridSetMover(gridDragName,Grids[gridDragName].value+Grids[gridDragName].step*steps);
    
    gridDrag_enddrag(e);
    gridDragObj='';
    gridDragName='';
}


function gridDrag_begindrag(e) {
    var mouseX=(IE)?(event.clientX+getScrollX()):e.pageX;
    var mouseY=(IE)?(event.clientY+getScrollY()):e.pageY;
    var mouseKey=(IE)?event.button:e.which;
	
	if (mouseKey == 1) {
	    gridDrag_lastX=mouseX;
		gridDrag_lastY=mouseY;
		gridDrag_dragging=true;
		gridDrag_dx=0;
		//$(gridDragName+'Grid').onmousemove=gridDrag_drag;
		//$(gridDragName+'Grid').onmouseup=gridEndDrag;
		document.onmousemove=gridDrag_drag;
		document.onmouseup=gridEndDrag;
	}
}

function gridDrag_enddrag(e) {
	var mouseKey=(IE)?event.button:e.which;
	//$(gridDragName+'Grid').onmousemove=function(e){};
	//$(gridDragName+'Grid').onmouseup=function(e){};
	document.onmousemove=gridDragOldMouseMove;
	document.onmouseup=gridDragOldMouseUp;
	if (mouseKey == 1) {
		gridDrag_dragging=false;
		gridDrag_dx=0;
	}
}

function gridDrag_drag(e) {
    var mouseKey=(IE)?event.button:e.which;
    if (mouseKey != 1) { gridEndDrag(e); }
	if (gridDrag_dragging) {
	    var _sH=getWindowH();
	    var _sW=getWindowW();
	    var _sX=getScrollX();
	    var _sY=getScrollY();
		var mouseX=((IE)?(event.clientX+_sX):(e.pageX));
        var mouseY=((IE)?(event.clientY+_sY):(e.pageY));
        if (mouseX > _sX && mouseX < _sW+_sX && mouseY > _sY && mouseY < _sH+_sY) {
			gridDrag_to(mouseX-gridDrag_lastX, mouseY-gridDrag_lastY);
			gridDrag_lastX = mouseX;
			gridDrag_lastY = mouseY;
		}
		else { 
		    gridEndDrag(e); 
		}
	}
	return false;
}

function gridDrag_to(dx,dy) {
    gridDrag_dx+=dx;
    var nleft=gridDragObj.offsetLeft+dx;
    var maxl=$(gridDragName+'Scale').offsetWidth-7;
    if (nleft>=-5 && nleft<=maxl-5) {
        gridDragObj.style.left=nleft+'px';
    }
    
}


function setPowerType() {
    var val=Number($('FinalPower').value);
    for (i=0;i<SubSteps['Power'].length;i++) {
        $('FinalPriceType_'+SubSteps['Power'][i]).style.display='none';
    }
    $('FinalPriceType_'+Powers[val].type).style.display='';
    sData['Power']=val;
}
function setHouseValue() {
    if ($('HouseW') && $('HouseL') && $('HouseH') && $('HouseHF') && $('FinalHouseV')) {
        var W=Number($('HouseW').value.replace(/\,/g,'.'));
        var L=Number($('HouseL').value.replace(/\,/g,'.'));
        var H=Number($('HouseH').value.replace(/\,/g,'.'));
        var HF=Number($('HouseHF').value.replace(/\,/g,'.'));
        var V=L*W*H+W*(HF-H)*L/2;
        $('FinalHouseV').innerHTML=String(V).replace(/\./g,',');
        
        houseSetDraft();
    }
}

var pxMin={
    'W': 55,
    'L': 45,
    'HF': 36,
    'H': 36,
    'h': 0
};
var pxMax={
    'W': 230,
    'L': 230,
    'HF': 150,
    'H': 150,
    'h': 100
};

function houseSetDraft() {
    // h: 86 - 150 px (этаж - 36, крыша - 0)
    // w,l: 45 - 240 px
    
    var W=Number($('HouseW').value.replace(/\,/g,'.'));
    var L=Number($('HouseL').value.replace(/\,/g,'.'));
    var H=Number($('HouseH').value.replace(/\,/g,'.'));
    var HF=Number($('HouseHF').value.replace(/\,/g,'.'));
    var h=HF-H;
    
    /*pxMax.H=(pxMax.HF*H+pxMin.HF*h)/(HF);
    pxMax.h=pxMax.HF-pxMax.H;*/
    
    
    if (L<=7) { var pL=pxMin.L; }                    //
    else if (L<=30) { var pL=Math.round(pxMin.L+(pxMax.L-pxMin.L)*(L-7)/(30-7)); } //  // длина дома
    else { var pL=pxMax.L; }                        //
    if (W<=7) { var pW=pxMin.W; }                    //
    else if (W<=30) { var pW=Math.round(pxMin.W+(pxMax.W-pxMin.W)*(W-7)/(30-7)); } //  // ширина дома
    else { var pW=pxMax.W; }                        //
    if (H<=3) { var pH=pxMin.H; }
    else if (H<12) { var pH=Math.round(pxMin.H+(pxMax.H-pxMin.H)*(H-3)/(12-3)); }     // высота этажей
    else { var pH=pxMax.H; }
    
    var ph=Math.round((pxMax.h-pxMin.h)*h/(20));                       // высота крыши
    var floors=(H>=6)?2:1;
    
    
    // проекция длина-высота
    var draft=$('DraftHouseHL');
    draft.innerHTML='';
    var fh=pH;
    var b=50;
    var addL=10;
    if (floors==2) {
        // добавляем цокольный этаж
        var fh=Math.floor(pH/2);
        var nd=document.createElement('div');
        nd.className='floor2';
        nd.style.left=(pxMax.L-pL)/2+7+addL+'px';
        nd.style.bottom=b+'px';
        nd.style.width=(pL-2*1)+'px';
        nd.style.height=(fh-1)+'px';
        b+=fh;
        fh=pH-fh;
        draft.appendChild(nd);
    }
    // добавляем этаж с окнами
    var nd=document.createElement('div');
    nd.className='floor1';
    nd.style.left=(pxMax.L-pL)/2+7+addL+'px';
    nd.style.bottom=b+'px';
    nd.style.width=(pL-2*1)+'px';
    nd.style.height=(fh-2)+'px';
    var sCount=Math.round((pL-2*1)/45);
    var sWidth=Math.floor((pL-2*1)/sCount);
    var l=0;
    for (i=0;i<sCount;i++) {
        if (i==0) {
            // добавляем колону в начало
            var nic=document.createElement('img');
            nic.className='floor_col';
            nic.src='/f/i/floor-col.gif';
            nic.style.height=(fh-2)+'px';
            nic.style.left=(l-1)+'px';
            nic.style.width=(5)+'px';
            nd.appendChild(nic);
        }
        var nds=document.createElement('div');
        nds.className='floor_sect';
        nds.style.left=(l+1)+'px';
        if (i==0 && sCount>1 && sCount*sWidth<(pL-2*1)) {
            // корректируем размер
            var w=(pL-2*1-(sCount-1)*sWidth);
        }
        else {
            var w=sWidth;
        }
        nds.style.width=w+'px';
        l+=sWidth;
        nds.style.height=(fh-2)+'px';
        // добавить окно
        var ndsw=document.createElement('div');
        ndsw.className='floor_win';
        ndsw.style.left=(w-23)/2+'px';
        nds.appendChild(ndsw);
        
        nd.appendChild(nds);
        
        // добавляем колону
        var nic=document.createElement('img');
        nic.className='floor_col';
        nic.src='/f/i/floor-col.gif';
        nic.style.height=(fh-2)+'px';
        if (i==sCount-1) { nic.style.left=(l-2)+'px'; }
        else { nic.style.left=(l-1)+'px'; }
        nd.appendChild(nic);
    }
    b+=fh;
    draft.appendChild(nd);
    
    // добавляем указатель длины дома
    var nd=document.createElement('div');
    nd.className='size_l';
    nd.style.left=(pxMax.L-pL)/2+7+addL+'px';
    nd.style.bottom=(50-29)+'px';
    nd.style.width=(pL-2*1)+'px';
    var ndsl=document.createElement('div'); ndsl.className='line'; nd.appendChild(ndsl);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='left'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='right'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='text'; 
    ndsi.style.left=(pL-2*1-25)/2+'px';
    nd.appendChild(ndsi);
    draft.appendChild(nd);
    
    // добавляем потолок
    var nd=document.createElement('div');
    nd.className='roof0';
    nd.style.left=(pxMax.L-pL-14)/2+7+addL+'px';
    nd.style.bottom=b+'px';
    nd.style.width=(pL+14-2*1)+'px';
    nd.style.height=((ph>20)?4:1)+'px';
    b+=(ph>20)?7:4;
    var rh=(ph>20)?7:4;
    draft.appendChild(nd);
    
    // добавляем указатель высоты до ската дома
    var nd=document.createElement('div');
    nd.className='size_h';
    nd.style.left=(pxMax.L+pL)/2+14+addL+'px';
    nd.style.bottom=(50-1)+'px';
    nd.style.height=(pH)+'px';
    var ndsl=document.createElement('div'); ndsl.className='line'; nd.appendChild(ndsl);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='top'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='bottom'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='text'; nd.appendChild(ndsi);
    draft.appendChild(nd);
    
    if (h>0) {
        // добавляем крышу
        var nd=document.createElement('div');
        nd.className='roof1';
        nd.style.left=(pxMax.L-pL-14)/2+7+addL+'px';
        nd.style.bottom=b+'px';
        nd.style.width=(pL+14-2*2)+'px';
        nd.style.height=(ph-2)+'px';
        b+=ph;
        if (ph>40) {
            // добавляем окна
            var wCount=(sCount>1)?(sCount-1):1;
            var wWidth=Math.floor((pL+14-2*2)/wCount);
            var l=0;
            for (i=0;i<wCount;i++) {
                var ndw=document.createElement('div');
                ndw.className='roof_w';
                if (i==0 && wCount>1 && wCount*wWidth<(pL+14-2*2)) {
                    // корректируем размер
                    var w=(pL+14-2*2-(wCount-1)*wWidth);
                }
                else {
                    var w=wWidth;
                }
                ndw.style.width=w+'px';
                ndw.style.left=l+'px';
                l+=w;
                nd.appendChild(ndw);
            }
            // добавляем балконы
            ndb=document.createElement('div');
            ndb.className='roof_balcony';
            ndb.style.left=(-8)+'px';
            nd.appendChild(ndb);
            ndb=document.createElement('div');
            ndb.className='roof_balcony';
            ndb.style.right=(-8)+'px';
            nd.appendChild(ndb);
            
        }
        draft.appendChild(nd);
    }
    
    // добавляем указатель высоты до ската дома
    var nd=document.createElement('div');
    nd.className='size_hf';
    nd.style.left=(pxMax.L-pL-14)/2+7-9+addL+'px';
    nd.style.bottom=(50-1)+'px';
    nd.style.height=(pH+ph+rh-1)+'px';
    var ndsl=document.createElement('div'); ndsl.className='line'; nd.appendChild(ndsl);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='top'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='bottom'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='text'; nd.appendChild(ndsi);
    draft.appendChild(nd);
    
    // добавляем трубы
    var nd=document.createElement('div');
    nd.className='roof_ts';
    nd.style.left=(pxMax.L-pL-14)/2+7+addL+'px';
    nd.style.bottom=b-((IE6)?1:0)+'px';
    nd.style.width=(pL+14-2*1)+'px';
    var tCount=Math.round(sCount/2);
    var tWidth=Math.floor(sWidth*sCount/tCount);
    var l=0;
    for (i=0;i<tCount;i++) {
        var ndt=document.createElement('div');
        ndt.className='roof_t';
        ndt.style.width=tWidth+'px';
        ndt.style.left=l+'px';
        l+=tWidth;
        nd.appendChild(ndt);
    }
    draft.appendChild(nd);
    b+=7;
    
    
    // проекция ширина-высота
    var draft=$('DraftHouseHW');
    draft.innerHTML='';
    var b=50;
    var fh=pH;
    var addR=20;
    if (floors==2) {
        // добавляем цокольный этаж
        var nd=document.createElement('div');
        var fh=Math.floor(pH/2);
        var fw=pW-2*1;
        nd.className='floor2';
        nd.style.right=(pxMax.W-pW)/2+addR+'px';
        nd.style.bottom=b+'px';
        nd.style.width=fw+'px';
        nd.style.height=(fh-2)+'px';
        b+=fh;
        var r=0;
        var sw3=fw;
        if (fw>=80) {
            // добавляем кладку
            var nds=document.createElement('div');
            nds.className='stones';
            nds.style.right=r+'px';
            nds.style.bottom=0+'px';
            nds.style.height=(fh-2)+'px';
            if (fw>=100) { var sw=Math.floor(fw*0.25); }
            else { var sw=Math.floor(fw*0.33); }
            nds.style.width=(sw-1)+'px';
            nd.appendChild(nds);
            r+=sw;
            sw3-=sw;
        }
        if (fw>=100) {
            // добавляем дополнительные окна
            var nds=document.createElement('div');
            nds.className='windows';
            nds.style.right=r+'px';
            nds.style.bottom=0+'px';
            nds.style.height=(fh-2)+'px';
            var sw=Math.floor(fw*0.25); 
            nds.style.width=sw+'px';
            // добавляем сюда окошки
            var wCount=Math.floor(sw/25); if (wCount<1) { wCount=1; }
            var wWidth=Math.floor(sw/wCount);
            var wr=0;
            for (i=0;i<wCount;i++) {
                var ndsw=document.createElement('div');
                ndsw.className='win_l';
                ndsw.style.bottom=12+'px';
                ndsw.style.right=wr+'px';
                ndsw.style.width=wWidth+'px';
                ndsw.style.height=(18)+'px';
                wr+=wWidth;
                nds.appendChild(ndsw);
            }
            
            nd.appendChild(nds);
            r+=sw;
            sw3-=sw;
        }
        var nds=document.createElement('div');
        nds.className='windows';
        nds.style.right=r+'px';
        nds.style.bottom=0+'px';
        nds.style.height=(fh-2)+'px';
        nds.style.width=sw3+'px';
        // добавляем сюда дерь
        var wr=0;
        var ndsd=document.createElement('div');
        ndsd.className='door';
        ndsd.style.bottom=0+'px';
        ndsd.style.right=wr+'px';
        ndsd.style.width=35+'px';
        ndsd.style.height=(fh-2)+'px';
        wr+=35;
        sw3-=35;
        nds.appendChild(ndsd);
        // добавляем сюда окошки
        var wCount=Math.floor(sw3/25); if (wCount<1) { wCount=1; }
        var wWidth=Math.floor(sw3/wCount);
        for (i=0;i<wCount;i++) {
            var ndsw=document.createElement('div');
            ndsw.className='win_r';
            ndsw.style.bottom=12+'px';
            ndsw.style.right=wr+'px';
            ndsw.style.width=wWidth+'px';
            ndsw.style.height=(18)+'px';
            wr+=wWidth;
            nds.appendChild(ndsw);
        }
        
        nd.appendChild(nds);
        fh=pH-fh;
        draft.appendChild(nd);
    }
    // добавляем этаж с окнами
    var fw=pW;
    var nd=document.createElement('div');
    nd.className='floor1';
    nd.style.right=(pxMax.W-pW)/2+((fw>=80)?3:0)+addR+'px';
    nd.style.bottom=b+'px';
    nd.style.width=(fw-2*1-((fw>=80)?3:0))+'px';
    nd.style.height=(fh-2)+'px';
    var sw3=fw;
    var r=0;
    if (fw>=80) {
        // добавляем веранду
        var nds=document.createElement('div');
        nds.className='verand';
        nds.style.right=(r+0)+'px';
        nds.style.bottom=0+'px';
        nds.style.height=(fh-2)+'px';
        if (fw>=100) { var sw=Math.floor(fw*0.25); }
        else { var sw=Math.floor(fw*0.33); }
        nds.style.width=(sw-1)+'px';
        // колона
        var nisc=document.createElement('img');
        nisc.className='col';
        nisc.src='/f/i/floor-col.gif';
        nisc.style.height=(fh-2)+'px';
        nisc.style.width=(5)+'px';
        nisc.style.right=(-2)+'px';
        
        nds.appendChild(nisc);
        nd.appendChild(nds);
        r+=sw;
        sw3-=sw;
    }
    if (fw>=100) {
        // добавляем дополнительные окна
        var nds=document.createElement('div');
        nds.className='windows vverand';
        nds.style.right=r+'px';
        nds.style.bottom=0+'px';
        nds.style.height=(fh-1)+'px';
        var sw=Math.floor(fw*0.25); 
        nds.style.width=(sw-1)+'px';
        // добавляем сюда окошки
        var wCount=Math.floor(sw/19); if (wCount<1) { wCount=1; }
        var wWidth=Math.floor(sw/wCount);
        var wr=0;
        for (i=0;i<wCount;i++) {
            var ndsw=document.createElement('div');
            ndsw.className='win_l';
            ndsw.style.bottom=15+'px';
            ndsw.style.right=wr+'px';
            ndsw.style.width=wWidth+'px';
            ndsw.style.height=(15)+'px';
            wr+=wWidth;
            nds.appendChild(ndsw);
        }
        
        nd.appendChild(nds);
        r+=sw;
        sw3-=sw;
    }
    var nds=document.createElement('div');
    nds.className='windows';
    nds.style.right=r+'px';
    nds.style.bottom=0+'px';
    nds.style.height=(fh-2)+'px';
    nds.style.width=sw3+'px';
    // добавляем большие окошки 
    var wr=0;
    var wCount=Math.floor(sw3/45); if (wCount<1) { wCount=1; }
    var wWidth=Math.floor(sw3/wCount);
    for (i=0;i<wCount;i++) {
        var ndsw=document.createElement('div');
        ndsw.className='win_c';
        ndsw.style.bottom=3+'px';
        ndsw.style.right=wr+'px';
        ndsw.style.width=wWidth+'px';
        ndsw.style.height=(26)+'px';
        wr+=wWidth;
        nds.appendChild(ndsw);
    }
    nd.appendChild(nds);
    
    b+=fh-1;
    draft.appendChild(nd);
    
    
    // добавляем указатель ширины дома
    var nd=document.createElement('div');
    nd.className='size_w';
    nd.style.right=(pxMax.W-pW)/2+addR+((pW>=80 && floors==1)?3:0)+'px';
    nd.style.bottom=(50-29)+'px';
    nd.style.width=(pW-2*1-((pW>=80 && floors==1)?3:0))+'px';
    var ndsl=document.createElement('div'); ndsl.className='line'; nd.appendChild(ndsl);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='left'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='right'; nd.appendChild(ndsi);
    var ndsi=document.createElement('img'); ndsi.src='/f/i/z.gif'; ndsi.className='text'; 
    ndsi.style.right=5+'px';
    nd.appendChild(ndsi);
    draft.appendChild(nd);
    
    
    
    // добавляем крышу
    var nd=document.createElement('div');
    nd.className='roof0';
    nd.style.right=((pxMax.W-pW)/2-3)+addR+'px';
    nd.style.bottom=(b)+'px';
    nd.style.width=(pW-2*1+6)+'px';
    nd.style.height=(0)+'px';
    b+=2;
    draft.appendChild(nd);
    
    
    var nd=document.createElement('div');
    nd.className='roof00'+((ph==0)?'0':'');
    nd.style.right=((pxMax.W-pW)/2-3+((pW>=80)?14:0))+addR+'px';
    nd.style.bottom=(b)+'px';
    nd.style.width=(pW-2*1+6-((pW>=80)?14:0))+'px';
    nd.style.height=((ph>40)?10:((ph>20)?8:2))+'px';
    b+=(ph>40)?7:((ph>20)?5:3);
    draft.appendChild(nd);
    
    if (ph>0) {
        if (!IE) {
            var nd=document.createElement('div');
            var ndH=ph;
            var ndW=(pW-2*1+6+14-((pW>=80)?14:0));
            var rb=((ph>40)?3:((ph>20)?3:0))
            nd.style.right=((pxMax.W-pW)/2-2-7+((pW>=80)?14:0))+addR+'px';
            nd.style.bottom=(b)+'px';
            nd.style.width=ndW+'px';
            nd.style.height=ndH+'px';
            nd.id="RoofCanvasDiv";
            nd.innerHTML='<canvas id="RoofCanvas" width="'+ndW+'px" height="'+ndH+'px"></canvas>';
            draft.appendChild(nd);
            
            var canvas=$('RoofCanvas').getContext('2d'); 
            canvas.clearRect(0,0,ndW,ndH);
            canvas.beginPath();
            canvas.strokeStyle = "rgba(151,160,194,1)";
            canvas.fillStyle = "rgba(151,160,194,1)";
            if (ph>40) {
                canvas.moveTo(2, ndH-1);
                canvas.lineTo(7, ndH-rb);
                canvas.lineTo(7, ndH-rb+1);
                canvas.lineTo(2, ndH);
                
                canvas.moveTo(ndW-2, ndH-1);
                canvas.lineTo(ndW-7, ndH-rb);
                canvas.lineTo(ndW-7, ndH-rb+1);
                canvas.lineTo(ndW-2, ndH);
            }
            
            canvas.moveTo(0, ndH);
            canvas.lineTo(ndW/2, 0);
            canvas.lineTo(ndW, ndH);
            canvas.lineTo(ndW-2, ndH);
            canvas.lineTo(ndW/2, 2);
            canvas.lineTo(2, ndH);
            canvas.lineTo(0, ndH);
            
            canvas.fill();
            canvas.closePath(); 
        }
        else {
            var ni=document.createElement('img');
            ni.className='roof1';
            ni.src=((ph>40)?'/f/i/roof-main1.gif':'/f/i/roof-main.gif');
            ni.style.right=((pxMax.W-pW)/2-2-7+((pW>=80)?14:0))+addR+'px';
            ni.style.bottom=(b)+'px';
            ni.style.width=(pW-2*1+6+14-((pW>=80)?14:0))+'px';
            ni.style.height=(ph)+'px';
            draft.appendChild(ni);
        }
        
        if (ph>40) {
            // добавляем балкон
            ndb=document.createElement('div');
            ndb.className='roof_balcony'+((pW>=80)?'1':'2');
            ndb.style.width=((pW>=80)?47:34)+'px';
            ndb.style.height=(23)+'px';
            ndb.style.bottom=(b)+'px';
            ndb.style.right=((pxMax.W-((pW>=80)?47:34))/2+((pW>=80)?7:0))+addR+'px';
            draft.appendChild(ndb);
        }
        b+=ph;
    }
    
    // добавляем трубу
    var nd=document.createElement('div');
    nd.className='roof_t';
    nd.style.right=(pxMax.W)/2+((pW>=80)?5:-5)+addR+'px';
    nd.style.bottom=(b-((ph>40)?4:((ph>20)?2:2)) )+'px';
    nd.style.width=(7)+'px';
    nd.style.height=((ph>40)?9:((ph>20)?7:7))+'px';
    draft.appendChild(nd);
    b+=7;
    
    
    
    
    // добавляем кусты
    var ground=$('DraftHouseGround');
    ground.innerHTML='';
    var posR=(pxMax.W-pW)/2-3+addR;
    var posL=(pxMax.L-pL)/2+addL;
    if (posL+posR > 25) {
        // сажаем куст №1
        var ni=document.createElement('img');
        ni.src='/f/i/ground-bush1.gif';
        ni.style.width='17px';
        ni.style.height='24px';
        ni.style.right=(280-posL-4)+'px';
        ground.appendChild(ni);
    }
    if (posL+posR > 60) {
        // сажаем дерево
        var ni=document.createElement('img');
        ni.src='/f/i/ground-tree.gif';
        ni.style.width='56px';
        ni.style.height='71px';
        ni.style.right=(280-posL+(posL+posR-60)/2)+'px';
        ground.appendChild(ni);
    }
    if (280-posL-pL > 30) {
        // сажаем куст №2
        var ni=document.createElement('img');
        ni.src='/f/i/ground-bush2.gif';
        ni.style.width='29px';
        ni.style.height='19px';
        ni.style.right=(280-posL-pL-39)+'px';
        ground.appendChild(ni);
    }
    if (280-posL-pL > 80) {
        // сажаем куст №3
        var ni=document.createElement('img');
        ni.src='/f/i/ground-bush3.gif';
        ni.style.width='26px';
        ni.style.height='11px';
        ni.style.right=(280-posL-pL-79)+'px';
        ground.appendChild(ni);
    }
}


var requestID=0;
var requestTimer=0;
function costEdit(ev) {
    var k = ev.keyCode;
    if (k==27 || k==13) { sendRequest(); }
}
function sendRequest() {
    clearTimeout(requestTimer);
    if (sData['Power']!=undefined) {
        var val=Number($('FinalPrice_'+Powers[sData['Power']].type).value.replace(/[^0-9\-\.\,]/g,'').replace(/\,/g,'.'));
        sData['PowerCost_'+Powers[sData['Power']].type]=val;
        $('FinalPrice_'+Powers[sData['Power']].type).value=String(val).replace(/\./g,',');
        calculationProcess('show');
        if (sData['PowerCost_'+Powers[sData['Power']].type]) {
            requestTimer=setTimeout('getCalculation()',0.5*1000);
        }
    }
}
function checkRequest() {
    if (sData['Power']!=undefined) {
        if (sData['PowerCost_'+Powers[sData['Power']].type]) {
            return true; 
        }
    }
    return false;
}
function getCalculation() {
    if (sData['PowerCost_'+Powers[sData['Power']].type]) {
        requestID++;
        var post='RID='+requestID+'&Region='+sData['Region']+
            '&HouseL='+sData['HouseL']+'&HouseW='+sData['HouseW']+
            '&HouseH='+sData['HouseH']+'&HouseHF='+sData['HouseHF']+
            '&Temperature='+sData['Temperature']+
            '&Roof='+sData['Roof']+'&RoofIsover='+sData['Roof_'+sData['Roof']]+'&RoofSize='+sData['RoofSize_'+sData['Roof']]+
            '&Wall='+sData['Wall']+'&WallIsover='+sData['Wall_'+sData['Wall']]+'&WallSize='+sData['WallSize_'+sData['Wall']]+
            '&Floor='+sData['Floor']+'&FloorIsover='+sData['Floor_'+sData['Floor']]+'&FloorSize='+sData['FloorSize_'+sData['Floor']]+
            '&Power='+sData['Power']+'&PowerCost='+sData['PowerCost_'+Powers[sData['Power']].type];
        new Ajax.Request( './calculate/?'+Math.random(),
            {
                method: 'post',
                postBody: post,
                onSuccess: setCalculation
            }
        );
    }
}

var efvMin=Math.exp(27/10000000*Math.log(2*Math.PI*20));
var efvMax=Math.exp(27/10000000*Math.log(2*Math.PI*1000));
//var efvMin=Math.exp(1/100000*Math.log(2*Math.PI*30));
//var efvMax=Math.exp(1/100000*Math.log(2*Math.PI*1000));
function setCalculation(answer) {
    var result=answer.responseText;
    result=result.split('|');
    if (result[0]=='ok') {
        var costDefault=Number(result[2]);
        var costIsover=Number(result[1]);
        
        $('FinalSummDefault').innerHTML=result[2].replace(/\./g,',');
        $('FinalSummIsover').innerHTML=result[1].replace(/\./g,',');
        $('FinalSummEconomy').innerHTML=String(costDefault-costIsover).replace(/\./g,',');
        
        $('FinalRoofLost').innerHTML='-'+result[3]+'%';
        $('FinalWallLost').innerHTML='-'+result[4]+'%';
        $('FinalFloorLost').innerHTML='-'+result[5]+'%';
        
        
        var efv=Math.exp(27/10000000*Math.log(2*Math.PI*result[6]));
        //var efv=Math.exp(1/100000*Math.log(2*Math.PI*result[6]));
        
        var alpha=-30+60*(efv-efvMin)/(efvMax-efvMin);
        var X=222;
        var Y=245;
        var R=148;
        var dx=R*Math.sin(alpha*Math.PI/180);
        var dy=R*Math.cos(alpha*Math.PI/180);
        
        var canvas=$('ResultCanvasEffect').getContext('2d'); 
        canvas.clearRect(0,0,431,324);
        canvas.beginPath();
        canvas.strokeStyle = "rgba(0,0,0,1)";
        canvas.moveTo(X-1, Y);
        canvas.lineTo(X+dx-1, Y-dy);
        canvas.lineTo(X+dx+1, Y-dy);
        canvas.lineTo(X+1, Y);
        canvas.fillStyle = "rgba(0,0,0,1)";
        canvas.fill();
        canvas.closePath(); 
        
        calculationProcess('prehide');
        
        // step = 4
        var ppg=(217-36)/costDefault;
        var h=Math.round(ppg*costIsover);
        h=6*Math.round(h/6)+36;
        $('FinalCoinsIsover').style.height=((h>0)?h:0)+'px';
        $('FinalCoinsIsoverHline').style.height=((h>36)?(h-36):0)+'px';
        $('FinalCoinsIsoverVline').style.height=((h>50)?(h-50):0)+'px';
        var eh=217-h;
        var imgs=$('FinalCoinsEconomyHline').getElementsByTagName('img');
        if (eh<14) { 
            imgs[0].style.display=imgs[1].style.display='none'; 
            $('FinalCoinsEconomyVline').style.height=eh+'px';
        }
        else { 
            imgs[0].style.display=imgs[1].style.display=''; 
            $('FinalCoinsEconomyVline').style.height=(eh-14)+'px';
        }
        $('FinalCoinsEconomyHline').style.height=((eh>0)?(eh-0):0)+'px';
        
        calculationProcess('hide');
    }
    else {
        
    }
}

function calculationProcess(opt) {
    switch (opt) {
        case 'init': {
            $('ResultProcess').style.display='';
            $('ResultProcessing').style.display='none';
            $('ResultButtons').style.display='none';
            showStepType('Result','');
            break;
        }
        case 'show': {
            if (requestID>0) {
                $('ResultProcessing').style.display='';
                $('ResultProcess').style.display='none';
            }
            else {
                $('ResultProcess').style.display='';
                $('ResultProcessing').style.display='none';
            }
            $('SPrint').style.display='';
            break;
        }
        case 'prehide': {
            if (requestID>1) {
                $('ResultProcessing').style.display='none';
            }
            else {
                $('ResultProcess').style.display='none';
            }
            break;
        }
        case 'hide': {
            if (!sData['Result']) {
                showStepType('Result',SubSteps['Result'][0]);
                $('ResultButtons').style.display='';
            }
            break;
        }
        case 'send': {
        	$('SPrint').style.display='none';
            sendRequest();
            break;
        }
    }
}





function bodyMakeTop(num) {
    var wH=getWindowH();
    var wW=getWindowW();
    var tH=wH-$('DivHeader').offsetHeight-$('DivNavigator').offsetHeight-$('DivFooter').offsetHeight-StepsH[num]-25;
    if (tH>0) {
        var mt=Math.floor(tH/2);
        var mb=tH-mt;
        $('DivBody').style.marginTop=(mt)+'px';
        $('DivBody').style.marginBottom=(mb)+'px';
    }
    else {
        $('DivBody').style.marginTop='0px';
        $('DivBody').style.marginBottom='0px';
    }
}
function onResize() {
	if (isPrint) { return; }
    if (!stepInProcess) {
        bodyMakeTop(stepCurrent);
    }
}

function myEscape(text) {
    return encodeURI(text).replace(/\+/g,'%2B').replace(/\&/g,'%26');
    return text.replace(/\+/g,'%2B').replace(/\&/g,'%26');
}
window.onresize=onResize;
