/*-----------------------------------------------------------------------------
-	File Name:
-		PayPalShared.js
-
-	Description:
-		Shared functions for PayPal objects.
-
-	This file contains proprietary and confidential information from WebAssist.com
-	corporation.  Any unauthorized reuse, reproduction, or modification without
-	the prior written consent of WebAssist.com is strictly prohibited.
-
-	Copyright 2003 WebAssist.com Corporation.  All rights reserved.
------------------------------------------------------------------------------*/
var BNVersion = "macromedia.contribute.3_0_0"; // MACR Solution ID
var MRB = "R-2K410090UE355584L"; // MACR Program ID
var PAL = "A3X7N2U4UM6U8";  // MACR affiliate ID
var MRBLINK = "https://www.paypal.com/mrb/mrb="+MRB+"&pal="+PAL;
var DWConfigPath = dw.getConfigurationPath();
var PayPalConfigFilePath = WA_getConfigurationPath("/shared/PayPal/PayPalConfig.xml");
var PayPalSIPTempPath = WA_getConfigurationPath("/shared/PayPal/PayPalSIPTemp.txt");
var DocPath = dw.getDocumentPath("document");
var FolderURL = DocPath.substring(0,DocPath.lastIndexOf("/")+1);
var FolderPath = MMNotes.localURLToFilePath(FolderURL);
var CUSTOM_VC_IMG = "PayPal VC PI.gif";
var CUSTOM_AC_IMG = "PayPal AC PI.gif";
var CUSTOM_SIP_IMG = "PayPal SIP PI.gif";
var CUSTOM_SUB_IMG = "PayPal SUB PI.gif";
var WIZARD_INTERFACE = "Wizard";
var ADVANCED_INTERFACE = "Advanced";

// MAC's only allow filenames up to 32 characters long
var MAX_FILE_LENGTH = 32;

/**************************** errormessages ****************************/

//--------------- LOCALIZEABLE GLOBALS ---------------

var MSG_NoUID = "You must enter your PayPal Account (i.e. user@mycompany.com).";
var MSG_NoItemName = "You must enter a name for this item (i.e. 'My Widget')";
var MSG_NoItemPrice = "You must enter a price for this item (i.e. 19.99).";
var MSG_NoSubPrice = "You must enter an amount to charge customers for this subscription (i.e. 19.99).";
var MSG_NoSubPriceValidAmount = "You must enter a valid amount to charge customers for this subscription (i.e. 19.99 or 100).";
var MSG_NoSubTrial1ValidAmount = "You must enter a valid amount to bill for your Trial Period (i.e. 19.99 or 100).";
var MSG_InvalidAmount = "You must enter a valid amount for the price of this item (i.e. 19.99 or 100).";
var MSG_AlreadyPayPalInForm = "This form already contains Paypal functionality.\rPlease ensure that your cursor is outside the current form or create a new form.";
var MSG_AlreadyInForm = "Your selection is currently inside an existing form.\nWould you like to insert this PayPal button after the current form?";

//---------------   END LOCALIZEABLE   ---------------


/***************************** open broswer windows ********************/
function displayHelp()     {
  displayPPGeneralHelp("");
}

//context sensitive help function
function displayContextHelp(inputAnchor)     {
  var fileStr  = dw.getConfigurationPath();
  var tempPath = WA_getConfigurationPath("/Shared/PayPal/ContextTemp.htm");
  var browsePath = tempPath;  
  var helpPath = WA_getConfigurationPath("/Shared/PayPal/PayPalHelp.htm");

  if (!DWfile.exists(tempPath))
  {
    tempPath = dw.getTempFolderPath(); // force to use local user paths
    var folderPath = tempPath.substring(0, tempPath.lastIndexOf("/"));
    tempPath =  tempPath.substring(0, tempPath.lastIndexOf("/")) + "/Shared/PayPal/ContextTemp.htm";
    browsePath = tempPath;
    DWfile.createFolder(folderPath+"/Shared");
    DWfile.createFolder(folderPath+"/Shared/PayPal");
  }

    scriptTag  = "<sc" + "ript language='JavaScript'>\n\r";
    var cfgPath  = dw.getConfigurationPath();
    var fPath = dw.getTempFolderPath();
    fPath     = fPath.substring(0, fPath.lastIndexOf("/"));
    if (DWfile.exists(fPath + "/Shared/PayPal/PayPalHelp.htm"))     {
      cfgPath = fPath;
    }

    if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     {
      cfgPath = cfgPath.replace(/file\:\/\/\//i, "");
  	  browsePath = browsePath.replace(/file\:\/\/\//i, "");  
	    if (cfgPath.substring(0, cfgPath.indexOf("/")) == browsePath.substring(0, browsePath.indexOf("/")))
	    {
	      cfgPath = "file://localhost" + cfgPath.substring(cfgPath.indexOf("/"));
  	  }
	    else
	    {
	      cfgPath = "file://localhost/Volumes/" + cfgPath;	  
  	  }
      scriptTag += "var helpLoc = \"" + escape(cfgPath) + "/Shared/PayPal/PayPalHelp.htm\";\n\r"; 
	    browsePath = "file://localhost" + browsePath.substring(browsePath.indexOf("/")); 
    }
    else     {
      scriptTag += "var helpLoc = \"" + cfgPath + "/Shared/PayPal/PayPalHelp.htm\";\n\r";
    }
    if (inputAnchor && inputAnchor != "")     {
      scriptTag += "helpLoc += \"#" + inputAnchor + "\";\n\r";
    }
    scriptTag += "document.location.href = helpLoc;\n\r";
    scriptTag += "</sc" + "ript>";
    DWfile.write(tempPath, scriptTag);
    dreamweaver.browseDocument(browsePath);  
}

function WA_getConfigurationPath(pathFromConfig)     {
  var cPath = dw.getConfigurationPath();
  var tPath  = dw.getTempFolderPath();
  tPath  = tPath.substring(0, tPath.lastIndexOf("/"));
  tPath += pathFromConfig;
  if (DWfile.exists(tPath))     {
    return tPath;
  }
  return cPath + pathFromConfig;
}

function displayPPGeneralHelp(inputAnchor)     {
  var fileStr  = dw.getConfigurationPath();
  var tempPath = WA_getConfigurationPath("/Shared/PayPal/ContextTemp.htm");
  var browsePath = tempPath;  
  var helpPath = WA_getConfigurationPath("/Shared/PayPal/ExtensionHelp.htm");

  if (!DWfile.exists(tempPath))
  {
    tempPath = dw.getTempFolderPath(); // force to use local user paths
    var folderPath = tempPath.substring(0, tempPath.lastIndexOf("/"));
    tempPath =  tempPath.substring(0, tempPath.lastIndexOf("/")) + "/Shared/PayPal/ContextTemp.htm";
    browsePath = tempPath;  
    DWfile.createFolder(folderPath+"/Shared");
    DWfile.createFolder(folderPath+"/Shared/PayPal");
  }
    scriptTag  = "<sc" + "ript language='JavaScript'>\n\r";
    var cfgPath  = dw.getConfigurationPath();
    var fPath = dw.getTempFolderPath();
    fPath     = fPath.substring(0, fPath.lastIndexOf("/"));
    if (DWfile.exists(fPath + "/Shared/PayPal/ExtensionHelp.htm"))     {
      cfgPath = fPath;
    }

    if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     {
      cfgPath = cfgPath.replace(/file\:\/\/\//i, "");
  	  browsePath = browsePath.replace(/file\:\/\/\//i, "");  
	    if (cfgPath.substring(0, cfgPath.indexOf("/")) == browsePath.substring(0, browsePath.indexOf("/")))
	    {
	      cfgPath = "file://localhost" + cfgPath.substring(cfgPath.indexOf("/"));
  	  }
	    else
	    {
	      cfgPath = "file://localhost/Volumes/" + cfgPath;	  
  	  }
      scriptTag += "var helpLoc = \"" + escape(cfgPath) + "/Shared/PayPal/ExtensionHelp.htm\";\n\r"; 
	    browsePath = "file://localhost" + browsePath.substring(browsePath.indexOf("/")); 
    }
    else     {
      scriptTag += "var helpLoc = \"" + cfgPath + "/Shared/PayPal/ExtensionHelp.htm\";\n\r";
    }
    if (inputAnchor && inputAnchor != "")    {
      scriptTag += "helpLoc += \"#" + inputAnchor + "\";\n\r";
    
    }
    scriptTag += "document.location.href = helpLoc;\n\r";
    scriptTag += "</sc" + "ript>";
    helpPath = tempPath;
    DWfile.write(tempPath, scriptTag);

  dreamweaver.browseDocument(browsePath);  
}

function openPPBrowser()     {
  dreamweaver.browseDocument("http://www.paypal.com");
}

function openWABrowser()     {
  dreamweaver.browseDocument("http://www.webassist.com");
}

function openURLInBrowser(myURL)     {
  dreamweaver.browseDocument(myURL);
}

function getCurrencyType(inCurrencyCode)
{
  var retCurrencyType = "";
  switch (inCurrencyCode)
  {
    case "USD":
      retCurrencyType = MSG_USDollar;
      break;

    case "GBP":
      retCurrencyType = MSG_Pounds;
      break;

    case "EUR":
      retCurrencyType = MSG_Euro;
      break;

    case "CAD":
      retCurrencyType = MSG_CanadianDollar;
      break;                  

    case "AUD":
      retCurrencyType = MSG_AusDollar;
      break;                  
      
    case "JPY":
      retCurrencyType = MSG_Yen;
      break;
  }
  return retCurrencyType;              
}

function getVCUID(theTag)     {
  var idTag = "business=";
  var idEndTag = "'";
  var ppID = "";
  var newTag = theTag.toLowerCase();
  var ppIDStart = newTag.indexOf(idTag);
  if (ppIDStart >= 0)
  {
    ppIDStart += idTag.length; // take out the 'business=' part out of the UserID string
    var ppIDLength = newTag.substring(ppIDStart).indexOf(idEndTag);  // go to the first single quote
    var ppIDEnd = ppIDStart + ppIDLength;
    ppID = theTag.substring(ppIDStart, ppIDEnd);  
  }
  return ppID;
}

function getUID(theTag)     {
  var idTag = "business=";
  var idEndTag = "&";
  var ppID = "";
  var newTag = theTag.toLowerCase();
  var ppIDStart = newTag.indexOf(idTag);
  if (ppIDStart >= 0)
  {
    ppIDStart += idTag.length; // take out the 'business=' part out of the UserID string
    var ppIDLength = newTag.substring(ppIDStart).indexOf(idEndTag);  // go to the first single quote
    var ppIDEnd = ppIDStart + ppIDLength;
    ppID = theTag.substring(ppIDStart, ppIDEnd);  
  }

  return ppID;
}

function getTranslatedImageURL(theTag)      {
  var theURL = "";
  var imageTag = 'img src="';
  var imageEndTag = '"';
  var theImage = "";
  var newTag = theTag.toLowerCase();
  var imageStart = newTag.indexOf(imageTag);
  imageStart += imageTag.length; // take out the 'business=' part out of the UserID string
  var imageLength = newTag.substring(imageStart).indexOf(imageEndTag);  // go to the first single quote
  var imageEnd = imageStart + imageLength;
  theURL = theTag.substring(imageStart, imageEnd);   
  theURL = WAURLDecode(theURL);
  return theURL;
}

function getTranslatedImage(theTag, theCustomImage)      {
  var theURL = getTranslatedImageURL(theTag).toLowerCase();
  var theImage = "";
  
  if (theURL.indexOf("http://images.paypal.com") >= 0)
  {
    // stock PayPal image
    theImage = theURL.substring(theURL.lastIndexOf("/")+1);
  }
  else  //custom image
  {
    theImage = theCustomImage;
  }
  return theImage;
}

function getTranslatedACImage(theURL)      {
  var theImage = "";
  var newURL = theURL.toLowerCase();  
  if (newURL.indexOf("://images.paypal.com") >= 0 || newURL.indexOf("://www.paypal.com/") >= 0)
  {
    // stock PayPal image
    theImage = theURL.substring(theURL.lastIndexOf("/")+1);
  }
  else  //custom image
  {
    theImage = CUSTOM_AC_IMG;
  }
  return theImage;
}

function getTranslatedVCImage(theURL)      {
  var theImage = "";
  var newURL = theURL.toLowerCase();  
  if (newURL.indexOf("http://images.paypal.com") >= 0 || newURL.indexOf("https://images.paypal.com") >= 0)
  {
    // stock PayPal image
    theImage = theURL.substring(theURL.lastIndexOf("/")+1);
  }
  else  //custom image
  {
    theImage = CUSTOM_VC_IMG;
  }
  return theImage;
}

function getTranslatedSIPImage(theURL)      {
  var theImage = "";
  var newURL = theURL.toLowerCase();  
  if (newURL.indexOf("http://images.paypal.com") >= 0 || newURL.indexOf("https://images.paypal.com") >= 0)
  {
    // stock PayPal image
    theImage = theURL.substring(theURL.lastIndexOf("/")+1);
  }
  else  //custom image
  {
    theImage = CUSTOM_SIP_IMG;
  }
  return theImage;
}

function getTranslatedSUBImage(theURL)      {
  var theImage = "";
  var newURL = theURL.toLowerCase();  
  if (newURL.indexOf("http://images.paypal.com") >= 0 || newURL.indexOf("https://images.paypal.com") >= 0)
  {
    // stock PayPal image
    theImage = theURL.substring(theURL.lastIndexOf("/")+1);
  }
  else  //custom image
  {
    theImage = CUSTOM_SUB_IMG;
  }
  return theImage;
}

function getLogoURL(theTag)     {
  var theStartTag = "image_url=";
  var theEndTag = "&";
  var theURL = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    theURL = theTag.substring(theStartIndex, theEndIndex); 
  }
  return theURL;
}

function getSuccessURL(theTag)     {
  var theStartTag = "return=";
  var theEndTag = "&";
  var theURL = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    theURL = theTag.substring(theStartIndex, theEndIndex); 
  }
  return theURL;
}

function getCancelURL(theTag)     {
  var theStartTag = "cancel_return=";
  var theEndTag = "&";
  var theURL = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    theURL = theTag.substring(theStartIndex, theEndIndex); 
  }
  return theURL;
}

function getItemName(theTag)     {
  var theStartTag = "item_name=";
  var theEndTag = "&";
  var retVal = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    retVal = theTag.substring(theStartIndex, theEndIndex); 
  }
  return retVal;
}

function getItemNumber(theTag)     {
  var theStartTag = "item_number=";
  var theEndTag = "&";
  var retVal = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    retVal = theTag.substring(theStartIndex, theEndIndex); 
  }
  return retVal;
}

function getAmount(theTag)     {
  var theStartTag = "amount=";
  var theEndTag = "&";
  var retVal = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    retVal = theTag.substring(theStartIndex, theEndIndex); 
  }
  return retVal;
}

function getQuantityYes(theTag)     {
  var theStartTag = "undefined_quantity=";
  var theEndTag = "&";
  var retVal = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    retVal = theTag.substring(theStartIndex, theEndIndex); 
  }
  return retVal;
}

function getShippingNo(theTag)     {
  var theStartTag = "no_shipping=";
  var theEndTag = "&";
  var retVal = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    retVal = theTag.substring(theStartIndex, theEndIndex); 
  }
  return retVal;
}

function getIncludeNote(theTag)     {
  var theStartTag = "no_note=";
  var theEndTag = "'";    // last tag so no more ampersands
  var retVal = "";
  var newTag = theTag.toLowerCase();
  var theStartIndex = newTag.indexOf(theStartTag);
  if (theStartIndex >= 0)
  {
    theStartIndex += theStartTag.length; // take out the 'image_url=' part out of the UserID string
    var theLength = newTag.substring(theStartIndex).indexOf(theEndTag);  // go to the first ampersand
    var theEndIndex = theStartIndex + theLength;
    retVal = theTag.substring(theStartIndex, theEndIndex); 
  }
  return retVal;
}

function enableDisableOtherImage(objType)     {
  var imgObj = "acimage";
  var otherImgObj = "otheracimage";
  
  if (objType == "VC")
  {
    imgObj = "vcimage";
    otherImgObj = "othervcimage";
  }
  else if (objType == "SIP")
  {
    imgObj = "sipimage";
    otherImgObj = "othersipimage";  
  }

  var myImage = findObject(imgObj);
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        findObject(otherImgObj).disabled = "enabled";
      }
      else
      {
        findObject(otherImgObj).disabled = "disabled";
      }
    }
  }
}

/****************************** import from existing CSV ************************/
function localURLToFullFileURL(filePath)      {
  var docURL   = dw.getDocumentDOM().URL;
  
  if (filePath.toLowerCase().indexOf("file://") != 0)    {
    docURL = docURL.substring(0, docURL.lastIndexOf("/"));
    while (filePath.indexOf("../") == 0)     {
      filePath = filePath.substring(3);
      docURL   = docURL.substring(0, docURL.lastIndexOf("/"));
    }
    docURL = docURL + "/" + filePath;
  }
  else     {
    docURL = filePath;
  }
  
  return docURL;
}

function generateVCTag()     {
  MM.setBusyCursor();
  var theDOM     = dw.getDocumentDOM();
  var vcTag      = "";
  var myID       = document.UserIDWP.document.ppUID.value;
  var myImage    = document.ButtonImageWP.document.vcimage;
  var myImageURL = "";
  var inForm = false;
  var theParent = theDOM.getSelectedNode();
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
      if (confirm(MSG_AlreadyInForm))  {
        theDOM.setSelection(theDOM.nodeToOffsets(theParent)[1]-1,theDOM.nodeToOffsets(theParent)[1]-1);
      }
      else  {
        closeWindow();
        return "";
      }    
	    break;
	  }
  }
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        myImageURL = document.ButtonImageWP.document.othervcimage.value;
      }
      else
      {
        myImageURL = "http://images.paypal.com/images/" + myImage[n].value;      
      }
    }
  }  

  //vcTag += "<a href=\"#\" onclick=\"window.open('https://www.paypal.com/cart/display=1&business=" + escape(myID) + "','cartwin','width=600,height=400,scrollbars,location,resizable,status');\"><img src=\"" + escape(myImageURL) + "\" border=\"0\"></a>";  
  //vcTag += "<a href=\"#\" onclick=\"window.open('https://www.paypal.com/cart/display=1&bn=" + BNVersion + "&business=" + WAURLEncode(myID) + "','cartwin','width=600,height=400,scrollbars,location,resizable,status');\"><img src=\"" + WAURLEncode(myImageURL) + "\" border=\"0\"></a>";  
  
  if (!inForm)  {
    vcTag += "<form target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n\r";
  }
  vcTag += "  <input type=\"hidden\" name=\"cmd\" value=\"_cart\">\n\r";
  vcTag += "  <input type=\"hidden\" name=\"display\" value=\"1\">\n\r";
  vcTag += "  <input type=\"hidden\" name=\"bn\" value=\"" + BNVersion + "\">\n\r";
  vcTag += "  <input type=\"hidden\" name=\"business\" value=\"" + myID + "\">\n\r";
  if (myID)  {
    vcTag += "  <input type=\"hidden\" name=\"receiver_email\" value=\"" + myID + "\">\n\r";
    vcTag += "  <input type=\"hidden\" name=\"mrb\" value=\"" + MRB + "\">\n\r";
    vcTag += "  <input type=\"hidden\" name=\"pal\" value=\"" + PAL + "\">\n\r";    
  }  
  
  vcTag += "  <input type=\"image\" name=\"submit\" src=\"" + myImageURL + "\" border=\"0\" alt=\"Make payments with PayPal - it's fast, free and secure!\">\n\r";
   
  if (!inForm)
    vcTag += "</form>\n\r";
  
  theDOM.synchronizeDocument();
  theDOM.insertHTML(vcTag, false);
  
  setLastUID();
  theParent = theDOM.getSelectedNode();
  while (theParent.parentNode && inForm)  {
    if (theParent.tagName=="FORM")  {
	    if (theParent.getAttribute("action")!="https://www.paypal.com/cgi-bin/webscr")
	      theParent.setAttribute("action","https://www.paypal.com/cgi-bin/webscr");
	    if (theParent.getAttribute("target")!="paypal")
	      theParent.setAttribute("target","paypal");  
		if (theParent.getAttribute("method")!="post")
	      theParent.setAttribute("method","post");    
	    break;
	  }  
    theParent = theParent.parentNode;
  }
  MM.clearBusyCursor();
  
  closeWindow();
  return "";
}

function generateSIPTag()     {
  MM.setBusyCursor();
  var theDOM     = dw.getDocumentDOM();
  var sipTag     = "";
  var myID       = document.UserIDWP.document.ppUID.value;
  var myImage    = document.ButtonImageWP.document.sipimage;
  var myImageURL = "";
  var myItemName = document.itemWP.document.itemName.value;
  var myItemID = document.itemWP.document.itemID.value;
  var myAmount = document.itemWP.document.itemPrice.value;
  var myCurrency = document.itemWP.document.itemCurrency.options[document.itemWP.document.itemCurrency.selectedIndex].value;
  var myQuantityYes = document.itemWP.document.quantityYes.checked;
  var myShipping = document.shippingWP.document.itemShipping.value;
  var myShipping2 = document.shippingWP.document.itemShipping2.value;
  var myHandling = document.shippingWP.document.itemHandling.value;  
  var myShippingNo = document.shippingWP.document.shippingNo[1].checked;
  var myIncludeNote = true;
  if (document.shippingWP.document.includeNote[1].checked)
  {
    myIncludeNote = false;
  }
  var myLogoURL = document.logoWP.document.ppLogo.value;
  var mySuccessURL = document.customWP.document.successURL.value;
  var myCancelURL = document.customWP.document.cancelURL.value;
  var inForm = false;
  var theParent = theDOM.getSelectedNode();
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
      if (confirm(MSG_AlreadyInForm))  {
        theDOM.setSelection(theDOM.nodeToOffsets(theParent)[1]-1,theDOM.nodeToOffsets(theParent)[1]-1);
	    }
      else  {
        closeWindow();
        return "";
      }
	    break;
	  }
  }
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        myImageURL = document.ButtonImageWP.document.othersipimage.value;      
      }
      else
      {
        myImageURL = "http://images.paypal.com/images/" + myImage[n].value;      
      }
    }
  }  
  if (!inForm)  {
    sipTag += "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n\r";
  }
  sipTag += "  <input type=\"hidden\" name=\"cmd\" value=\"_xclick\">\n\r";
  sipTag += "  <input type=\"hidden\" name=\"bn\" value=\"" + BNVersion + "\">\n\r";
  sipTag += "  <input type=\"hidden\" name=\"business\" value=\"" + myID + "\">\n\r";
     
  if (myItemName != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"item_name\" value=\"" + myItemName + "\">\n\r";  
  }
  
  if (myItemID != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"item_number\" value=\"" + myItemID + "\">\n\r";  
  }
  
  if (myAmount != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"amount\" value=\"" + myAmount + "\">\n\r";  
  }

  sipTag += "  <input type=\"hidden\" name=\"currency_code\" value=\"" + myCurrency + "\">\n\r";  

  if (myShipping != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"shipping\" value=\"" + myShipping + "\">\n\r";  
  }

  if (myShipping2 != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"shipping2\" value=\"" + myShipping2 + "\">\n\r";  
  }
  
  if (myHandling != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"handling\" value=\"" + myHandling + "\">\n\r";  
  }
 
  if (isValidURL(myLogoURL))
  {
    sipTag += "  <input type=\"hidden\" name=\"image_url\" value=\"" + myLogoURL + "\">\n\r";  
  }
  
  if (isValidURL(mySuccessURL))
  {
    sipTag += "  <input type=\"hidden\" name=\"return\" value=\"" + mySuccessURL + "\">\n\r";  
  }
              
  if (isValidURL(myCancelURL))
  {
    sipTag += "  <input type=\"hidden\" name=\"cancel_return\" value=\"" + myCancelURL + "\">\n\r";  
  }
    
  sipTag += "  <input type=\"hidden\" name=\"undefined_quantity\" value=\"";
  if (myQuantityYes)
    sipTag += "1";
  else
    sipTag += "0";  
  sipTag +="\">\n\r";      

  if (myID)  {
    sipTag += "  <input type=\"hidden\" name=\"receiver_email\" value=\"" + myID + "\">\n\r";
    sipTag += "  <input type=\"hidden\" name=\"mrb\" value=\"" + MRB + "\">\n\r";
    sipTag += "  <input type=\"hidden\" name=\"pal\" value=\"" + PAL + "\">\n\r";        
  }
    
  // Always insert these three parameters as the last params
  sipTag += "  <input type=\"hidden\" name=\"no_shipping\" value=\"";
  if (myShippingNo)
    sipTag += "1";
  else
    sipTag += "0";
  sipTag +="\">\n\r";
         
  sipTag += "  <input type=\"hidden\" name=\"no_note\" value=\"";
  if (!myIncludeNote)
    sipTag += "1";
  else
    sipTag += "0";  
  sipTag +="\">\n\r";
   
  sipTag += "  <input type=\"image\" name=\"submit\" src=\"" + myImageURL + "\" border=\"0\" alt=\"Make payments with PayPal, it's fast, free, and secure!\">\n\r";  
  
  if (!inForm)
    sipTag += "</form>\n\r";
  
  theDOM.synchronizeDocument();
  theDOM.insertHTML(sipTag, false);
  
  setLastUID();
  theParent = theDOM.getSelectedNode();
  while (theParent.parentNode && inForm)  {
    if (theParent.tagName=="FORM")  {
	    if (theParent.getAttribute("action")!="https://www.paypal.com/cgi-bin/webscr")
	      theParent.setAttribute("action","https://www.paypal.com/cgi-bin/webscr");
	    if (theParent.getAttribute("target")!="paypal")
	      theParent.setAttribute("target","paypal");  
		if (theParent.getAttribute("method")!="post")
	      theParent.setAttribute("method","post");
	    break;
	  }  
    theParent = theParent.parentNode;
  }
  MM.clearBusyCursor();
  
  closeWindow();
  return "";
}

function generateACTag()     {
  MM.setBusyCursor();
  var theDOM     = dw.getDocumentDOM();
  var acTag      = "";
  var myID       = document.UserIDWP.document.ppUID.value;
  var myImage    = document.ButtonImageWP.document.acimage;
  var myImageURL = "";
  var myItemName = document.itemWP.document.itemName.value;
  var myItemID = document.itemWP.document.itemID.value;
  var myAmount = document.itemWP.document.itemPrice.value;
  var myCurrency = document.itemWP.document.itemCurrency.options[document.itemWP.document.itemCurrency.selectedIndex].value;  
  var myShippingNo = document.itemWP.document.shippingNo[1].checked;
  var myIncludeNote = true;
  var inForm = false;
  var theParent = theDOM.getSelectedNode();
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
      if (confirm(MSG_AlreadyInForm))  {
        theDOM.setSelection(theDOM.nodeToOffsets(theParent)[1]-1,theDOM.nodeToOffsets(theParent)[1]-1);
	    }
      else  {
        closeWindow();
        return "";
      }    
	    break;
	  }
  }
  if (document.itemWP.document.includeNote[1].checked)
  {
    myIncludeNote = false;
  }
  var myLogoURL = document.logoWP.document.ppLogo.value;
  var mySuccessURL = document.customWP.document.successURL.value;
  var myCancelURL = document.customWP.document.cancelURL.value;
  
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        myImageURL = document.ButtonImageWP.document.otheracimage.value;      
      }
      else
      {
        myImageURL = "http://images.paypal.com/images/" + myImage[n].value;      
      }
    }
  }  
  if (!inForm)  {
    acTag += "<form target=\"paypal\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n\r";
  }
  acTag += "  <input type=\"hidden\" name=\"cmd\" value=\"_cart\">\n\r";
  acTag += "  <input type=\"hidden\" name=\"add\" value=\"1\">\n\r";
  acTag += "  <input type=\"hidden\" name=\"bn\" value=\"" + BNVersion + "\">\n\r";
  acTag += "  <input type=\"hidden\" name=\"business\" value=\"" + myID + "\">\n\r";
  
//  acTag += "<a href=\"#\" onclick=\"window.open('https://www.paypal.com/cart/add=1&bn=" + BNVersion + "&business=" + WAURLEncode(myID);
  
  if (myItemName != "")
  {
    acTag += "  <input type=\"hidden\" name=\"item_name\" value=\"" + myItemName + "\">\n\r";
  }
  
  if (myItemID != "")
  {
    acTag += "  <input type=\"hidden\" name=\"item_number\" value=\"" + myItemID + "\">\n\r";
  }
  
  if (myAmount != "")
  {
    acTag += "  <input type=\"hidden\" name=\"amount\" value=\"" + myAmount + "\">\n\r";
  }

  acTag += "  <input type=\"hidden\" name=\"currency_code\" value=\"" + myCurrency + "\">\n\r";
      
  if (isValidURL(myLogoURL))
  {
    acTag += "  <input type=\"hidden\" name=\"image_url\" value=\"" + myLogoURL + "\">\n\r";
  }
  
  if (isValidURL(mySuccessURL))
  {
    acTag += "  <input type=\"hidden\" name=\"return\" value=\"" + mySuccessURL + "\">\n\r";
  }    
  
  if (isValidURL(myCancelURL))
  {
    acTag += "  <input type=\"hidden\" name=\"cancel_return\" value=\"" + myCancelURL + "\">\n\r";
  }
       
  if (myID)  {
    acTag += "  <input type=\"hidden\" name=\"receiver_email\" value=\"" + myID + "\">\n\r";
    acTag += "  <input type=\"hidden\" name=\"mrb\" value=\"" + MRB + "\">\n\r";
    acTag += "  <input type=\"hidden\" name=\"pal\" value=\"" + PAL + "\">\n\r";        
  }
         
  acTag += "  <input type=\"hidden\" name=\"no_shipping\" value=\"";
  if (myShippingNo)
    acTag += "1";
  else
    acTag += "0";
  acTag += "\">\n\r";
         
  acTag += "  <input type=\"hidden\" name=\"no_note\" value=\"";
  if (!myIncludeNote)
    acTag += "1";
  else
    acTag += "0";  
  acTag += "\">\n\r";
   
  acTag += "  <input type=\"image\" name=\"submit\" src=\"" + myImageURL + "\" border=\"0\" alt=\"Make payments with PayPal - it's fast, free and secure!\">\n\r";
  
  if (!inForm)
    acTag += "</form>\n\r";
    
  theDOM.synchronizeDocument();
  theDOM.insertHTML(acTag, false);
  
  setLastUID();
  theParent = theDOM.getSelectedNode();
  while (theParent.parentNode && inForm)  {
    if (theParent.tagName=="FORM")  {
	    if (theParent.getAttribute("action")!="https://www.paypal.com/cgi-bin/webscr")
	      theParent.setAttribute("action","https://www.paypal.com/cgi-bin/webscr");
	    if (theParent.getAttribute("target")!="paypal")
	      theParent.setAttribute("target","paypal");  
		if (theParent.getAttribute("method")!="post")
	      theParent.setAttribute("method","post");      
	    break;
	  }  
    theParent = theParent.parentNode;
  }
  MM.clearBusyCursor();
  closeWindow();
  return "";
}

function generateSUBTag()     {
  MM.setBusyCursor();
  var theDOM     = dw.getDocumentDOM();
  var subTag     = "";
  var myID       = document.UserIDWP.document.ppUID.value;
  var myImage    = document.ButtonImageWP.document.subimage;
  var myImageURL = "";
  var myItemName = document.itemWP.document.subscriptionName.value;
  var myItemID = document.itemWP.document.subscriptionID.value;
  var myAmount = document.itemWP.document.subscriptionPrice.value;
  var myCurrency = document.itemWP.document.itemCurrency.options[document.itemWP.document.itemCurrency.selectedIndex].value;    
  var myTrial1 = document.itemWP.document.trial1Amount.value;
  var myLogoURL = document.logoWP.document.ppLogo.value;
  var mySuccessURL = document.customWP.document.successURL.value;
  var myCancelURL = document.customWP.document.cancelURL.value;
  var inForm = false;
  var theParent = theDOM.getSelectedNode();
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
      if (confirm(MSG_AlreadyInForm))  {
        theDOM.setSelection(theDOM.nodeToOffsets(theParent)[1]-1,theDOM.nodeToOffsets(theParent)[1]-1);
	    }
      else  {
        closeWindow();
        return "";
      }    
	    break;
	  }
  } 
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        myImageURL = document.ButtonImageWP.document.othersubimage.value;      
      }
      else
      {
        myImageURL = "http://images.paypal.com/images/" + myImage[n].value;      
      }
    }
  }  
  
  subTag += "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n\r";
  subTag += "  <input type=\"hidden\" name=\"cmd\" value=\"_xclick-subscriptions\">\n\r";
  subTag += "  <input type=\"hidden\" name=\"bn\" value=\"" + BNVersion + "\">\n\r";
  subTag += "  <input type=\"hidden\" name=\"business\" value=\"" + myID + "\">\n\r";
     
  if (myItemName != "")
  {
    subTag += "  <input type=\"hidden\" name=\"item_name\" value=\"" + myItemName + "\">\n\r";  
  }
  
  if (myItemID != "")
  {
    subTag += "  <input type=\"hidden\" name=\"item_number\" value=\"" + myItemID + "\">\n\r";  
  }

  subTag += "  <input type=\"hidden\" name=\"currency_code\" value=\"" + myCurrency + "\">\n\r";  
  
  if (isValidAmount(myTrial1))
  {
    subTag += "  <input type=\"hidden\" name=\"a1\" value=\"" + myTrial1 + "\">\n\r";
    subTag += "  <input type=\"hidden\" name=\"p1\" value=\"" + document.itemWP.document.trial1Value.options[document.itemWP.document.trial1Value.selectedIndex].value + "\">\n\r";
    subTag += "  <input type=\"hidden\" name=\"t1\" value=\"" + document.itemWP.document.trial1Period.options[document.itemWP.document.trial1Period.selectedIndex].value + "\">\n\r";
  }

  subTag += "  <input type=\"hidden\" name=\"a3\" value=\"" + myAmount + "\">\n\r";  
  subTag += "  <input type=\"hidden\" name=\"p3\" value=\"" + document.itemWP.document.billingValue.options[document.itemWP.document.billingValue.selectedIndex].value + "\">\n\r";
  subTag += "  <input type=\"hidden\" name=\"t3\" value=\"" + document.itemWP.document.billingPeriod.options[document.itemWP.document.billingPeriod.selectedIndex].value + "\">\n\r";  
    
  if (isValidURL(myLogoURL))
  {
    subTag += "  <input type=\"hidden\" name=\"image_url\" value=\"" + myLogoURL + "\">\n\r";  
  }
  
  if (isValidURL(mySuccessURL))
  {
    subTag += "  <input type=\"hidden\" name=\"return\" value=\"" + mySuccessURL + "\">\n\r";  
  }
              
  if (isValidURL(myCancelURL))
  {
    subTag += "  <input type=\"hidden\" name=\"cancel_return\" value=\"" + myCancelURL + "\">\n\r";  
  }
                
  subTag += "  <input type=\"hidden\" name=\"src\" value=\"";
  if (document.itemWP.document.recurringBilling[0].checked)
    subTag += "1";
  else
    subTag += "0";
  subTag +="\">\n\r";
                        
  if (document.itemWP.document.recurringBilling[1].checked)
  {
    subTag += "  <input type=\"hidden\" name=\"srt\" value=\"" + document.itemWP.document.stopRecurringValue.options[document.itemWP.document.stopRecurringValue.selectedIndex].value + "\">\n\r";
  }             

  subTag += "  <input type=\"hidden\" name=\"sra\" value=\"";  
  if (document.shippingWP.document.reattemptBilling[0].checked)
    subTag += "1";
  else
    subTag += "0";
  subTag +="\">\n\r";
  
  if (myID)  {
    subTag += "  <input type=\"hidden\" name=\"receiver_email\" value=\"" + myID + "\">\n\r";
    subTag += "  <input type=\"hidden\" name=\"mrb\" value=\"" + MRB + "\">\n\r";
    subTag += "  <input type=\"hidden\" name=\"pal\" value=\"" + PAL + "\">\n\r";        
  }  
     
  subTag += "  <input type=\"hidden\" name=\"no_shipping\" value=\"";
  if (document.shippingWP.document.includeShipping[1].checked)
    subTag += "1";
  else
    subTag += "0";
  subTag +="\">\n\r";
                 
  // no_note is required by the SUB API
  subTag += "  <input type=\"hidden\" name=\"no_note\" value=\"1\">\n\r";
  
  subTag += "  <input type=\"image\" name=\"submit\" src=\"" + myImageURL + "\" border=\"0\" alt=\"Make payments with PayPal, it's fast, free, and secure!\">\n\r";  
  
  if (!inForm)
    subTag += "</form>\n\r";
    
  theDOM.synchronizeDocument();
  theDOM.insertHTML(subTag, false);
  
  setLastUID();
  theParent = theDOM.getSelectedNode();
  while (theParent.parentNode && inForm)  {
    if (theParent.tagName=="FORM")  {
	    if (theParent.getAttribute("action")!="https://www.paypal.com/cgi-bin/webscr")
	      theParent.setAttribute("action","https://www.paypal.com/cgi-bin/webscr");
	    if (theParent.getAttribute("target")!="paypal")
	      theParent.setAttribute("target","paypal");  
		if (theParent.getAttribute("method")!="post")
	      theParent.setAttribute("method","post");    
	    break;
	  }  
    theParent = theParent.parentNode;
  }
  MM.clearBusyCursor();
  
  closeWindow();
  return "";
}

function generateDTag()     {
  MM.setBusyCursor();
  var theDOM     = dw.getDocumentDOM();
  var sipTag     = "";
  var myID       = document.UserIDWP.document.ppUID.value;
  var myImage    = document.ButtonImageWP.document.sipimage;
  var myImageURL = "";
  var myItemName = document.itemWP.document.itemName.value;
  var myItemID = document.itemWP.document.itemID.value;
  var myCurrency = document.itemWP.document.itemCurrency.options[document.itemWP.document.itemCurrency.selectedIndex].value;
  var myShippingNo = document.itemWP.document.shippingNo[1].checked;
  var myIncludeNote = true;
  if (document.itemWP.document.includeNote[1].checked)
  {
    myIncludeNote = false;
  }
  var myLogoURL = document.logoWP.document.ppLogo.value;
  var mySuccessURL = document.customWP.document.successURL.value;
  var myCancelURL = document.customWP.document.cancelURL.value;
  var inForm = false;
  var theParent = theDOM.getSelectedNode();
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
	    inForm = true;
	    break;
	  }
  }
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        myImageURL = document.ButtonImageWP.document.othersipimage.value;      
      }
      else
      {
        myImageURL = "http://images.paypal.com/images/" + myImage[n].value;      
      }
    }
  }  
  if (!inForm)  {
    sipTag += "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">\n\r";
  }
  sipTag += "  <input type=\"hidden\" name=\"cmd\" value=\"_xclick\">\n\r";
  sipTag += "  <input type=\"hidden\" name=\"bn\" value=\"" + BNVersion + "\">\n\r";
  sipTag += "  <input type=\"hidden\" name=\"business\" value=\"" + myID + "\">\n\r";
     
  if (myItemName != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"item_name\" value=\"" + myItemName + "\">\n\r";  
  }
  
  if (myItemID != "")
  {
    sipTag += "  <input type=\"hidden\" name=\"item_number\" value=\"" + myItemID + "\">\n\r";  
  }
  
  sipTag += "  <input type=\"hidden\" name=\"currency_code\" value=\"" + myCurrency + "\">\n\r";  

  if (isValidURL(myLogoURL))
  {
    sipTag += "  <input type=\"hidden\" name=\"image_url\" value=\"" + myLogoURL + "\">\n\r";  
  }
  
  if (isValidURL(mySuccessURL))
  {
    sipTag += "  <input type=\"hidden\" name=\"return\" value=\"" + mySuccessURL + "\">\n\r";  
  }
              
  if (isValidURL(myCancelURL))
  {
    sipTag += "  <input type=\"hidden\" name=\"cancel_return\" value=\"" + myCancelURL + "\">\n\r";  
  }
  
   
  if (myID)  {
    sipTag += "  <input type=\"hidden\" name=\"receiver_email\" value=\"" + myID + "\">\n\r";
    sipTag += "  <input type=\"hidden\" name=\"mrb\" value=\"" + MRB + "\">\n\r";
    sipTag += "  <input type=\"hidden\" name=\"pal\" value=\"" + PAL + "\">\n\r";        
  }
   
  // Always insert these three parameters as the last params
  sipTag += "  <input type=\"hidden\" name=\"no_shipping\" value=\"";
  if (myShippingNo)
    sipTag += "1";
  else
    sipTag += "0";
  sipTag +="\">\n\r";
         
  sipTag += "  <input type=\"hidden\" name=\"no_note\" value=\"";
  if (!myIncludeNote)
    sipTag += "1";
  else
    sipTag += "0";  
  sipTag +="\">\n\r";
   
  sipTag += "  <input type=\"image\" name=\"submit\" src=\"" + myImageURL + "\" border=\"0\" alt=\"Make payments with PayPal, it's fast, free, and secure!\">\n\r";  
  
  if (!inForm)
    sipTag += "</form>\n\r";
  
  theDOM.synchronizeDocument();
  theDOM.insertHTML(sipTag);
  
  setLastUID();
  theParent = theDOM.getSelectedNode();
  while (theParent.parentNode && inForm)  {
    if (theParent.tagName=="FORM")  {
	    if (theParent.getAttribute("action")!="https://www.paypal.com/cgi-bin/webscr")
	      theParent.setAttribute("action","https://www.paypal.com/cgi-bin/webscr");
	    if (theParent.getAttribute("target")!="paypal")
	      theParent.setAttribute("target","paypal");  
		if (theParent.getAttribute("method")!="post")
	      theParent.setAttribute("method","post");
	    break;
	  }  
    theParent = theParent.parentNode;
  }
  MM.clearBusyCursor();
  
  closeWindow();
  return "";
}

function validatePPUID()     {
  var errMsg = "";
  var ppID = findObject("ppUID").value;
  if ((ppID == "") || (ppID.indexOf("@") < 0))
  {
    errMsg += "- "+ MSG_NoUID + "\n";
  }

  return errMsg;
}

function validateSubsDetails()    {
  var retVal = "";
  var myPrice = document.itemWP.document.subscriptionPrice.value;
  var myTrial1 = document.itemWP.document.trial1Amount.value;
  
  if (myPrice == "")
  {
    retVal += MSG_SubscriptionPrice + MSG_NoSubPrice + "\n";
  }
  else if (!isValidAmount(myPrice) || parseInt(myPrice) <= 0)
  {
    retVal += MSG_SubscriptionPrice + MSG_NoSubPriceValidAmount + "\n";  
  } 

  if (myTrial1 != "")
  {
    if (!isValidAmount(myTrial1))
    {
        retVal += MSG_TrialPrice+ MSG_NoSubTrial1ValidAmount + "\n";    
    }   
  }
  
  return retVal;
}

function validateItemName()     {
  var errMsg = "";
  var myItemName = document.itemWP.document.itemName.value;
  if (myItemName == "")
  {
    errMsg += "- "+ MSG_NoItemName + "\n";
  }

  return errMsg;
}

function validateItemPrice()     {
  var errMsg = "";
  var myItemPrice = document.itemWP.document.itemPrice.value;
  if ((myItemPrice == "") || !isValidAmount(myItemPrice))
  {
    errMsg += "- " + MSG_InvalidAmount + "\n";
  }

  return errMsg;
}

function validateAmount(theAmount)     {
  var errMsg = "";
  if (!isValidAmount(theAmount))
  {
    errMsg = "- " + MSG_InvalidAmount + "\n";
  }

  return errMsg;
}

function isValidEmail(theEmail)     {
  var retVal = true;

  if ((theEmail == "") || (theEmail.indexOf("@") < 0))
  {
    retVal = false;
  }

  return retVal;
}

function isValidAmount(thePrice)     {
  var retVal = true;
  if ( (thePrice == "") || isNaN(thePrice) || (parseInt(thePrice) < 0) )
  {
    retVal = false;
  }
  var decimalIndex = thePrice.indexOf(".");
  if (decimalIndex >= 0)
  {
    thePrice = thePrice.substring(decimalIndex+1);
    if (thePrice.length != 2)
      retVal = false;
  }
  return retVal;
}

function isValidURL(theURL)     {
  theURL = theURL.toLowerCase();
  var retVal = true;

  if ( (theURL == "") || (theURL == "http://") || (theURL == "https://") )
    retVal = false;
  else if ( (theURL.indexOf("http://") < 0) && (theURL.indexOf("https://") < 0) )
    retVal = false;

  return retVal;
}

function setLastUID()     {
  var myID = document.UserIDWP.document.ppUID.value; 
  var newTag = '<paypal uid="' + WAURLEncode(myID) + '">\n';

  DWfile.write(PayPalConfigFilePath, newTag);
}

function getLastUID()     {
  var xmlDOM     = dw.getDocumentDOM(PayPalConfigFilePath);
  var thePayPal  = xmlDOM.getElementsByTagName("paypal");
  
  var theUID = thePayPal[0].getAttribute("uid");

  document.UserIDWP.document.ppUID.value = WAURLDecode(theUID);
}

/*
** Full URL Encoding of non-alphanumeric characters (escape() only performs a subset of
** these characters).  Just comment out any replace lines for characters you do not want
** to encode for this particular implementation.
*/
function WAURLEncode(theString)     {
	var re0 = /!/g;
  var re1 = /"/g;
	var re2 = /#/g;
	var re3	= /\$/g;
	var re4 = /%/g;
	var re5 = / /g; 
  var re6 = /&/g; 
  var re7 = /'/g;
  var re8 = /\(/g;
  var re9 = /\)/g;
  var re10 = /\*/g;
  var re11 = /\+/g;
  var re12 = /,/g;
  var re13 = /-/g;
  var re14 = /\./g;
  var re15 = /:/g;
  var re16 = /;/g;
  var re17 = /</g;
  var re18 = /=/g;
  var re19 = />/g;
  var re20 = /\?/g;
  var re21 = /@/g;
  var re22 = /|/g;
  var re23 = /~/g;
                             
	theString = theString.replace(re4, "\%25");     // be sure to encode percents first or else it will mess up the rest of the replacements 
	theString = theString.replace(re0, "%21");
	theString = theString.replace(re1, "%22");
	theString = theString.replace(re2, "%23");
	theString = theString.replace(re3, "%24");
	theString = theString.replace(re11, "%2B");     //Encode '+' as '%2B' ** must do this before encoding ' ' as '+'
	theString = theString.replace(re5, "+");        //Encode ' ' as '+'
	theString = theString.replace(re6, "%26");
	theString = theString.replace(re7, "%27"); 
	theString = theString.replace(re8, "%28");
	theString = theString.replace(re9, "%29");
	theString = theString.replace(re10, "%2A");   

	theString = theString.replace(re12, "%2C");   
	//theString = theString.replace(re13, "%2D");   // PayPal doesn't like dashes "-" encoded
	//theString = theString.replace(re14, "%2E");   // PayPal doesn't like periods "." encoded
	//theString = theString.replace(re15, "%3A");   // PayPal doesn't like colons ":" encoded
	theString = theString.replace(re16, "%3B");   
	theString = theString.replace(re17, "%3C");   
	theString = theString.replace(re18, "%3D");   
	theString = theString.replace(re19, "%3E");   
	theString = theString.replace(re20, "%3F");   
	//theString = theString.replace(re21, "%40");   // PayPal doesn't like the at symbol "@" encoded
	//theString = theString.replace(re22, "%7C");   // DW MX doesn't like encoding pipe characters
	theString = theString.replace(re23, "%7E");   
	return theString;
}

/*
** Full URL Encoding of non-alphanumeric characters (escape() only performs a subset of
** these characters).
*/
function WAURLDecode(theString)     {
	var re0 = /%21/gi;
  var re1 = /%22/gi;
	var re2 = /%23/gi;
	var re3	= /%24/gi;
	var re4 = /%25/gi;
	var re5 = /\+/gi; 
  var re6 = /%26/gi; 
  var re7 = /%27/gi;
  var re8 = /%28/gi;
  var re9 = /%29/gi;
  var re10 = /%2A/gi;
  var re11 = /%2B/gi;
  var re12 = /%2C/gi;
  var re13 = /%2D/gi;
  var re14 = /%2E/gi;
  var re15 = /%3A/gi;
  var re16 = /%3B/gi;
  var re17 = /%3C/gi;
  var re18 = /%3D/gi;
  var re19 = /%3E/gi;
  var re20 = /%3F/gi;
  var re21 = /%40/gi;
  var re22 = /%7C/gi;
  var re23 = /%7E/gi;
                             
	theString = theString.replace(re0, "!");
	theString = theString.replace(re1, "\"");
	theString = theString.replace(re2, "#");
	theString = theString.replace(re3, "$");

	theString = theString.replace(re5, " ");      // decode '+' to ' ' ** must do this before decoding '%2B' to '+'
	theString = theString.replace(re6, "&");
	theString = theString.replace(re7, "'"); 
	theString = theString.replace(re8, "(");
	theString = theString.replace(re9, ")");
	theString = theString.replace(re10, "*");   
	theString = theString.replace(re11, "+");     // decode '%2B' to '+'
	theString = theString.replace(re12, ",");   
	theString = theString.replace(re13, "-");   
	theString = theString.replace(re14, ".");   
	theString = theString.replace(re15, ":");   
	theString = theString.replace(re16, ";");   
	theString = theString.replace(re17, "<");   
	theString = theString.replace(re18, "=");   
	theString = theString.replace(re19, ">");   
	theString = theString.replace(re20, "?");   
	theString = theString.replace(re21, "@");   
	//theString = theString.replace(re22, "|");   // DW MX doesn't like encoding pipe characters
	theString = theString.replace(re23, "~");   
	
  theString = theString.replace(re4, "%");      // decode percents last
	return theString;
}

function getButtonString(inString)    {
  var outPad = "   ";
  if ( (parseInt(dreamweaver.appVersion) >= 6) && dreamweaver.isOSX() )
  {
    // no extra padding for MacOS X
    outPad = "";
  }
  
  return (outPad + inString + outPad);
}

function closeWindow()  {
  window.close();
  dreamweaver.forceGarbageCollection();
}
