/*-----------------------------------------------------------------------------
-	File Name:
-		GoogleFreeShared.js
-
-	Description:
-		Shared functions for Google Free Search objects.
-
-	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 2004 WebAssist.com Corporation.  All rights reserved.
------------------------------------------------------------------------------*/
var GOOGLESEARCHENGLISH = "Google Search";

/**************************** errormessages ****************************/
/***   Localized in html  **/
/***************************** open broswer windows ********************/

// Code footprint generator.
function generateCustom()     {
  var theDOM     = dw.getDocumentDOM();
  var customTag  = "";
  var cofVal     = "";
  var page1      = document.DomainWP.document;
  var page2      = document.LogoWP.document;
  var page3      = document.ColorWP.document;
  var searchType = page1.searchRDO[1].checked;
  var theDomain  = page1.domains.value;
  var safeSearch = page1.safeSearchCHK.checked;
  var logoURL    = page2.logoURL.value;
  var logoWidth  = page2.width.value;
  var logoHeight = page2.height.value;
  var logoAlign  = page2.logoAlignLST.options[page2.logoAlignLST.selectedIndex].value;
  var logoBGCol  = page2.googleBGColorLST.options[page2.googleBGColorLST.selectedIndex].value;
  var pageBGCol  = page3.bgColor.value;
  var pageBGURL  = page3.bgURL.value;
  var textCol    = page3.textColor.value;
  var altCol     = page3.altColor.value;
  var impCol     = page3.importantColor.value;
  var faintCol   = page3.faintColor.value;
  var linkCol    = page3.linkColor.value;
  var activeCol  = page3.activeColor.value;
  var visitCol   = page3.visitedColor.value;
  var googleLink = "";
  var googleImg  = "Logo_40wht.gif";
  var inForm = false;
 
  var theParent = theDOM.getSelectedNode();
  while (theParent.parentNode)  {
    theParent = theParent.parentNode;
    if (theParent.tagName=="FORM")  {
      if (confirm(ALREADYINFORM))  {
        theDOM.setSelection(theDOM.nodeToOffsets(theParent)[1]-1,theDOM.nodeToOffsets(theParent)[1]-1);
	    }
      else  {
        window.close();
        return "";
      }    
	    break;
	  }
  } 
  var theGoogleSearch = GOOGLESEARCH;
  var formURL = "http://www.google.com/custom";
  var googleURL = "http://www.google.com";
  var encodingVars = "";
  var charSet = dw.getDocumentDOM().getCharSet();
  charSet = charSet.toLowerCase();

  // if japanese or korean, then add extra encoding variables and change form action
  if (dreamweaver.appVersion.indexOf('ja') != -1)
  {
    formURL = "http://www.google.co.jp/custom";
    googleURL = "http://www.google.co.jp";
    encodingVars += "        <input type=\"hidden\" name=\"hl\" value=\"ja\">\n";
    if (charSet != "shift_jis" && charSet != "euc-jp" && charSet != "utf-8")
    {
      theGoogleSearch = GOOGLESEARCHENGLISH;
    }
  }
  else if (dreamweaver.appVersion.indexOf('ko') != -1)
  {
    formURL = "http://www.google.co.kr/custom";
    googleURL = "http://www.google.co.kr";
    encodingVars += "        <input type=\"hidden\" name=\"hl\" value=\"ko\">\n";
    if (charSet != "euc-kr" && charSet != "utf-8")
    {
      theGoogleSearch = GOOGLESEARCHENGLISH;
    }
  }
  //Add hidden field with the current encoding if the encoding is shift_jis, euc-jp, euc-kr or utf-8
  if  (charSet == "shift_jis" || charSet == "euc-jp" || charSet == "euc-kr" || charSet == "utf-8")
  {
    encodingVars += "        <input type=\"hidden\" name=\"ie\" value=\"" + charSet + "\">\n";
  }

  MM.setBusyCursor();   
  if (safeSearch == true)   {
    googleLink += "search?safe=vss";
	googleImg = "Google_Safe.gif";
  }
  logoURL = logoURL.toLowerCase();
  if (logoURL != "" && logoURL != "http://" && logoURL != "https://")  {
    cofVal += "L:" + logoURL + ";";
	if (logoWidth != "") {
	  cofVal += "LW:" + logoWidth + ";";
	}
	if (logoHeight != "")  {
	  cofVal += "LH:" + logoHeight + ";";
	}
	if (logoAlign != "")  {
	  cofVal += "AH:" + logoAlign + ";";
	}
  }
  if (logoBGCol != "")  {
    cofVal += "GL:" + logoBGCol + ";";
  }
  if (pageBGCol != "")  {
    cofVal += "BGC:" + pageBGCol + ";";
  }
  pageBGURL = pageBGURL.toLowerCase();
  if (pageBGURL != "" && pageBGURL != "http://" && pageBGURL != "https://")  {
    cofVal += "BIMG:" + pageBGURL + ";";
  }
  if (textCol != "")  {
    cofVal += "T:" + textCol + ";";
  }
  if (altCol != "")  {
    cofVal += "GALT:" + altCol + ";";
  }
  if (impCol != "")  {
    cofVal += "GIMP:" + impCol + ";";
  }
  if (faintCol != "")  {
    cofVal += "GFNT:" + faintCol + ";";
  }
  if (linkCol != "")  {
    cofVal += "LC:" + linkCol + ";";
  }
  if (activeCol != "")  {
    cofVal += "ALC:" + activeCol + ";";
  }
  if (visitCol != "")  {
    cofVal += "VLC:" + visitCol + ";";
  }
  customTag += "<form method=\"get\" action=\""+formURL+"\">\n";
  customTag += "  <table bgcolor=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
  customTag += "    <tr>\n";
  customTag += "      <td nowrap><a href=\"" + googleURL + "/" + googleLink + "\"><img src=\"http://www.google.com/logos/" + googleImg + "\" border=\"0\" alt=\"Google search\" align=\"absmiddle\"></a></td>\n";
  customTag += "      <td nowrap>\n";
  customTag += "        <input type=\"text\" name=\"q\" size=\"25\" maxlength=\"255\" value=\"\">\n";
  customTag += encodingVars;
  customTag += "        <input type=\"submit\" name=\"btnG\" value=\""+theGoogleSearch+"\">\n";
  if (safeSearch == true)  {
    customTag += "        <input type=\"hidden\" name=\"safe\" value=\"vss\">\n";
  }
  if (cofVal != "")  {
    customTag += "        <input type=\"hidden\" name=\"cof\" value=\"" + cofVal + "\">\n";
  }
  if (searchType == true)  {
	customTag += "          <input type=\"hidden\" name=\"domains\" value=\"" + theDomain + "\"><br>\n";
	customTag += "          <input type=\"radio\" name=\"sitesearch\" value=\"\"> "+THREEW+" <input type=\"radio\" name=\"sitesearch\" value=\"" + theDomain + "\" checked> " + theDomain + " <br>\n";
  }
  customTag += "      </td>\n";
  customTag += "    </tr>\n";
  customTag += "  </table>\n";
  customTag += "</form>\n";
  
  theDOM.synchronizeDocument();
  theDOM.insertHTML(customTag, true);
  MM.clearBusyCursor();  
  window.close();
}

// General configuration path finder.
function WA_getConfigurationPath(pathFromConfig)     {
  var tPath  = dw.getTempFolderPath();
  tPath = tPath.substring(0, tPath.lastIndexOf("/"));  // strip out the temp
  tPath += pathFromConfig;
  
  // if we find the file in the temp path, then return it
  if (DWfile.exists(tPath))
  {
    return tPath;
  }
  
  var cPath = dw.getConfigurationPath();
 
  cPath += pathFromConfig;
  
  // if we find the file in the config path, then return it
  if (DWfile.exists(cPath))
  {
      return cPath;
  }
  
  // Default to returning temp folder path if file does not exist
  return tPath;
}

//context sensitive help function
function displayContextHelp(thePage)     {
  var tempPath = WA_getConfigurationPath("/Temp/ContextTemp.htm");
  var helpPath = WA_getConfigurationPath("/Shared/Google/FreeSearch/Help/help.htm");
  var PLATFORM = navigator.platform;
   
 var writePath = tempPath;
 
  var scriptTag = "";

  if (navigator.platform.toLowerCase().indexOf("mac") >= 0)     
  {
    helpPath = helpPath.substring(8);
       
    
    // apparently Mac doesn't like file:// url's with 3 slashes. Only wants 2.
    // -- jcheng #171088
    if (PLATFORM == "Win32")
    {
    	helpPath = "file:///" + helpPath.substring(helpPath.indexOf("/")+1);	 
    }
    else
    {
//    	helpPath = "file://" + helpPath.substring(helpPath.indexOf("/")+1);
		helpPath = "file://" + helpPath;
    } 
    tempPath = tempPath.substring(8);
    if (PLATFORM == "Win32")
    {
    	tempPath = "file:///" + tempPath.substring(tempPath.indexOf("/")+1);	  
	}
	else
	{
//		tempPath = "file://" + tempPath.substring(tempPath.indexOf("/")+1);
		tempPath = "file://" + tempPath;
	}
	  tempPath = escape(tempPath);
  }       
  
  if (thePage != "" && PLATFORM == "Win32")     {
    scriptTag += "<HTML>\r\n<HEAD>\r\n";
    scriptTag += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n"
	  scriptTag += "</HEAD>\r\n<BODY>\r\n"
    scriptTag += "<script language='JavaScript'>\r\n";
	  scriptTag += "  document.location.href = '" + helpPath + "#" + thePage + ">>cap="+GOOGLEHELP+">>pan=2>>pbs=toc|ndx|nls>>pdb=toc'\r\n";
	  scriptTag += "</script>\r\n</BODY>\r\n</HTML>";
	
    DWfile.write(writePath, scriptTag);
	
  }  
  

  else // no context-sensitive so open to help page
  {
    tempPath = helpPath;  
  }
  dreamweaver.browseDocument(tempPath);  
}


// Support functions


function openWABrowser()     {
  dreamweaver.browseDocument("http://www.webassist.com");
}

function openGBrowser()     {
  dreamweaver.browseDocument("http://www.google.com/services/");
}


function setColorBox(theElement, thePick)     {
// simply sets the form element to the returned value of the color button
  theElement.value = thePick.value;
}

function setColorPick(thePick, theElement)     {
// ensures that the user has entered a proper hexidecimal value
  var theColor = theElement.value;
  var badcolor = false;
  
  if (theColor == "")     {  // sets to default
    thePick.value = theColor;
  }
  else     {
    if (theColor.substring(0,1) != "#" || theColor.length != 7)     {// color must have a # and be 7 characters
      alert(MSG_BadColor);
      theElement.focus();
    }
    else      {
      for (var n=1; n<7; n++)     { // loop through characters 2-6
        theChar = theColor.substring(n,n+1);
        if (isNaN(theChar))     {
          theChar = theChar.toUpperCase();
          if (theChar < "A" || theChar > "F")     {  // check for a-f if not a number.
            badcolor = true;
            alert(BADCOLOR);
            theElement.focus();
            break;
          }
        }
      }
      if (!badcolor)     {
        thePick.value = theColor;  // set the color picker.
      }
    }
  }
}

function switchPreview()
{
  var retVal = "";
  var theImg = WA_getConfigurationPath("/shared/Google/FreeSearch/Images/logo_40wht.gif");
  var theSiteSearch = "";
  var theGoogleSearch = GOOGLESEARCH;
  var charSet = dw.getDocumentDOM().getCharSet();
  charSet = charSet.toLowerCase();

  // if japanese or korean, then modify Google Search string accordingly
  if (dreamweaver.appVersion.indexOf('ja') != -1)
  {
    if (charSet != "shift_jis" && charSet != "euc-jp" && charSet != "utf-8")
    {
      theGoogleSearch = GOOGLESEARCHENGLISH;
    }
  }
  else if (dreamweaver.appVersion.indexOf('ko') != -1)
  {
    if (charSet != "euc-kr" && charSet != "utf-8")
    {
      theGoogleSearch = GOOGLESEARCHENGLISH;
    }
  }
  
  if (document.DomainWP.document.safeSearchCHK.checked)
  {
    theImg = WA_getConfigurationPath("/shared/Google/FreeSearch/Images/google_safe.gif");
  }
  if (document.DomainWP.document.searchRDO[1].checked)
  {
    theSiteSearch = '<br><input type="radio" name="sitesearch" value=""> '+THREEW+' <input type="radio" name="sitesearch" checked>' + document.DomainWP.document.domains.value + '<br>';
    document.DomainWP.document.domains.disabled = "enabled";
  }
  else
  {
    document.DomainWP.document.domains.disabled = "disabled";
  }

  retVal = '  <table width="100%" bgcolor="#FFFFFF" cellpadding="0" cellspacing="5" border="0">\n' +
           '    <tr valign="middle">\n' +
           '      <td nowrap valign="middle">\n' +
           '        <img src="' +  theImg + '" border="0" align="absmiddle" alt="Google search">' +
           '      </td>\n' + 
           '      <td nowrap valign="middle">\n' +           
           '        <input type="text" name="q" style="width:100px" value="">\n' +
           '        <input type="submit" name="btnG" value="'+theGoogleSearch+'">\n' +
                    theSiteSearch +
           '      </td>\n' +
           '    </tr>\n' +
           '  </table>\n';

   document.DomainWP.document.previewSpan.innerHTML = retVal;
}


/*
This function encodes translated orig tags.
*/
function WA_translatorEncode(theStr)      {

  theStr = theStr.replace(/</g, "%3C");
  theStr = theStr.replace(/>/g, "%3E");
  theStr = theStr.replace(/ /g, "%20");
  theStr = theStr.replace(/\"/g, "%22");//"
  theStr = theStr.replace(/\'/g, "%27");//'

  return theStr;
}
