﻿var SelectedCell;
var SelectedControl;
var CellsList;
var CellsArray;
var CellsCounter;
var SelectionMode;
var errInput = true;
var errInputID = '';
var canSelect=true;
var CompleteError=false;
function ChangeDIR()
{
    if (SelectedCell !=''){
             if (document.getElementById("hf_SelectionMode").value == 'H')
                {
                    
                    document.getElementById("img_dir").src= '../images/home/green-arrow-d.png';
                      //document.getElementById("hf_SelectionControl").value = obj3;
                      document.getElementById("hf_SelectionMode").value = "V";
                      GenerateClick(document.getElementById("" + SelectedCell).className,'',SelectedCell,true);
                }
                else
                {
                     if (document.getElementById("hf_SelectionMode").value == 'V')
                     {
                        document.getElementById("img_dir").src= '../images/home/green-arrow-l.png';
                         //document.getElementById("hf_SelectionControl").value = obj3;
                          document.getElementById("hf_SelectionMode").value = "H";
                          GenerateClick(document.getElementById("" + SelectedCell).className,'',SelectedCell,true);
                     }
               
                }
                }
}
function ConfirmGame(obj)
{

    if (obj)
    {
        
     
       
        SetHTMLStyle("tab_confirm",'inline','table');
        
        document.getElementById("btn_showFull").style.display = 'none';
        document.getElementById("tab_help").style.display = 'none';
        
    }
    else
    {
         
        SetHTMLStyle("btn_showFull",'inline','table');
        document.getElementById("tab_confirm").style.display = 'none';
       document.getElementById("tab_help").style.display = 'block';
    }
}
    
    
                    var Rc=255;
                    var Gc=59;
                    var Bc=59;
                    var FlagC =1;
                    var TimC;
                    function ColoredTextC(Obj)
                    {
                        
                        Gc=Gc+(10*FlagC);
                        Bc=Bc;
                        Rc=Rc;
                        document.getElementById(Obj).style.backgroundColor= "rgb(" + Rc + "," + Gc + "," + Bc + ")";
                        if(Gc>=250)
                            {FlagC=-1;}
                        if(Gc<=50)
                            {FlagC=1;}
                        TimC=setTimeout("ColoredTextC('" + Obj + "')",120);
                    }

     function GenerateH(obj,spn)
    {
         if (navigator.appName == "Microsoft Internet Explorer")
        {
              document.aspnetForm.onkeyup = function() {ProcessEvent(window.event);};
             document.aspnetForm.onkeydown = function() {HandleKeyPress(window.event);};
        }
        else
        {
//                document.aspnetForm.attachEvent("onkeyup","return ProcessEvent(event);")
//                document.aspnetForm.attachEvent("onkeydown","return HandleKeyPress(event);")
             
        }
         
             if (CheckHTMLObj("spn_timer",'00:00'))
             {
                
                     NewGame();
             }
             document.getElementById("img_dir").src= '../images/home/green-arrow-l.png';
        document.getElementById("hf_SelectionMode").value = 'H';
        document.getElementById("hd_lh").value = obj;
    if (errInput ==true)
    {
               var lcount;
               lcount = 0;
               var vCount  = document.getElementById("hf_VCount").value;
               var c;                
                for (c=0;c<vCount;c++)
                {
                   
                    document.getElementById("spn_V" + c).style.color="black";
                    document.getElementById("spn_V" + c).style.textDecoration = "none";
                   
                }
        
        CellsList = document.getElementById("hf_cells").value;
        SelectionMode  = document.getElementById("hf_SelectionMode").value;
        CellsArray = CellsList.split(",");
                  
        CellsCounter =0;
        while (CellsCounter < CellsArray.length-1)
        {
             if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
             {
                    
                     
                     document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                     if (document.getElementById("" + CellsArray[CellsCounter]).className == obj)
                        {      
                             lcount = lcount + 1;       
                             document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                             if (lcount==1)
                             {
                             
                                 SelectedCell = CellsArray[CellsCounter];
                                 document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                             }
                        } 
                               
             }
             
            CellsCounter+=1;
        }
                
                        var hCount  = document.getElementById("hf_HCount").value;
                        var c;                
                        for (c=0;c<hCount;c++)
                        {
                             document.getElementById("spn_H" + c).style.textDecoration = "none";
                             document.getElementById("spn_H" + c).style.color="black";
                        }
                        document.getElementById(spn).style.textDecoration = "none";
                        document.getElementById(spn).style.color="red";
                        ShowHTMLObj("spn_question",spn)
             if (canSelect==true)
             {
                 
                   ShowHTMLText("spn_count",lcount + 'حروف');
             
             }
           
     }
     }
     
     
      function GenerateV(obj,spn)
    {
        
        if (navigator.appName == "Microsoft Internet Explorer")
        {
              document.aspnetForm.onkeyup = function() {ProcessEvent(window.event);};
             document.aspnetForm.onkeydown = function() {HandleKeyPress(window.event);};
        }
        else
        {
//               document.aspnetForm.attachEvent("onkeyup","return ProcessEvent(event);")
//                document.aspnetForm.attachEvent("onkeydown","return HandleKeyPress(event);")
        }
        
      
         
                //alert(CheckHTMLObj("spn_timer",'00:00'));    
             if (CheckHTMLObj("spn_timer",'00:00'))
             {
                
                     NewGame();
             }
             
             
             
             
    if (errInput ==true)
    {
                document.getElementById("img_dir").src= '../images/home/green-arrow-d.png';
                document.getElementById("hf_SelectionMode").value = 'V';
                document.getElementById("hd_lv").value = obj;
                  var lcount;
                lcount = 0;
            var hCount  = document.getElementById("hf_HCount").value;
                var c;
                
                for (c=0;c<hCount;c++)
                {
                    document.getElementById("spn_H" + c).style.color="black";
                    document.getElementById("spn_H" + c).style.textDecoration = "none";
                   
                }
                CellsList = document.getElementById("hf_cells").value;
                  SelectionMode  = document.getElementById("hf_SelectionMode").value;
                  CellsArray = CellsList.split(",");
                CellsCounter =0;
        while (CellsCounter < CellsArray.length-1)
        {
             if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
             {
                   if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
                    {
                        document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                        if (document.getElementById("" + CellsArray[CellsCounter]).abbr == obj)
                        {
                             lcount = lcount +1 ;      
                             document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                             if (lcount==1)
                             {
                             
                                 SelectedCell = CellsArray[CellsCounter];
                                 document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                             }
                        }
                     }
             }
            CellsCounter+=1;
        }
       
              var VCount  = document.getElementById("hf_VCount").value;
                var c;
                c=0;
                for (c=0;c<VCount;c++)
                {
                 document.getElementById("spn_V" + c).style.textDecoration = "none";
                   document.getElementById("spn_V" + c).style.color="black";
                }
                document.getElementById(spn).style.textDecoration = "none";
                  document.getElementById(spn).style.color="red";
                   ShowHTMLObj("spn_question",spn);
                 if (canSelect==true){
           
                     ShowHTMLText("spn_count",lcount + 'حروف');
             }
            }
     }
       var countW;
     function GenerateClick(obj1,obj2,obj3,obj4)
    {  
         if (navigator.appName == "Microsoft Internet Explorer")
        {
              document.aspnetForm.onkeyup = function() {ProcessEvent(window.event);};
             document.aspnetForm.onkeydown = function() {HandleKeyPress(window.event);};
        }
        else
        {
//               document.aspnetForm.attachEvent("onkeyup","return ProcessEvent(event);")
//                document.aspnetForm.attachEvent("onkeydown","return HandleKeyPress(event);")
        }
        SelectedCell = obj3;
    

    
             
             if (CheckHTMLObj("spn_timer",'00:00'))
             {
                     NewGame();
             }
             
    if (errInput ==true)
    {
                var lcount;
                lcount = 0;
            
               var hCount  = document.getElementById("hf_HCount").value;
               var chCount;
                
                for (chCount=0;chCount<hCount;chCount++)
                {
                    document.getElementById("spn_H" + chCount).style.color="black";
                    document.getElementById("spn_H" + chCount).style.textDecoration = "none";
                   
                }
                  var VCount  = document.getElementById("hf_VCount").value;
                var cVCount;
                cVCount=0;
                for (cVCount=0;cVCount<VCount;cVCount++)
                {
                 document.getElementById("spn_V" + cVCount).style.textDecoration = "none";
                 document.getElementById("spn_V" + cVCount).style.color="black";
                }
         
                if (document.getElementById("hf_SelectionMode").value == 'H' && document.getElementById("hf_SelectionControl").value==obj3)
                {
               
                      //document.getElementById("hf_SelectionControl").value = obj3;
                      if (obj4==false){                      
                      document.getElementById("hf_SelectionMode").value = "V";
                       document.getElementById("img_dir").src= '../images/home/green-arrow-d.png';
                      }
                }
                else
                {
                     if (document.getElementById("hf_SelectionMode").value == 'V' && document.getElementById("hf_SelectionControl").value==obj3)
                     {
            
                          //document.getElementById("hf_SelectionControl").value = obj3;
                          if (obj4==false)
                          {
                           document.getElementById("img_dir").src= '../images/home/green-arrow-l.png';
                          document.getElementById("hf_SelectionMode").value = "H";
                         
                          }
                     }
                }
                  document.getElementById("hf_SelectionControl").value = obj3;

                    
                  CellsList = document.getElementById("hf_cells").value;
                  SelectionMode  = document.getElementById("hf_SelectionMode").value;
                  CellsArray = CellsList.split(",");
                   
                  CellsCounter =0;
                  while (CellsCounter < CellsArray.length-1)
                  {
                    if (SelectionMode == "H")
                    {
                            document.getElementById("hd_lh").value = obj1;
                            try{
                            if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
                            {
                            
                                
                                document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                                
                                if (document.getElementById("" + CellsArray[CellsCounter]).className == obj1)
                                {   
                                    ControlList = ControlList + CellsArray[CellsCounter] + ',';
                                    lcount = lcount +1 ;      
                                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
 
                                }
                             }
                             }//try
                             catch(err)
                             {
                                alert("" + CellsArray[CellsCounter]);
                             }
                             
                               // Select The Hint
                                     
                       }// end if SelectionMode
                       else
                       {
                             document.getElementById("hd_lv").value = document.getElementById("" + obj3).abbr;
                            
                             
                            
                             if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
                                {
                                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                                  
                                    if (document.getElementById("" + CellsArray[CellsCounter]).abbr == document.getElementById("" + obj3).abbr)
                                    {            
                                        ControlList = ControlList + CellsArray[CellsCounter] + ',';
                                        lcount = lcount +1 ;        
                                        document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                                    }
                                 }
                       }
                       
                      CellsCounter+=1;               
                  }
             
                    if (SelectionMode == "H")
                    {
                        var cname = document.getElementById("" +  obj3).className;
                        var hCount  = document.getElementById("hf_HCount").value;
                        var c;
                
                        for (c=0;c<hCount;c++)
                        {
                             document.getElementById("spn_H" + c).style.color="black";
                             document.getElementById("spn_H" + c).style.textDecoration = "none";
                             
                             if (document.getElementById("spn_H" + c).className == cname)
                             {
                                document.getElementById("spn_H" + c).style.color="red";
                               
                                
                                ShowHTMLObj("spn_question","spn_H" + c);
                                document.getElementById("spn_H" + c).style.textDecoration = "none";
                              }
                          }
                    
                    
                      }// end if SelectionMode
                      else
                      {
                            var cname = document.getElementById("" + obj3).abbr;
                            var VCount  = document.getElementById("hf_VCount").value;
                            var c;
                            c=0;                   
                            for (c=0;c<VCount;c++)
                            {
                                document.getElementById("spn_V" + c).style.color="black";
                                document.getElementById("spn_V" + c).style.textDecoration = "none";
                                if ( document.getElementById("spn_V" + c).className == cname)
                                { 
                                    
                                      ShowHTMLObj("spn_question","spn_V" + c);
                                     document.getElementById("spn_V" + c).style.color="red";
                                     document.getElementById("spn_V" + c).style.textDecoration = "none";
                                }
                             }                     
                      
                      }// end ELSE SelectionMode
                
               if( document.getElementById("" + obj3).style.backgroundColor != "red")
               {
                      document.getElementById("" + obj3).style.backgroundColor="#43a0cb";                
                     }
                      if (canSelect==true)
                      {
                             switch(lcount)
                             {
                                case 1 :  GenerateClick(obj1,obj2,obj3,false);
                                break;
                                case 2 :  countW = 'حرفان';
                                break;
                                default :  countW = 'حروف';
                             }
                            if (lcount != 1)
                            {
                                       
                                 ShowHTMLText("spn_count",lcount + ' ' + countW);     
                            }
                     }
                     else
                     {
                            //document.getElementById(errInputID).focus();
                     }
                }
     }// End Function
     
     //var life=170;
     var life=3;
     var ControlList;
     var oldColor;
     function coverme(status,CellName)
     {
       
        
        if (status==1)
        {
            if (SelectedCell != CellName)
            {
            
                 oldColor =document.getElementById("" + CellName).style.backgroundColor;
                 document.getElementById("" + CellName).style.backgroundColor = "#d9e5ea";
             
            }
        
        }
        else
        {
             if (SelectedCell != CellName)
            {
            
               document.getElementById("" + CellName).style.backgroundColor = oldColor;
           
            }
      
        
       
        
        }
     }
     
    function showWord()
     {
     var obj;
     //life = life - 57;
     life = life - 1;
     //document.getElementById('img_life').width = life;
     document.getElementById('img_life').src = 'http://www.artonline.tv/images/home/crosswords/'+ life +'help.jpg';
     
     switch (life)
     {
        case 2 :
       // document.getElementById('img_life').src = '../images/home/lifebar_o.jpg'; 
         
       // ShowHTMLText('spn_level','بقي فرصتان');
        
        break;
        case 1  : 
       //  document.getElementById('img_life').src = '../images/home/lifebar_r.jpg'; 
        
            // ShowHTMLText('spn_level','بقي فرصة واحدة');
         break;
         
        case 0  : 
         {document.getElementById('btn_showWord').style.display = 'none';
         document.getElementById('btn_showFull').style.display = 'inline';
         document.getElementById('img_life').onclick = null;
          document.getElementById('img_life').style.cursor="default";
             // ShowHTMLText('spn_level','انتهت فرص المساعدة المتاحة');
         break;}
     
     }
     
     if (document.getElementById("hf_SelectionMode").value == 'H')
     
     {
                obj =  document.getElementById("hd_lh").value;
          
       
                var lcount;
                lcount = 0;
                 
                CellsCounter =0;
                while (CellsCounter < CellsArray.length-1)
                {
                    if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
                    {
                              var CID  = CellsArray[CellsCounter];
                              var CNAME = "C" + CID.substring(5);
                              document.getElementById("" + CellsArray[CellsCounter]).style.color = "black";
                              if (document.getElementById("" + CellsArray[CellsCounter]).className == obj)
                              {      
                                      
                                      lcount = lcount +1 ;
                                      if (navigator.appName == "Microsoft Internet Explorer")
                                      {
                                        document.getElementById("s" + CellsArray[CellsCounter]).innerText = document.getElementById(CNAME).value;
                                      }
                                      else
                                      {
                                        document.getElementById("s" + CellsArray[CellsCounter]).textContent = document.getElementById(CNAME).value;
                                      }                                                                          
                                         
                              }
                    
                    }
                    CellsCounter+=1
                }
                  
                  
     
     }
     
     else
     {
             obj =  document.getElementById("hd_lv").value;
             var lcount;
                lcount = 0;
                
                CellsCounter =0;
                while (CellsCounter < CellsArray.length-1)
                {    
                     if (document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor != "red")
                    {
                              var CID  = CellsArray[CellsCounter];
                              var CNAME = "C" + CID.substring(5);
                              document.getElementById("" + CellsArray[CellsCounter]).style.color = "black";
                              if (document.getElementById("" + CellsArray[CellsCounter]).abbr == obj)
                              {      
                                      lcount = lcount +1 ;       
                                       if (navigator.appName == "Microsoft Internet Explorer")
                                      {
                                        document.getElementById("s" + CellsArray[CellsCounter]).innerText = document.getElementById(CNAME).value;
                                      }
                                      else
                                      {
                                        document.getElementById("s" + CellsArray[CellsCounter]).textContent = document.getElementById(CNAME).value;
                                      } 
                              }
                    }
                
                  CellsCounter+=1;
                }
      
            
          
     
     
     
     }
     }
    

   
function countdown(obj)
{
	this.obj		= obj;
	this.Div		= "clock";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 5:00 AM";
	this.DisplayFormat	= "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
	this.CountActive	= true;
	
	this.DisplayStr;

	this.Calcage		= cd_Calcage;
	this.CountBack		= cd_CountBack;
	this.Setup		= cd_Setup;
}

function cd_Calcage(secs, num1, num2)
{
   
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}
function cd_CountBack(secs)
{
  this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));

try
  {document.getElementById(this.Div).innerHTML = this.DisplayStr;
  document.getElementById('hd_timer').value = this.DisplayStr;
  if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs+1) + ")", 990);
  }
  catch(err)
  {}
}

function showHID()
{
       var blksArr = document.getElementById('hd_timer').value.split(':');
     
            document.getElementById('Text1').value = document.getElementById('hd_timer').value;
                document.getElementById('hd_serTimer').value = document.getElementById('hd_timer').value;
       if (parseFloat(blksArr[1]) >= parseFloat(5))
       {
         
        
            return true;
       
       }
       else
       {
             ShowHTMLText('spn_AnswerCounter','لا يمكنك الاستسلام قبل مرور 5 دقائق');
            return false;
       
       }
       
       

}
function cd_Setup()
{
	var dthen	= new Date(this.TargetDate);
  	var dnow	= new Date();
  	
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(0);
	this.CountBack(gsecs);
}

 function StartNewGame()
    {
         document.getElementById('tr_Flash').style.display="none";
         document.getElementById('div_hints').style.display="block";
       
        SetHTMLStyle("tr_Game",'block','table-row');
         cd1.Setup();
        
    }
    
    function NewGame()
    {
      
         cd1.Setup();
        
    }
    
    //   underConstruction
    function ProcessEvent(evt)
    {
    
    if (SelectedCell!='')
    {
            MyChar=new Array (222);
            var i = 0;
            while (i<MyChar.length)
            {
                MyChar[i]='';
                i+=1;
            }
            MyChar[221]='د';
            MyChar[219]='ج';
            MyChar[80]='ح';
            MyChar[79]='خ';
            MyChar[73]='هـ';
            MyChar[85]='ع';
            MyChar[89]='غ';
            MyChar[84]='ف';
            MyChar[82]='ق';
            MyChar[69]='ث';
            MyChar[87]='ص';
            MyChar[81]='ض';
            MyChar[222]='ط';
            MyChar[186]='ك';
            MyChar[76]='م';
            MyChar[75]='ن';
            MyChar[74]='ت';
            MyChar[72]='ا';
            MyChar[71]='ل';
            MyChar[70]='ب';
            MyChar[68]='ي';
            MyChar[83]='س';
            MyChar[65]='ش';
            MyChar[191]='ظ';
            MyChar[190]='ز';
            MyChar[188]='و';
            MyChar[77]='ة';
            MyChar[78]='ى';
            MyChar[86]='ر';
            MyChar[67]='ؤ';
            MyChar[88]='ء';
            MyChar[192]='ذ';
            MyChar[59]='ك';
            MyChar[90]='ئـ';
            
         var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
         var charCase =  String.fromCharCode(charCode);
         var className,abbr;
         var ArrowFlag;
         ArrowFlag=false;
          if (evt.keyCode ==32)//Space
         {
            ChangeDIR();
            return;
         }
         if (evt.keyCode ==39)//Right Arrow
         {
        
           ArrowFlag=true;
             SelectionMode  = "H";
             document.getElementById("hf_SelectionMode").value = "H";
             CellsCounter =0;
             className = document.getElementById("" + SelectedCell).className;
             while (CellsCounter < CellsArray.length-1)
             {
                
                if(document.getElementById("" + CellsArray[CellsCounter]).className == className) 
                {
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                }
                else
                {
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                }
                if(CellsArray[CellsCounter] == SelectedCell )
                {
                    if (CellsCounter==0)
                    {
                        document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                        return 0;
                    }
                    if(document.getElementById("" + CellsArray[CellsCounter-1]).className == className) 
                    {
                            SelectedCell = CellsArray[CellsCounter-1];
                            document.getElementById("" + CellsArray[CellsCounter-1]).style.backgroundColor = "#43A0CB";
                            //return 0;
                    }
                    else
                    {
                            document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                    }
                }
                
                CellsCounter+=1;
             }       
             document.getElementById("hd_lh").value  =className;  
             SelectWord();  
                   
         }//left Arrow
         if (evt.keyCode ==37)
         {
            ArrowFlag=true;
            SelectionMode  = "H";
                document.getElementById("hf_SelectionMode").value = "H";
             var lastFlag;
             
             lastFlag = false;
             CellsCounter =0;
             className = document.getElementById("" + SelectedCell).className;
             while (CellsCounter < CellsArray.length-1)
             {
                
                if(document.getElementById("" + CellsArray[CellsCounter]).className == className) 
                {
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                }
                else
                {
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                }
                if(CellsArray[CellsCounter] == SelectedCell)
                {
                    if (CellsCounter+2==CellsArray.length)
                    {
                       
                        document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                        return 0;
                    }
                    if (lastFlag==true)
                    {
                         document.getElementById("" + SelectedCell).style.backgroundColor = "#43A0CB";
                        CellsCounter+=1;
                        continue;
                    }
                    if(document.getElementById("" + CellsArray[CellsCounter+1]).className == className) 
                    {
                            //alert(CellsCounter + " | " + CellsArray.length);
                            SelectedCell = CellsArray[CellsCounter+1];
                            document.getElementById("" + CellsArray[CellsCounter+1]).style.backgroundColor = "#43A0CB";
                            lastFlag=true;
                            //return 0;
                    }
                    else
                    {
                            document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                    }
                }
                
                CellsCounter+=1;
             }    
             document.getElementById("hd_lh").value  =  className;     
             SelectWord();
         
         }
         if (evt.keyCode ==38)// UP Arrow
         {
            ArrowFlag=true;
                SelectionMode  = "V";
                document.getElementById("hf_SelectionMode").value = "V";
             CellsCounter =0;
             abbr = document.getElementById("" + SelectedCell).abbr;
             var lastAbbr;
             lastAbbr='';
             var lastFlag;
             lastFlag=false;
             while (CellsCounter < CellsArray.length-1)
             {
                
                if(document.getElementById("" + CellsArray[CellsCounter]).abbr == abbr) 
                {
                    
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                }
                else
                {
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                }

                if(CellsArray[CellsCounter] == SelectedCell)
                {
                   
                    if (lastAbbr!='')
                    {
                        if(document.getElementById("" + lastAbbr).abbr == abbr) 
                        {
                                SelectedCell = lastAbbr;
                                document.getElementById("" + lastAbbr).style.backgroundColor = "#43A0CB";
                                lastFlag=true;
                               // return 0;
                        }
                        else
                        {
                                document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                        }
                    }
                    else
                    {
                            document.getElementById("" + SelectedCell).style.backgroundColor = "#43A0CB";
                    }
                }
                if(document.getElementById("" + CellsArray[CellsCounter]).abbr == abbr) 
                {
                 lastAbbr = CellsArray[CellsCounter];
                 
                 }
                
                CellsCounter+=1;
             }  
             document.getElementById("hd_lv").value  =   abbr;  
             SelectWord();  
                       
         }
         if (evt.keyCode ==40)// DOWN Arrow
         {
           ArrowFlag=true;
              SelectionMode  = "V";
              document.getElementById("hf_SelectionMode").value = "V";
             CellsCounter =0;
             abbr = document.getElementById("" + SelectedCell).abbr;
             var lastAbbr;
             lastAbbr='';
             var lastFlag;
             lastFlag=false;
             var lastF;
             lastF=false;
             while (CellsCounter < CellsArray.length-1)
             {
                
                if(document.getElementById("" + CellsArray[CellsCounter]).abbr == abbr) 
                {
                  document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                                      
                }
                else
                {
                   
                    
                    document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "white";
                }

               
                  
                 if (lastFlag==true)
                   {
                    if (lastAbbr!='')
                    {
                        if(document.getElementById("" + CellsArray[CellsCounter]).abbr == abbr && lastFlag==true) 
                        {
                                SelectedCell = CellsArray[CellsCounter];
                               lastFlag=false;
                                lastAbbr='';
                                lastF=true;
                                //return 0;
                        }
                        
                       
                    }
                    
                    
                   } 

                if(CellsArray[CellsCounter] == SelectedCell && lastF==false)
                {
                    lastFlag=true;
                } 
                if(document.getElementById("" + CellsArray[CellsCounter]).abbr == abbr  && lastFlag==true && lastAbbr=='') 
                {
                 lastAbbr = CellsArray[CellsCounter]; 
                            
                }
             
                CellsCounter+=1;
             }     
             document.getElementById("hd_lv").value  =   abbr;  
             document.getElementById("" + SelectedCell).style.backgroundColor = "#43A0CB"; 
             SelectWord();     
                
         }
         var flag;
         var exitFlag;
         var vFlag;
         var CompleteFlag;
         var CorrectFlag;
         vFlag=false;
         flag=false;
         exitFlag=false;
         CompleteFlag=true;
         CorrectFlag=true;
        
            if (ArrowFlag==false)
            {
                 ShowHTMLText("s" + SelectedCell,MyChar[charCode]);
            }
        
            if (MyChar[charCode] !='')
            {
                 CellsCounter =0;
                 while (CellsCounter < CellsArray.length-1)
                 {
                     
                    if (CheckHTMLObj("s" + CellsArray[CellsCounter],''))
                    {
                         CompleteFlag=false;
                    }
                     // Moving the cell
                    if (flag==true)
                    {
                        if (SelectionMode == 'H')
                        {
                            if(document.getElementById("" + CellsArray[CellsCounter]).className == document.getElementById("" + SelectedCell).className) 
                           {
                                 SelectedCell = CellsArray[CellsCounter];
                                 document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                                 flag=false;
                                 exitFlag = true;
                           }
                           else
                           {
                                flag=false; 
                           }
                        }
                        else
                        {
                           if(document.getElementById("" + CellsArray[CellsCounter]).abbr == document.getElementById("" + SelectedCell).abbr) 
                           {
                               
                                 SelectedCell = CellsArray[CellsCounter];
                                 document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#43A0CB";
                                 flag=false;
                                 exitFlag = true;
                                 vFlag=false;
                           }
                           else
                           {
                                vFlag=true;
                                flag=false; 
                           }
                        }
                    }
                    if (CellsArray[CellsCounter] == SelectedCell && exitFlag == false)
                    {      
                         document.getElementById("" + CellsArray[CellsCounter]).style.backgroundColor = "#c4ecff";
                         flag=true;
                    } 
                    if (vFlag==true)
                    {
                        flag=true
                    }
                    
                    CellsCounter+=1;
                 } // End While              
                 
                 if (CompleteFlag==true)
                 {
                    
                    canSelect=false;
                    cd1.CountActive	= false;
                    document.getElementById("btn_showWord").style.display = 'none';
                    CellsCounter =0;
                    while (CellsCounter < CellsArray.length-1)
                    {
                            var CID  = CellsArray[CellsCounter];
                            var CNAME = "C" + CID.substring(5);
                            if (navigator.appName == "Microsoft Internet Explorer")
                            {
                                  if (document.getElementById("s" + CellsArray[CellsCounter]).innerText != document.getElementById(CNAME).value)
                                     {
                                         CorrectFlag=false; 
                                     }
                            }
                            else
                            {
                                   if (document.getElementById("s" + CellsArray[CellsCounter]).textContent != document.getElementById(CNAME).value)
                                     {
                                         CorrectFlag=false;  
                                     }
                            }
                            CellsCounter+=1;
                    }
                 
                 if (CorrectFlag==true)
                    {
                          if (CompleteError==false)
                          {
                              document.aspnetForm.onkeyup = null;
                              document.aspnetForm.onkeydown = null;
                              document.getElementById("tab_playerInfo").style.display = 'block';
                              document.getElementById("tab_help").style.display = 'none';
                              document.getElementById("tab_confirm").style.display = 'none';
                                 ShowHTMLText("spn_count",'مبروك لقد اتممت الحل بنجاح')
                              var blks = document.getElementById("hf_blocks").value;
                              var blksArr = blks.split(',');
                              var counter =0;
                              while (counter < blksArr.length-1)
                                {
                                    ColoredTextC('' + blksArr[counter]);
                                    counter+=1;
                                }
                            }
                            else
                            {
                              document.getElementById('img_wrong').style.display='none';
                              document.aspnetForm.onkeyup = null;
                              document.aspnetForm.onkeydown = null;
                               ShowHTMLText("spn_count",'مبروك لقد اتممت الحل بنجاح')  
                            }
                           
                     }// End if CorrectFlag
                 
                    else
                      {
                        CompleteError = true;
                           document.getElementById('img_wrong').style.display='block';
                            // here show wrong chars
                              
                              ShowHTMLText("spn_count",'بعض الحروف خاطئة يرجى التعديل أو ')
                              var newLink = document.createElement('span');
                              newLink.innerHTML  = "شاهد الحل";
                              
                              if (navigator.appName == "Microsoft Internet Explorer")
                                {
                                    newLink.onclick =  function() {ShowWronChars();};
                                }
                               else
                               {
                                    newLink.setAttribute("onclick","return ShowWronChars();");
                               }
                              newLink.style.color = "#F3C45D";
                              newLink.style.cursor = "pointer";
                              document.getElementById('spn_count').appendChild(newLink);
                              
                       }
                       }// End if Complete
             }//End myChar
             
    }
   else
   {
//         var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
//         var charCase =  String.fromCharCode(charCode);
         
         var text = document.createTextNode("'" + evt.keyCode + "'");
         document.getElementById('txt_name').appendChild(text);
   
   }
    
}
    
    
    function exitCW()
    {
        document.aspnetForm.onkeyup = null;
        document.aspnetForm.onkeydown = null;
       // alert('exit');
        SelectedControl = "txt_name";
        SelectedCell ='';
        return true;
    }
    
    //----------
   
    
    function SelectWord()
    {
         if (navigator.appName == "Microsoft Internet Explorer")
        {
              document.aspnetForm.onkeyup = function() {ProcessEvent(window.event);};
             document.aspnetForm.onkeydown = function() {HandleKeyPress(window.event);};
        }
        else
        {
//                document.aspnetForm.attachEvent("onkeyup","return ProcessEvent(event);")
//                document.aspnetForm.attachEvent("onkeydown","return HandleKeyPress(event);")
        }
         
         if (SelectionMode == "H")
                    {
                        var cname = document.getElementById("" +  SelectedCell).className;
                        var hCount  = document.getElementById("hf_HCount").value;
                        var c;
                        var vCount  = document.getElementById("hf_VCount").value;
                         for (c=0;c<vCount;c++)
                        {
                             document.getElementById("spn_V" + c).style.textDecoration = "none";
                             document.getElementById("spn_V" + c).style.color="black";
                        }
                        for (c=0;c<hCount;c++)
                        {
                             document.getElementById("spn_H" + c).style.color="black";
                             document.getElementById("spn_H" + c).style.textDecoration = "none";
                             
                             if (document.getElementById("spn_H" + c).className == cname)
                             {
                                document.getElementById("spn_H" + c).style.color="red";
                               
                                ShowHTMLObj("spn_question","spn_H" + c);
                                document.getElementById("spn_H" + c).style.textDecoration = "none";
                              }
                          }
                    
                    
                      }// end if SelectionMode
        else
          {
                            var cname = document.getElementById("" + SelectedCell).abbr;
                            var VCount  = document.getElementById("hf_VCount").value;
                            var c;
                            c=0;    
                              var hCount  = document.getElementById("hf_HCount").value;
                             for (c=0;c<hCount;c++)
                             {
                             document.getElementById("spn_H" + c).style.textDecoration = "none";
                             document.getElementById("spn_H" + c).style.color="black";
                             }               
                            for (c=0;c<VCount;c++)
                            {
                                document.getElementById("spn_V" + c).style.color="black";
                                document.getElementById("spn_V" + c).style.textDecoration = "none";
                                if ( document.getElementById("spn_V" + c).className == cname)
                                { 
                                     
                                     ShowHTMLObj("spn_question","spn_V" + c);
                                     document.getElementById("spn_V" + c).style.color="red";
                                     document.getElementById("spn_V" + c).style.textDecoration = "none";
                                }
                             }                     
                      
           }// end ELSE SelectionMode
                
               

                }
    
    
    function ShowHTMLText(obj,data)
    {
         if (navigator.appName == "Microsoft Internet Explorer") 
         {
             document.getElementById(obj).innerText = data;
         }
         else
         {
             document.getElementById(obj).textContent = data;
         }
    
    
    }
    
     function ShowHTMLObj(obj,obj2)
    {
         if (navigator.appName == "Microsoft Internet Explorer") 
         {
             document.getElementById(obj).innerText = document.getElementById(obj2).innerText;
         }
         else
         {
             document.getElementById(obj).textContent = document.getElementById(obj2).textContent;
         }
    
    
    }
    function CheckHTMLObj(obj,data)
    {
         if (navigator.appName == "Microsoft Internet Explorer") 
         {
            return document.getElementById(obj).innerText == data;
                       
         }
         else
         {
            return document.getElementById(obj).textContent == data;
         }
    
    
    }
    
    function SetHTMLStyle(obj,data1,data2)
    {
         if (navigator.appName == "Microsoft Internet Explorer") 
         {
           document.getElementById(obj).style.display = data1;
                       
         }
         else
         {
           document.getElementById(obj).style.display = data2;
         }
    
    
    }
    
    function ShowWronChars()
    {
          
//          document.aspnetForm.onkeyup = null;
//          document.aspnetForm.onkeydown = null;
            CellsCounter =0;
                while (CellsCounter < CellsArray.length-1)
                {
                     var CID  = CellsArray[CellsCounter];
                     var CNAME = "C" + CID.substring(5);
                     
                     if (navigator.appName == "Microsoft Internet Explorer")
                        {
                             if (document.getElementById("s" + CellsArray[CellsCounter]).innerText != document.getElementById(CNAME).value)
                             {
                                 document.getElementById("" + CellsArray[CellsCounter]).style.color = 'red';
                                 document.getElementById("" + CellsArray[CellsCounter]).style.fontWeight = 'bold';
                                 document.getElementById("s" + CellsArray[CellsCounter]).innerText = document.getElementById(CNAME).value;
                             }
                         }
                       else
                       {
                            if (document.getElementById("s" + CellsArray[CellsCounter]).textContent != document.getElementById(CNAME).value)
                             {
                                 document.getElementById("" + CellsArray[CellsCounter]).style.color = 'red';
                                 document.getElementById("" + CellsArray[CellsCounter]).style.fontWeight = 'bold';
                                 document.getElementById("s" + CellsArray[CellsCounter]).textContent = document.getElementById(CNAME).value;
                             }
                       }
                     CellsCounter+=1;
                }        
    
            ShowHTMLText("spn_count",'حظاً أوفر في اللعبة المقبلة')
            return true;
    }
     
                        
      
                       