function init_listeners() {
  var f = document.forms[0];
  f.reset();
  f.onkeyup = function () {check_form();};
  f.gb_nick.onkeyup = function () {check_length(this.value,3)};
  f.gb_email.onkeyup = function () {check_email(this)};
  f.gb_country.onkeyup = function () {ajax.doPost('includes/zi_ajax.php','action=get_country&search='+escape(this.value),check_country)};
  f.gb_url.value = 'http://';
  f.gb_img_url.value = 'http://';
  f.gb_url.onkeyup = function () {};
  //f.gb_message.onkeyup = function () {check_length(this.value,20)};
  document.getElementById('gb_entries').style.display = 'none';
  document.getElementById('gb_nav').style.display = 'none';
  document.getElementById('gb_fields').style.display = 'block';
  f.gb_nick.focus();
}

function check_length(str,val) {
  if (str.length>=val) {
  return true;
  } else {
  return false;
  }
}

function check_email(obj) {
  if (obj.value.length>0) {
    if (obj.value.search(/^\w+((-\w+)|(.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) !=-1) {
      return true;
    } else {
      return false;
    }
  } else {
  return true;
  }
}

function check_country(str) {
  var f = document.forms[0];
  var p_child = document.getElementById('gb_country');
  var parent = p_child.parentNode;
  if (document.getElementById('c_list')) {
  parent.removeChild(document.getElementById('c_list'))
  }
if (str.length>0) {
  var arr = str.split(';');
  if (arr.length==1) {
    var tmp = arr[0].split(',');
    f.gb_country.value = tmp[1];
    f.co_id.value = tmp[0];
  } else {
    var c_list = document.createElement('div');
    c_list.setAttribute('id','c_list');
    c_list.className = 'gb_clist';
    br = p_child.nextSibling;
    parent.insertBefore(c_list,br.nextSibling);
    for (i=0;i<arr.length;i++) {
      var item = document.createElement('div');
      tmp = arr[i].split(',');
      item.setAttribute('id',tmp[0]);
      item.innerHTML = tmp[1];
      item.onclick = function () {f.gb_country.value=this.innerHTML;f.co_id.value=this.id;parent.removeChild(this.parentNode);}
      c_list.appendChild(item);
    }
  }
}
}

function check_form() {
var f = document.forms[0];
var nick_ok = check_length(f.gb_nick.value,3);
var email_ok = check_email(f.gb_email);
//var mess_ok = check_length(f.gb_message.value,20);

  if (nick_ok==true && email_ok==true) {
    f.gb_sign.disabled = false;
  } else {
    f.gb_sign.disabled = true;
    f.gb_sign.onclick = function () {insert_gb_entry()};
  }

}

function insert_gb_entry() {
var f = document.forms[0];
var str = 'action=insert_guestbook';
var inst = tinyMCE.getInstanceById('gb_message');
f.gb_message.value = inst.getHTML();
var url = f.gb_url.value;
var img_url = f.gb_img_url.value;

  if (url.replace(/http\:\/\//gi,'').length==0) {
  f.gb_url.value = '';
  }

  if (img_url.replace(/http\:\/\//gi,'').length==0) {
  f.gb_img_url.value = '';
  }

  if (!f.co_id.value>0) {
  f.co_id.value = 1;
  }

var mess_ok = check_length(f.gb_message.value,5);
  if (mess_ok==true) {
    for (i=0;i<f.elements.length;i++) {
    str += '&'+f.elements[i].name+'='+escape(f.elements[i].value);
    }
    ajax.doPost('includes/zi_ajax.php',str,confirm_entry);
  } else {
    alert("are you sure that you don\'t want to leave a message?");
  }
}

function confirm_entry(nick) {
  str = 'Thank you '+nick+' for signing our guestbook\nPlease check back frequently to check for replies.'
  alert(str);
  window.open(location,'_top');
}

function get_gb_entries(page,rows) {
if (page==null) {
  page = 1;
}
if (rows==null) {
  rows = 5;
}
ajax.doPost('includes/zi_ajax.php','action=get_guestbook&page_no='+page+'&rows_per_page='+rows,print_guestbook);
}

function print_guestbook(str) {
//alert(str);
var root = document.getElementById('gb_entries');
var nav = document.getElementById('gb_nav');
var control = document.getElementById('gb_control');
  if (str.length>0) {
  //get string content
  var arr = str.split(';');
  var tmp = arr[0].split(',');
  var cur_page = Math.abs(tmp[0]);
  var no_entries = tmp[1];
  var no_rows = tmp[2];
  var no_pages = Math.round(no_entries/no_rows);
  var nav_fwd = ' ';
  var nav_rew = ' ';
  control.innerHTML = '<a href="javascript:void(0);" onclick="init_listeners()" style="float:right">Sign</a> We got '+no_entries+' entries in our guestbook ';
  if (cur_page>1) {
  var nav_to = cur_page-1;
  nav_fwd = '<a href="javascript:void(0);" onclick="javascript:get_gb_entries('+nav_to+','+no_rows+')" style="float:left" title="'+nav_to+'">&lt;&lt;</a>'
  }
  if (cur_page<no_pages) {
  var nav_to = cur_page+1;
  nav_rew = '<a href="javascript:void(0);" onclick="javascript:get_gb_entries('+nav_to+','+no_rows+')" style="float:right" title="'+nav_to+'">&gt;&gt;</a>'
  }
  nav.innerHTML = nav_fwd+nav_rew+' '+cur_page+' of '+no_pages+' ';
  arr = arr.splice(1,5);
  while (root.firstChild) {
    root.removeChild(root.firstChild);
  }
    for (i=0;i<arr.length;i++) {
    var tmp = arr[i].split(',');
      var gb_node = document.createElement('div');
      gb_node.setAttribute('id','gb_id'+tmp[0]);
      gb_node.className = 'gb_entry';
      root.appendChild(gb_node);

      var gb_head = document.createElement('h2');
      var url = '';
      if (tmp[6].length>0) {
      url = '<a href="'+tmp[6]+'" style="float:right" target="_blank">Home</a>'
      }
      gb_head.innerHTML = url+'on '+tmp[3]+' '+to_html(unescape(tmp[4]).replace(/\+/g,' '))+' in '+tmp[1]+' wrote';
      gb_node.appendChild(gb_head);

      var gb_text = document.createElement('span');
      var txt = unescape(tmp[8]);
      txt = txt.replace(/\+/g,' ');
      txt = txt.replace(/\n/g,'<br />');
      gb_text.innerHTML = to_html(txt);
      gb_node.appendChild(gb_text); 
    }
  }
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------
