var bIsIE = ('Microsoft Internet Explorer' == navigator.appName);
var bIsNS = (!bIsIE && 'Netscape' == navigator.appName);
var bIsNS72 = (bIsNS && -1 < navigator.userAgent.toUpperCase().indexOf('NETSCAPE/7.2'));
var bIsMac = (-1 < navigator.platform.toUpperCase().indexOf('MAC'));
var iNodeBaseIndex = (bIsNS) ? 1 : 0;

var iDPIMult = (bIsIE && screen.deviceXDPI) ? (screen.deviceXDPI / screen.logicalXDPI) : 1;
var iBrowserTitleBarWidth = 0, iBrowserTitleBarHeight = 0;
var bDoDialogResize = false;

var objMainFrame = getTopFrameSet(this);
var objInitFocusField = null;

//window.onerror = genErrHandler;
window.onunload = genWindowUnload;

// If we are in an IE modal dialog window and the title/border width/height has
// not been determined yet, figure out the values and store them.
if (window.dialogArguments && window.dialogArguments.location) {
	if (objMainFrame && 'YES' == getParamVal('getBorderDims', window.location.href) && window.dialogArguments.location) bDoDialogResize = true;
}
else if (bIsNS) {
	// The client browser is Netscape Navigator or Mozilla. Change the BODY overflow style
	// to display automatic scroll bars in the proper manner. Obviously, the style sheet
	// declaration should be placed before the include statement for this file.
	document.write('<STYLE TYPE="text/css"> BODY { overflow:visible; } </STYLE>');
}

if (objMainFrame) {
	iBrowserTitleBarWidth = objMainFrame.iBrowserTitleBarWidth;
	iBrowserTitleBarHeight = objMainFrame.iBrowserTitleBarHeight;
}

function setWinBorderDimensions() {
	if (objMainFrame) {
		objMainFrame.iBrowserTitleBarWidth = iBrowserTitleBarWidth = ((parseInt(window.dialogWidth) / iDPIMult) - document.body.offsetWidth); // * iDPIMult;
		objMainFrame.iBrowserTitleBarHeight = iBrowserTitleBarHeight = (bIsIE ? ((parseInt(window.dialogHeight) / iDPIMult) - document.body.offsetHeight) * iDPIMult : -(window.outerHeight - window.innerHeight));
	}
}

function getTopFrameSet(objCurWin) {
	var objMain = objCurWin;
	var objAbsTop = objMain.top;

	while (objMain != objAbsTop) {
		if (objMain.bTopFrame) break;
		objMain = objMain.parent;
	}

	if (!objMain.bTopFrame)  {
		// Might be in a Supercession or Prior window.
		if (top.window.opener)
			objMain = top.window.opener.objMainFrame;
		else if(window.dialogArguments && window.dialogArguments.objMainFrame) // Modal
			objMain = window.dialogArguments.objMainFrame;
		else
			objMain = null;
	}

	return objMain;
}

function getEPCFrameWin(objCurWin) {
	var objMain = objCurWin.parent;
	var objAbsTop = objMain.top;

	while (objMain != objAbsTop) {
		if (objMain.bEPCFrame) break;
		objMain = objMain.parent;
	}

	if (!objMain.bEPCFrame) objMain = null;

	return objMain;
}

function getOuterHTML(objElement) {
	var sOuterHTML = '';

	if (bIsIE)
		sOuterHTML = objElement.outerHTML;
	else {
		var iNumAtt = objElement.attributes.length;

		sOuterHTML = '<' + objElement.tagName;

		for (var iCount = 0; iCount < iNumAtt; iCount++)
			sOuterHTML += ' ' + objElement.attributes[iCount].name + '="' + objElement.attributes[iCount].value.replace(/"/g, '&#34;') + '"';

		sOuterHTML += '>' + objElement.innerHTML + '</' + objElement.tagName + '>';
	}

	return sOuterHTML;
}

function getParamVal(sParam, sSrc) {
	var X, sTemp, sOut = '';
	var sPU = sParam.toUpperCase();
	var sSU = sSrc.toUpperCase();

	X = sSU.search('&' + sPU + '=');
	if (-1 == X) X = sSU.search('\\?' + sPU + '=');
	if (-1 < X) {
		sTemp = sSU.slice(X + sPU.length + 2, sSU.length);
		if (0 < sTemp.length) {
			X = sTemp.search('&');
			if (-1 == X) X = sTemp.length;
			sOut = sTemp.substr(0, X);
		}
	}
	return sOut;
}

function getIntVal(sVal) {
	if (!sVal || 0 == sVal.length || isNaN(sVal)) return 0;
	return parseInt(sVal);
}

function parsePercent(sPercentVal) {
	var iLen = sPercentVal.length;
	var bSigned = ('%' == sPercentVal.substr(iLen - 1, 1))
	var fOut = '';
	var sTemp = '';

	if (0 < iLen && ((bSigned && 1 < iLen) || isNaN(sPercentVal))) {
		for (var iCount = 0; iCount < iLen; iCount++) {
			sTemp = sPercentVal.charAt(iCount);
			if (!isNaN(sTemp)) fOut += sTemp;
		}

	}
	
	fOut = parseInt(fOut);
	if (!isNaN(fOut)) fOut = fOut / 100;

	return fOut;
}

function getRandomNum() {
	return Math.round((Math.random() * 19999) + 1);
}

function strFormat(sFormatStr, vArg0, vArg1, vArg2, vArg3, vArg4, vArg5, vArg6, vArg7, vArg8, vArg9) {
	var iCurrStart = 0;
	var iMark = 0;

        var arrResult = new Array();

        // Replace a %v for every variable argument
	for (var iCount = 0; iCount < arguments.length - 1; iCount++) {
		iMark = sFormatStr.indexOf('%v', iCurrStart);
		if (-1 == iMark) break;

		arrResult.push(sFormatStr.substring(iCurrStart, iMark));
		arrResult.push(eval('vArg' + iCount));

		iCurrStart = iMark + 2;
	}

	arrResult.push(sFormatStr.substr(iCurrStart));

	return arrResult.join('');
}

function Trim(sToTrim, sTrimChar) {
	var iCharCount = 0;

	if (!sTrimChar) sTrimChar = ' ';

	while (iCharCount < sToTrim.length && sToTrim.charAt(iCharCount) == sTrimChar)
		iCharCount++;

	if (iCharCount) sToTrim = sToTrim.substr(iCharCount);
	iCharCount = 0;

	while (iCharCount < sToTrim.length && sToTrim.charAt(sToTrim.length - iCharCount - 1) == sTrimChar)
		iCharCount++;

	if (iCharCount) sToTrim = sToTrim.substring(0, sToTrim.length - iCharCount);

	return sToTrim;
}

function genErrHandler(sMsg, sURL, iLnNo) {
	alert(GetLocalString('GENERIC_ERROR_MSG', sMsg, iLnNo));
	return true;
}

function genWindowUnload() {
	window.status = '--[ ' + GetLocalString('EW_ELECTRONIC_PARTS_CATALOG') + ' ]--';
}

function triggerEvent(objElement, sEventName) {
	if (objElement && sEventName) {
		if (document.fireEvent)
			objElement.fireEvent('on' + sEventName);
		else if (document.dispatchEvent) {
			var objEvent = objElement.ownerDocument.createEvent('MouseEvents');
			objEvent.initMouseEvent(sEventName, true, true, objElement.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
			objElement.dispatchEvent(objEvent);
		}
	}
}

function openModalDialog(sURL, sWinName, vArgs, iWidth, iHeight) {
	if (bIsIE) {
		var bSetBorderDims = (objMainFrame.iBrowserTitleBarWidth) ? false : true;

		iWidth = (iWidth + iBrowserTitleBarWidth) * iDPIMult;
		iHeight = (iHeight + iBrowserTitleBarHeight) * iDPIMult;

		if (bSetBorderDims) sURL += '&getBorderDims=Yes';
		window.showModalDialog(sURL + '&cachePrevent=' + getRandomNum(), vArgs, 'dialogWidth:' + iWidth  + 'px; dialogHeight:' + iHeight + 'px; scroll:no; status:no; help:no;');
	}
	else
		window.open(sURL, sWinName, 'width=' + iWidth + ',height=' + iHeight + ',dependent,alwaysRaised');
}

function openEPCWindow(objOpener, sURL, sWindowName) {
	var new_win = objOpener.open(sURL, sWindowName, getChildWindowParams() + 'menubar,status,resizable');

	if(objMainFrame.bMaximizeIPLs) {
	    new_win.moveTo(0,0);
		new_win.resizeTo(screen.availWidth, screen.availHeight);
	}

	objMainFrame.arrOpenWindows[objMainFrame.arrOpenWindows.length] = new_win;

	return new_win;
}

function openWindow(objOpener, sURL, sWindowName, sParams) {
	var new_win = objOpener.open(sURL, sWindowName, sParams);

	objMainFrame.arrOpenWindows[objMainFrame.arrOpenWindows.length] = new_win;

	return new_win;
}

function closeOpenWindows() {
    if( ! objMainFrame.bCloseOrphanedWindows ) return;

    for(var i = objMainFrame.arrOpenWindows.length - 1; i >= 0; --i) {
	    var objWindow = objMainFrame.arrOpenWindows[i];

	    if(objWindow)
		    objWindow.close();

		objMainFrame.arrOpenWindows[i] = null;
	}

	objMainFrame.arrOpenWindows.length = 0;
}

function getChildWindowParams() {
	var child_params;

	child_params = 'width=' + objMainFrame.iEPCWindowWidth + ',height=' + objMainFrame.iEPCWindowHeight + ',screenX=10,screenY=10,left=10,top=10';

	return child_params;
}

function getParentRow(objElement) {
	var objRow = objElement;

	while (objRow && ('TR' != objRow.tagName.toUpperCase())) {
		objRow = objRow.parentNode;
	}

	return objRow;
}

function shrinkWinToFitContent(iContentWidth, iContentHeight) {
	shrinkWinWidthToFit(iContentWidth);
	shrinkWinHeightToFit(iContentHeight);
}

function shrinkWinWidthToFit(iContentWidth) {
	if (document.body.offsetWidth && !isNaN(iContentWidth) && 16 < iContentWidth) {
		var iBufferWidth = 12;
		var iBodyWidth = (bIsIE) ? parseInt(document.body.offsetWidth) : window.innerWidth - 30;
		var iTotalScreenRoom = parseInt(screen.availWidth) - parseInt(bIsIE ? window.screenLeft : window.screenX) - iBufferWidth;
		iContentWidth += iBufferWidth; 

		if (iContentWidth < iTotalScreenRoom) {
			// The window is smaller than the screen -- make it as large as it needs to be.
			if (window.dialogWidth) {
				window.dialogWidth = ((iContentWidth + iBrowserTitleBarWidth) * iDPIMult) + 'px';
			}
			else {
				window.resizeBy((iContentWidth - iBodyWidth), 0);
				document.body.scroll = 'no';
			}
		}
		else {
			// Increase the width of the window to fit the screen.
			// This situation will not happen with a dialog window; the browser
			// will relocate the window to display all of its content.
			window.resizeBy((iTotalScreenRoom - iBodyWidth), 0);
		}
	}
}

function shrinkWinHeightToFit(iContentHeight) {
	if (document.body.offsetHeight && !isNaN(iContentHeight) && 16 < iContentHeight) {
		var iBufferHeight = 0;
		var iBodyHeight = (bIsIE) ? parseInt(document.body.offsetHeight) : window.innerHeight;

		var iTotalScreenRoom = parseInt(screen.availHeight) -  parseInt(bIsIE ? window.screenTop : window.screenY) - iBufferHeight;
		iContentHeight += iBufferHeight; 

		if (iTotalScreenRoom > iContentHeight) {
			// Increase the height of the window to fit the content.
			if (window.dialogHeight) {
				window.dialogHeight = ((iContentHeight * iDPIMult) + iBrowserTitleBarHeight) + 'px';
			}
			else {
				window.resizeBy(0, (iContentHeight - iBodyHeight));
				document.body.scroll = 'no';
			}
		}
		else {
			// Increase the height of the window to fit the screen.
			// This situation will not happen with a dialog window; the browser
			// will relocate the window to display all of its content.
			window.resizeBy(0, (iTotalScreenRoom - iBodyHeight));
		}
	}
}

function sizeToFit(objToSize, objToFit, iBuffer) {
	if (objToSize && objToFit) {
		if (isNaN(iBuffer)) iBuffer = 0;
		if ((objToFit.offsetHeight + iBuffer) < objToSize.offsetHeight) objToSize.style.height = (objToFit.offsetHeight + iBuffer) + 'px';
	}
}

function setActiveObj(objNew) {
	objInitFocusField = objNew;
}

function unsetActiveObj() {
	objInitFocusField = null;
}

function processAddToPick(sPNID) {
	if (sPNID) {
		var objPN = document.getElementById('pn' + sPNID);
		if (objPN) objPN.className = 'onPkLst';
	}

	if (objMainFrame && objMainFrame.refreshPickList) objMainFrame.refreshPickList();
}

function doTabPageResize() {
	var iHeight = window.innerHeight - iWindowHeightBuffer;

	try {
		try {
			if (bIsNS) objSearchResultsGroup.style.height = iHeight;
		}
		catch (e) {
		}
		finally {
			objSearchResultsFrame.style.height = iHeight - iGroupMargin;
		}
	}
	catch (e) {}

	return true;
}

function getFadeHeader(sHeaderText, sCellID) {
	var sHeader = '<TD STYLE="font-size:8pt; font-weight:Bold; color:#FFFFFF; padding:0px 0px 2px 2px;  ';

	if (bIsIE)
		sHeader += 'background-color:#14100F; filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=70, FinishOpacity=10, Style=1, StartX=0,  FinishX=100, StartY=0, FinishY=0);';
	else
		sHeader += 'background-color:#3F312E;';

	if (sCellID) sHeader += '" ID="' + sCellID;
	sHeader += '">' + sHeaderText + '</TD>';

	return sHeader;
}

function doHelpMenuOver(objElement) {
	try {
		if (bPLMenuVisible) doMenuOutImmediate();
	}
	catch (e) {}
	
	if (objElement) objElement.className = 'MenuHeaderOver';
}

function doHelpMenuOut(objElement) {
	if (objElement) objElement.className = 'MenuHeader';
}

function linkOut(objEvent) {
	window.status = '';
}

function disableSelect(objEvent) {
	var objElement = (bIsIE) ? event.srcElement : objEvent.target;

	if (objElement && 'INPUT' != objElement.tagName.toUpperCase()) return false;
} 

function enableSelect(objEvent) {
	return true;
} 

function GetLocalString(sIdent, vArg0, vArg1, vArg2, vArg3, vArg4, vArg5, vArg6, vArg7, vArg8, vArg9) {
	sIdent = sIdent.toUpperCase();

	try {
		var objRetVal = objMainFrame[sIdent];
	}
	catch(e) {
  		objRetVal = 'ERROR: ' + sIdent;
	}

	if(objRetVal && arguments.length > 1)
    		return strFormat(objRetVal, vArg0, vArg1, vArg2, vArg3, vArg4, vArg5, vArg6, vArg7, vArg8, vArg9);

	return objRetVal ? objRetVal : '';
}

function goToMain() {
	if (!objMainFrame) {
		if (window.dialogArguments)
			objMainFrame = window.dialogArguments.top;
		else if (window.opener)
			objMainFrame = window.opener.top;
		else
			objMainFrame = window.top;
	}

	if(objMainFrame.sMainPageLink && objMainFrame.sMainPageLink.length > 0) {
		if(bIsIE)
			objMainFrame.location = objMainFrame.sMainPageLink;
		else
			objMainFrame.location.replace(objMainFrame.sMainPageLink);
	}
	else if(objMainFrame.sSiteURL) {
		if(bIsIE)
			objMainFrame.location = objMainFrame.sSiteURL;
		else
			objMainFrame.location.replace(objMainFrame.sSiteURL);
	}
	else {
		history.go(-1);
	}

	if (window.dialogArguments)
		window.close();
	else if (window.opener)
		top.window.close();
}

function escapeForHTML(sString) {
	var sRetVal = '';

	for(var i = 0; i < sString.length; ++i) {
		var ch = sString.charAt(i);

		switch(ch) {
		case '<':
			sRetVal += '&lt;';
			break;
		case '>':
			sRetVal += '&gt;';
			break;
		case '&':
			sRetVal += '&amp;';
			break;
		case ' ':
			sRetVal += '&nbsp;';
			break;
		default:
			sRetVal += ch;
			break;
		}
	}

	return sRetVal;
}

function unescapeHTML(sString) {
	var sRetVal = '';

	for(var i = 0; i < sString.length; ++i) {
		var ch = sString.charAt(i);

		if(ch == '&') {
			var sSubString = sString.substring(i);

			if(sSubString.indexOf('&amp;') == 0) {
				sRetVal += '&';
				i += 4;
			}
			else if(sSubString.indexOf('&gt;') == 0) {
				sRetVal += '>';
				i += 3;
			}
			else if(sSubString.indexOf('&lt;') == 0) {
				sRetVal += '<';
				i += 3;
			}
			else if(sSubString.indexOf('&nbsp;') == 0) {
				sRetVal += ' ';
				i += 5;
			}
			else {
				sRetVal += ch;
			}
		}
		else {
			sRetVal += ch;
		}
	}

	return sRetVal;
}

function addToPicklist(sPartNum, sPartDesc, sQty, sCatCodeXXX) { 
	// Change sCatCodeXXX to sCatCode when back end passes proper values
	if( objMainFrame.bIsCartApp ) {
		var sURL = objMainFrame.sShopURL + '&ProductID=' + escape(sPartNum) + '&ProductQlfr=' + sCatCode;

		openWindow(window, sURL, 'cartWindow', 'width=350,height=200');
	}
	else if( objMainFrame.bIsCausalApp ) {
		var bHasQueryString = (objMainFrame.sXMLPostDestination.indexOf('?') > -1);

		var sURL = objMainFrame.sXMLPostDestination + (bHasQueryString ? '&' : '?') + objMainFrame.sXMLFieldName + '=' + escape(sPartNum) + '&Description=' + escape(sPartDesc);

		openWindow(window, sURL, objMainFrame.sXMLPostTarget, '');
	}
	else {
		var sURL = sEMURL + '&mode=31&DBID=' + objMainFrame.getCurrentOEMID() + '&PartName=' + escape(sPartNum) + '&PartDesc=' + escape(sPartDesc) + '&Qty=' + escape(sQty);

		openModalDialog(sURL, 'addPickItem', self, 300, 145);
	}
}

function getCopyrightMessage(sOEMDesc) {
  var copy_string = objMainFrame.arrCopyrightMessages[sOEMDesc];
 
  return (copy_string == null ? objMainFrame.arrApplicationTitle[objMainFrame.sCurrLang] : copy_string);
}

