//JavaScript Utilities v2.5.9 - (c) 2007 Design.uz Studio. http://www.design.uz 
//TRANSLATIONS
words = new Array();
words['required'] = "Required";
words['toolong'] = "Too long";
words['invalid'] = "Invalid value";
words['atleastone'] = "At least one required";
words['error'] = "Error";

$(function() {
	printiMeils2();
	mess.isSet();
});

function word(key){
  return (words[key]!=null) ? words[key] : key;
};
 
function toggleValue(input){
  if(input.title == null || input.title == '') input.title = input.value;	
  input.select();
  if(input.value == input.title) input.value = "";
  input.onblur = function(){ this.value?"":this.value=this.title; }
};

function getURL(url){
  location.href=url;
  return false;
}

function openWindow(url, name, width, height){
  if(!name) name = "popup";
  if(!width) width = 600;
  if(!height) height = $('body').height();
  window.open(url,name,'scrollbars=yes,toolbar=no,directories=no,location=no,menubar=no,resizable=yes,header=no,status=yes,width='+width+',height='+height).focus();
  return false;
};

function printiMeil(name,domain,text,styleclass, pretext){
  return (pretext?pretext+' ':'')+'<a href="mai' + 'lto:' + name + '@' + domain + '"' + (styleclass?' class="' + styleclass + '"':'') + '>' + (text ? text : (name + '@' + domain)) + '</a>';
};

function printiMeils(){
  $('.imeil').each(function(){
    em = this.innerHTML.split(/[,:|-]/);
    this.innerHTML = printiMeil(em[0]?$.trim(em[0]):'',em[1]?$.trim(em[1]):'',em[2]?$.trim(em[2]):'',em[3]?$.trim(em[3]):''); 
  });
};

function printiMeils2(){
  $('.imeil').each(function(){
	if(this.title != null && this.title != ''){
		em = this.title.split(/[|]/);
	}else{
		em2 = /e=([^=$#]+)/.exec(this.href);
		em = RegExp.$1.split(/[|]/);
		if(em[2] != null) em[2] = em[2].replace(/%20/g,' ');
	}
    $(this).after(printiMeil(em[0]? $.trim(em[0]):'', em[1]?$.trim(em[1]):'', em[2]?$.trim(em[2]):'', em[3]?$.trim(em[3]):'', em[4]?$.trim(em[4]):'')); 
	$(this).remove();																												  
  });
};

// HIGHLIGHT    
function over(obj, opener, sclass){
  if(sclass == null) sclass = 'over';
  if(typeof obj == 'string') obj = $('#'+obj);
  else if(typeof obj == 'object') obj = $(obj);
  
  if(opener != null && opener.onmouseout == null){
    opener.onmouseout = function(){out(obj, sclass)};
  };  
  obj.addClass(sclass); 
};
function out(obj,overclass){
  if(typeof obj == 'string') obj = $('#'+obj);
  else if(typeof obj == 'object') obj = $(obj);
  if(overclass == null) overclass = obj.overclass;
  obj.removeClass(overclass); 
};

// FORM VALIDATION
function Dec(){
  re = new RegExp("([^-]+)-","g");
  if(re.exec(this.value))   this.value = this.value.replace(re,"$1");
  re = new RegExp("([^\.]+\\.[^\.]*)\\.","g");
  if(re.exec(this.value))   this.value = this.value.replace(re,"$1");
  re = new RegExp("[^0-9\.-]","g");
  if(re.exec(this.value))   this.value = this.value.replace(re,'');
};
function Int(obj){
  if(obj.target != '') obj = obj.target;
  re = new RegExp("[^0-9]","g");
  if(re.exec(obj.value)) obj.value = obj.value.replace(re,'');
};
function Hour(obj){
  if(obj.target != '') obj = obj.target;
  Int(obj);
  if(obj.value > 23) obj.value = obj.value.substr(0,obj.value.length - 1);
};
function Sec(obj){
  if(obj.target != '') obj = obj.target;
  Int(obj);
  if(obj.value > 59) obj.value = obj.value.substr(0,obj.value.length - 1);
};
function Year(obj){
  if(obj.target != '') obj = obj.target;
  Int(obj);
  if(obj.value > 9999) obj.value = obj.value.substr(0,obj.value.length - 1);
//  else if(obj.value < 1000) obj.value = obj.value + '000'.substr(0,3 - obj.value.length);
};
function isDateTime(){
  re = new RegExp("[^0-9-:\\s]","g");
  if(re.exec(this.value))   this.value = this.value.replace(re,'');
};  
function isVersion(){
  re = new RegExp("[^0-9\.]","g");
  if(re.exec(this.value))   this.value = this.value.replace(re,'');
};
function makeYear(obj){
	if(obj.value == '') return;
	Year(obj);
	if(obj.value < 10) obj.value = '2000'.substr(0,4 - obj.value.length) + obj.value;
	else if(obj.value < 1000) obj.value = '1900'.substr(0,4 - obj.value.length) + obj.value;
};
function sCheck(obj, func){
  obj.onkeyup = func;
  if(obj.onblur == null) obj.onblur = func;
};

required = new Array();

function checkForm(forma){

  i = 0;
  requiredList = new Array(); 
  tooLongList = new Array(); 
  invalidList = new Array();
  requiredArrayList = new Array();
  
  arrayWithValue = new Array();
  requiredArrayCounted = new Array();
  requiredArrayID = new Array();
  
  isemailRE = /email/i;
  isurlRE = /(url|link)/i;
  emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/i;
  urlRE = /^(http|ftp|https):\/\/([\w-]+\.)+(\/[\w- .\/?%&=]*)?/i;
  arrayRE = /^([^\[\]]+)\[[^\]]*\]$/i;

  while(i < forma.elements.length){
    fe = forma.elements[i];

	highlightID = (arrayRE.exec(fe.name) && fe.id != null && fe.id != '')?fe.id:fe.name;
	
    if(required[fe.name] != null && required[fe.name] == true && fe.value == '')
      requiredList[requiredList.length] = mess.makeLink(highlightID,fe.title);
	else if(fe.maxlength && fe.value.length > fe.maxlength)
      tooLongList[toolongList.length] = mess.makeLink(highlightID,fe.title);
    else if(fe.value != '' && 
        (isemailRE.exec(fe.name) && !emailRE.exec(fe.value)) ||
        (isurlRE.exec(fe.name) && !urlRE.exec(fe.value)))
      invalidList[invalidList.length] = mess.makeLink(highlightID,fe.title);
    else if(arrayRE.exec(fe.name))
		
      switch(required[RegExp.$1]){
        case 'atleastone':  
          if((fe.value == '' || (fe.type == 'checkbox' && fe.checked == false)) && arrayWithValue[RegExp.$1] == null){
            if(requiredArrayCounted[RegExp.$1] == null){
			  requiredArrayCounted[RegExp.$1] = true;
			  if(requiredArrayID[RegExp.$1] == null) requiredArrayID[RegExp.$1] = requiredArrayList.length;
              requiredArrayList[requiredArrayID[RegExp.$1]] = mess.makeLink(highlightID,fe.title);			  
            }
          }else{
            arrayWithValue[RegExp.$1] = true;
			requiredArrayList.splice(requiredArrayID[RegExp.$1],1);
		  }
        break;
        case '1':
          requiredList[requiredList.length] = mess.makeLink(highlightID,fe.title);
        break;
      };
    i++;
  };
  if(requiredList.length) mess.add(word('required') + ': ' + requiredList.join(', '));
  if(requiredArrayList.length != 0) mess.add(word('atleastone') + ': ' + requiredArrayList.join(', '));
  if(tooLongList.length) mess.add(word('toolong')+ ': ' + tooLongList.join(', '));
  if(invalidList.length) mess.add(word('invalid') + ': ' + invalidList.join(', ')); 
  mess.mode = 'error';
  return mess.display();
};  
// ERRORS AND MESSAGES
  
function messages(){
  
  this.name = 'messagebox';
  this.div = '#'+this.name;
  this.mode = 'error';
  this.messages = new Array();
  this.pause = '';
  this.shown = false;
  
  this.add = function(text,relid){
    if(relid != null) text = this.makeLink(relid,text);
    this.messages.push(text);
  };
  
  this.makeLink = function(relid,text){
    if(text == null || text == '') text = relid;
    if($(this.div).length == 0) return text;
    if($('#f_'+relid).length != 0) hlobj = 'f_'+relid;
    else if($('#t_'+relid).length != 0) hlobj = 't_'+relid;
    else hlobj = relid;
    if(hlobj != null) return '<a href="#'+hlobj+'" onclick="$(\'#'+relid+'\').focus(); return false" onmouseover="over(\''+hlobj+'\',this,\'hl\')">'+text+'</a>';
  };
  
  this.hideOut = function(){
    $(this.div).fadeOut();
    this.shown = false;
    clearTimeout(this.pause);
    $(this.div).mouseout('');
    $(this.div).mouseover('');    
    return false;
  };
  
  this.autoHide = function(){
    if(this.pause == '') this.pause = pause = setTimeout("mess.hideOut()",5000);
    $(this.div).mouseover(function(){clearTimeout(pause)});
//    $(this.div).mouseout(function(){func = "$('#"+this.id+"').hide()"; timeout = setTimeout(func,3000)});   
  };

  this.setPosition = function(){    
	if($(this.div).width() > 600){
		$(this.div).width(600);
		$('ul',this.div).width(570);
	}
	if($(this.div).height() > 400) $('ul', this.div).height(300);
    $(this.div).css('top',($('body').height() - $(this.div).height())/2);
    $(this.div).css('left',($('body').width() - $(this.div).width())/2); 
//	$('#messagebox').onmousedown = dragThis;
//	location.href=this.div;
  };

  this.isSet = function(){
  	if($(this.div).html() != ''){
		this.setPosition();
	 	$(this.div).fadeIn('',this.autoHide());
	}
  };
  
  this.display = function(){
    Message = '';   i = 0;
    
    if(this.shown == true){this.messages = new Array(); return false;};
    
    if($(this.div).length != 0){

      if(this.messages.length == 0){
        $(this.div).html('');
        return true;
      };
      
      this.shown = true;
      
      while(i < this.messages.length){
        Message += '<li>' + this.messages[i] + '</li>';
        i++;
      };
      
      switch(this.mode){
        case 'error':
          title = '<h3>'+word('error')+':</h3>';
        break;
        default:
          title = '';
        break;
      };
      
      $(this.div).name=this.name;
      $(this.div).html('<div class="'+this.mode+'"><a href="#" onclick="return mess.hideOut()" class="x">x</a>'+title+'<ul>'+Message+'</ul></div>');
      this.setPosition();
	  if(this.noauto == null || this.noauto == false) $(this.div).fadeIn('',this.autoHide());
	  else $(this.div).fadeIn();
	  location.href='#messagebox';
    }else{
      
      if(this.messages.length == 0) return true;
    
      while(i < this.messages.length){
        Message += this.messages[i] + "\n";
        i++;
      };
      alert(Message);
    };
    this.messages = new Array();
    return false;
  };    
};

mess = new messages(); // OBJECT INITIALIZATION

function showStatus(mode){
	$('#astatus').html('Processing...');
	$('#astatus').show();
}
function hideStatus(mode){
	$('#astatus').hide();
}

// RESIZERS
//var toResize = {'scroller':'h200'};
function autoResize(targs){

  if(targs == null)
    if(toResize == null) return false;
    else targs = toResize;
  else toResize = targs;
  
  i = 0;  re = /([w|h])([0-9]+)(%)?/i;
  while(i < targs.length){
    switch(targs[i][1].match(re)[1]){
      case 'h':
        sH = screenSize().h;
		
        if($('#'+targs[i][0]).oldh == null) $('#'+targs[i][0]).oldh = $('#'+targs[i][0]).height();
        newh = sH - ((targs[i][1].match(re)[3]!=null && targs[i][1].match(re)[3]!='')? sH*targs[i][1].match(re)[2]*.01:targs[i][1].match(re)[2]);
        if($('#'+targs[i][0]).oldh > newh) newh = $('#'+targs[i][0]).oldh;
        if(targs[i][1].match(re)[3] != null && targs[i][1].match(re)[3] > newh) newh = targs[i][1].match(re)[3];
        $('#'+targs[i][0]).height(newh);
      break;
      case 'w':
		sH = screenSize().w;
        if($('#'+targs[i][0]).oldw == null) $('#'+targs[i][0]).oldw = $('#'+targs[i][0]).width();
        neww = sW - ((targs[i][1].match(re)[3]!=null && targs[i][1].match(re)[3]!='')? sW*targs[i][1].match(re)[2]*.01:targs[i][1].match(re)[2]);
        if($('#'+targs[i][0]).oldw > neww) neww = $('#'+targs[i][0]).oldw;
        if(targs[i][1].match(re)[3] != null && targs[i][1].match(re)[3] > neww) neww = targs[i][1].match(re)[3];
        $('#'+targs[i][0]).width(neww);
      break;
    };
    i++;
  };
  window.onresize = autoResize;
};

function screenSize(){
  var sSize = new Object;
  sSize.dw = document.body.scrollWidth; 
  sSize.dh = document.body.scrollHeight;
  if (self.innerWidth){
	sSize.w = self.innerWidth;
    sSize.h = self.innerHeight;
	sSize.dw = document.body.scrollWidth; 
  }
  else if (document.documentElement && document.documentElement.clientWidth)
  {
    sSize.w = document.documentElement.clientWidth;
    sSize.h = document.documentElement.clientHeight;
  }
  else if (document.body)
  {
    sSize.w = document.body.clientWidth;
    sSize.h = document.body.clientHeight;
  }
  
  return sSize;
}

function autoFocus(fieldname,form){
  if(fieldname == null) return false;
  if(form != null){form.fieldname.select(); form.fieldname.focus()}
  else{$('#'+fieldname)[0].select(); $('#'+fieldname)[0].focus();};
}