motor turn back and forth upon button presses

hi, i have a problem with my code. i want to turn a dc motor clockwise upon button press and turn counterclockwise when the same button is pressed again. so it should be: button pressed, turn left, button pressed again, turn right. i tried like this and thought it should work, but it doesnt rest after being pressed once, but it turns right back.

int pin1 = 13;
int pin2 = 12;
int inputpin = 2;
int val = 0;

void setup(){
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(inputpin, INPUT);
}

void loop(){
val = digitalRead(inputpin);
if (val == HIGH){

digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);

} else {

digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
delay(1500);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);

}
if (val == HIGH){

digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);

} else {

digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
delay(1500);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
}

i tried it with the command for the turning back inside and outside the first if(){...
and outside. tried everything, but i guess im missing something here.
i hope this is not a too stupid question and somebody can help me here,
thanks

OK I have not got my Arduino here but the code is all wrong in structure. You have to wait until the button is not pressed, then wait until it has been pressed. See what direction it was before and change it. Therefore each time through the loop function you have to remember the state of these past things and compare them with what is happening now to find out what you should do next. Please draw your self a flow diagram it will be so much clearer.

Anyway off the top of my head here is a bit of pseudo code that gives you the idea:-

loop(){
read button value
if(button_value != last_button_value) {
if(button_value == low) {
// Turn motor off
}
else
{ if(last_turn != clockwise) {
// turn motro clockwise;
last_turn == clockwise;
}
else {
// turn motor anty_clockwise
last_turn = ~clockwise; // a logica NOT to clockwise
}
}

}

delay(100)
last_button_value = button_value;
}