top.requireJS = function(packageName,fileName)
{
//   top.dojo.registerModulePath(packageName, "../"+packageName);
   top.dojo.require(packageName+"."+fileName);
}

//----------------------------------------------------------------------------
// Trim
//----------------------------------------------------------------------------
function trimStr(str, size)
{
	if (str.length<=size)
		return str;
	return str.substring(0,size-3)+"...";
}

function writeTrimStr(doc, str, size)
{
	doc.write(trimStr(str, size));
}

// Preform the maxlength property on textarea input - prevent oppertunity to paste text
function textLimit (field, maxlen, maxlines)
{
   var fieldStr = field.value;
   if(maxlines)
   {
      var fieldLines = fieldStr.split("\n");
      if(fieldLines.length > maxlines)
      {
         fieldStr = "";
         for(var i = 0; i < maxlines; i++)
         {
            fieldStr += fieldLines[i];
            if(i+1 < maxlines)
               fieldStr += "\n";
         }
         field.value = fieldStr;
      }
   }
   if(maxlen)
   {
      if (fieldStr.length > maxlen-1)
         field.value = fieldStr.substring(0, maxlen-1);
   }
}

//------------------------------------------------------------------------------------
// Name        : disableAutoComplete(tagName)
// Description : Disable the auto complete option from a form
// Parameters  : tagName - the name of the tag which the auto complete is going to be removed from
//------------------------------------------------------------------------------------
function disableAutoComplete (tagName)
{
   var forms = document.body.getElementsByTagName(tagName);
   if (forms != null)
   {
       for (i=0; i < forms.length; i++)
         forms[i].autocomplete = "off";
   }
}
// Used by the landing page flash
function navigateToArticle (articleOrd)
{
   var artilceOrdVal = 0;
   if(articleOrd)
   {
      if(typeof articleOrd == "string")
      {
         if(!isNaN(parseInt(articleOrd)))
         {
            artilceOrdVal = parseInt(articleOrd);
         }
      }
      else if(typeof articleOrd == "number")
      {
         artilceOrdVal = articleOrd;
      }
   }

   top.getWorkArea().document.location.href =
         top.addContextPath("/navigateBook.do?operation=gotoChapterOperation&chapterOrd="+artilceOrdVal);
}

// Used by the landing page flash
function gotoMarketingPage()
{
   top.document.location.href = top.addContextPath("/memorybookMarketingPageTop.do");
}

// used by landing page
function setParentIframeHeight(frameWin, iframeId, newHeight)
{
   var theIframe = frameWin.parent.document.getElementById(iframeId);
   if(theIframe && (theIframe.tagName.toLowerCase()=="iframe"))
   {
      theIframe.style.height = newHeight;
   }
}

// used by landing page
function navigateToCreateBookMakingSpace()
{
   top.getWorkArea().document.location.href = top.addContextPath("/createBookMakingSpacePage.do");
}

// used be MemoryBook marketing pages
function startNow(doc)
{
   var selectedVal = "" ;
   var collection = doc.all("mbType");
   if (collection == null)
   {
      selectedVal = "hardCover";
   }
   else
   {
      for (i=0; i<collection.length; i++)
      {
         if (doc.all("mbType")[i].checked)
            selectedVal = doc.all("mbType")[i].value;
      }
   }

   //if user did not select anything and pressed the button
   if (selectedVal == "")
   {
      return false;
   }
   else
   {
      //if user selected "saved" memorybook
      if (selectedVal == "saved")
      {
         doc.getElementById('start_now').onclick = "" ;
         top.document.location.href = top.getContextPath()+"/detourExpressBookTop.do";
      }
      //else user creates new
      else
      {
         var coverTypeStrParamValue = "coverTypeStrParam=" + selectedVal ;
         //if already has a saved memorybook he will get a warning it will be deleted
         if (top.infoMarketing.savedMemoryBookId != "")
         {
            var dlg = getWorkArea().dojo.widget.byId("DialogContent");
            dlg.clickYes = function()
            {
               doc.location.href = top.getContextPath()+"/detourMarketingPagePost.do?deleteCurrentMemoryBook=true&"+coverTypeStrParamValue;
               this.hide();
               disableStartNowButton(doc);
            }
            dlg.show();
         }
         //else - doesn't have a saved memorybook, creates new
         else
         {
            doc.location.href = top.getContextPath()+"/detourMarketingPagePost.do?"+coverTypeStrParamValue;
            disableStartNowButton(doc);
         }
      }
   }
}

// Called from "Welcome Page" (new marketing page)  
function continueFromWelcomePage(doc)
{
   var gotoUrl = top.getContextPath()+ "/openBook.do";
   top.getWorkArea().document.location.replace(gotoUrl);
   disableStartNowButton(doc);
}

// Called from "startNow()"
function disableStartNowButton(doc)
{
   // replace the "Start Now" button with an animated-disabled button (if exists)
   // NOTE: trying to put this code before the submit resulted in the image disappearing the first time when its not yet in the cache.
   if( doc )
   {
      var startNowImgObj = doc.getElementById("startNowImg");
      if( startNowImgObj )
      {
         startNowImgObj.src = "images/btn_start_now_disabled.gif";
      }
   }
}

function getWorkArea()
{
   return top.workArea;
}

function getMenubarArea()
{
   var retVal = null;
   if( top != undefined )
   {
      retVal =  top.menubar;
   }
   return retVal;
}
function getMenubarAreaDocument()
{
   var retVal = null;
   if( top.menubar != undefined )
   {
      retVal =  top.menubar.document;
   }
   return retVal;
}

function getCacheFrame()
{
   return top.cacheFrame;
}

function getLogFrame()
{
   return top.logFrame;
}

function getBookArea()
{
   return top.getWorkArea().bookArea;
}

top.getBookAreaDocument = function ()
{
   return top.getWorkArea().bookArea.document;
}

top.getMediaArea = function ()
{
   return top.getWorkArea().mediaArea;
}

top.getMediaAreaDocument = function ()
{
   return top.getWorkArea().mediaArea.document;
}

top.isPPEvent = false;
top.isPromptedPostEvent = function ()
{
   return top.isPPEvent;
}

top.setPromptedPostEvent = function ( pIsPPEvent )
{
   top.isPPEvent = pIsPPEvent;
}

