﻿Type.registerNamespace("GIRO.Hastinfo.Web");

GIRO.Hastinfo.Web.LocationPersistor = function(element) 
{
GIRO.Hastinfo.Web.LocationPersistor.initializeBase(this, [element]);

this._formId = null;
this._persistOnPostBack = null;
this._unload$delegate = null;
}

GIRO.Hastinfo.Web.LocationPersistor.prototype = 
{
initialize : function() 
   {
   GIRO.Hastinfo.Web.LocationPersistor.callBaseMethod(this, "initialize");
   
   if (this._unload$delegate == null)
      this._unload$delegate = Function.createDelegate(this, this.onUnload)
   Sys.Application.add_unload(this._unload$delegate);
   }, 
     
dispose : function() 
   {
   GIRO.Hastinfo.Web.LocationPersistor.callBaseMethod(this, "dispose");
   
   if (this._unload$delegate != null)
      {
      Sys.Application.remove_unload(this._unload$delegate);
      delete this._unload$delegate;
      }         
   },
   
get_FormId : function() { return (this._formId); },
set_FormId : function(value) { this._formId = value; },
   
get_PersistOnPostBack : function() { return (this._persistOnPostBack); },
set_PersistOnPostBack : function(value) { this._persistOnPostBack = value; },   
   
_GetBody : function(form)
   {
   var formBody = new Sys.StringBuilder();
   var count = form.elements.length;
   
   for (var i = 0; i < count; i++) 
      {
      var element = form.elements[i];
      var name = element.name;
      if (typeof(name) === "undefined" || (name === null) || (name.length === 0))
         continue;

      var tagName = element.tagName;
      if (tagName === 'INPUT') 
         {
         var type = element.type;
         if ((type === 'text') || (type === 'password') || (type === 'hidden') || (((type === 'checkbox') || (type === 'radio')) && element.checked)) 
            {
            formBody.append(name);
            formBody.append('=');
            formBody.append(encodeURIComponent(element.value));
            formBody.append('&');
            }
         }
      else if (tagName === 'SELECT') 
         {
         var optionCount = element.options.length;
         for (var j = 0; j < optionCount; j++) 
            {
            var option = element.options[j];
            if (option.selected) 
               {
               formBody.append(name);
               formBody.append('=');
               formBody.append(encodeURIComponent(option.value));
               formBody.append('&');
               }
            }
         }
      else  if (tagName === 'TEXTAREA') 
         {
         formBody.append(name);
         formBody.append('=');
         formBody.append(encodeURIComponent(element.value));
         formBody.append('&');
         }
      }
      
   return (formBody.toString());
   },

_GetForm : function()
   {
   var form = document.forms[this._formId];
   if (form == null)
      {
      var formByProperty = "document." + this._formId;
      form = eval(formByProperty);
      }           
      
   return (form);
   },

onUnload : function(sender, e)
   {   
   var form = this._GetForm();
   if (form == null)
      return;    
   
   if (form.__EVENTTARGET.value.length != 0 && !this._persistOnPostBack)
      return;
   
   form.__EVENTTARGET.value = this.get_id();
   form.__EVENTARGUMENT.value = "";
   
   var request = new Sys.Net.WebRequest();
   request.set_url(form.action);
   request.set_httpVerb("POST"); 
   request.get_headers()['Cache-Control'] = 'no-cache';
   var body = this._GetBody(form);      
   request.set_body(body);     
   request.invoke();  
   }
}
   
GIRO.Hastinfo.Web.LocationPersistor.registerClass("GIRO.Hastinfo.Web.LocationPersistor", Sys.UI.Control);


if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();