// Function that replaces the element source (typically an image)
// with the given imag path
function noHeroProductImage(element, imgPath) 
{
    (element.src) = imgPath;
}

function goImgWin(url) {
    var myHeight = 524;
    var myWidth = 524;
    TheImgWin = window.open(url,null,'height='+ myHeight +',width='+ myWidth +',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
    TheImgWin.focus();
}

function NewWindow(url, width, height) {
    TheImgWin = window.open(url,null,'height=' + height +',width='+ width +',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
    TheImgWin.focus();
}


// Move an element directly on top of another element (and optionally
// make it the same size)
function Cover(bottom, top, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    top.style.top = location.y + 'px';
    top.style.left = location.x + 'px';
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}

// Global array variable to keep track of which gift box text box fields are in focus.
var giftMessageInputFields = null;

// Class to hold the current id and the 'next' id
function InputIds ( current, prev, next )
{
    this.current = current;
    this.next = next;
    this.prev = prev;
}

// Sets the value indicating a gift message text box is focused or not.
function SetGiftMessageInputField(index, currentInputId, prevInputId, nextInputId)
{    
    if( giftMessageInputFields == null )
        giftMessageInputFields = new Array(null, null, null, null, null, null);

    if( currentInputId == null )
        giftMessageInputFields[index] = null;
    else
        giftMessageInputFields[index] = new InputIds(currentInputId, prevInputId, nextInputId);
}

// Returns true if any of the gift message textbox is in focus.
function GiftMessageIsInFocus()
{
    for (index in giftMessageInputFields)
    {
        if( giftMessageInputFields[index] != null)
            return giftMessageInputFields[index];
    }
    
    return null;
}

// Handles the keypress event for some of the checkout views to suppress 'enter' key while editing the message.
function DefaultSubmitHandler(e, defaultButton) {

    if( !e )
        e = window.event;

    var key = e.keyCode ? e.keyCode : e.which;

    var inputField = GiftMessageIsInFocus();
    if( inputField != null ){
        if( key == 13 ){
            var elem =  document.getElementsByName(inputField.next.name)[0];
            elem.focus();       
            elem.value = elem.value;
            return false;
        }

        return true;
    }
    else{
        return WebForm_FireDefaultButton(e, defaultButton);
    }
}

function IsLastCharALetter( inputField)
{
    var lastChar = inputField.current.value.charAt(inputField.current.value.length-1);
             
    return (lastChar >= 'A' && lastChar <= 'Z' || lastChar >= 'a' && lastChar <= 'z' )
}

function KeyUpHandler(e)
{
    var LINE_MAX = 40;
    
    if( !e )
        e = window.event;

    var key = e.keyCode ? e.keyCode : e.which;

    var inputField = GiftMessageIsInFocus();
    if( inputField != null )
    {
        if( key == 8 && inputField.current.value.length == 0 )   // back key when the field is empty
        {
            var elem =  document.getElementsByName(inputField.prev.name)[0];
            elem.focus();
            elem.value = elem.value;
            return false;
        }
        else{        
            // for the 40th key, go to the next textbox.  This '40' could be set by the app as well.
            if( inputField.current.value.length == LINE_MAX 
            && ((key >= 48 && key <= 90) || key == 32 || key == 92 || (key >= 96 && key <= 111) || (key >= 186 && key <= 192) || (key >= 219 && key <= 222)) 
            ){
                
                var elem =  document.getElementsByName(inputField.next.name)[0];
                
                if (supportsCaretPositioning(elem))
                {
                    var buffer = "";
                    
                    // if last character in last line is a letter, move the word to next line
                    if (IsLastCharALetter(inputField))
                    {
                        
                        // while we cannot find a punctuation character
                        // and we don't get to the beginning of the field,
                        // and the buffer is not as big as the max length of a row
                        while (IsLastCharALetter(inputField) && inputField.current.value.length >= 0 && buffer.length < LINE_MAX)
                        {
                            buffer = inputField.current.value.charAt(inputField.current.value.length-1) + buffer;
                            inputField.current.value = inputField.current.value.substring(0, inputField.current.value.length-1);
                        }
                        
                        if (buffer.length >= LINE_MAX)
                        {
                            inputField.current.value = buffer;
                            buffer = "";
                        }
                        
                    }
                    
                    if (buffer.length > 0 && elem != null)
                    {
                        elem.value = buffer;
                    }
                    
                    elem.focus();                    
                    setCaretTo(elem, buffer.length);     
                    
                }
            }
            
            return true;
        }       
    }
    
    return true;
}

function supportsCaretPositioning(obj)
{
    return obj.createTextRange != null || obj.selectionStart != null;
}

function setCaretTo(obj, pos) {   
    if(obj.createTextRange != null) {   
        /* Create a TextRange, set the internal pointer to  
           a specified position and show the cursor at this  
           position  
        */  
        var range = obj.createTextRange();   
        range.move("character", pos);   
        range.select();   
    } else if(obj.selectionStart != null) {   
        /* Gecko is a little bit shorter on that. Simply  
           focus the element and set the selection to a  
           specified position  
        */   
        obj.setSelectionRange(pos, pos);   
    }   
}  


 function RemoveItemHandler(sku, quantity, price)
{
    var quantityElem = document.getElementById(quantity);
}

function setMainProductImage( imageId, linkImageId, filepath, productZoomViewUrl )
{
    var image = document.getElementById(imageId);
    
    
    
    if (image != null)
    {

        image.src = filepath;    
        image.onclick = function() { goImgWin(productZoomViewUrl); return false; }
    }
    
    var linkImage = document.getElementById(linkImageId);
    
    if (linkImage)
    {
        linkImage.onclick = function() { goImgWin(productZoomViewUrl); return false;}
    }
    
    

}
function setMainProductImageAndPopupURL( imageId, linkImageId, filepath, productZoomViewUrl, 
ektronOrRedirectURL, title, windowHeight, windowWidth, strIsScrollable, strIsNavigable )
{
    var image = document.getElementById(imageId);
    
    //window.open(ektronOrRedirectURL, null, 'height='+ windowHeight +',width='+ windowWidth +',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
    NewWindow(ektronOrRedirectURL, windowWidth, windowHeight, strIsScrollable, strIsNavigable);

    if (image != null)
    {

        image.src = filepath;    
        image.onclick = function() { NewWindow(ektronOrRedirectURL, windowWidth, windowHeight, strIsScrollable, strIsNavigable);  return false; }
    }
    
    var linkImage = document.getElementById(linkImageId);
    
    if (linkImage)
    {
        linkImage.onclick = function() { goImgWin(productZoomViewUrl); return false;}
    }
    
}


function NewWindow(url, width, height, strIsScrollable, strIsNavigable) {
    
    // if window is navigable, then it's just a regular new window as it would happen with a target blank
    if (strIsNavigable == '1')
    {
        window.open(url);
    }
    else
    {
        TheImgWin = window.open(url,null,'height=' + height +',width='+ width +',toolbar=no,directories=no,status=no,menubar=no,scrollbars='+ strIsScrollable +',resizable=no,location=' + strIsNavigable);
        TheImgWin.focus();
    }
}
function autoSelectNewBilling( id)
{
    var rdo = $get(id);
    if (rdo)
        rdo.checked = true;
}

//Asserts
function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
    return 'AssertException: ' + this.message;
};

function assert(exp, message) {
    if (!exp) {
        throw new AssertException(message);
    }
}
   
// Set slideShowSpeed (milliseconds)
//var slideShowSpeed = 5000;
//// Duration of crossfade (seconds)
//var crossFadeDuration = 2;
//// Specify the image files
//var Pic = new Array();
//// to add more images, just continue
//// the pattern, adding to the array below
////Pic[0] = '/common/uploadedImages/homepageimage0421.jpg'
//Pic[0] = '/common/uploadedimages/new_features/080509/rotate_gourmet_coffee.jpg'
//Pic[1] = '/common/uploadedimages/new_features/080509/rotate1.jpg'
//Pic[2] = '/common/uploadedimages/new_features/080509/rotate2.jpg'
//Pic[3] = '/common/uploadedimages/new_features/080509/rotate3.jpg'
//Pic[4] = '/common/uploadedimages/new_features/080509/rotate4.jpg'

//var PicURL = new Array();
//var PicTarget = new Array();
//// to add more images, just continue
//// the pattern, adding to the array below
////PicURL[0] = '/new-and-seasonal/mothers-day-treats.aspx?refcode=flink'
//PicURL[0] = 'http://www.deandeluca.com/coffee-tea-cocoa/coffee-by-type.aspx?PageIndex=0&PageSize=-1&refcode=flink'
//PicTarget[0]= '_self'
//PicURL[1] = 'http://www.deandeluca.com/common/email/lobster-bake/lobster-bake.htm'
//PicTarget[1]= '_blank'
//PicURL[2] = 'http://www.deandeluca.com/common/email/lobster-bake/lobster-bake.htm'
//PicTarget[2]= '_blank'
//PicURL[3] = 'http://www.deandeluca.com/gifts/julie-and-julia-basket.aspx?refcode=flink'
//PicTarget[3]= '_self'
//PicURL[4] = 'http://www.deandeluca.com/juliejulia/index.htm'
//PicTarget[4]= '_blank'

//// do not edit anything below this line
//var t;
//var j = 0;
//var p = Pic.length;
//var preLoad = new Array();
//var preLoadURL = new Array();
//for (i = 0; i < p; i++) {
//preLoad[i] = new Image();
//preLoad[i].src = Pic[i];
//preLoadURL[i] = PicURL[i];
//}
//function runSlideShow() {
///* jowens 07072011 not sure when this was working, but it doesn't appear to be working now
//    there is no document.images.SlideShow object */

////if (document.all) {
////document.images.SlideShow.style.filter="blendTrans(duration=2)";
////document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)";
////document.images.SlideShow.filters.blendTrans.Apply();
////}
////document.images.SlideShow.src = preLoad[j].src;
////document.getElementById("SlideShowLink").target=PicTarget[j];
////document.getElementById("SlideShowLink").href = preLoadURL[j];
//// 
////if (document.all) {
////document.images.SlideShow.filters.blendTrans.Play();
////}
////j = j + 1;
////if (j > (p - 1)) j = 0;
////t = setTimeout('runSlideShow()', slideShowSpeed);
//}
 
 
// recipe pop
//function MM_openBrWindow(theURL,winName,features) { //v2.0
//  window.open(theURL,winName,features);
//}
