


function cellOnFocus(el){


	


	if(getColorHex(el.style.backgroundColor) == "#f4f4f4" || getColorHex(el.style.backgroundColor) == ""){


		el.style.backgroundColor = "#f3e8cc";


	}


	


}





function cellOutFocus(el){


	


	if(getColorHex(el.style.backgroundColor) == "#f3e8cc"){


		el.style.backgroundColor = "#f4f4f4";


	}


	drawShopTable();


	


}





function cellSet(el, type, img){





	if (type == 1){


		document.getElementById('tr_picture').value 		= el;


		document.getElementById('tr_picture_url').value 	= img;


	}


	


	if(type == 2){


		document.getElementById('tr_top_image').value 		= el;		


		document.getElementById('tr_top_image_url').value 	= img;		


	}


	


	if(type == 3){


		document.getElementById('tr_border').value 			= el;		


		document.getElementById('tr_border_url').value 		= img;		


	}


	


	if(type == 4){


		document.getElementById('tr_bottom_image').value 		= el;		


		document.getElementById('tr_bottom_image_url').value 	= img;		


	}





	drawShopTable();


		


}





function getColorHex(currentColor){


	


	if (currentColor.indexOf("rgb") >= 0){


		


        var rgbStr 		= currentColor.slice(currentColor.indexOf('(') + 1,currentColor.indexOf(')'));


        var rgbValues 	= rgbStr.split(",");


        var hexChars 	= "0123456789abcdef";


        currentColor 	= "#";


        


        for (var i = 0; i < 3; i++){


            var v = rgbValues[i].valueOf();


            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);


        }


    }


    


    return currentColor;


}


function setImageTop() {
	document.getElementById('top_image').src = miritec_url['top_image'];
}
function setImagePic() {
	document.getElementById('picture').src = miritec_url['picture'];
}
function setImageBot() {
	document.getElementById('bottom_image').src = miritec_url['bottom_image'];
}

var miritec_timeout = new Array();
var miritec_url = new Array();
var miritec_tm = 100;

function setImage(imgObj, url) {
	if (miritec_timeout && miritec_timeout[imgObj]) clearTimeout(miritec_timeout[imgObj]);
	miritec_url[imgObj] = url;
	if (imgObj == 'top_image') {
		miritec_timeout[imgObj] = setTimeout(setImageTop, miritec_tm);
	}
	if (imgObj == 'picture') {
		miritec_timeout[imgObj] = setTimeout(setImagePic, miritec_tm);
	}
	if (imgObj == 'bottom_image') {
		miritec_timeout[imgObj] = setTimeout(setImageBot, miritec_tm);
	}
	
}


function activateInput(id){


	if(document.getElementById(id).disabled == 0){


		document.getElementById(id).value 		= 0;


		document.getElementById(id).disabled 	= 1;


		countCost();


	}else{


		document.getElementById(id).disabled = 0;


		document.getElementById(id).value = "";


		document.getElementById(id).focus();


	}


	


}





function countCost(){


	


  var items_count		= 0;


  var total_cost 		= 0;


  var total_cost_many 	= 0;


  	


  var inputs_vals		= new Array();


  var items_costs_vals	= new Array();


  var items_costs_many_vals	= new Array();





  


  var table = document.getElementById('items_table');


	


  if (table){


	  


	  var inputs = table.getElementsByTagName("input");


	


	  if (inputs){


	  


	  	for (var t=0; t < inputs.length; t++){


			if	(inputs[t].className == "input_text"){


				


				if (parseInt(inputs[t].value) > 0){


					inputs_vals.push(parseInt(inputs[t].value));


				}else{


					inputs_vals.push(0);


				}


				


			}


	  	}


	  }


	    


	  var items_costs		= table.getElementsByTagName("div");


	


	  if (items_costs){


	  


	  	for (var t=0; t < items_costs.length; t++){


			if	(items_costs[t].className == "item_cost"){


				


				if (parseFloat(items_costs[t].innerHTML) > 0){


					items_costs_vals.push(parseFloat(items_costs[t].innerHTML));


				}else{


					items_costs_vals.push(0);


				}


				


			}


	  	}


	  }


	


	  var items_costs_many	= table.getElementsByTagName("div");


	


	  if (items_costs_many){


	  


	  	for (var t=0; t < items_costs_many.length; t++){


			if	(items_costs_many[t].className == "item_cost_many"){


	


				if (parseFloat(items_costs_many[t].innerHTML) > 0){


					items_costs_many_vals.push(parseFloat(items_costs_many[t].innerHTML));


				}else{


					items_costs_many_vals.push(0);


				}


				


			}


	  	}


	  }


	  


	  for (var t=0; t < inputs_vals.length; t++){


			


		  items_count 		+= inputs_vals[t];


		  total_cost 		+= (inputs_vals[t] * items_costs_vals[t]);


		  total_cost_many 	+= (inputs_vals[t] * items_costs_many_vals[t]);


	  }


	  


	  if (document.getElementById('items_count')){ 


	  	document.getElementById('items_count').innerHTML 		= items_count;


	  }


	  if (document.getElementById('total_cost')){


	  	document.getElementById('total_cost').innerHTML 		= total_cost.toFixed(2) + "грн.";


	  }


	  if (document.getElementById('total_cost_many')){


	  	document.getElementById('total_cost_many').innerHTML 	= total_cost_many.toFixed(2)  + "грн.";


	  }


	  


	  if (document.getElementById('cost')){


		  document.getElementById('cost').value 		= total_cost.toFixed(2);


	  }


	  


	  if (document.getElementById('cost_many')){


		  document.getElementById('cost_many').value 	= total_cost_many.toFixed(2);


	  }


	  


  }


  


}








function drawShopTable(){


	


	var items_table = document.getElementById('items_table');


	


	if (items_table){


	


		var trtags = items_table.getElementsByTagName("tr");


		  


		for (var t=1; t < trtags.length; t++){


			trtags[t].style.backgroundColor = "#f4f4f4";


		}


		


		if (document.getElementById('tr_picture')){


			trtags[document.getElementById('tr_picture').value].style.backgroundColor 		= "#f3e8cd";


			setImage('picture', document.getElementById('tr_picture_url').value);


		}


		


		if (document.getElementById('tr_top_image')){


			trtags[document.getElementById('tr_top_image').value].style.backgroundColor 	= "#f3e8cd";


			setImage('top_image', document.getElementById('tr_top_image_url').value);


		}


		


		if (document.getElementById('tr_border')){


			trtags[document.getElementById('tr_border').value].style.backgroundColor 		= "#f3e8cd";


			setImage('border', document.getElementById('tr_border_url').value);


		}


		


		if (document.getElementById('tr_bottom_image')){


			trtags[document.getElementById('tr_bottom_image').value].style.backgroundColor 	= "#f3e8cd";


			setImage('bottom_image', document.getElementById('tr_bottom_image_url').value);


		}


	}

	countCost();

}




if (window.addEventListener){
	window.addEventListener("load", drawShopTable, false);
}else if (window.attachEvent){
	window.attachEvent("onload", drawShopTable);
}
