/*-----------------------------------------------------------------------------
-	File Name:
-		Edit PayPal Subscriptions.js
-
-	Description:
-		Functions for PayPal Subscriptions Advanced Mode.
-
-	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 2002 WebAssist.com Corporation.  All rights reserved.
------------------------------------------------------------------------------*/
var CURRENTPAGE = 0;
var INSPECTIONPARAMS = false;
var INTERFACE        = "advanced";
var theOrig = "";

function commandButtons()     {
  return new Array();
}

function displayHelp()     {
  INTERFACE   = "advanced";
  displayContextHelp();
}

function receiveArguments(charttype, ssRec, paramString)     {
  INSPECTIONPARAMS = true;
}

/* Initialize the UI.  First thing that's called on the body load event. */
function initializeUI()     {
  MM.setBusyCursor();
  if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     {
    document.background.document.tabBgWin.src = "../Shared/PayPal/images/AdvancedBG_Mac.gif";
	document.Tab0.document.tabBG.src = "../Shared/PayPal/images/tabBg_Mac.gif";
	document.Tab1.document.tabBG.src = "../Shared/PayPal/images/longTabBg_Mac.gif";
	document.Tab2.document.tabBG.src = "../Shared/PayPal/images/longTabBg_Mac.gif";
	document.Tab3.document.tabBG.src = "../Shared/PayPal/images/TabBg_Mac.gif";
	document.background.style = "top:23px;";
	
	document.cancel.style = "position:absolute; width:75px; height:20px; z-index:94; left: 290px; top: 373px; visibility: visible";
	document.cancel.document.btnCancel.style = "width:85; height:20; font-family:'Lucidia Grande'; font-size: 10px";
    
	document.OK.style = "position:absolute; width:55px; height:20px; z-index:94; left: 385px; top: 373px; visibility: visible";
	document.OK.document.btnOK.style = "width:50; height:20; font-family:'Lucidia Grande'; font-size: 10px";
    
	document.help.style = "position:absolute; width:55px; height:20px; z-index:94; left: 231px; top: 373px; visibility: visible";
	document.help.document.btnHelp.style = "width:50; height:20; font-family:'Lucidia Grande'; font-size: 10px";

  
    document.walogo.top = 375;
	document.pplogo.top = 376;
	}
  document.OK.visibility = "visible";
  document.cancel.visibility = "visible";
  document.help.visibility = "visible";
  document.background.visibility = "visible";
  document.Tab0.visibility = "visible";
  document.Tab1.visibility = "visible";
  document.Tab2.visibility = "visible";
  document.Tab3.visibility = "visible";
  // Get values from the inspected node and populate our UI accordingly
  var theDOM = dw.getDocumentDOM();
  var theSelection = theDOM.getSelectedNode();
  theOrig = unescape(theSelection.orig);

  while (theSelection.tagName!="FORM" && theSelection.parentNode && theSelection.PPwholeForm!="true")  {
    theSelection = theSelection.parentNode;
  }
  theOrig = theSelection.outerHTML;

  var tempOrig = theOrig;
  var firstInput = findOpenTag(tempOrig,"input");
  while (firstInput != "")
  {
    var inputName = getTagProp(firstInput,"input","name");
    var theVal = getTagProp(firstInput,"input","value");
    switch (inputName.toLowerCase())
    {
      case "business":
        if (theVal != "")   
          document.generalWP.document.ppUID.value = theVal;      
        break;

      case "item_name":
        if (theVal != "")   
          document.itemWP.document.subscriptionName.value = theVal;      
        break;

      case "item_number":
        if (theVal != "")   
          document.itemWP.document.subscriptionID.value = theVal;      
        break;

      case "currency_code":
        var theCurrency = document.itemWP.document.itemCurrency;      
        for (var n=0; n<theCurrency.options.length; n++)
        {
            if (theVal == theCurrency.options[n].value)
            {
              theCurrency.selectedIndex = n;
              break;
            }
        }              
        break;                                                                                
                
      case "a1":
        if (theVal != "")   
          document.itemWP.document.trial1Amount.value = theVal;            
        break;

      case "p1": 
        var trialValue = document.itemWP.document.trial1Value;
        for (var i=0; i<trialValue.options.length; i++)
        {
          if (trialValue.options[i].value == theVal)
          {
            trialValue.selectedIndex = i;
          }      
        }    
        break;
              
      case "t1": 
        var trialValue = document.itemWP.document.trial1Period;
        for (var i=0; i<trialValue.options.length; i++)
        {
          if (trialValue.options[i].value == theVal)
          {
            trialValue.selectedIndex = i;
          }      
        }    
        break;
              

      case "a3":
        if (theVal != "")   
          document.itemWP.document.subscriptionPrice.value = theVal;            
        break;

      case "p3": 
        var trialValue = document.itemWP.document.billingValue;
        for (var i=0; i<trialValue.options.length; i++)
        {
          if (trialValue.options[i].value == theVal)
          {
            trialValue.selectedIndex = i;
          }      
        }    
        break;
              
      case "t3": 
        var trialValue = document.itemWP.document.billingPeriod;
        for (var i=0; i<trialValue.options.length; i++)
        {
          if (trialValue.options[i].value == theVal)
          {
            trialValue.selectedIndex = i;
          }      
        }    
        break;
                                               
      case "image_url":
        if (isValidURL(theVal))   
          document.generalWP.document.ppLogo.value = theVal;            
        break;

      case "return":
        if (isValidURL(theVal))   
          document.generalWP.document.successURL.value = theVal;
        break;
        
      case "cancel_return":
        if (isValidURL(theVal))   
          document.generalWP.document.cancelURL.value = theVal;      
        break;
  
      case "src":
        if (theVal == "1")
        {
          document.itemWP.document.recurringBilling[0].checked = true;
        }
        else
        {
          document.itemWP.document.recurringBilling[1].checked = true;        
        }
        break;                                                                                

      case "srt":
        document.itemWP.document.recurringBilling[1].checked = true;        
        var srtValue = document.itemWP.document.stopRecurringValue;
        for (var i=0; i<srtValue.options.length; i++)
        {
          if (srtValue.options[i].value == theVal)
          {
            srtValue.selectedIndex = i;
          }      
        }            
        break;                                                                                
                  
      case "sra":
        if (theVal == "1")
        {
          document.optionWP.document.reattemptBilling[0].checked = true;
        }
        else
        {
          document.optionWP.document.reattemptBilling[1].checked = true;        
        }
        break;                                                                                
                          
      case "no_shipping":
        if (theVal == "0")
        {
          document.optionWP.document.includeShipping[0].checked = true;
        }
        else
        {
          document.optionWP.document.includeShipping[1].checked = true;        
        }
        break;                                                                                

      case "submit":  // button image URL
        var theImg = document.buttonWP.document.subimage;   
        theVal = getTagProp(firstInput,"input","src");
        var myImage = getTranslatedSUBImage(theVal);
        if (myImage == CUSTOM_SUB_IMG)
        {
          myImage = "other";
          document.buttonWP.document.othersubimage.value = theVal;
        }
        
        for (var n=0; n<theImg.length; n++)
        {
            if (myImage == theImg[n].value)
            {
              theImg[n].checked = true;
            }
            else
            {
              theImg[n].checked = false;
            }
        }              
        break;                                                                                
                        
      default:
        break;
    }   

    tempOrig = tempOrig.substring(tempOrig.indexOf(firstInput)+firstInput.length); 
    firstInput = findOpenTag(tempOrig,"input");
    
  }  

  enableDisableOtherImage();
  enableDisableStopRecurring();
  
  // Go to the item details page
  showPageNum(2);
  
  MM.clearBusyCursor();
  
  return true;
}

/******************************** support functions ******************/
function generateSUB()
{
  var myErrors = validateUI();
  if (myErrors == "")
  {
    updateSUB(theOrig);
  }
  else
  {
    alert (myErrors);
  }
 
  return;
}

function validateUI()    {
  var errMsg = "";
  errMsg += validatePPUID();
  if (errMsg != "")     {
    errMsg = MSG_PaypalAccount + errMsg;
  }
  errMsg += validateSubsDetails();     
  if ( errMsg != "" )
  {
    errMsg = "The following errors were found:\n\n" + errMsg;
  }

  return errMsg;
}

function enableDisableOtherImage()     {
  var myImage = document.buttonWP.document.subimage;
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        document.buttonWP.document.othersubimage.disabled = "enabled";
      }
      else
      {
        document.buttonWP.document.othersubimage.disabled = "disabled";
      }
    }
  }
}

function enableDisableStopRecurring()   {
  if (document.itemWP.document.recurringBilling[1].checked)
  {
    document.itemWP.document.stopRecurringValue.disabled = "enabled";
  }
  else
  {
    document.itemWP.document.stopRecurringValue.disabled = "disabled";    
  } 
}

function updateSUB(formTag)  {
  var theForm = formTag;
  var theBusiness = document.generalWP.document.ppUID.value;
  var theItemName = document.itemWP.document.subscriptionName.value;
  var theItemNumber = document.itemWP.document.subscriptionID.value;
 
  var theTrialAmount = document.itemWP.document.trial1Amount.value
  var theTrialPeriod = document.itemWP.document.trial1Value.options[document.itemWP.document.trial1Value.selectedIndex].value
  var theTrialTime = document.itemWP.document.trial1Period.options[document.itemWP.document.trial1Period.selectedIndex].value
  var theSubAmount = document.itemWP.document.subscriptionPrice.value;
  var theSubPeriod = document.itemWP.document.billingValue.options[document.itemWP.document.billingValue.selectedIndex].value
  var theSubTime = document.itemWP.document.billingPeriod.options[document.itemWP.document.billingPeriod.selectedIndex].value
  var Recurr = (document.itemWP.document.recurringBilling[0].checked)?"1":"0";
  var StopAfter = document.itemWP.document.stopRecurringValue.options[document.itemWP.document.stopRecurringValue.selectedIndex].value;
  var ReAttempt = (document.optionWP.document.reattemptBilling[0].checked)?"1":"0";
  var theCurrencyCode = document.itemWP.document.itemCurrency.options[document.itemWP.document.itemCurrency.selectedIndex].value;
  var theImageURL = document.generalWP.document.ppLogo.value;
  var theReturn = document.generalWP.document.successURL.value;
  var theCancel = document.generalWP.document.cancelURL.value
  var theNoShip = (document.optionWP.document.includeShipping[0].checked)?"0":"1";
  var theButtonImage = document.buttonWP.document.subimage[0].value;
  for (x in document.buttonWP.document.subimage)  {
    if (document.buttonWP.document.subimage[x].checked)
      theButtonImage = document.buttonWP.document.subimage[x].value;
  }
  if (theButtonImage == "other")
    theButtonImage = document.buttonWP.document.othersubimage.value;
  else  
    theButtonImage = "http://images.paypal.com/images/" + theButtonImage;
    
  if (theImageURL == "https://"  || theImageURL == "http://")
    theImageURL = "";
  if (theReturn == "https://"  || theReturn == "http://")
    theReturn = "";
  if (theCancel == "https://"  || theCancel == "http://")
    theCancel = "";
  
  if (theTrialAmount == "")  {
    theTrialPeriod = "";
    theTrialTime = "";
  }
  
  if (theSubAmount == "")  {
    theSubPeriod = "";
    theSubTime = "";
  }
  
  if (Recurr == "1")  {
    StopAfter = "";
  }
    
  theForm = updateFormInput(theForm,"business",theBusiness);
  theForm = updateFormInput(theForm,"item_name",theItemName);
  theForm = updateFormInput(theForm,"item_number",theItemNumber);
  theForm = updateFormInput(theForm,"a1",theTrialAmount);
  theForm = updateFormInput(theForm,"p1",theTrialPeriod);
  theForm = updateFormInput(theForm,"t1",theTrialTime);
  theForm = updateFormInput(theForm,"a3",theSubAmount);
  theForm = updateFormInput(theForm,"p3",theSubPeriod);
  theForm = updateFormInput(theForm,"t3",theSubTime);
  theForm = updateFormInput(theForm,"sra",ReAttempt);
  theForm = updateFormInput(theForm,"srt",StopAfter);
  theForm = updateFormInput(theForm,"src",Recurr);
  theForm = updateFormInput(theForm,"currency_code",theCurrencyCode);
  theForm = updateFormInput(theForm,"image_url",theImageURL);
  theForm = updateFormInput(theForm,"return",theReturn);
  theForm = updateFormInput(theForm,"cancel_return",theCancel);
  theForm = updateFormInput(theForm,"receiver_email",theBusiness);
  theForm = updateFormInput(theForm,"mrb",MRB);  
  theForm = updateFormInput(theForm,"pal",PAL);      
  theForm = updateFormInput(theForm,"no_shipping",theNoShip);
  theForm = updateFormInput(theForm,"bn",BNVersion);
  theForm = updateImage(theForm,theButtonImage);
  var theDOM = dreamweaver.getDocumentDOM();
  var thePage = theDOM.documentElement.outerHTML;
  var theSelection = theDOM.getSelectedNode();
  var theOffsets = new Array();
  while (theSelection.tagName!="FORM" && theSelection.PPwholeForm!="true")  {
    theSelection = theSelection.parentNode;
  }
  theDOM.setSelectedNode(theSelection);
  theDOM.insertHTML(theForm);
  closeWindow();
}

function updateImage(formHTML,image)  {
  var theInput = /<input\s[^<]*type=(['"])image\1/i;
  if (formHTML.search(theInput)>=0)  {
    var theTag = findOpenTag(formHTML.substring(formHTML.search(theInput)),"input");
    formHTML = formHTML.substring(0,formHTML.indexOf(theTag)) + updateTagProp(theTag,"input","src",image) + formHTML.substring(formHTML.indexOf(theTag)+theTag.length);
  }
  else  {
    formHTML = formHTML.substring(0,formHTML.search(/\s*<\/form>/i))+'\r\n<input type="image" name="submit" src="'+image+'" border="0" alt="Make payments with PayPal - it\'s fast, free and secure!">'+formHTML.substring(formHTML.search(/\s*<\/form>/i));
  }
  return formHTML;
}

function updateFormInput(formHTML,fieldName,fieldValue)  {
  var theInput = new RegExp('<input\\s[^<]*name=([\'"]?)'+fieldName+'\\1',"i");
  if (formHTML.search(theInput)>=0)  {
    var theTag = findOpenTag(formHTML.substring(formHTML.search(theInput)),"input");
    if (fieldValue!="")
      formHTML = formHTML.substring(0,formHTML.indexOf(theTag)) + updateTagProp(theTag,"input","value",fieldValue) + formHTML.substring(formHTML.indexOf(theTag)+theTag.length);
    else
      formHTML = formHTML.substring(0,formHTML.indexOf(theTag)) + formHTML.substring(formHTML.indexOf(theTag)+theTag.length);
  }
  else  {
    if (fieldValue!="")
      formHTML = formHTML.substring(0,formHTML.search(/\s*<\/form>/i))+'\r\n<input type="hidden" name="'+fieldName+'" value="'+fieldValue+'">'+formHTML.substring(formHTML.search(/\s*<\/form>/i));
  }
  return formHTML;
}

/******************************** PAGE FUNCTIONS ********************************/
function Pg0_Load()     {
  document.generalWP.visibility = "visible";
  setTimeout("document.generalWP.document.ppUID.focus()",500);
  return true;
}
function Pg0_unLoad()     {
  document.generalWP.visibility = "hidden";
  document.generalWP.visibility = "visible";
  document.generalWP.visibility = "hidden";
  return true;
}

function Pg1_Load()     {
  document.buttonWP.visibility = "visible";
  return true;
}
function Pg1_unLoad()     {
  document.buttonWP.visibility = "hidden";
  document.buttonWP.visibility = "visible";
  document.buttonWP.visibility = "hidden";
  return true;
}

function Pg2_Load()     {
  document.itemWP.visibility = "visible";
  setTimeout("document.itemWP.document.subscriptionName.focus()",500);
  return true;
}
function Pg2_unLoad()     {
  document.itemWP.visibility = "hidden";
  document.itemWP.visibility = "visible";
  document.itemWP.visibility = "hidden";
  return true;
}

function Pg3_unLoad()     {
  document.optionWP.visibility = "hidden";
  document.optionWP.visibility = "visible";
  document.optionWP.visibility = "hidden";
  return true;
}

function Pg3_Load()     {
  document.optionWP.visibility = "visible";
  //setTimeout("document.optionWP.document.reattemptBilling[0].focus()",500);
  return true;
}

function showPageNum(pageNum)  {
  eval("Pg"+CURRENTPAGE+"_unLoad()");
  if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     {
    eval("document.Tab"+CURRENTPAGE+".document.tabBG.src = document.Tab"+CURRENTPAGE+".document.tabBG.src.substring(0,document.Tab"+CURRENTPAGE+".document.tabBG.src.toLowerCase().lastIndexOf('tabbg'))+'tabBg_Mac.gif'");
  }
  else  {
    eval("document.Tab"+CURRENTPAGE+".document.tabBG.src = document.Tab"+CURRENTPAGE+".document.tabBG.src.substring(0,document.Tab"+CURRENTPAGE+".document.tabBG.src.toLowerCase().lastIndexOf('tabbg'))+'tabBg.gif'");
  }
  CURRENTPAGE = pageNum;
   if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     {
    eval("document.Tab"+CURRENTPAGE+".document.tabBG.src = document.Tab"+CURRENTPAGE+".document.tabBG.src.substring(0,document.Tab"+CURRENTPAGE+".document.tabBG.src.toLowerCase().lastIndexOf('tabbg'))+'tabBgSel_Mac.gif'");
   }
  else  {  
    eval("document.Tab"+CURRENTPAGE+".document.tabBG.src = document.Tab"+CURRENTPAGE+".document.tabBG.src.substring(0,document.Tab"+CURRENTPAGE+".document.tabBG.src.toLowerCase().lastIndexOf('tabbg'))+'tabBgSel.gif'");
  }
  eval("Pg"+CURRENTPAGE+"_Load()");
}
