//"Accept terms" form submission- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use

var calcTotal="true";
var checkobj;
var totalCost;

function agreesubmit(el)
{
	checkobj=el
	if (document.all||document.getElementById)
	{
		for (i=0;i<checkobj.form.length;i++)
		{
			//hunt down submit button
			var tempobj=checkobj.form.elements[i];
			if(tempobj.type.toLowerCase()=="submit")
			{
				var error='';
				
				if (calcTotal=="true")
				{
					var inst=document.getElementById('hidden_installation_option').value;
					if (inst==1)
					{
						if (parseFloat(document.getElementById('totalCost').innerHTML)>0 && tempobj.disabled) tempobj.disabled=!checkobj.checked;
						tempobj.disabled=!checkobj.checked;
						if (parseFloat(document.getElementById('totalCost').innerHTML)==0) tempobj.disabled=true;
					}
					else
					{
						if (parseFloat(document.getElementById('totalCost').innerHTML)>0) tempobj.disabled=false;
						//tempobj.disabled=!checkobj.checked;
						if (parseFloat(document.getElementById('totalCost').innerHTML)==0) tempobj.disabled=true;
					}
				}
				else tempobj.disabled=!checkobj.checked;
					
				if (document.getElementById('panelsel').value==0 && document.getElementById('postsel').value==0 && document.getElementById('capsel').value==0 && document.getElementById('clipsel').value==0)
				{
					error+="You have not selected any fence items to order.";
					tempobj.disabled=true;
				}				
				//if (document.getElementById('panelqty').value<4 && document.getElementById('panelsel').value>0)
				if (document.getElementById('panelqty').value<4)
				{
					error+="You must select a minimum of 4 fence panels.<br />";
					tempobj.disabled=true;
				}
				if (document.getElementById('panelsel').value==0)
                                {
                                        error+="You must select a minimum of 4 fence panels.<br />";
                                        tempobj.disabled=true;
                                }
				/*
				if (document.getElementById('postqty').value<5 && document.getElementById('postsel').value>0)
				{
					error+="You must select a minimum of 5 fence posts.<br />";
					tempobj.disabled=true;
				}
				if (document.getElementById('capqty').value<5 && document.getElementById('capsel').value>0)
				{
					error+="You must select a minimum of 5 post caps.<br />";
					tempobj.disabled=true;
				}
				if (document.getElementById('clipqty').value<16 && document.getElementById('clipsel').value>0)
				{
					error+="You must select a minimum of 16 fence clips.<br />";
					tempobj.disabled=true;
				}
				*/
				if (error)
				{
					document.getElementById('error').style.color="#E14840";
					document.getElementById('error').innerHTML=error;
				}
				else
				{
					document.getElementById('error').style.color="#577736";
					document.getElementById('error').innerHTML="Please click the button below to proceed to the checkout.";
				}
			}
		}
	}
}

function defaultagree(el)
{
        if (!document.all&&!document.getElementById)
        {
                if (window.checkobj&&checkobj.checked) return true
                else
                {
                        alert("Please read/accept terms to submit form")
                        return false
                }
        }
}

// General Functions

function updateQty(n)
{
	var re5digit=/^\d{1,4}$/
	if (document.getElementById('panelqty').value.search(re5digit)==-1) document.getElementById('panelqty').value="";
	if (document.getElementById('postqty').value.search(re5digit)==-1) document.getElementById('postqty').value="";
	if (document.getElementById('capqty').value.search(re5digit)==-1) document.getElementById('capqty').value="";
	if (document.getElementById('clipqty').value.search(re5digit)==-1) document.getElementById('clipqty').value="";
	if (n=="post" && document.getElementById('panelqty').value>0) document.getElementById('postqty').value=parseInt(document.getElementById('panelqty').value)+1;
	else if (n=="cap" && document.getElementById('postqty').value>0) document.getElementById('capqty').value=parseInt(document.getElementById('postqty').value);
	else if (n=="clip" && document.getElementById('panelqty').value>0) document.getElementById('clipqty').value=parseInt(document.getElementById('panelqty').value)*4;
}

function setSelVal(r,i,n,p)
{
	if (document.getElementById(n+'sel').value)
	{
		elemId=document.getElementById(n+'sel').value;
		document.getElementById(n+elemId).style.backgroundColor='#FFFFFF';
		document.getElementById(n+elemId).style.border='';
	}
	else
	{
		document.getElementById(n+"0").style.backgroundColor='#FFFFFF';
		document.getElementById(n+"0").style.border='';		
	}
	r.style.backgroundColor="#E5EFD7";
	r.style.border="1px solid black";
	document.getElementById(n+'sel').value=i;
	document.getElementById(n+'cost').value=p;
	calculateTotalCost();
}

function setHiddenVal(v)
{
	document.getElementById('hidden_installation_option').value=v;
}

function calculateTotalCost()
{
	if (calcTotal=="true")
	{
		var panelcost=document.getElementById('panelcost');
		var paneltot=document.getElementById('paneltot');
		var postcost=document.getElementById('postcost');
		var posttot=document.getElementById('posttot');
		var capcost=document.getElementById('capcost');
		var captot=document.getElementById('captot');
		var clipcost=document.getElementById('clipcost');
		var cliptot=document.getElementById('cliptot');
		var instot=document.getElementById('instot');
		totalCost=parseFloat(0);
		paneltot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;0.00";
		posttot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;0.00";
		captot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;0.00";
		cliptot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;0.00";
		instot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;0.00";
		if (panelcost.value>0)
		{
			p1=panelcost.value*document.getElementById('panelqty').value;
			p1.toFixed(2);
			totalCost+=parseFloat(p1);
			
			// VAT is calculated on each item so we dont need to do this here.
			//paneltotVAT=p1.toFixed(2)*0.175;
			//paneltotIncVAT=parseFloat(p1)+parseFloat(paneltotVAT);
			//paneltot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+paneltotIncVAT.toFixed(2);
			paneltot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+p1.toFixed(2);
		}
		if (postcost.value>0)
		{
			p2=postcost.value*document.getElementById('postqty').value
			p2.toFixed(2);
			totalCost+=parseFloat(p2);
			
			// VAT is calculated on each item so we dont need to do this here.
			//posttotVAT=p2.toFixed(2)*0.175;
			//posttotIncVAT=parseFloat(p2)+parseFloat(posttotVAT);
			//posttot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+posttotIncVAT.toFixed(2);
			posttot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+p2.toFixed(2);
		}
		if (capcost.value>0)
		{
			p3=capcost.value*document.getElementById('capqty').value;
			p3.toFixed(2);
			totalCost+=parseFloat(p3);
			
			// VAT is calculated on each item so we dont need to do this here.
			//captotVAT=p3.toFixed(2)*0.175;
			//captotIncVAT=parseFloat(p3)+parseFloat(captotVAT);
			//captot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+captotIncVAT.toFixed(2);
			captot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+p3.toFixed(2);
		}
		if (clipcost.value>0)
		{
			p4=clipcost.value*document.getElementById('clipqty').value;
			p4.toFixed(2);
			totalCost+=parseFloat(p4);
			
			// VAT is calculated on each item so we dont need to do this here.
			//cliptotVAT=p4.toFixed(2)*0.175;
			//cliptotIncVAT=parseFloat(p4)+parseFloat(cliptotVAT);
			//cliptot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+cliptotIncVAT.toFixed(2);
			cliptot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+p4.toFixed(2);
		}
		var inst=document.getElementById('hidden_installation_option').value;
		if (inst==1)
		{
			var panelqty=document.getElementById('panelqty').value;
			var ins1=document.getElementById('ins1').value;
			var ins2=document.getElementById('ins2').value;
			var ins3=document.getElementById('ins3').value;
			var ins4=document.getElementById('ins4').value;
			if (panelqty <= 4)												extra=parseFloat(ins1);
			else if (panelqty > 4 && panelqty <= 8)		extra=parseFloat(ins2);
			else if (panelqty > 8 && panelqty <= 18)	extra=parseFloat(ins3);
			else if (panelqty > 18) 									extra=parseFloat(ins4);
			if (extra)
			{
				totalCost+=parseFloat(extra);
				//instotVAT=extra.toFixed(2)*0.175;
				//instotIncVAT=parseFloat(extra)+parseFloat(instotVAT);
				//instot.innerHTML="&pound;"+instotIncVAT.toFixed(2);
				instot.innerHTML="<span style=\"color:#454546;\">Sub total: </span>&pound;"+extra.toFixed(2);
			}
		}
			
		//totVAT=totalCost.toFixed(2)*0.175;
		//totIncVAT=parseFloat(totalCost)+parseFloat(totVAT);
		//document.getElementById('totalCost').innerHTML=totIncVAT.toFixed(2);
		document.getElementById('totalCost').innerHTML=totalCost.toFixed(2);
	}
	agreesubmit(document.getElementById('agreecheck'));
}

function togglePanelShow()
{
        if (document.getElementById('fence_panels_box').style.visibility=="hidden") document.getElementById('fence_panels_box').style.visibility="visible";
        else if (document.getElementById('fence_panels_box').style.visibility=="visible") document.getElementById('fence_panels_box').style.visibility="hidden";
}

function init()
{
	var browserName=navigator.appName;
        bar2.hideBar()
        document.getElementById('loading').style.visibility="hidden";
	if (browserName=="Netscape") document.getElementById('fencebuilder').style.opacity=1;
        //else if (browserName=="Microsoft Internet Explorer") document.getElementById('fencebuilder').filters.alpha.opacity=100;
}
