var poolType;

/* - - - the images - - - */
var elipse = new Image(184, 150);
elipse.src = 'images/elipse.gif';
var circle = new Image(184,150);
circle.src = 'images/circle.gif';
var rectangle = new Image(184,150);
rectangle.src = 'images/rectangle.gif';

var total;
var poolImage;
var widthRow;
var lengthRow;
var radiusRow;
var diameterRow1;
var diameterRow2;



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;
}

/* - - - the images - - - */
function calculateVolume(){
	var volDepth = parseFloat(document.volumeCalculator.volDepth.value);
	var volLength = parseFloat(document.volumeCalculator.volLength.value);
	var volWidth = parseFloat(document.volumeCalculator.volWidth.value);
	var volRadius = parseFloat(document.volumeCalculator.volRadius.value);
	var volDiameter1 = parseFloat(document.volumeCalculator.volDiameter1.value);
	var volDiameter2 = parseFloat(document.volumeCalculator.volDiameter2.value);

	
	switch(poolType){
		case 'rect': 
			if (isNaN(volDepth) || isNaN(volLength) || isNaN(volWidth)) {
				alert('Please fill out the form completely.');
			} else {
				var volume = volDepth * volLength * volWidth * 7.5;
				total.innerHTML = addCommas(parseInt(volume)) +' gallons';
			}
			break;
		case 'elip':
			if (isNaN(volDiameter1) || isNaN(volDiameter2) || isNaN(volLength) || isNaN(volDepth)) {
				alert('Please fill out the form completely.');
			} else {
				var volume = ((volDiameter1 + volDiameter2 ) * volLength) * volDepth * 7.5;
				total.innerHTML = addCommas(parseInt(volume)) +' gallons';
			}
			break;
		case 'circ':
			if (isNaN(volDepth) || isNaN(volRadius)) {
				alert('Please fill out the form completely.');
			} else {
				var volume = (volRadius * volRadius * 3.14 ) * volDepth * 7.5;
				total.innerHTML = addCommas(parseInt(volume)) +' gallons';
			}
			break
	}
}

function poolFormDisplay(){
	var poolChoice = document.volumeCalculator.poolType.value;
	
	total = document.getElementById('total');
	poolImage = document.getElementById('poolImage');
	widthRow = document.getElementById('widthRow');
	lengthRow = document.getElementById('lengthRow');
	radiusRow = document.getElementById('radiusRow');
	diameterRow1 = document.getElementById('diameterRow1');
	diameterRow2 = document.getElementById('diameterRow2');
	
	switch(poolChoice){
		case 'rectangle': 
			poolType = 'rect';
			poolImage.src = rectangle.src; 
			widthRow.style.display = '';
			lengthRow.style.display = '';
			diameterRow1.style.display = 'none';
			diameterRow2.style.display = 'none';
			radiusRow.style.display = 'none';
			total.innerHTML ='';
			break;
		case 'elipse': 
			poolType = 'elip';
			poolImage.src = elipse.src; 
			widthRow.style.display = 'none';
			diameterRow1.style.display = '';
			diameterRow2.style.display = '';
			lengthRow.style.display = '';
			radiusRow.style.display = 'none';
			total.innerHTML ='';
			break;
		case 'circle': 
			poolType = 'circ';
			widthRow.style.display = 'none';
			lengthRow.style.display = 'none';
			diameterRow1.style.display = 'none';
			diameterRow2.style.display = 'none';
			radiusRow.style.display = '';
			poolImage.src = circle.src; 
			total.innerHTML ='';
			break;
	}
}

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];}
}
