// Add a trim method to the string object
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

// Returns a div reference
function getRefToDiv(divID,oDoc) 
{
	if(document.getElementById) { return document.getElementById(divID); }			
	if(document.all) { return document.all[divID]; }	
	if(!oDoc) { oDoc = document; }

	if(document.layers) 
	{
		if(oDoc.layers[divID]) 
		{
			return oDoc.layers[divID]; 
		} 
		else 
		{
			//repeatedly run through all child layers
			for(var x = 0, y; !y && x < oDoc.layers.length; x++) 
			{
				//on success, return that layer, else return nothing
				y = getRefToDiv(divID,oDoc.layers[x].document); 
			}
			return y; 
		} 
	}
	return false;
}

// Set properties and values to a div
function placeDiv(divID_as_string, prop_as_string, values_as_string, width_biggest_element_as_string) 
{
	//get a reference as above ...
	myReference = getRefToDiv(divID_as_string);
    SetGlobalVars(width_biggest_element_as_string);
	
	var props = new Array();
	var values = new Array();
	
	// Load properties and values in their respective array
	props = prop_as_string.split(",");
	values = values_as_string.split(",");
	
	// Make sure we have a reference
	if(!myReference) 
	{
		//window.alert('Nothing works in this browser');
		return; //don't go any further
	}
	
	// Always set the "left" property
	myReference.style['left'] = leftMargin;
	
	// Loop through the properties and set the appropriate value
	for(var i=0; i<props.length; i++)
	{
	    //if(myReference.style[props[i].trim()] != 'display')
            myReference.style[props[i].trim()] = eval(values[i].trim());
        //else
            //myReference.style[props[i].trim()] = 'none';   
	}
	
	// Always reset the "visibility" after the move is complete
	if(myReference.id != 'palm')
	    myReference.style['visibility'] = 'visible';
}

function ResetBBSTab(divID_as_string, prop_as_string, values_as_string)
{
    myReference = getRefToDiv(divID_as_string);
    
    var props = new Array();
	var values = new Array();
	
	// Load properties and values in their respective array
	props = prop_as_string.split(",");
	values = values_as_string.split(",");
	
	// Make sure we have a reference
	if(!myReference) 
	{
		window.alert('Nothing works in this browser');
		return; //don't go any further
	}
	
	// Loop through the properties and set the appropriate value
	for(var i=0; i<props.length; i++)
		myReference.style[props[i].trim()] = eval(values[i].trim());
}

function NavigateUrl(subjectID)
{
    ResetBBSTab('tab1', 'zIndex', '10');
    ResetBBSTab('tab2', 'zIndex', '10');
    ResetBBSTab('tab3', 'zIndex', '10');
    ResetBBSTab('tab4', 'zIndex', '10');
    ResetBBSTab('tab5', 'zIndex', '10');
    ResetBBSTab('tab6', 'zIndex', '10');
    ResetBBSTab('tab7', 'zIndex', '10');
    window.location = 'bbs_threads.aspx?subject='+subjectID;
}

function MoveTabUp(divID_as_string){
    myReference = getRefToDiv(divID_as_string);
    // Make sure it matches the zIndex that is set in bbs_masterpage
    if(myReference.style['zIndex'] == '100') return;
    myReference.style['cursor'] = 'hand';
    // use the top position defined in the styles.css -4px
    myReference.style['top'] = '157px'; 
}

function MoveTabDown(divID_as_string){
    myReference = getRefToDiv(divID_as_string);
    myReference.style['cursor'] = 'hand';
    // use the top position defined in the styles.css 
    myReference.style['top'] = '161px'; 
}


function SwapImage(imageId, imagePath){
    var ctrl = imageId;
    var imageSrc = imagePath;
    var ref = eval('document.images.'+ctrl);
    if(ref)
        ref.src = imageSrc;
}

function SetGlobalVars(elementWidth)
{
	if(parseInt(navigator.appVersion)>3) 
	{
		if(navigator.appName=="Netscape") 
		{
			// Netscape syntax
			//
			// Window width
			winW = window.innerWidth;
			// Window height
			winH = window.innerHeight;
		}
		if(navigator.appName.indexOf("Microsoft")!=-1) 
		{
			//IE syntax
			//
			// Window width
			winW = document.body.offsetWidth;
			// Window height
			winH = document.body.offsetHeight;
		}
	}
	
	// Calculate page margin
    menuW = elementWidth;//692;//984; // need to match the value set in WriteFlashMenu()
    leftMargin = (winW-menuW)/2;
}

// The method HidePalm is called from flash.
// In this case I choosed to hide the div containg the flash. 
// It gives the impression that the flash has been closed
function HidePalm(){
    ShowSelectTags();
    var palm = document.getElementById('palm');
    palm.style['display'] = 'none';
}

function ShowPalm(){
    HideSelectTags();
    var palm = document.getElementById('palm');
    palm.style['display'] = 'block';
}

function HideSelectTags()
{
    var theForm = document.forms[0]
    for(i=0; i<theForm.elements.length; i++)
    {
      if(theForm.elements[i].type == "select-one")
        theForm.elements[i].style['display'] = 'none';
    }
}

function HideVideo()
{
    promo_form_layover.style['display'] = 'none';
}

function ShowVideo()
{
    promo_form_layover.style['display'] = 'block';
}

// Problem with the bday ddl's
function ShowSelectTags()
{
    var theForm = document.forms[0]
    for(i=0; i<theForm.elements.length; i++)
    {
      if(theForm.elements[i].style['display'] == 'none')
        theForm.elements[i].style['display'] = 'inline';
    }
}
 
// Set the dimensions of the flash to be 100%, no scale (prevent the palm itself from getting bigger)
// NOTE: In order for the flash to take 100% of the screen (including what may be beyond what you see - when scrolling is needed), 
// we must set the width and height of the div that holds the flash with document.body.scrollWidth and document.body.scrollHeight

/* Messaging scripts */

/* Read */
function confirmation(strPrompt,target)
{
if (confirm(strPrompt))
    {
	    location.href=target;
    } else {
	    return;
    }
}

/* Inbox */
function doSelection_ORIG(gridId, checked, itemId)
{
    var chkBox;
    var i = 3;
    
    if (i < 10)
        i = "0" + i;
    
    chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl"+ i + "_" + itemId);
    while (chkBox != null && chkBox != "undefined")
    {
        chkBox.checked = checked;
        ++i;
        
        if (i < 10)
            i = "0" + i;
            
        chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i +"_" + itemId);
    }
}

function doSelection(gridId, checked, itemId)
{
    var chkBox;
    var i = 3;
    
    if (i < 10)
        i = "0" + i;
    
    chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl"+ i + "_" + itemId);
    while (chkBox != null && chkBox != "undefined")
    {
        chkBox.checked = checked;
        ++i;
        
        if (i < 10)
            i = "0" + i;
            
        chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i +"_" + itemId);
    }
}

function getChecked(gridId, itemId, targetURL)
{
    var chkBox;
    var messID;
    var i = 3;
    var retval;
             
    retval = "";

    if (i < 10)
        i = "0" + i;
    
    chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_" + itemId);
    messID = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_messID");
    while (chkBox != null && chkBox != "undefined")
    {
        if (chkBox.checked == true)
        {
            retval += messID.value + "-";
        }
        ++i;
        if (i < 10)
            i = "0" + i;

        chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_" + itemId);
        messID = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_messID");
    }

    if (retval.length > 0)        
        window.location = targetURL + retval;
}

function getChecked_ORIG(gridId, itemId, targetURL)
{
    var chkBox;
    var messID;
    var i = 3;
    var retval;
             
    retval = "";

    if (i < 10)
        i = "0" + i;
    
    chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_" + itemId);
    messID = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_messID");
    while (chkBox != null && chkBox != "undefined")
    {
        if (chkBox.checked == true)
        {
            retval += messID.innerText + "-";
        }
        ++i;
        if (i < 10)
            i = "0" + i;

        chkBox = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_" + itemId);
        messID = document.getElementById("ctl00_ContentPlaceHolder_Msg_Inbox1_" + gridId + "_ctl" + i + "_messID");
    }

    if (retval.length > 0)        
        window.location = targetURL + retval;
}

function OpenGiftCard(){
    var win = window.open("gift_card.aspx", "", "width=500, height=250");
}

function OpenMusicPlus(){
    var win = window.open("http://www.dynamite.ca/garage/VJ_FR/home.html", "", "width=1000, height=815");
}

function Music_fr(){
    window.open("RadioObj/Radio%20Fr/index.html", "", "location=0,status=0,scrollbars=0,width=400,height=228");
}

function Music_en(){  
    window.open("RadioObj/Radio%20En/index.html", "", "location=0,status=0,scrollbars=0,width=400,height=228");
}

function GaragePopUp(url_as_string, param_as_string){
	if(url_as_string != "")
	{
        window.open(url_as_string, "", param_as_string);
	}
}

function checklogin(targetURL)
{
	var loggedInElt = document.getElementById("ctl00_LoggedIn");
	var message = document.getElementById("ctl00_messErrNotLogged");
				
	if(loggedInElt != null && loggedInElt.value == "False")
		alert(message.value);
	else
		location.href = targetURL;
}

function productSubDepartmentChange(obj, target)
{   
    target += "?dept=";
    target += obj.value;
    
    // if no subdept we do nothing.
    if (target.indexOf("-") < 0)
        return;
    
    location.replace(target);
}

function ClosingSection()
{
    location.href= 'http://www.garage.ca/announcement.aspx';
}

function fbs_click(productId)
{
    u=location.href;
    t=document.title;

    window.open('http://www.facebook.com/sharer.php?u='+ encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');

    return false;
}
    