/* ==============================================================
// a182 - multimedia popup - MJ
// a443 - flash animation -MJ
// a493 - including old and new flash animation fix - mj 6/28/02
// a188 - commented out SearchSubmit in SubmitPagingForm - sk 7/31/02
// a188 - reinserted SearchSubmit in SubmitPagingForm, but 
//        changed the param that was sent to the form - sk 8/2/2002
// a881 - inserted a new function UncheckOtherChkBox. This is used to 
//	    make checkboxes behave like radio buttons
// a938 - added a new function OpenPrintable - just like OpenMI, but the window is smaller.
// a1827 - addeda new function - OpenEmailPolicy
// a2166 - added code to checkIfUserSelNewNickName to handle URLs of the form
//			/us/en/product/sku=abc
//			added new function OpenMoreImagesWin
// a2568 - new PIP 2004 additional changes
// a3062 - skumbera More IMages TN listing order
// a3499 - TN listing order bugs SK 3/14/2005
// a3596 - skumbera remove onclick from href tags 4/14/2005
// a5098 - SKumbera	Add BML 9/28/2006
// a5048 - add utility functions getElement(), showId(), hideId(), setDisplay() - dwighth 9/8/06
// a5012 - change checkIfUserSelNewNickName() to handle new ship-to indexing
// ==============================================================
*/

function browserDetect()
{
// from http://www.quirksmode.org/js/detect.html

	var detect = navigator.userAgent.toLowerCase();
	

	var browser = '';

	if (detect.indexOf('konqueror') + 1)
	{
		browser = "Konqueror";
	}		
	else if (detect.indexOf('safari') + 1) browser = "safari"
	else if (detect.indexOf('omniweb') + 1) browser = "omniweb"
	else if (detect.indexOf('opera') + 1) browser = "opera"
	else if (detect.indexOf('webtv') + 1) browser = "Webtv";
	else if (detect.indexOf('icab') + 1) browser = "icab"
	else if (detect.indexOf('msie') + 1) browser = "msie"
	else if (detect.indexOf('firefox') + 1) browser = "firefox"
	else if (!detect.indexOf('compatible') + 1)
	{
		// Netscape Navigator
		browser = "navigator"
	}
	else browser = "An unknown browser";

	return browser;
}


function UncheckOtherChkBox(currChk, myForm, allowAllBoxesToBeEmpty)
{
	allBoxesAreEmpty = true;
	
	for (var i = 0; i < myForm.elements.length; i++)
	{
		if (myForm.elements[i].type== "checkbox")
		{
			if (currChk.name == (myForm.elements[i].name) &&
					currChk.value != (myForm.elements[i].value))
			{
				myForm.elements[i].checked=false
			}
			
			if (currChk.name == myForm.elements[i].name)
				allBoxesAreEmpty = allBoxesAreEmpty && !myForm.elements[i].checked;
		}
	}
		
	if (allBoxesAreEmpty && !allowAllBoxesToBeEmpty)
		currChk.checked=true;
	
}

function SubmitForm(myForm)
{
	myForm.submit();
}

function SubmitPagingForm(myForm, myStart, myEnd)
{
	myForm.s.value = myStart;
	myForm.e.value = myEnd;
	RequerySubmit(myForm);
	SubmitForm(myForm);
}

function SubmitCLForm(myForm, myCFlag, myLFlag)
{
	myForm.changeC.value = myCFlag;
	myForm.changeL.value = myLFlag;
	SubmitForm(myForm);
}

function OpenVB(myPage)
{
	window.name = "main";
	OpenWin = window.open(myPage, "VBWindow", "toolbar=yes,menubar=no,location=no,scrollbars=no,resizable=no,width=730,height=505");
}

function OpenEmailPolicy(myPage)
{
	window.name = "main";
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(640/2));
	var t = Math.round((ht/2)-(404/2));
	var ops = 'width= 640, height= 404, top='+t+',left='+l+',toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no';
	OpenWin = window.open(myPage, "EmailPolicy",ops);
	//OpenWin = window.open(myPage, "EmailPolicy", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=640,height=430");
}

function CloseVB(myPage)
{
    if (window.opener.opener != null)
    {
        if (window.opener.opener.closed)
        {
            window.open(myPage, "main");
        }
        else
        {
            window.opener.opener.location = myPage;
        }
    }
    else
    {
        if (window.opener.closed)
        {
            window.open(myPage, "main");
        }
        else
        {
            window.opener.location = myPage;
        }
    }
    window.close();
}

function OpenPrintable(myPage)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(658/2));
	var t = Math.round((ht/2)-(440/2));
	var ops = 'width= 658, height= 440, top='+t+',left='+l+',toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes';
	window.name = "main";
	OpenWin = window.open(myPage, "MIWindow",ops);
}

function OpenMI(myPage)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(658/2));
	var t = Math.round((ht/2)-(700/2));
	var ops = 'width= 658, height= 700, top='+t+',left='+l+',toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes';
	window.name = "main";
	OpenWin = window.open(myPage, "MIWindow",ops);
}

function Open3DWin2(myPage, myPid, myContentPath, myWidth, myHeight)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no';

	window.name = "main";
	if (myPage.indexOf("?") >= 0)
	{
		myPage = myPage + "&pid=" + myPid + "&contentpath=" + myContentPath;
	}
	else
	{
		myPage = myPage + "?pid=" + myPid + "&contentpath=" + myContentPath;
	}
	
	OpenWin = window.open(myPage, "3DWindow", ops);
	return false;
}

function OpenAnimationWin(myPage, myContentPath, myWidth, myHeight)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,location=no,directories=no,status=no,scrollbars=no, resizable=no';

	window.name = "main";
	if (myPage.indexOf("?") >= 0)
	{
		myPage = myPage + "&contentpath=" + myContentPath;
	}
	else
	{
		myPage = myPage + "?contentpath=" + myContentPath;
	}
	OpenWin = window.open(myPage, "AnimationWindow", ops);
	return false;
}

function OpenNewAnimationWin(myPage, myContentPath, myWidth, myHeight)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,location=no,directories=no,status=no,scrollbars=no, resizable=no';

	window.name = "main";
	/*
	if (myPage.indexOf("?") >= 0)
	{
		myPage = myPage + "&contentpath=" + myContentPath;
	}
	else
	{
		myPage = myPage + "?contentpath=" + myContentPath;
	}
	*/
	myPage = myPage + myContentPath;
	OpenWin = window.open(myPage, "AnimationWindow", ops);
	return false;
}

function OpenMoreImagesWin(myPage, moreInfoImagePath, sku, thumbNailImages, thumbNailImagesNumber)
{

	var browser = browserDetect();
	var myWidth, myHeight;
	
	if (browser == 'msie') 
	{
		myWidth = 570;   // should have been 560 but window.resizeTo seems to need the extra 10 pixels for some reason.
		myHeight = 363;  // should have been 313 but window.resizeTo seems to need the extra 30 pixels for some reason.
	}
	else
	{
		myWidth = 568;
		myHeight = 363;
	}


	if (thumbNailImagesNumber > 2)
		myHeight += 150;
		
	if (thumbNailImagesNumber > 6)
		myHeight += 150;
	
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,location=no,directories=no,status=no,scrollbars=no, resizable=no';

	myPage = myPage + moreInfoImagePath;

	if (myPage.indexOf("?") >= 0)
	{
		myPage = myPage + "&sku=" + sku;
	}
	else
	{
		myPage = myPage + "?sku=" + sku;
	}
	
	moreImagesWindow = window.open(myPage, "MoreImagesWindow", ops);
	moreImagesWindow.resizeTo(myWidth, myHeight);
	
	return false;
}

function OpenMultimediaWin(myPage, myContentPath, myWidth, myHeight)
{
	/*
	if (parseInt(navigator.appVersion)>3) 
	{
		if (navigator.appName=="Netscape") 
		{
			winW = window.innerWidth;
	 	 	winH = window.innerHeight;
	 	}
	 	if (navigator.appName.indexOf("Microsoft")!=-1) 
	 	{
	  		winW = document.body.offsetWidth;
	  		winH = document.body.offsetHeight;
	 	}
	}
	*/

	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,location=no,directories=no,status=no,scrollbars=no, resizable=no';

	window.name = "main";
	myPage = myPage + myContentPath;

	OpenWin = window.open(myPage, "MultimediaWindow", ops);
	return false;
}

function openFreightPopup(myPage,myWidth, myHeight)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no';
	window.name = "main";
	OpenWin = window.open(myPage, "FreightPopupWin", ops);
}

function OpenBMLWin(myPage, myContentPath, myWidth, myHeight)
{
	var wt = screen.availWidth;
	var ht = screen.availHeight;
	var l = Math.round((wt/2)-(myWidth/2));
	var t = Math.round((ht/2)-(myHeight/2));
	var ops = 'width='+myWidth+',height='+myHeight+',top='+t+',left='+l+',toolbar=no,location=no,directories=no,status=no,scrollbars=yes, resizable=no';

	window.name = "main";
	myPage = myPage + myContentPath;

	OpenWin = window.open(myPage, "BMLWindow", ops);
	return false;
}

function RequerySubmit(myForm)
{
	if (myForm.action.indexOf("?") >= 0)
	{
		myForm.action = myForm.action + "&requery=true";
	}
	else
	{
		myForm.action = myForm.action + "?requery=true";
	}
}

function ShopCartSubmit(myForm, myCartAction)
{
	myForm.cartAction.value = myCartAction;
	if (myForm.action.indexOf("?") >= 0)
	{
		myForm.action = myForm.action + "&cartAction=" + escape(myForm.cartAction.value);
	}
	else
	{
		myForm.action = myForm.action + "?cartAction=" + escape(myForm.cartAction.value);
	}
	SubmitForm(myForm);
}


//================================================================================
// This function used by the "Ship-To" select box on the shop cart page. 
// It is invoked by the onChange event of that control.  If the user has just
// selected the "New Nickname" option of that select control, the add new ship-to
// screen will be displayed.
// Version 2 for shopbasket - a5012 address validation changes & redesign
//================================================================================
function checkIfUserSelNewNickName2(oShipToSelectControl, index, stindex, oCallingPageRelUrl, nicknameURL)
{
	//==========================================================================
	// Check to see if the user has selected the "Add Nickname" entry.  This
	// entry is always the last in the list, so we know they have selected it
	// if the sel index is equal to the array size minus 1 (remember the
	// sel index is 0 based).
	//==========================================================================
	var oSelIndex = oShipToSelectControl.selectedIndex;
	var oListSize = oShipToSelectControl.options.length;
	/*
	alert("oSelIndex: " + oSelIndex);
	alert("oListSize: " + oListSize);
	*/
	if (oSelIndex == oListSize-1) // picked last one... build URL & go there
	{	
		//=======================================================================
		// Build the Url to the nick name entry page.  Start with the 
		// relative path of the nickname page.  Add to it the params passed.
		// (will look similar to: ?create=true&...)
		//=======================================================================
		var oUrl = nicknameURL + "?create=true&index=" + index + "&stindex=" + stindex;

		//=======================================================================
		// Launch the new page.
		//=======================================================================
		document.location = oUrl;	
	}
}

//================================================================================
// This function is used by the "Ship-To" select box on the pip page (shipto.jhtml). 
// It is invoked by the onChange event of that control.  If the user has just
// selected the "New Nickname" option of that select control, the add new ship to
// screen will be displayed.
//================================================================================
function checkIfUserSelNewNickNamePip(oShipToSelectControl, oCallingPageRelUrl, nicknameURL)
{
	//==========================================================================
	// Check to see if the user has selected the "Add Nickname" entry.  This
	// entry is always the last in the list, so we know they have selected it
	// if the sel index is equal to the array size minus 1 (remember the
	// sel index is 0 based).
	//==========================================================================
	var oSelIndex = oShipToSelectControl.selectedIndex;
	var oListSize = oShipToSelectControl.options.length;
 
	/*
	alert("oSelIndex: " + oSelIndex);
	alert("oListSize: " + oListSize);
	*/
	
	if (oSelIndex == oListSize-1)
	{	
		var windowlocation = window.location.href;
		var queryParamSku = "";
		var queryParamPid = "";
		if (windowlocation.indexOf("sku__") >= 0 || windowlocation.indexOf("sku=") >= 0)
		{
			var skuPlusRest = windowlocation.indexOf("sku__");
			if (skuPlusRest < 0)
				skuPlusRest = windowlocation.substring(windowlocation.indexOf("sku=") + 4);
			else
				skuPlusRest = windowlocation.substring(skuPlusRest+5);

			slashSeparator = skuPlusRest.indexOf("/");
			ampersandSeparator = skuPlusRest.indexOf("&");

			if (slashSeparator == -1) slashSeparator=skuPlusRest.length;
			if (ampersandSeparator== -1) ampersandSeparator=skuPlusRest.length;

			if (slashSeparator < ampersandSeparator)
				queryParamSku = skuPlusRest.substring(0, slashSeparator);
			else
				queryParamSku = skuPlusRest.substring(0, ampersandSeparator);
		}      

		//=======================================================================
		// Build the Url to the nick name entry page.  Start with the 
		// relative path of the nickname page.  Add to it the sku present
		// on this current page, cuz we'll need it when we come back here
		// (will look similar to: ?create=true&sku=SI817GRY).  
		//=======================================================================
		var oUrl = nicknameURL + "?create=true";
		
		if (queryParamSku.length > 0)
			oUrl += "&sku=" + queryParamSku;
				
		//=======================================================================
		// Launch the new page.
		//=======================================================================
		document.location = oUrl;	
	}
}

//================================================================================
// This function used by the "Ship-To" select box on the pip page.  
// It is invoked by the onChange event of that control.  If the user has just
// selected the "New Nickname" option of that select control, the add new ship to
// screen will be displayed.
// This is the old version which may be DELETED after a5239 is production.
//================================================================================
function checkIfUserSelNewNickName(oShipToSelectControl, index, oCallingPageRelUrl, nicknameURL)
{
	//==========================================================================
	// Check to see if the user has selected the "Add Nickname" entry.  This
	// entry is always the last in the list, so we know they have selected it
	// if the sel index is equal to the array size minus 1 (remember the
	// sel index is 0 based).
	//==========================================================================
	var oSelIndex = oShipToSelectControl.selectedIndex;
	var oListSize = oShipToSelectControl.options.length;
	
	var windowlocation = window.location.href;
	var queryParamSku = "";
	var queryParamPid = "";
 
	if (windowlocation.indexOf("sku__") >= 0 || windowlocation.indexOf("sku=") >= 0)
	{
		var skuPlusRest = windowlocation.substring(windowlocation.indexOf("sku__"));
		if (skuPlusRest < 0)
			skuPlusRest = windowlocation.substring(windowlocation.indexOf("sku=") + 4);
		else
			skuPlusRest += 5;

		slashSeparator = skuPlusRest.indexOf("/");
		ampersandSeparator = skuPlusRest.indexOf("&");

		if (slashSeparator == -1) slashSeparator=skuPlusRest.length;
		if (ampersandSeparator== -1) ampersandSeparator=skuPlusRest.length;

		if (slashSeparator < ampersandSeparator)
			queryParamSku = skuPlusRest.substring(0, slashSeparator);
		else
			queryParamSku = skuPlusRest.substring(0, ampersandSeparator);
	}      
	
	if (windowlocation.indexOf("pid__") >= 0 || windowlocation.indexOf("pid=") >= 0)
	{
		var pidPlusRest = windowlocation.substring(windowlocation.indexOf("pid__"));
		if (pidPlusRest < 0)
			pidPlusRest = windowlocation.substring(windowlocation.indexOf("pid=") + 4);
		else
			pidPlusRest += 5;
			
		slashSeparator = pidPlusRest.indexOf("/");
		ampersandSeparator = pidPlusRest.indexOf("&");

		if (slashSeparator == -1) slashSeparator=pidPlusRest.length;
		if (ampersandSeparator== -1) ampersandSeparator=pidPlusRest.length;

		if (slashSeparator < ampersandSeparator)
			queryParamPid = pidPlusRest.substring(0, slashSeparator);
		else
			queryParamPid = pidPlusRest.substring(0, ampersandSeparator);
	}                    	
			
	/*
	alert("oSelIndex: " + oSelIndex);
	alert("oListSize: " + oListSize);
	*/
	
	if (oSelIndex == oListSize-1)
	{	
		//=======================================================================
		// Build the Url to the nick name entry page.  Start with the 
		// relative path of the nickname page.  Add to it the params present
		// on this current page, cuz we'll need them when we come back here
		// (will look similar to: ?pid=175000&pcatid=3...).  
		//=======================================================================
		var oUrl = nicknameURL + "?";
		
		if (queryParamSku.length > 0)
			oUrl += "sku=" + queryParamSku;
		if (queryParamPid.length > 0)
			oUrl += "pid=" + queryParamPid;
				
		//=======================================================================
		// Add the go back paramater to the end.  Take care to use the "?" or
		// "&" character depending on the circumstance.
		//=======================================================================
		if (oUrl.indexOf("?") >= 0)
		{
   		oUrl += "&create=true&index=" + index;
		}
		else
		{
   		oUrl += "?create=true&index=" + index;
		}
			
		//=======================================================================
		// Launch the new page.
		//=======================================================================
		document.location = oUrl;	
	}
}

// Find an element in a non-browser-specific fashion.
function getElement(id)
{
	// self-replacing function, gets replaced by proper one based on browser facilities.
	
	if(document.getElementById) {
		getElement = function(id){ return document.getElementById(id); }
	}
	else if(document.all) {
		getElement = function(id){ return document.all[id]; };
	}
	else if(document.layers) {
		getElement = function(id){ return document.layers[id]; };
	}
	else {
		getElement = function() { return null; }
	}

	// When we get here, the getElement function has been replaced.
	// So we return the result of the new function.
	return getElement(id);
}

// make a hidden object visible.
function showId(objId) 
{
	setDisplay(objId,"");
}

// make an object hidden.
function hideId(objId) 
{
	setDisplay(objId,"none");
}

// Change display status of an object
function setDisplay(objId,value) 
{
	obj = getElement(objId);	// may return null/undefined
	if (obj != null && obj.style != null ) {
		obj.style.display = value;
	}
}


// Find the x position (within the window) of an element.
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// Find the y position (within the window) of an element.
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

