//
// global.js
//
// LeapFrog global JavaScript Library
// 
// Authors:  Acquity Group Commerce OnDemand (www.acquitygroup.com)
//
//
//



// *******************************************************
// ******  Image pre-load for Navigation Rollovers  ******
// *******************************************************
//
// IMPORTANT:  Organized Alphabeticallly
//
//if (document.images) {
//
//	tabLeapFrogup    = new Image();
//   tabLeapFrogup.src= "media/leapfrog/images/common/leapfrogtab.gif" ;
//    tabLeapFrogdown  = new Image() ;
//    tabLeapFrogdown.src = "media/leapfrog/images/common/leapfrogtabdown.gif" ;
//}

//+----------------------------------------------------------------------------
//  Function:       resizeFix
//  Author:         Kevin P. Wojdak, Acquity Group, LLC
//  Created:        June 2006
//  Revised:        July 2006
//  Description:    Resets the page when the page is resized.
//  Arguments:      none
//  Returns:        none
//+----------------------------------------------------------------------------
//
function resizeFix() {
		fixPosition();
}

//+----------------------------------------------------------------------------
//  Function:       fixPosition
//  Author:         Kevin P. Wojdak, Acquity Group, LLC
//  Created:        July 2006
//  Description:    Left justifies the interface when working on a screen resolution  
//                  of 800x600 or less.
//  Arguments:      none
//  Returns:        none
//+----------------------------------------------------------------------------
//
function fixPosition() {  
	var sW = 1000;
	sW = windowWidth();
	var winObj = document.getElementById('pageContainer');
	if (winObj != null) {
		if (sW <= '805') {
			winObj.style.marginLeft = 0;
			winObj.style.left = 0;
		} else {
			winObj.style.marginLeft = '-400px';
			winObj.style.left = '50%';
		}
	}
}


//+----------------------------------------------------------------------------
//  Function:       windowWidth
//  Author:         Original alertSize script provided for free by http://www.howtocreate.co.uk. 
//  Modified by:    Kevin P. Wojdak, Acquity Group, LLC
//  Created:        July 2006
//  Description:    Provides the current viewable page width.
//                  
//  Arguments:      none
//  Returns:        width of the page as one string
// 
//  Note:           Modified to return just width in one return value.  
//+----------------------------------------------------------------------------
//
function windowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  var returnString = myWidth;
  return returnString;
}


//+----------------------------------------------------------------------------
//  Function:       imagebtndown
//  Author:         Kevin P. Wojdak
//  Created:        June 2006
//  Description:    Used to display the correct rollover image when the mouse rolls  
//                  over a button.
//  Arguments:      buttonname - name of the button image to get
//  Returns:        none
//+----------------------------------------------------------------------------
//
function imagebtndown( buttonname )
{
    if (document.images) {
      document.getElementById(buttonname).src = eval( buttonname + "down.src" );
    }
}


//+----------------------------------------------------------------------------
//  Function:       imagebtnup
//  Author:         Kevin P. Wojdak
//  Created:        June 2006
//  Description:    Used to display the original button image when the mouse rolls  
//                  away from a button.
//  Arguments:      buttonname - name of the button image to get
//  Returns:        none
//+----------------------------------------------------------------------------
//
function imagebtnup ( buttonname )
{
    if (document.images) {
      document.getElementById(buttonname).src = eval( buttonname + "up.src" );
    }
}

//+----------------------------------------------------------------------------
//  Function:       enableMsgBox
//  Author:         Kevin Wojdak
//  Created:        June 2006
//  Description:    Shows the gift message box when the Gift Wrap checkbox is selected
//                  
//  Arguments:      idNum = current id of checkbox
//                  
//  Called from:    Order page
//+----------------------------------------------------------------------------
//
function enableMsgBox(idNum) {
	var giftObj = document.getElementById('messageBox' + idNum);
	giftObj.style.display = (giftObj.style.display == "none") ? "inline" : "none" ;
	document.getElementById('giftmessage' + idNum).select();
}

//+----------------------------------------------------------------------------
//  Function:       textAreaMaxLimit
//  Author:         Kevin Wojdak
//  Created:        June 2006
//  Description:    Limits the textArea's value length to a max limit
//                  
//  Arguments:      textAreaField = textAreaField object
//                  strokesField = span field where strokes left will be displayed
//                  maxlimit = max number of characters allowed
//  Called from:    a TextArea's onKeyDown and onKeyUp event listeners
//+----------------------------------------------------------------------------
//
function textAreaMaxLimit(textAreaField, strokesField, maxlimit) {
	var limitObj = document.getElementById(strokesField);
	if (textAreaField && (textAreaField.value.length > maxlimit)) { // if too long...trim it!
		textAreaField.value = textAreaField.value.substring(0, maxlimit);
	} else {
		var strokesLeft = maxlimit - textAreaField.value.length;
		limitObj.innerHTML = strokesLeft;
	}
	//alert(textAreaField.value.length);
	return true;
}

//+----------------------------------------------------------------------------
//  Function:       changeDiv
//  Author:         Kevin Wojdak
//  Created:        June 2006
//  Description:    Changes the displayed Shipping Address
//                  
//  Arguments:      objValue = id of object to display
//                  objId = id of current object
//                  
//  Called from:    Multiple Shipping Addresses page
//+----------------------------------------------------------------------------
//
function changeDiv(objValue,objId) {
	var selectedItem = document.getElementById(objId);
	var pageSELOpts = selectedItem.getElementsByTagName("OPTION");
	// Hide all current address DIVs
	for (j=0; j < pageSELOpts.length; j++) {
		document.getElementById(pageSELOpts[j].value).style.display = 'none';
	}
	// Display the selected DIV
	document.getElementById(objValue).style.display = 'block';
}

//+----------------------------------------------------------------------------
//  Function:       initDIVs
//  Author:         Kevin P. Wojdak
//  Created:        June 2006
//  Description:    Initializes the shipping address DIVs on the Multiple Shipping
//                  address page.
//                  
//  Arguments:      objValue = id of object to display
//                  objId = id of current object
//                  
//  Called from:    Multiple Shipping Addresses page
//+----------------------------------------------------------------------------
//
function initDIVs(tblName) {
	var tblObj = document.getElementById(tblName);
	var pageSELs = tblObj.getElementsByTagName("SELECT");
	var selectedOpts;
	selectedOpts = new Array();
	var a = 0;
	// Prepare all address DIVs by hiding them all
	for (i=0; i < pageSELs.length; i++) {
		var pageSELOpts = pageSELs[i].getElementsByTagName("OPTION");
		for (j=0; j < pageSELOpts.length; j++) {
			// If a DIV is "selected", save its ID for retrieval later
			if (pageSELOpts[j].selected == true) {
				a++;
				selectedOpts[a] = pageSELOpts[j].value;
			}
			document.getElementById(pageSELOpts[j].value).style.display = 'none';
		}
	}
	// Show only the DIVs that are currently selected
	for (k=1; k < selectedOpts.length; k++) {
		document.getElementById(selectedOpts[k]).style.display = 'block';
	}
}

//+----------------------------------------------------------------------------
// NEEDS TO BE FORMATED
//+----------------------------------------------------------------------------
//
function openNavPopup(href) {
	var args = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=480,top=100,left=100';
	var name = 'Popup';
	return windowOpener(href, name, args);
}

function windowOpener(url, name, args) {
	if (typeof(popupWin) != "object"){
			popupWin = window.open(url,name,args);
		} else {
		if (!popupWin.closed){
			popupWin.location.href = url;
		} else {
			popupWin = window.open(url, name,args);
		}
	}
	popupWin.focus();
}

function openNewWindow(href) {
	w=800
	h=600
	if (window.screen) {
	   w = window.screen.availWidth;
	   h = window.screen.availHeight;
	}
	window.open(href,'new','width='+w+',height='+h+',top=0,left=0,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
}

function swapProductImage(img) {
	document.images["product_image"].src = img;
}

function launchPopup(imagename) {
	window.open("popup.htm",'Popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=450,height=480,top=100,left=100');
}

var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

var movepx = 24; // width of swatch image + padding
var newpos = 0;
function scrollleft(layer, numswatches)
{
	if (!DHTML) return;
	var x = new getObj(layer);
	var current = x.style.left;
	// take out the 'px'
	if (current.indexOf('px') != -1) current = current.substring(0, (current.length)-2);
	// force the string to a number
	current = current * 1;
	newpos = current + movepx;
	if (newpos > 0) newpos = current;
	x.style.left = newpos + "px";
}

function scrollright(layer, numswatches)
{
	var divwidth = numswatches * movepx;
	var stoppoint = divwidth - 142;
	if (!DHTML) return;
	var x = new getObj(layer);
	current = x.style.left;
	if (current.indexOf('px') != -1) current = current.substring(0, (current.length)-2);
	current = current * 1;
	amount = current - movepx;
	if (amount < -(stoppoint)) amount = current;
	x.style.left = amount  + "px";
}
function getById(n)
{
    if ( document.getElementById )
    {
        return document.getElementById(n);
    }
    else if ( document.all )
    {
        return document.all[n];
    }
    else if ( document.layers )
    {
        return document.layers[n];
    }
    else
    {
        return null;
    }
}

function selectLast(s)
{
    s.selectedIndex = s.length - 1;
}

function checkAddAccessory(checkboxId, dropDown)
{
	var addCheckbox = getById(checkboxId);
	if(addCheckbox)
	{
		if(dropDown.selectedIndex > 0)
		{
			if(!addCheckbox.checked)
				addCheckbox.checked = true;
		}
	}
}

//+----------------------------------------------------------------------------
//  Function:       addBMHiddenClass
//  Author:         Kevin P. Wojdak
//  Created:        July 2006
//  Description:    Adds BMHidden class to BMI hidden input fields
//                  
//  Arguments:      none
//                  
//  Called from:    Wherever needed
//+----------------------------------------------------------------------------
//
function addBMHiddenClass () {
		var pageId = document.getElementById("pageContainer");
		var inputFlds = pageId.getElementsByTagName("INPUT");
		for (i=0; i < inputFlds.length; i++) {
			if (inputFlds[i].type == "hidden") {
				inputFlds[i].setAttribute("className","BMHidden" );
				//alert(inputFlds[i].id);
			}
		}
	}
	
//+----------------------------------------------------------------------------
//  Function:       resetIfDefault
//  Author:         Evan Cooperman
//  Created:        March 2009
//  Description:    Clears a search input box if the value
//					in the box is default when the user clicks in it
//                  
//  Arguments:      defaultText = text that displays in the input by default
//					obj = javascript input object (the search text for the form)
//                  
//  Called from:    Any form needed
//+----------------------------------------------------------------------------
//
function resetIfDefault(defaultText, obj) {
	if ( obj.value == defaultText ) {
		obj.value = "";
	}
}

//+----------------------------------------------------------------------------
//  Function:       restoreIfEmpty
//  Author:         Evan Cooperman
//  Created:        March 2009
//  Description:    Resets a search input box to the default value
//					if a user clicks away from the box and leaves it empty
//                  
//  Arguments:      defaultText = text that displays in the input by default
//					obj = javascript input object (the search text for the form)
//                  
//  Called from:    Any form needed
//+----------------------------------------------------------------------------
//
function restoreIfEmpty(defaultText, obj) {
	if ( obj.value == "" ) {
		obj.value = defaultText;
	}
}