var loadedBoxes="";

function flagAbuse(base, id) {
  text_id='abuse_text_' + id;
  if (document.layers) obj = document.layers[text_id];
  else if (document.getElementById) obj = document.getElementById(text_id);
  else if (document.all) obj = document.all[text_id];
  value=obj.value;

  url=base + '/flag_abuse_comment?id=' + id + '&value=' + value;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(abuseFlagHide);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}


function abuseFlagHide() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    html_id=root.getAttribute('html_id');
  
    if (document.layers) obj = document.layers[html_id];
    else if (document.getElementById) obj = document.getElementById(html_id);
    else if (document.all) obj = document.all[html_id];

    if (navigator.userAgent.indexOf("MSIE")>=0){
      obj.setAttribute('className','hide');
    }
    else {
      obj.setAttribute('class','hide');
    }
  return;
  }
}

function publishComment(base, id, mode) {
    url=base + '/publish_comment?id=' + id + '&mode=' + mode;
    xmlHttp=false;
    xmlHttp=GetXmlHttpObject(setAsPublished);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function setAsPublished() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    html_id=root.getAttribute('html_id');
  
    if (document.layers) obj = document.layers[html_id];
    else if (document.getElementById) obj = document.getElementById(html_id);
    else if (document.all) obj = document.all[html_id];

    if (navigator.userAgent.indexOf("MSIE")>=0){
      cl=obj.getAttribute('className');
      if (cl=='comment unpublished' || cl=='comment abuse') obj.setAttribute('className','comment');
    }
    else {
      cl=obj.getAttribute('class');
      if (cl=='comment unpublished' || cl=='comment abuse') obj.setAttribute('class','comment');
    }
  return;
  }
}

function deleteComment(base, id) {
    url=base + '/delete_comment?id=' + id;
    xmlHttp=false;
    xmlHttp=GetXmlHttpObject(setAsDeleted);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}
function setAsDeleted() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    html_id=root.getAttribute('html_id');
    sql_id=root.getAttribute('sql_id');
    new_button_text=root.getAttribute('new_button_text');
    base=root.getAttribute('base');

    button_id='delete_button_'+sql_id;
    if (document.layers) {
      obj = document.layers[html_id];
      button=document.layers[button_id];
    }
    else if (document.getElementById) {
      obj = document.getElementById(html_id);
      button=document.getElementById(button_id);
    }
    else if (document.all) {
      obj = document.all[html_id];
      button=document.all[button_id];
    }

    if (navigator.userAgent.indexOf("MSIE")>=0){
      cl=obj.getAttribute('className');
      obj.setAttribute('className','comment deleted');
    }
    else {
      cl=obj.getAttribute('class');
      obj.setAttribute('class','comment deleted');
    }
    button.setAttribute('onclick',"undeleteComment('" + base + "'," + sql_id + ");");
    button.innerHTML=new_button_text;
    return;
  }
}


function undeleteComment(base, id) {
    url=base + '/undelete_comment?id=' + id;
    xmlHttp=false;
    xmlHttp=GetXmlHttpObject(setAsUndeleted);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}
function setAsUndeleted() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    html_id=root.getAttribute('html_id');
    sql_id=root.getAttribute('sql_id');
    new_button_text=root.getAttribute('new_button_text');
    mode=root.getAttribute('mode');
    base=root.getAttribute('base');

    button_id='delete_button_'+sql_id;
    if (document.layers) {
      obj = document.layers[html_id];
      button=document.layers[button_id];
    }
    else if (document.getElementById) {
      obj = document.getElementById(html_id);
      button=document.getElementById(button_id);
    }
    else if (document.all) {
      obj = document.all[html_id];
      button=document.all[button_id];
    }

    if (navigator.userAgent.indexOf("MSIE")>=0){
      cl=obj.getAttribute('className');
      obj.setAttribute('className','comment '+mode);
    }
    else {
      cl=obj.getAttribute('class');
      obj.setAttribute('class','comment '+mode);
    }
    button.setAttribute('onclick',"deleteComment('" + base + "'," + sql_id + ");");
    button.innerHTML=new_button_text;
    return;
  }
}

function expand(base, id) {
  html_id="button_expand_"+id;
  expand_image_obj=document.getElementById(html_id);
  if (loadedBoxes.indexOf("expand_"+id)<0) {
    expand_image_obj.src="/grf/spacer.gif";
    var url=base + "/get_sub_comments?id=" + id;
    xmlHttp=false;
    xmlHttp=GetXmlHttpObject(updateExpandingThread);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
  }
  else {
    obj=document.getElementById("expand_"+id);
    if(expand_image_obj.src.indexOf("/grf/expand.gif")<0) {
      if (navigator.userAgent.indexOf("MSIE")>=0){
        obj.setAttribute('className','hide');
      }
      else {
        obj.setAttribute('class','hide');
      }
      expand_image_obj.src="/grf/expand.gif";
    }
    else {
      if (navigator.userAgent.indexOf("MSIE")>=0){
        obj.setAttribute('className','');
      }
      else {
        obj.setAttribute('class','');
      }
      expand_image_obj.src="/grf/contract.gif";
    }
  }
}
function updateExpandingThread() {
  resp="";
  html_id="";
  html_content="";
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    //resp=xmlHttp.responseText;
    var xmldoc = xmlHttp.responseXML;
    root = xmldoc.getElementsByTagName('response')[0];
    contenttype=root.getAttribute('contenttype');
    html_id=root.getAttribute('html_id');
    html_content=root.getElementsByTagName('contents')[0].firstChild.nodeValue;
    button_html_id="button_"+html_id;
    document.getElementById(html_id).innerHTML=html_content;
    document.getElementById(button_html_id).src="/grf/contract.gif"
    loadedBoxes=loadedBoxes + "," + html_id;

    return;
  } 
}
function toggleDisplayBox(idName) {
  if (document.layers) obj = document.layers[idName];
  else if (document.getElementById) obj = document.getElementById(idName);
  else if (document.all) obj = document.all[idName];

  if (navigator.userAgent.indexOf("MSIE")>=0){
    cl=obj.getAttribute('className');
    if (cl.indexOf('hide')>0) obj.setAttribute('className',cl.replace('hide',''));
    else obj.setAttribute('className', cl + ' hide');
  }
  else {
    cl=obj.getAttribute('class');
    if (cl.indexOf('hide')>0) obj.setAttribute('class',cl.replace('hide',''));
    else obj.setAttribute('class', cl + ' hide');
  }
}

function addPlus(base, c_id) {
  var url=base + "/add_plus?id=" + c_id;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(updateById);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}

function addMinus(base, c_id) {
  var url=base + "/add_minus?id=" + c_id;
  xmlHttp=false;
  xmlHttp=GetXmlHttpObject(updateById);
  xmlHttp.open("GET", url , true);
  xmlHttp.send(null);
}

function updateById() {
  resp="";
  html_id="";
  html_content="";
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    xmldoc=xmlHttp.responseXML;
    root=xmldoc.getElementsByTagName('response')[0]
    contenttype=root.getAttribute('contenttype');
    html_id=root.getAttribute('html_id');
    html_content=root.getElementsByTagName('contents')[0].firstChild.nodeValue;

    document.getElementById(html_id).innerHTML=html_content;

    return;
  } 
}

var formError='Please fill out all fields marked in red.'

function validateForm(obj, obligFields, sql_id) {
  name=obj.name.value;
  email=obj.email.value;
  title=obj.title.value;
  comment=obj.comment.innerHTML;

  if (document.layers) errObj = document.layers['error_'+sql_id];
  else if (document.getElementById) errObj = document.getElementById('error_'+sql_id);
  else if (document.all) errObj = document.all['error_'+sql_id];

  fields=obligFields.split(',');
  OKState=true;
  for (i=0;i<fields.length;i++) {
    field=obj[fields[i]];
    if (obj[fields[i]].value=='' && obj[fields[i]].innerHTML=='') {

      if (navigator.userAgent.indexOf("MSIE")>=0){
        cl=field.getAttribute('className');
        if (cl.indexOf('error')<0) field.setAttribute('className',cl + ' error');
      }
      else {
        cl=field.getAttribute('class');
        if (cl.indexOf('error')<0) field.setAttribute('class',cl + ' error');
      }
      errObj.innerHTML=formError;
      OKState=false;
    }
    else {
      if (navigator.userAgent.indexOf("MSIE")>=0){
        cl=field.getAttribute('className');
        field.setAttribute('className',cl.replace(' error',''));
      }
      else {
        cl=field.getAttribute('class');
        field.setAttribute('class',cl.replace(' error',''));
      }
    }
  }
  if (OKState) errObj.innerHTML='';
  return OKState;
}
