//
//             Copyright (c) 2002, 2003 Smartlink Corp.
//                       All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////
//
// Speller methods
//

var SPELL_PATH    = "/speller1/";
var CROSS_DOMAIN  = false; //true; //false;
var CLIENT_ID     = "IDAWPXIB"; // "CID80048798";
var MAX_TEXT_LEN  = 1000;
var MIME_ENCODING = ""; // "base64";
var SPELL_CLIENT  = "spellclient/spellclient.htm";
var BROWSER_IE    = (window.showModalDialog ? true : false);
var BROWSER_NS4   = !(document.getElementById);
var ASP_EXT       = ".asp";

var spellSession  = null;
var userDicDlg = null;

function invokeSpeller (ctrls, query, path) {
//alert("ok");
   if (spellSession)
      closeSpeller ();
   if (!query)
      query = "";
   // Find controls
   if ((ctrls = findCtrls (ctrls)) == null)
      return;
   var text = new Array (), names = new Array ();
   for (var i = 0; i < ctrls.length; i++) {
      text  [i] = ctrls [i].value;
	  names [i] = ctrls [i].name;
/*
	  if( BROWSER_IE) {
		 var wname = ctrls [i].document.parentWindow.name;
		  if(wname != window.name)
	  			names [i] = wname + "/" + ctrls [i].name;
		}
	   alert(names[i] + " , "+text  [i] );	
*/
   }

   // Build query string
   if (!CROSS_DOMAIN)
      query = setQueryParam (query, "ctrl", escape (names.join (" ")));
   if (CLIENT_ID)
      query = setQueryParam (query, "clientID", CLIENT_ID);
   
   // Build href
   var href = (path ? path : "");
   if (href && href.charAt (href.length - 1) != "/")
      href += "/";
   if (CROSS_DOMAIN)
      href += SPELL_CLIENT;
   else
      href += SPELL_PATH + "spelldlg" + ASP_EXT;
   href += "?" + query;

   // Create new SpellSession
   spellSession = { ctrls: ctrls, text: text };

   if (BROWSER_IE) {
      var args = { opener: this };
      var features = "dialogWidth:436px; dialogHeight:291px;scroll:no;help:no;status:no;";
      var result = CROSS_DOMAIN ? window.showModalDialog (href, args, features) : showDialog (href, args, features);
      if (typeof (result) == "object")
         applySpellResult (result);
      spellSession = null;
   }
   else {
      var features = "width=426,height=261,toolbar=no,status=no,menubar=no,directories=no,resizable=no,modal=yes";
      spellSession.dialog = window.open (href, "spelldlg", features);
      if (spellSession.dialog.closed) // modal dialog
         spellSession = null;
   }
}

function closeSpeller () {
   if (spellSession) {
      if (spellSession.dialog && !spellSession.dialog.closed)
         spellSession.dialog.close ();
      spellSession = null;
   }
}

function spellOptions () {
   var href = SPELL_PATH + "options" + ASP_EXT;
   href += "?standalone=yes";
   if (CLIENT_ID)
      href += "&clientID=" + CLIENT_ID;

   if (BROWSER_IE) {
      var features = "dialogWidth:306px; dialogHeight:316px; scroll:no; status:no; resizable:no; help:no; center:yes;";
      var result = showDialog (href, null, features);
   }
   else {
      var width = 296, height = 268;
      if (BROWSER_NS4)
         height = 300;
      var features = "width=" + width + "px,height=" + height + "px,toolbar=no,status=no,menubar=no,directories=no,resizable=no,modal=yes";
      window.open (href, "options", features);
   }
}

function editUserDic () {
   if (!userDicDlg || userDicDlg.closed) {
      var params = "width=322px,height=258px,toolbar=no,status=no,menubar=no,directories=no,resizable=no";
      userDicDlg = window.open (SPELL_PATH + "userdic" + ASP_EXT, "userdic", params);
   }
   userDicDlg.focus ();
}

//////////////////////////////////////////////////////////////////////////////
//
// Encode/Decode
//

function encodeText (spellSession, maxLen, showAlert) {
   var MAX_URL_LEN = 1800; // max URL length is 2048
   var textLength = spellSession.text.join ("").length
   var len = Math.min (textLength, MAX_URL_LEN);
   if (maxLen && len > maxLen)
      len = maxLen;

   var encodedText = "", text = spellSession.text;
   while (true) {
      truncTextByWord (text, len);
      encodedText = myEncode (text);
      if ("base64" == MIME_ENCODING)
         encodedText = base64Encode (utf8Encode (encodedText));
      encodedText = escEncode (encodedText);
      if (encodedText.length <= MAX_URL_LEN || len <= 300)
         break;
      len = Math.floor ((len - 1) / 20) * 20;
   }
   if (text.join ("").length != textLength) {
      if (showAlert) {
         // window.alert ("Only " + len + " characters can be checked.");
		 window.alert ("Warning: The free service is able to spellcheck up to "+ len +" characters at one time.");
		}
		 
   }
   if ("base64" == MIME_ENCODING)
      encodedText += "&charset=utf-8&mime=base64";
   return encodedText;
}

function decodeText (query) {
   var mime = getQueryParam (query, "mime");
   var text = getQueryParam (query, "text");
   var ctrl = getQueryParam (query, "ctrl");
   text = escDecode (text);
   if ("base64" == mime)
      text = utf8Decode (base64Decode (text));
   return myDecode (text);
}

function myEncode (text) {
   var result = new Array ();
   for (var i = 0; i < text.length; i++)
      result [i] = text [i].replace (/\x1a/g, " ");
   return result.join (String.fromCharCode (0x1a));
}

function myDecode (str) {
   return str.split (String.fromCharCode (0x1a));
}

//////////////////////////////////////////////////////////////////////////////
//
// Helper functions
//

function applySpellResult (result) {
   if (spellSession) {
      var modified = false;
/*
      for (var i = 0; i < result.length; i++) {
         var ctrl = spellSession.ctrls [i];
         var text = spellSession.text [i];
         if (ctrl.value.substr (0, text.length) == text)
            ctrl.value = result [i] + ctrl.value.substr (text.length);
         else
            modified = true;
      }
*/
      if (modified)
         window.alert ("Changes can not be applied because source text " +
                       "has been modified outside of the spelling dialog.");
      if (spellSession.dialog) // modeless dialog
         spellSession = null;
   }
}

function truncStrByWord (str, length) {
   if (str.length > length) {
      str = str.substring (0, length + 1);
      str = str.replace (new RegExp ("\\S*$"), "");
      str = str.substring (0, length);
   }
   return str;
}

function truncTextByWord (array, limit) {
   for (var i = 0; i < array.length; i++) {
      var str = array [i];
      array [i] = truncStrByWord (array [i], Math.max (limit, 0));
      limit -= str.length;
   }
}

function setQueryParam (query, param, value) {
   return query + (query ? "&" : "") + param + "=" + value;
}

function getQueryParam (query, param) {
   var value = new RegExp (param + "=([^&]*)").exec (query);
   if (!value || value.length < 2)
      return "";
   return value [1];
}

function findCtrls (list) {
   var ctrls = new Array ();
   if (typeof (list) != "string") {
       ctrls [0] = list;
   }
   else {
      list = list.split (" ");
      for (var i = 0; i < list.length; i++) {
         if (list [i] == "")
            continue;
         if ((ctrls [ctrls.length] = findCtrl (list [i])) == null) {
            alert ("Can't find '" + list [i] + "' control.");
            ctrls.length = ctrls.length - 1;
         }
      }
   }
   return ctrls;
}

function findCtrl (name) {
   var forms = document.forms;
   for (var i = 0; i < forms.length; i++) {
      var ctrl = forms [i] [name];
      if (ctrl)
         return ctrl;
   }

   for(i = 0; i <doc.frames.length; i++) {
   		var frame = doc.frames[i].document;
		var fr_ctrl =  findCtrl(name, frame);
		if (fr_ctrl)
        	 return fr_ctrl;
   }
   return null;
}

function showDialog (url, args, features) {
   var href = "", ref = url, query = "", arr = url.split ("?");
   if (arr.length > 1) {
      ref = arr [0];
      query = arr [1];
   }
   var sep = ref.lastIndexOf ("/");
   if (sep >= 0) {
      href = ref.substr (0, sep + 1);
      ref = ref.substr (sep + 1);
   }
   href += "dlgframe.htm" + "?ref=" + ref + (query ? "&" : "") + query;
   return showModalDialog (href, args, features);
}
