{*
*	Module Name: Surface (Area) / Volume Calculator for Prestashop 1.5
*	Module URI: Please contact with info@megventure.com
*	Description: A utility to calculate quantity in area or volume units
*	Version: 1.0
*	Author: MEG Venture
*
*	Copyright 2013, MEG Venture (info@megventure.com)
*
*	This program is not a free software: you can't redistribute it and/or modify
*	it. All rights reserved.
*
*
*	This copyright notice  and licence should be retained in all modules based on this framework.
*	This does not affect your rights to assert copyright over your own original work.
*}
{if ($page_name == 'product') AND isset($admintab_display)} 
	{if ($admintab_type == "surface") AND ($admintab_display =="yes")}
<!-- Square Meter Calculation Module -->
	<link rel="stylesheet" type="text/css" href="{$module_dir}css/jquery-ui.css" />
	<link rel="stylesheet" type="text/css" href="{$module_dir}css/ui.spinner.css" />
	<script type="text/javascript" src="{$module_dir}js/jquery-ui.min.js"></script>
    {*<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>*}
	<script type="text/javascript" src="{$module_dir}js/ui.spinner.js"></script>
    <script type="text/javascript" src="{$module_dir}js/format.min.js"></script>

<script type="text/javascript">

function check_width(){
	var qty_el_alt = document.getElementById('quantity_wanted_alt');      
	var qty_alt = Number(qty_el_alt.value);
		if (qty_alt<{$admintab_minwidth}) 
		{
			qty_el_alt.value = Number({$admintab_minwidth}); 
		}	
		else if (qty_alt>{$admintab_maxwidth})
		{
			qty_el_alt.value = Number({$admintab_maxwidth}); 
		}
		else if (isNaN( qty_alt ))	
		{
			qty_el_alt.value = Number({$admintab_minwidth});
		}
	prod();
	return false;		
}
    

function check_height(){
	var qty_el_alth = document.getElementById('quantity_wanted_alth');      
	var qty_alth = Number(qty_el_alth.value);
		if (qty_alth<{$admintab_minheight}) 
		{
			qty_el_alth.value = Number({$admintab_minheight}); 
		}	
		else if (qty_alth>{$admintab_maxheight})
		{
			qty_el_alth.value = Number({$admintab_maxheight}); 
		}
		else if (isNaN( qty_alth ))	
		{
			qty_el_alth.value = Number({$admintab_minheight});
		}
	prod();
	return false;		
} 

function check_quantity(){
	var quantity = document.getElementById('quantity');      
	var quantity_value = Number(quantity.value);
		if (quantity_value<1) 
		{
			quantity.value = 1; 
		}	
		else if (isNaN( quantity_value ))	
		{
			quantity.value = 1;
		}
	prod();
	return false;		
} 
    
function prod() { 
//Quantity in m2
if ("{$admintab_conversion_unit}"=="m2")
{
	if ("{$admintab_minwidth_unit}"=="mm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = (Number(qty_el_alt.value))/1000; 	
			}
	else if ("{$admintab_minwidth_unit}"=="cm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = (Number(qty_el_alt.value))/100; 			
			}
	else if ("{$admintab_minwidth_unit}"=="dm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = (Number(qty_el_alt.value))/10; 			
			}			
	else if ("{$admintab_minwidth_unit}"=="m")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value); 			
			}	

	if ("{$admintab_minheight_unit}"=="mm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = (Number(qty_el_alth.value))/1000; 			
			}
	else if ("{$admintab_minheight_unit}"=="cm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = (Number(qty_el_alth.value))/100; 			
			}
	else if ("{$admintab_minheight_unit}"=="dm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = (Number(qty_el_alth.value))/10; 			
			}			
	else if ("{$admintab_minheight_unit}"=="m")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value); 			
			}		
			
	}
	
//Quantity in dm2
if ("{$admintab_conversion_unit}"=="dm2")
{
	if ("{$admintab_minwidth_unit}"=="mm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)/100; 			
			}
	else if ("{$admintab_minwidth_unit}"=="cm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)/10; 			
			}
	else if ("{$admintab_minwidth_unit}"=="dm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value); 			
			}			
	else
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)*10; 			
			}	

	if ("{$admintab_minheight_unit}"=="mm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)/100; 			
			}
	else if ("{$admintab_minheight_unit}"=="cm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)/10; 			
			}
	else if ("{$admintab_minheight_unit}"=="dm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value); 			
			}		
	else
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)*10; 			
			}	
	}
	
//Quantity in cm2
if ("{$admintab_conversion_unit}"=="cm2")
{
	if ("{$admintab_minwidth_unit}"=="mm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)/10; 			
			}
	else if ("{$admintab_minwidth_unit}"=="cm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value); 			
			}
	else if ("{$admintab_minwidth_unit}"=="dm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)*10; 			
			}			
	else
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)*100; 			
			}	

	if ("{$admintab_minheight_unit}"=="mm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)/10; 			
			}
	else if ("{$admintab_minheight_unit}"=="cm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value); 			
			}
	else if ("{$admintab_minheight_unit}"=="dm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)*10; 			
			}			
	else
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)*100; 			
			}	
						
	}
//Quanitity in mm2
if ("{$admintab_conversion_unit}"=="mm2")
{
	if ("{$admintab_minwidth_unit}"=="mm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value); 			
			}
	else if ("{$admintab_minwidth_unit}"=="cm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)*10; 			
			}
	else if ("{$admintab_minwidth_unit}"=="dm")
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)*100; 			
			}			
	else
		{
		var qty_el_alt = document.getElementById('quantity_wanted_alt');      
		var qty_alt = Number(qty_el_alt.value)*1000; 			
			}

			
	if ("{$admintab_minheight_unit}"=="mm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value); 			
			}
	else if ("{$admintab_minheight_unit}"=="cm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)*10; 			
			}
	else if ("{$admintab_minheight_unit}"=="dm")
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)*100; 			
			}
	else
		{
		var qty_el_alth = document.getElementById('quantity_wanted_alth');      
		var qty_alth = Number(qty_el_alth.value)*1000; 			
			}					
	}
	var qty_prod = document.getElementById('quantity_wanted');      
	var qty_production = Number(qty_prod.value);

	if ("{$admintab_addquan}"=="yes") {
	var req = document.getElementById('quantity');      
	var req_value = Number(req.value);
		if (("{$admintab_dispquan}"=="onevisible") || ("{$admintab_dispquan}"=="oneinvisible")) {					
			qty_prod.value = 1;
		}
		else {
			qty_prod.value = Math.ceil((qty_alth * qty_alt/{$admintab_conversion}) * req_value);
		}
  	var dimen=document.getElementById("dimension");
  	dimen.innerHTML="{l s='Width' mod='squaremeter'}&nbsp;" + qty_el_alt.value + "&nbsp;{$admintab_minwidth_unit},&nbsp;" + "{l s='Height' mod='squaremeter'}&nbsp;" + qty_el_alth.value + "&nbsp;{$admintab_minheight_unit},&nbsp;" + req_value + "&nbsp;{l s='ea' mod='squaremeter'}&nbsp;<br>(" + format( "#,##0.00##", Math.ceil(qty_alth * qty_alt * req_value)) + "&nbsp;{$admintab_conversion_unit})";	
	}
	else {
		if (("{$admintab_dispquan}"=="onevisible") || ("{$admintab_dispquan}"=="oneinvisible")) {					
			qty_prod.value = 1;
		}
		else {
			qty_prod.value = Math.ceil(qty_alth * qty_alt/{$admintab_conversion});
		}
  	var dimen=document.getElementById("dimension");
  	dimen.innerHTML="{l s='Width' mod='squaremeter'}&nbsp;" + qty_el_alt.value + "&nbsp;{$admintab_minwidth_unit},&nbsp;" + "{l s='Height' mod='squaremeter'}&nbsp;" + qty_el_alth.value + "&nbsp;{$admintab_minheight_unit}" + "<br>(" + format( "#,##0.00##", Math.ceil(qty_alth * qty_alt)) + "&nbsp;{$admintab_conversion_unit})";	
	}

if (("{$admintab_dispprice}"=="pricetotalprice") || ("{$admintab_dispprice}"=="nopricetotalprice")) {					
		totalpriceinfo();
	}
	
	var dimx = dimen.innerHTML;
	dimx=dimx.replace(/&nbsp;/gi," ");
    dimx=dimx.replace(/&amp;/gi,"&");
    dimx=dimx.replace(/&quot;/gi,'"');
	dimx=dimx.replace(/<br>/gi,', ');
    dimx=dimx.replace(/&lt;/gi,'<');
    dimx=dimx.replace(/&gt;/gi,'>');
	
	document.getElementById("dim").value = dimx; 

  
	return false;
}  

function calc_pos(){
  var qty_position = document.getElementById('quantity_wanted_p');
  var reference = document.getElementById('squaremeter');
  var conversion = document.getElementById('conversion'); 
if ("{$admintab_dispprice}"!="pricenototalprice")  { 
  var addtocart = document.getElementById('add_to_cart');  
  var totalprice = document.getElementById('totalprice');    
}
  var dimension = document.getElementById('dimension');     

  qty_position.parentNode.insertBefore(reference, qty_position);
  qty_position.parentNode.insertBefore(conversion, qty_position.nextSibling);
  qty_position.parentNode.insertBefore(dimension, qty_position); 
if ("{$admintab_dispprice}"!="pricenototalprice")  {        
  addtocart.parentNode.insertBefore(totalprice, addtocart);   
}      
  }

function totalpriceinfo() {
	var qty_prod = document.getElementById('quantity_wanted');      
	var qty_production = Number(qty_prod.value);
	var price = document.getElementById('our_price_display');
	var pricecurrencyval = price.innerHTML.replace(/[0-9.,-]/g, '');
	var totalprice = qty_production * productPrice
  	var totalpriceinfo = document.getElementById("totalpriceinfo");
	totalpriceinfo.innerHTML = "Total Price (Tax and Shipping Excl.) : " + pricecurrencyval + format( "#,##0.00##", totalprice);
}

window.onload = function(){
	calc_pos();
	check_height();
	check_width();
	document.getElementById('quantity_wanted').parentNode.getElementsByTagName('label')[0].firstChild.data ='{l s='Order Quantity:' mod='squaremeter'}';		
	document.getElementById("quantity_wanted").readOnly=true;
	if (("{$admintab_dispquan}"=="oneinvisible") || ("{$admintab_dispquan}"=="calcinvisible"))
		{	
		document.getElementById("quantity_wanted_p").style.display='none';
		}	
	if (("{$admintab_dispprice}"=="nopricebutton") || ("{$admintab_dispprice}"=="nopricetotalprice")) {					
		document.getElementById("our_price_display").style.display='none';
		document.getElementById("reduction_percent").style.display='none';
		document.getElementById("reduction_amount").style.display='none';
		document.getElementById("old_price").style.display='none';		
	}
}

		jQuery().ready(function($) {
			$('#quantity_wanted_alt').spinner({ min: {$admintab_minwidth}, max: {$admintab_maxwidth}, step:0.1, showOn: 'both' });
			$('#quantity_wanted_alth').spinner({ min: {$admintab_minheight}, max: {$admintab_maxheight}, step:0.1, showOn: 'both' });
			$('#quantity').spinner({ min: 1, showOn: 'both' });			
		});
</script>

<script type="text/javascript">

$(document).ready(function(){
  $("span#our_price_display").change(function(){
    prod();
  });
});

</script>

	{/if}
{/if}
<!-- Square Meter Calculation Module -->

