/**************************************************************************  
 *  Shortcuts.com
 *  (c) 2004-2009 Shortcuts.com Development Team
 *
 *  Methods specific to the Shortcuts.com project, which are used (and reused)
 *  in various pages throughout the site. 
 *
 ****************************************************************************/
 
 var response;
 var menuTimer;
 
 ////////////////////////////////////////////////
// getCardMediaId()
//
//  - Retrieves the saving card ID and initiates image change
/////////////////////////////////////////////////
function getCardMediaId(selectBox)
{
    var numberType = document.getElementById("numberType");
    
    // REMOVE LOGIC TO SET VALUES BACK TO CARD...(PMF 1/15/09)
    
    //Setting retailer ID here...
    var temp =  selectBox[selectBox.selectedIndex].id;
    if((temp != null) && (temp.length >0) && (document.getElementById("loyalty_card_group") != null))
    {
        document.getElementById("loyalty_card_group").value = temp;
    }  
    
    // *REMOVING ALT ID REG (PMF - 3/15/2010)*
    //isAltIdAllowed(temp);
    changeCardImage(selectBox);
}

////////////////////////////////////////////////
// changeCardImage()
//
//  - Resets image, or calls AJAX to make image change
/////////////////////////////////////////////////
function changeCardImage(selectBox)
{
    if (selectBox.selectedIndex == 0)
    {
        var frontImage = document.getElementById("savingsCardFront");
        frontImage.src = "i/generic_front.jpg";
        frontImage.alt = "Savings Card Front Image";
        var backImage  = document.getElementById("savingsCardBack");
        backImage.src  = "i/generic_back.jpg";
        backImage.alt = "Savings Card Back Image";
        return;
    }
    
    var currentSelectedCard = selectBox[selectBox.selectedIndex].value;
    var url = "/myprofile/HelpChangeImage.action";
    var params = "savingsCardId=" + currentSelectedCard;
    myAjax = new Ajax.Request(url, {method: 'post', parameters: params, asynchronous: false, onComplete: updateSavingsCardImage});
}

////////////////////////////////////////////////
// updateSavingsCardImage()
//
//  - Function called on result of AJAX saving 
//    card image check.
/////////////////////////////////////////////////
function updateSavingsCardImage(request)
{
    response = request.responseText.evalJSON();
        
    if (response != null && response != "")
    {
        var cardFrontUrl = response.frontImage;
        var cardBackUrl  = response.backImage;    
        var cardType 	 = response.cardType;

        var frontImage = document.getElementById("savingsCardFront");
        var backImage  = document.getElementById("savingsCardBack");
        
        frontImage.setAttribute("src",cardFrontUrl);
        var imageId = cardBackUrl + '';
        if (imageId.indexOf("null") == -1)   
            backImage.setAttribute("src", cardBackUrl);
        else
            backImage.setAttribute("src", "i/generic_back.jpg");
            
        //Set card help message as needed
        if (document.getElementById("cardHelp") != null)
        {
            var cardHelpText = response.helpCardText;
            if (cardHelpText == null || cardHelpText == "")
            {
                document.getElementById("cardHelp").style.display="none";
            }
            else
            {
                document.getElementById("cardHelp").innerHTML=cardHelpText;
                document.getElementById("cardHelp").style.display="block";
            }
        }
        
        //If card type is phone/pin update input fields
        changeCardInputFields(cardType);
    }
}

var masterType;
function changeCardInputFields(cardTypeIdentifier)
{
	var label1 = document.getElementById("CardNumberInputText");
    var label2 = document.getElementById("ConfirmCardNumberInputText");
    masterType = document.getElementById("loyaltyCardRegType");
    
	if (cardTypeIdentifier == 1)
	{
		masterType = 1;
		label1.innerHTML = "*Phone Number";
        label2.innerHTML = "*PIN";
	}
	else
	{
		masterType = 0;
		label1.innerHTML = "*Card Number";
        label2.innerHTML = "*Confirm Card Number";
	}

}

////////////////////////////////////////////////
// popup()
//
//  - Function which pops up a Green div box to 
//    display message passed.
/////////////////////////////////////////////////
function popup(message)
{
    var toDisplay="<table width=150 bgcolor=#B5DF10><td align=center><font color=#000000 size=1><b>"+message+"</b></font></td></table>";
    document.getElementById("hiddendiv").innerHTML=toDisplay;   
    document.getElementById("hiddendiv").style.display='block';
}

////////////////////////////////////////////////
// popupContent()
//
//  - Gets the hidden div element within the page,
//    assigns HTML to it, and displays it.
/////////////////////////////////////////////////
function popupContent(content)
{
    document.getElementById("hiddendiv").innerHTML=content;   
    document.getElementById("hiddendiv").style.display='block';
}

////////////////////////////////////////////////
// get_mouse()
//
//  - Used to get and set mouse coordinates.
/////////////////////////////////////////////////
function get_mouse(e)
{
    var x,y;
    if (IE) { 
        x = event.clientX+document.documentElement.scrollLeft;
        y = event.clientY+document.documentElement.scrollTop;
    }
    else {  // grab the x-y pos.s if browser is NS
        x = e.pageX;
        y = e.pageY;
    } 

    if (x < 0){x = 0;}
    if (y < 0){y = 0;} 

    document.getElementById("hiddendiv").style.left=(x-70)+"px";
    document.getElementById("hiddendiv").style.top =(y+15)+"px";
    xcoord = x;
    ycoord = y;
} 

////////////////////////////////////////////////
// hidepopup()
//
//  - Resets "hiddendiv" element to be invisible.
/////////////////////////////////////////////////
function hidepopup()
{
    document.getElementById("hiddendiv").style.display="none";
}

////////////////////////////////////////////////
// showCategories()
//
//  - Sets the "categories" div to be visible.
/////////////////////////////////////////////////
function showCategories()
{
    clearTimeout(menuTimer);  
    document.getElementById("otherCatDiv").style.backgroundColor = "#DDEEFF";
    
    var browser = new BrowserObj;
  
    var rowHeight = 30;
    var categoriesObj = document.getElementById("categories");
    // height of flyout
    var divHeight = categoriesObj.clientHeight;
    // y position of "Other Categories"
    var otherCatY = getYCoordinate(document.getElementById("otherCatDiv"));
    
    var minOffset = rowHeight;
    var maxOffset = divHeight - (2 * rowHeight);
    
    // amount of scroll
    var scrollAmount = (browser.client == "IE" ? document.documentElement.scrollTop : window.pageYOffset);

    // start in lowest quarter of the flyout
    var offset = ((divHeight / 4) * 3) + rowHeight;

    //alert("divHeight = " + divHeight + "\notherCatY = " + otherCatY + "\nscrollAmount = " + scrollAmount + "\noffset = " + offset);
    
    if (offset > maxOffset)
        offset = maxOffset;
    
    // if there is any scroll, adjust the offest
    if (scrollAmount > 0)
         offset = offset - scrollAmount;
         
    if (offset < minOffset)
        offset = minOffset;

    //alert("" + (-1 * offset) + "px");
    categoriesObj.style.marginTop = "" + (-1 * offset) + "px";
    categoriesObj.style.visibility= "visible";   
                                
}

function categoryHover(cell)
{
    cell.style.backgroundColor="#DDEEFF";
    cell.style.borderTop="#B5DC10 1px solid";
    cell.style.borderBottom="#B5DC10 1px solid";
}

function categoryOut(cell)
{
    cell.style.backgroundColor="#FFFFFF";
    cell.style.borderTop="#FFFFFF 1px solid";
    cell.style.borderBottom="#FFFFFF 1px solid";
}

////////////////////////////////////////////////
// hideCategories()
//
//  - Sets the "categories" div to be invisible.
/////////////////////////////////////////////////
function hideCategories()
{
    document.getElementById("otherCatDiv").style.backgroundColor = "#FFFFFF";
    menuTimer=setTimeout("document.getElementById('categories').style.visibility='hidden'",500);
}

//****************************************************
//  Sets the background to a greyed disabled look.
//  Or, returns it to normal.
// ***************************************************
function toggleBackground()
{
    var disabledPopup = document.getElementById("backgroundDisabled");
    //Gray out the background
    if (disabledPopup.style.display == "none")
    {
        if(document.body.offsetHeight < document.documentElement.clientHeight)
            disabledPopup.style.height = document.documentElement.clientHeight + "px";
        else
            disabledPopup.style.height = document.body.offsetHeight + "px";
        disabledPopup.style.display = "block";
        backgroundDisabled = true;
    }
    //Bring back background
    else
    {
        disabledPopup.style.display = "none";
        backgroundDisabled = false;
    }
}

// *****************************************************
//  This is a special function for IE, because it has 
//  some #^%$@^ feature that is causing a click on an href
//  not to work!  I am guessing it is a conflict with 
//  jQuery or the DOM layout, but I cannot find exactly what.
//  Time is getting tight, so here we are....good stuff.
// *****************************************************
function navToPageInIE(url)
{
    var browser = new BrowserObj;
    if (browser.client == "IE")
        document.location = url;    
}
