Need help in troubleshooting this code involving Arduino Uno and GSM sim900

My aim of the project is to detect the presence of electricity and then turn on the motor or any appliance so I am firstly doing this on small scale.

How this code I want to work is: Pin number 8 will detect HIGH voltage level(i.e electricity present), i do this by giving it +5V then Arduino will send a message to given no. about the presence of electricity then the user will send SMS to gsm module to turn the led on(#0) or off(#1) connected to pin no. 13.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
boolean bOK = HIGH;
char msg; 
int pinstate=0;
int lastpinstate=0;
void setup()
{
pinMode(8,INPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
digitalWrite(13,HIGH);
mySerial.println("AT");
delay(1200);
digitalWrite(13,LOW);
}
void loop()
{
if(bOK==1)  //loop-1
{
mySerial.println("AT+CMGF=1"); // sets the SMS mode to text
delay(1500);
mySerial.print("AT+CMGS=\""); // send the SMS number
mySerial.print("+9199xxxxxxxx"); // +91 for india
mySerial.print("\"");
delay(1000);
mySerial.println("Arduino & GSM On"); // SMS body
Serial.println("Arduino & GSM On"); // SMS body
delay(2500);
mySerial.write(0x1A); // sends ctrl+z- end of message
mySerial.write(0x0D); // carriage return = go back to starting i.e left
mySerial.write(0x0A);  // new line
       bOK=LOW;
}
pinstate=digitalRead(8);
if(pinstate !=lastpinstate) //loop-2
{
  if(pinstate==HIGH)  //loop-3
  {
    mySerial.print("AT+CMGS=\""); // send the SMS number
    mySerial.print("+9199xxxxxxxx"); // +91 for india
    mySerial.println("\"");
    delay(1000);
    mySerial.print("Electricity Available"); // SMS body
    Serial.println("Electricity Available"); // SMS body
    delay(1000);
    mySerial.write(0x1A);
    mySerial.write(0x0D);
    mySerial.write(0x0A);
    delay(3000);
   // Serial.println("AT+CMGF=1");
    delay(2000);
  //  Serial.println("AT+CNMI=2,2,0,0,0");
    if(mySerial.available()>0)  //loop-4
    {
    delay(10);
    msg=mySerial.read();
    if(msg=='#')   //loop-5
    {
    delay(10);
    msg=mySerial.read();
    mySerial.println(msg);
    if(msg=='0')    //loop-6
    { 
        delay(10);
      Serial.println(msg);
      digitalWrite(13,HIGH); //led on
    }
      else if(msg=='1')      //loop-7
    {
      delay(10);
      Serial.print(msg);
      digitalWrite(13,LOW);  //led off
    }
    }
   }
}    
else            //loop-8
{
    mySerial.print("AT+CMGS=\""); // send the SMS number
    mySerial.print("+9199xxxxxxxx"); // +91 for india
    mySerial.println("\"");
    delay(1000);
    mySerial.print("Electricity Off"); // SMS body
    Serial.println("Electricity Off"); // SMS body
    delay(1000);
    mySerial.write(0x1A);
    mySerial.write(0x0D);
    mySerial.write(0x0A);
    delay(3000); 
}
}
lastpinstate=pinstate;
}

Where the problem is: The code is working except in loop where I can on/off the led i.e loop 4,5,6,7. I am getting messages of electricity available and off but when electricity is available I can’t on or off the led. When I tested that part of code separately I was able to on/off the led, But when I add it to main code it doesn’t work.

Any help would be much appreciated!

pinMode(8,INPUT);

What is actually connected to this pin? Does it pull the pin HIGH or LOW all the time?

boolean bOK = HIGH;

Boolean variables should be assigned true or false, not HIGH or LOW.

    if(mySerial.available()>0)  //loop-4
    {
    delay(10);
    msg=mySerial.read();
    if(msg=='#')   //loop-5
    {
    delay(10);
    msg=mySerial.read();
    mySerial.println(msg);

Stuffing your head in the sand for 20 milliseconds will not magically make serial data appear. You should NOT rely on that happening.

I am getting messages of electricity available and off

What, EXACTLY, do those messages say?

Please use Tools + Auto Format before posting code again. Your indenting sucks, making it very difficult to follow the logic.