//
//  
//  _WMHook.js 
//  MemoWeb 3 script
//  Copyright Goto Software 2000
//	
//  
//

// herited function from MemoWeb 98
function MemoWebLU(url) 
{
 document.cookie = 'MW3_'+'url=' + url +';PATH=/';
}


function GetCookieArg(arg)
{
  var s = document.cookie;
  arg = 'MW3_'+arg + "=";
  var result;
  var len = arg.length;
  result = "";
  if (s.length > 0)
  {
    pos = s.indexOf(arg);
    if (pos >= 0) 
    {
      end = s.indexOf(";", pos+len);
      if (end >= 0) 
      {
        result = unespace(s.substring(pos+len,end));
      } 
      else
      {
        result = s.substring(pos+len,s.length);
      }
    }
  }
  return result;
}


// Remove bad chars from CGI request
function MWRemoveInvalidChars(value)
{
  return value;
}

//
// Replace the submit method par this one
// returne false if treated
//
function MWFormHook(form,path)
{
  var url,localfile,result,target;
  url = MWGetSubmitUrl(form);
  result = (MWFormHookUrl != null);
  if(result)
  {
     localfile = MWFormHookUrl(url,path);
     target    = form.target;
     
     if (target == '_parent') 
     {
       parent.location = localfile;
       return false; 	
     }
       
     if (target == '_blank')
     {
       window.open(localfile,'','');
       return false; 	
     }
       
     if (target == '_self')
     {
       self.location = localfile; 
       return false; 	
     }
       
     if((target == '_top') || (!target.length))
     {	 
       top.location = localfile;
       return false;
     } 
      
     parent.frames[target].location = localfile;
     return false;
  }
  
  if(!result)
    form.Submit();
     
  return result;
}


// Method Hook
function MWHookMethod(TheObject,HookId)
{
  switch(HookId)
  {
    // replace document.submit()
    case 11 :
    {	 
      MWFormHook(TheObject);
    }
  }
}

function MWQueryAdd(query,element)
{
 if (query.length > 0) query = query + '&';   
 query = query + element.name + '=' + MWRemoveInvalidChars( element.value);	 
 return query;
}


// Create the Get URL from form object
function MWGetSubmitUrl(form)
{
  var query = '';
  var result = '';
  var n,e,i;
  for (n = 0; n < form.elements.length; n++)
  {
    e = form.elements[n];   
    if((e.type == 'radio') || (e.type == 'checkbox'))
    {
      if (e.checked) 
      	query = MWQueryAdd(query,e);
    } else
    if ((e.type == 'select-one') || (e.type == 'select-multiple')) 
    {  
      for (i=0; i < e.options.length; i++)
      if(e.options[i].selected)
      {
        if (!query.length) query ='?'; else query = query + '&';   
        query = query + e.name + '=' + MWRemoveInvalidChars(e.options[i].value);
      }    
    } 
    else  
    {
      if( (e.type != 'button') && (e.type!='reset') && (e.type!='submit') )
      {
      	query = MWQueryAdd(query,e);
 
      }
    }
  }
  

  result = form.action;
  if (result.indexOf('?')  == -1) 
     result = result + '?' + query;
  else
     result = result + '&' + query;
  
  return result;
}



// make an relative URL
function MWRelatifUrl(path,file)
{
  var i,list,list2,d,result;

  if(!path.length)
     return file;

  
  // remove end  '/' 
  if(path.charAt(path.length-1) == '/') 
    path = path.substr(0,path.length-1);

  
  list  = path.split('/');
  list2 = file.split('/');
  
  for(i = 0; i < list2.length ; i ++)
  {
     d = list2[i];
     if((d == '..') && (list.length > 0))
       list.pop();
     else
       list[list.length] = d; 
  }

  result = list.join('/');
  return result;
}





   