﻿/*
 +-----------------------------------------------------------------------+
 | M@il.go.th Login library                                              |
 |                                                                       |
 | This file is part of the M@il.go.th                                   |
 | Copyright (C) 2005-2008, MICT, - Thailand                             |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: Surapun Prasit - RDDI                                         |
 |         Government Information Technology Services(GITS)              |
 +-----------------------------------------------------------------------+
 
 varsion 0.2.5
 $Id: 081029
*/

function jscookie(){
  this.get_offset_time = function(offset_str, factor)
  {
    if (factor == null) factor=1;

    var regs = null;
    var regex = new RegExp('^([0-9]+)\s*([smhdwnou])','i');
    regs = regex.exec(offset_str);

    if ( regs != null )
    {
      var amount = parseInt(regs[1]);
      var unit   = regs[2].toLowerCase();
    }
    else
    {
      var amount = parseInt(offset_str);
      var $unit  = 's';
    }
    
    var dt = new Date();
    var ts = dt.getTime();

    switch (unit)
    {
      case 'o':
      	ts = this.get_offset_time('30d', factor);
	break;
      case 'w':
        amount *= 7;
      case 'd':
        amount *= 24;
      case 'h':
        amount *= 60;
      case 'm':
        amount *= 60;
      case 's':
        amount *= 1000;
      case 'u':
        ts += amount * factor;
    }
  
    dt.setTime(ts);
    return dt.toGMTString();
  }

  this.create = function(name,value,time) {
    if (time) {
      var expires = "; expires="+this.get_offset_time(time);
    }
    else 
      var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
  }
											
  this.read = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  }

  this.erase = function(name) {
    this.create(name,"",-1);
  }
}

function MGT(){
  this.env = env.login_box;

  // save username in cookies
  this.save_username = function(username){
    var cook = new jscookie();
    var mem_me = document.getElementById(this.env.remember_me.id);

    if( mem_me != null && mem_me.checked && this.env.remember_me.enable){
      cook.create('_account',username,this.env.username_expire);
      username = username.split('@',2);
      cook.create('_username',username[0],this.env.username_expire);
      cook.create('_domain',username[1],this.env.domain_expire);
    }
  }

  // submit login
  this.login = function(){
    var usr = document.getElementById(this.env.username_field.id);
    var pwd = document.getElementById(this.env.password_field.id);

    if( usr != null && usr.value != '')
        this.save_username(usr.value);
    else{
      alert(this.env.username_empty);
      return false;
    }

    if( pwd == null || pwd.value == '' ){
      alert(this.env.password_empty);
      return false;
    }
  }
	this.diff_acc = function(){
    var opt = document.getElementById(this.env.add_option.id);
    var acc_field = document.getElementById(this.env.username_div.id);
		var cook = new jscookie();
  	if(opt!=null)
      opt.innerHTML = this.memme_check();
    if( acc_field != null )
      acc_field.innerHTML = this.user_input();
      cook.erase('_account');
      cook.erase('_username');
  }
 
  // return HTML code of user input field
	this.user_input = function(){
	  return '<input class="'+this.env.username_field.classname+'" id="'+this.env.username_field.id+'" name='+this.env.username_field.name+' type="text" />';
	}

  // return HTML for remember-me selector
  this.memme_check = function(){
		var tag = '<input id="'+this.env.remember_me.id+'" ';
		tag += 'name="'+this.env.remember_me.name+'" type="checkbox" ';
		tag += this.env.remember_me.checked ? 'checked="checked"' : '';
		tag += '/><label class="mailgoth-option-text" for="mailgoth-remember_me">'+this.env.mem_me+'</label>';
		return this.env.remember_me.enable ? tag : '';
	}

  // load login form
  this.load = function(){
    var cook = new jscookie();
    var opt = document.getElementById(this.env.add_option.id);
    var acc_field = document.getElementById(this.env.username_div.id);
		var pwd = document.getElementById(this.env.password_field.id);
		var frm = document.getElementById('mailgoth-login-form');
    var acc;
    
    pwd.name = this.env.password_field.name;
    frm.action = this.env.login_form.action;
    frm.method = this.env.login_form.method;
		
		// additionnal params
		var newinput;
		for( varname in this.env.additional_params ){
		  newinput = document.createElement('input');
      newinput.setAttribute('name', varname);
      newinput.setAttribute('value',this.env.additional_params[varname]);
      newinput.setAttribute('type','hidden');
      frm.appendChild(newinput);
		}

    if( ( acc = cook.read('_account')) != null ){
      if(opt!=null) 
        opt.innerHTML = '<a href="#" onclick="mgt.diff_acc()" class="mailgoth-option-text">'+this.env.diff_signin+'</a>';
      if(acc_field != null){
        acc_split = acc.split('@',2);
        acc_field.innerHTML  = acc_split[0]+'<span class="mailgoth-domain">@'+acc_split[1]+'</span>';
        acc_field.innerHTML += '<input class="text" id="'+this.env.username_field.id+'" name="'+this.env.username_field.name+'" type="hidden" value="'+acc+'" />';
         
      }
    }
    else{
      if(opt!=null)
        opt.innerHTML = this.memme_check();
      if(acc_field != null){
        acc_field.innerHTML = this.user_input();
	    var usr = document.getElementById(this.env.username_field.id);
	    //ksearch.init_address_input_events(usr);
      }
    }
  }
}

var mgt = null;
