var html_progressbar = "<div style='float:left;z-index:10;'><img src='images/preloader2.gif'></div><div style='font:Arial, Helvetica, sans-serif; color:#3B73B9;'>&nbsp;&nbsp;Please Wait..</div>";
//var html_progressbar = "<div style='width:50%; float:right;z-index:10;'>Please Wait...</div>";


function printCalculation()
{
	window.open("print_calculation.php","mywindow","width=800,height=800,status=0,scrollbars=0,resizable=1,menubar=1,toolbar=1");	
}

function validate(val,which)
{
	
	if(isNaN(val))
	{
	 alert("Please enter numeric values");
	 document.getElementById(which).value="";
	 if(which == 'hight_ft' || which == 'hight_in')
	 {
		 document.getElementById("num_coumns").value="";
	 }
	 if(which == 'width_ft' || which == 'width_in')
	 {
		 document.getElementById("num_coumns").value="";
	 }
	 
	 return false;
	}
	
}

/*function select_color(val)
{
	var url = 'getColor.php?id='+val;
	//alert(url);
	loadXMLDoc(url);
}*/
function select_color(val,colorval,psuval,s2m)
{

	var feet_matric = document.getElementById("feet_matric").value;
	//alert(feet_matric);
	if(s2m=="s2m" || s2m=="s2f")
	{	
		var url = 'getColor.php?id='+val+'&colorval='+colorval+'&psuval='+psuval+'&s2m='+s2m;
	}
	else if(feet_matric=="meter" || feet_matric=="feet")
	{
		var url = 'getColor.php?id='+val+'&s2m='+feet_matric;		
	}
	else
	{		
		var url = 'getColor.php?id='+val;		
	}
	//alert(url);
	document.getElementById("color").innerHTML = html_progressbar;	
	loadXMLDoc(url);
}



function disable_option()
{
	if(document.getElementById("footage").value=="")
	{
		//document.getElementById("to_disable").style.display="block";	
		document.getElementById("height").disabled=false;
		document.getElementById("letters").disabled=false;	
		document.getElementById("strokemultiplier").disabled=false;	
	}else
	{
		document.getElementById("height").disabled="disabled";
		document.getElementById("letters").disabled="disabled";	
		//document.getElementById("letter_id").style.display="none";
		document.getElementById("strokemultiplier").disabled="disabled";	
		
	}
}


function calculate()
{
	var total;
	var total_w;
	
	var height_ft=document.getElementById("height_ft").value;
	var height_in=document.getElementById("height_in").value;
	var width_ft=document.getElementById("width_ft").value;
	var width_in=document.getElementById("width_in").value;
	
	var feetdisplay = document.getElementById("matric").style.display;
	if(feetdisplay=="block")
	{
		
		var height_cmval = (height_ft*100);		
		if(height_in)
		{
			height_cmval = parseFloat(height_cmval)+parseFloat(height_in);
		}
		
		var rowval = Math.floor((parseFloat(height_cmval)/20),0);		
		
		document.getElementById("num_rows").value=rowval;
		var width_cmval = (width_ft*100);
		if(width_in)
		{
			width_cmval = parseFloat(width_cmval)+parseFloat(width_in);
		}
		var columnval = Math.floor((parseFloat(width_cmval)/30),0);
		document.getElementById("num_columns").value=columnval;
	
	}
	else
	{
		if(height_ft!="")
		{
			if(height_in){
				total = Math.floor((((parseFloat(height_ft)+(parseFloat(height_in)/12))*12)/8),0);
			}else{
				total = Math.floor((((parseFloat(height_ft))*12)/8),0);
			}
		}
		
		if(!isNaN(total))
		document.getElementById("num_rows").value=total;
		
		if(width_ft!="")
		{
			if(width_in){			
				total_w = Math.floor(((parseFloat(width_ft)+(parseFloat(width_in)/12))*1),0); 
			}else{
				total_w = Math.floor(((parseFloat(width_ft))*1),0); 	
			}
		}
		if(!isNaN(total_w))
		document.getElementById("num_columns").value=total_w;	
		
	}
	


	if(isNaN(height_ft) || isNaN(height_in))
	{
		document.getElementById("num_rows").value="";
	}
	if(isNaN(width_ft) || isNaN(width_in))
	{
		document.getElementById("num_columns").value="";
	}

}


function switchToMatric(val)
{
	var stom='';
	var height_ft=document.getElementById("height_ft").value;
	var height_in=document.getElementById("height_in").value;
	var width_ft=document.getElementById("width_ft").value;
	var width_in=document.getElementById("width_in").value;

	var product = "Tetra Power Grid";	
	var color=document.getElementById("color_s2m").value;
	
	var psu=document.frmCalculator.psu.value;
	var display = document.getElementById("results").style.display;

	var matric_h_mt,matric_w_mt;

	if(display=="block")
	{
		if(color=="")
		{
			alert("Please select a Color");
			document.frmCalculator.color.focus();
			return false;
		}
		if(psu=="")
		{
			alert("Please select a power supply");
			document.frmCalculator.psu.focus();
			return false;
		}
		if(height_ft=="")
		{
			alert("Please enter the Height");
			document.getElementById("height_ft").focus();
			return false;
		}
		/*if(height_in=="")
		{
			alert("Please enter the Height");
			document.getElementById("height_in").focus();
			return false;
		}*/
		if(width_ft=="")
		{
			alert("Please enter the Width");
			document.getElementById("width_ft").focus();
			return false;
		}
		
		/*if(width_in=="")
		{
			alert("Please enter the Width");
			document.getElementById("width_in").focus();
			return false;
		}*/
	}
	if(val=="switch to matric")
	{
		document.getElementById("feet_matric").value="meter";
		
		if(height_ft!="")
		{
			var matric_h=roundNumber(height_ft * 0.3048,2);
			if(!isNaN(matric_h))
			document.getElementById("height_ft").value=matric_h;
		}
		if(width_ft!="")
		{
			var matric_w=roundNumber(width_ft * 0.3048,2);		
			if(!isNaN(matric_w))
			document.getElementById("width_ft").value=matric_w;
		}
		if(height_in!="")
		{
			var matric_h_mt =roundNumber(height_in * 2.54,2);
			if(!isNaN(matric_h_mt))
			document.getElementById("height_in").value=matric_h_mt;
		}
		if(width_in!="")
		{
			var matric_w_mt =roundNumber(width_in * 2.54,2);		
			if(!isNaN(matric_w_mt))
			document.getElementById("width_in").value=matric_w_mt;
		}

		var height_cmval = (matric_h*100);
		if(matric_h_mt)
		{
			height_cmval = height_cmval+matric_h_mt;
		}
		var rowval = Math.floor((parseFloat(height_cmval)/20),0);
		if(!isNaN(rowval)){
		document.getElementById("num_rows").value=rowval;
		}

		var width_cmval = (matric_w*100);
		if(matric_w_mt)
		{
			width_cmval = width_cmval+matric_w_mt;
		}
		var columnval = Math.floor((parseFloat(width_cmval)/30),0);
		
		if(!isNaN(columnval)){
		document.getElementById("num_columns").value=columnval;
		}
		

		document.getElementById("matric").style.display="block";
		document.getElementById("feet").style.display="none";

		document.getElementById("height_m").style.display="block";
		document.getElementById("height_f").style.display="none";
		document.getElementById("height_c").style.display="block";
		document.getElementById("height_i").style.display="none";
		
		document.getElementById("width_m").style.display="block";
		document.getElementById("width_f").style.display="none";
		document.getElementById("width_c").style.display="block";
		document.getElementById("width_i").style.display="none";

		stom = 's2m';
	}
	else
	{
		document.getElementById("feet_matric").value="feet";
		
		if(height_ft!="")
		{
			var feet_h=roundNumber(height_ft / 0.3048,0);
			if(!isNaN(feet_h))
			document.getElementById("height_ft").value=feet_h;
		}
		if(width_ft!="")
		{
			var feet_w=roundNumber(width_ft / 0.3048,0);
			if(!isNaN(feet_w))
			document.getElementById("width_ft").value=feet_w;
		}
		if(height_in!="")
		{
			var feet_h_mt =roundNumber(height_in / 2.54,0);
			if(!isNaN(feet_h_mt))
			document.getElementById("height_in").value=feet_h_mt;
		}
		if(width_in!="")
		{
			var feet_w_mt =roundNumber(width_in / 2.54,0);		
			if(!isNaN(feet_w_mt))
			document.getElementById("width_in").value=feet_w_mt;
		}
		
		
		if(feet_h!="")
		{
			total = Math.floor((((parseFloat(feet_h)+(parseFloat(feet_h_mt)/12))*12)/8),0);
		}		
		if(!isNaN(total))
		document.getElementById("num_rows").value=total;
		
		if(feet_w!="")
		{
			total_w = Math.floor(((parseFloat(feet_w)+(parseFloat(feet_w_mt)/12))*1),0); 
		}
		if(!isNaN(total_w))
		document.getElementById("num_columns").value=total_w;	
		

	
	
		
		
		document.getElementById("matric").style.display="none";
		document.getElementById("feet").style.display="block";
		
		document.getElementById("height_m").style.display="none";
		document.getElementById("height_f").style.display="block";
		document.getElementById("height_c").style.display="none";
		document.getElementById("height_i").style.display="block";
		
		document.getElementById("width_m").style.display="none";
		document.getElementById("width_f").style.display="block";
		document.getElementById("width_c").style.display="none";
		document.getElementById("width_i").style.display="block";		
		//select_color(document.getElementById("led_product").value,document.getElementById("color_s2m").value,document.getElementById("psu_s2m").value,'s2f');		
		stom = 's2f';
	}
	var num_rows = document.getElementById("num_rows").value;
	var num_columns = document.getElementById("num_columns").value;

	//By Nilaja For showing the result page after clicking on "Switch to Metric", depends on also Reset
	
	if(display=="block")
	{	
		/********  NEW FOR CALCULATE RESULT AT SWITCH TO METRIC *************/
			var unit = "";
			
			if(document.getElementById("height_m").style.display=="block")
			{
				if(document.getElementById("height_m").style.display=="block"){ unit ="m";	}
				else if(document.getElementById("height_c").style.display=="block"){ unit ="cm";	}
			}
			else
			{
				if(document.getElementById("height_f").style.display=="block") { unit ="ft";}
				else if(document.getElementById("height_i").style.display=="block") { unit ="Inches";}
			}

			var id= product;
			var url="finalcalculation_switch_power_grid_na.php?product="+product+"&color="+color+"&psu="+psu+"&height_ft="+height_ft+"&height_in="+height_in+"&width_ft="+width_ft+"&width_in="+width_in+"&num_rows="+num_rows+"&num_columns="+num_columns+"&unit="+unit+"&s2m="+stom+"&id="+id;			
			
			//alert("1==========>"+url);
			//document.getElementById("color").innerHTML = html_progressbar;
			
			loadXMLDocResult_new(url,stom);
			document.getElementById("dot_bg").style.display="block";	
			document.getElementById("results").style.display="block";
		
		/**** END NEW **********************/
		
	}
	else
	{
			
			//select_color(document.getElementById("led_product").value,document.getElementById("color_s2m").value,document.getElementById("psu_s2m").value,stom);		
		
	}
	document.getElementById("results").style.display = display;
	//End
}

function roundNumber(num,places) {
	var newnumber = Math.round(num*Math.pow(10,places))/Math.pow(10,places);
	return(newnumber);
}

function loadXMLDoc(url)
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) { 
			req.onreadystatechange = processReqChange;			
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange()
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response  = req.responseXML.documentElement;

			var urlpath='';
			for(var x = 0; response.getElementsByTagName('urlpath')[0].childNodes[x]; x++ )
			 urlpath = urlpath.concat(response.getElementsByTagName('urlpath')[0].childNodes[x].data);	
		//alert(document.getElementById("color").innerHTML);
		//alert(urlpath);
			document.getElementById("color").innerHTML = urlpath;
		//alert(document.getElementById("color").innerHTML);	

			var urlpath1='';
			//alert(response.getElementsByTagName('urlpath1')[0].childNodes[0].data);

			for(var x = 0; response.getElementsByTagName('urlpath1')[0].childNodes[x]; x++ )
			 urlpath1 = urlpath1.concat(response.getElementsByTagName('urlpath1')[0].childNodes[x].data);				
			document.getElementById("psu").innerHTML = urlpath1;
					
			return false;
		} else {
			alert("There was a problem display_tags_alpha retrieving the XML data:\n" + req.statusText);
		}
	}
}


/////////////////////////////// display result calculation  /////////////////////
function display_results(from,extra)
{
	var product = "Tetra Power Grid";
	var color=document.frmCalculator.color.value;
	var psu=document.frmCalculator.psu.value;

	var height_ft=document.getElementById("height_ft").value;	
	var height_in=document.getElementById("height_in").value;
	
	var width_ft=document.getElementById("width_ft").value;
	var width_in=document.getElementById("width_in").value;
	
	var num_rows = document.getElementById("num_rows").value;
	var num_columns = document.getElementById("num_columns").value;

	var unit = "";
	
	if(from=="s2m" || from=="s2f")
	{
 		
	}
	else
	{ 	from='';
		
		if(color=="")
		{
			alert("Please select a Color");
			document.frmCalculator.color.focus();
			return false;
		}
		if(psu=="")
		{
			alert("Please select a power supply");
			document.frmCalculator.psu.focus();
			return false;
		}
		if(height_ft=="")
		{
			alert("Please enter the Height");
			document.getElementById("height_ft").focus();
			return false;
		}
		/*if(height_in=="")
		{
			alert("Please enter the Height");
			document.getElementById("height_in").focus();
			return false;
		}*/		
		if(width_ft=="")
		{
			alert("Please enter the Width");
			document.getElementById("width_ft").focus();
			return false;
		}
		
		/*if(width_in=="")
		{
			alert("Please enter the Width");
			document.getElementById("width_in").focus();
			return false;
		}*/
		
	}
	
	if(document.getElementById("height_m").style.display=="block")
	{
		if(height_ft!=""){ unit ="m";}		
	}
	else
	{
		unit ="ft";
	}
	
	//alert(from);alert(document.getElementById("f").style.display);
	if(from!="")
	{
		var url="finalcalculation_power_grid_na.php?product="+product+"&color="+color+"&psu="+psu+"&foot="+foot+"&ht="+ht+"&let="+let+"&unit="+unit+"&s2m="+from+"&display_result=1";
		//alert("1=="+url);
		loadXMLDocResult(url,from);		
	}
	else
	{
		//alert("height_m=="+document.getElementById("height_m").style.display);
		//alert("height_f=="+document.getElementById("height_f").style.display);
		
		if(document.getElementById("height_m").style.display=="block")
		{
			var url="finalcalculation_power_grid_na.php?product="+product+"&color="+color+"&psu="+psu+"&height_ft="+height_ft+"&height_in="+height_in+"&width_ft="+width_ft+"&width_in="+width_in+"&num_rows="+num_rows+"&num_columns="+num_columns+"&unit="+unit+"&s2m=s2f&display_result=1";
//alert("2=="+url);
			loadXMLDocResult(url);		
		}
		else
		{
			//var url="finalcalculation_power_grid.php?product="+product+"&color="+color+"&psu="+psu+"&foot="+foot+"&ht="+ht+"&let="+let+"&unit="+unit+"&s2m=s2m"+"&display_result=1";
			var url="finalcalculation_power_grid_na.php?product="+product+"&color="+color+"&psu="+psu+"&height_ft="+height_ft+"&height_in="+height_in+"&width_ft="+width_ft+"&width_in="+width_in+"&num_rows="+num_rows+"&num_columns="+num_columns+"&unit="+unit+"&s2m=s2m&display_result=1";
//alert("3=="+url);
			loadXMLDocResult(url);	
		}
		
		
	}
	if(from=="s2m" || from=="s2f")
	{

//select_color(document.getElementById("led_product").value,document.getElementById("color_s2m").value,document.getElementById("psu_s2m").value,from); //see here
		if(document.getElementById("results").style.display)
		{
			document.getElementById("dot_bg").style.display="block";	
			document.getElementById("results").style.display="block";
		}
		else
		{
			document.getElementById("dot_bg").style.display="none";	
			document.getElementById("results").style.display="none";			
		}

	}
	else
	{
		document.getElementById("dot_bg").style.display="block";	
		document.getElementById("results").style.display="block";
	}
	
	browser_version = navigator.appVersion;
	b_ver = browser_version.split(";");
	ver = b_ver[1].split(" ");
	browser_type = navigator.appName;
	browser_version = parseInt(navigator.appVersion);
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ //test for MSIE x.x;
	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	}
	
	if(from!="s2m" && from!="s2f")
	{
		document.getElementById("page").style.height="540px";	
	}
	return false;
}


function reset_func()
{
	document.frmCalculator.reset();
	document.getElementById("results").style.display="none";
	document.getElementById("dot_bg").style.display="none";	
	document.getElementById("page").style.height="290px";
	return false;
}

function loadXMLDocResult(url,from)
{
	if(from!="")
	{		
		//select_color(document.getElementById("led_product").value,document.getElementById("color_s2m").value,document.getElementById("psu_s2m").value,from);
	}
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange =processReqChangeResult;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChangeResult;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChangeResult()
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response  = req.responseXML.documentElement;

			var urlpath='';
			for(var x = 0; response.getElementsByTagName('urlpath')[0].childNodes[x]; x++ )
			 urlpath = urlpath.concat(response.getElementsByTagName('urlpath')[0].childNodes[x].data);		
			 
			document.getElementById("results").innerHTML = urlpath;
	
	
			return false;
		} else {
			alert("There was a problem display_tags_alpha retrieving the XML data:\n" + req.statusText);
		}
	}
}


function loadXMLDocResult_new(url,from)
{
	if(from!="")
	{		
		//select_color(document.getElementById("led_product").value,document.getElementById("color_s2m").value,document.getElementById("psu_s2m").value,from);
	}
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange =processReqChangeResultNew;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChangeResultNew;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChangeResultNew()
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response  = req.responseXML.documentElement;


			var urlpath1='';var urlpath2='';
			//for(var x = 0; response.getElementsByTagName('urlpath1')[0].childNodes[x]; x++ )
			//urlpath1 = urlpath1.concat(response.getElementsByTagName('urlpath1')[0].childNodes[x].data);				
			//document.getElementById("color").innerHTML = urlpath1;
			
			//alert(urlpath1);
			//for(var x = 0; response.getElementsByTagName('urlpath2')[0].childNodes[x]; x++ )
			//urlpath2 = urlpath2.concat(response.getElementsByTagName('urlpath2')[0].childNodes[x].data);				
			//document.getElementById("psu").innerHTML = urlpath2;
//alert(urlpath1);alert(urlpath2);

			var urlpath='';
			for(var x = 0; response.getElementsByTagName('urlpath')[0].childNodes[x]; x++ )
			 urlpath = urlpath.concat(response.getElementsByTagName('urlpath')[0].childNodes[x].data);				
			 
			document.getElementById("results").innerHTML = urlpath;
			

			return false;
		} else {
			alert("There was a problem display_tags_alpha retrieving the XML data:\n" + req.statusText);
		}
	}
}
////////////////////////////////////////////////////////////////////////////////


