﻿String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
var helpShown = false;
		
function countChars(controlName, txtControl, helpDivID, imagePath)
{
	var txtBox = document.getElementById(controlName + '_' + txtControl);
	var strLines = txtBox.value.split("\r");
	var boolLongFound = false;
	for (var i = 0; i < strLines.length; i++)
	{
		if (strLines[i].length > 45)
		{
			boolLongFound = true;
			showHelp(helpDivID, imagePath);
		}
	}
	
	if (!boolLongFound && helpShown)
	{
		hideHelp(helpDivID);
	}
}

function startHelp()
{
	changeOpac(0, 'quantityHelp');
	setTimeout( 'if ( !helpShown ) showHelp( \'quantityHelp\', \'images/customize/popup_enter_quantity.gif\' );', 500 );
}

function showHelp( divId, img )
{
	if (helpShown)
	{
		return;	
	}

	var helpDiv = document.getElementById( divId );
	
	//alert("ShowHelp DivID: " + divId + " found: " + helpDiv);
	
	//helpShown = true;
	helpDiv.innerHTML = '<img src=\"' + img + '\" />';
	opacity(divId, 0, 100, 500);
	setTimeout( 'if ( helpShown ) hideHelp( \'' + divId + '\' );', 7000 );
}

function hideHelp( divId )
{
    var helpDiv = document.getElementById(divId).style; 
    
    //alert("HideHelp DivID: " + divId + " found: " + helpDiv);
    
	if (helpDiv.opacity == "0")
		return;
			
	if (helpShown)
	{
		//helpShown = false;
		opacity(divId, 100, 0, 500);
	}
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    helpShown = (object.opacity != "0");
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
    if (opacity == 0)
    {
		object.display = 'none';
    }
    else if (object.display == 'none')
    {
		object.display = '';
    }
} 

function RoundQty( t )
{
	var nQty = parseInt(t.value, 10);
	
	if (nQty == 0)
	{
		setTimeout("showHelp( 'quantityHelp', 'images/customize/popup_quantity_rounded.gif' );", 150);
	}
	
	if( (nQty % 25) != 0)
	{
		setTimeout("showHelp( 'quantityHelp', 'images/customize/popup_quantity_rounded.gif' );", 150);
	}
}

function clearDefaultText(textbox, defaultText)
{
    if(typeof editingPersonalization != 'undefined' && textbox.getAttribute('RequiresPreview') == null ) {
        editingPersonalization(textbox, true);
    }
    textbox.setAttribute("oldValue", textbox.value);
	if (textbox.value == defaultText) { textbox.value = ''; }
	
}

function setDefaultText(textbox, defaultText)
{
	if (textbox.value == '')
	{
		var command = "document.getElementById('" + textbox.id + "').value = '" + defaultText + "'";
		setTimeout(command, 100);		
		if(textbox.getAttribute('oldValue') == defaultText) { 
		    editingPersonalization(textbox, false);
		}
	}	
	if((typeof editingPersonalization != 'undefined' )  != 'undefined' && textbox.value == textbox.getAttribute('oldValue'))
	{ 
	    editingPersonalization(textbox, false);
	}	
}

function quantityKeyPress(control)
{
    if(control.which || control.keyCode)
    {
        if ((control.event.which == 13) || (control.keyCode == 13)) 
        {
            control.blur();
            return false;
        }
        
        return true;
    }
}

function showHelpWindow(windowName)
{
   
    var oWnd = radopen(null, windowName);
   
    oWnd.setUrl(oWnd.get_navigateUrl());
    // freeze position 
	if (!oWnd.IsPinned()) oWnd.TogglePin()  
}


function showExternalWindow(url)
{	
	var oWindow = window.radopen(url, null);
	oWindow.SetSize(560, 560);
	var theTop = 40;
	if (document.documentElement && document.documentElement.scrollTop)
	{
		theTop += document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		theTop += document.body.scrollTop;
	}
	oWindow.MoveTo(40, theTop);
	// freeze position 	
	if(document.body.clientWidth > 800 && document.body.clientHeight> 600 
    && !oWindow.isPinned()) {
	    oWindow.togglePin();
    }
	return oWindow;
}

function wb_start(gVersion)
{
   //NOTE: this is called from footer, but not implemented in new pages.
}

function BM()
{
	if(window.external!=null)
	{
		window.external.AddFavorite(location.href, document.title);		
	}
}

function UpdateSuggestedText(outerControl, destinationTxtBox)
{
    var el;
    var txtBox;
    
    if (document.getElementById)
    {
        el = document.getElementById(outerControl + '_btnCustomTextUpdate');
        txtBox = document.getElementById(outerControl + '_' + destinationTxtBox);
    }
    else 
    {
        if (document.all)
        {
            el = document.all[outerControl + '_btnCustomTextUpdate'];
            txtBox = document.all[outerControl + '_' + destinationTxtBox];
        }
    }

    //alert('outerControl: ' + outerControl + ' -- destinationTxtBox: ' + destinationTxtBox);
    //alert('Value: ' + txtBox.value);
    //alert('el should be : ' + outerControl + '_btnCustomTextUpdate' + ' ::: ' + el);
    
    try
    {
        if(el)
        {
            var replace = true;
           
            // prompt user to confirm the change of text if necessary
            if(txtBox && txtBox.value.length > 0 && 
               txtBox.value.trim().indexOf("Enter Your Personalization") == -1 &&
               txtBox.value.trim().indexOf("Enter Your Custom") == -1)
            {
                replace = confirm("Are you sure you want to replace your current text?");
            }
            
            // Check to see if we need to cancel the submission
            if(replace)
            {
                el.click();
            }
        }
    }
    catch(e)
    {
        //alert("The following error occurred: " + e.name + " - " + e.message);
    }
}

/* 
** Added function to allow the showing of the product information window from any control
** that is called from customizecard.aspx
*/
function showProductInformationWindow(productID)
{
	var url = '/Popups/ProductInformation.aspx?Pro__ProductID=' + productID;
	var oWnd = showExternalWindow(url);
}

/* 
** Added function to allow the showing of the product zoom image from any control.
** that is called from customizecard.aspx
*/
function showZoomWindow(zoomWidth, zoomImageUrl)
{
    var zoomWidth = zoomWidth;
    var imageUrl = zoomImageUrl;
//debugger;
    var oWnd = showExternalWindow('/popups/ZoomPreview.aspx');
    oWnd.Argument  = imageUrl;
    var windowWidth = (zoomWidth / 1) + 60;
    oWnd.SetSize(windowWidth, 580);
    return false;
}

function findBothPos(obj) 
{
    var curleft = curtop = 0;
    if (obj.offsetParent) 
    {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        
        while (obj = obj.offsetParent) 
        {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    
    return [curleft,curtop];
}