// скрипты для калькулятора

var WindowType = {};
var minWidth;
var minHeight;

var defVal = [
              {height:1400,width:800,subtype:0,pod:156,otl:163},
              {height:1400,width:1300,subtype:0,pod:156,otl:163},
              {height:1400,width:2050,subtype:0,pod:156,otl:163},
              {height:2100,width:700,subtype:0,pod:173,otl:174},
              {height:1700,width:800,subtype:0,pod:156,otl:163},
              {height:1400,width:800,subtype:0,pod:156,otl:163}
             ];


 function properties(obj)
  { var res="";
    for(var i in obj)
    { res+=obj.name+"."+i+"="+obj[i]+"<br>";
    }
    res+="<hr>";
    document.write("<p style='font-size:12;'>"+res);
  }


function initWindowType()
{
    // формируем массив типов окон
    HTML_AJAX.defaultServerUrl = '/ajax.php?ajmod=Calculator'
    var objTemp = new Object();
    var objCallback = { init: function() {} };
    var objRT = new Calculator();
    objRT.init(objTemp);

    if ('object' != typeof WindowType)
    {
        return ;
    }
    // рисуем типы окон
    var html = '';
    var div = document.getElementById('windowtype');
    if (div)
    {
        for (i in WindowType) 
        {
            //WindowType[i]['subtype']
            html += '<a href="javascript: void chooseWindowType('+i+')"><img src="/picture/small'+WindowType[i]['ipictureid']+'" alt="'+WindowType[i]['vctitle']+'" title="'+WindowType[i]['vctitle']+'"></a>';
        }
        div.className = '';
        div.innerHTML = html;
        div.className = 'tippic';
    }
    // позиционируем на первый элемент
    chooseWindowType(0);
}


// строим подтипы
function chooseWindowType(i)
{
    if (WindowType[i]['subtype'])
    {
        var html = '';
        var div = document.getElementById('windowsubtype');
        if (div)
        {
            for (j in WindowType[i]['subtype']) 
            {
                html += '<a href="javascript: void chooseWindowSubType('+i+','+j+')"><img src="/picture/small'+WindowType[i]['subtype'][j]['ipictureid']+'" alt="'+WindowType[i]['subtype'][j]['vctitle']+'" title="'+WindowType[i]['subtype'][j]['vctitle']+'" class="mar"></a>';
            }
            
              
            div.className = '';
            div.innerHTML = html;
            div.className = 'padt10';
            //properties(div);
            var img = document.getElementById('chousewindow');
            var txt = document.getElementById('chousename');
            if (img)
            {
                img.src = '/picture/large' + WindowType[i]['ipictureid'];
            }
            if (txt)
            {
                txt.innerHTML = WindowType[i]['vcdesc'];
            }
        }
        if (document.getElementById('windowsill'))
        {
            document.getElementById('windowsill').disabled = (i == 3);
        }
        if (document.getElementById('tide'))
        {
            document.getElementById('tide').disabled = (i == 3);
        }

    }
    

    //alert(i);
    autoSelect(i);
    
    
    minWidth = WindowType[i]['minwidth'];
	
    $("#width").change(checkWidth);
    
	minHeight = WindowType[i]['minheight'];
	
    $("#height").change(checkHeight);
    

    
    
    checkHeight();
    checkWidth();

}

function autoSelect(i){
	var w = defVal[i];
	chooseWindowSubType(i,w.subtype);
	document.getElementById('height').value=w.height;
	document.getElementById('width').value=w.width;
	document.getElementById('st_149').setAttribute('checked','true');
	$("#windowsill").val(w.pod);
	$("#tide").val(w.otl);
	
	/*if (w.pod !=null){
	options = document.getElementById('windowsill').options;
		for (k in options){
			if (options[k].value == w.pod){
				options[k].selected = true;
			}
		}
	}
	
	if (w.otl !=null){
		options = document.getElementById('tide').options;
			for (k in options){
				if (options[k].value == w.otl){
					options[k].selected = true;
				}
			}
	}*/
	//console.log(options);
}


function chooseWindowSubType(i, j)
{
    var img = document.getElementById('chousewindow');
    var txt = document.getElementById('chousename');
    var id = document.getElementById('iwindowstypeid');
    var height = document.getElementById('height');
    var width = document.getElementById('width');
    
    
    if (img)
    {
        img.src = '/picture/large' + WindowType[i]['subtype'][j]['ipictureid'];
    }
    if (txt)
    {
        txt.innerHTML = WindowType[i]['subtype'][j]['vcdesc'];
    }
    if (id)
    {
        id.value = WindowType[i]['subtype'][j]['iwindowtypeid'];
    }
   
    
   
    	if (WindowType[i]['subtype'][j]['minwidth']!=0){
    	minWidth = WindowType[i]['subtype'][j]['minwidth'];
        $("#width").change(checkWidth);
    	}
    	
    	if(WindowType[i]['subtype'][j]['minheight']!=0){
    	minHeight = WindowType[i]['subtype'][j]['minheight'];
    	
        $("#height").change(checkHeight);
    	}
        checkHeight();
        checkWidth();
    
}

function checkHeight(){

    	$('#height_span').remove();
    	if($('#height').val()!=='' && parseInt($('#height').val()) < parseInt(minHeight)){
    		$('div.height').after('<span id="height_span">Минимальный размер '+minHeight+' мм</span>');
    	}   
}

function checkWidth(){
	
	$('#width_span').remove();
	if($('#width').val()!=='' && parseInt($('#width').val()) < parseInt(minWidth)){
		$('div.width').after('<span id="width_span">Минимальный размер '+minWidth+' мм</span>');
	}
}

function doCalc()
{
    //alert('расчет');
    var objTemp = new Object();
    // смотри все ли параметры заданы
    if (!document.getElementById('iwindowstypeid') || document.getElementById('iwindowstypeid').value == '')
    {
        alert('Не выбран вариант открывания створок');
        return ;
    } else
    {
        objTemp['iwindowstypeid'] = document.getElementById('iwindowstypeid').value;
    }
    
    if (document.getElementById('windowsill') && document.getElementById('windowsill').value != '')
    {
        objTemp['windowsill'] = document.getElementById('windowsill').value;
    }
    if (document.getElementById('tide') && document.getElementById('tide').value != '')
    {
        objTemp['tide'] = document.getElementById('tide').value;
    }

    if (!document.getElementById('color') || document.getElementById('color').value == '')
    {
        alert('Не выбран Цвет');
        return ;
    } else
    {
        objTemp['color'] = document.getElementById('color').value;
    }
    
    if (document.getElementById('mosquito') && document.getElementById('mosquito').checked)
    {
        objTemp['mosquito'] = 1;
    }
    if (document.getElementById('count'))
    {
        objTemp['count'] = document.getElementById('count').value;
    } else
    {
        objTemp['count'] = 1;
    }
    if (!document.getElementById('width') || document.getElementById('width').value == '')
    {
        alert('Не задана ширина');
        return ;
    } else
    {
        objTemp['width'] = document.getElementById('width').value;
    }
    if (!document.getElementById('height') || document.getElementById('height').value == '')
    {
        alert('Не задана высота');
        return ;
    } else
    {
        objTemp['height'] = document.getElementById('height').value;
    } 
    document.getElementById('CalculatorForm').submit();
}

function DeleteBasket(id)
{
    if (confirm('Хотите удалить позицию из корзины?'))
    {
        HTML_AJAX.defaultServerUrl = '/ajax.php?ajmod=Calculator'
        var objTemp = new Object();
        var objCallback = { basketdelete: function() {} };
        var objRT = new Calculator();
        objTemp['index'] = id;
        var result = objRT.basketdelete(objTemp);
        if ('object' != typeof result)
        {
            result = {};
        }
        var message = result['message'];
        var type = result['type'];;
        if (type == 0)
        {
            window.location.reload();
        }
    }
}

if (window.addEventListener) 
{
    window.addEventListener('load', function() { initWindowType(); }, false);
} else if (window.attachEvent) 
{
    window.attachEvent('onload', function() { initWindowType(); });
}

