// last updated on 10-DEC-2006
// created by boris kovalevsky
// imagerev.com
// email: pcne@hotmail.com
// all rights reserved

window.onload=init;
function init() {
	var debug = 0;
	var v1=0;
	var v2=0;
	var v3=0;
	var v4=0;
	//var v5=0;
	//var v6=0;
	var v7=0;
	var v8=0;
	var v10=0;
	var v11=0;
	var v12=0;
	var v13=0;
	var v14=0;
	var v15=0;
	
	var vclass = document.getElementById("class").value;
	var sab = document.getElementById("sab").value;
	var esc = document.getElementById("esc").value;
	var weight = document.getElementById("weight").value;
	//var nfrontal_d = document.getElementById("nfrontal_driver").value;
	//var nfrontal_p = document.getElementById("nfrontal_pass").value;
	var n_side_front = document.getElementById("nside_front").value;
	var n_side_rear = document.getElementById("nside_rear").value;
	var nrollover = document.getElementById("nrolloverrisk").value;
	var ifront = document.getElementById("ifront").value;
	var iside = document.getElementById("iside").value;
	var irear = document.getElementById("irear").value;
	var itop = document.getElementById("itop").value;
	var nfrontalrisk = document.getElementById("nfrontal_risk").value;
	
	var score = document.getElementById("score");
	var frontal_impact = document.getElementById("frontal_impact");
	var side_impact = document.getElementById("side_impact");
	var rear_impact = document.getElementById("rear_impact");
	var rollover = document.getElementById("rollover");
	
	if (iside != "average") {		
		document.getElementById("sab").disabled = true;
	}
	
	if (debug) {
		alert('sab '+sab+
		  '\nesc '+esc+
		  '\nweight '+weight+
		  //'\nnfrontal_d '+nfrontal_d+
		  //'\nnfrontal_p '+nfrontal_p+
		  '\nn_side_front '+n_side_front+
		  '\nn_side_rear '+n_side_rear+
		  '\nnrollover '+nrollover+
		  '\nifront '+ifront+
		  '\niside '+iside+
		  '\nirear '+irear+
		   '\nitop '+itop+
		  '\nnfrontalrisk '+nfrontalrisk);
	} // end if
	
	// sab value
	if (sab.toUpperCase() == 'UNKNOWN') {
		v2 = selectionFactors[57][1];
	}
	else if (sab.toUpperCase() == 'W/SAB') {
		if (iside.toUpperCase() == 'AVERAGE') {
			v2 = selectionFactors[53][1];
		}
		else { v2 = selectionFactors[54][1]; }
	}
	else {
		if (iside.toUpperCase()=='AVERAGE') {
			v2 = selectionFactors[55][1];
		}
		else {
			v2 = selectionFactors[56][1];
		}
	}
	
	// esc value
	if (esc.toUpperCase() == 'UNKNOWN') {
		v3 = selectionFactors[3][1];
	}
	else if (esc.toUpperCase() == 'NO ESC') {
		if (nrollover.toUpperCase()=='AVERAGE') {
			v3 = selectionFactors[1][1];
		}
		else { v3 = selectionFactors[2][1]; }
	}
	else {
		v3 = selectionFactors[0][1];
	}
	
	// get weight coeficient
	if (vclass.toUpperCase() == 'PASS. CAR') {
		for(i=0; i<passCarWeightFactors.length; i++) {
			if (passCarWeightFactors[i][0]==weight) {
				v4 = passCarWeightFactors[i][1];
			}
		}		
	}
	else if (vclass.toUpperCase() == 'SUV') {
		for(i=0; i<suvWeightFactors.length; i++) {
			if (suvWeightFactors[i][0]==weight) {
				v4 = suvWeightFactors[i][1];
			}
		}		
	}
	else if (vclass.toUpperCase() == 'VAN') {
		for(i=0; i<vanWeightFactors.length; i++) {
			if (vanWeightFactors[i][0]==weight) {
				v4 = vanWeightFactors[i][1];
			}
		}		
	}
	else if (vclass.toUpperCase() == 'PICKUP') {
		for(i=0; i<pickUpFactors.length; i++) {
			if (pickUpFactors[i][0]==weight) {
				v4 = pickUpFactors[i][1];
			}
		}		
	}
	
	// NHTSA frontal impact star rating driver side
	/*v5 = selectionFactors[12][1];
	for (c=1; c<=5; c++) {
		if (nfrontal_d == c) { v5 = selectionFactors[c+6][1]; }
	}*/
	
	// NHTSA frontal impact star rating (passenger side)
	/*v6 = selectionFactors[18][1];
	for (c=1; c<=5; c++) {
		if (nfrontal_p == c) {
			v6 = selectionFactors[c+12][1];
		}
	}*/		
	
	// NHTSA side impact star rating (front seat)
	v7 = selectionFactors[46][1];
	for (c=1; c<=5; c++) {
		if (n_side_front == c) {
			v7 = selectionFactors[c+40][1];
		}
	}
	
	// NHTSA side impact star rating (rear seat)
	v8 = selectionFactors[52][1];
	for (c=1; c<=5; c++) {
		if (n_side_rear == c) {
			v8 = selectionFactors[c+46][1];
		}
	}
	
	// NHTSA rollover risk
	if (nrollover.toUpperCase() == 'AVERAGE') {
		switch(vclass) {
			case 'PASS. CAR':
			v10 = selectionFactors[37][1];
			break;
			case 'SUV':
			v10 = selectionFactors[38][1];
			break;
			case 'VAN':
			v10 = selectionFactors[39][1];
			break;
			case 'PICKUP':
			v10 = selectionFactors[40][1];
			break;
			default:
			v10 = nrollover;	
		}
	} 
	else {
		if (nrollover > 0) { v10 = nrollover; }
	}
	
	// NHTSA frontal risk 
	if (nfrontalrisk.toUpperCase() == 'AVERAGE') {
		//v14 = 0.15;
		v14 = selectionFactors[36][1];
	}
	else {
		if (nfrontalrisk > 0) { v14 = nfrontalrisk; }
	}
	
	// IIHS frontal rating
	switch(ifront.toUpperCase()) {
		case 'POOR':
		v11 = selectionFactors[4][1];
		break;
		case 'MARGINAL':
		v11 = selectionFactors[5][1];
		break;
		case 'ACCEPTABLE':
		v11 = selectionFactors[6][1];
		break;
		case 'GOOD':
		v11 = selectionFactors[7][1];
		break;
		default:
		v11 = selectionFactors[8][1];
		break;
	} 
	
	// IIHS side impact rating
	switch(iside.toUpperCase()) {
		case 'POOR':
		v12 = selectionFactors[19][1];
		break;
		case 'MARGINAL':
		v12 = selectionFactors[20][1];
		break;
		case 'ACCEPTABLE':
		v12 = selectionFactors[21][1];
		break;
		case 'GOOD':
		v12 = selectionFactors[22][1];
		break;
		default:
		v12 = selectionFactors[23][1];
		break;
	} 
	
	// IIHS rear impact rating
	switch(irear.toUpperCase()) {
		case 'POOR':
		v13 = selectionFactors[9][1];
		break;
		case 'MARGINAL':
		v13 = selectionFactors[10][1];
		break;
		case 'ACCEPTABLE':
		v13 = selectionFactors[11][1];
		break;
		case 'GOOD':
		v13 = selectionFactors[12][1];
		break;
		default:
		v13 = selectionFactors[13][1];
		break;
	}
	
	// IIHS roof strength rating
	switch(itop.toUpperCase()) {
		case 'POOR':
		v15 = selectionFactors[14][1];
		break;
		case 'MARGINAL':
		v15 = selectionFactors[15][1];
		break;
		case 'ACCEPTABLE':
		v15 = selectionFactors[16][1];
		break;
		case 'GOOD':
		v15 = selectionFactors[17][1];
		break;
		default:
		v15 = selectionFactors[18][1];
		break;
	}
	
	if (debug) {
		alert('v2 '+v2+
		  '\nv3 '+v3+
		  '\nv4 '+v4+
		  //'\nv5 '+v5+
		  //'\nv6 '+v6+
		  '\nv7 '+v7+
		  '\nv8 '+v8+
		  '\nv10 '+v10+
		  '\nv11 '+v11+
		  '\nv12 '+v12+
		  '\nv13 '+v13+
		  '\nv14 '+v14+
		  '\nv15 '+v15);
	} // end if

	// frontal impact fatality
	var fif = coefValues[0] * v4 * ((coefValues[1] * v14) + (coefValues[2] * v11)) * coefValues[3];
	if (fif > 0) { replaceText(frontal_impact, roundnum(fif)); } else { replaceText(frontal_impact, 'ERROR'); }
	// side impact fatality
	var sif  =coefValues[4]*(coefValues[5]*v7+coefValues[6]*v8+coefValues[7]*[v12])*v2*coefValues[8];
	if (sif > 0) { replaceText(side_impact, roundnum(sif)); } else { replaceText(side_impact, 'ERROR'); }
	// rear impact fatality
	var rif = coefValues[9]*v13*coefValues[10];
	if (rif > 0) { replaceText(rear_impact, roundnum(rif)); } else { replaceText(rear_impact, 'ERROR'); }
	// rollover fatality risk points
	
	var rfr = coefValues[11]*v3*v10*coefValues[12]*v15;
	if (rfr > 0) { replaceText(rollover, roundnum(rfr)); } else { replaceText(rollover, 'ERROR'); }
	// calc score
	if ((rfr)&&(rif)&&(sif)&&(fif)) {
		si = roundnum(rfr+rif+sif+fif);
		replaceText(score, si);
	}
	else {
		replaceText(score, 'ERROR');
	}
} // end init

// function to replace text in a span element
function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}
function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}
function roundnum(num) {
	x =  Math.round(num*10)/10;
	if (x == Math.floor(x)) {
		return x+".0";
	}
	else return x;
}
function change_sab() {
	var iside = document.getElementById("iside").value;
	var sab = document.getElementById("sab");
	
	if (iside=='average') {		
		sab.disabled = false;
	}
	else {
		sab.disabled = true;
		alert("This rating is based on the SAB configuration of the test vehicle. The SAB\n"+
			  "configuration cannot be changed independently, unless \"Average\" rating is\nselected.");		
	}	
}