/** calculator functions **/
// data structures

//matrix_0: Defines how a range of angle refers to other arbitrary data values
var matrix_0 = new Array();
matrix_0[0] = new Array();
matrix_0[0][0] = [22.5,125,135];
matrix_0[0][1] = [25,125,135];
matrix_0[0][2] = [27.5,125,135];
matrix_0[0][3] = [30,115,135];
matrix_0[0][4] = [32.5,115,125];
matrix_0[0][5] = [35,105,125];
matrix_0[0][6] = [37.5,105,125];
matrix_0[0][7] = [40,90,115];
matrix_0[0][8] = [42.5,90,115];
matrix_0[0][9] = [45,90,115];
matrix_0[0][10] = [47.5,75,115];
matrix_0[0][11] = [50,75,105];
matrix_0[0][12] = [52.5,75,105];
matrix_0[0][13] = [55,75,105];
matrix_0[0][14] = [57.5,60,105];
matrix_0[0][15] = [60,60,105];
matrix_0[1] = new Array();
matrix_0[1][0] = [25,135,145,67.5];
matrix_0[1][1] = [27.5,120,135,62.5];
matrix_0[1][2] = [30,120,135,62.5];
matrix_0[1][3] = [32.5,120,135,55];
matrix_0[1][4] = [35,105,135,55];
matrix_0[1][5] = [37.5,105,120,55];
matrix_0[1][6] = [40,105,120,45];
matrix_0[1][7] = [42.5,90,120,45];
matrix_0[1][8] = [45,90,120,45];
matrix_0[1][9] = [47.5,80,120,0];
matrix_0[1][10] = [50,80,105,0];
matrix_0[1][11] = [52.5,72.5,105,0];
matrix_0[1][12] = [55,72.5,105,0];
matrix_0[1][13] = [57.5,62.5,105,0];
matrix_0[1][14] = [60,62.5,105,0];

// matrix_1: Defines which angles have a "special" note - associated/mapped to correspond with matrix_0
var matrix_1 = new Array();
matrix_1[0] = new Array();
matrix_1[0][0] = [0,0,0];
matrix_1[0][1] = [0,0,0];
matrix_1[0][2] = [0,0,0];
matrix_1[0][3] = [0,0,0];
matrix_1[0][4] = [0,0,0];
matrix_1[0][5] = [0,0,0];
matrix_1[0][6] = [0,0,0];
matrix_1[0][7] = [0,0,0];
matrix_1[0][8] = [0,0,0];
matrix_1[0][9] = [0,0,0];
matrix_1[0][10] = [0,0,0];
matrix_1[0][11] = [0,0,0];
matrix_1[0][12] = [0,0,0];
matrix_1[0][13] = [0,0,0];
matrix_1[0][14] = [0,1,0];
matrix_1[0][15] = [0,1,0];
matrix_1[1] = new Array();
matrix_1[1][0] = [0,0,0,0];
matrix_1[1][1] = [0,0,0,0];
matrix_1[1][2] = [0,0,0,0];
matrix_1[1][3] = [0,0,0,1];
matrix_1[1][4] = [0,0,0,1];
matrix_1[1][5] = [0,0,0,1];
matrix_1[1][6] = [0,0,0,1];
matrix_1[1][7] = [0,0,0,1];
matrix_1[1][8] = [0,0,0,1];
matrix_1[1][9] = [0,1,0,0];
matrix_1[1][10] = [0,1,0,0];
matrix_1[1][11] = [0,0,0,0];
matrix_1[1][12] = [0,0,0,0];
matrix_1[1][13] = [0,0,0,0];
matrix_1[1][14] = [0,0,0,0];
matrix_1[1][15] = [0,0,0,0];

// matrix_2: Defines a datastructure to map Tegral Product Codes to their NCS/RAL listing code
var matrix_2 = new Array();
matrix_2[0] = new Array();
matrix_2[0][0]=['White - N191','','RAL 9006'];
matrix_2[0][1]=['Anthracite - N251','NCS-S-7500-N','RAL 7022'];
matrix_2[0][2]=['Grey - N271','NCS-S-1010-R80B-1',''];
matrix_2[0][3]=['Grey - N272','NCS-S-1010-R80B-3',''];
matrix_2[0][4]=['Grey - N273','NCS-S-1010-R80B-5',''];
matrix_2[0][5]=['Grey - N281','','RAL 7012'];
matrix_2[0][6]=['Grey - N291','','RAL 7048'];
matrix_2[0][7]=['Grey - N293','','RAL 9007'];
matrix_2[0][8]=['Rubin - N359','NCS-S-5030-Y80R','RAL 8029'];
matrix_2[0][9]=['Beige - N892','','RAL 1035'];
matrix_2[0][10]=['Brown - N971','NCS-S-7010-Y30R-1',''];
matrix_2[0][11]=['Brown - N972','NCS-S-7010-Y30R-3',''];
matrix_2[0][12]=['Brown - N973','NCS-S-7010-Y30R-5',''];
matrix_2[1] = new Array();
matrix_2[1][0]=['Anthracite - NP251','NCS-S-7500-N','RAL 7022'];
matrix_2[1][1]=['Crème White - NP154','NCS-S-1502-Y',''];
matrix_2[1][2]=['Rubin  - NP359','NCS-S-5030-Y-80R','RAL 8029'];
matrix_2[1][3]=['Brown - NP973','NCS-S-7010-Y30R-5',''];
matrix_2[1][4]=['White - NP191','','RAL 9006'];
matrix_2[2] = new Array();
matrix_2[2][0]=['Beige - PI 841','NCS-S-1002-R',''];
matrix_2[2][1]=['Beige - PI 842','NCS-S-2005-Y30R',''];
matrix_2[2][2]=['Blue - PI 441','NCS-S-5020-R80B',''];
matrix_2[2][3]=['Brown - PI 941','NCS-S-8010-Y50R',''];
matrix_2[2][4]=['Brown - PI 943','NCS-S-4010-Y50R',''];
matrix_2[2][5]=['Grey - PI 241','NCS-S-6500-N',''];
matrix_2[2][6]=['Grey - PI 242','','RAL 7037'];
matrix_2[2][7]=['Grey - PI 243','NCS-S-2500-N',''];
matrix_2[2][8]=['Green - PI 541','NCS-S-7010-G10Y',''];
matrix_2[2][9]=['Green - PI 542','','RAL 1027'];
matrix_2[2][10]=['Green - PI 543','NCS-6020-Y',''];
matrix_2[2][11]=['Orange - PI 741','NCS-S-3050-Y50R',''];
matrix_2[2][12]=['Red - PI 341','NCS-S-4550-Y80R',''];
matrix_2[2][13]=['Black - PI 041','NCS-S-8500-N',''];
matrix_2[2][14]=['White - PI 141','NCS-S-0502-B',''];
matrix_2[3] = new Array();
matrix_2[3][0]=['White - TG102','NCS-S-0500-N','RAL 9003'];
matrix_2[3][1]=['Grey - TG205','NCS-S-2000-N','RAL 7047'];
matrix_2[3][2]=['Grey - TG206','NCS-S-4000-N','RAL 7004'];
matrix_2[3][3]=['Blue - TG403','NCS-S-2010-B50G',''];
matrix_2[3][4]=['Blue - TG404','NCS-S-3010-B',''];
matrix_2[3][5]=['Green - TG504','NCS-S-1005-B80G',''];
matrix_2[3][6]=['Yellow - TG601','NCS-S-1010-Y',''];
matrix_2[3][7]=['Yellow - TG602','NCS-S-1020-Y20R',''];
matrix_2[3][8]=['Beige - TG803','NCS-S-2010-Y',''];
matrix_2[3][9]=['Black - TA001','NCS-S-8000-N',''];
matrix_2[3][10]=['Orange - TA701','NCS-S-4050-Y60R','RAL 8023'];
matrix_2[3][11]=['Red - TA304','NCS-S-4050-R','RAL 3011'];
matrix_2[3][12]=['Red - TA305','NCS-S-2070-Y90R','RAL 3000'];
matrix_2[3][13]=['Blue - TA405','NCS-S-4550-R80B','RAL 8002'];
matrix_2[3][14]=['Grey - TA207','NCS-S-6005-R80B',''];

var matrix_3 = new Array('Natura','Natura Pro','Pictura','Textura');

// map data values to selected
function calcRidgeAngle(f) {
	// 1 - calculation choice
	window.alert(f);
	//var calctype = f.calc_type;
	/*
	// 2 - angle choice
	var angle = $(f);
	// 3 - find mapping
	var resultSet = new Array();
	for(var i = 0; i < matrix[calctype][angle].length; i++) {
		resultSet.push(matrix[calctype][angle][i]);
	}
	*/
	// 4 - update display
	//
	return false;
}

function emptyList( box ) {
  if(box!=null){
    while ( box.options.length ) box.options[0] = null;
  }
}
function isEmpty(s) {
	var s = new String(s);
	s = s.replace(/^\s+/, '');
	s = s.replace(/\s+$/, '');
	return (s.length == 0 || s == '');
}

function fillList( box, arr, defaultValue, keypos) {
	DselectedIndex = 0;
	var buff = '';
	for (key in arr) {
		//window.alert(isEmpty(arr[key][keypos]));
		//buff += arr[key][keypos] + ":" + isEmpty(arr[key][keypos]) + "\n";
		if(!isEmpty(arr[key][keypos])) {
			option = new Option( arr[key][keypos],key);
			if(key == defaultValue){
				DselectedIndex = box.length;
			}
			box.options[box.length] = option;
		}
		//
	}
	box.selectedIndex=DselectedIndex;
	//window.alert(buff);
}

function reset(f) {
	switch(f) {
		case "calcForm":
			$("#selang").html("0");$("#ridcap").html("0");$("#hipcap").html("0");if($("#monrid"))$("#monrid").html("0");$("#special").hide();
			break;
		case "ncsForm":
			$("#tegrange").html("-");$("#tegcode").html("-");$("#ncscode").html("-");$("#ralcode").html("-");
			break;
		default:
	}
}
var selectText = "Select...";
// attach actions
// D O C U M E N T  L O A D
$(document).ready(function(){
	// RIDGE ANGLE CALCULATOR - BIND OPTIONS ACTIONS
	$("input[name|=calctype]", document.calcForm).each(function(index,domEle) {
		//window.alert($(this).attr('id'));
		$(this).click( function() {
			var selectedMatrix = $(this).attr('id');
			selectedMatrix = selectedMatrix.substr(selectedMatrix.lastIndexOf("_")+1);
			//window.alert($("#roof_pitch_angle"));
			var DOM_select = document.getElementById("roof_pitch_angle");
			emptyList(DOM_select);
			option = new Option(selectText,-1);
			DOM_select.options[DOM_select.length] = option;
			fillList(document.getElementById("roof_pitch_angle"),matrix_0[selectedMatrix],-1,0);
			// add mono-ridge row depending on selected
			if(selectedMatrix == 1) $("#table_results").append("<tr id=\"row_modrid\"><td class=\"label\">Mono-Ridge</td><td><span id=\"monrid\">0</span>&deg; <span id=\"special_monrid\" class=\"note special\">special</span></td></tr>");
			else if($("#row_modrid")) $("#row_modrid").remove();
			// show relevant para
			$("#note_" + selectedMatrix).show();
			$("#note_" + (1 - selectedMatrix)).hide();
			// reset values
			reset('calcForm');
		});
	});
	$("#roof_pitch_angle").change( function() {
		if($(this).first().val() != -1) {
			var selectedAngle = $(this).val();
			var selectedMatrix = 0;
			$("input[name|=calctype]").each(function(index,domEle) {
				if(domEle.checked == true) selectedMatrix = $(this).val();
			});
			//window.alert(matrix[selectedMatrix][selectedAngle][0]);
			//ridcap,hipcap,monrid,note
			$("#selang").html(matrix_0[selectedMatrix][selectedAngle][0]);
			$("#ridcap").html(matrix_0[selectedMatrix][selectedAngle][1]);
			$("#hipcap").html(matrix_0[selectedMatrix][selectedAngle][2]);
			if(matrix_0[selectedMatrix][selectedAngle][3] != undefined) $("#monrid").html(matrix_0[selectedMatrix][selectedAngle][3]);
			if(matrix_1[selectedMatrix][selectedAngle][1]) { $("#special_ridcap").show(); } else { $("#special_ridcap").hide(); }
			if(matrix_1[selectedMatrix][selectedAngle][3]) { $("#special_monrid").show(); } else { $("#special_monrid").hide(); }
		} else { reset('calcForm'); }

	});
	$("#calctype_1").click();
	var DOMSelect = new Array("code_tegral","code_ncs","code_ral");
	var DOMUpdateFields = new Array("tegcode","ncscode","ralcode");
	// NCS CODES - BIND OPTIONS ACTIONS
	$("input[name|=tegtype]", document.ncsForm).each(function(index,domEle) {
		$(this).click( function() {
			var selectedMatrix = $(this).attr('id');
			selectedMatrix = selectedMatrix.substr(selectedMatrix.lastIndexOf("_")+1);
			for (var i=0; i<DOMSelect.length; i++) {
				var DOM_select = document.getElementById(DOMSelect[i]);
				emptyList(DOM_select);
				option = new Option(selectText,-1);
				DOM_select.options[DOM_select.length] = option;
				fillList(DOM_select,matrix_2[selectedMatrix],-1,i);
			}
			reset('ncsForm');
		});
	});
	// apply change actions
	for (var i=0; i<DOMSelect.length; i++) {
		$("#" + DOMSelect[i]).change( function() {
			if($(this).first().val() != -1) {
				// get selected tegtype
				var selectedAngle = $(this).val();
				var selectedMatrix = 0;
				$("input[name|=tegtype]").each(function(index,domEle) {
					if(domEle.checked == true) selectedMatrix = $(this).val();
				});
				// update fields
				$("#tegrange").html(matrix_3[selectedMatrix]);
				for (var j=0; j<DOMUpdateFields.length; j++) {
					if(matrix_2[selectedMatrix][selectedAngle][j]) $("#" + DOMUpdateFields[j]).html(matrix_2[selectedMatrix][selectedAngle][j]); else $("#" + DOMUpdateFields[j]).html('-');
				}
				// update select fields
				for (var j=0; j<DOMSelect.length; j++) {
					if($(this).attr('id') != $("#" + DOMSelect[j]).attr('id')) {
						if(!isEmpty(matrix_2[selectedMatrix][selectedAngle][j])) $("#" + DOMSelect[j]).val(selectedAngle);
						else $("#" + DOMSelect[j]).val(-1);
					}
				}
				
			} else { reset('ncsForm'); }
		});
	}
	// default
	$("#tegtype_0").click();
});
