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

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

function receiveArguments()     {
  INSPECTIONPARAMS = true;
}

/*************************** end tab definitions ***************************/
/* 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.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 = 360;
	document.pplogo.top = 361;
	}
  document.OK.visibility = "visible";
  document.cancel.visibility = "visible";
  document.help.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;
    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 "submit":  // button image URL
        var theImg = document.generalWP.document.vcimage;   
        theVal = getTagProp(firstInput,"input","src");
        var myImage = getTranslatedVCImage(theVal);
        if (myImage == CUSTOM_VC_IMG)
        {
          myImage = "other";
          document.generalWP.document.othervcimage.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('VC');
  
  Pg0_Load()
  
  MM.clearBusyCursor();
  
  return true;
}

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



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

function updateVC(formTag)  {
  var theForm = formTag;
  var theBusiness = document.generalWP.document.ppUID.value;
  var theButtonImage = document.generalWP.document.vcimage[0].value;
  for (x in document.generalWP.document.vcimage)  {
    if (document.generalWP.document.vcimage[x].checked)
      theButtonImage = document.generalWP.document.vcimage[x].value;
  }
  if (theButtonImage == "other")
    theButtonImage = document.generalWP.document.othervcimage.value;
  else  
    theButtonImage = "http://images.paypal.com/images/" + theButtonImage;
  theForm = updateFormInput(theForm,"business",theBusiness);
  theForm = updateFormInput(theForm,"receiver_email",theBusiness);
  theForm = updateFormInput(theForm,"mrb",MRB);  
  theForm = updateFormInput(theForm,"pal",PAL);      
  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;
}

/******************************** PAGE FUNCTIONS ********************************/
function Pg0_Load()     {
  document.generalWP.document.ppUID.focus();
  return true;
}
function Pg0_unLoad()     {
  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()");
}
