// JavaScript Document
sectionImages = new Array() ;
ColorImages = new Array();
var http = getHTTPObject();

function recalculateFence(src)
{
	
	footage = frmQuote.footage.value;
	
	section_width =  frmQuote.section_width.value;
	section_price = frmQuote.section_price.value;
	//if the selected color is TAN  then ass 10%
	
	sections_required = Math.ceil(footage / section_width);
	
	posts_required = sections_required + 1;
	spantotalfootage.innerHTML = footage+" Fts.";
	spantotalsections.innerHTML = sections_required;
	spantotalposts1.innerHTML = posts_required;
	spantotalposts2.innerHTML = posts_required;
	spantotalposts3.innerHTML = posts_required;
	
	//if there is change in footage
	if(src=="footage")
	{
		for (i=0; i< postdesc.length; i++)
		{
			desc = postdesc[i].innerText;
			if(desc.indexOf('Line Post')>=0)
			{
				postindex = i;
			}
		}
		for (i=0; i< capdesc.length; i++)
		{
			desc = capdesc[i].innerText;
			if(desc.indexOf('Flat Cap Ext')>=0)
			{
				capindex = i;
			}
		}
		
		frmQuote.post_qty[postindex].value = posts_required;
		frmQuote.cap_qty[capindex].value = posts_required;
	} //end of check for footage change
	fence_price = sections_required * section_price;
	
	gate_total = getGateTotal();
	
	post_price = getPostPrice();
	cap_price = getCapPrice();
	
	fence_total = fence_price+post_price+cap_price;
	if (frmQuote.hdnColor.value == "tan")
	{
		fence_total = parseFloat(fence_total) + parseFloat(fence_total*0.10);
	}
	
	
	total_price  = fence_total + gate_total;
	fencesub.innerHTML  = "$"+parseFloat(fence_total.toFixed(2));
	gatesub.innerHTML  = "$"+parseFloat(gate_total.toFixed(2));
	totalval.innerHTML  = "$"+parseFloat(total_price.toFixed(2));
	

}
function getGateTotal()
{
	gate_subtotal = 0;
	gateqtystr = "";
	
	if(!isNaN(frmQuote.gate_qty.length))
	{
		for (i=0; i<frmQuote.gate_qty.length; i++)
		{
			if(frmQuote.gate_qty[i].value>0)
			{
				ind_price = parseFloat(frmQuote.gate_qty[i].value*frmQuote.gate_price[i].value);
				gate_subtotal = parseFloat(gate_subtotal + ind_price);
			}
			gateqtystr  = gateqtystr+frmQuote.gate_qty[i].value+"|";
			
		}
	}
	
	frmQuote.gateqtystr.value = gateqtystr.substr(0,gateqtystr.length-1);
	return gate_subtotal
}
function getPostPrice()
{
	post_subtotal = 0;
	postqtystr = "";
	
	if(!isNaN(frmQuote.post_qty.length))
	{
		for (i=0; i<frmQuote.post_qty.length; i++)
		{
			if(frmQuote.post_qty[i].value>0)
			{
				ind_price = parseFloat(frmQuote.post_qty[i].value*frmQuote.post_price[i].value);
				post_subtotal = parseFloat(post_subtotal + ind_price);
				
			}
			postqtystr  = postqtystr+frmQuote.post_qty[i].value+"|";
		}
	}
	frmQuote.postqtystr.value = postqtystr.substr(0,postqtystr.length-1);
	return post_subtotal;
}
function getCapPrice()
{
	cap_subtotal = 0;
	capqtystr = "";
	if(!isNaN(frmQuote.cap_qty.length))
	{
		for (i=0; i<frmQuote.cap_qty.length; i++)
		{
			if(frmQuote.cap_qty[i].value>0)
			{
				ind_price = parseFloat(frmQuote.cap_qty[i].value*frmQuote.cap_price[i].value);
				cap_subtotal = parseFloat(cap_subtotal + ind_price);
			}
			capqtystr  = capqtystr+frmQuote.cap_qty[i].value+"|";
		}
	}
	frmQuote.capqtystr.value = capqtystr.substr(0,capqtystr.length-1);
	return cap_subtotal;
}
function showhidestyle()
{
	if(fencedisplay.style.display=="inline")
	{
	fencedisplay.style.display="none";
	fenceform.style.display="inline";
	}
	else
	{
	fenceform.style.display="none";
	fencedisplay.style.display="inline";
	}
}
function showSectionimage(index, height, imagename)
{
	document.images['sectionimage'].src=sectionImages[index].src;
	sectionname.innerHTML = "Section Height:"+height;
	//sectiondiv.style.display="inline";
	frmQuote.hdnHeights.value = height;

}
function swicthcolors(total, current)
{
for(i=0; i<total; i++)
{
	if (i==current)
	{
	currentsource = ColorImages[i];
	currentimage = document.images[currentsource].src;
		if(currentimage.indexOf("_over")>1)
		{
		document.images[currentsource].src= "images/quote/"+currentsource+".gif";
		frmQuote.hdnColor.value = "";
		}
		else
		{
		document.images[currentsource].src= "images/quote/"+currentsource+"_over.gif";
		frmQuote.hdnColor.value = currentsource;
		}
		//colordiv.style.display="inline";
		colorname.innerHTML = "Color:"+currentsource.toUpperCase();
		document.images['colorimage'].src= "images/quote/"+currentsource+"_over.gif";
		
	}
	else
	{
	mysource = ColorImages[i];
	document.images[mysource].src = "images/quote/"+mysource+".gif";
	}
	
}

}
function preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();
    var i,j = document.imageArray.length, args = preloadImages.arguments;
    
    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}
function toggleforms(index)
{
err=false;
	if(index==1)
	{
	fenceproject.style.display="inline";
	fencecontact.style.display="none";
	}
	if(index==2)
	{
		errHeights.innerHTML="";
		errColor.innerHTML="";
		errFootage.innerHTML="";
		if(frmQuote.hdnHeights.value=="")
		{
		errHeights.innerHTML = "select fence height ";
		err=true;
		}
		if(frmQuote.hdnColor.value=="")
		{
		errColor.innerHTML = "select fence color ";
		err=true;
		}
		if((frmQuote.footage.value=="")||(frmQuote.footage.value=="0"))
		{
		errFootage.innerHTML = "enter linear footage";
		err=true;
		}
		
		if(!err)
		{
		fencecontact.style.display="inline";
		fenceproject.style.display="none";
		}
		else
		{ return false; }
	}


}
function getZipinfo(elementval)
{
  //alert(elementval);
  var url = "getzipinfo.php?zip="+elementval; 
  //alert(url);
  http.open("GET", url , true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
	
}
function getHTTPObject() {
	var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
@else
	xmlhttp = false;
@end @*/
//
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
		xmlhttp = false;
		}
	}
	return xmlhttp;
	
}


function handleHttpResponse() {
var rText;
  if (http.readyState == 4) {
	rText = http.responseText;
	//alert(rText);
	responseArray = rText.split("|");
	errzip.innerText="";
	errcallbackzip.innerText="";
	if(responseArray[0]=='error')
	{
		frmQuote.zip.value="";
		
		frmQuote.callme_zip.value="";
		
		frmQuote.city.value="";
		frmQuote.state.value="";
		
		errzip.innerText=responseArray[1];
		errcallbackzip.innerText=responseArray[1];
		
		return;
	}
	else
	{
		frmQuote.city.value=responseArray[1];
		frmQuote.state.value=responseArray[2];
		
		if(frmQuote.phone.value=="")
		{
			frmQuote.phone.value="("+responseArray[3]+")";
		}
		
		return;
	}
  }
}
//function to validate call back form
function validateCallBack()
{
	err=false;
	document.getElementById('errcallbackname').innerHTML ="";
	document.getElementById('errcallbackzip').innerHTML ="";
	document.getElementById('errcallbackphone').innerHTML ="";
	document.getElementById('errcallbackterms').innerHTML ="";
	if(frmQuote.callme_name.value=="")
	{
		errcallbackname.innerHTML = "<br>Name is required";
		err=true;
	}
	if(frmQuote.callme_zip.value=="")
	{
		errcallbackzip.innerHTML = "<br>zip is required";
		err=true;
	}
	if(frmQuote.callme_phone.value=="")
	{
		errcallbackphone.innerHTML = "<br>Phone is required";
		err=true;
	}
	if(!frmQuote.callme_terms.checked)
	{
		errcallbackterms.innerHTML = "<br>Select checkbox to agree to user agreement";
		err=true;
	}
	if(!err)
	{
		frmQuote.action = "http://www.fencenetwork.com/fence-callback-thanks.php";
		frmQuote.submit();
	}
}
function showlarge(imagesrc, titletext, source, product)
{
window.open ('fence-image-gallery.php?imagesrc='+imagesrc+'&h1='+titletext+'&source='+source+'&pid='+product,'productimages','menubar=no, resizable=no, toolbar=no, height=700, width=650,top=5,left=400, location=no, scrollbars=no');
}
document.onload =preloadImages('images/white.gif','images/tan.gif','images/clay.gif','images/grey.gif','images/quote/white_over.gif','images/quote/white_over.gif','images/quote/tan_over.gif','images/quote/clay_over.gif','images/quote/grey_over.gif');
