/*-----------------------------------------------------------------------------
-	File Name:
-		Insert PayPal Subscriptions.js
-
-	Description:
-		Functions for Subscriptions wizard.
-
-	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;

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

/* Initialize the UI.  First thing that's called on the body load event. */
function initializeUI()     {
  if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     {
		
	document.cancel.style = "position:absolute; width:90px; height:20px; z-index:90; left: 256px; top: 397px; visibility: visible";
	document.cancel.document.btnCancel.style = "width:85; height:20; font-family:'Lucidia Grande'; font-size: 10px";
		
	document.help.style = "position:absolute; width:90px; height:20px; z-index:90; left: 165px; top: 397px; visibility: visible";
	document.help.document.btnHelp.style = "width:85; height:20; font-family:'Lucidia Grande'; font-size: 10px";
		
	document.next.style = "position:absolute; width:99px; height:20px; z-index:90; left: 439px; top: 397px; visibility: visible";
	document.next.document.btnNext.style = "width:98; height:20; font-family:'Lucidia Grande'; font-size: 10px";
		
	document.back.style = "position:absolute; width:90px; height:20px; z-index:90; left: 347px; top: 397px; visibility: visible";
	document.back.document.btnBack.style = "width:85; height:20; font-family:'Lucidia Grande'; font-size: 10px";
		
	document.finish.style = "position:absolute; width:99px; height:20px; z-index:90; left: 439px; top: 397px; visibility: visible";
	document.finish.document.btnFinish.style = "width:98; height:20; font-family:'Lucidia Grande'; font-size: 10px";

  
    document.walogo.top = 402;
	document.pplogo.top = 403;
  }
  document.back.visibility = "visible";
  document.next.visibility = "visible";
  document.help.visibility = "visible";
  document.cancel.visibility = "visible";
  var theDOM = dreamweaver.getDocumentDOM();
  var theParent = theDOM.getSelectedNode();
  var inForm = false;
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
	    inForm = theParent;
	    break;
    }
	}
  if (inForm)  {
    if (inForm.innerHTML.search(/<input type="[^"]*"[^>]*\sname="cmd"[^>]*>/i)>=0)  
    {
        alert(MSG_AlreadyPayPalInForm);
        window.close();
    }
  }
  MM.setBusyCursor();
   
  showPageNum(0);
  
  getLastUID();  
  enableDisableOtherImage();
  enableDisableStopRecurring();
  MM.clearBusyCursor();
}

/******************************** support functions ******************/
function buildSummaryText()     {
  var retVal = "";
  var myID       = findObject("ppUID").value;
  var myImage    = findObject("subimage");
  var myImageURL = "";
  var myItemName = findObject("subscriptionName").value;
  var myItemID = findObject("subscriptionID").value;
  var myAmount = findObject("subscriptionPrice").value;
  var myCurrency = findObject("itemCurrency").options[findObject("itemCurrency").selectedIndex].value;    
  var myTrial1 = findObject("trial1Amount").value;
  var myTrial2 = findObject("trial2Amount").value;
  var myIncludeShipping = true;
  var myBillRecurring = true; 
  var myReattemptPayment = true;
  if (findObject("includeShipping")[1].checked)
  {
    myIncludeShipping = false;
  }
  if (findObject("recurringBilling")[1].checked)
  {
    myBillRecurring = false;
  }   
  if (findObject("reattemptBilling")[1].checked)
  {
    myReattemptPayment = false;
  }
  var myLogoURL = findObject("ppLogo").value;
  var mySuccessURL = findObject("successURL").value;
  var myCancelURL = findObject("cancelURL").value;
    
  retVal += MSG_PaypalAccountSummary + myID;
  retVal += "\n";
  retVal += MSG_ButtonImageURL;
  for (var n=0; n<myImage.length; n++)
  {
    if (myImage[n].checked)
    {
      if (myImage[n].value == "other")
      {
        retVal += findObject("othersubimage").value;      
      }
      else
      {
        retVal += "http://images.paypal.com/images/" + myImage[n].value;      
      }
    }
  }
  retVal += "\n";
  
  if (myItemName != "")
  {
    retVal += MSG_SubscriptionName + myItemName + "\n";
  }
  
  if (myItemID != "")
  {
    retVal += MSG_SubscriptionID + myItemID + "\n";  
  }
  
  retVal += MSG_Currency + getCurrencyType(myCurrency) + "\n";    
    
  if (myAmount != "")
  {
    retVal += MSG_ChargeCustomers + myAmount + MSG_Every + findObject("billingValue").options[findObject("billingValue").selectedIndex].value + " " + displayTimePeriod(findObject("billingPeriod").options[findObject("billingPeriod").selectedIndex].value) + "\n";
  } 
  
  if (myBillRecurring)
  {
    retVal += MSG_BillRecurring;
  }
  else
  {
    retVal += MSG_StopBilling + findObject("stopRecurringValue").options[findObject("stopRecurringValue").selectedIndex].value + MSG_PaymentsNewline;
  }

  if (isValidAmount(myTrial1))
  {
    retVal += MSG_TrialPeriod + myTrial1 + " - " + findObject("trial1Value").options[findObject("trial1Value").selectedIndex].value + " " + displayTimePeriod(findObject("trial1Period").options[findObject("trial1Period").selectedIndex].value) + "\n";
  }  
       
  retVal += MSG_ReattemptPayments;
  if (myReattemptPayment)
    retVal += MSG_Yes;
  else
    retVal += MSG_No;
  retVal += "\n";
      
  retVal += MSG_RequestCustomer;    
  if (!myIncludeShipping)
    retVal += MSG_No;
  else
    retVal += MSG_Yes;  
  retVal +="\n";
       
  if (isValidURL(myLogoURL))
  {
    retVal += MSG_CustomURL + myLogoURL + "\n";
  }
  
  if (isValidURL(mySuccessURL))
  {
    retVal += MSG_SuccessfulURL + mySuccessURL + "\n";
  }
  
  if (isValidURL(myCancelURL))
  {
    retVal += MSG_CancelledURL + myCancelURL + "\n";
  }
        
  return retVal;
}

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

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


function openBrowseImageWindow()      {
  var browseURL = "";
  var LABEL_Select = "Select Single Item Purchase Image...";  // label for file window
  browseURL = dreamweaver.browseForFileURL("select",LABEL_Select, false, true); // opens file window and returns file string as URL
  if (browseURL)
  {
    //browseURL = WA_absoluteToRelativeURL(browseURL, docpath, false);          
    findObject("othersubimage").value = browseURL;  //set the DataSource    
  }
  findObject("othersubimage").focus();
}

function displayTimePeriod(thePeriod)
{
  var retVal = "";
  switch (thePeriod.toLowerCase())
  {
    case "w":
      retVal = MSG_Weeks;
      break;

    case "m":
      retVal = MSG_Months;
      break;

    case "y":
      retVal = MSG_Years;
      break;
                  
    default:
      retVal = MSG_Days;
      break;
  }
  
  return retVal;
}

/****************************** page functions ****************************/
function Pg0_load()
{
  document.UserIDWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "disabled"); // disable the back button
  findObject("next").visibility = "visible";                  // enable next button
  findObject("finish").visibility = "hidden";                 // hide finish button

  // Set focus but this only seems to work if you delay the page loading by some amount of time (in msec)
  setTimeout('findObject("ppUID").focus()',500);
  return true;
}

function Pg0_unLoad()
{
  document.UserIDWP.visibility = "hidden";
  return true;
}

function Pg1_canLoad()      {
  var ppError = validatePPUID();
  if (ppError != "")
  {
    alert (MSG_PaypalAccountSummary + ppError);
    return false;
  }
  return true;
}

function Pg1_load()
{
  document.ButtonImageWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "enabled"); // disable the back button
  findObject("next").visibility = "visible";                  // enable next button
  findObject("finish").visibility = "hidden";                 // hide finish button
    // Set focus but this only seems to work if you delay the page loading by some amount of time (in msec)
  setTimeout('findObject("subimage")[0].focus()',500);
  return true;
}

function Pg1_unLoad()
{
  document.ButtonImageWP.visibility = "hidden";
  return true;
}

function Pg2_canLoad() 
{
  return true;
}

function Pg2_load()
{
  document.itemWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "enabled"); // disable the back button
  findObject("next").visibility = "visible";                  // enable next button
  findObject("finish").visibility = "hidden";                 // hide finish button

  // Set focus but this only seems to work if you delay the page loading by some amount of time (in msec)
  setTimeout('findObject("subscriptionName").focus()',500);
  return true;
}

function Pg2_unLoad()
{
  document.itemWP.visibility = "hidden";
  return true;
}

function Pg3_canLoad() 
{
  var subsDetailError = validateSubsDetails();
  if (subsDetailError != "")
  {
    alert (subsDetailError);
    return false;
  }
  return true;
}

function Pg3_load()
{
  document.shippingWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "enabled"); // disable the back button
  findObject("next").visibility = "visible";                  // enable next button
  findObject("finish").visibility = "hidden";                 // hide finish button
  // Set focus but this only seems to work if you delay the page loading by some amount of time (in msec)
  setTimeout('findObject("reattemptBilling")[0].focus()',500);
  return true;
}

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

function Pg4_canLoad() 
{
  return true;
}

function Pg4_load()
{
  document.logoWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "enabled"); // disable the back button
  findObject("next").visibility = "visible";                  // enable next button
  findObject("finish").visibility = "hidden";                 // hide finish button

  // Set focus but this only seems to work if you delay the page loading by some amount of time (in msec)
  setTimeout('findObject("ppLogo").focus()',500);
  return true;
}

function Pg4_unLoad()
{
  document.logoWP.visibility = "hidden";
  return true;
}

function Pg5_canLoad() 
{
  return true;
}

function Pg5_load()
{
  document.customWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "enabled"); // disable the back button
  findObject("next").visibility = "visible";                  // enable next button
  findObject("finish").visibility = "hidden";                 // hide finish button

  // Set focus but this only seems to work if you delay the page loading by some amount of time (in msec)
  setTimeout('findObject("successURL").focus()',500);
  return true;
}

function Pg5_unLoad()
{
  document.customWP.visibility = "hidden";
  return true;
}

function Pg6_canLoad() 
{
  return true;
}

function Pg6_load()
{
  document.SummaryWP.visibility = "visible";
  findObject("btnBack").setAttribute("disabled", "enabled"); // disable the back button
  findObject("next").visibility = "hidden";                  // enable next button
  findObject("finish").visibility = "visible";                 // hide finish button

  findObject("summaryText").value = buildSummaryText();
  return true;
}

function Pg6_unLoad()
{
  document.SummaryWP.visibility = "hidden";
  return true;
}

function showNextPage()   {
  var nextPage = CURRENTPAGE + 1;
  if (eval("Pg"+nextPage+"_canLoad()"))
  {
    eval("Pg"+CURRENTPAGE+"_unLoad()");
    CURRENTPAGE++;
    eval("Pg"+CURRENTPAGE+"_load()");  
  }
}

function showPreviousPage()   {
  eval("Pg"+CURRENTPAGE+"_unLoad()");
  CURRENTPAGE--;
  eval("Pg"+CURRENTPAGE+"_load()");
}

function showPageNum(pageNum)  {
  eval("Pg"+CURRENTPAGE+"_unLoad()");
  CURRENTPAGE = pageNum;
  eval("Pg"+CURRENTPAGE+"_load()");
}