// <script language="JavaScript" type="text/javascript">
var swTipFriendAjax = {
  version : 'v1.0',
  copyright : 'Steinhaug AS',
  AjaxTipFriendLauchButtonInit : function () {
    el = document.getElementById('AjaxTipFriendLauchButton');
    if(el){
      el.href = 'javascript:swTipFriendAjax.init();';
      el.removeAttribute('target');
    }
  },
  ajaxTipFriendFormInit : function () {
    var el = document.getElementById('tipfriend-form');
    if(el){
      addEvent(el, 'submit', swTipFriendAjax.ajaxTipFriendValidate, false);
      el.onsubmit = function() { return false; }
    }
  },
  init : function () {
    this.applyStyleString(document.getElementById('tipfriend-greeting'),'display: none;')
    this.applyStyleString(document.getElementById('tipfriend-form-container'),'display: block;')

    this.applyStyleString(document.getElementById('tipfriend-comfirm'),'display: none;')
    this.applyStyleString(document.getElementById('tipfriend-loading'),'display: none;')

//    document.getElementById('tipsvennimage').src = '/gfx/tips-en-venn-on.gif';
  },
  fallback : function () {
    // We have a problem, so we post it manually
    var fromname = document.getElementById('tv-sender').value;
    var fromemail = document.getElementById('tv-senderepost').value;
    var toname = document.getElementById('tv-mottaker').value;
    var toemail = document.getElementById('tv-mottakerepost').value;
    var message = document.getElementById('tv-kommentar').value;
    var query = 'fromname=' + fromname + '&fromemail=' + fromemail + '&toname=' + toname + '&toemail=' + toemail + '&message=' + message;
    location.replace('lib.tipsenvenn.php?' + query);
  },

  ajaxTipFriendValidate : function () {
    swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-form-container'),'display: none;')
    swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-loading'),'display: block;')
    setTimeout("swTipFriendAjax.ajaxTipFriendValidate2()",500);  // IE hangs while waiting for data, so we delay the ajax so that
                                                                 // progressbar is activated.
  },
  ajaxTipFriendValidate2 : function () {
    var currentTime = new Date();
    var fromname = document.getElementById('tv-sender').value;
    var fromemail = document.getElementById('tv-senderepost').value;
    var toname = document.getElementById('tv-mottaker').value;
    var toemail = document.getElementById('tv-mottakerepost').value;
    var message = document.getElementById('tv-kommentar').value;

    var query = 'fromname=' + fromname + '&fromemail=' + fromemail + '&toname=' + toname + '&toemail=' + toemail + '&message=' + message;
    // Activate failsafe error message incase we don't get response
    sw_AjaxTipFriendRequest = setTimeout("swTipFriendAjax.fallback()",12000);
    document.getElementById('tipfriend-ajax-reponse').innerHTML = '';
    var req = new XMLHttpRequest();
    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
          clearTimeout(sw_AjaxTipFriendRequest);
          // We create the HTML, so we can access it
          document.getElementById('tipfriend-ajax-reponse').innerHTML = '&nbps;' + req.responseText;
          var responsetext = document.getElementById('tipfriend-ajax-reponse');

          var isMSIE = navigator.appName == "Microsoft Internet Explorer";
          if(!isMSIE){
            try{
              var RTstatus =  responsetext.getElementsByTagName('status')[0].firstChild.data;
              var RTmessage =  responsetext.getElementsByTagName('message')[0].firstChild.data;
            }catch(error){
              var RTstatus = 'unknown error';
              var RTmessage = 'En uventet feil har oppstått, sjekk konfigurasjon av Tips en Venn script';
            }
          } else {
            try{
              var RTstatus =  responsetext.getElementsByTagName('status')[0].nextSibling.nodeValue;
              var RTmessage =  responsetext.getElementsByTagName('message')[0].nextSibling.nodeValue;
            }catch(error){
              var RTstatus = 'unknown error';
              var RTmessage = 'En uventet feil har oppstått, sjekk konfigurasjon av Tips en Venn script';
            }
          }
// kim@steinhaug.com
          if(RTstatus=='ok'){
            swTipFriendAjax.ajaxTipFriendResetForm();
            document.getElementById('tipfriend-comfirm').innerHTML = RTmessage;
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-loading'),'display: none;')
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-comfirm'),'display: block;')
          } else if(RTstatus=='validation-error'){
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-loading'),'display: none;')
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-comfirm'),'display: none;')
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-form-container'),'display: block;')
            alert('Feil med mottatte data : ' + "\n" + RTmessage);
          } else {
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-loading'),'display: none;')
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-comfirm'),'display: none;')
            swTipFriendAjax.applyStyleString(document.getElementById('tipfriend-form-container'),'display: block;')
            alert(RTmessage);
          }
        };
      };
      req.open('GET', 'lib.tipsenvenn.ajax.php' + '?' + query + '&' + currentTime.getDay() + currentTime.getHours() + currentTime.getMinutes() + currentTime.getSeconds());
      req.send(null);
    }
    return false;
  },
  ajaxTipFriendResetForm : function (){
    var fieldArea = document.getElementById('tipfriend-form');
    var inputs = fieldArea.getElementsByTagName('input');
    var inputsLen = inputs.length;
    var tAreas = fieldArea.getElementsByTagName('textarea');
    var tAreasLen = tAreas.length;
    for ( i=0;i<inputsLen;i++ ) {
      if ( inputs[i].getAttribute('type') == 'text' ) {
        inputs[i].value = '';
      }
    }
    for ( j=0;j<tAreasLen;j++ ) {
      tAreas[j].value = '';
    }
  },
  applyStyleString : function (obj,str) {
    if(document.all && !window.opera) {
      obj.style.setAttribute("cssText",str);
    } else {
      obj.setAttribute("style",str);
    }
  }
};
addEvent(window, 'load',swTipFriendAjax.ajaxTipFriendFormInit, false);
addEvent(window, 'load',swTipFriendAjax.AjaxTipFriendLauchButtonInit, false);

