Could some wise person please help with this animation problem

Hi there and thanks for looking. The problem i am having is i want this script to play like this

  1. when i dont press anything it animates "idle"
  2. when i press "a" or "d" it animtes and loops "run"
  3. Whe i let go it stops looping "run" and Animates and loops idle.

Here is my code below also could someone please tell me how to format my code for these forums

function Awake()
{
   animation["Idle"].layer = 0;
   animation["Run"].layer = 1;
   animation["Jump"].layer = 2;
}

var wrapMode : WrapMode;

function Update()
{
   if (Input.GetKey("d")) {
      animation["Run"].layer = 1;
      animation["Run"].wrapMode = wrapMode.Loop;
   }
   else if (Input.GetKeyUp("d")) {
      animation["Idle"].layer = 0;
      animation["Idle"].wrapMode = wrapMode.Loop;
   }
   else if (Input.GetKey("a")) {
      animation["Run"].layer = 1;
      animation["Run"].wrapMode = wrapMode.Loop;
   }
   else if (Input.GetKeyUp("a")){
      animation["Idle"].layer = 0;
      animation["Idle"].wrapMode = wrapMode.Loop;
   }

   if (Input.GetButtonDown("Jump")) {
      animation.CrossFade("Jump");
   }
   else{
      animation.CrossFade("Idle");
   }
}

The problem is, that Run is on layer 1 and idle on 0, so Run beats Idle. You also don't have to set the layers and wrapModes always again. Once set, it keeps until the game is stopped. Also, you could try to stop the run animation if the key is lowered. I think of something in a way like this:

 if (Input.GetKeyUp("a")){
      animation.Stop("Run");
}

This should be everything you need.