var T_SLOUPEC;

function zoom_in(){
	var id = document.getElementById('shelf_id');
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';

	var postvars = 'action=zoom_in&shelf_id=' + id.value;

	if (!send_xmlhttprequest(zoom_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function zoom_out(){
	var id = document.getElementById('shelf_id');
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';

	var postvars = 'action=zoom_out&shelf_id=' + id.value;

	if (!send_xmlhttprequest(zoom_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function zoom_service(xmlhttp) {
	var imageobj = document.getElementById('shelf_image');
		
	if (xmlhttp.readyState == 4) {	
        setDndButtons();
        imageobj.src = document.getElementById('original_img').value + '&' + (new Date()).getTime();
	}
}

function set_name(){
	var bf = document.getElementById('real_name');
	var nw = document.getElementById('false_name');	
	
	nw.value = bf.value;
}

function change_shelf_height(index){
	var id = document.getElementById('shelf_id');
	
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var postvars = 'action=change_height&index=' + index;
	
	var obj = document.getElementById('col_height_' + index);
	postvars = postvars + '&height=' + obj.options[obj.selectedIndex].value;
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function change_shelf_fill(index){
	var id = document.getElementById('shelf_id');
	
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var postvars = 'action=change_fill&index=' + index;
	
	var obj = document.getElementById('col_fill_' + index);
	postvars = postvars + '&fill=' + obj.options[obj.selectedIndex].value;
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function change_shelf_count(index){
	var id = document.getElementById('shelf_id');
	
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var postvars = 'action=change_count&index=' + index;
	
	var obj = document.getElementById('col_count_' + index);
	postvars = postvars + '&count=' + obj.options[obj.selectedIndex].value;
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function change_shelf_material(type){
	var id = document.getElementById('shelf_id');
	
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var postvars = 'action=change_material&type=' + type;
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function change_depth(){
    var id = document.getElementById('shelf_id');
    
    var headers= new Array();
    headers['Content-type']= 'application/x-www-form-urlencoded';
    headers['Connection']='close';
    
    
    var obj = document.getElementById('depth');
    var postvars = 'action=change_depth&depth=' + obj.options[obj.selectedIndex].value;
    
    if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
        alert('Error in servise');
    }
}

function change_shelf_width(index){
	var id = document.getElementById('shelf_id');
	
	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var postvars = 'action=change_width&index=' + index;
	
	var obj = document.getElementById('col_width_' + index);
	postvars = postvars + '&width=' + obj.options[obj.selectedIndex].value;
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
		alert('Error in servise');
	}
}

function add() {
	var cols = document.getElementById('col_count');
	var id = document.getElementById('shelf_id');

	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var node, nodeText, obj, old, older, i, j, childs, option;
	
	old = cols.value;
	older = old - 1;

	if (cols.value < 20){
		cols.value++;
		
        node = document.createElement('td');    
		node.appendChild(document.createTextNode(cols.value + '. ' + document.getElementById('t_sloupec').value));
		node.setAttribute('id', 'row_column_' + old);
		document.getElementById('row_column').appendChild(node);
		
		var objects = new Array();
		objects.push('width'/*,'height', 'count'*/);
					
		for (i = 0; i < objects.length; i++){
			node = document.createElement('td');
			node.setAttribute('id', 'row_' + objects[i] + '_' + old);
			document.getElementById('row_' + objects[i]).appendChild(node);

			node = document.createElement('select');
			node.setAttribute('id', 'col_' + objects[i] + '_' + old);
			node.setAttribute('onchange', 'change_shelf_' + objects[i] +'(' + old +');');

			document.getElementById('row_' + objects[i] + '_' + old).appendChild(node);

			childs = document.getElementById('col_' + objects[i] + '_' + older).childNodes;

			node = document.getElementById('col_' + objects[i] + '_' + old);

			for (j = 0; j < childs.length; j++) {
				option = childs[j].cloneNode(true);
				node.appendChild(option);
			}
		

			node.options[document.getElementById('col_' + objects[i] + '_' + older).selectedIndex].selected = true;
		}
		
		/*node = document.createElement('td');
		node.setAttribute('id', 'row_light_' + old);
		document.getElementById('row_light').appendChild(node);
		document.getElementById('row_light_' + old).innerHTML = document.getElementById('row_light_' + older).innerHTML;*/
	}
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, 'action=add_column', headers)) {
		alert('Error in servise');
	}
}

function remove() {
	var obj, node, i;
	var cols = document.getElementById('col_count');
	var id = document.getElementById('shelf_id');

	var headers= new Array();
	headers['Content-type']= 'application/x-www-form-urlencoded';
	headers['Connection']='close';
	
	var objects = new Array();
	objects.push('width', 'column'/*, 'height', 'count', 'light'*/);

	if (cols.value - 1 > 0) {
		cols.value--;

		for (i = 0; i < objects.length; i++){
			obj = document.getElementById('row_' + objects[i]);
			node = document.getElementById('row_' + objects[i] + '_' + cols.value);
			obj.removeChild(node);
		}

		//obj = document.getElementById('shelf_header');
		//obj.colSpan++;
	}
	
	if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, 'action=rm_column', headers)) {
		alert('Error in servise');
	}
}

function refresh(){
	if (!send_xmlhttprequest(refresh_image_service, 'POST', '/pneu_handle.php?id=' + id.value + '&material=' + material, postvars, headers)) {
		alert('Error in servise');
	}
}

function refresh_service(xmlhttp) {
	var imageobj = document.getElementById('shelf_image');
	var obj, nodes;
	var i,j, index, inner;
		
	if (xmlhttp.readyState == 4) {	    	    	    
		if (xmlhttp.responseXML != null) {
    		obj = xmlhttp.responseXML.getElementsByTagName('count');
			if (obj != null && obj.length > 0) {
				for (j = 0; j < obj.length; j++) {
						nodes = obj[j].childNodes;

						selected = 0;
						
						for (i = 0; i < nodes.length; i++){
							if (nodes[i].nodeName == 'count_index')
								index = nodes[i].firstChild.data;
								
							if (nodes[i].nodeName == 'count_selected')
								selected = nodes[i].firstChild.data;
								
							if (nodes[i].nodeName == 'count_values') {
								inner = nodes[i].firstChild.data;
								inner = inner.replace("&gt;", '<');
								inner = inner.replace("&lt;", '>');
								
								select_innerHTML(document.getElementById('col_count_' + index), inner);
							}
						}

						document.getElementById('col_count_' + index).selectedIndex = selected;
				}
			}		
			
		
		    obj = xmlhttp.responseXML.getElementsByTagName('light');
			if (obj != null && obj.length > 0) {
				var index = xmlhttp.responseXML.getElementsByTagName('light_index')[0].firstChild.data;
				var value = xmlhttp.responseXML.getElementsByTagName('light_value')[0].firstChild.data;
				
				var tmp = document.getElementById('row_light_' + index);
				
				if (tmp != null)
					tmp.innerHTML = value;
			}
		
			obj = xmlhttp.responseXML.getElementsByTagName('width');
			if (obj != null && obj.length > 0)
				document.getElementById('_width').innerHTML = obj[0].firstChild.data;
                
            obj = xmlhttp.responseXML.getElementsByTagName('weight');
            if (obj != null && obj.length > 0)
                document.getElementById('_weight').innerHTML = obj[0].firstChild.data;				
				
			obj = xmlhttp.responseXML.getElementsByTagName('price');
			if (obj != null && obj.length > 0)
				document.getElementById('_total_price').innerHTML = '<strong>' + obj[0].firstChild.data + '</strong>';
                
            obj = xmlhttp.responseXML.getElementsByTagName('discount');
            if (obj != null && obj.length > 0)
                document.getElementById('_discount_price').innerHTML = '<strong>' + obj[0].firstChild.data + '</strong>';
				
			obj = xmlhttp.responseXML.getElementsByTagName('addPrice');
			if (obj != null && obj.length > 0)
				document.getElementById('_add_total_price').innerHTML = '<strong>' + obj[0].firstChild.data + '</strong>';
				
			if (document.getElementById('store_state') != null) {
				obj = xmlhttp.responseXML.getElementsByTagName('store');
				if (obj != null && obj.length > 0)
                    if (obj[0].firstChild.data == 'true') 
					    document.getElementById('store_state').innerHTML = document.getElementById('_t_stored').value;
                    else
                        document.getElementById('store_state').innerHTML = document.getElementById('_t_unstored').value;
			}
		}
		
        setDndButtons();
        
        imageobj.src = document.getElementById('original_img').value + '&' + (new Date()).getTime();
	}
}

function setDndButtons(stdmenu) {
    var id = document.getElementById('shelf_id');

    var headers= new Array();
    headers['Content-type']= 'application/x-www-form-urlencoded';
    headers['Connection']='close';

    var postvars = 'action=getFields';
    
    if (!send_xmlhttprequest(setDndButtonsService, 'POST', '/designer_m15_buttons.php?id=' + id.value, postvars, headers)) {
        alert('Error in servise');
    }    
    
}

function setDndButtonsService(xmlhttp) {
    var obj, width, height, nodes, i, j, x, index, type, target, node, hght;
    
    var T_COLUMN = document.getElementById('_t_column').value;
    var T_ADDCOLUMN = document.getElementById('_t_addcolumn').value;
    var T_RMCOLUMN = document.getElementById('_t_rmcolumn').value;
    var T_ADDSHELF = document.getElementById('_t_addshelf').value;
    var T_RMSHELF = document.getElementById('_t_rmshelf').value;
    
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.responseXML != null) {
//            alert(xmlhttp.responseText);
            width = xmlhttp.responseXML.getElementsByTagName('width');
            height = xmlhttp.responseXML.getElementsByTagName('height');
            
            target = document.getElementById('btnBox');
            target.innerHTML = '';
            
            target.style.width = width[0].firstChild.data + 'px';

    /// Pridat odebrat buttonny            
			hght = parseInt(height[0].firstChild.data) - 15;

            target2 = document.getElementById('btnBoxTop');
            target2.innerHTML = '';
            
            target2.style.width = target.style.width;
            
            node = document.createElement('div');
            node.setAttribute('onclick', 'add(\''+ T_COLUMN +'\');');            
            node.className = "prid_sl";
            node.style.left = (width[0].firstChild.data) + 'px';
            node.style.top = hght + 'px';
            node.title = T_ADDCOLUMN;
            node.innerHTML = T_ADDCOLUMN;

            target2.appendChild(node);
            
            node = document.createElement('div');
            node.setAttribute('onclick', 'remove();');            
            node.className = "odebr_sl";
            node.style.left = (width[0].firstChild.data) + 'px';            
            node.style.top = (hght + 22) + 'px';
            node.title = T_RMCOLUMN;
            node.innerHTML = T_RMCOLUMN;
            
            target2.appendChild(node);
                        
    
    ////***********************
    
         /*   obj = xmlhttp.responseXML.getElementsByTagName('button');
            
            for (i = 0; i < obj.length; i++) {
                nodes = obj[i].childNodes;

                for (j = 0; j < nodes.length; j++){
                    if (nodes[j].nodeName == 'x')
                        x = parseInt(nodes[j].firstChild.data);
                        
                    if (nodes[j].nodeName == 'index')
                        index = nodes[j].firstChild.data;
                        
                    if (nodes[j].nodeName == 'type')
                        type = nodes[j].firstChild.data;
                }

                node = document.createElement('img');
                
                if (type == 'add') {
                    node.setAttribute('src', '/images/nav-plus.gif');
                    node.setAttribute('onclick', 'change_shelf_count_ar(' + index +', \'add\');');
                    node.title = T_ADDSHELF;
                } else {
                    node.setAttribute('src', '/images/nav-minus.gif');
                    node.setAttribute('onclick', 'change_shelf_count_ar(' + index +', \'rm\');');
                    node.title = T_RMSHELF;
                }
                
                node.style.cursor = 'pointer';
                node.style.position = 'absolute';
                node.style.left = x + 'px';

                target.appendChild(node);
            }*/
        }       
    }        
}

function change_shelf_count_ar(index, value){
    var id = document.getElementById('shelf_id');
    
    var headers= new Array();
    headers['Content-type']= 'application/x-www-form-urlencoded';
    headers['Connection']='close';
    
    var postvars = 'action=change_shelf_count_ar&index=' + index + '&value=' + value;

    var obj = document.getElementById('col_count_' + index);
    
    if (obj != null) {
        if (value == 'add') {
            if (obj.options[obj.selectedIndex].value < 7) {
                obj.selectedIndex += 1;
            }
        } else {
            var minimal = 5;
    		var tmpv = document.getElementById('col_height_' + index);
    		if (tmpv != null) {
    			if (tmpv.options[tmpv.selectedIndex].value == 2000)
					minimal = 4;
			} 
					
            if (obj.options[obj.selectedIndex].value > minimal) {
            	obj.selectedIndex -= 1;
            }
        }
    }
    
    if (!send_xmlhttprequest(refresh_service, 'POST', '/designer_m15_handle.php?id=' + id.value, postvars, headers)) {
        alert('Error in servise');
    }
}

function select_innerHTML(objeto,innerHTML){
    objeto.innerHTML = ""
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.toLowerCase().indexOf("<option")<0){//se não é option eu converto
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
  var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ //IE
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   //getting attributes
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }
   //getting styles
   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }
   //value and text
   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML
   //IE
   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}