/*
QSC advanced amplifier selector
Version: 0.1
Released: April 30, 2008
User specifies the power handling rating of their speaker and 
their speaker configuration and this script lists the recommended
QSC amplifiers.
Copyright 2006 QSC Audio Products, LLC
Unauthorized copying of this script is prohibited
*/

//You need to run data.js before this script

// redirect to iPhone version
if(navigator.userAgent.indexOf('iPhone')!=-1){
	document.location.href="amp_selector/iphone_amp_selector.htm";
}

if(! data_loaded){
	document.write("ERROR: data file was not loaded");	
}



var info = "";
var attention_color = "#fcffa6";
var attention_btn_color = "#ffe400";
var normal_color = "#EFEFEF";
var scale = 4; // 1 pixel = 4 watts - the scale.gif image must reflect this scale factor
var barstartpoint = 96; // this adjusts for the width of the 1st table column - 15px
var showall = 0;

var min2ohm = 325;
var min4ohm = 325;
var min8ohm = 125;
var max2ohm = 3000;
var max4ohm = 5000;
var max8ohm = 4000

var imp1 = 8;
var pwr1 = 0;
var pwr1adj = 0;
var dbl1 = 0;
var info1 = "";
var imp2 = 8;
var pwr2 = 0;
var pwr2adj = 0;
var dbl2 = 0;
var info2 = "";



function calculate(imp,pwr,info,whichform){
	if(data_loaded){
		// select the range for this impedance selection
		if(imp==2){
				minlimit = min2ohm;
				maxlimit = max2ohm;
		} else if(imp == 4){
				minlimit = min4ohm;
				maxlimit = max4ohm;
		} else {
				minlimit = min8ohm;
				maxlimit = max8ohm;	
		}
		if(pwr != '' && (pwr < minlimit || pwr > maxlimit)){
			show_error(minlimit,maxlimit,whichform);
			return;
		}
		if(imp < 2){
			if(whichform == '2'){
			document.getElementById('results'+whichform).innerHTML = "<b class=\"error\">The total impedance of the subwoofer configuration would be below 2 ohms.</b>";	
			} else {
				document.getElementById('results'+whichform).innerHTML = "<b class=\"error\">The total impedance of the full range configuration would be below 2 ohms.</b>";
			}
			
			return;	
		}
		
		// set the startpoint for the graph rounded to the nearest 100 watts
		//minimum = Math.max(100,(Math.round((pwr1adj/100)*0.6)*100));
		minimum = Math.max(100,(Math.round((pwr-400)/100)*100));
		output = "<span class=\"resultlabel\"><b>Recommended Amplifiers" + info;
		output += "<div class=\"resultstable\" style=\"position:relative;padding-top:27px\">\n";
		output += pointer(pwr,minimum,imp);
		output += "<table width=\"100%\" cellpadding=1 cellspacing=0 class=\"outline_table\">\n";
		output += "<tr>\n";
		output += "<th>Choose One:</th>\n";
		// set the background-position of the scale
		// note: the scale image is offset to the left by 15px
		scalepos = (minimum/scale) -15;
		output += "<th class=\"scale\" style=\"background-position:-"+scalepos+"px 4px\"><img src=\"amp_selector/blank.gif\" width=200 height=1 alt=''></th>\n";
		output += "<th>Power&nbsp;Rating</th>\n";
		output += "<th>Weight</th>\n";
		output += "<th>List&nbsp;Price</th>\n";
		output += "<th>&nbsp;</th>\n";
		output += "</tr>\n";
		// find matching amplifiers
		ampcount = 0;
		for(i=0;i<amp_models.length;i++){
			ampspecs = get_amp(i,imp);
			powerrating = ampspecs[0];
			minpwr = ampspecs[1];
			maxpwr = ampspecs[2];
			bridged = ampspecs[3];
			url = ampspecs[5];
			modelname = "<a href=\""+url+"\" target=_blank style=\"color:#000000\"><b>" + ampspecs[4] + "</b></a>";
			weight = ampspecs[6];
			price = ampspecs[7];
			buynow = "<a href=\""+ampspecs[8]+"\"><img src=\"amp_selector/sm_buy_it_now.png\" width=80 height=18 alt=\"Buy It Now\" border=0></a>";
			if(bridged == "1"){
				modelname += "**";
 			}
			if((minpwr<=pwr && maxpwr>=pwr) || showall){
				output += "<tr><td width=90>";
				output += modelname;
				output += "</td>";
				output += "<td class=\"bar\" width=255>" + bar(minpwr,maxpwr,minimum)  + "</td>";
				output += "<td>" + powerrating + "&nbsp;W&nbsp;@&nbsp;"+ imp +"&nbsp;ohms*</td>\n";
				output += "<td>" + weight + "</td>\n";
				output += "<td align=right>" + price + "&nbsp;</td>\n";
				output += "<td>" + buynow + "</td>\n";
				output += "</tr>";
				ampcount ++;
			}
		}
		output += "</table>\n";
		output += "</div>"
		output += show_notes();
		if(! ampcount){
			output = "<b class=\"error\">There were no compatible amplifiers found for this loudspeaker configuration.</b>";
		}
		if(pwr != ''){
			document.getElementById('results'+whichform).innerHTML = output;
			document.getElementById('notes').style.display = "block";
		}
	}
}


function get_amp(j,imp){
	rating= amp_rating_2ohm;
	if(imp==4){
		rating = amp_rating_4ohm;
	}
	if(imp==8){
		rating = amp_rating_8ohm;;
	}
	powerrating = rating[j];
	// calculate spread as +/- 1 dB
	// 0.74, 1.26
	minpwr = Math.floor(powerrating*0.74);
	maxpwr = Math.ceil(powerrating*1.26);

	modelname = amp_models[j];
	url = amp_links[j];
	bridged = amp_bridged[j];
	weight = amp_weight[j];
	price = amp_price[j];
	buynow = amp_buynow[j];
	return new Array(powerrating,minpwr,maxpwr,bridged,modelname,url,weight,price,buynow);
}

function get_model(frm1,whichform){
	document.getElementById('modelborder'+whichform).style.backgroundColor = normal_color;  
	document.getElementById('modelmessage'+whichform).style.fontWeight = "normal";
	selected_spk = frm1.model.options[frm1.model.selectedIndex].value;	
	if(selected_spk != ""){
		if(selected_spk < spk_data.length){
			speaker_data = spk_data[selected_spk];
			manuf = speaker_data[0];
			model = speaker_data[1];
			imp = speaker_data[2];
			pwr = speaker_data[3];
			pwradj = pwr;
			subwf = speaker_data[4];
			info = " for " + manuf + " " + model;
			info += "</b> (" + imp + " ohms, " + pwr + " watts program power rating)";
			if(frm1.double.checked){
				pwradj = pwr*2;
				info += " - 2 per side (parallel)";
				imp = Math.floor(imp)/2;	
			}
			info += "</span>\n";
			calculate(imp,pwradj,info,whichform);
		}		
	}
}

function get_custom(frm1,whichform){
	for(g=0;g<frm1.elements.length;g++){
		if(frm1.elements[g].name == "impedance" && frm1.elements[g].checked){
			//needed to get the value of the radio button
			imp = frm1.elements[g].value;
			break;
		}
	}
		pwrratingmode = "cont";
		info = " for " + imp + " ohm ";
		if(whichform == '2'){
			info += "subwoofers, ";
		} else{
			info += "loudspeakers, ";
		}
	for(g=0;g<frm1.elements.length;g++){	
		if(frm1.elements[g].name == "pwrratingmode" && frm1.elements[g].checked){
			pwrratingmode = frm1.elements[g].value;
			break;
		}
	}
		pwr = Math.floor(frm1.power_rating.value);
		if(pwrratingmode == "cont"){
			info += pwr + " watts continuous power rating</b>";
			pwr = pwr*2;
		} else {
			info += pwr + " watts program power rating</b>";
		}
		if(frm1.doublecustom.checked){
			pwr = pwr*2;
			imp = Math.floor(imp)/2;
			info += " - 2 per side (parallel)";	
		}
		info += "</span>\n";
	calculate(imp,pwr,info,whichform);
}


function bar(start,end,minimum){
	blank = (start-minimum)/scale - 4;
	baroutput = "<div style=\"overflow:hidden;width:255px\"><img src=\"amp_selector/bar.gif\" height=16 width=" + (end-start)/scale + " style=\"margin-left:"+blank+"px\"></div>";
	return baroutput;
}

function pointer(val,minimum,imp){
	pos = barstartpoint + ((val-minimum)/scale);
	label = val + " W recommended amp power (" + imp + " ohms)";
	return "<div class=\"pointer\" style=\"left:" + pos + "px\"><b>"+label+"</b></div>\n";	
}

function show_error(minlimit,maxlimit,whichform){
	
	if(whichform =='2'){
		document.getElementById('results2').innerHTML = "<b class=\"error\">Subwoofer power rating must be between " + minlimit + " and " + maxlimit + " watts. Please modify the values and try again.</b>";
	} else {
		document.getElementById('results').innerHTML = "<b class=\"error\">Loudspeaker power rating must be between " + minlimit + " and " + maxlimit + " watts. Please modify the values and try again.</b>";
	}
	
}


function show_notes(){
	return	"<i class=\"smaller\">* EIA 1 kHz 1% THD</i><hr style=\"height:1px;\">";
}


function reset_manuf_menu(frm1,whichform){
	if(data_loaded){
		// populate manufacturer menu
		thisform = document.forms[frm1];
		for(i=0;i<manufacturers.length;i++){
			thisform.manufacturer.options.length ++;
			thisform.manufacturer.options[thisform.manufacturer.options.length-1].value= manufacturers[i];
			thisform.manufacturer.options[thisform.manufacturer.options.length-1].text= manufacturers[i];	
			document.getElementById('manufborder'+whichform).style.backgroundColor = attention_color; 
			document.getElementById('manufmessage'+whichform).style.fontWeight = "bold";
			document.getElementById('modelborder'+whichform).style.display = "none";
		}
	} else {
			// no data	
			document.forms[frm1].manufacturer.options[0].text= "";
	}
	if(whichform =='2'){
		document.spkrcustomform2.custom.checked = false;
	} else {
		document.spkrcustomform.custom.checked = false;
	}

	document.getElementById('numspeakers'+whichform).style.display = "none";
	document.getElementById('notes').style.display = "none";
	
}

function set_models(thisform,manuf,whichform){
	field = thisform.model;
	document.getElementById('manufborder'+whichform).style.backgroundColor = normal_color; 
	document.getElementById('manufmessage'+whichform).style.fontWeight = "normal";
	
	document.getElementById('modelborder'+whichform).style.display = "block";
	document.getElementById('modelborder'+whichform).style.backgroundColor = attention_color;  
	document.getElementById('modelmessage'+whichform).style.fontWeight = "bold";
	
	document.getElementById('numspeakers'+whichform).style.display = "block";
	
	document.getElementById('results'+whichform).innerHTML = "";
	
	field.options.length = 1;
	for(i=0;i<spk_data.length;i++){
		row = spk_data[i];
		if(row[0] == manuf){
			if(whichform == '2'){
				// subwoofers
				if(row[4] == '1'){
					field.options.length ++;
					field.options[field.options.length-1].value = i;
					field.options[field.options.length-1].text = row[1] ;
				}
			} else {
				//full range	
				if(row[4] == '0'){
					field.options.length ++;
					field.options[field.options.length-1].value = i;
					field.options[field.options.length-1].text = row[1] ;	
				}
			}
		}	
	}
	if(field.options.length == 1){
		// no data
		field.options[0].text= "None Available";	
	} else {
		field.options[0].text= "- Select One - ";
	}
}


function selectCustom(chk,whichform){
	if(chk.checked){
		// custom field selected
		document.getElementById('spkrmanufform'+whichform).style.display = "none";
		document.getElementById('or'+whichform).style.display = "none";
		document.getElementById('customform'+whichform).style.display = "block";
		document.getElementById('results'+whichform).innerHTML = "";
		get_custom(document.forms['spkrcustomform'+whichform],whichform);
		
	} else {
		document.getElementById('spkrmanufform'+whichform).style.display = "block";
		document.getElementById('or'+whichform).style.display = "block";
		document.getElementById('customform'+whichform).style.display = "none";
		document.getElementById('results'+whichform).innerHTML = "";
		get_model(document.forms['spkrcustomform'+whichform],whichform);
	}
	
}





