/*
File: utility.js
Purpose: Javascript File to contain functions used throughout the site
Created: 05/05/2008
*/

//Globals


/*Function to randomly choose text to display in a given div
Params: 
    iNumTextBlocks - number of text blocks to choose randomly
    aText - array of text blocks to choose from
    divContainer - containing div or element
*/
function RandomText( iNumTextBlocks, aText, objContainer )
{
    //number of elements in the array
    var iElements = aText.length;
    //get the object to set the text to
    var obj = document.getElementById(objContainer);
    //need a string to contain the text
    var sTextBlocks = "";
    //need to check if there are enough blocks to choose from
    if ( iElements < iNumTextBlocks )
    {
        //since there are not enough we can just set the ones there are
        for (i=0; i<iElements; i++)
        {
            sTextBlocks = sTextBlocks + "<p>" + aText[i] + "</p>";
        }
    }
    else
    {
        var iRand = 0;
        var iTries = 0;
        var iLimit = 100;
        var bDone = false;
        var iUsedCnt = 0;
        //create array for used values
        var aUsedArray = new Array(iNumTextBlocks);
        
        while( !bDone && iTries < iLimit )
        {
            //need random number
            iRand = Math.floor(Math.random()*iElements);
            //only use the 
            if (!ArrayContains( aUsedArray, aText[iRand] ))
            {
                //add random to used array
                aUsedArray[iUsedCnt] = aText[iRand];
                //add this text block to the string
                sTextBlocks = sTextBlocks + "<p>" + aText[iRand] + "</p>";
                iUsedCnt++;
            }
            //need to check if there are enough blocks
            if (iUsedCnt >= iNumTextBlocks)
            {
                bDone = true;
            }
            //add one to the number of times tried
            iTries++;
        } 
    }
    //dump the information to the page
    document.write(sTextBlocks);
}
//shows thd dealer register form
function showDRForm()
{
    document.getElementById("drForm").style.display = "block";
}

/*
Method to find a match in an array
Maybe this should be prototyped for the array object but for now 
it will stay as a method
*/
function ArrayContains( aSource, sToFind )
{
    var i = 0;
    if (aSource != null)
    {
        for (i=0; i<aSource.length; i++)
        {
            if (aSource[i] == sToFind)
            {
                return true;
            }
        }
    }   
    return false;
}

function showDLForm()
{
    document.getElementById("dlForm").style.display = "block";
    
}

function closeDLForm()
{
    document.getElementById("dlForm").style.display = "none";
    document.getElementById("ctl00_MainContent_lblChooseRegion").style.display = "none";
}

function showCAResults()
{
    document.getElementById("ctl00_MainContent_dCADealerInfo").style.display = "block";
    document.getElementById("ctl00_MainContent_dUSDealerInfo").style.display = "none";
    document.getElementById("ctl00_MainContent_dIntDealerInfo").style.display = "none";
    document.getElementById("ctl00_MainContent_lblChooseRegion").style.display = "none";
}

function showIntResults()
{
    document.getElementById("ctl00_MainContent_dIntDealerInfo").style.display = "block";
    document.getElementById("ctl00_MainContent_dCADealerInfo").style.display = "none";
    document.getElementById("ctl00_MainContent_dUSDealerInfo").style.display = "none";
    document.getElementById("ctl00_MainContent_lblChooseRegion").style.display = "none";
}
//---------Promotion functions--------------------------------------
function showCanPromo()
{
    document.getElementById("dealerinfo_canada").style.display = "block";
    document.getElementById("dealerinfo_usa").style.display = "none";
    document.getElementById("dealerinfo_international").style.display = "none";
    document.getElementById("plsClick").style.display = "none";
    
}
function showIntPromo()
{
    document.getElementById("dealerinfo_canada").style.display = "none";
    document.getElementById("dealerinfo_usa").style.display = "none";
    document.getElementById("dealerinfo_international").style.display = "block";
    document.getElementById("plsClick").style.display = "none";
}
function showUsaPromo()
{
    document.getElementById("dealerinfo_canada").style.display = "none";
    document.getElementById("dealerinfo_usa").style.display = "block";
    document.getElementById("dealerinfo_international").style.display = "none";
    document.getElementById("plsClick").style.display = "none";
}
//----------------------------------------------------

/*
This function swaps out an image with another
*/
function changeImage(imgNew, imgLoc, imgID)
{
    var sImagePath = "http://media.coverall.net/images/" + imgLoc;
    if (document.images) 
    {
        document[imgID].src = sImagePath + imgNew;
    }
}

/*
    Sets a cookie
*/
function setCookie(c_name,value,expiredays)
{
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}




/*
    forces a click on the correct object when the enter
    keys is pressed
*/
function doClick(buttonName,e)
{
    var key;

    if(window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13)
    {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null)
        { //If we find the button click it
        btn.click();
        event.keyCode = 0
        }
    }
}

