var TP = {
  
    Element: {
  
        collide: function (ev, el) {
      var ev = new Event(ev);
      var el = new Element(el);
      return (
        (ev.page.x > el.getCoordinates().left) && (ev.page.x < el.getCoordinates().right)
        &&
        (ev.page.y > el.getCoordinates().top) && (ev.page.y < el.getCoordinates().bottom)
      );
    },
    
     
    Data: {},
    
        hideShow: function (elh, els) {
    
            if (typeof(elh) == 'string') elh = [elh]; 
      
            if (typeof(els) == 'string') els = [els]; 

      if (elh && (elh.length>0)) {
        var el_id = elh.pop();
        var el = $(el_id);
        if (!el) throw('cannot find id:'+el_id); 
        var hasMore = (elh.length > 0);
        var fx = new Fx.Morph(el);
                fx.start({'opacity':0}).chain(function() {
        
                    el.addClass('hide');
          
                    if (els && (!hasMore)) TP.Element.hideShow(null, els);
        });
                if (hasMore) TP.Element.hideShow(elh, els);
        
      } else if (els && (els.length>0)) {
      
                for (var i = 0; i < els.length; i++) {
          var el = $(els[i]);
          if (!el) throw('cannot find id:'+els[i]); 
          
                    el.removeClass('hide');
          var fx = new Fx.Morph(el);
          fx.start({'opacity':1});
        }
      }
    },
    
        setButtonText: function (el, txt) {
      el = new Element(el);
      el.getElement('td.label a.content').set('text', txt);
    }
  },

    f: function (key, func) {
    if (!func) {
      func = key;
      key = false;
    }
    return function() {
      try {
        return func.apply(this, arguments);
      } catch (e) {
        TP.f._last_exception = e;

        if (TP.f._last_key) {
          if (key) TP.f._last_key += ":" + key;
        } else {
          TP.f._last_key = key;
        }

                         
                        if (navigator.userAgent.toLowerCase().indexOf('msie') > 0) throw e;

        var mess = e.message;
        if (e.name) mess = e.name+":"+mess;

        var line = $pick(e.lineNumber /*firefox*/, e.line /*safari*/);
        var src = $pick(e.fileName /*firefox*/, e.sourceURL /*safari*/);

                window.onerror(mess, src, line);
        
              }
    };
  },
  
    Form: {
    
        clear: function (el) {
      this.clearInputs(el);
      this.clearErrors(el);
    },
    
        clearErrors: function (el) {
      el.getElements('label.field_error').each(function (error) {
        error.destroy();
      });
    },
    
        clearInputs: function (el) {
      el.getElements('input, textarea').each(function (input) {
        if (input.getAttribute('type') != 'hidden' && input.getAttribute('type') != 'submit') input.value = '';
      });
    },
    
        disable: function (el) {
      el.getElements('button, input, select, textarea').each(function (input) {
        input.setAttribute('disabled', 'disabled');
        input.addClass('disabled');
      });
    },
    
        displayErrors: function (el, errors) {
      for (var input_id in errors) {
        var input = $(input_id);
        if (input) {
          var error_label = new Element('label', { 'class': 'field_error', 'for': input_id });
          var error_text = new Element('span', { 'class': 'error_text' });
          
          error_text.set('text', errors[input_id]);
          error_text.injectInside(error_label);
          error_label.injectAfter(input);
        }
      }
    },
    
        enable: function (el) {
      el.getElements('button, input, select, textarea').each(function (input) {
        input.removeAttribute('disabled');
        input.removeClass('disabled');
      });
    },
    
        lock: function (el) {
      el.getElements('button, input, select, textarea').each(function (input) {
        input.setAttribute('readonly', 'readonly');
        input.addClass('disabled');
      });
    },
    
        submit: function (el, params) {
      if (typeof(el) == "string") el = $(el);
      while(el && (el.nodeName != "FORM")) el=el.parentNode;
      if ((!el) && (1 == document.forms.length)) {         el = document.forms[0];
      }
      if (!el) {
        throw('Error: Could not find form');
        return;
      }
      var form = el;
      for(var key in params) {
        var value = params[key];
        if (form.elements[key]) {
          form.elements[key].value = value;
        } else {
          var div = document.createElement('div');
          div.innerHTML = "<input type='hidden' name='"+key+"' value='"+value+"'/>";
          form.appendChild(div);
        }
      }
      
      form.submit();
      return false;  
    }
  },
  
    Format: {
    
        currency: function (amt) {
			amt -= 0;
			amt = (Math.round(amt.toFloat()*100))/100;
			
			return (amt == Math.floor(amt)) ? amt + '.00' 
				: ( (amt*10 == Math.floor(amt*10))
				? amt + '0' : amt);
		}
  },
  
    Request: {
  
        post: function (url, params) {
      var form = new Element('form', {
        'action': url,
        'method': 'post',
        'styles': {
          'display': 'none'
        }
      }).inject(document.body);
      
      TP.Form.submit(form, params);
    }
  },
  
    Table: {
    
        zebraStripe: function (el) {
      if (!el || (el.tagName != 'TABLE' && el.tagName != 'TBODY')) return;
      
      if (el.tagName == 'TABLE' && el.getElement('tbody')) el = el.getElement('tbody');
      
      var row_length = el.rows.length;
      var count = 0;
      
      for (var i = 0; i < row_length; i++) {
        var row = new Element(el.rows[i]);

        if (row.getStyle('display') != 'none' && row.getStyle('visibility') != 'hidden')
          count++;
        
        if ((count % 2) == 1) {
          row.removeClass('alt');
        } else {
          row.addClass('alt'); 
        }
      }
    }
  },
  
    logError: function (err_code, err_msg) {
    var image = new Image();
    image.src = 'http://static.trialpay.com/error/?ec=' + err_code + ((err_msg && encodeURIComponent) ? '&d=' + encodeURIComponent(err_msg) : '');
  }
}

window.onerror = function (msg, url, ln) {
  var code = 300;
  
  if (TP.f._last_exception) {
    code = 400;
    msg = ":"+msg;
    if (TP.f._last_key) {
      msg = TP.f._last_key+msg;
    } 
    delete TP.f._last_exception;
    delete TP.f._last_key;
  }

  TP.logError(code, msg + ":" + url + ":" + ln);
  
    return true;
  }
