function GetURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  
  if (strHref.indexOf('?') > -1)
  {
    var strQueryString = strHref.substr(strHref.indexOf('?')).toLowerCase();
    var aQueryString = strQueryString.split('&');
    for (var iParam = 0; iParam < aQueryString.length; iParam++ )
    {
      if (aQueryString[iParam].indexOf(strParamName + '=') > -1 )
      {
        var aParam = aQueryString[iParam].split('=');
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
} 

function InitForm()
{
  UpdateTotal('');
}

function SubmitFormButton() 
{
  if (CountProducts() == 0)
    {
      alert('You must select at least one product!');
      document.fnprg_order.action = "";
      return false;
    }
  else
    {
      // Set discount coupon 
      var couponvalue = 'reset:*';
      switch (CountProducts())
      {
      case 2: 
          couponvalue = 'bundle20';          
         break;       
        case 3: 
          couponvalue = 'bundle30';          
          break;  
        case 4: 
          couponvalue = 'bundle35';
          break;
        case 5: 
          couponvalue = 'bundle40';
          break;
      }      
      // Create coupon field            
      if (couponvalue != '')
      {         
         var couponfld = document.createElement("input");
         couponfld.type = "hidden";
         couponfld.name = "coupon";
         couponfld.value = couponvalue;
         document.fnprg_order.appendChild(couponfld);                
      }        
      document.fnprg_order.action = "http://sites.fastspring.com/fnprg/api/order";
      document.fnprg_order.submit(); 
      return true;
    }
}

function CountProducts()
{
  var Count = 0
  if (document.getElementById('bookcat').checked)
    {
      Count = Count + 1;
    }
  if (document.getElementById('catraxx').checked)
    {
      Count = Count + 1;
    }
  if (document.getElementById('catvids').checked)
    {
      Count = Count + 1;
    }
  if (document.getElementById('softcat').checked)
    {
      Count = Count + 1;
    }
  if (document.getElementById('stampcat').checked)
    {
      Count = Count + 1;
    }
  if (document.getElementById('bookcat_fampack').checked)
    {
      Count = Count + 1;
    }    
  if (document.getElementById('catraxx_fampack').checked)
    {
      Count = Count + 1;
    } 
  if (document.getElementById('catvids_fampack').checked)
    {
      Count = Count + 1;
    }       
  if (document.getElementById('softcat_fampack').checked)
    {
      Count = Count + 1;
    }       
  if (document.getElementById('stampcat_fampack').checked)
    {
      Count = Count + 1;
    }       
  return Count;
}

function QuantityOnChange(id)
{
  if (document.getElementById(id).checked == false)
  {  
    document.getElementById(id).checked = true;
  }
  UpdateTotal(id);
}

function QuantityOnKeyUp(id)
{
  if (document.getElementById(id).checked == false)
  {  
    document.getElementById(id).checked = true;
  }  
  UpdateTotal(id);  
}

function UpdateTotal(id)
{  	
  var Currency = 'US $';
  var ProdPrice = 0;
  var ProcPriceDisc = 0;
  var SubTotal = 0;
  var SubTotalDiscount = 0;
  var SubTotalNoDiscount = 0;
  var Total = 0;
  var DiscountPercent = 0;
  var ProdCount = 0; 
  var nbsp = String.fromCharCode(160);
  var Quantity = 1;
  var Discount = 0;
  
  // Not possible to select single license and family pack   
  if (id == 'bookcat' && document.getElementById('bookcat_fampack').checked == true)
  { 
    document.getElementById('bookcat_fampack').checked = false;	    
  }
  else if (id == 'bookcat_fampack' && document.getElementById('bookcat').checked == true)
  {    
    document.getElementById('bookcat').checked = false;	    
  }
  else if (id == 'catraxx' && document.getElementById('catraxx_fampack').checked == true)
  {    
    document.getElementById('catraxx_fampack').checked = false;	    
  }
  else if (id == 'catraxx_fampack' && document.getElementById('catraxx').checked == true)
  {    
    document.getElementById('catraxx').checked = false;	    
  }
  else if (id == 'catvids' && document.getElementById('catvids_fampack').checked == true)
  {    
    document.getElementById('catvids_fampack').checked = false;	    
  }
  else if (id == 'catvids_fampack' && document.getElementById('catvids').checked == true)
  {    
    document.getElementById('catvids').checked = false;	    
  }  
  else if (id == 'softcat' && document.getElementById('softcat_fampack').checked == true)
  {    
    document.getElementById('softcat_fampack').checked = false;	    
  }
  else if (id == 'softcat_fampack' && document.getElementById('softcat').checked == true)
  {    
    document.getElementById('softcat').checked = false;	    
  }
  else if (id == 'stampcat' && document.getElementById('stampcat_fampack').checked == true)
  {    
    document.getElementById('stampcat_fampack').checked = false;	    
  }
  else if (id == 'stampcat_fampack' && document.getElementById('stampcat').checked == true)
  {    
    document.getElementById('stampcat').checked = false;	    
  }
  
  ProdCount = CountProducts();
  
  switch (ProdCount) 
  {
    case 1: 
      DiscountPercent = 0;
      break;
    case 2: 
      DiscountPercent = 20;
      break;       
    case 3: 
      DiscountPercent = 30;
      break;  
    case 4: 
      DiscountPercent = 35; 
      break;
	  case 5: 
      DiscountPercent = 40; 
      break;
  }
  
  if (document.getElementById('bookcat').checked)
    {
      Quantity = GetQuantity('bookcat_quantity');      
      SubTotal = SubTotal + GetTotalPrice('bookcat', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('bookcat', Quantity, true);
    }
  if (document.getElementById('catraxx').checked)
    {
      Quantity = GetQuantity('catraxx_quantity');      
      SubTotal = SubTotal + GetTotalPrice('catraxx', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('catraxx', Quantity, true);
    }
  if (document.getElementById('catvids').checked)
    {
      Quantity = GetQuantity('catvids_quantity');      
      SubTotal = SubTotal + GetTotalPrice('catvids', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('catvids', Quantity, true);
    }
  if (document.getElementById('softcat').checked)
    {
      Quantity = GetQuantity('softcat_quantity');      
      SubTotal = SubTotal + GetTotalPrice('softcat', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('softcat', Quantity, true);
    }
  if (document.getElementById('stampcat').checked)
    {
      Quantity = GetQuantity('stampcat_quantity');      
      SubTotal = SubTotal + GetTotalPrice('stampcat', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('stampcat', Quantity, true);
    }
  if (document.getElementById('bookcat_fampack').checked)
    {
      Quantity = 1;      
      SubTotal = SubTotal + GetTotalPrice('bookcat_fampack', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('bookcat_fampack', Quantity, true);
    }    
  if (document.getElementById('catraxx_fampack').checked)
    {
      Quantity = 1;      
      SubTotal = SubTotal + GetTotalPrice('catraxx_fampack', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('catraxx_fampack', Quantity, true);
    }    
  if (document.getElementById('catvids_fampack').checked)
    {
      Quantity = 1;      
      SubTotal = SubTotal + GetTotalPrice('catvids_fampack', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('catvids_fampack', Quantity, true);
    }    
  if (document.getElementById('softcat_fampack').checked)
    {
      Quantity = 1;      
      SubTotal = SubTotal + GetTotalPrice('softcat_fampack', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('softcat_fampack', Quantity, true);
    }    
  if (document.getElementById('stampcat_fampack').checked)
    {
      Quantity = 1;      
      SubTotal = SubTotal + GetTotalPrice('stampcat_fampack', Quantity, false);
      SubTotalDiscount = SubTotalDiscount + GetTotalPrice('stampcat_fampack', Quantity, true);
    }    

  Total = ((SubTotalDiscount * ((100 - DiscountPercent) / 100)) + SubTotalNoDiscount);   
  Discount = SubTotal - Total;
 
  {
    // BookCAT Family Pack
    document.getElementById('bookcat_fampack_price2').style.fontWeight = 400;
    if (document.getElementById('bookcat_fampack').checked)
      {
        document.getElementById('bookcat_fampack_price2').style.fontWeight = 700;
      }
    // CATraxx Family Pack
    document.getElementById('catraxx_fampack_price2').style.fontWeight = 400;
    if (document.getElementById('catraxx_fampack').checked)
      {
        document.getElementById('catraxx_fampack_price2').style.fontWeight = 700;
      }
    // CATVids Family Pack
    document.getElementById('catvids_fampack_price2').style.fontWeight = 400;
    if (document.getElementById('catvids_fampack').checked)
      {
        document.getElementById('catvids_fampack_price2').style.fontWeight = 700;
      }      
    // SoftCAT Family Pack
    document.getElementById('softcat_fampack_price2').style.fontWeight = 400;
    if (document.getElementById('softcat_fampack').checked)
      {
        document.getElementById('softcat_fampack_price2').style.fontWeight = 700;
      }
    // StampCAT Family Pack
    document.getElementById('stampcat_fampack_price2').style.fontWeight = 400;
    if (document.getElementById('stampcat_fampack').checked)
      {
        document.getElementById('stampcat_fampack_price2').style.fontWeight = 700;
      }                 
    // BookCAT
    ProdPrice = GetPrice('bookcat', GetQuantity('bookcat_quantity'), true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('bookcat_price').style.fontWeight = 400;
    document.getElementById('bookcat_price2').style.fontWeight = 400;
    document.getElementById('bookcat_price2').firstChild.data = '';
    if (document.getElementById('bookcat').checked)
      {
        if ((ProdCount > 1) || (GetQuantity('bookcat_quantity') > 1))
          {
            document.getElementById('bookcat_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('bookcat_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('bookcat_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('bookcat_price2').firstChild.data == '')
      {
        document.getElementById('bookcat_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('bookcat_price').style.textDecoration = 'line-through';                  
      }    
    // BookCAT - Fam Pack
    ProdPrice = GetPrice('bookcat_fampack', 1, true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('bookcat_fampack_price').style.fontWeight = 400;
    document.getElementById('bookcat_fampack_price2').style.fontWeight = 400;
    document.getElementById('bookcat_fampack_price2').firstChild.data = '';
    if (document.getElementById('bookcat_fampack').checked)
      {
        if (ProdCount > 1)
          {
            document.getElementById('bookcat_fampack_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('bookcat_fampack_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('bookcat_fampack_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('bookcat_fampack_price2').firstChild.data == '')
      {
        document.getElementById('bookcat_fampack_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('bookcat_fampack_price').style.textDecoration = 'line-through';                  
      }    
    // CATraxx
    ProdPrice = GetPrice('catraxx', GetQuantity('catraxx_quantity'), true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('catraxx_price').style.fontWeight = 400;
    document.getElementById('catraxx_price2').style.fontWeight = 400;
    document.getElementById('catraxx_price2').firstChild.data = '';
    if (document.getElementById('catraxx').checked)
      {
        if ((ProdCount > 1) || (GetQuantity('catraxx_quantity') > 1))
          {
            document.getElementById('catraxx_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('catraxx_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('catraxx_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('catraxx_price2').firstChild.data == '')
      {
        document.getElementById('catraxx_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('catraxx_price').style.textDecoration = 'line-through';          
      }
    // CATraxx - Fam Pack
    ProdPrice = GetPrice('catraxx_fampack', 1, true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('catraxx_fampack_price').style.fontWeight = 400;
    document.getElementById('catraxx_fampack_price2').style.fontWeight = 400;
    document.getElementById('catraxx_fampack_price2').firstChild.data = '';
    if (document.getElementById('catraxx_fampack').checked)
      {
        if (ProdCount > 1)
          {
            document.getElementById('catraxx_fampack_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('catraxx_fampack_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('catraxx_fampack_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('catraxx_fampack_price2').firstChild.data == '')
      {
        document.getElementById('catraxx_fampack_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('catraxx_fampack_price').style.textDecoration = 'line-through';                  
      }    
    // CATVids
    ProdPrice = GetPrice('catvids', GetQuantity('catvids_quantity'), true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('catvids_price').style.fontWeight = 400;
    document.getElementById('catvids_price2').style.fontWeight = 400;
    document.getElementById('catvids_price2').firstChild.data = '';    
    if (document.getElementById('catvids').checked)
      {
        if ((ProdCount > 1) || (GetQuantity('catvids_quantity') > 1))
          {
            document.getElementById('catvids_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('catvids_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('catvids_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('catvids_price2').firstChild.data == '')
      {
        document.getElementById('catvids_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('catvids_price').style.textDecoration = 'line-through';          
      }
    // CATVids - Fam Pack
    ProdPrice = GetPrice('catvids_fampack', 1, true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('catvids_fampack_price').style.fontWeight = 400;
    document.getElementById('catvids_fampack_price2').style.fontWeight = 400;
    document.getElementById('catvids_fampack_price2').firstChild.data = '';
    if (document.getElementById('catvids_fampack').checked)
      {
        if (ProdCount > 1)
          {
            document.getElementById('catvids_fampack_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('catvids_fampack_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('catvids_fampack_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('catvids_fampack_price2').firstChild.data == '')
      {
        document.getElementById('catvids_fampack_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('catvids_fampack_price').style.textDecoration = 'line-through';                  
      }    
    // SoftCAT
    ProdPrice = GetPrice('softcat', GetQuantity('softcat_quantity'), true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('softcat_price').style.fontWeight = 400;
    document.getElementById('softcat_price2').style.fontWeight = 400;
    document.getElementById('softcat_price2').firstChild.data = '';    
    if (document.getElementById('softcat').checked)
      {
        if ((ProdCount > 1) || (GetQuantity('softcat_quantity') > 1))
          {
            document.getElementById('softcat_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('softcat_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('softcat_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('softcat_price2').firstChild.data == '')
      {
        document.getElementById('softcat_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('softcat_price').style.textDecoration = 'line-through';          
      }
    // SoftCAT - Fam Pack
    ProdPrice = GetPrice('softcat_fampack', 1, true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('softcat_fampack_price').style.fontWeight = 400;
    document.getElementById('softcat_fampack_price2').style.fontWeight = 400;
    document.getElementById('softcat_fampack_price2').firstChild.data = '';
    if (document.getElementById('softcat_fampack').checked)
      {
        if (ProdCount > 1)
          {
            document.getElementById('softcat_fampack_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('softcat_fampack_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('softcat_fampack_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('softcat_fampack_price2').firstChild.data == '')
      {
        document.getElementById('softcat_fampack_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('softcat_fampack_price').style.textDecoration = 'line-through';                  
      }    
    // StampCAT
    ProdPrice = GetPrice('stampcat', GetQuantity('stampcat_quantity'), true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('stampcat_price').style.fontWeight = 400;
    document.getElementById('stampcat_price2').style.fontWeight = 400;
    document.getElementById('stampcat_price2').firstChild.data = '';       
    if (document.getElementById('stampcat').checked)
      {
        if ((ProdCount > 1) || (GetQuantity('stampcat_quantity') > 1))
          {
            document.getElementById('stampcat_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('stampcat_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('stampcat_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('stampcat_price2').firstChild.data == '')
      {
        document.getElementById('stampcat_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('stampcat_price').style.textDecoration = 'line-through';          
      }
    // StampCAT - Fam Pack
    ProdPrice = GetPrice('stampcat_fampack', 1, true);
    ProdPriceDisc = ProdPrice * ((100 - DiscountPercent) / 100) + 0.001;
    document.getElementById('stampcat_fampack_price').style.fontWeight = 400;
    document.getElementById('stampcat_fampack_price2').style.fontWeight = 400;
    document.getElementById('stampcat_fampack_price2').firstChild.data = '';
    if (document.getElementById('stampcat_fampack').checked)
      {
        if (ProdCount > 1)
          {
            document.getElementById('stampcat_fampack_price2').firstChild.data = Currency + ProdPriceDisc.toFixed(2);
            document.getElementById('stampcat_fampack_price2').style.fontWeight = 700;
 	        }
 	      else
 	        { 	          
 	          document.getElementById('stampcat_fampack_price').style.fontWeight = 700;
 	        }    	        
      }
    if (document.getElementById('stampcat_fampack_price2').firstChild.data == '')
      {
        document.getElementById('stampcat_fampack_price').style.textDecoration = '';
      }
    else
      {
        document.getElementById('stampcat_fampack_price').style.textDecoration = 'line-through';                  
      }        
    // Total
    document.getElementById('subtotal').firstChild.data = Currency + SubTotal.toFixed(2);
    document.getElementById('discount').firstChild.data = Currency + Discount.toFixed(2);
    if (SubTotal == 0)
    {
      document.getElementById('discountpercent').firstChild.data = '0%';
    }
    else
    {
      document.getElementById('discountpercent').firstChild.data = ((Discount / SubTotal) * 100).toFixed(0) + '%';
    }    
    document.getElementById('total').firstChild.data = Currency + Total.toFixed(2);
  }
} 

function GetQuantity(id)
{
  if (isNaN(document.getElementById(id).value))
  {
    return 1;
  }
  else if (document.getElementById(id).value == '')
  {
    return 1;
  }
  else
  {
    return parseInt(document.getElementById(id).value);		         
  }
}

function GetPrice(id, quantity, volumediscount)
{
  if ((id == 'bookcat') || (id == 'catraxx') || (id == 'catvids'))
  {    
    if (quantity < 2 || volumediscount == false)
    {
      price = 39.95;
    }
    else if (quantity < 5)    
    {
      price = 34.95;
    }
    else if (quantity < 10)
    {
      price = 29.95;
    }
    else if (quantity < 20)
    {
      price = 24.95;
    }
    else 
    {
      price = 19.95;
    }
  }
  else if ((id == 'bookcat_fampack') || (id == 'catraxx_fampack') || (id == 'catvids_fampack'))
  {    
    price = 59.95;
  }
  else if ((id == 'softcat_fampack') || (id == 'stampcat_fampack'))
  {    
    price = 49.95;
  }  
  else
  {
    if (quantity < 2 || volumediscount == false)
    {
      price = 29.95;
    }
    else if (quantity < 5)    
    {
      price = 26.95;
    }
    else if (quantity < 10)
    {
      price = 22.95;
    }
    else if (quantity < 20)
    {
      price = 18.95;
    }
    else 
    {
      price = 14.95;
    }
  }
  return price;
}

function GetTotalPrice(id, quantity, volumediscount)
{
  return GetPrice(id, quantity, volumediscount) * quantity;  
}

