/****************************************************************************
 This file is a part of the Dyno Database Online Application.                
 Copyright 2008 Alternative Consulting Solutions, LLC, All Rights Reserved.


  The SOFTWARE PRODUCT is protected by copyright laws and international
  copyright treaties, as well as other intellectual property laws and
  treaties. The SOFTWARE PRODUCT is licensed, not sold. This also applies
  to demo versions.

  GRANT OF LICENSE. This LICENSE AGREEMENT grants you the following rights:
 
  APPLICATION SOFTWARE
  You may install and use one copy of the SOFTWARE
  PRODUCT, or any prior version for the same operating system, for one
  internet domain per company.
 
  STORAGE/NETWORK USE
  You may also store a copy of the SOFTWARE PRODUCT on a storage device,
  such as a network server, used only as an installation archive of the
  SOFTWARE PRODUCT. You must acquire and dedicate a license for each
  separate internet domain on which the SOFTWARE PRODUCT will be used. 
  A license for the SOFTWARE PRODUCT may not be shared or used concurrently
  on different internet domains or by different but affiliated companies.


  THESE SOURCE FILE ARE CONSIDERED CONFIDENTIAL AND ARE
  THE PROPERTY OF ALTERNATIVE CONSULTING SOLUTIONS, LLC AND ARE NOT TO BE
  RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  CONSENT OF ALTERNATIVE CONSULTING SOLUTIONS, LLC. YOU ALSO MAY NOT REVERSE
  ENGINEER, DECOMPILE OR DISASSEMBLE THE SOFTWARE PRODUCT.

  You may contact us at: support@acs.us.com
  http://www.acs.us.com
  
  Build 2.1.20091114.10.20 
****************************************************************************/
var imgloading;
function onChangeShop(list)
{
	document.getElementById('runlist'+list).innerHTML = "";
	document.getElementById('make'+list).options.length = 0;
	document.getElementById('model'+list).options.length = 0;
	document.getElementById('year'+list).options.length = 0;
    document.getElementById('owner'+list).options.length = 0;
	getmake(list);
	
}


function onChangeMake(list)
{
	document.getElementById('runlist'+list).innerHTML = "";
	document.getElementById('model'+list).options.length = 0;
	document.getElementById('year'+list).options.length = 0;
    document.getElementById('owner'+list).options.length = 0;
	getmodel('make'+list,list);
	
}

function onChangeModel(list)
{
	document.getElementById('runlist'+list).innerHTML = "";
	document.getElementById('year'+list).options.length = 0;
    document.getElementById('owner'+list).options.length = 0;
	getyear('model'+list,'make'+list,list);
}

function onChangeYear(list)
{
	document.getElementById('runlist'+list).innerHTML = "";
    document.getElementById('owner'+list).options.length = 0;
	getowner('model'+list,'make'+list,'year'+list,list);
}


function LoadChangeMake()
{
	
	 listid=1;
	while(document.getElementById("trcar"+listid)!=undefined)
	{
		onChangeMake(listid);
		listid++
	}
}

function showgraph(fnct)
{
	imgdiv = document.getElementById('graphdiv');
	imgdiv.style.display='';
	imgdiv = document.getElementById('graphlinkdiv');
	imgdiv.style.display='';
	//if(lastsearchvalid == 0)
	//{
 	 rank = document.getElementById('rankdiv');
	 rank.style.display='none';
	//}
	img = document.getElementById('graph');
	img.src=imgloading.src;
		
	gb = gvradio('gback');	
	axis="";
	if(gvcheck('hp') == 1)
	{
		axis+='&hp=1';
	}
	if(gvcheck('torque') == 1)
	{
		axis+='&torque=1';
	}
	if(gvcheck('afr') == 1)
	{
		axis+='&afr=1';
	}
	if(gvcheck('psi') == 1)
	{
		axis+='&boost=1';
	}
	
	if(gvradio('xaxis') != "")
	{
		 axis+='&'+gvradio('xaxis')+'=1';
	}
	
	

	if(gvcheck('ShowLegend') == 1)
	{
		axis+='&sl=1';
		DisableOne('ShowLegendNotes',false);
	}
	else
	{
		axis+='&sl=0';
		DisableOne('ShowLegendNotes',true);
	}
	
	if(gvcheck('ShowLegendNotes') == 1)
	{
		axis+='&sln=1';
	}
	else
	{
		axis+='&sln=0';
	}
	
	param = "?gb="+gb;	
	param += axis;
	z=1;
	listid=1;
	while(document.getElementById("trcar"+listid)!=undefined)
	{
	i=0;
	
	while(document.getElementById("list"+listid+"-"+i)!=undefined)
	{
		if(gvcheck("list"+listid+"-"+i)==1)
		{
	 		list1val = gv('list'+listid+"-"+i);
	 		rgb1="";
	 		if(list1val == "")
	 		{
				list1val = qrunid[listid];
	 		}
	 		
			rgb1 = document.getElementById("color"+list1val).value;
	 		
	 		param += "&runid"+z+"="+list1val;
	 		param += "&rgb"+z+"="+rgb1;	
			z++;
		}
		i++;
	}
	listid++
	}
	
	url = "graph.php" + param;
	//alert(url);
	
	img = document.getElementById('graph');
	img.src=url;

	
	gl = document.getElementById("graphlink");
	gl.href = "graph.php"+param;
	pl = document.getElementById("pagelink");
	pl.href = "index.php"+param;
	
	
	var wpath = window.location.toString();
	idx = wpath.indexOf("index.php");
	if(idx>0)
	{
	  wpath = wpath.substring(0,idx);
	}
	
	idx = wpath.indexOf("#graphanchor");
	if(idx>0)
	{
	  wpath = wpath.substring(0,idx);
	}
	
	
	//gl.innerHTML = "here"; //wpath+"graph.php"+param;
	
	//pl.innerHTML = "here"; //wpath+"index.php"+param;
	
	getoverlay(param);
	//alert(url);
	if(fnct==2)
	{
	 paramstr = "HEIGHT="+(baseimgy+10)+",WIDTH="+(baseimgx+10);
	 window.open("printgraph.php?url="+encodeURIComponent(url),"",paramstr);
	}
	
}

function CheckForTwo()
{
	var hp=0;
	var torque=0;
	var afr=0;
	var boost=0;
	var hpstate=false;
	var torquestate=false;
	var afrstate=false;
	var booststate=false;
	if(gvcheck('hp') == 1)
	{
	  hp=1;
	  hpstate=true;
	}
	if(gvcheck('torque') == 1)
	{
	 torque=1;
	 	  torquestate=true;
	}
	if(gvcheck('afr') == 1)
	{
     afr=1;
	 afrstate=true;
	}
	if(gvcheck('psi') == 1)
	{
	 boost=1;
	 booststate=true;
	}
	SetDimInfo();	
	if(hp+torque+afr+boost == 3)
	{
		DisableOne('hp',!hpstate);
		DisableOne('torque',!torquestate);
		DisableOne('afr',!afrstate);
		DisableOne('psi',!booststate);
		showgraph(1);
	}
	else if(hp+torque+afr+boost == 2)
	{
		DisableOne('hp',false);
		DisableOne('torque',false);
		DisableOne('afr',false);
		DisableOne('psi',false);
		showgraph(1);
	}
	else
	{
		DisableOne('hp',false);
		DisableOne('torque',false);
		DisableOne('afr',false);
		DisableOne('psi',false);
	}

}


function SetDefaultValues()
{
 if(qgb!=0)
 {
  svradio("gback",qgb);
 }
 else
 {
  svradio("gback",0);
 }
 
 if(qsl!=0)
 {
  svcheck("ShowLegend",true);
 }
 else
 {
  svcheck("ShowLegend",false);
 }
 
  if(qsln!=0)
 {
  svcheck("ShowLegendNotes",true);
 }
 else
 {
  svcheck("ShowLegendNotes",false);
 }
 
 
 
 
 if((qhp+qtorque+qafr+qpsi)>=2)
 {
 svcheck("hp",qhp);
 svcheck("torque",qtorque);
 svcheck("afr",qafr);
 svcheck("psi",qpsi);
 }

 svradio("xaxis",qxaxis);
 
 
 for(z=1;z<qmakeid.length;z++)
 {
	 if(qmakeid[z]>0)
	 {
		if(z>1)
		{
		 AddAnotherCar();
		}
		 sv("make"+z,qmakeid[z]);
	 }
 }
 
 
}

function resetpage()
{
    id = 1;
	while(document.getElementById("trcar"+id)!= undefined)
	{
		if(id>1)
		{
	 		showdiv2("trcar"+id,false);
	 		showdiv2("trcar"+id+"div",false);
		}
	 document.getElementById('runlist'+id).innerHTML = "";
	 
	 if(document.getElementById('shop'+id) != null)
	 {
		 sv('shop'+id,0);
		 onChangeShop(id);
	 }
	 else
	 {
 	 	sv('make'+id,0);
	    onChangeMake(id);
	 }
	 id++;
	}
	showgraph(1);
	
}

function preloadImages()
{
  imgloading = new Image;
  imgloading.src = "graph.php?loading=1&gb=0";
}



function checkEnter(e, buttonname)
{ 
var characterCode ;
if(!e)
{
    characterCode = window.event.keyCode;
}
else
{
	characterCode = e.keyCode;
}

if(characterCode == 13)
{ 

 document.getElementById(buttonname).click();
 return false;
}else
{
	return true;
}


}


function GetAddCarTooltip(ev)
{
		ev=ev||window.event;
	
    offset = 15;
    obj=document.getElementById("addcartooltip");
       if (document.pageYOffset)
       {
           pagex = document.pageXOffset;
           pagey = document.pageYOffset;
       }
       else if(document.documentElement && document.documentElement.scrollTop)
       {
           pagex = document.documentElement.scrollLeft;
           pagey = document.documentElement.scrollTop;
       }
       else if(document.body)
       {
           pagex = document.body.scrollLeft;
           pagey = document.body.scrollTop;
       }
	   
	tempX = ev.clientX + pagex;
    tempY = ev.clientY + pagey;
	
	obj.style.top  = (tempY + offset) + 'px';
    obj.style.left = (tempX + offset) + 'px';
	
	obj.style.display='';
}

function GetTooltip(ev)
{
	TrackTooltip(ev);
	obj = document.getElementById("tooltip");
	
	obj.style.display = '';

}

function TrackTooltip(ev)
{
	ev=ev||window.event;
	
    offset = 15;
    obj = document.getElementById("tooltip");
    graphobj =document.getElementById("graph");
	   
	   if (document.pageYOffset)
       {
           pagex = document.pageXOffset;
           pagey = document.pageYOffset;
       }
       else if(document.documentElement && document.documentElement.scrollTop)
       {
           pagex = document.documentElement.scrollLeft;
           pagey = document.documentElement.scrollTop;
       }
       else if(document.body)
       {
           pagex = document.body.scrollLeft;
           pagey = document.body.scrollTop;
       }
	   
	tempX = ev.clientX + pagex;
    tempY = ev.clientY + pagey;
	
	obj.style.top  = (tempY + offset) + 'px';
    obj.style.left = (tempX + offset) + 'px';
	 txt="";
	// txt = ev.clientX + " , " + ev.clientY + "<BR>" + tempX + " , " + tempY;
	 //txt += "<BR>" + ev.offsetX + " , " + ev.offsetY;
	  //txt += "<BR>" + ev.layerX + " , " + ev.layerY;
	  
	 r= getRealPos(graphobj);
	  //txt += "<BR>" + r[0] + " , " + r[1];
	  
	  realx = (tempX-r[0]-(graphobj.offsetLeft));
	  realy = (tempY-r[1]-(graphobj.offsetTop));
	  //txt += "<BR>" + realx + " , " + realy ;
	  runidx=1;
	  if((OverlayData != undefined) && (OverlayData[realx] != undefined) && (OverlayData[realx][realy] != undefined) && (OverlayData[realx][realy]["run"+runidx]!=undefined))
	  {
	   //txt += "<BR>X: " + OverlayData[realx][realy]["x"] +" , " + OverlayData[realx][realy]["y"];  
	   var xaxis = gvradio("xaxis").toUpperCase();;
	  
	   if(xaxis == "TM")
	   {
		   xaxis = "Time";
	   }
	    xaxis += ":";
	   
	   txt += "<BR><b>"+xaxis+"</b><div style=\"display:inline;font-weight:bold;\">" + OverlayData[realx][realy]["run"+runidx]["rpm"] +"</div>";
	   do
	   {
		  
		 rundata = OverlayData[realx][realy]["run"+runidx];
		
	    
	    if(rundata["hp"] != undefined)
	    {
  	     txt += "<BR><b>HP:</b><div style=\"display:inline;font-weight:bold;color:#"+rundata["color"] + ";\">" + rundata["hp"] +"</div>";
	    }
	    if(rundata["torque"] != undefined)
	    {
   	    txt += "<BR><b>Torque:</b><div style=\"display:inline;font-weight:bold;color:#"+rundata["color"] + ";\">" + rundata["torque"] +"</div>";
	    }
	    if(rundata["afr"] != undefined)
	    {
  	    txt += "<BR><b>AFR:</b><div style=\"display:inline;font-weight:bold;color:#"+rundata["color"] + ";\">" + rundata["afr"] +"</div>";
	    }
	    if(rundata["psi"] != undefined)
	    {
  	    txt += "<BR><b>PSI:</b><div style=\"display:inline;font-weight:bold;color:#"+rundata["color"] + ";\">" + rundata["psi"] +"</div>";	   
	    }
		runidx++;
	   }
	   while(OverlayData[realx][realy]["run"+runidx] != undefined);
	   //alert(txt);
	  }
	 obj.innerHTML = txt;
    
}


function getRealPos(obj)
{
  x=0;
  y=0;
	
  while(obj.offsetParent)
  {
   x=x+obj.offsetParent.offsetLeft;
   y=y+obj.offsetParent.offsetTop;
    if(obj==document.getElementsByTagName('body')[0]){break}
    else{obj=obj.offsetParent;}
  }
 var r = new Array();
 r[0]=x;
 r[1]=y;
 return r;
}

function AddAnotherCar()
{
	 id=2;
	 while(document.getElementById("trcar"+id)!= undefined)
	 {
		    st = document.getElementById("trcar"+id).style;
		    stdiv = document.getElementById("trcar"+id+"div").style;
		
 			if(st.display == "none")
			{
   				st.display="";
				stdiv.display="";
				break;
			}
			id++;
	 }
}

function RemoveThisCar(id)
{
	showdiv2("trcar"+id,false);
	showdiv2("trcar"+id+"div",false);
	document.getElementById('runlist'+id).innerHTML = "";
	document.getElementById('make'+id).value = 0;
	onChangeMake(id);
	showgraph(1);
}


