var offset = 0;
var infoOn=false;
var priorityOn=false;
var currentID=0;
var currentCAT=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;
		if($("#cat2repairreq").length>0) {
			$("#cat2repairreq").remove();
		}
		
		
		
		if(currentCAT ==2) {
			$('#cat2').append('<input id="cat2repairreq" type="hidden" name="r_cox_repair_replace" value="Repair or replace is required" />');
			
		}
		
		//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(productstr)
{
	totalCost = 0;
	document.getElementById('showtotal').innerHTML= '$0';
	
	//passing in "productId-categoryId"
	var prodarr = productstr.split('-');
	var product = prodarr[0];
	var cat = prodarr[1];
//alert('PROD'+product);
//alert('CAT'+cat);
	var url="getupgrades.php?product=" + product;
	currentID = product;
	currentCAT = cat;
	kestrel = 0;
	//hide all the categories
	if(cat==2){
      document.getElementById("preauthstatement").style.display="none"; }
    else{
          document.getElementById("preauthstatement").style.display="block"; 
	}
    
	for(x=1;x<5;x++)
	{
		document.getElementById("cat"+x).style.display="none";
	}
	//underneath the hidden categories, check to see if a priority or rush has been selected 
	if(priorityOn)
	{
		document.getElementById("shipping").style.display="block";
	}
	else
	{
		document.getElementById("shipping").style.display="none";
	}
	//the kestrel ids
	if( (product>14 && product<30) || (product ==43 ) || (product ==44 ) || (product==47))
	{
		//paragreaph blurb stating some legalese
		// 2/23/2011 document.getElementById("priorityservice").style.display="block";
		kestrel = 1; 
	}
//alert(document.raform.priority);
	//document.raform.priority are the radiobuttons for standard, priority, rush
	showPriorityInfo(document.raform.priority,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 markDelete2(mark,id)
{
	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;
   }
}
/*
 * This function gets called directly when you click a priority service radio button
 * It is called indirectly through showUpgradeOptions when you select a product from the dropdown
 *
 * robj will be blank if you've selected a product and no priority has been selected yet
 * thisVal is true/false based on whether or not ANY priority service radio button is checked (even "standard")
 */
function showPriorityInfo(robj,thisVal)
{
//alert(robj);
//alert(robj.id);
//alert(document.getElementById(robj.id).value);
//alert("ThisVal should be whether priority is set or not and = "+thisVal);
 
	if(document.getElementById('productid').value == "") {
		alert("Please select a product above");
	//	robj.checked = false;
		return false;		
	}
	
	/* 
	* The if condition is true if you select a product and there is not already a priority set (normal workflow)
	* The else condition is true if you already have selected a product and then change the priority
	* PriorityLevel is 0:Standard, 1:Priority, 2:Rush
	*/
	/* 2/23/2011
   if(robj.id === undefined) {
      priorityLevel = getCheckedValue(document.raform.priorityservice1);
	  //alert("priority undefined; You probably selected a product \n\r it is now set by priorityservice1 field on the form " + priorityLevel);
	  }
   else{    
	  priorityLevel = document.getElementById(robj.id).value;
	  //alert("priority is now getElementById("+robj.id+").value which is " + priorityLevel);
	  } */
	 thisVal = true;
	  priorityLevel = 1;
	  priorityOn = true;
//alert(kestrel);
//alert(priorityLevel);
	//If standard has been selected or no priority at all hide all categories, preauth and calibration sections
/* 2/23/2011 
 if(priorityLevel == 0 || priorityLevel=="" || typeof(priorityLevel)=="undefined" || priorityLevel == null) {
      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";	  
	}
	*/
	//if non-kestrel and priority is standard
	if(!kestrel && priorityLevel == 0) {
		priorityOn = false;
		return false; 
	}
	//if kestrel is selected but standard is, you don't want to show shipping, but should show testing	
   if(kestrel && priorityLevel == 0) thisVal = false;
   
   //if priority is Rush
   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;
      }
   }
   //if priority is 1 (Priority) or 0 (which should only happen for kestrels at this point)
   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;
      }
   }
   //currentID is product id and is sent in via the product dropdown (via setUpgradeOptions function)
   if(currentID==0)
      document.getElementById("productwarning").style.display="block";
   else 
      document.getElementById("productwarning").style.display="none";
  
    
//alert(thisVal);
//alert(currentID);
	
	if(kestrel && priorityLevel !=0) {
		forceShippingBlockOn = "block";
	}
	if(kestrel) forceCalibration = "block";
	//thisVal is true/false based on whether or not ANY priority service radio button is checked (even "standard")
	// it is manually set to false if a kestrel and standard priority are selected
    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";
	//new jake
	  if(typeof(forceShippingBlockOn) !='' && typeof(forceShippingBlockOn)!='undefined') {
      document.getElementById("shipping").style.display=forceShippingBlockOn;	  
	  }else {
	  document.getElementById("shipping").style.display="block";
	  }
  //  document.getElementById("upgrade").style.display="";
  if(!kestrel) {
	document.getElementById("testandcalibration").style.display="none";
  }
      if(currentCAT == 1)
      {
         document.getElementById("cat1").style.display="block";
      }

      if(currentCAT == 2) //coxbox
      {
		var thisElement=document.getElementById('cox_repair');
		thisElement.checked= false;
		var thisElement=document.getElementById('cox_replace');
		thisElement.checked= false;

         document.getElementById("cat2").style.display="block";
      }
      if(currentCAT == 3)
      {
         document.getElementById("cat3").style.display="block";
      }
      if(currentCAT == 4 || currentCAT == 5) // Kestrel
      {
         document.getElementById("cat4").style.display="block";
         document.getElementById("testandcalibration").style.display="block";
      }
      if(currentCAT == 6)
      {
         document.getElementById("cat6").style.display="block";
      }
      if(currentCAT == 7)
      {
         document.getElementById("cat7").style.display="block";
      }
	   
   }
   else{
	//alert('thisval is '+thisVal+' therefore everything is being disabled');
      priorityOn=false;
      document.getElementById("cat1").style.display="none";
      document.getElementById("cat2").style.display="none";
      document.getElementById("cat3").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";
	  if( (kestrel) ) {
	  //&& (forceCalibration !==undefined || forceShippingBlockOn!== undefined
		document.getElementById("cat4").style.display="block";
		 document.getElementById("testandcalibration").style.display="block";
	  }
	  else {
		document.getElementById("cat4").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)
{
	//separate situation for coxbox category; only two options exist
	if(currentCAT == 2) {
		totalCost=0;
		
	}
	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.saddr1.value=thisForm.baddr1.value;
		thisForm.saddr2.value=thisForm.baddr2.value;
		thisForm.scity.value=thisForm.bcity.value;
		thisForm.sstate.value=thisForm.bstate.value;
		thisForm.szip.value=thisForm.bzip.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.rname.value;
   thisForm.BillingAddress2a.value=thisForm.saddr1.value;
   thisForm.BillingCity2.value=thisForm.scity.value;
   thisForm.BillingState2.value=thisForm.sstate.value;
   thisForm.BillingZip2.value=thisForm.szip.value;
   thisForm.BillingPhone2.value=thisForm.workphone.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;
	}
}

