var SITE_URL;
var _ajax_element_id;
var LANG = new Array();

function set_site_url(url){
  SITE_URL = url;
}

function add_change_events(obj, func){
  obj.onclick = func;
  obj.onfocus = func;
  obj.onblur = func;
  obj.change = func;
  obj.onkeydown = func;
  obj.onkeypress = func;
  obj.onkeyup = func;
}

function add_focusblur_events(obj, func){
  obj.onfocus = func;
  obj.onblur = func;
}

function e(id){
  return document.getElementById(id);
}

function err(err_msg){
  if (err_msg == "" || err_msg == false) {
    document.getElementById('error_box').innerHTML = "";
    document.getElementById('error_box').style.display = "none";
  }
  else {
    msg ("");
    html_code = "<div style='text-align: right; width: 99%;'><a href='javascript: err(\"\")'><img id='error_msg_close_icon' style='position: absolute; margin-top: -7px; border: 0px;' src='"+SITE_URL+"images/close_red.gif' /></a><input type='text' style='position: absolute; border: 0px; background: transparent; font-size: 1px; width: 1px; margin-top: -30px; left: -2px;' id='mt'></div>";
    html_code += err_msg;
    document.getElementById('error_box').style.display = 'block';
    document.getElementById('error_box').innerHTML = html_code;
    //location.href = trim(location.href, "#") + "#";
    document.getElementById('mt').focus();
  }
}
function msg(msg){
  if (msg == "" || msg == false) {
    document.getElementById('message_box').innerHTML = "";
    document.getElementById('message_box').style.display = "none";
  }
  else {
    err("");
    html_code = "<div style='text-align: right; width: 99%;'><a href='javascript: msg(\"\")'><img style='position: absolute; margin-top: -8px; margin-left: -1px; border: 0px;' src='"+SITE_URL+"images/close_green.gif' /></a><input type='text' style='position: absolute; border: 0px; background: transparent; font-size: 1px; width: 1px; margin-top: -30px; left: -2px;' id='mt'></div>";
    html_code += msg;
    document.getElementById('message_box').innerHTML = html_code;
    document.getElementById('message_box').style.display = 'block';
    //location.href = trim(location.href, "#") + "#";
    document.getElementById('mt').focus();
  }
}

function loading_image(display) {
  if (display){
    document.getElementById('loading_image').style.display = 'block';
  }
  else {
    document.getElementById('loading_image').style.display = 'none';
  }
}

function ajax_get_page(url, element_id){
  _ajax_element_id = element_id;
  makeGetRequest(url, "ajax_get_page2");
  document.body.style.cursor = 'wait';
}

function ajax_get_page2(r){
  /* Process the JS <script>'s
  var regex = new RegExp("<script>(.*?)</script>");
  
  while (match = regex.exec(r)) {
    if (match[1] == "") {
      break;
    }
    eval2(match[1]);
    r = r.replace(match[1], "");
  }
  */
  if (r != "") {
    document.getElementById(_ajax_element_id).innerHTML = r;
  }
  document.body.style.cursor = 'default';
}

// ajax_get_page using the "content2" object
function agp(url){
  ajax_get_page(url, "content2");
}

function unixTime(){
  var d = new Date();
  return d.getTime();
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function strlen(strVar){
  return(strVar.length);
}

function strpos(str, ch) {
  for (var i = 0; i < str.length - ch.length; i++) {
    if (str.substring(i, i+ch.length) == ch)
      return i;
  }
  return -1;
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function GET(argname){
    query = location.search.substring(1);
    pairs = query.split("&");
    for(i=0; i<pairs.length; i++){
        key   = pairs[i].split("=")[0];
        value = pairs[i].split("=")[1];
        if(key == argname){
            return value;
        }
    }
    return "";
}

function this_page(keep_get_vars, new_get_vars){
  uri = trim(location.href, "#");
  
  if (!keep_get_vars){
    p = strpos(uri, "?");
    if (p != -1)
      uri = uri.substr(0, p);
  }
  
  if (new_get_vars) {
    vars = new_get_vars.split("&");
    
    for (i=0; i<vars.length; i++) {
      cvar = vars[i];
      cvar = cvar.split("=");
      name = cvar[0];
      value = cvar[1];

      p = uri.indexOf(name + "=");
      if (p != -1){
        p += strlen(name + "=");
        q = strpos(uri.substr(p), "&");
        if (q != -1){
          uri = uri.substr(0, p)  + value + uri.substr(p+1, p+q);
        }
        else {
          uri = uri.substr(0, p) + value;
        }
      }
      else {
        if (strpos(uri, "?") == -1){
          uri += "?" + escape(name) + "=" + escape(value);
        }
        else
          uri += "&" + escape(name) + "=" + escape(value);
      }
    }
  }
  return uri;
}

function set_lang_word (name,value) {
  var obj = new Object();
  obj.name = name;
  obj.value = value;
  LANG[LANG.length] = obj;
}

function je (name) {
  for (i=0;i<LANG.length;i++) {
    if (LANG[i].name == name) {
      return LANG[i].value;
    }
  }
  return name;
}

function friendly_duration(seconds){
  z = Math.floor(seconds / (3600*24));
  h = Math.floor((seconds-z*3600*24) / 3600);
  m = Math.floor((seconds-(z*3600*24+h*3600))/ 60);
  s = Math.floor(seconds % 60);
  
  wday     = je("day");
  wdays    = je("days");
  whour    = je("hour");
  whours   = je("hours");
  wminute  = je("minute");
  wminutes = je("minutes");
  wsecond  = je("second");
  wseconds = je("seconds");
  wand     = je("and");
  
  msg = "";
  if(z != 0){
   if(z == 1)
     msg = msg + " " + wday + ", ";
   else
     msg = msg + z + " " + wdays + ", ";
   if(h == 1)
     msg = msg + " " + whour + ", ";
   else if(h != 0)
     msg = msg + h + " " + whours + ", ";
   if(m != 0)
     if(m == 1)
       msg = msg + " " + wminute + " ";
     else
       msg = msg + m + " " + wminutes + " ";
     if(s != 0) {
       if(s == 1)
         msg = msg + " " + wand + " " + wsecond;
       else
         msg = msg + " " + wand + " " + s + " " + wseconds;
     }
  }
  else if(h != 0){
   if(h == 1)
     msg = msg + " " + whour;
   else
     msg = msg + h + " " + whours + ", ";
   if(m != 0)
     if(m == 1)
       msg = msg + " " + wminute;
     else
       msg = msg + m + " " + wminutes + " ";
     if(s != 0) {
       if(s == 1)
         msg = msg + " " + wand + " " + " " + wsecond;
       else
         msg = msg + " " + wand + " " + s + " " + wseconds;
     }
  }
  else {
   if(m != 0){
     if(m == 1)
       msg = msg + " " + wminute;
     else
       msg = msg + m + " " + wminutes;
     if(s != 0){
       if(s == 1)
         msg = msg + " " + wand + " " + " " + wsecond;
       else
         msg = msg + " " + wand + " " + s + " " + wseconds;
     }
   }
   else {
     if(s == 1)
       msg = msg + " " + wsecond;
     else
       msg = msg + s + " " + wseconds;
   }
  }
  return msg;
}

function print_countdown_timer(seconds, CDT_id){
  document.write("<div id='CDT_"+CDT_id+"'></div>");
  myCDT = new CDT(seconds, CDT_id);
  myCDT.start();
  void 0;
  return false;
}

if (!CDT_Objects) {
  var CDT_Objects = [];
};

function CDT(seconds, CDT_id) {
  this._objectId;
  this._id;
  this.sec_rem;
  this._stopped=false;
  this.CDT_id;
  
  this._constructor = function(seconds, id) {
    this._id = CDT_Objects.length;
    this.CDT_id = id;
    this.sec_rem = seconds;
    CDT_Objects[this._id] = this;
    this._objectId = "CDT_"+this._id;
  }
  
  this._tick = function(){
    if (this._stopped){
      return false;
    }
    this.sec_rem--;
    this._update_msg(this.sec_rem);
    if (this.sec_rem > 0) {
      setTimeout("CDT_Objects["+this._id+"]._tick()", 1000);
    }
  }
  
  this._update_msg = function(sec_rem){
    if (this._stopped){
      return false;
    }
  
    msg = friendly_duration(sec_rem);
    if (!document.getElementById('CDT_'+this.CDT_id)) {
      this.stop();
    }
    else if(sec_rem > 0) {
      document.getElementById('CDT_'+this.CDT_id).innerHTML = msg;
    }
    else {
      document.getElementById('CDT_'+this.CDT_id).innerHTML = "-";
    }
  }
  
  this.start = function(){
    sec = this.sec_rem;
    if (sec > 0) {
      setTimeout("CDT_Objects["+this._id+"]._tick()", 1000);
    }
  }
  
  this.stop = function(){
    this._stopped = true;
  }
  
  this._constructor(seconds, CDT_id);
}