var offset = 0;
var infoOn=false;
var priorityOn=false;
var currentID=0;
var currentTradePrice=0;
var currentTradePrice2=0;
var totalCost=0;
var chargeSet = 0;
var direction=1;
var kestrel = 0;
var upgset = 0;

function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on
	@if(@_jscript_version >=5)
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			xmlhttp=false;
		}
	}
	@else
		xmlhttp=false;
	@end @*/
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp=false;
		}
	}
	return xmlhttp;
}

function handleUpgradeResults()
{
	if(http.readyState == 4)
	{
		//alert(http.responseText);
		document.getElementById("upg_opts").innerHTML = http.responseText;
		//eval(http.responseText);
		//alert(modelHTML);
		//document.getElementById("wrapper").innerHTML = modelHTML;
		//document.getElementById("count").innerHTML = countHTML;
	}
}
function getCheckedValue(radioObj) 
{
   if(!radioObj)
      return "";
   var radioLength = radioObj.length;
   if(radioLength == undefined)
      if(radioObj.checked)
         return radioObj.value;
      else
         return "";
   for(var i = 0; i < radioLength; i++) {
      if(radioObj[i].checked) {
         return radioObj[i].value;
      }
   }
   return "";
}
function setUpgradeOptions(product)
{
	var url="getupgrades.php?product=" + product;
   currentID = product;
   kestrel = 0;
   for(x=1;x<5;x++)
   {
      document.getElementById("cat"+x).style.display="none";
   }
   if(priorityOn)
   {
      document.getElementById("shipping").style.display="block";
   }
   else
   {
      document.getElementById("shipping").style.display="none";
   }
   if(product>14 && product<30)
   {
      document.getElementById("priorityservice").style.display="block";
      kestrel = 1;
   }
   showPriorityInfo(document.raform.priorityservice1,priorityOn);

   document.getElementById("upgrade").style.display="";

//alert(url);
	http.open("GET",url, true);
	http.onreadystatechange=handleUpgradeResults;
	http.send(null);
}
function handleDelmark()
{
	if(http.readyState == 4)
	{
//		alert(http.responseText);
	}
}
function markDelete(elm,id)
{
   var mark = elm.checked;
	var url="markfordel.php?id=" + id + "&delmark=" + mark;
//alert(url);
	http.open("GET",url, true);
	http.onreadystatechange=handleDelmark;
	http.send(null);
}
function handleUpgradePriceResults()
{
	if(http.readyState == 4)
	{
		//alert(http.responseText);
		//document.getElementById("t2").innerHTML = http.responseText;
      calcValue(http.responseText,'t2');
	}
}
function getUpgradePrice(product)
{
	var url="getupgradeprice.php?oldproduct=" + currentID + "&newproduct=" + product;
//alert(url);
	http.open("GET",url, true);
	http.onreadystatechange=handleUpgradePriceResults;
	http.send(null);
}
function handleSortResponse()
{
	if(http.readyState == 4)
	{
		alert(http.responseText);
		//document.getElementById("resultstab").innerHTML = http.responseText;
      calcValue(http.responseText,'t2');
	}
}
//function sortMe(field)
//{
//	var url="getsortedresults.php?field=" + field + "&direction=" + direction;
 //  if(direction == 1) direction = 2;
 //  else direction = 1;
//alert(url);
//	http.open("GET",url, true);
//	http.onreadystatechange=handleSortResponse;
//	http.send(null);
//}
function showMoreInfo(thisVal)
{
   if(thisVal)
   {
      infoOn=true;
   }
   else
   {
      infoOn=false;
   }
}
function showPriorityInfo(robj,thisVal)
{
//alert(robj);
//alert(robj.id);
//alert(document.getElementById(robj.id).value);
   if(robj.id === undefined)
      priorityLevel = getCheckedValue(document.raform.priorityservice1);
   else
      priorityLevel = document.getElementById(robj.id).value;
//alert(kestrel);
//alert(priorityLevel);
   if(!kestrel && priorityLevel == 0) return false;
   //if(kestrel && priorityLevel == 0) thisVal = false;
   if(priorityLevel == 2)
   {
      var thisForm=document.raform;
      var thisElement=document.getElementById('showtotal');
      if(!chargeSet)
      {
         totalCost *= 100;
         totalCost += 2500;
         totalCost /= 100;
         thisForm.subtotal.value=totalCost;
         thisElement.innerHTML="$"+totalCost;
         document.getElementById('total2').innerHTML = totalCost;
         chargeSet = 1;
      }
   }
   else
   {
      var thisForm=document.raform;
      var thisElement=document.getElementById('showtotal');
      if(chargeSet)
      {
         totalCost *= 100;
         totalCost -= 2500;
         totalCost /= 100;
         thisForm.subtotal.value=totalCost;
         thisElement.innerHTML="$"+totalCost;
         document.getElementById('total2').innerHTML = totalCost;
         chargeSet = 0;
      }
   }
   if(currentID==0)
      document.getElementById("productwarning").style.display="block";
   else 
      document.getElementById("productwarning").style.display="none";
//alert(thisVal);
//alert(currentID);
   if(thisVal)
   {
      priorityOn=true;
      document.getElementById("cat1").style.display="none";
      document.getElementById("cat2").style.display="none";
      document.getElementById("cat3").style.display="none";
      document.getElementById("cat4").style.display="none";
      document.getElementById("cat6").style.display="none";
      document.getElementById("cat7").style.display="none";
      document.getElementById("testandcalibration").style.display="none";
      document.getElementById("shipping").style.display="block";
    //  document.getElementById("upgrade").style.display="";
      if(currentID>=2 && currentID<9)
      {
         document.getElementById("cat1").style.display="block";
      }

      if(currentID>=9 && currentID<11){
         document.getElementById("cat2").style.display="block";
      }

      if(currentID>=11 && currentID<15){
         document.getElementById("cat3").style.display="block";
      }
      if((currentID>=15 && currentID<30) || currentID > 42) // Kestrel
      {
         document.getElementById("cat4").style.display="block";
         document.getElementById("testandcalibration").style.display="block";
      }
      if(currentID>=31 && currentID<32)
      {
         document.getElementById("cat6").style.display="block";
      }
      if(currentID>=33 && currentID<43)
      {
         document.getElementById("cat7").style.display="block";
      }
   }
   else{
      priorityOn=false;
      document.getElementById("cat1").style.display="none";
      document.getElementById("cat2").style.display="none";
      document.getElementById("cat3").style.display="none";
      document.getElementById("cat4").style.display="none";
      document.getElementById("cat6").style.display="none";
      document.getElementById("cat7").style.display="none";
      document.getElementById("testandcalibration").style.display="none";
      document.getElementById("productwarning").style.display="none";
      document.getElementById("shipping").style.display="none";
   //   document.getElementById("upgrade").style.display="none";
   }
}
function showTrade(thisVal)
{
   if(thisVal)
   {
      document.getElementById("upg_opts").style.display="block";
      currentTradePrice=currentTradePrice2;
      totalCost += currentTradePrice;
      calcValue(currentTradePrice,'t2');
   }
   else
   {
      document.getElementById("upg_opts").style.display="none";
      currentTradePrice2=currentTradePrice;
      calcValue(0,'t2');
   }
}
var totalShipping=0;
var navColor="support";
function calcValue(iValue,thisSpan)
{
//alert('ivalue is' + iValue);
   document.getElementById(thisSpan).innerHTML=iValue;
   if(thisSpan == 't2')
      document.getElementById('trade_val').value = iValue;

   iValue=parseInt(iValue);
   var thisForm=document.raform;
   var thisElement=document.getElementById('showtotal');
   if(!iValue || iValue==""){
      totalCost-=currentTradePrice;
      totalCost+=0;
      currentTradePrice=0;
   }
   else
   {
      totalCost-=currentTradePrice;
      totalCost+=iValue;
      currentTradePrice=iValue;
   }
   if(totalCost%1!=0)totalCost=totalCost+"0";
   thisForm.subtotal.value=totalCost;

   thisElement.innerHTML="$"+totalCost;
   document.getElementById('total2').innerHTML = totalCost;
}
function calcTotal(thisControl,thisValue,bShip){
   thsValue=parseFloat(thisValue);
//alert(bShip);
//alert(thisValue)
   totalCost *=100;
   thisValue *=100;
   var thisForm=document.raform;
   var thisElement=document.getElementById('showtotal');
   thisForm.priority_filled.value="1";
   if(bShip!=1){
      if(thisControl.checked)totalCost+=thisValue;
      else totalCost-=thisValue;
   }
   else{
      totalCost-=totalShipping;
      totalShipping=thisValue;
      totalCost+=thisValue;

   }
   totalCost /= 100;
   if(totalCost%1!=0)totalCost=totalCost+"0";
   if(totalCost < 0) totalCost=0;
   thisForm.subtotal.value=totalCost;
   thisElement.innerHTML="$"+totalCost;
   document.getElementById('total2').innerHTML = totalCost;
}
function autoFill(thisVal){
   var thisForm=document.raform;
   if(thisVal){
      thisForm.ShippingAddress.value=thisForm.BillingAddress.value;
      thisForm.ShippingAddress2.value=thisForm.BillingAddress2.value;
      thisForm.ShippingCity.value=thisForm.BillingCity.value;
      thisForm.ShippingState.value=thisForm.BillingState.value;
      thisForm.ShippingZip.value=thisForm.BillingZip.value;

   }

}
function checkEmail(incMult){
   var thisForm=window.document.raform;
   if(incMult)thisForm.multiple.value="1";
   if(thisForm.email.value==""){
      alert('Please be sure to provide your email address before continuing.');
      return false;
   }
   else if(thisForm.email.value!=thisForm.verify_email.value){
      alert('Please be sure to verify your email address before continuing.');
      return false;
   }
   else return true;
}
function prefillInfo(){
   var thisForm=window.document.raform;
   thisForm.BillingName2.value=thisForm.realname.value;
   thisForm.BillingAddress2a.value=thisForm.BillingAddress.value;
   thisForm.BillingCity2.value=thisForm.BillingCity.value;
   thisForm.BillingState2.value=thisForm.BillingState.value;
   thisForm.BillingZip2.value=thisForm.BillingZip.value;
   thisForm.BillingPhone2.value=thisForm.OfficeDayPhone.value;
}

function chkReason(elem)
{
   var thisForm=document.raform;
   var thisElement=document.getElementById('showtotal');
 //  alert(elem.options[elem.selectedIndex].value);
   if(elem.options[elem.selectedIndex].value == 'Kestrel 4250 Upgrade' && !upgset)
   {  // increase price
//alert('up');
      upgset = 1;
      totalCost *= 100;
      totalCost += 8900;
      totalCost /= 100;
      if(totalCost%1!=0)totalCost=totalCost+"0";
      if(totalCost < 0) totalCost=0;
      thisForm.subtotal.value=totalCost;
      thisElement.innerHTML="$"+totalCost;
      document.getElementById('total2').innerHTML = totalCost;
   }
   if(elem.options[elem.selectedIndex].value != 'Kestrel 4250 Upgrade' && upgset)
   {  // reduce price
//alert('down');
      upgset = 0;
      totalCost *= 100;
      totalCost -= 8900;
      totalCost /= 100;
      if(totalCost%1!=0)totalCost=totalCost+"0";
      if(totalCost < 0) totalCost=0;
      thisForm.subtotal.value=totalCost;
      thisElement.innerHTML="$"+totalCost;
      document.getElementById('total2').innerHTML = totalCost;
   }
}
