var $j = jQuery.noConflict();

function isFloat(val) {
	if(!val || (typeof val != "string" || val.constructor != String)) {
		return(false);
	}
	var isNumber = !isNaN(new Number(val));
	if(isNumber) {
		if(val.indexOf('.') != -1) {
		return(true);
	} else {
		return(false);
	}
	} else {
	return(false);
	}
}

function calculateArea(product_id){
		//validation 
		if( ($('width_ft_'+product_id).value.search(/\S/) == -1) ){ 
			$('width_ft_'+product_id).className = 'error';
			$('width_ft_'+product_id).focus();
			return false;
		}else $('width_ft_'+product_id).className = 'textbox';
		
		if($('width_ft_'+product_id).value < 3){
			$('width_ft_'+product_id).value = '';
			alert("Please enter minimum dimension of 3 ft");
			$('width_ft_'+product_id).focus();
			return false;
		}
		
		if( ($('length_ft_'+product_id).value.search(/\S/) == -1) ){ 
			$('length_ft_'+product_id).className = 'error';
			$('length_ft_'+product_id).focus();
			return false;
		}else $('length_ft_'+product_id).className = 'textbox';
		
		if($('length_ft_'+product_id).value < 3){
			$('length_ft_'+product_id).value = '';
			alert("Please enter minimum dimension of 3 ft");
			$('length_ft_'+product_id).focus();
			return false;
		}
		//end of validation 
		var width_ft  = parseInt($('width_ft_'+product_id).value);
		if($('width_in_'+product_id).value.search(/\S/) != -1)// if inch given
			width_ft = width_ft + parseInt($('width_in_'+product_id).value)/12;
			
		var length_ft = parseInt($('length_ft_'+product_id).value);
		if($('length_in_'+product_id).value.search(/\S/) != -1)// if inch given
			length_ft = length_ft + parseInt($('length_in_'+product_id).value)/12;
			
		var ft  = width_ft*length_ft; //calculate sq ft
		ft = ft.toString();
		if(isFloat(ft)){ 
		  ft = parseFloat(ft);
		  ft = ft.toFixed(2); // rounding upto 2 decimal numbers
		}	
		$('sq_ft_area_'+product_id).value = ft; //put sq ft area into the hidden field
		
		var yd = ft/9; //calculate sq yd
		yd = yd.toString();
		if(isFloat(yd)){ 
		  yd = parseFloat(yd);
		  yd = yd.toFixed(2); // rounding upto 2 decimal numbers
		}
		$('sq_yd_area_'+product_id).value = yd; //put sq yd area into the hidden field
		
		$('estimate_'+product_id).style.display = '';
		$('area_'+product_id).innerHTML = yd +' yd<sup>2</sup> &nbsp;( '+ ft +' ft<sup>2</sup> )'; //display area
		
		//calculate estimate price
		var sizePrice = $('pack_size_'+product_id).value;
		sizePriceArr = sizePrice.split("^"); 
		$('price_'+product_id).value = sizePriceArr[1];
		estimate(product_id);
		//end of calculate estimate price
		
		return false;
}

function estimate(product_id){
		
	var area = parseFloat($('sq_yd_area_'+product_id).value);// take sq yd area from the hidden field
	area     = area + area/20;// 5% has been added on for wastage
	var pack_size = parseFloat($('pack_size_'+product_id).value);
	
	var packs = area/pack_size;
	packs = Math.ceil(packs); // rounding up to upper
	
	pack_size = pack_size.toString();
	if(!isFloat(pack_size)){
		pack_size = parseFloat(pack_size);
		pack_size = pack_size.toFixed(1);// rounding upto 1 decimal numbers
	}
	
	$('estimate_area_'+product_id).innerHTML = 'Your estimate<br><b>'+packs+' packs, '+pack_size+' yd<sup>2</sup> per pack</b><br>5% has been added on for waste';
	
	var price = parseFloat($('price_'+product_id).value);
	if($('price_'+product_id).value.search(/\S/) == -1) price = 0;
	var cost = packs*price;
	$('cost_'+product_id).innerHTML = '&euro; '+cost.toFixed(2);// rounding upto 2 decimal numbers
	return false;
}
function getPrice(product_id){
	var sizePrice = $('pack_size_'+product_id).value;
	sizePriceArr = sizePrice.split("^");
	$('price_'+product_id).value = sizePriceArr[1];
	
}