/*-----------------------------------------------------------------------------
-	File Name:
-		Edit PayPal Add to Cart.js
-
-	Description:
-		Functions for PayPal Add to Cart 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 2003 WebAssist.com Corporation.  All rights reserved.
------------------------------------------------------------------------------*/
var CURRENTPAGE = 0;

var theOrig = "";
var updateVersion = false;

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

function displayHelp()     {
  var thePageNum = CURRENTPAGE;
  var anchorLink = "";
  switch (thePageNum)
  {
    case 1:
      break;
      
    case 2:
      anchorLink = "custom_logo";
      break;
      
    case 3:
      anchorLink = "spending_limit";
      break;
      
    default:
      break;
  }
  
  displayContextHelp(anchorLink);
}

/* 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.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";
  
  // Get values from the inspected node and populate our UI accordingly
  var theDOM = dw.getDocumentDOM();
  var theSelection = theDOM.getSelectedNode();
  theOrig = unescape(theSelection.orig);
  if (getTagType(theOrig).toLowerCase() == "a")  {
    updateVersion = true;
    theOrig = unescape(theSelection.orig.replace(/\%2B/ig," "));
    var theImg = theOrig.match(/<img\s.*src=(['"])([^'"]*)\1/)[2];
    theOrig = theOrig.match(/(['"])[^'"]*https?:\/\/www\.paypal\.com\/cart\/([^'"]*)\1/i)[2];
    theOrig = "cmd=_cart&"+theOrig;
    theOrig = '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="image" name="submit" src="'+theImg+'" border="0" alt="Make payments with PayPal - it\'s fast, free and secure!">\r\n<input type="hidden" name="' + theOrig.replace(/=/g,'" value="').replace(/\&/g,'">\r\n<input type="hidden" name="') + '">\r\n</form>';
  }
  else  {
    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.itemName.value = theVal;      
        break;

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

      case "amount":
        if (theVal != "")   
          document.itemWP.document.itemPrice.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 "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 "no_shipping":
        if (theVal == "1")
        {
          document.itemWP.document.shippingNo[1].checked = true;
        }
        break;                                                                                

      case "no_note":
        if (theVal == "1")
        {
          document.itemWP.document.includeNote[1].checked = true;
        }      
        break;

      case "submit":  // button image URL
        var theImg = document.buttonWP.document.acimage;   
        theVal = getTagProp(firstInput,"input","src");
        var myImage = getTranslatedACImage(theVal);
        if (myImage == CUSTOM_AC_IMG)
        {
          myImage = "other";
          document.buttonWP.document.otheracimage.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('AC');

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

function insertTag()    {
  var myErrors = validateUI();
  if (myErrors == "")
  {
    updateATC(theOrig);
  }
  else
  {
    alert (myErrors);
  }
}

function updateATC(formTag)  {
  var theForm = formTag;
  var theBusiness = document.generalWP.document.ppUID.value;
  var theItemName = document.itemWP.document.itemName.value;
  var theItemNumber = document.itemWP.document.itemID.value;
  var theAmount = document.itemWP.document.itemPrice.value;
  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.itemWP.document.shippingNo[0].checked)?"0":"1";  
  var theNoNote = (document.itemWP.document.includeNote[1].checked)?"1":"0";
  var theButtonImage = document.buttonWP.document.acimage[0].value;
  for (x in document.buttonWP.document.acimage)  {
    if (document.buttonWP.document.acimage[x].checked)
      theButtonImage = document.buttonWP.document.acimage[x].value;
  }
  if (theButtonImage == "other")
    theButtonImage = document.buttonWP.document.otheracimage.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 = "";
    
  theForm = updateFormInput(theForm,"business",theBusiness);
  theForm = updateFormInput(theForm,"item_name",theItemName);
  theForm = updateFormInput(theForm,"item_number",theItemNumber);
  theForm = updateFormInput(theForm,"amount",theAmount);
  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,"no_note",theNoNote);
  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();
  if (!updateVersion)  {
    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;
}

function validateUI()    {
  var errMsg = "";
  var accountErrMsg = validatePPUID();
  var nameErrMsg = validateItemName();
  var priceErrMsg = validateItemPrice();

  if ( (accountErrMsg != "") || (nameErrMsg != "") || (priceErrMsg != "") )
  {
    errMsg = "The following errors were found:\n\n";
  }
  
  if (accountErrMsg != "")
    errMsg += "PayPal Account " + accountErrMsg;
    
  if (nameErrMsg != "")
    errMsg += "Item Name " + nameErrMsg;
    
  if (priceErrMsg != "")
    errMsg += MSG_ItemPrice + priceErrMsg;    

  return errMsg;
}

/******************************** 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.itemName.focus()",500);
  return true;
}
function Pg2_unLoad()     {
  document.itemWP.visibility = "hidden";
  document.itemWP.visibility = "visible";
  document.itemWP.visibility = "hidden";
  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()");
}