var datafaxwin;
var showPointsCost = true;
window.onscroll = refreshPointsBox;

function refreshPointsBox()
{
   //document.getElementById('pointsbox').style.top = document.body.scrollTop+250;
   document.getElementById('pointsbox').style.top = document.documentElement.scrollTop+250;
}


function init()
{
  data = document.forms['designform'];
  data.reset();
  applyWeapons();
}


function applyCrew()
{
  var index = data.crew.selectedIndex;
  myCrew = Math.pow(2,index);
  applyType();
  applyWeapons();
  applyFields();
  applyCC();
  applyTauOptions(0);
  applyNecronOptions(0);

  //DCC 13/2/09 - turn on/off tau and necron options
  if (((TAU+BS4TAU) & myCrew) != 0) {
    document.getElementById('tauoptions').style.display = "";
  } else {
    document.getElementById('tauoptions').style.display = "none";
  }
  
  if ((NECRON & myCrew) != 0) {
    document.getElementById('necronoptions').style.display = "";
    document.getElementById('necronponderous').style.display = "";
    document.getElementById('soupedup').style.display = "none";
	data.engines.checked = false;
  } else {
    document.getElementById('necronoptions').style.display = "none";
    document.getElementById('necronponderous').style.display = "none";
    document.getElementById('soupedup').style.display = "";
	data.ponderous.checked = false;
  }  


}


function applyType()
{
  myType = data.type.selectedIndex;


  if ((NECRON & myCrew) !=0)
  {
	if (myType == LIGHTVEHICLE)
	{
	  window.alert("Necrons may not have Light Vehicles");
	  myType = TANK;
      data.type.selectedIndex = myType;
	} 
	if (myType == FLYER)
	{
	  window.alert("Necrons may not have Flyers");
	  myType = TANK;
      data.type.selectedIndex = myType;
	} 
  }

  myMaxArmour = maxArmour[mySize][myType];
  myMaxAV = maxAV[mySize][myType];
  applySpeed();
  applyArmour();
  wipeWeapons();
  applyTunneller();
  applyCarriage();
  applyOpentopped();
  applyCC();
  applyTauOptions(0);
  applyNecronOptions(0);
}


function applySize()
{
  mySize = data.size.selectedIndex;

  if ((NECRON & myCrew) !=0)
  {
	if (mySize == SMALL)
	{
	  window.alert("Necrons may not have Small vehicles");
	  mySize = NORMAL;
      data.size.selectedIndex = mySize;
	} 
  }

  myMaxArmour = maxArmour[mySize][myType];
  myMaxAV = maxAV[mySize][myType];
  applySpeed();
  applyArmour();

  applyStructure();
  wipeWeapons();
  applyCC();

  applyTauOptions(0);
  applyNecronOptions(0);

}


function applySpeed()
{

  //check for tau upgrades on non-skimmer
  if (data.skimmer.checked == false && (((TAU+BS4TAU) & myCrew) != 0) && data.taulandinggear.checked)
  {
      window.alert("Only Tau skimmers may have the Landing Gear option");
      data.taulandinggear.checked = false;
  }

  //necron war machine tanks must be skimmers
  if ((NECRON & myCrew) != 0) 
  {
	if (myType == TANK && mySize == WARMACHINE && data.skimmer.checked == false) 
	{
      window.alert("Necron War Machine Tanks must be skimmers");
      data.skimmer.checked = true;
	}

	if (data.skimmer.checked == false && data.ponderous.checked == true)
	{
	    window.alert("Only skimmers can be Ponderous");
		data.skimmer.checked = true;
	}

    if (data.engines.checked == true)
	{
	  window.alert("Necron vehicles may not have souped-up engines");
	  data.engines.checked = false;
	}

  } else {
	if (data.ponderous.checked == true)
	{
	    window.alert("Only Necron skimmers can be Ponderous");
		data.ponderous.checked = false;
	}
  }
	

  mySpeed = speeds[mySize][myType];

  if(data.engines.checked)
  {
	
    switch(myType)
    {
      case WALKER:
        mySpeed = AGILE;  
        break;
      case TANK:
      case LIGHTVEHICLE:
        if(mySize == WARMACHINE)  mySpeed = AGILE;
        else if(myTotalArmour > 50)
        {
          window.alert("Normal vehicles with more than 50 points of armour may not be given souped-up engines");
          data.engines.checked = false;
        }
        else
        {
          mySpeed = (myTotalArmour < 43) ? FAST : AGILE;
          if(data.skimmer.checked && myTotalArmour < 47) mySpeed = FAST;
        }
        break;
      default:
        window.alert("This type of vehicle may not be given souped-up engines");
        data.engines.checked = false;
     }

    if (data.engines.checked && mySpeed == FAST)
    {
        if ( (((TAU+BS4TAU) & myCrew) != 0) && mySize != SMALL )
        {
          window.alert("Tau vehicles may not be Fast unless they are Small");
          data.engines.checked = false;
          mySpeed = speeds[mySize][myType];
        }
    }

  }
  
  if (data.ponderous.checked)
  {
    if (myType == IMMOBILE) 
	{
      window.alert("Immobile vehicles may not be Ponderous");
      data.ponderous.checked = false;
	} else {
      if (myTotalArmour < 56) 
	  {
	      window.alert("Only vehicles with total armour of 56 can be Ponderous");
	      data.ponderous.checked = false;
      } else {
	    mySpeed = LUMBERING;
      }
	}
  }

  if (data.ponderous.checked && mySpeed == LUMBERING)
  {
	  data.speed.value = "Ponderous";
  } else {
	  data.speed.value = speedText[mySpeed];
  }


  refreshPoints();
}


function applyStructure()
{
  if(mySize == WARMACHINE && data.structure.selectedIndex < 1)
  {
    window.alert("War machines must be given 2 or more structure points");
    data.structure.selectedIndex = 1;
  }
  else if(mySize != WARMACHINE && data.structure.selectedIndex != 0)
  {
    window.alert("Only war machines may be given more than one structure point");
    data.structure.selectedIndex = 0; 
  }
  applyTransport();
  applyFields();
  applyTauOptions(0);
  applyNecronOptions(0);
}


function applyArmour()
{
  applyAV(data.frontarmour);
  applyAV(data.sidearmour);
  applyAV(data.reararmour);
}


function applyAV(f)
{
  if(f.selectedIndex+9 > myMaxAV)
  {
     window.alert("This type/size of vehicle may not have an AV above " + myMaxAV);
     f.selectedIndex = 0;
  }

  var ta = data.frontarmour.selectedIndex + 9;
  ta += data.sidearmour.selectedIndex + 9;
  ta += data.sidearmour.selectedIndex + 9;
  ta += data.reararmour.selectedIndex + 9;

  if(ta > myMaxArmour)
  {
    window.alert("The armour on this type/size of vehicle may not exceed " + myMaxArmour);
    f.selectedIndex = 0;
    ta = data.frontarmour.selectedIndex + 9;
    ta += data.sidearmour.selectedIndex + 9;
    ta += data.sidearmour.selectedIndex + 9;
    ta += data.reararmour.selectedIndex + 9;
  }

  myTotalArmour = ta;
  applySpeed();
}


function applyTransport()
{
  var capacity = (mySize == SMALL) ? 6 : 11;
  capacity += data.structure.selectedIndex * 5;
  if(parseInt(data.transport.value) > capacity)
  {
    window.alert("The transport capacity for this vehicle is " + capacity);
    data.transport.value = capacity;
  }
    refreshPoints();
}


function wipeSelectBox(sb)
{
  l = sb.length;
  for(var i=0 ; i<l ; i++)
  {
    sb.options[0] = null; 
  } 
}


function applyCC()
{
 if(data.closecombat.checked) 
 {

  if((((TAU+BS4TAU) & myCrew) != 0))
  {  
    window.alert("Tau may not select close combat weapons");
    data.ccwpntype.selectedIndex = 0;
    data.closecombat.checked = 0;
  }

  if(((NECRON & myCrew) != 0) && mySize == WARMACHINE && myType == TANK)
  {  
    window.alert("Necrons may not have a War Machine Tank with close combat weapons");
    data.ccwpntype.selectedIndex = 0;
    data.closecombat.checked = 0;
  }

  if(data.ccwpntype.selectedIndex == 3 && mySize != WARMACHINE)
  {  
    window.alert("Only war machines may select war machine close combat weapons");
    data.ccwpntype.selectedIndex = 0;
  }

  if(data.ccwpntype.selectedIndex != 3 && mySize == WARMACHINE && ((NECRON & myCrew) == 0))
  {  
    window.alert("War machines may not select close combat weapons other than of war machine type");
    data.ccwpntype.selectedIndex = 3;
  }
 }
   refreshPoints();
}


function wipeWeapons()
{
 l = data.wpnlist.length;
 for(var i=1 ; i<l ; i++)
 {
    data.wpnlist.options[1] = null; 
 } 
  data.wpnblast.checked = false;
  data.wpncoax.checked = false;
  data.wpngatling.checked = false;
  data.wpnlong.checked = false;
  data.wpnshort.checked = false;
  data.wpnmega.checked = false;
  data.wpntitan.checked = false;
  data.wpntwin.checked = false;
  data.wpnaa.checked = false;
  data.wpnbattery.selectedIndex = 0;
  data.wpnrof.selectedIndex = 0;
  
  refreshPoints();
}


function applyBlast()
{
  checkWpnUpgrade(BLAST, "Blast", data.wpnblast);

  if(myType != IMMOBILE && mySize != WARMACHINE && data.wpnblast.checked)
  {
     window.alert("The Blast upgrade may only be used on weapons mounted on war machines or immobile vehicles.");
     data.wpnblast.checked = false;
  }
}


function applyGatling()
{
  checkWpnUpgrade(GATLING, "Gatling", data.wpngatling);
}


function applyBattery()
{
  wpnType = weapons[data.wpn.options[data.wpn.selectedIndex].value][1];
  if(!upgradesAllowed[wpnType][GUN_BATTERY] && data.wpnbattery.selectedIndex != 0)
  {
    window.alert("This weapon may not be upgraded to a Gun Battery.");
    data.wpnbattery.selectedIndex = 0;
  }
}


function applyLongbarrel()
{
  checkWpnUpgrade(LONG_BARREL, "Long-barrel", data.wpnlong);
  if(data.wpnshort.checked && data.wpnlong.checked)
    data.wpnshort.checked = false;
}


function applyMega()
{
  checkWpnUpgrade(MEGA, "Mega", data.wpnmega); 
  if(myType != IMMOBILE && mySize != WARMACHINE && data.wpnmega.checked)
  {
     window.alert("The Mega upgrade may only be used on weapons mounted on war machines or immobile vehicles.");
     data.wpnmega.checked = false;
  }
}


function applyShortbarrel()
{
  checkWpnUpgrade(SHORT_BARREL, "Short-barrel", data.wpnshort);
  if(data.wpnshort.checked && data.wpnlong.checked)
    data.wpnlong.checked = false;
}


function applyROF()
{
  wpnType = weapons[data.wpn.options[data.wpn.selectedIndex].value][1];
  if(!upgradesAllowed[wpnType][SLOWER_ROF] && data.wpnrof.selectedIndex != 0)
  {
    window.alert("This weapon may not be given the slower ROF upgrade.");
    data.wpnrof.selectedIndex = 0;
  }
 
  wpnShots = weapons[data.wpn.options[data.wpn.selectedIndex].value][2];
  wpnShots += parseInt(data.wpnrof.options[data.wpnrof.selectedIndex].text)
  if(wpnShots < 1)
  {
    window.alert("The number of shots may not be reduced to fewer than 1.");
    data.wpnrof.selectedIndex = 0;
  }
}


function applyTitan()
{
  checkWpnUpgrade(TITAN_KILLER, "Titan-killer", data.wpntitan); 

  if(!data.wpnmega.checked && data.wpntitan.checked)
  {
     window.alert("The Titan-killer upgrade may only be applied to Mega weapons.");
     data.wpntitan.checked = false;
  }
}


function applyTwinlinked()
{
  checkWpnUpgrade(TWIN_LINKED, "Twin-linked", data.wpntwin);
}


function checkWpnUpgrade(upgrade,name,cb)
{
  if(cb.checked)
  {
    wpnType = weapons[data.wpn.options[data.wpn.selectedIndex].value][1];
    if(!upgradesAllowed[wpnType][upgrade])
    {
      window.alert("This weapon may not be given the " + name + " upgrade.");
      cb.checked = false;
    }
  }
}


function applyWpnUpgrades()
{
  applyBlast();
  applyGatling();
  applyBattery();
  applyLongbarrel();
  applyMega();
  applyShortbarrel();
  applyROF();
  applyTitan();
  applyTwinlinked();
}


function applyWeapons()
{
 var myBS = myCrew;

 if(data.wpntargeters.checked)
 {
   if(myCrew == HUMAN) myBS = ADEPTUS;
    else if(myCrew == ELDAR) myBS = BS4ELDAR;
    else if(myCrew == TAU) myBS = BS4TAU;
     else
     {
       window.alert("Only vehicles with BS3 may be given targeters");
       data.wpntargeters.checked = false;
     }
 }

 wipeSelectBox(data.wpn);
 wipeSelectBox(data.wpncoaxname);

 for(var j=0, i=0 ; j<weapons.length ; j++)
 {
  if((weapons[j][3] & myBS) != 0 ) 
  {
   data.wpn.options[i] = new Option(weapons[j][0],j);
   data.wpncoaxname.options[i] = new Option(weapons[j][0],j);
   i++;
  }
 }
  data.wpn.selectedIndex = 0;
  data.wpncoaxname.selectedIndex = 0;

  wipeWeapons();
}


function applyFields()
{


  if(data.structure.selectedIndex+1 < data.fieldqty.selectedIndex)
  {
    window.alert("Only one force field per structure point is allowed.");
    data.fieldqty.selectedIndex = data.structure.selectedIndex+1;
  }

  if(data.fieldqty.selectedIndex != 0)
  {

    if(mySize == SMALL)
    {
      window.alert("This size of vehicle may not have force fields");
      data.fieldqty.selectedIndex = 0;
      return;
    } 

    switch(data.fieldtype.selectedIndex)
    { 
      case 0:
        if( (((MARINE+HUMAN+ADEPTUS) & myCrew) == 0) || mySize != WARMACHINE )
        {
          window.alert("Only Imperial War Machines may select Imperial fields.");
          data.fieldqty.selectedIndex = 0;
        }
        break;
      case 1:
        if(myCrew != ELDAR && myCrew != BS4ELDAR && myCrew != DARKELDAR)
        { 
          window.alert("Only Eldar or Dark Eldar vehicles may use Eldar fields.");
          data.fieldqty.selectedIndex = 0;
        }
        else if(data.fieldqty.selectedIndex > 1)
        { 
           window.alert("Only 1 Eldar field is allowed per facing.")
           data.fieldqty.selectedIndex = 1;
        }
        break;
      case 2:
        if(myCrew != ORK)
        {
           window.alert("Only ork vehicles may use ork fields.");
           data.fieldqty.selectedIndex = 0;
        }
        break;
      case 3:
        if( (((TAU+BS4TAU) & myCrew) == 0) || mySize != WARMACHINE )
        {
          window.alert("Only Tau War Machines may select Tau fields.");
          data.fieldqty.selectedIndex = 0;
        }
    }
    applyFieldFacing();
  }

}


function applyFieldFacing()
{
  if(data.fieldtype.selectedIndex == 1)
  {
     if(!data.fieldfront.checked && !data.fieldside.checked && !data.fieldrear.checked)
     {
        window.alert("Eldar fields must have at least one facing selected");
        data.fieldfront.checked = true; 
     } 
  }
  else if(data.fieldfront.checked || data.fieldside.checked || data.fieldrear.checked)
  {
    window.alert("Field facings only apply to Eldar fields");
    data.fieldfront.checked = false;
    data.fieldside.checked = false;
    data.fieldrear.checked = false;
  }
    refreshPoints();
}


function addFlyerWeapon()
{
  if(myType == FLYER)
  {
    var wpnString = ""; 
    var wpnCost = 0;
    wpnString += data.bombqty.value + " x ";
    wpnString += data.bombs.options[data.bombs.selectedIndex].text;
     
    wpnCost += parseInt(data.bombs.options[data.bombs.selectedIndex].value);
    wpnCost *= parseInt(data.bombqty.value);
  
    data.wpnlist.options[data.wpnlist.length] = new Option(wpnString,wpnCost);

    data.wpnlist.selectedIndex = data.wpnlist.length-1;
  }
  else
    window.alert("Only flyers may be given flyer weapons");
    
    refreshPoints();
}


function addWeapon()
{
  var wpnCost = 0;
  var wpnString = "";
  var wpnMplr = 1.0;

  wpnString += "" + data.wpnqty.value + " x ";
  wpnCost += weapons[data.wpn.options[data.wpn.selectedIndex].value][4];

  if(data.wpnaa.checked)  wpnString += "AA ";

  if(data.wpntwin.checked)
  {
    wpnString += "Twin-linked ";
    wpnMplr += 0.5;
  }

  if(data.wpngatling.checked)
  {
    wpnString += "Gatling ";
    wpnMplr += 1.5;
  }

  if(data.wpnlong.checked)
  {
    wpnString += "Long-barrelled ";
    wpnMplr += 0.5;
  }

  if(data.wpnshort.checked)
  {
    wpnString += "Short-barrelled ";
    wpnMplr -= 0.25;
  }

  if(data.wpnmega.checked)
  {
    wpnString += "Mega "
    wpnMplr += 1.5;
  }

  if(data.wpnblast.checked)
  {
    wpnString += "Blast ";
    wpnMplr += 1.5;
  }

  if(data.wpntitan.checked)
  {
    wpnString += "Titan-killer ";
    wpnMplr += 0.5;
  }

  wpnString +=  data.wpn.options[data.wpn.selectedIndex].text ;

  if(data.wpnbattery.selectedIndex != 0)
  {
    var x = parseInt(data.wpnbattery.options[data.wpnbattery.selectedIndex].text);
    wpnString += " Gun Battery[+" + x + "] ";
    wpnMplr += x * 0.5;
  }

  if(data.wpnrof.selectedIndex != 0)
  {
    var x = parseInt(data.wpnrof.options[data.wpnrof.selectedIndex].text);
    wpnString += " [" + x + " shots] ";
    wpnMplr += x * 0.25;
  }

  wpnCost *= wpnMplr;

  if(data.wpncoax.checked)
  {
    wpnString +=  " + co-axial " + data.wpncoaxname.options[data.wpncoaxname.selectedIndex].text;
    wpnCost += weapons[data.wpncoaxname.options[data.wpncoaxname.selectedIndex].value][4];
  } 

  if(data.wpnaa.checked)  wpnCost *= 1.5;

  wpnCost *= parseInt(data.wpnqty.value);
//wpnString += " ["+ wpnCost +"] ";
  data.wpnlist.options[data.wpnlist.length] = new Option(wpnString,wpnCost);

  data.wpnlist.selectedIndex = data.wpnlist.length-1;
    refreshPoints();
}


function removeWeapon()
{
  var i = data.wpnlist.selectedIndex;
  if(i > 0)  data.wpnlist.options[i] = null;
    refreshPoints();
}


function applyTunneller()
{
  if((((TAU+BS4TAU) & myCrew) != 0) && data.tunneller.checked)
  {
    window.alert("Tau may not select the Tunneller option.");
    data.tunneller.checked = false;
  }

  if(myType != TANK && myType != LIGHTVEHICLE && data.tunneller.checked)
  {
    window.alert("Only tanks and light vehicles may select the tunneller option");
    data.tunneller.checked = false;
  }
    refreshPoints();
}

function applyOption(thisitem)
{
    if((((TAU+BS4TAU) & myCrew) != 0) && thisitem.checked )
    {
        window.alert("Tau may not select this option.");
        thisitem.checked = false;
    }
    
    if(((NECRON & myCrew) != 0) && thisitem.checked )
    {
        window.alert("Necrons may not select this option.");
        thisitem.checked = false;
    }
    
    refreshPoints();
    
}

function applyNecronOptions(showmsg)
{

    if ((NECRON & myCrew) == 0) {
        if (showmsg) window.alert("Only Necrons may select this option.");
        
        data.necronsepulchre.checked = false;
        data.necronpowermatrix.checked = false;
        data.necronlivingmetal.checked = false;
        data.necronshroud.checked = false;
        
    } else {
        if(data.necronsepulchre.checked && (mySize != WARMACHINE))
        {
            if (showmsg) window.alert("Only Necron War Machines may select Sepulchre.");
            data.necronsepulchre.checked = false;
        }
    
        if(data.necronpowermatrix.checked && ((myType != TANK) && (myType != IMMOBILE)))
        {
            if (showmsg) window.alert("Only Necron Tanks and Immobile vehicles may select Power Matrix.");
            data.necronpowermatrix.checked = false;
        }
    
    }

    refreshPoints();

}


function applyTauOptions(showmsg)
{
    if(((TAU+BS4TAU) & myCrew) == 0)
    {
        //not tau
        if (showmsg) window.alert("Only Tau may select this option.");
        data.taulandinggear.checked = false;
        data.tauescapepods.checked = false;
        data.taudronerack.checked = false;
        
    } else {

        if(data.taulandinggear.checked && data.skimmer.checked == false)
        {
            if (showmsg) window.alert("Only Tau Skimmers may select Landing Gear.");
            data.taulandinggear.checked = false;
        }
        
        if(data.tauescapepods.checked && (myType != FLYER))
        {
            if (showmsg) window.alert("Only Tau Flyers may select Escape Pods.");
            data.tauescapepods.checked = false;
        }
            
        if(data.taudronerack.checked && (myType != FLYER))
        {
            if (showmsg) window.alert("Only Tau Flyers may select Drone Racks.");
            data.taudronerack.checked = false;
        }
    }
    
    refreshPoints();

}

function applyCarriage()
{
  if(data.carriage.checked && (((TAU+BS4TAU) & myCrew) != 0))
  {
    window.alert("Tau may not select the Carriage option.");
    data.carriage.checked = false;
  }


  if(data.carriage.checked && ((NECRON & myCrew) != 0))
  {
    window.alert("Necron may not select the Carriage option.");
    data.carriage.checked = false;
  }

  if(data.carriage.checked && myType != IMMOBILE)
  {
    window.alert("Only Immobile vehicles may select the Carriage option.");
    data.carriage.checked = false;
  }
    refreshPoints();
}


function applyOpentopped()
{
  if(data.armoured[0].checked && (myType == FLYER))
  {
     window.alert("Flyers always count as being fully armoured.");
     data.armoured[1].checked = true;
  }
  
  refreshPoints();
}



function calc(arg)
{
  myPoints = 0;
  myPoints += data.structure.selectedIndex * 100;
  myPoints += armourCost[0][data.frontarmour.selectedIndex];
  myPoints += armourCost[1][data.sidearmour.selectedIndex] * 2;
  myPoints += armourCost[2][data.reararmour.selectedIndex];
  myPoints += speedCost[mySize][mySpeed];
  myPoints += parseInt(data.transport.value);
  if(data.amphibious.checked) myPoints += 10;
  if(data.orbital.checked) myPoints += 30;
  if(data.recovery.checked) myPoints += 5;
  if(data.wrecker.checked) myPoints += 5;
  if(data.skimmer.checked) myPoints += 20;
  if(data.tunneller.checked) myPoints += 25;
  if(data.carriage.checked) myPoints += 5;
  if(data.towbar.checked) myPoints += 5;

  if(data.taulandinggear.checked) myPoints += 5;
  if(data.tauescapepods.checked) myPoints += 20;
  if(data.taudronerack.checked) myPoints += 10;

  if(data.necronsepulchre.checked) myPoints += 50;
  if(data.necronpowermatrix.checked) myPoints += 60;
  if(data.necronlivingmetal.checked) myPoints += 50;

  if(data.armoured[0].checked)
  {
    myPoints -= (myTotalArmour > 50) ? 10 : 0;
    myPoints -= (myTotalArmour > 54) ? 10 : 0;
  }
  else
  {
    if(myType != FLYER)
    {
      myPoints += (myTotalArmour < 47) ? 10 : 0;
      myPoints += (myTotalArmour < 43) ? 10 : 0;
    }
  }

  switch(data.fieldtype.selectedIndex)
  {
    case 0:
      myPoints += (data.fieldqty.selectedIndex * 35);
      break;  
    case 1:
      if(data.fieldqty.selectedIndex != 0)
      {
        myPoints += (data.fieldfront.checked) ? 25 : 0 ;
        myPoints += (data.fieldside.checked) ? 50 : 0 ;
        myPoints += (data.fieldrear.checked) ? 25 : 0 ;
      }
      break;
    case 2:
      myPoints += (data.fieldqty.selectedIndex * 15);
      break; 
    case 3:
      myPoints += (data.fieldqty.selectedIndex * 35);
      break;  
  }

  var totalWpnPoints = 0;

  for(var i=1 ; i<data.wpnlist.length ; i++)
  {
     totalWpnPoints += parseFloat(data.wpnlist.options[i].value);
  }
  myPoints += totalWpnPoints;

  var myAttacks = 1;

  if(data.closecombat.checked)
  {
	if (parseInt(data.ccwpnqty.value) < 1) 
	{
		data.closecombat.checked = false;
	}
  }

  if(data.closecombat.checked)
  {
    wpnCosts = new Array(1,3,5,10);   
 
    myAttacks += parseInt(data.ccwpnqty.value);
    myAttacks += (data.ferocious.checked) ? 1 : 0 ; 
    myPoints += myAttacks * crewStats[data.crew.selectedIndex][0] * wpnCosts[data.ccwpntype.selectedIndex];
  }

  if(isNaN(myPoints))
  {
    window.alert("There was an error in calculating the points cost, most likely you've used non-numeric characters in one of the text fields");
    return false;
  }
  else
{

  //Necron Shroud last as it's 25% of total vehicle cost 
  if(data.necronshroud.checked)
  {
    myPoints = myPoints * 1.25;
  }


 myPoints = Math.ceil(myPoints);
 
 

crw = data.crew.selectedIndex;

modBS = 0 + crewStats[crw][1];
 if(data.wpntargeters.checked) modBS = "(" + (modBS+1) + ")";
    else modBS = "";

var modS = '';
if(data.closecombat.checked)
{

 modS = crewStats[crw][2];
 if(data.ccwpntype.selectedIndex == 2) modS = "(" + (modS*2) + ")";
  else if(data.ccwpntype.selectedIndex == 3) modS = "(10)";
    else modS = "";

}

var myWpns = "";
for(var i=1 ; i<data.wpnlist.length ; i++)
{
  myWpns += "" + data.wpnlist.options[i].text + "<BR>";
}

if(data.closecombat.checked)
{
  myWpns += data.ccwpnqty.value + " x " + data.ccwpntype.options[data.ccwpntype.selectedIndex].text + "<BR>";
}

var specials = "";

if(parseInt(data.transport.value) > 0)
  specials += "Transport " + data.transport.value + "<BR>";

if(data.fieldqty.selectedIndex > 0)
{
  specials += data.fieldqty.selectedIndex + " x " + data.fieldtype.options[data.fieldtype.selectedIndex].text + " field ";

if(data.fieldtype.selectedIndex == 1)
{
  specials += (data.fieldfront.checked) ? "[front]" : "";
  specials += (data.fieldside.checked) ? "[side]" : "";
  specials += (data.fieldrear.checked) ? "[rear]" : "";
}

specials += "<BR>";

}


if(data.amphibious.checked) specials += "Amphibious <BR>"; 
if(data.carriage.checked) specials += "Carriage <BR>"; 
if(data.orbital.checked) specials += "Orbital lander <BR> ";
if(data.armoured[0].checked) specials += "Open-topped <BR>";
if(data.recovery.checked) specials += "Recovery vehicle <BR> ";
if(data.wrecker.checked) specials += "Wrecker <BR> ";
if(data.skimmer.checked) specials += "Skimmer <BR>";
if(data.ferocious.checked) specials += "Ferocious <BR>";
if(data.engines.checked) specials += "Souped-up engine <BR>"; 
if(data.ponderous.checked) specials += "Ponderous <BR>"; 
if(data.towbar.checked) specials += "Tow-Bar <BR>"; 
if(data.tunneller.checked) specials += "Tunneller <BR>";
if(data.wpntargeters.checked) specials += "Targeters <BR>";

if(data.taulandinggear.checked) specials += "Landing Gear <BR>";
if(data.tauescapepods.checked) specials += "Escape Pods <BR>";
if(data.taudronerack.checked) specials += "Drone Rack <BR>";

if(data.necronsepulchre.checked) specials += "Sepulchre <BR>";
if(data.necronpowermatrix.checked) specials += "Power Matrix <BR>";
if(data.necronlivingmetal.checked) specials += "Living Metal <BR>";
if(data.necronshroud.checked) specials += "Shroud <BR>";


cat = category[mySize][myType];
if(cat == "Transport")
{
 if(parseInt(data.transport.value) == 0 || totalWpnPoints > 50)
   cat = "Heavy Support";
} 

var sp = 1 + data.structure.selectedIndex;
if(sp == 1) sp = "&nbsp;";

if(!arg)
{
  if(data.closecombat.checked || myType == WALKER ) dfwinloc = 'ccdatafax.html';
    else dfwinloc = 'datafax.html';

  datafaxwin = window.open(dfwinloc, "datafaxwin","width=675,height=350,left=50,top=15,location=no,directories=no,toolbar=yes,status=no,menubar=yes,scrollbars=yes");

}

save = document.forms['saveForm'];
if(data.vname.value != '') save.name.value = data.vname.value;
  else save.name.value = '-';
save.points.value = myPoints;
save.race.value = data.crew.options[data.crew.selectedIndex].text;
save.type.value = data.type.options[myType].text;
save.size.value = data.size.options[mySize].text;
save.speed.value = data.speed.value;
save.category.value = cat;
save.fa.value = eval(data.frontarmour.selectedIndex+9);
save.sa.value = eval(data.sidearmour.selectedIndex+9);
save.ra.value = eval(data.reararmour.selectedIndex+9);
save.bs.value = crewStats[crw][1] + modBS;
save.ccw.value = data.closecombat.checked;
if(isNaN(parseInt(sp))) sp='-';
save.structure.value = sp;
save.weapons.value = myWpns;
save.options.value = specials;

if(data.closecombat.checked || myType == WALKER )
{
  save.ws.value = crewStats[crw][0];
  save.s.value = crewStats[crw][2] + modS;
  save.i.value = crewStats[crw][3];
  save.a.value = myAttacks;
}
else
{
  save.ws.value = '-';
  save.s.value = '-';
  save.i.value = '-';
  save.a.value = '-';
}
  return true;

}
}


function togglePoints()
{
    if(showPointsCost)
    {
    	showPointsCost = false;
    	document.getElementById('pointsbox').style.display = 'none';
    	document.forms['designform'].showpointsbtn.value = 'Show Points Cost';
    	document.getElementById('showpointsbtn2').value = 'Show Points Cost';
    }
    else
    {
    	showPointsCost = true;
    	showPoints();
    	document.forms['designform'].showpointsbtn.value = 'Hide Points Cost';
    	document.getElementById('showpointsbtn2').value = 'Hide Points Cost';
    	
    	refreshPointsBox();
    }
}


function showPoints()
{
    if(calc('nodisplay'))
    {
       //document.getElementById('pointsbox').style.top = document.documentElement.scrollTop+250;
       document.getElementById('pointsbox').innerHTML = save.points.value;
       document.getElementById('pointsbox').style.display = 'block';
	refreshPointsBox();
    }
    else
    {
       document.getElementById('pointsbox').style.display = 'none';
       showPointsCost = false;
    }
}

function refreshPoints()
{
   if(showPointsCost) showPoints()
}


function dfCallback()
{
   save = document.forms['saveForm'];
   with(datafaxwin.document)
   {
      title = save.name.value;
      getElementById('name').innerHTML = save.name.value;
      getElementById('points').innerHTML = save.points.value;
      getElementById('type').innerHTML = save.type.value;
      getElementById('size').innerHTML = save.size.value;
      getElementById('speed').innerHTML =  save.speed.value;
      getElementById('category').innerHTML = save.category.value;
      getElementById('fa').innerHTML = save.fa.value;
      getElementById('sa').innerHTML = save.sa.value;
      getElementById('ra').innerHTML = save.ra.value;
      getElementById('bs').innerHTML = save.bs.value;
      getElementById('structure').innerHTML = save.structure.value;
      getElementById('weapons').innerHTML = save.weapons.value;
      getElementById('options').innerHTML = save.options.value;
   }
   if(data.closecombat.checked || myType == WALKER)
   {
      datafaxwin.document.getElementById('ws').innerHTML = save.ws.value;
      datafaxwin.document.getElementById('s').innerHTML = save.s.value;
      datafaxwin.document.getElementById('i').innerHTML = save.i.value;
      datafaxwin.document.getElementById('a').innerHTML = save.a.value;
   }
}


function doSubmit()
{
   if(data.vname.value == "")
   {
     window.alert("You must enter the vehicle name before submitting");
     return false;
   }
   if(calc('nodisplay')) return true;
     else return false;
}

