function estimate_formIT_count() {
	var form = document.forms["estimator"];
	var s = form.thickness.value * form.footage.value * (100+parseInt(form.allowance.value))/100;
	var num_602 = Math.floor(s/602);
//	alert(s - num_602 * 602);
	var num_202 = Math.floor((s - num_602 * 602)/202);
	var num_12 = Math.ceil((s - num_602 * 602 - num_202 * 202)/12);
	//alert( "num_602: "+num_602 + "  num_202:" + num_202 + "  num_12:" + num_12 );
	if ( parseInt(num_12) >= 10 ) {
		num_202 = parseInt(num_202) + 1;
		num_12 = 0;
	}
	if ( parseInt(num_202) >= 2 ) {
		num_602 = parseInt(num_602) + 1;
		num_202 = 0;
		num_12 = 0;
	}
	
	document.getElementById("num_602").innerHTML = num_602;
	document.getElementById("num_202").innerHTML = num_202;
	document.getElementById("num_12").innerHTML = num_12;
	document.getElementById("num_total").innerHTML = num_602+num_202+num_12;
	document.getElementById("add_to_cart_link").innerHTML = '<a href="javascript:get_addtocart('+num_602+','+num_202+','+num_12+')" class="action_button">Add to Cart</a>';
	
	document.getElementById("answer").style.display = 'block';
}

function get_addtocart ( num_602, num_202, num_12 ) {
	// ajax call to add to cart
	if ( addCart ) {
		addCart = false;
		
	if ( num_602 != 0 ) {
		var opt = {
			asynchronous: false
		};
		new Ajax.Request( '/products/index.php?page=shop.cart&option=com_virtuemart&func=cartadd&category_id=6&product_id=18&quantity='+num_602, opt);
	}
	
	if ( num_202 != 0 ) {
		var opt = {
			method: 'get',
			asynchronous: false,			
			onSuccess: function(t) {
			},
			// Handle 404
			on404: function(t) {
				alert('Error 404: location "' + t.statusText + '" was not found.');
			},
			// Handle other errors
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
		}
		new Ajax.Request( '/products/index.php?page=shop.cart&option=com_virtuemart&func=cartadd&category_id=6&product_id=17&quantity='+num_202, opt);
	}

	if ( num_12 != 0 ) {
		var opt = {
			asynchronous: false
		};
		new Ajax.Request( '/products/index.php?page=shop.cart&option=com_virtuemart&func=cartadd&category_id=6&product_id=31&quantity='+num_12, opt);	
	}
	
	window.location = "/products/index.php?page=shop.cart&option=com_virtuemart";
	
	}
}

var addCart = true;
var walls_idx = 1, ceilings_idx=1;
var price_602=609.98, price_202=329.99, price_12=29.99;
function calculate_sqft() {
	var walls_sqrt = 0, ceilings_sqrt = 0, total_sqrt;
	var i, length, height;
	
	if ( !$("dimension_radio").checked ) {
		total_sqrt = $("total_sqrt_value").value;
	}
	else {
		for ( i=0; i<walls_idx; i++ ) {
			length = parseInt($("wall_length_"+i).value);
			height = parseInt($("wall_height_"+i).value);
			if ( length != "" && height != "" && !isNaN(length) && !isNaN(height)) {
				walls_sqrt += length*height;
				$("wall_sqrt_"+i).innerHTML = length*height;
			}
		}
		
		for ( i=0; i<ceilings_idx; i++ ) {
			length = parseInt($("ceiling_length_"+i).value);
			height = parseInt($("ceiling_height_"+i).value);
			
			if ( length != "" && height != "" && !isNaN(length) && !isNaN(height)) {
				ceilings_sqrt += length*height;
				$("ceiling_sqrt_"+i).innerHTML = length*height;
			}
		}
		
		total_sqrt = parseInt(walls_sqrt) + parseInt(ceilings_sqrt) - parseInt($("windows_sqrt").value);
		$("total_sqrt").innerHTML = total_sqrt;
	}
	
	calculate_foamIT_count(total_sqrt);
	calculate_payback_period();
}

function calculate_sqft_b() {
	calculate_sqft();
}

function add_another_wall() {

	 new Insertion.Before( 'row_ceiling_sqrt_0', '\n    <tr><td>&nbsp;</td>\n'+
'      <td><input name="wall_length_'+walls_idx+'" id="wall_length_'+walls_idx+'" type="text" value="" size="4" maxlength="8" onkeyup="calculate_sqft();"></td>\n'+
'      <td>X</td>\n'+
'      <td><input name="wall_height_'+walls_idx+'" id="wall_height_'+walls_idx+'" type="text" value="" size="4" maxlength="8" onkeyup="calculate_sqft();"></td>\n'+
'      <td>=</td>\n'+
'      <td id="wall_sqrt_'+walls_idx+'"></td>\n'+
'      <td>&nbsp;</td>\n'+
'	</tr>\n' );
	walls_idx ++;
}

function add_another_ceiling() {

	 new Insertion.Before( 'row_windows', '\n    <tr><td>&nbsp;</td>\n'+
'      <td><input name="ceiling_length_'+ceilings_idx+'" id="ceiling_length_'+ceilings_idx+'" type="text" value="" size="4" maxlength="8" onkeyup="calculate_sqft();"></td>\n'+
'      <td>X</td>\n'+
'      <td><input name="ceiling_height_'+ceilings_idx+'" id="ceiling_height_'+ceilings_idx+'" type="text" value="" size="4" maxlength="8" onkeyup="calculate_sqft();"></td>\n'+
'      <td>=</td>\n'+
'      <td id="ceiling_sqrt_'+ceilings_idx+'"></td>\n'+
'      <td>&nbsp;</td>\n'+
'	</tr>\n' );
	ceilings_idx ++;
}

function calculate_foamIT_count(footage) {
	
	var s = $("thickness").value * footage * (100+parseInt($("allowance").value))/100;
	var num_602 = Math.floor(s/602);
//	alert(s - num_602 * 602);
	var num_202 = Math.floor((s - num_602 * 602)/202);
	var num_12 = Math.ceil((s - num_602 * 602 - num_202 * 202)/12);
	//alert( "num_602: "+num_602 + "  num_202:" + num_202 + "  num_12:" + num_12 );
	if ( parseInt(num_12) >= 10 ) {
		num_202 = parseInt(num_202) + 1;
		num_12 = 0;
	}
	if ( parseInt(num_202) >= 2 ) {
		num_602 = parseInt(num_602) + 1;
		num_202 = 0;
		num_12 = 0;
	}
	
	var cost_602 = num_602 * price_602;
	var cost_202 = num_202 * price_202;
	var cost_12 = num_12 * price_12;
	
	document.getElementById("num_602").innerHTML = num_602;
	document.getElementById("num_202").innerHTML = num_202;
	document.getElementById("num_12").innerHTML = num_12;
	document.getElementById("num_total").innerHTML = num_602+num_202+num_12;
	document.getElementById("cost_602").innerHTML = cost_602.toFixed(2);
	document.getElementById("cost_202").innerHTML = cost_202.toFixed(2);
	document.getElementById("cost_12").innerHTML = cost_12.toFixed(2);
	document.getElementById("cost_total").innerHTML = (cost_602+cost_202+cost_12).toFixed(2);
	document.getElementById("add_to_cart_link").innerHTML = '<a href="javascript:get_addtocart('+num_602+','+num_202+','+num_12+')" class="action_button">Add to Cart</a>';
	addCart = true;
	
}

function calculate_payback_period() {
	var cost_total = parseInt(document.getElementById("cost_total").innerHTML);
	var mo_saving  = parseInt(document.getElementById('mo_saving').innerHTML);
	if ( cost_total!="" && mo_saving!="" && !isNaN(mo_saving) && !isNaN(cost_total) ) {
		document.getElementById("payback_months").innerHTML = Math.round(cost_total/mo_saving);
	}
	else {
		document.getElementById("payback_months").innerHTML = "?";
	}
}

// JavaScript Document
sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" over";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" over\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function gsfLoadVideo () {
	 flashembed("get_video", 
	
		/* 
			first argument supplies standard Flash parameters. See full list:
			http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701
		*/
		{
			src:'/video/flowplayer-2009/FlowPlayerDark.swf',
			wmode: 'transparent',
			width: 290, 
			height: 239
		},
		
		/*
			second argument is Flowplayer specific configuration. See full list:
			http://flowplayer.org/player/configuration.html
			Mar 2010: '/video/FIG_Promo_Video.flv'
			prior to Mar 2010: '/video/The_Easy_To_Use_Spray_Foam_Kit.flv'
		*/
		{config: {   
			autoPlay: false,
			autoBuffering: true,
			controlBarBackgroundColor:'0x00804c',
			initialScale: 'scale',
			videoFile: '/video/FIG_Promo_Video.flv'
		}} 
	);
}

function catstr(thisform){
	thisform.query.value="site:www.sprayfoamdirect.com "+thisform.query.value
}
