/*-----------------------------------------------------------------------------
-	File Name:
-		Edit PayPal Donate.js
-
-	Description:
-		Functions for PayPal Donate 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 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.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);

  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 "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.sipimage;   
        theVal = getTagProp(firstInput,"input","src");
        var myImage = getTranslatedSIPImage(theVal);
        if (myImage == CUSTOM_SIP_IMG)
        {
          myImage = "other";
          document.buttonWP.document.othersipimage.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();
  
  // Go to the details page
  showPageNum(2);
   
  MM.clearBusyCursor();
  
  return true;
}

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

function updateD(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 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.sipimage[0].value;
  
  if (theImageURL == "https://"  || theImageURL == "http://")
    theImageURL = "";
  if (theReturn == "https://"  || theReturn == "http://")
    theReturn = "";
  if (theCancel == "https://"  || theCancel == "http://")
    theCancel = "";
  
  for (x in document.buttonWP.document.sipimage)  {
    if (document.buttonWP.document.sipimage[x].checked)
      theButtonImage = document.buttonWP.document.sipimage[x].value;
  }
  if (theButtonImage == "other")
    theButtonImage = document.buttonWP.document.othersipimage.value;
  else  
    theButtonImage = "http://images.paypal.com/images/" + theButtonImage;
  theForm = updateFormInput(theForm,"business",theBusiness);
  theForm = updateFormInput(theForm,"item_name",theItemName);
  theForm = updateFormInput(theForm,"item_number",theItemNumber);
  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();  
  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[^\r\n]*type=(['"])image\1/i;
  if (formHTML.search(theInput)>=0)  {
    var theTag = formHTML.substring(formHTML.search(theInput));
	while (theTag.substring(1).search(theInput)>=0)  {
	  theTag = theTag.substring(theTag.substring(1).search(theInput)+1);
	}
	theTag = findOpenTag(theTag, "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[^\r\n]*name=([\'"]?)'+fieldName+'\\1',"i");
  var theImg = /<input\s[^\r\n]*type=(['"])image\1/i;
  if (formHTML.search(theInput)>=0)  {
    var theTag = formHTML.substring(formHTML.search(theInput));
	while (theTag.substring(1).search(theInput)>=0)  {
	  theTag = theTag.substring(theTag.substring(1).search(theInput)+1);
	}
	theTag = findOpenTag(theTag, "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!="")  {
	  if (formHTML.search(theImg)>=0)  {
		var theImgTag = formHTML.substring(formHTML.search(theImg));
	    while (theImgTag.substring(1).search(theImg)>=0)  {
	      theImgTag = theImgTag.substring(theImgTag.substring(1).search(theImg)+1);
	    }
	    theImgTag = findOpenTag(theImgTag, "input");
		formHTML = formHTML.substring(0,formHTML.indexOf(theImgTag)) + '  <input type="hidden" name="'+fieldName+'" value="'+fieldValue+'">\r\n' + formHTML.substring(formHTML.indexOf(theImgTag))
	  }
	  else
        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 tempErrMsg = ""; 
  var accountErrMsg = validatePPUID();
  if (accountErrMsg != "")
  {
    errMsg += "PayPal Account " + accountErrMsg;
  }
   
  if ( errMsg != "" )
  {
    errMsg = "The following errors were found:\n\n" + errMsg;
  }
  
  return errMsg;
}

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

/******************************** PAGE FUNCTIONS ********************************/
function Pg0_Load()     {
  document.generalWP.visibility = "visible";
  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";
  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()");
}