/*
    Javascript support for Logix website
    Authors: Lucian Grecu, Eugene Brukson
    Date: May, June 2008
*/

//ajax powered product page big product showcase  
var loadedProduct = 0;
function loadProduct(id) { 
	url = "index.php?n=builder&o=get_product&id=" + id;
	loadedProduct = id;
	initObj();
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=_loadProduct;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	  xmlhttp.send(null);
	}

}
function _loadProduct() {
	if(checkReadyState(xmlhttp)) {
		var response = xmlhttp.responseXML.documentElement;
		response.normalize;
		x=response.getElementsByTagName("table");
		el = document.getElementById("productDetails");
		el.innerHTML = getXMLNode(x);

		x=response.getElementsByTagName("title");
		el = document.getElementById("productTitle");
		el.innerHTML = getXMLNode(x);
		
		x=response.getElementsByTagName("image");
		el = document.getElementById("homeProductImage");
		el.src = getXMLNode(x);  

		x=response.getElementsByTagName("plan");
		el = document.getElementById("productPlanThumb");
		el.innerHTML = getXMLNode(x);
		
		el = document.getElementById("footnote");
		if (loadedProduct == 2 || loadedProduct == 3) el.style.display = 'block'; else el.style.display = 'none'; 

	}
}

//ajax powered home page small product showcase
var currentProductID = 1;
var homeProducts = [];
function showProduct(id) { 

//		el = document.getElementById("prodImage" + homeProducts[id][0]);
//        el.src = homeProducts[id][3];
//
		for (i=0; i< homeProducts.length; i++)
		{
			el = document.getElementById("cell" +  homeProducts[i][0]);
			el.setAttribute("class", "homepageProduct");
			el.innerHTML = '<a href="#" class="prodImage" onclick="showProduct('+i+');"><img src="' + homeProducts[i][2] + '" border="0" width="50" height="50"></a>';
		}

		txt = '<table cellpadding="0" cellspacing="0" border="0" style="height: 400px;">';
		txt += '<tr><td height=45 width=45></td><td height=45 valign="bottom"><span style="font-size: 24px; color: #6c9c31">'+homeProducts[id][1]+'</span></td><td height=45 width=45><a href="#self" onclick="closeWait();"><img src="images/pop_button_close.gif" onmouseover="this.src=\'images/pop_button_close_over.gif\';" onmouseout="this.src=\'images/pop_button_close.gif\';" border="0"></a></td></tr>';
		txt += '<tr><td width=45></td><td height=280 width="300" align="left" valign="middle"><img src="'+homeProducts[id][3]+'"></td><tdwidth=45></td></tr>';
		txt += '<tr><td width=45></td><td height="60" valign="middle" align="right"><a href="index.php?n=builder&o=main&id='+homeProducts[id][0]+'" class="button_medgrey" style="width: 120px;" id="homeProductUrl">LEARN MORE</a></td><tdwidth=45></td></tr>';
		txt += "</table>";
		
//		el = document.getElementById("homeProductName");
//		el.innerHTML = homeProducts[id][1];
//		
//		el = document.getElementById("homeProductImage");
//		el.src = homeProducts[id][3];
		
//		el = document.getElementById("prodImage" +  + homeProducts[id][0]);
//		el.src = homeProducts[id][3];
//		el = document.getElementById("prodLink" +  + homeProducts[id][0]);
//		el.onmouseout = "";
		el = document.getElementById("cell" + homeProducts[id][0]);
		el.setAttribute("class", "homepageProduct_disabled");
		el.innerHTML = '<a href="#" class="prodImage" onclick="showProduct('+id+');"><img style="border: 1px solid #C5D7AD;" src="' + homeProducts[id][2] + '" width="50" height="50"></a>';

//		el = document.getElementById("homeProductUrl");
//		el.href= "index.php?n=builder&o=main&id=" + homeProducts[id][0];

		showWait(txt, 390, 500);

}


function selectPlan(id) {  //ajax driven home plan display
    url = "index.php?n=builder&o=get_plan&id=" + id;
    initObj();
    if (xmlhttp!=null) {
      xmlhttp.onreadystatechange=_selectPlan;
      xmlhttp.open("GET",url,true);
      xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
      xmlhttp.send(null);
    }

}

function _selectPlan() {
    if(checkReadyState(xmlhttp)) {
        var response = xmlhttp.responseXML.documentElement;
        response.normalize;
        x=response.getElementsByTagName("description");
        el = document.getElementById("planDescription");
        el.innerHTML = getXMLNode(x);

        x=response.getElementsByTagName("name");
        el = document.getElementById("planName");
        el.innerHTML = getXMLNode(x);
        
        x=response.getElementsByTagName("image");
        el = document.getElementById("planImage");
        el.src = getXMLNode(x);
    
        x=response.getElementsByTagName("id");
        id = getXMLNode(x);
        
        x=response.getElementsByTagName("thumb");
        el = document.getElementById("planImage" + id);
        el.src = getXMLNode(x);

    }
}


function showVideo() {
	el = document.getElementById('divVideo');  
	if (!el) {
		el = document.createElement("div");
		el.id = "divVideo";
    }
		el.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"   width="620" height="405" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"><param name="movie" value="swf/logixVid.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />  <embed src="swf/logixVid.swf" quality="high" bgcolor="#ffffff" width="620" height="405" name="logixVid" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain"  type="application/x-shockwave-flash"  pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
		el.style.width = "620px";
		el.style.height = "420px";
		el.style.border = "0px solid #ccc";
		el.style.position = "absolute";
		tmp = document.getElementById('videoSpacer');

		el.style.top = (findPosY(tmp) + 80) + "px";
		el.style.left = (findPosX(tmp) + 0) + "px";
		bdy = document.getElementsByTagName("body");
		bdy[0].appendChild(el);
	
	el.style.display = "block";

}
function closeVideo() {
	el = document.getElementById('divVideo');
	el.style.display = "none";
}


function showDistributors(StateID) {
	showWait();
    url = "index.php?n=distributor&o=get_distributors&id=" + StateID;
    initObj();
    if (xmlhttp!=null) {
      xmlhttp.onreadystatechange=_showDistributors;
      xmlhttp.open("GET",url,true);
      xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
      xmlhttp.send(null);
    }
}
function showDistributorsMexico() {
	showWait();
    url = "index.php?n=distributor&o=get_distributors_mexico";
    initObj();
    if (xmlhttp!=null) {
      xmlhttp.onreadystatechange=_showDistributors;
      xmlhttp.open("GET",url,true);
      xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
      xmlhttp.send(null);
    }
}
function _showDistributors() {
    if(checkReadyState(xmlhttp)) {
        var response = xmlhttp.responseXML.documentElement;
        response.normalize;
        x=response.getElementsByTagName("content");
        el=  document.getElementById("DistributorList");
        el.innerHTML = getXMLNode(x);

		x1=response.getElementsByTagName("gmap_id");
		x2=response.getElementsByTagName("gmap_long");
		x3=response.getElementsByTagName("gmap_lat");
		for (i=0; i<x1.length ;i++ )
		{
			try
			{
				if (x2[i] && x3[i] && x3[i].firstChild.data && x2[i].firstChild.data) showDistributorsGoogleMap(x1[i].firstChild.data, x3[i].firstChild.data , x2[i].firstChild.data);	
			}
			catch (ex)
			{
				;
			}
			
		}
		closeWait();	
		window.location='#masterList';
	}
}

function showDistributorsGoogleMap(id, longitude, latitude) {
	if (parseInt(longitude) && parseInt(latitude))
	{
		 if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map_canvas" + id));
			center = new GLatLng(latitude, longitude);
			map.setCenter(center, 13);
			marker = new GMarker(center, {draggable: false});
			map.addControl(new GSmallMapControl());
			map.addOverlay(marker);
		  }
	}
}


function showContactDesigner(id) {
	txt = "<div class='designer-contact-popup'><div class='title'><a href='#self' onclick='closeWait();'>close  </a></div>";
	txt += "<form action='index.php?o=contact-designer&id="+id+"' method='post'>";
	txt += "<table cellpadding='0' cellspacing='4' border='0'>";
	txt += "<tr><td>Your Name:</td><td><input name='Name' value='' type='text' size=50></td></tr>";
	txt += "<tr><td>Your Email:</td><td><input name='Email' value='' type='text' size=50></td></tr>";
	txt += "<tr><td>Subject:</td><td><input name='Subject' value='' type='text' size=50></td></tr>";
	txt += "<tr><td>Message:</td><td><textarea name='Message' rows=6 cols=30></textarea></td></tr>";
	txt += "<tr><td colspan='2'><input type='submit' value='Send Message'></td></tr>";
	txt += "</form>";
	txt += "</table>";
	txt += "</div>";

	showWait(txt);
}

function showContactDistributor(id) {

	if (getQueryVariable("mode") == "quote")
	{
		contactDistributorForQuote(id);
		return true;
	}
	txt = "<div class='designer-contact-popup'><div class='title'><a href='#self' onclick='closeWait();'>close  </a></div>";
	txt += "<form action='index.php?o=contact-distributor&id="+id+"&quote="+getQueryVariable("quote")+"' method='post'>";
	txt += "<table cellpadding='0' cellspacing='4' border='0'>";
	txt += "<tr><td>Your Name:</td><td><input name='Name' id='Name' value='' type='text' size=50></td></tr>";
	txt += "<tr><td>Your Email:</td><td><input name='Email' id='Email' value='' type='text' size=50></td></tr>";
	txt += "<tr><td>Subject:</td><td><input name='Subject' id='Subject' value='' type='text' size=50></td></tr>";
	txt += "<tr><td>Message:</td><td><textarea name='Message' id='Message' rows=6 cols=30></textarea></td></tr>";
	txt += "<tr><td colspan='2'><input type='button' onclick='contactDistributor("+id+")' value='Send Message'></td></tr>";
	txt += "</form>";
	txt += "</table>";
	txt += "</div>";

	showWait(txt);
}

function contactDistributor(id) {
	txt = "";
	el = document.getElementById('Name'); txt = "Name=" + el.value; if (!el.value) {alert("Name is a required field!"); return true;}
	el = document.getElementById('Email'); txt += "&Email=" + el.value; if (!el.value) {alert("Email is a required field!"); return true;}
	el = document.getElementById('Subject'); txt += "&Subject=" + el.value;
	
	el = document.getElementById('Message'); 
	ttmp = el.innerHTML;
	if (!ttmp)
	{
		ttmp = el.value;
	}
	txt += "&Message=" + ttmp;
	url = "index.php?n=distributor&o=contact-distributor&id=" + id + "&quote=" + getQueryVariable("quote") + "&" + txt;
	initObj();
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=_contactDistributor;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	  xmlhttp.send(null);
	}

}

function contactDistributorForQuote(id) {
	url = "index.php?n=distributor&o=contact-distributor-quote&id=" + id;
	initObj();
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=_contactDistributorQuote;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	  xmlhttp.send(null);
	}

}

function _contactDistributor() {
	if(checkReadyState(xmlhttp)) {
		var response = xmlhttp.responseXML.documentElement;
		response.normalize;
		closeWait();
	}
}

function _contactDistributorQuote() {
	if(checkReadyState(xmlhttp)) {
		var response = xmlhttp.responseXML.documentElement;
		response.normalize;
		alert("Your quote request has been forwarded to the distributor you've selected.");
	}
}

var estimateData = "";
var estimateData2 = "";
var estimateid = 0;
function estimateCalculate() {
	resetEstimate();
	eheight = 0;
	elength = 0;
	ethickness = 0;
	earea = 0;
	e90s = 0;
	e45s = 0;
	etype = "";
	erebarh = 0;
	erebarv = 0;

	earea2 = 0;

	volume = [0,0,0,0,0,0];
	rebars = [0,0,0,0,0,0];
	formsstd = [0,0,0,0,0,0];
	forms90 = [0,0,0,0,0,0];
	forms45 = [0,0,0,0,0,0];

	var showMsg = false;
	var showMsg2 = false;

	for (i=1;i<=6 ;i++ )
	{
		idx = i - 1;
		el = document.getElementById("Height" + i); eheight = parseFloat(el.value); if (!parseFloat(eheight)) break;
		el = document.getElementById("Length" + i); elength = parseFloat(el.value);
		el = document.getElementById("Thickness" + i); ethickness = parseFloat(el.value);
		el = document.getElementById("Area" + i); earea = parseFloat(el.value);
		el = document.getElementById("Corners90" + i); e90s = parseFloat(el.value);
		el = document.getElementById("Corners45" + i); e45s = parseFloat(el.value);
		el = document.getElementById("WallType" + i); etype = el.value;
//		el = document.getElementById("HorRebar" + i); erebarh = parseFloat(el.value);
//		el = document.getElementById("VerRebar" + i); erebarv = parseFloat(el.value);
		estimateData += etype + "^^^" + eheight + "^^^"  + elength + "^^^"  + ethickness + "^^^"  + earea + "^^^"  + e90s + "^^^"  + e45s + "sss";
		earea2 = parseFloat(eheight * elength);
		volume[i-1] = Math.round((earea2 - earea) * (ethickness/12));
		
		if (ethickness == 10 || ethickness == 12 )
		{
			showMsg = true;
		}
		if (ethickness == 4 && etype == "below" )
		{
			showMsg2 = true;
		}

		coef = 0;
		if (etype == "above") {
			coef = 0.625; 
			erebarh = 32;
			
		} else {
			coef = 1.25;
			erebarh = 16;
		}
		erebarv = 48;
		rebars[i-1] = Math.round(coef * (earea2 - earea));

		forms90[i-1] = Math.round(e90s * (eheight/1.333));

		unitArea90SqFt = 0;
		unitArea45SqFt = 0;

		switch (ethickness)
		{
		case 4:
		case 6.25:
		case 8:
			forms45[i-1] = Math.round(e45s *  (eheight/1.333));
			unitArea90SqFt = 5.361;
			unitArea45SqFt = 5.333;
			break;
		default: 
			forms45[i-1] = 0;
	 		unitArea90SqFt = 5.889;
			unitArea45SqFt = 0;
		
		}

		formsstd[i-1] = Math.round((earea2 - (earea + unitArea90SqFt * forms90[i-1] + unitArea45SqFt * forms45[i-1])) / 5.333);

	}
	
	tvolume = 0;
	trebars = 0;
	tforms45 = 0;
	tforms90 = 0;
	tformsstd = 0;

	txt = "<div style='color: #6c9c31; padding: 20px 0px 10px 0px'><b>Results: </b></div><table class='estimator-results' cellspacing='0' cellpadding='0'>";
	txt += "<tr><th>Section</th><th>Concrete Volume (m<sup>3</sup>)</th><th>Length of Rebar (Ft)</th><th>No. of 45&deg; Corner Forms</th><th>No. of 90&deg; Corner Forms</th><th>No. of Standard Forms</th></tr>";


	for (i=0; i<=5 ; i++)
	{
		estimateData2 += volume[i] + "^^^" + rebars[i] + "^^^"  + forms45[i] + "^^^"  + forms90[i] + "^^^"  + formsstd[i] + "sss";
		txt += "<tr><td>" + (i+1) + "</td><td>" + Math.round(volume[i] * 0.028316,2)+ "</td><td>" + rebars[i]+ "</td><td>" + forms45[i]+ "</td><td>" + forms90[i]+ "</td><td>" + formsstd[i]+ "</td></tr>";
		tvolume += parseFloat(volume[i]);
		trebars += parseFloat(rebars[i]);
		tforms45 += parseFloat(forms45[i]);
		tforms90 += parseFloat(forms90[i]);
		tformsstd += parseFloat(formsstd[i]);
	}
	txt += "<tr><td><b>Total</td><td><b>" + Math.round(tvolume * 2.8316) / 100  + " m<sup>3</sup> (" + Math.round(100*tvolume / 27)/100+  " Yd<sup>3</sup>)"+ "</td><td><b>" + trebars+ "</td><td><b>" + tforms45+ "</td><td><b>" + tforms90+ "</td><td><b>" + tformsstd+ "</b></td></tr>";
	txt += "</table><br>";
	txt += "<div style='font-size: 9px; padding: 10px 0px 20px'>Note: ";
	if (showMsg)
	{
		txt += "Additional 12\" LOGIX Standard forms have been added to replace 45 degree corner forms.<br>";
	}
	if (showMsg2)
	{
		txt += "4\" LOGIX has been selected for below-grade walls.  Confirm with local design professionals, or building officials, whether 4\" concrete walls are permitted by code in your project area.<br>";
	}
	
//	txt += "Rebar lengths are based on horizontal and vertical rebar spacings of 16” and 24”, respectively, for below-grade walls, and 32” and 48”, respectively, for above-grade walls.</div>";
	

	txt += '<input class="logix_button" type="button" value="Download PDF" onclick="estimatePrint(\'print\')"> ';
	txt += '<input class="logix_button" type="button" value="Email" onclick="estimatePrint(\'email\')">';
	
	txt += '<div style="font-size: 11px; padding: 20px 0px">';
	txt += 'The One Minute Estimator is for the purpose of quickly gaining a general idea of the amount of materials required for a LOGIX project';
	txt += '<br>For the purposes of obtaining a "rough" estimate for reinforcement quantities, the One Minutes Estimator uses the rebar spacings shown below.  Actual Reinforcement requirements will be different for each construction project.  ';
	txt += "<br><ul><li>Above-grade: 16\" and 48\" for horizontal and vertical rebar spacing, respectively</li> ";
	txt += '<li>Below-grade: 32\" and 24\" for horizontal and vertical rebar spacing, respectively</li></ul>';
	txt += '<br>For <u>questions</u> or for a more detailed, precise estimate please choose one of the following options:”:'
	txt += '<br><br><a href="#" onclick="estimatePrint(\'quote\')">Request a Quote </a>'
	txt += '<br> <a href="http://www.logixicf.com/media/library/documents/logixEst1.exe">Download the LOGIX Estimator</a></div>';


	el = document.getElementById('estimateResults');
	el.innerHTML = txt;
}

function estimatePrint(esttype) {
	if (estimateid)
	{
		doEstimate(esttype);
		return true;
	}
	txt = "<div class='estimator-popup'><table cellspacing='10' cellpadding='0' border='0'>";
	txt += "<tr><td>Your name: </td><td><input type='text' id='estName' value=''></td></tr>";
	txt += "<tr><td>Project name: </td><td><input type='text' id='estProject' value=''></td></tr>";
	txt += "<tr><td>Project location: </td><td><input type='text' id='estLocation' value=''></td></tr>";
	txt += "<tr><td>Your email: </td><td><input type='text' id='estEmail' value=''></td></tr>";
	txt += "<tr><td></td><td><input  class='logix_button' type='button' value='submit' onclick='doEstimate(\""+esttype+"\")'> <input  class='logix_button' type='button' value='cancel' onclick='closeWait()'></td></tr>";
	txt += "</table></div>";
	showWait(txt, 390, 500);
}

function doEstimate(esttype) {
	url = "index.php?n=library&o=estimate-" + esttype;
	if (!estimateid) {
		el = document.getElementById("estName"); url += "&name=" + el.value;
		el = document.getElementById("estProject"); url += "&project=" + el.value;
		el = document.getElementById("estLocation"); url += "&location=" + el.value;
		el = document.getElementById("estEmail"); url += "&email=" + el.value;
		 url += "&data=" + estimateData + "&data2=" + estimateData2;
	}
	initObj();
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=_doEstimate;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	  xmlhttp.send(null);
	}

}

function _doEstimate() {
	if(checkReadyState(xmlhttp)) {
		var response = xmlhttp.responseXML.documentElement;
		response.normalize;
		x=response.getElementsByTagName("fid");
		estimateid = getXMLNode(x);
		x=response.getElementsByTagName("ftype");
		ret = getXMLNode(x);
		x=response.getElementsByTagName("fname");
		fn = getXMLNode(x);

		if (ret == "print")
		{
			window.open(fn, "print");
		} 
		if (ret == "quote")
		{
			window.location = 'index.php?n=distributor&mode=quote';
			return true;
		}
		
		showWait("<div class='estimator-popup' style='padding: 20px; font-size: 13px; line-height: 150%; max-width: 500px; max-height: 300px;'>Your request has been processed succesfully! <br>If you're having trouble downloading the PDF, <a href='"+fn+"'>click here</a> to access it<br><br><input class='logix_button' type='button' value='Close' onclick='closeWait()'></div>", 390, 500);
	}

}

function resetEstimate() {
	estimateid = 0;
	estimateData = "";
	estimateData2 = "";
	url = "index.php?n=library&o=reset-estimate";
	initObj();
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=_resetEstimate;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	  xmlhttp.send(null);
	}

}

function _resetEstimate() {
	if(checkReadyState(xmlhttp)) {
		var response = xmlhttp.responseXML.documentElement;
		response.normalize;
	}

}


