var updateInProgress = false;
var rootDivs         = null;
var divIndex         = -1;

var ajaxReq          = null;
var ajaxDoc          = null;
var aForumForm       = null;
var aForumMessageUrl = null;
var aForumPostMessageUrl = null;
var aCreateUrl       = null;
var aopt    = null
var forumSubmitParams = null;
var IE;

var what  = null;
var aUrl             = null;
var aAjaxDiv         = null;
var aPostDiv         = null;



function doDelay(Amount) {
  d = new Date(); //today's date
  while (1) {
    mill = new Date(); // Date Now
    diff = mill-d; //difference in milliseconds
    if( diff > Amount ) {
      break;
    }
  }
}

function newRequest() {
  if (IE == true)  {
    return new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    return new XMLHttpRequest();
  }
} // newRequest()

function processAjax() {
  if ((ajaxReq.readyState == 4) && (ajaxReq.status == 200)) {
    ajaxDoc           = ajaxReq.responseText;
    if (what == 1) {
      aForumMessageUrl  = ajaxDoc;
      checkForm();
    }
    else{
      aAjaxDiv.innerHTML = ajaxDoc;
    }
  }
} // processAjax()

function processAjaxForumSubmit() {
  if ((ajaxReq.readyState == 4) && ((ajaxReq.status == 200) || (ajaxReq.status == 404))) {
    ajaxDoc           = ajaxReq.responseText;
    document.getElementById(aPostDiv).innerHTML =  ajaxDoc;
    what = null;
    aopt = null;

    doDelay(2000);
    lStr = window.location.href;
    lStr = lStr.replace(/.Nocache../i, "");

    if (lStr.indexOf("frompost=1") == -1)
    {

      lStr = window.location.href + '&frompost=1';
      if(aopt == 1)
        lStr = window.location.href + '&frompost=1&Nocache=1';

      //window.location = lStr;
    }
    else{
      //window.location = lStr;
      //window.location.reload();
    }
  }

} // processAjaxPollResult()

function processForumSubmit() {
  try {
    ajaxReq = newRequest();
    ajaxReq.onreadystatechange = processAjaxForumSubmit;
    ajaxReq.open("POST", aForumPostMessageUrl, true);
    ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxReq.setRequestHeader("Content-length", forumSubmitParams.length);
    ajaxReq.setRequestHeader("Connection", "close");
    ajaxReq.send(forumSubmitParams);
  } catch(e) {
    alert(e);
  }

}  // processPollMiniResult()

function checkForm() {
  formErrors      = true;
  aForm           = document.getElementById(aForumForm);
  var getstr = '';
  for (i=0; i<aForm.childNodes.length; i++) {
    if (aForm.childNodes[i].tagName == "INPUT"){
      if (aForm.childNodes[i].type == "text") {
        getstr += aForm.childNodes[i].name + "=" + escape(aForm.childNodes[i].value) + "&";
      }
      if (aForm.childNodes[i].type == "checkbox") {
        if (aForm.childNodes[i].checked) {
          getstr += aForm.childNodes[i].name + "=" + aForm.childNodes[i].value + "&";
        } else {
          getstr += aForm.childNodes[i].name + "=&";
        }
      }
      if (aForm.childNodes[i].type == "radio") {
        if (aForm.childNodes[i].checked) {
          getstr += aForm.childNodes[i].name + "=" + aForm.childNodes[i].value + "&";
        }
      }
    }
    if (aForm.childNodes[i].tagName == "TEXTAREA"){
      getstr += aForm.childNodes[i].name + "=" + escape(aForm.childNodes[i].value) + "&";
    }
    if (aForm.childNodes[i].tagName == "SELECT") {
      var sel = aForm.childNodes[i];
      getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
    }
    if (aForm.childNodes[i].type == "hidden") {
      getstr += aForm.childNodes[i].name + "=" + aForm.childNodes[i].value + "&";
    }
  }
  getstr = getstr + "Title=" + document.location.href + "&";
  forumSubmitParams = getstr;
  ajaxReq = null;
  processForumSubmit();
}


function processForum() {
  try {
    ajaxReq                    = newRequest();
    ajaxReq.onreadystatechange = processAjax;
    ajaxReq.open("GET", aCreateUrl, true);
    ajaxReq.send(null);
  } catch(e) {
    alert(e);
  }

} // processPollMini()
function ProcessRequest(aDivTag, aurl ) {
  try {
    aAjaxDiv = document.getElementById(aDivTag);
    aAjaxDiv.innerHTML  = '<center> <img align="center" src="/images/progress.gif" border="0" alt="Loading, please wait..." /></center> ';
    ajaxReq                    = newRequest();
    ajaxReq.onreadystatechange = processAjax;
    ajaxReq.open("GET", aurl, true);
    ajaxReq.send(null);
  } catch(e) {
    alert(e);
  }
}
function storeCaret(textEl) {
  if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

if ((document.all) && (navigator.userAgent.indexOf('Opera')== -1)) {
  IE = true;
} else {
  IE = false;
}

function doSubmitMessage(aFormId,aUrl,aPostUrl,anew,adiv){
  aForumForm = aFormId;
  aCreateUrl = aUrl+"&omniture=0";
  aForumMessageUrl = '';
  aForumPostMessageUrl = aPostUrl;
  aopt = anew;
  aPostDiv = adiv;
  document.getElementById('progress').style.visibility = "visible"

  p_Author = document.getElementById('m_Author').value;
  p_Email = document.getElementById('m_Email').value;
  p_Title = document.getElementById('m_Title').value;
  p_Comment = document.getElementById('m_Body').value;
  p_Captcha = document.getElementById('m_Captcha').value;
  p_CaptchaID = document.getElementById('m_CaptchaID').value;


  document.getElementById('Author').value =  p_Author;
  document.getElementById('Email').value =  p_Email;
  document.getElementById('Title').value =  p_Title;
  document.getElementById('Body').value =  p_Comment;
  document.getElementById('Captcha').value =  p_Captcha;
  document.getElementById('CaptchaID').value =  p_CaptchaID;

  what = 1;
  processForum();

}

function hasClass(ele,cls) {
  return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
  if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
  if (hasClass(ele,cls)) {
    var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
    ele.className = "";
  }
}

function _toggleClass(id, cls)
{
  var ele = document.getElementById(id);

  if(ele.toggleClass){

    if(hasClass(ele, cls))
    {
      console.log("b");
      removeClass(ele,cls);
    }
    else
    {
      addClass(ele,cls);
    }

  }
  else
  {
    ele.toggleClass("active")
  }
}

jQuery.noConflict();

(function ($) {
  $(document).ready(function () {
    $.openxtag('init', {
      delivery: 'http://publish.mogid.no/Openads/www/delivery',
      forceAsync: false // async true is not compatible with block(campaign)
    });
    $('.banners').each(function() {
      var c = $(this).attr("count");
      var zone = $(this).attr("zone");
      for(var i = 0; i < c; i++)
      {
        $(this).openxtag('zone', zone, {
          block: true
        });
      }
    });

    $('.banner').each(function() {
      $(this).openxtag('zone', $(this).attr("zone"), {
        block: true
      });
    });

  });

})(jQuery);

