// class Jsquery for support - server side scripting using iframe
function JsqueryClass()
{
   function init(doc)
   {
      this.doc=doc;
      this.paramString=this.createObjWithParamString();
   }
   this.init=init;

   this.jsqueryDebug=0

   /////////////////////////////////////////////////////////////////////////
   // iframe 'server side'

   // no need to call that rtn explicitely
   // it is already called bellow
   function createObjWithParamString()
   {
      var obj=new Object;

       qs=this.doc.URL.substring(this.doc.URL.indexOf('?')+1,this.doc.URL.length);
       queries=qs.split(/\&/);
       for(i=0;i<queries.length;i++)
       { query=queries[i].split(/\=/);
         obj[query[0]]=unescape(query[1]);
       }
       return obj;
   }
   this.createObjWithParamString=createObjWithParamString

   /*this.paramString=new obj_paramString();*/

   function addServerVar(k,v)
   {  if(this.jsqueryDebug) alert("adding server variable "+k+" value :"+v)
      this.paramString[k]=v;
   }
   this.addServerVar=addServerVar

   // not used
   var stringOfParams
   function toString_params()
   {
      stringOfParams=paramString.join(';')
   }
   this.toString_params=toString_params

   function returnToParent()
   {
      if (window.parent==null)
      {
         if(this.jsqueryDebug)
            alert("no parent window for "+this.doc.URL)
      }
      else
      {
         if(this.jsqueryDebug)
            alert('return to parent')
         // store result returned.
         window.parent.res = this.paramString
         // calling parent window method by : window.parent.'toDoNext' function (window.parent.res)
         eval('window.parent.'+this.paramString.toDoNext+'(window.parent.res)')
      }
   }
   this.returnToParent=returnToParent

   /////////////////////////////////////////////////////////////////////////
   // client side


   function pushKV(arr,k,v)
   {
      arr.push([k,v]);
   }
   
   function buildQueryString(theParameters)
   {
      if(this.jsqueryDebug) alert("build query string "+theParameters)
      var params=new Array();
      var paramString="";
      if (theParameters==null)
      {
      }
      else
      if (typeof theParameters == 'string')
      {
         paramString=theParameters;
      }
      else
      if (typeof theParameters == 'object')
      {
         if ((theParameters.nodeType==1) && (theParameters.nodeName=='FORM'))
         {
            theForm=theParameters;
            for (e=0;e<theForm.elements.length;e++)
            {
               if ((e.nodeType==1) && (e.nodeName=='SELECT'))
               {
                  for(opt in e.options)
                  {
                     if ((e.name!='')&&(opt.value!=''))
                     {
                        pushKV(params, e.name, opt.value);
                     }
                  }
               }
               else // should be normal input
               if (theForm.elements[e].name!='')
               {
                  pushKV(params, theForm.elements[e].name,theForm.elements[e].value);
               }
            }
         }// form
         else
         {
            var key;
            for (key in theParameters)
            {
               var v=theParameters[key];
               if (typeof key == 'string')
               {
                  pushKV(params, key, v);
               }
               else
               {
                  pushKV(params, key, '"'+String(v)+'"');
               }
            }
         }// object reflection
      } // of object
      else
      {
         if(this.jsqueryDebug)
            alert('no form '+theFormName)
      }

      var qs = '?jsquery'
      var key;
      for (key in params)
      {
         obj=params[key];
         k=obj[0];
         v=obj[1];
         qs+='&'+k+'='+escape(v);
      }
      // string params speciel case
      if (paramString!='')
      {
         qs+='&'+paramString;
      }
      return qs
   }
   this.buildQueryString=buildQueryString

   function oldbuildQueryString(theFormName)
   {
      theForm = this.doc.forms[theFormName];
      var qs = ''
      if (theForm==null)
      {
         if(this.jsqueryDebug)
            alert('no form '+theFormName)
      }
      else
      for (e=0;e<theForm.elements.length;e++) {
         if (theForm.elements[e].name!='') {
            qs+=(qs=='')?'?':'&'
            qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
            }
         }

      return qs
   }
   this.oldbuildQueryString=oldbuildQueryString

   function callToServerThenDo(action, theFormName, toDoNext)
   {
      // add form parameters
      var qs
      qs=this.buildQueryString(theFormName)
      /*if(qs=='')
      {// dummy so code depending won't break
         qs='?dummy'
      }*/

      var URL = action +qs+'&'+'toDoNext='+toDoNext;
      if(this.jsqueryDebug)
         alert('calling '+URL)

      this.callToServerSub(URL)
   }
   this.callToServerThenDo=callToServerThenDo

   function callToServer(action, theFormName) {
      var URL = action + buildQueryString(theFormName);
   }
   this.callToServer=callToServer

   var IFrameObj=null; // our IFrame object
   function callToServerSub(URL) {
      if (!this.doc.createElement) {return true};
      var IFrameDoc;
      if (!IFrameObj && this.doc.createElement) {
         // create the IFrame and assign a reference to the
         // object to our global variable IFrameObj.
         // this will only happen the first time
         // callToServer() is called
         try {
            var tempIFrame=this.doc.createElement('iframe');
            // The following line is required so that if we call this function from an https context, the IE6 browser
            // won't warn that "the page contains both secure and unsecure items" when the src-less iframe is appended
            // to the document.
            tempIFrame.src = URL;
            tempIFrame.setAttribute('id','RSIFrame');
            tempIFrame.style.border='0px';
            tempIFrame.style.width='0px';
            tempIFrame.style.height='0px';
            IFrameObj = this.doc.body.appendChild(tempIFrame);

            if (this.doc.getElementById)
            {
               IFrameObj = this.doc.getElementById('RSIFrame');
            }
            else
            if (this.doc.frames) {
               // this is for IE5 Mac, because it will only
               // allow access to the this.doc object
               // of the IFrame if we access it through
               // the this.doc.frames array
               IFrameObj = this.doc.frames['RSIFrame'];
            }
         } catch(exception) {
            // This is for IE5 PC, which does not allow dynamic creation
            // and manipulation of an iframe object. Instead, we'll fake
            // it up by creating our own objects.
            iframeHTML='<iframe id="RSIFrame" style="';
            iframeHTML+='border:0px;';
            iframeHTML+='width:0px;';
            iframeHTML+='height:0px;';
            iframeHTML+='"><\/iframe>';
            this.doc.body.innerHTML+=iframeHTML;
            IFrameObj = new Object();
            IFrameObj.document = new Object();
            IFrameObj.document.location = new Object();
            IFrameObj.document.location.iframe = this.doc.getElementById('RSIFrame');
            IFrameObj.document.location.replace = function(location) {
               this.iframe.src = location;
            }
         }
      }

      if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
         // we have to give NS6 a fraction of a second
         // to recognize the new IFrame
         setTimeout('callToServerSub("'+URL+'")',10);
         return false;
      }

      if (IFrameObj.contentDocument) {
         // For NS6
         IFrameDoc = IFrameObj.contentDocument;
      } else if (IFrameObj.contentWindow) {
         // For IE5.5 and IE6
         IFrameDoc = IFrameObj.contentWindow.document;
      } else if (IFrameObj.document) {
         // For IE5
         IFrameDoc = IFrameObj.document;
      } else {
         return true;
      }

      IFrameDoc.location.replace(URL);
      return false;
   }
   this.callToServerSub=callToServerSub

}
jsqueryClass = new JsqueryClass()

function Jsquery(doc)
{
   this.init(doc)
}
Jsquery.prototype = jsqueryClass;

// default instance for use.
jsquery = new Jsquery(document);
