$(document).ready(function() { 
	$("#products-detailnav .tabs li a").click(function() { 
		openTab($(this)); return false; 
	});
	
	$("#calculator-nav .tabs li a").click(function() { 
		openTabCalculator($(this)); return false; 
	});
	
	$("#step1_next").click(function() { 
		openTabCalculatorNext(1); return false; 
	});
	
	$(".step2_nextButton").click(function() { 
		openTabCalculatorNext(2); return false; 
	});
	
	$("#step3_next").click(function() { 
		openTabCalculatorNext(3); return false;
	});
 
	var currentTab = 0; 
	$("#products-detailnav .tabs li a:eq("+currentTab+")").click();
	
	var currentTab = 0; 
	$("#calculator-nav .tabs li a:eq("+currentTab+")").click();
	
 
	function openTab(clickedTab) {
		var thisTab = $("#products-detailnav .tabs a").index(clickedTab);
		$("#products-detailnav .tabs li").removeClass("on");
		$("#products-detailnav .tabs li:eq("+thisTab+")").addClass("on");
		$("#product-details-inner .tabbed-content").hide(500);
		$("#product-details-inner .tabbed-content:eq("+thisTab+")").show(500);
		currentTab = thisTab;
	}
	
	function openTabCalculator(clickedTab) {
		var thisTab = $("#calculator-nav .tabs a").index(clickedTab);
		
		var error = errorCheck(thisTab+1);
		
		if(error == false) {
			//Stop Function
		} else {
		
			$("#calculator-nav .tabs li").removeClass("on");
			$("#calculator-nav .tabs li:eq("+thisTab+")").addClass("on");
			$("#calculator-nav-inner .tabbed-content").hide(0);
			$("#calculator-nav-inner .tabbed-content:eq("+thisTab+")").show(0);
			currentTab = thisTab;
			
			calculateHouseCost(); 
			calculateWaterCost();
		}
	}
	
	function openTabCalculatorNext(thisTab) {
		
		var error = errorCheck(thisTab+1);
		
		if(error == false) {
			//Stop Function
		} else {
		
			$("#calculator-nav .tabs li").removeClass("on");
			$("#calculator-nav .tabs li:eq("+thisTab+")").addClass("on");
			$("#calculator-nav-inner .tabbed-content").hide(0);
			$("#calculator-nav-inner .tabbed-content:eq("+thisTab+")").show(0);
			currentTab = thisTab;
			calculateHouseCost(); 
			calculateWaterCost();
		
		}
	}
	
	$('.accordion .head').parent().next().hide();
	$('.accordion .head').click(function () {
		$(this).parent().next().toggle(200);
		if ($(this).children('img').attr('src') == 'images/arrow_orange.png') {
			//$(this).children('span').text('[-]');
			$(this).children('img').attr('src', 'images/arrow_down_orange.png');
		}
		else {
			//$(this).children('span').text('[+]');
			$(this).children('img').attr('src', 'images/arrow_orange.png');
		}
		return false;
	}).next().hide();
});

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function calcTotal(thisid, thisfactor, resultbox) {
	var thisValue = thisid;
	if(thisValue != "" && IsNumeric(thisValue)) {
		var total = thisValue * thisfactor;
		document.getElementById(resultbox).innerHTML = "$" + addCommas(total.toFixed(2));
	}
	else if(thisValue != "") {
		document.getElementById(resultbox).innerHTML = "Cost per unit must be numeric";
	}
	//alert(thisValue);
}

function settab(thistab) {
	//alert(document.MM_sr[0].oSrc);
	//alert(x.oSrc);
	var thiselement = "";
	var thisimage = "";
	for(i = 1; i <= 4; i++) {
		var thisitem = "tab_content_" + i;
		var thisicon = "tab_off_" + i;
		if(i == thistab) {
			thiselement = document.getElementById(thisitem);
			thiselement.style.display = "block";
			thisimage = document.getElementById(thisicon);
			thisimage.src = "images/tab_on_" + i + ".gif";
			document.MM_sr[0].oSrc = "images/tab_on_" + i + ".gif";
			
		}
		else {
			thiselement = document.getElementById(thisitem);
			thiselement.style.display = "none";
			thisimage = document.getElementById(thisicon);
			thisimage.src = "images/tab_off_" + i + ".gif";
			//document.MM_sr[0].oSrc = "images/tab_off_" + i + ".gif";
			
		}
	}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function viewpageid(thispage, thisid, thiswidth, thisheight) {
	var thiswindow;
	thiswindow = window.open(thispage + '?id=' + thisid, 'viewpage', 'width=' + thiswidth + ',height=' + thisheight + ',scrollbars,resizable');
	thiswindow.focus();
}
function viewpage(thispage, thiswidth, thisheight) {
	var thiswindow;
	thiswindow = window.open(thispage, 'viewpage2', 'width=' + thiswidth + ',height=' + thisheight + ',scrollbars,resizable');
	thiswindow.focus();
}
function viewimage(thisimg, thiswidth, thisheight) {
	var thiswindow;
	thiswindow = window.open('viewimage.php?id=' + thisimg, 'viewphoto', 'width=' + thiswidth + ',height=' + thisheight + ',scrollbars,resizable');
	//alert('viewimage.php?id=' + thisimg);
	thiswindow.focus();
}
function viewphotogallery(thisimg, thiswidth, thisheight) {
	var thiswindow;
	thiswindow = window.open('viewphotogallery.php?id=' + thisimg, 'viewphotogallery', 'width=' + thiswidth + ',height=' + thisheight + ',scrollbars,resizable');
	//alert('viewimage.php?id=' + thisimg);
	thiswindow.focus();
}
function closethiswindow() {
	window.close();
	window.opener.focus();
}
function inittb(pagestr)
{
tb_show('Loaded onload', pagestr, null);
}
function setBackground(thisid, thisnewimage) {
	if(thisnewimage == "inherit" || thisnewimage == "none") {
		document.getElementById(thisid).style.backgroundImage = thisnewimage
	}
	else {
		document.getElementById(thisid).style.backgroundImage = "url(" + thisnewimage + ")"
	}
}


function setzipprod() {
	if (document.prodform.prod_wm.checked) {
		document.zipform.prod_wm.value = "y";
		document.cityform.prod_wm.value = "y";	
	} else {
		document.zipform.prod_wm.value = "";
		document.cityform.prod_wm.value = "";
	}
	if (document.prodform.prod_wmp.checked) {
		document.zipform.prod_wmp.value = "y";
		document.cityform.prod_wmp.value = "y";	
	} else {
		document.zipform.prod_wmp.value = "";
		document.cityform.prod_wmp.value = "";
	}
	if (document.prodform.prod_hl.checked) {
		document.zipform.prod_hl.value = "y";
		document.cityform.prod_hl.value = "y";	
	} else {
		document.zipform.prod_hl.value = "";
		document.cityform.prod_hl.value = "";
	}
	if (document.prodform.prod_flex.checked) {
		document.zipform.prod_flex.value = "y";
		document.cityform.prod_flex.value = "y";	
	} else {
		document.zipform.prod_flex.value = "";
		document.cityform.prod_flex.value = "";
	}
	
	if (document.prodform.prod_grill.checked) {
		document.zipform.prod_grill.value = "y";
		document.cityform.prod_grill.value = "y";	
	} else {
		document.zipform.prod_grill.value = "";
		document.cityform.prod_grill.value = "";
	}
	
	if (document.prodform.prod_biomax.checked) {
		document.zipform.prod_biomax.value = "y";
		document.cityform.prod_biomax.value = "y";	
	} else {
		document.zipform.prod_biomax.value = "";
		document.cityform.prod_biomax.value = "";
	}
	
	
	
	document.zipform.submit();
}

function setcityprod() {
	if (document.prodform.prod_wm.checked) {
		document.zipform.prod_wm.value = "y";
		document.cityform.prod_wm.value = "y";	
	} else {
		document.zipform.prod_wm.value = "";
		document.cityform.prod_wm.value = "";
	}
	if (document.prodform.prod_wmp.checked) {
		document.zipform.prod_wmp.value = "y";
		document.cityform.prod_wmp.value = "y";	
	} else {
		document.zipform.prod_wmp.value = "";
		document.cityform.prod_wmp.value = "";
	}
	if (document.prodform.prod_hl.checked) {
		document.zipform.prod_hl.value = "y";
		document.cityform.prod_hl.value = "y";	
	} else {
		document.zipform.prod_hl.value = "";
		document.cityform.prod_hl.value = "";
	}
	if (document.prodform.prod_flex.checked) {
		document.zipform.prod_flex.value = "y";
		document.cityform.prod_flex.value = "y";	
	} else {
		document.zipform.prod_flex.value = "";
		document.cityform.prod_flex.value = "";
	}
	
	if (document.prodform.prod_grill.checked) {
		document.zipform.prod_grill.value = "y";
		document.cityform.prod_grill.value = "y";	
	} else {
		document.zipform.prod_grill.value = "";
		document.cityform.prod_grill.value = "";
	}
	if (document.prodform.prod_biomax.checked) {
		document.zipform.prod_biomax.value = "y";
		document.cityform.prod_biomax.value = "y";	
	} else {
		document.zipform.prod_biomax.value = "";
		document.cityform.prod_biomax.value = "";
	}
	document.cityform.submit();
}
