/* Copyright Rob Miller - All Rights Reserved */

if (data) {
	$('roomName').setValue(data['roomName']);
	$('roomNumber').setValue(data['roomNumber']);
	if (data['units'] == "Metric") {unitsCallback();}
	$('roomLength').setValue(data['roomLength']);
	$('roomWidth').setValue(data['roomWidth']);
	$('roomHeight').setValue(data['roomHeight']);
	$('rtGoal').setValue(data['rtGoal']);
	$('roomCapacity').setValue(data['roomCapacity']);
//	$('calcGoalRT').setValue(data['calcGoalRT']);
//	$('calcSabineRT').setValue(data['calcSabineRT']);
//	$('calcEyringRT').setValue(data['calcEyringRT']);
//	$('calcFitzroyRT').setValue(data['calcFitzroyRT']);
	$('modDate').innerHTML = data['modDate'];
	
	data["rts"].each(function(j) {
		if (j.type == "Multiplier") {
			threedb.rt.freq.each(function(k) {
				$("rtGoalMultiplier" + k).setValue(j["rt" + k]);
			});
		} else {
			if (j.type == "Goal") {
				threedb.rt.freq.each(function(k) {
					$("rtGoal" + k).setValue(j["rt" + k]);
				});
			}
		}
	});
	
	// j = surface index
	for (j = 0; j < data["surfaces"].length; j++) {
		threedb.rt.addSurface.render({
		    index: threedb.user.data.room.surface.length,
            surface: threedb.user.data.room.surface[threedb.user.data.room.surface.length]
		});
		$('lr' + j).setValue(data["surfaces"][j]["lr"]);
		$('fr' + j).setValue(data["surfaces"][j]["fr"]);
		$('fc' + j).setValue(data["surfaces"][j]["fc"]);
//		$('note' + j).innerHTML = data["surfaces"][j]["note"];

		$('finishId' + j).setValue(data["surfaces"][j]["fid"]);
		
		// k = array index of finish
		var k = 0;
		while (data["surfaces"][j]["fid"] != threedb.finishes[k]["finishid"]) {
			k++;
		}


/*

		var finishHTML = [];
			if ((threedb.finishes[k]['manufacturer'] !== '') && (threedb.finishes[k]['model'] !== '')) {
				finishHTML.push("<span>" + threedb.finishes[k]['manufacturer'] + " " + threedb.finishes[k]['model'] + "</span>");
			};
			if ((threedb.finishes[k]['manufacturer'] !== '') && (threedb.finishes[k]['desc'] !== '')) {finishHTML.push("(" + threedb.finishes[k]['desc'] + ")</span>");}
			if (!(threedb.finishes[k]['manufacturer'] !== '') && (threedb.finishes[k]['desc'] !== '')) {finishHTML.push("<span>" + threedb.finishes[k]['desc'] + "</span>");}
        $('finish' + j).innerHTML = finishHTML.join("");
 
        
        var finishDetailHTML = [];
			if (threedb.finishes[k]['mounting']  !== '')     {finishDetailHTML.push("Type " + threedb.finishes[k]['mounting'] + " mounting, ");}
			if (threedb.finishes[k]['thickness'] !== '')     {finishDetailHTML.push(threedb.finishes[k]['thickness'] + " thick, ");}
			if (threedb.finishes[k]['density']   !== '')     {finishDetailHTML.push(threedb.finishes[k]['density'] + " density");}
			if (threedb.finishes[k]['feat1']     !== '')     {finishDetailHTML.push(threedb.finishes[k]['feat1']);}
			if (threedb.finishes[k]['feat2']     !== '')     {finishDetailHTML.push(threedb.finishes[k]['feat2']);}
			if (threedb.finishes[k]['feat3']     !== '')     {finishDetailHTML.push(threedb.finishes[k]['feat3']);}
			if (threedb.finishes[k]['feat4']     !== '')     {finishDetailHTML.push(threedb.finishes[k]['feat4']);}
			if (threedb.finishes[k]['feat5']     !== '')     {finishDetailHTML.push(threedb.finishes[k]['feat5']);}
			if (threedb.finishes[k]['leed']       == true)   {finishDetailHTML.push(", LEED");}
		$('finish-detail' + j).innerHTML = finishDetailHTML.join("");

//		$('hdg-lr' + j).innerHTML = $F('lr' + j);
//		$('hdg-fr' + j).innerHTML = $F('fr' + j);
//		$('hdg-fc' + j).innerHTML = $F('fc' + j);

		$('hdg-area' + j).innerHTML = threedb.util.fixNaN(Math.round(parseFloat($F('lr' + j)) + parseFloat($F('fr' + j)) + parseFloat($F('fc' + j))));

		if (threedb.finishes[k].imageurl != "") {
			$('surface-details-image' + j).show();
			$('surface-details-image' + j).setAttribute('src','./images/finishes/' + threedb.finishes[k].imageurl);
			$('surface-details-image' + j).setAttribute('style','margin-right: 12px; margin-bottom: 12px;');
		} else {
			$('surface-details-image' + j).hide();
		}
		
		if (threedb.finishes[k].thickness != "") {$('surfaceThickness' + j).innerHTML = "Thickness: " + threedb.finishes[k].thickness;} else {$('surfaceThickness' + j).innerHTML = "Thickness: Unknown";}
		if (threedb.finishes[k].density != "") {$('surfaceDensity' + j).innerHTML = "Density: " + threedb.finishes[k].density;} else {$('surfaceDensity' + j).innerHTML = "Density: Unknown";}
		if (threedb.finishes[k].mounting != "") {$('surfaceMounting' + j).innerHTML = "Mounting: Type " + threedb.finishes[k].mounting;} else {$('surfaceMounting' + j).innerHTML = "Mounting: Unknown";}

		$('surfaceFeatures' + j).innerHTML = '';
		if (threedb.finishes[k].feat1 != "") {$('surfaceFeatures' + j).appendChild(new Element('li', {className: 'surface-desc'})).appendChild(document.createTextNode(threedb.finishes[k].feat1));}
		if (threedb.finishes[k].feat2 != "") {$('surfaceFeatures' + j).appendChild(new Element('li', {className: 'surface-desc'})).appendChild(document.createTextNode(threedb.finishes[k].feat2));}
		if (threedb.finishes[k].feat3 != "") {$('surfaceFeatures' + j).appendChild(new Element('li', {className: 'surface-desc'})).appendChild(document.createTextNode(threedb.finishes[k].feat3));}
		if (threedb.finishes[k].feat4 != "") {$('surfaceFeatures' + j).appendChild(new Element('li', {className: 'surface-desc'})).appendChild(document.createTextNode(threedb.finishes[k].feat4));}
		if (threedb.finishes[k].feat5 != "") {$('surfaceFeatures' + j).appendChild(new Element('li', {className: 'surface-desc'})).appendChild(document.createTextNode(threedb.finishes[k].feat5));}
		
		$('finishId' + j).setValue(threedb.finishes[k].finishid);

		threedb.rt.freq.each(function(f, i) {
			threedb.user.data.room.surface[j].alpha[i] = parseFloat(threedb.finishes[k]["a" + f]);
			$('surfaceAlpha' + f +'_' + j).innerHTML = threedb.user.data.room.surface[j].alpha[i];
			$('surfaceLR' + f +'_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsLR(i)));
			$('surfaceFR' + f +'_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsFR(i)));
			$('surfaceFC' + f +'_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsFC(i)));
			$('surfaceSum' + f +'_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsSum(i)));
		});

		$('surfaceLR_' + j).innerHTML = $F('lr' + j);
		$('surfaceFR_' + j).innerHTML = $F('fr' + j);
		$('surfaceFC_' + j).innerHTML = $F('fc' + j);
		$('surfaceSum_' + j).innerHTML = threedb.util.fixNaN(Math.round(parseFloat($F('lr' + j)) + parseFloat($F('fr' + j)) + parseFloat($F('fc' + j))));
		
		if ((threedb.finishes[k].standard != "") && (threedb.finishes[k].standardurl != "")) {
			YAHOO.util.Dom.get("surfaceStandard" + i).innerHTML = '<a href="' + threedb.finishes[k].standardurl + '">' + threedb.finishes[k].standard + '</a>';
		} else {
			if (threedb.finishes[k].standard != "") {
				YAHOO.util.Dom.get("surfaceStandard" + j).innerHTML = threedb.finishes[k].standard;
			} else {
				YAHOO.util.Dom.get("surfaceStandard" + j).innerHTML = 'Unavailable';
			}
		}
		
		if ((threedb.finishes[k].source != "") && (threedb.finishes[k].sourceurl != "")) {
			document.getElementById("surfaceSource" + j).innerHTML = '<a href="' + threedb.finishes[k].sourceurl + '">' + threedb.finishes[k].source + '</a>';
		} else {
			if (threedb.finishes[k].source != "") {
				document.getElementById("surfaceSource" + j).innerHTML = threedb.finishes[k].source;
			} else {
				document.getElementById("surfaceSource" + j).innerHTML = 'Unknown';
			}
		}
*/		
		threedb.rt.addSurface.loadSurfaceData(j, threedb.finishes[k]);

		threedb.rt.getSurfaceAreas();

		for (l = 0; l < threedb.rt.freq.length; l++) {
			threedb.user.data.room.surface[j].alpha[l] = parseFloat(threedb.finishes[k]["a" + threedb.rt.freq[l]]);
			$('surfaceAlpha' + threedb.rt.freq[l] + '_' + j).innerHTML = threedb.user.data.room.surface[j].alpha[l];
			$('surfaceLR'    + threedb.rt.freq[l] + '_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsLR(l)));
			$('surfaceFR'    + threedb.rt.freq[l] + '_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsFR(l)));
			$('surfaceFC'    + threedb.rt.freq[l] + '_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsFC(l)));
			$('surfaceSum'   + threedb.rt.freq[l] + '_' + j).innerHTML = threedb.util.fixNaN(Math.round(threedb.user.data.room.surface[j].sabinsSum(l)));
		}

	}

	// j = audiencearea index
	data["audienceareas"].each(function(a, j) {
		threedb.rt.addAudienceArea.render({
			index: threedb.user.data.room.audience.length,
			data: {
				l: data["audienceareas"][j]["l"],
				w: data["audienceareas"][j]["w"],
				h: data["audienceareas"][j]["h"],
				pctOcc: data["audienceareas"][j]["occ"],
				note: data["audienceareas"][j]["note"],
				afid: data["audienceareas"][j]["afid"]
			}
		});
	});
/*	
	for (j = 0; j < data["surfaces"].length; j++) {
		function setSparkline() {
			var sparkdata = new Array(threedb.rt.freq.length);

			for (s = 0; s < threedb.rt.freq.length; s++) {
				sparkdata[s] = Math.round(threedb.user.data.room.surface[j].sabinsSum(s) / threedb.user.data.room.sabinGoal(s) * 100) / 100;
			}
			$('hdg-area' + j).setStyle({backgroundImage: 'url(' + threedb.draw.surfaceSparkline(sparkdata) + ')'});
		}

		setSparkline();
	}

	for (j = 0; j < data["audienceareas"].length; j++) {
		function setSparkline() {
			var sparkdata = new Array(threedb.rt.freq.length);

			for (s = 0; s < threedb.rt.freq.length; s++) {
				sparkdata[s] = Math.round(threedb.user.data.room.audience[j].sabinsSum(s) / threedb.user.data.room.sabinGoal(s) * 100) / 100;
			}
			$('audience-sparkline' + j).innerHTML = threedb.draw.surfaceSparkline(sparkdata);
		}

		setSparkline();
	}
*/	
	var data = false; // Set to false so additional surfaces don't run the routine.  Array is cleared.
	
} else {
//	threedb.rt.addSurface.render(threedb.user.data.room.surface.length);
}

function exportRT() {
	var exportData = {};
	exportData = $('rtcalc').descendants(); // Walk DOM to get all descendant elements
	exportData.each(function(s, index){
		if ((s.tagName == 'TD' || s.tagName == 'SPAN' || s.tagName == 'TEXTAREA' || s.tagName == 'SELECT') && s.id != '') {  // Data is limited to TD and SPAN tags
			document.rtcalc.appendChild(new Element('input', {type: 'hidden', value: s.innerHTML || null, name: '_' + s.id, 'id': '_' + s.id, className: 'exportTemp'})) // Create hidden inputs with the data
		}	
	});
	
	rid = document.rtcalc.action.substr(document.rtcalc.action.indexOf('?'));

	document.rtcalc.action = './rt/rt-export.php' + rid;
	document.rtcalc.submit();
	document.rtcalc.action = './rt.php' + rid;

	$$('input.exportTemp').each(function(e) {e.remove();});
}

