var xmlDoc;var presetCars=new Array();var selectedCarPreset=new Array();var xmlDescription;var xmlWeight;var xmlWeightUnits;var xmlMinRpm;var xmlMaxRpm;var xmlStepRpm;var xmlMinTorque;var xmlMaxTorque;var xmlTorques=new Array();var xmlNumGears;var xmlGearRatios=new Array();var xmlDifferentialRatio;var xmlTireWidth;var xmlTireAspect;var xmlTireRim;var xmlDragCoefficient;var xmlFrontalArea;var xmlCarName;var xmlShiftingTime;function loadXml(){if(document.implementation&&document.implementation.createDocument){xmlDoc=document.implementation.createDocument("","",null);xmlDoc.onload=collectCarNames;}else if(window.ActiveXObject){xmlDoc=new ActiveXObject("Microsoft.XMLDOM");xmlDoc.onreadystatechange=function(){if(xmlDoc.readyState==4){collectCarNames();}}}else{alert('Your browser doesn\'t support XML functionality');return;};xmlDoc.load("cars.xml");}function collectCarNames(){var root=xmlDoc.getElementsByTagName('root');for(var car=0;car<root[0].childNodes.length;car++){var carNode=root[0].childNodes[car];if(carNode.nodeType!=1)continue;for(var data=0;data<carNode.childNodes.length;data++){var dataNode=carNode.childNodes[data];if(dataNode.nodeType!=1)continue;if(dataNode.nodeName=="name"){presetCars.push(dataNode.firstChild.nodeValue);}}}for(var car=0;car<numCars;car++){var id="preset_car"+car;setPresets(id,car);}}function setPresets(id,car){var content="<FONT size='-1'>Presets:</FONT> <SELECT onChange='carSelect("+car+", this.selectedIndex)'>";if(selectedCarPreset[car]=="Custom"){content+="<OPTION SELECTED>Custom";}else{content+="<OPTION>Custom";};for(var i=0;i<presetCars.length;i++){if(selectedCarPreset[car]==presetCars[i]){content+="<OPTION SELECTED>"+presetCars[i];}else{content+="<OPTION>"+presetCars[i];}}content+="</SELECT>";document.getElementById(id).innerHTML=content;}function carSelect(car,ind){var val=presetCars[parseInt(ind)-1];//alert("car: " + car + " val: " + val);
selectedCarPreset[car]=val;setVarValues(car,val,true);draw();}function setVarValues(car,carPreset,force){if(carPreset=="Custom"){if(typeof(carNames[car])=="undefined"||force){carNames[car]="Custom car "+car;}if(typeof(description[car])=="undefined"||force){description[car]="Custom car";}if(typeof(g_weight[car])=="undefined"||force){g_weight[car]=3000;}// Dynamometer
if(typeof(stepRpm[car])=="undefined"||force){stepRpm[car]=500;saneStepRpm[car]=true;}if(typeof(minRpm[car])=="undefined"||force){minRpm[car]=2000;saneMinRpm[car]=true;}if(typeof(maxRpm[car])=="undefined"||force){maxRpm[car]=6000;saneMaxRpm[car]=true;saneMinMaxRpm[car]=true;}if(typeof(minTorque[car])=="undefined"||force){minTorque[car]=0;saneMinTorque[car]=true;}if(typeof(maxTorque[car])=="undefined"||force){maxTorque[car]=200;saneMaxTorque[car]=true;saneMinMaxTorque[car]=true;}if(typeof(torques[car])=="undefined"||force){torques[car]=new Array();for(var i=parseInt(minRpm[car]);i<=parseInt(maxRpm[car]);i=i+parseInt(stepRpm[car])){torques[car][i]=100;}}// Drivetrain
if(typeof(numGears[car])=="undefined"||force){numGears[car]=4;}if(typeof(gearRatios[car])=="undefined"||force){gearRatios[car]=new Array();gearRatios[car][1]=3.2;gearRatios[car][2]=1.9;gearRatios[car][3]=1.3;gearRatios[car][4]=1.0;gearRatios[car][5]=0.8;gearRatios[car][6]=0.7;}if(typeof(differentials[car])=="undefined"||force){differentials[car]=4.0;}if(typeof(tireWidth[car])=="undefined"||force){tireWidth[car]=215;}if(typeof(tireRatio[car])=="undefined"||force){tireRatio[car]=45;}if(typeof(tireIntDia[car])=="undefined"||force){tireIntDia[car]=16;}if(typeof(shiftingTime[car])=="undefined"||force){shiftingTime[car]=500;}if(typeof(driverWeight[car])=="undefined"||force){driverWeight[car]=160;}if(typeof(dragCoefficients[car])=="undefined"||force){dragCoefficients[car]=0.3;}if(typeof(frontalAreas[car])=="undefined"||force){frontalAreas[car]=20;}}else{getXmlData(carPreset);if(typeof(carNames[car])=="undefined"||force){carNames[car]=xmlCarName;}if(typeof(description[car])=="undefined"||force){description[car]=xmlDescription;}if(typeof(g_weight[car])=="undefined"||force){g_weight[car]=xmlWeight;}// Dynamometer
if(typeof(stepRpm[car])=="undefined"||force){stepRpm[car]=xmlStepRpm;saneStepRpm[car]=true;}if(typeof(minRpm[car])=="undefined"||force){minRpm[car]=xmlMinRpm;saneMinRpm[car]=true;}if(typeof(maxRpm[car])=="undefined"||force){maxRpm[car]=xmlMaxRpm;saneMaxRpm[car]=true;saneMinMaxRpm[car]=true;}if(typeof(minTorque[car])=="undefined"||force){minTorque[car]=xmlMinTorque;saneMinTorque[car]=true;}if(typeof(maxTorque[car])=="undefined"||force){maxTorque[car]=xmlMaxTorque;saneMaxTorque[car]=true;saneMinMaxTorque[car]=true;}if(typeof(torques[car])=="undefined"||force){for(var i=parseInt(minRpm[car]);i<=parseInt(maxRpm[car]);i=i+parseInt(stepRpm[car])){torques[car][i]=xmlTorques[i];}}// Drivetrain
if(typeof(numGears[car])=="undefined"||force){numGears[car]=xmlNumGears;}if(typeof(gearRatios[car])=="undefined"||force){gearRatios[car]=new Array();gearRatios[car][1]=3.2;gearRatios[car][2]=1.9;gearRatios[car][3]=1.3;gearRatios[car][4]=1.0;gearRatios[car][5]=0.8;gearRatios[car][6]=0.7;for(var g=1;g<=numGears[car];g++){gearRatios[car][g]=xmlGearRatios[g];}}if(typeof(differentials[car])=="undefined"||force){differentials[car]=xmlDifferentialRatio;}if(typeof(tireWidth[car])=="undefined"||force){tireWidth[car]=xmlTireWidth;}if(typeof(tireRatio[car])=="undefined"||force){tireRatio[car]=xmlTireAspect;}if(typeof(tireIntDia[car])=="undefined"||force){tireIntDia[car]=xmlTireRim;}if(force&&typeof(xmlDragCoefficient)!="undefined"){dragCoefficients[car]=xmlDragCoefficient;}if(force&&typeof(xmlFrontalArea)!="undefined"){frontalAreas[car]=xmlFrontalArea;}if(force&&typeof(xmlShiftingTime)!="undefined"){shiftingTime[car]=xmlShiftingTime;}}}function getXmlData(carPreset){var root=xmlDoc.getElementsByTagName('root');var finish=false;var nodeOfInterest;for(var car=0;car<root[0].childNodes.length&&!finish;car++){var carNode=root[0].childNodes[car];if(carNode.nodeType!=1)continue;for(var data=0;data<carNode.childNodes.length&&!finish;data++){var dataNode=carNode.childNodes[data];if(dataNode.nodeType!=1)continue;if(dataNode.nodeName=="name"){var tmp=dataNode.firstChild.nodeValue;if(tmp==carPreset){nodeOfInterest=carNode;finish=true;}}}}if(!finish){alert("Fatal error");}for(var data=0;data<nodeOfInterest.childNodes.length;data++){var dataNode=nodeOfInterest.childNodes[data];if(dataNode.nodeType!=1)continue;if(dataNode.nodeName=="name"){xmlCarName=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="description"){xmlDescription=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="weight"){xmlWeight=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="weight_units"){xmlWeightUnits=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="minRpm"){xmlMinRpm=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="maxRpm"){xmlMaxRpm=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="stepRpm"){xmlStepRpm=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="minTorque"){xmlMinTorque=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="maxTorque"){xmlMaxTorque=dataNode.firstChild.nodeValue;}if(dataNode.nodeName=="torque"){xmlTorques[dataNode.getAttribute("rpm")]=Math.round(dataNode.firstChild.nodeValue);}if(dataNode.nodeName=="numGears"){xmlNumGears=parseInt(dataNode.firstChild.nodeValue);}if(dataNode.nodeName=="gearRatio"){xmlGearRatios[parseInt(dataNode.getAttribute("gear"))]=parseFloat(dataNode.firstChild.nodeValue);}if(dataNode.nodeName=="differentialRatio"){xmlDifferentialRatio=parseFloat(dataNode.firstChild.nodeValue);}if(dataNode.nodeName=="tire"){xmlTireWidth=parseInt(dataNode.getAttribute("width"));xmlTireAspect=parseInt(dataNode.getAttribute("aspect"));xmlTireRim=parseInt(dataNode.getAttribute("rim"));}if(dataNode.nodeName=="dragCoefficient"){xmlDragCoefficient=parseFloat(dataNode.firstChild.nodeValue);}if(dataNode.nodeName=="frontalArea"){xmlFrontalArea=parseFloat(dataNode.firstChild.nodeValue);}if(dataNode.nodeName=="shiftingTime"){xmlShiftingTime=parseFloat(dataNode.firstChild.nodeValue);}}return carPreset;}
