function redir(url)
{
  location.href=url;
}

function $(id)
{
  return(document.getElementById(id));
}

function textCounter(name) {
  var field = $(name);
  var chars = $('char_count');
  var maxlimit = 140;
  if(field != null)
  {
    if(field.value.length > maxlimit)
      field.value = field.value.substring(0, maxlimit);
    else
      chars.innerHTML = maxlimit - field.value.length;
  }
}

function validateDelete()
{
  return(confirm("are you sure you want to delete this record ?"));
}

/*
  Server time
*/

var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var dateObj;

function srvClock(date)
{
  dateObj = new Date(date);
  displayTime();
  window.onload = function(){setInterval("displayTime()", 1000)}
}

function padlength(what)
{
  return ((what.toString().length==1)? "0" + what : what);
}

function displayTime()
{
  this.dateObj.setSeconds(this.dateObj.getSeconds()+1)
  var datestring = montharray[this.dateObj.getMonth()] + " " + padlength(this.dateObj.getDate()) + ", " + this.dateObj.getFullYear();
  var timestring = padlength(this.dateObj.getHours()) + ":" + padlength(this.dateObj.getMinutes()) + ":" + padlength(this.dateObj.getSeconds());
  $("servertime").innerHTML = datestring+" "+timestring;
}

/*
  Flip routines
*/

var flipTable = {
a : '\u0250', b : 'q', c : '\u0254', d : 'p', e : '\u01DD', f : '\u025F', g : '\u0183', h : '\u0265',
i : '\u0131', j : '\u027E', k : '\u029E', m : '\u026F', n : 'u', r : '\u0279', t : '\u0287', v : '\u028C',
w : '\u028D', y : '\u028E', '.' : '\u02D9', '[' : ']', '(' : ')', '{' : '}', '?' : '\u00BF', '!' : '\u00A1',
"\'" : ',', '<' : '>', '_' : '\u203E', ';' : '\u061B', '\u203F' : '\u2040', '\u2045' : '\u2046', '\u2234' : '\u2235'
}

for (i in flipTable)
{
  flipTable[flipTable[i]] = i;
}


function flipString(aString)
{
 var last = aString.length - 1;
 var result = new Array(aString.length)
 for (var i = last; i >= 0; --i)
 {
  var c = aString.charAt(i)
  var r = flipTable[c]
  result[last - i] = r != undefined ? r : c
 }
 return result.join('')
}


function flipText(name)
{
  var result = flipString($(name).value.toLowerCase());
  $(name).value = result;
}

/*
  Icons
*/
function showIcons()
{
	$("icon_list").style.visibility = "visible";
}

function hideIcons()
{
  $("icon_list").style.visibility = "hidden";
}

function addIcon(field,icon)
{
  var f = $(field);
  f.value = f.value + icon;
  hideIcons();
  textCounter(field);
}

function checkStep1()
{
  if(document.install.db_server.value == '')
    document.install.db_server.focus();
  else if(document.install.db_database.value == '')
    document.install.db_database.focus();
  else if(document.install.db_username.value == '')
    document.install.db_username.focus();
  else if(document.install.db_password.value == '')
    document.install.db_password.focus();
  else
  {
    document.install.submit();
  }
}

function checkStep2()
{
  if(document.install.site_name.value == '')
    document.install.site_name.focus();
  else if(document.install.site_url.value == '')
    document.install.site_url.focus();
  else if(document.install.site_webmaster_name.value == '')
    document.install.site_webmaster_name.focus();
  else if(document.install.site_webmaster_email.value == '')
    document.install.site_webmaster_email.focus();
  else if(document.install.max_users_per_page.value == '')
    document.install.max_users_per_page.focus();
  else if(document.install.max_msgs_per_page.value == '')
    document.install.max_msgs_per_page.focus();
  else if(document.install.max_msgs_per_hour.value == '')
    document.install.max_msgs_per_hour.focus();
  else if(document.install.max_days_to_delete.value == '')
    document.install.max_days_to_delete.focus();
  else
  {
    document.install.submit();
  }
}

function checkStep3()
{
  if(document.install.email_signup_subject.value == '')
    document.install.email_signup_subject.focus();
  else if(document.install.email_signup_body.value == '')
    document.install.email_signup_body.focus();
  else if(document.install.email_confirmed_subject.value == '')
    document.install.email_confirmed_subject.focus();
  else if(document.install.email_confirmed_body.value == '')
    document.install.email_confirmed_body.focus();
  else if(document.install.email_forgot_subject.value == '')
    document.install.email_forgot_subject.focus();
  else if(document.install.email_forgot_body.value == '')
    document.install.email_forgot_body.focus();
  else
  {
    document.install.submit();
  }
}

function checkDelete()
{
  if(confirm("are you 100% sure you want to delete your account ?"))
    location.href='settings.php?a=delete';
    //document.settings.submit();
}