// Set the active menu item
function setActiveMenuItem(asItemId)
{  
  if (parent.frames["menuframe"] != null &&
      parent.frames["menuframe"].document.getElementById(asItemId) != null)
  {
    var itemPos = parent.frames["menuframe"].getMenuItemPos(parent.frames["menuframe"].document.getElementById(asItemId));
    parent.frames["menuframe"].setActiveItem(parent.frames["menuframe"].document.getElementById(asItemId));
    parent.frames["menuframe"].setMenuItemPos(parent.frames["menuframe"].document.getElementById(asItemId), itemPos);
  }  
}

// Set the active row
function setRowHighlight(oldRowObj, newRowObj)
{
	if (oldRowObj != null)
		oldRowObj.className = "RowNoHighlight";
	newRowObj.className = "RowHighlight";
}

// This function will set the height and width of a div object to force scrolling.
// The first parameter will be used for setting up the height, and the second
// parameter will be used for setting up the width.
// Each parameter can be either a string that represents the name of the div object,
// or an Array of 2 elements containing the name of the div object and the amount of "offset".
// The div object with the given name will be set to the dimensions of the body,
// less the offset.  If no offset is provided, a height offset of 70 pixels will be
// used (the space of the page header and location bar), and a width offset of 0 pixels
// will be used as defaults.
// Finally, a function is attached to the onresize event of the window object.
// The attached function will reset the height and width of the div object using
// arrays containing the div name and offset, both for the div's height and width.
function setResizableDiv(heightParam, widthParam)
{
	var heightOffset = 0;
	var heightDivObj = null;
	if (typeof(heightParam) != "string")		// if not a string then an array
	{
		heightDivObj = heightParam[0];		// set the div object to the first element
		heightOffset = heightParam[1];		// set the offset to the second element
	} else {
		heightDivObj = heightParam;		// set the div object to the string
		heightOffset = 70;			// set the offset to 70 (used in splash pages)
	}

	if (widthParam != null)
	{
		var widthOffset = 0;
		var widthDivObj = null;
		if (typeof(widthParam) != "string")		// if not a string then an array
		{
			widthDivObj = widthParam[0];		// set the div object to the first element
			widthOffset = widthParam[1];		// set the offset to the second element
		} else {
			widthDivObj = widthParam;		// set the div object to the string
			widthOffset = 0;			// set the offset to 0
		}
		addEvent(window, 'onresize', getOnResizable(new Array(document.getElementById(heightDivObj), heightOffset), new Array(document.getElementById(widthDivObj), widthOffset)));
		document.getElementById(widthDivObj).style.width = (document.body.offsetWidth-widthOffset);
	} else {
		addEvent(window, 'onresize', getOnResizable(new Array(document.getElementById(heightDivObj), heightOffset)));
	}
	document.getElementById(heightDivObj).style.height = (document.body.offsetHeight-heightOffset);
}

// *** Pop-up Window Functions ***

//generic popup window
function popUpWindow(url)
{
	newwindow=window.open(url,'hello','height=550,width=790,top=100,left=200,resizable=yes,location=no,titlebar=no,menubar=no,scrollbars=yes,toolbar=no,status=no');
	if (window.focus) 
	{
		newwindow.focus()
	}
} 

// display help in a popup window
function displayHelp(helpName, qgsessionid)
{
	var windowHelp = window.open("/throwback/utils/showHelp.do?helpname=" + helpName + "&QGSID=" + qgsessionid, "CHPJHelp",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=400,width=400");
	windowHelp.focus();
}

// display info in a popup window
function displayInfo(infoFileName)
{
	var windowInfo = window.open("/throwback/utils/qginfo/" + infoFileName, "CHPJInfo",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=400,width=400");
	windowInfo.focus();
}

// display paper offerings search page in a popup window
function displayPaperOfferingsSearch(qgsessionid, grade, minBasisWeight, maxBasisWeight, minRollWidth, maxRollWidth, printable)
{
		var arg1="/throwback/paper/offerings/showPaperOfferingsSearch.do?QGSID=" + qgsessionid 
			+ "&grade=" + escape(grade) + "&minBasisWeight=" + minBasisWeight 
			+ "&maxBasisWeight=" + maxBasisWeight + "&minRollWidth=" 
			+ minRollWidth + "&maxRollWidth=" + maxRollWidth + "&printable=" + printable;
		var arg2="CHPJPaperOfferingsSearch";
		var arg3="dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no,"+
			"titlebar=no,resizable=yes,top=5,left=5,height=250,width=400";
 		windowEdit = window.open(arg1, arg2, arg3);   
		windowEdit.focus();
}


// display paper offerings information request in a popup window
function displayPaperOfferingsRequest(qgsessionid, lotNumber, printable) {
	var windowRequest = window.open("/throwback/paper/offerings/showPaperOfferingsRequest.do?QGSID=" + qgsessionid + "&lotNumber=" + lotNumber + "&printable=" + printable,
			"CHPJPaperOfferingsRequestForm", 
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=330,width=400");
    windowRequest.focus();
    return windowRequest;
}

// display product request new in a popup window
function displayProductRequestNew(qgsessionid)
{
	var windowEdit = window.open("/throwback/postproduction/productrequest/showProductRequestEditor.do?new=true&QGSID=" + qgsessionid, "CHPJProductRequestEdit",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=430,width=600");
	windowEdit.focus();
}

// display product request edit in a popup window
function displayProductRequestEdit(prodReqId, qgsessionid)
{
	var windowEdit = window.open("/throwback/postproduction/productrequest/showProductRequestEditor.do?id=" + prodReqId + "&QGSID=" + qgsessionid, "CHPJProductRequestEdit",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=430,width=600");
	windowEdit.focus();
}

// display product request view in a popup window
function displayProductRequestView(prodReqId, qgsessionid)
{
	var windowView = window.open("/throwback/postproduction/productrequest/showProductRequestViewer.do?id=" + prodReqId + "&QGSID=" + qgsessionid, "CHPJProductRequestView",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=390,width=500");
	windowView.focus();
}

// display product request details edit in a popup window
function displayProductRequestDetailsEdit(prodReqId, qgsessionid)
{
	var windowEdit = window.open("/throwback/postproduction/productrequest/showProductRequestDetails.do?id=" + prodReqId + "&editable=true&QGSID=" + qgsessionid, "CHPJProductRequestDetailsEdit",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=310,width=500");
	windowEdit.focus();
}

// display product summary in a popup window
function displayProductRequestSummary(prodReqId, qgsessionid)
{
	var windowEdit = window.open("/throwback/postproduction/productrequest/showProductRequestSummary.do?id=" + prodReqId + "&QGSID=" + qgsessionid, "CHPJProductRequestSummary",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=500,width=640");
	windowEdit.focus();
}

// display product request note edit in a popup window
function displayProductRequestNoteEdit(prodReqId, editable, qgsessionid)
{
	var windowEdit = window.open("/throwback/postproduction/productrequest/showProductRequestNoteEditor.do?id=" + prodReqId + "&editable=" + editable + "&QGSID=" + qgsessionid, "CHPJProductRequestNoteEdit",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5,height=310,width=500");
	windowEdit.focus();
}

// display product request note edit in a popup window
function displayTitleSearch(qgsessionid)
{
	var searchWindow = window.open("/throwback/mainframe/showTitleSearch.do?QGSID=" + qgsessionid, "TitleSearch",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=120,left=50,height=210,width=400");
	searchWindow.focus();
}

// display product request tracking in a popup window
function displayProductRequestTracking(shipServiceName, trackingNumber, customerId)
{
    shipServiceName = shipServiceName.toUpperCase();
    if( shipServiceName.indexOf("UPS") > -1 )
    {
       var windowTrck = window.open("http://wwwapps.ups.com/etracking/tracking.cgi?tracknums_displayed=1&TypeOfInquiryNumber=T&HTMLVersion=4.0&InquiryNumber1=" + trackingNumber + "&track=Track",
                           "UPSTracking",
                           "dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
                           "titlebar=no,resizable=yes,scrollbars=yes,height=550,width=750");
	   windowTrck.focus();
    } 
    else if( shipServiceName.indexOf("FEDEX") > -1 || shipServiceName.indexOf("RPS") > -1 )
    {
       var windowTrck = window.open("http://www.fedex.com/cgi-bin/tracking?action=track&language=english&cntry_code=us&initial=x&tracknumbers=" + trackingNumber,
                          "FedexTracking",
                          "dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
                          "titlebar=no,resizable=yes,scrollbars=yes,height=550,width=750");
	   windowTrck.focus();
    } 
    else if( shipServiceName.indexOf("PARCEL DIRECT") > -1 && customerId != null && customerId.length > 0)
    {
       var windowTrck = window.open("https://www.parceldirect.com/pd/tracking.htm?bgcolor=WHITE&CID=" + customerId + "&PID=" + trackingNumber,
                          "ParcelDirectTracking",
                          "dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
                          "titlebar=no,resizable=yes,scrollbars=yes,height=550,width=750");
	   windowTrck.focus();
    }
    else if( shipServiceName.indexOf("QUALITY AIR") > -1)
    {
       var windowTrck = window.open("http://67.37.103.10/ShipmentInquiry.asp?SReviewHAST=QAF&SReviewHAWB=" + trackingNumber,
                          "ParcelDirectTracking",
                          "dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
                          "titlebar=no,resizable=yes,scrollbars=yes,height=550,width=750");
	   windowTrck.focus();
    }
    else
    {
       alert("No Online Tracking available" );
    }

}

// display DBLOnline in a popup window using a link and a session id
// if a job number and a signature name are given, append those to the query string also
function displayDBLOnline(link, qgsessionid, jobNumber, signatureName)
{
	var dblOnlineLink = link + "?QGSID=" + qgsessionid;
    if (jobNumber != null && signatureName != null)
    {
    	dblOnlineLink += "&jobnumber=" + jobNumber + "&signaturename=" + signatureName;
    }
    
    var location = "no";
    if( is_ie && is_major >= 7  )
    {
    	location = "yes";
    }
    
    var windowDBL = window.open(dblOnlineLink, "DBLOnline",
                       "dependent=no,directories=no,location=" + location + ",menubar=no,status=no,toolbar=yes," + 
                       "titlebar=no,resizable=yes,scrollbars=yes,height=550,width=790");

	windowDBL.focus();
}

// display furnished product image in a popup window
function displayFurnishedProductImage(invId, filename, qgsessionid)
{
	var windowImage = window.open("/throwback/furnishedproduct/showImage.do?id=" + invId + "&filename=" + filename + "&QGSID=" + qgsessionid, "CHPJFurnishedProductImage",
			"dependent=yes,directories=no,location=no,menubar=no,status=no,toolbar=no," + 
            "titlebar=no,resizable=yes,top=5,left=5");
	windowImage.focus();
}

function wrapDisplayCalendarForInput(top, left, input, qgsessionid)
{
	displayCalendarForInput(top, left, input, qgsessionid);
}

// this function will popup a window with a calendar, positioned according to
// the values passed in as "top" and "left".  the form input field that should receive
// the chosen date is passed in as the 3rd argument
function displayCalendarForInput(top, left, input, qgsessionid)
{
	var windowCal = window.open("/throwback/utils/Calendar.jsp?datefield=" + input.id + "&currdate=" + input.value + "&QGSID=" + qgsessionid, "CHPJCalendar",
			"location=no,directories=no,menubar=no,toolbar=no,titlebar=no,scrollbars=yes,status=no,resizable=yes,"
			+ "top=" + top + ",left=" + left + ",height=200,width=200");
	windowCal.focus();
}

// this function will popup a window with a calendar, positioned according to
// the values passed in as "top" and "left".  the form input field that should receive
// the chosen date is passed in as the 3rd argument.  the multieditor field that
// should be flagged upon change is passed in as the 4th argument.
function displayCalendarForMultieditor(top, left, input, changeField, qgsessionid)
{
	var windowCal = window.open("/throwback/utils/Calendar.jsp?datefield=" + input.id + "&currdate=" + input.value + "&changefield=" + changeField.id + "&QGSID=" + qgsessionid, "CHPJCalendar",
			"location=no,directories=no,menubar=no,toolbar=no,titlebar=no,scrollbars=yes,status=no,resizable=yes,"
			+ "top=" + top + ",left=" + left + ",height=200,width=200");
	windowCal.focus();
}

// this function will popup a window with a calendar, positioned according to
// the values passed in as "top" and "left".
function displayCalendar(top, left, qgsessionid)
{
	var windowCal = window.open("/throwback/utils/Calendar.jsp?QGSID=" + qgsessionid, "CHPJCalendar",
			"location=no,directories=no,menubar=no,toolbar=no,titlebar=no,scrollbars=yes,status=no,resizable=yes,"
			+ "top=" + top + ",left=" + left + ",height=220,width=200");
	windowCal.focus();
}

// this function tests the browsers size to make sure that it is of the 
// minimum height and width
// Check the browser size for minimum requirements
function checkBrowserSize (w, h)
{
  var liBrowserWidth;
  var liBrowserHeight;
  var liScreenWidth;
  var liScreenHeight;
  var liMinimumWidth = parseInt(w);
  var liMinimumHeight = parseInt(h);
  
  //alert("in checkBrowserSize(" + w + ", " + h + ")");
  
  liBrowserWidth = document.body.offsetWidth;
  liBrowserHeight = document.body.offsetHeight;
  liScreenWidth = top.screen.width;
  liScreenHeight = top.screen.height;
  
  //alert("liBrowserWidth = " + liBrowserWidth + "\n liBrowserHeight = " + liBrowserHeight + "\n liScreenWidth = " + liScreenWidth + "\n liScreenHeight = " + liScreenHeight);  
  
  if (liBrowserWidth < 778 || liBrowserHeight < 432)
  {
    // Browser is too small
    if (liScreenWidth < liMinimumWidth)
    {
      // Need to maximize broswer
      alert ("For optimal viewing, please increase your monitor's display resolution.");
    }
    else if (liScreenWidth == liMinimumWidth)
    {
      // Need to maximize broswer
      alert ("For optimal viewing, please maximize your browser window.");
    }
    else
    {
      // Need to increase broswer size
      alert ("For optimal viewing, please increase the size of your browser window.");
    }
  }
}

// this function will popup a window with a message, positioned according to
// the values passed in as "top" and "left".
/*function displayAlert(top, left, message)
{
	var windowAlert = window.open("/throwback/utils/Alert.jsp?message=" + message, "CHPJAlert",
			"location=no,directories=no,menubar=no,toolbar=no,titlebar=no,scrollbars=yes,status=no,resizable=yes,"
			+ "top=" + top + ",left=" + left + ",height=150,width=250");
	// mac ie gives js errors when setting the focus when this function is called from an onblur event
	if (!(is_mac && is_ie))
		windowAlert.focus();
}*/
	
	
function enableImage(imgObj, pathToImage)
{
	imgObj.disabled = false;
	imgObj.className = "GeneralButton";
	
	if (pathToImage != null)
		imgObj.src = pathToImage;
}

function disableImage(imgObj, pathToImage)
{
	imgObj.className = "";
	imgObj.disabled = true;
	
	if (pathToImage != null)
		imgObj.src = pathToImage;
}

function isImageEnabled(imgObj)
{
	if (imgObj.className == null || imgObj.className == "" || imgObj.disabled == true)
		return false;
	else
		return true;
}

// *** Attach Event Functions ***

// This function will attach the event "eventName" to "objElement" passing
// in an anonymous function which wraps up the desired function and its arguments
// with which it needs to fire.

// In the most complicated case, the calling JavaScript should look like this:
// addEvent(newInputField, "onclick", 
// function () { displayCalendarForShipType(270, 200, shipTypeFieldName, 
// dateType, document.getElementById(targetInput)); });
function addEvent(objElement, eventName, functionWrapper)
{
	if (is_mac && is_ie)
		addMacEvent(objElement, eventName, functionWrapper);
	else if (is_gecko || is_safari)
		objElement.addEventListener(eventName.substring("on".length), functionWrapper, false);
	else
		objElement.attachEvent(eventName, functionWrapper);
}

var windowOnLoadEvents = new Array();
var windowOnResizeEvents = new Array();

function addMacEvent(objElement, eventName, functionWrapper)
{
	if (objElement == window)
	{
		if (eventName == "onresize")
			windowOnResizeEvents[windowOnResizeEvents.length] = functionWrapper;
		else
			windowOnLoadEvents[windowOnLoadEvents.length] = functionWrapper;
	}
	else
	 	eval("objElement."+eventName+" = "+functionWrapper);
}

function performEventFunctions(eventType)
{
  for (var i=0; i<eval(eventType+"Events").length; i++)
  {
    eval(eventType+"Events")[i]();
  }
}

window.onload = function() { performEventFunctions("windowOnLoad") };
window.onresize = function () { performEventFunctions("windowOnResize") };

// this function will return a function that will set the value of the object "statusField"
// to "changed"
function getOnEditable(statusField)
{
  return function()
  {
    document.getElementById(statusField).value="changed";
  };
}


// this function will return a function that will set the height of the first element
// in heightParamArray to the offsetHeight of the body of the document less the value of
// the second element in heightParamArray
// it will then set the width of the first element in widthParamArray to the
// offsetWidth of the body of the document less the value of the second element
// in widthParamArray
// if either array has 4 elements, the third and fourth elements will be used to
// set the dimensions in the same way as the first and second elements were used.
function getOnResizable(heightParamArray, widthParamArray)
{
  return function()
  {
    heightParamArray[0].style.height = (document.body.offsetHeight-heightParamArray[1]);
    if (heightParamArray.length == 4)
    	heightParamArray[2].style.height = (document.body.offsetHeight-heightParamArray[3]);
    if (widthParamArray != null)
    {
    	widthParamArray[0].style.width = (document.body.offsetWidth-widthParamArray[1]);
    	if (widthParamArray.length == 4)
    		widthParamArray[2].style.width = (document.body.offsetWidth-widthParamArray[3]);
    }
  };
}

String.prototype.LTrim = new Function("return this.replace(/^\\s+/,'')");
String.prototype.RTrim = new Function("return this.replace(/\\s+$/,'')");
String.prototype.Trim = new Function("return this.replace(/^\\s+|\\s+$/,'')");