var status = "shown"; //first status of slider (needs to have start of 150 is hidden and 0 if shown)
var start = 0; //start right value
var speed = 20; //speed of slide
var quotes = new Array();
var Random = 0;
var RandomSelected = "";
var NextSlot = 1;
var SearchWord;

quotes[0] = "<span class='rightGreen'>Up to three months <span class='rightBlue'>free</span> death cover</span><br /><span class='rightSmallBlue'>(even before you take transfer of your property)</span>";
quotes[1] = "<span class='rightGreen'>Secure your home loan debt in the event of your death or disablement</span>";
quotes[2] = "<span class='rightGreen'>Up to R1,500 000 cover.</span> <span class='rightBlue'>No medicals or HIV test required</span>";
quotes[3] = "<span class='rightGreen'>You have 30 days to review your policy</span><br /><span class='rightSmallBlue'>(if you are not entirely happy you may cancel it)</span>";


function getRightQuotes()
{        
    for(var i = 1; i <= 4; i++)
    {                
        do
        {        
            if(RandomSelected.indexOf("," + Random) < 0)
            {            
                eval("document.getElementById('Slot" + i + "')").innerHTML = quotes[Random];
                NextSlot++;
                RandomSelected += "," + Random;
            }
            else
            {
                i = i - 1;
            }
        }
        while(CheckRandom(Random))
    }
}

function CheckRandom(rnd)
{
    if(RandomSelected.indexOf("," + rnd) < 0)
    {
        RandomSelected += "," + rnd;
        return true;
    }
    else
    {
        Random = Math.floor(Math.random() * quotes.length);
        return false;
    }
}

function getRandomBlock()
{
    var rndom = Math.floor(Math.random() * quotes.length);
    document.getElementById('RandomBlock').innerHTML = quotes[rndom];
    
    setTimeout("getRandomBlock()", 5000);
}

function getOneRandomQuote()
{
    var rndom = Math.floor(Math.random() * quotes.length);
    document.getElementById('RandomBlock').innerHTML = quotes[rndom];
}

        
function showInfo()
{
    if(status == "hidden")
    {
        if(start > 0)
        {
            start = start - 10;
            document.getElementById('slider').style.right = start + "px";
            setTimeout("showInfo()",speed);
        }
        else
        {
            status = "shown";
        }
    }
    else if(status == "shown")
    {
        if(start < 150)
        {
            start = start + 10;
            document.getElementById('slider').style.right = start + "px";
            setTimeout("showInfo()",speed);
        }
        else
        {
            status = "hidden";
        }
    }
}
function showCallMeForm()
{
    if(document.getElementById('CallMeForm').style.display == "")
    {
        document.getElementById('CallMeForm').style.display = "none";
    }
    else
    {
        document.getElementById('CallMeForm').style.display = "";
    }
}

var occupationTextBoxId = "";
var occupationSelected = 0;
function Complete(results)

{
    document.getElementById("resultsData").innerHTML = "";
    //Getting the search word from the result string
    var CharIndex = results.toString().indexOf("$",0);
    SearchWord = results.toString().substring(CharIndex + 1,results.toString().length);
    var RemoveSearchWord = results.toString().substring(0,CharIndex);
    //Removing the search word from the result string
    results = RemoveSearchWord;
    items = results.split(";");
    for(var i = 0; i < items.length;i++)
    {
        var childNode = document.createElement("DIV");
        var textVal = items[i];
        Font = "";
        //Changing the font color of the letter searched in the result set
        //var Font = "<FONT COLOR=red>" + SearchWord + "</FONT>";
        childNode.innerHTML = textVal;
        childNode.className = "DivItem";
        childNode.onclick = function()
        {
            SetValue(this,"onclick");
        };
        childNode.onmouseover = function()
        {
            SetValue(this,"onmouseover");
            this.className = "DivItemHover";
        };
        childNode.onmouseout = function()
        {
            //SetValue(this,"onmouseout");
            this.className = "DivItem";
        };
        document.getElementById("resultsData").appendChild(childNode);
        
        //Hightlighting the searched letters
        if((!(textVal == "")) || ((!textVal == null)) || (textVal.length > 0))
        {
            SearchWord =  document.getElementById("resultsData").childNodes.item(i).innerHTML.toString().match(new RegExp(SearchWord,"i"));
            Font =  "<FONT COLOR=red>" + document.getElementById("resultsData").childNodes.item(i).innerHTML.toString().match(new RegExp(SearchWord,"i"))  + "</FONT>";
            document.getElementById("resultsData").childNodes.item(i).innerHTML = document.getElementById("resultsData").childNodes.item(i).innerHTML.toString().replace(SearchWord,Font);
        }else
        {
            document.getElementById("resultsData").childNodes.item(i).innerHTML = "Search word not found";
        }
    }
}

function SetValue(object,EventType)
{   
    //Matching the lower and upper case of the letters
    SearchWord =  object.innerHTML.toString().match(new RegExp(SearchWord,"i"));
    //Removing the html tag <FONT> for display on the occupation textbox
    var ClearTags = "<FONT color=red>" + SearchWord + "</FONT>";
    var DisplayOnField = object.innerHTML.toString().replace(ClearTags,SearchWord);
    if (object.innerHTML.indexOf("<font color=\"red\">") != -1)
    {
        ClearTags = "<font color=\"red\">" + SearchWord + "</font>";
        DisplayOnField = object.innerHTML.toString().replace(ClearTags,SearchWord);
    }
    document.getElementById(occupationTextBoxId).value = DisplayOnField;
    //Checking the type of event and setting the property of the div according to the event
    if(EventType == "onmouseover")
    {
        SelectState("block")
    }
    if(EventType == "onmouseout")
    {
        SelectState("none");
    }
    else if(EventType == "onclick")
    {
        SelectState("none")
    };
    occupationSelected = 1;
}
function SelectState(state)
{
    document.getElementById("resultsData").style.display = state;
}
function  ScrollControl(Key,Count)
{
//For scrolling up and down the div tag
    if(document.getElementById("resultsData").childNodes.length >= Count)
    {
         //UP
        if(Key == "38")
        {
        if(navigator.appName == "Microsoft Internet Explorer")   
            {
                if(Count > 0)
                {
                    //Highlight the background and change the wording on the search textbox
                    DeSelect(Count);
                    document.getElementById("resultsData").childNodes.item(Count = Count - 1).className = "DivItemHover";
                    document.getElementById(occupationTextBoxId).value = document.getElementById("resultsData").childNodes.item(Count).innerText;
                    document.getElementById("resultsData").childNodes.item(Count).focus();
                    occupationSelected = 1;
                }else
                {
                     document.getElementById(occupationTextBoxId).value = SearchWord;
                     document.getElementById(occupationTextBoxId).focus();
                     occupationSelected = 1;
                }
            }
          
        }
        //DOWN
        else if(Key == "40")
        {
            if(navigator.appName == "Microsoft Internet Explorer")   
            {
                if(Count < document.getElementById("resultsData").childNodes.length)
                {
                    //Highlight the background and change the wording on the search textbox
                    DeSelect(Count);
                    document.getElementById("resultsData").childNodes.item(Count).className = "DivItemHover";
                    document.getElementById(occupationTextBoxId).value = document.getElementById("resultsData").childNodes.item(Count).innerText;
                    document.getElementById("resultsData").childNodes.item(Count).focus();
                    occupationSelected = 1;
                }
            }
        }
        //ENTER
        else if(Key == "13")
        {   
            //setting starting index
            var StartIndex = 0;
            if(Count == 0)
            {
                StartIndex = 0;
            }else
            {
                StartIndex = StartIndex - 1;
            }
            //document.getElementById(occupationTextBoxId).value = document.getElementById("resultsData").childNodes.item(StartIndex).innerText;
            SelectState("none");
            document.getElementById(occupationTextBoxId).focus();
            document.forms.
            occupationSelected = 1;
        }
        //TAB
        else if(Key == 9)
        {
            //setting starting index
             var StartIndexTab = 0;
            if(Count == 0)
            {
                StartIndexTab = 0;
            }else
            {
                StartIndexTab = Count - 1;
            }
            //document.getElementById(occupationTextBoxId).value = document.getElementById("resultsData").childNodes.item(StartIndexTab).innerText;
            SelectState("none");
            document.getElementById(occupationTextBoxId).focus()
            occupationSelected = 1;
        }
        //BACKSPACE
        else if(Key == 8)
        {
            document.getElementById(occupationTextBoxId).focus()
        }
        

    }
}
function DeSelect(Count)
{
    //de-selecting highlighted items
    for(var i = 0;i < document.getElementById("resultsData").childNodes.length;i++)
    {
        document.getElementById("resultsData").childNodes.item(i).className = "DivItem";        
    }
}