﻿/* 
	Используется в html_form().
	Служит для сворачивания/разворачивания групп.
*/
function fields_group_roll(id){
	var g = document.getElementById('fields_group_' + id);
	var i = document.getElementById('group_indicator_' + id);
	
	if(g.style.display=='block'){
		g.style.display='none';
		i.src = '/yadro/interface/images/down.gif';
	}else{
		g.style.display = 'block';
		i.src = '/yadro/interface/images/up.gif';
	}
}
/* 
	Используется в html_form().
	Служит для обновления CAPTCHA.
*/
function captcha_refresh(id){
	// Антикэш.
	var d = new Date();
	document.getElementById('captcha_'+id).src = '/captcha.php?' + d.getTime();
}

// Служебные глобальные переменные.
var start_y = 0;
var start_height = 0;
var current_height = 0;
var old_onmousemove = null;
var old_onmouseup = null;
var min_height = 20;
var textbox = null;

function textbox_expander(ev,expander,tb){
    if(ev == null){ ev = window.event }
    if(ev.preventDefault){
        ev.preventDefault();
    };
    textbox = document.getElementById(tb);
    start_y = ev.clientY;
    start_height = textbox.offsetHeight;  
    old_onmousemove = document.onmousemove;
    old_onmouseup = document.onmouseup;
    document.onmousemove = textbox_expander_move;
    document.onmouseup = textbox_expander_up;
    return false;	  
}

function textbox_expander_move(ev){
	if(ev == null){ ev = window.event }
	if(ev.button<=1){
		current_height = (start_height+(ev.clientY-start_y));
		if (current_height<min_height) current_height = min_height;
		textbox.style.height = current_height + 'px';
		return false;
	}
}

function textbox_expander_up(e) {
	// Восстанавливаем обработчики.
	document.onmousemove=old_onmousemove;
	document.onmouseup=old_onmouseup;
	// Значения по умолчанию.
	start_y = 0;
	start_height = 0;
	current_height = 0;
	old_onmousemove = null;
	old_onmouseup = null;
	textbox = null;		
}




// http://blog.versed.se/2008/07/transliterate-in-javascript.html
// На основе универсальной заготовки.



String.prototype.tr = function() {
	from = 'а,б,в,г,д,е,ё,ж,з,и,й,к,л,м,н,о,п,р,с,т,у,ф,х,ц,ч,ш,щ,ы,э,ю,я,ь,ъ, ';
	to =   'a,b,v,g,d,e,e,zh,z,i,y,k,l,m,n,o,p,r,s,t,u,f,kh,ts,ch,sh,shch,y,e,yu,ya,,,-';

	from += ',А,Б,В,Г,Д,Е,Ё,Ж,З,И,Й,К,Л,М,Н,О,П,Р,С,Т,У,Ф,Х,Ц,Ч,Ш,Щ,Ы,Э,Ю,Я,Ь,Ъ,\",\'';
	to +=   ',A,B,V,G,D,E,E,Zh,Z,I,Y,K,L,M,N,O,P,R,S,T,U,F,Kh,Ts,Ch,SH,Shch,Y,E,Yu,Ya,,,,';
	
    var fromChars = from.split(",");  
    var toChars = to.split(",");  

    var mapTable = {};  

    for(i = 0; i < fromChars.length; i++) {  
        var c = i < toChars.length ? toChars[i] : "";  
        mapTable[fromChars[i]] = c;  
    }  

    var str = this;  

    var re = new RegExp(fromChars.join("|"), "g");  
    str = str.replace(re, function(c) { 
        return mapTable[c]; 
    });  

    return str;  
};

function sef_click(source,dest){
	var t = $('#'+source).val();
	$('#'+dest).val(t.tr());
}




//http://deer.org.ua/2008/07/02/1/comments/	
/* Определяем через паренты начало элемента по оси Х
Входной парметр, АйДи элемента
Возвращает Int*/
function findPosX(obj){
  var curleft = 0;
  if(obj.offsetParent)
    while(1) {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)break;
      obj = obj.offsetParent;
    }
  else if(obj.x) curleft += obj.x;
  return curleft;
}
/* Определяем Не Ие ли Броузер :)
Возвращает Bool*/
function isIE (){
    return navigator.userAgent.toLowerCase().indexOf("msie") > -1;}
/* Определяем через паренты начало элемента по оси Y
Входной парметр, АйДи элемента
Возвращает Int*/
function findPosY(obj){
  var curTop= 0;
  if(!isIE) curTop= obj.offsetHeight;
  if(obj.offsetParent)
    while(1){
     curTop+= obj.offsetTop;
     if(!obj.offsetParent) break;
     obj= obj.offsetParent;
    } 
  else if(obj.y) curTop+= obj.y; 
  return curTop;
} 
/* Определяем через функции findPosХ и findPosY координаты елемента
Входной парметр, АйДи элемента
Возвращает масив координат array {x,y}*/
function getPositionMy(e){
  return {x:findPosX(e), y:findPosY(e)};
}



function sk_combobox_get_key(n){
	var i = eval('sk_combobox_selected_index_'+n);
	var key = eval('sk_combobox_values_'+n+'['+i+'][0]');
	return key;
}

function sk_combobox_get_val(n){
	var i = eval('sk_combobox_selected_index_'+n);
	var val = eval('sk_combobox_values_'+n+'['+i+'][1]');
	return val;
}

function sk_combobox_set_value(i,n){
	var length = eval('sk_combobox_values_'+n+'.length');
	if(length>0){
		var name = '#sk_combobox_value_'+n;
		var val = eval('sk_combobox_values_'+n+'['+i+'][1]');
		var key = eval('sk_combobox_values_'+n+'['+i+'][0]');
		eval('sk_combobox_selected_index_'+n+'='+i);
		$(name).html(val);
		$('#'+n).val(key);
	}
}

function sk_combobox_click(el,n){
	var name = '#sk_combobox_list_'+n;
	
	var pos = $(el).position();

	$(name).css("top",(pos.top+el.offsetHeight));
	$(name).css("left",pos.left);	
	
	$(name).css("width",el.offsetWidth-2); 
	$(name).toggle();
	var display = $(name).css('display');
	// Спрятать все открытые комбобоксы.
	$('.sk_combobox_list').css('display','none');
	$(name).css('display',display);
}
	
function sk_combobox_click_handler(e){
	if(e.target==undefined){
		class_name = e.srcElement.className;
	}else{
		class_name = e.target.className;
	}
	if(class_name!='sk_combobox_button'&&class_name!='sk_combobox'&&class_name!='sk_combobox_value'){
		$('.sk_combobox_list').css('display','none');	
	}
}

function combobox_init(){
	// Перехват события click на всей странице.
	if(document.addEventListener){
		document.addEventListener('click', sk_combobox_click_handler, false);
	}else{
		document.attachEvent('onclick', function(e) { sk_combobox_click_handler(window.event); } );	
	}	
}


function sk_select_tab(control_id,tab_id){
	eval('sk_tabs_control_'+control_id+'_active_tab='+tab_id+';');
	$('.sk_tab').removeClass('active');
	$('#sk_tab_'+tab_id).addClass('active');
	$('.sk_tab_content').hide();
	$('#sk_tab_content_'+tab_id).show();
}

















// Служебные глобальные переменные.
var start_y = 0;
var start_height = 0;
var current_height = 0;
var old_onmousemove = null;
var old_onmouseup = null;
var min_height = 0;
var prev_div = null;
var sync_resize = null; 

function div_expander(){
	ev = arguments[0];
	expander = arguments[1];
	
	if(arguments[2]!=null) sync_resize = arguments[2];
 
	prev = sibleNode(expander,false);
	
    if(ev == null){ ev = window.event }
    if(ev.preventDefault){ ev.preventDefault() };
	
	prev_div = prev;

    start_y = ev.clientY;
    start_height = prev_div.offsetHeight;  
    old_onmousemove = document.onmousemove;
    old_onmouseup = document.onmouseup;
    document.onmousemove = div_expander_move;
    document.onmouseup = div_expander_up;
    return false;		
}


function div_expander_move(ev){
	if(ev == null){ ev = window.event }
	if(ev.button<=1){
		current_height = (start_height+(ev.clientY-start_y));
		if (current_height<min_height) current_height = min_height;
		prev_div.style.height = current_height + 'px';
	
		if(sync_resize!=null) sync_resize();
	
		return false;
	}
}

function div_expander_up(){
	// Восстанавливаем обработчики.
	document.onmousemove=old_onmousemove;
	document.onmouseup=old_onmouseup;
	// Значения по умолчанию.
	start_y = 0;
	start_height = 0;
	current_height = 0;
	old_onmousemove = null;
	old_onmouseup = null;
	prev_div = null;
}


// http://forum.sources.ru/index.php?showtopic=247406
function sibleNode (oElm, forward) {
      var nElm;
      if (forward) { // path to sibling
              nElm = oElm.nextSibling;
      } else {
              nElm = oElm.previousSibling;
      }
      if (nElm.nodeType == 1) { // Node.ELEMENT_NODE
              return nElm;
      } else {
              return sibleNode (nElm, forward);
      }
}

var start_x = 0;
var start_width = 0;
var current_width = 0;
var min_width = 20;
var next_div = null;
var min_right_width = 40;

function div_expander_v(ev,expander){
    if(ev == null){ ev = window.event }
    if(ev.preventDefault){ ev.preventDefault() };
	
	prev_div = sibleNode(expander,false);
	next_div = sibleNode(expander,true);
	 
    start_x = ev.clientX;
    start_width = prev_div.offsetWidth;  
    old_onmousemove = document.onmousemove;
    old_onmouseup = document.onmouseup;
    document.onmousemove = div_expander_v_move;
    document.onmouseup = div_expander_v_up;
    return false;		
}

function div_expander_v_move(ev){
	if(ev == null){ ev = window.event }
	if(ev.button<=1){
		current_width = (start_width+(ev.clientX-start_x));
		if (current_width<min_width) current_width = min_width;

		var parent = prev_div.parentNode;
		var right_width = parent.offsetWidth - current_width - 4;
		if (right_width<min_right_width) right_width = min_right_width - 4;
		
	//	$('#ds_tree_').html(right_width);
		
		next_div.style.width = right_width + 'px';
		
	//	alert(right_width);
		
		prev_div.style.width = current_width + 'px';
		
		

		
		return false;
	}
}

function div_expander_v_up(){
	// Восстанавливаем обработчики.
	document.onmousemove=old_onmousemove;
	document.onmouseup=old_onmouseup;
	// Значения по умолчанию.
	start_x = 0;
	start_width = 0;
	current_width = 0;
	right_width = 0;
	old_onmousemove = null;
	old_onmouseup = null;
	prev_div = null;
	next_div = null;
}

function sk_checkbox_click(name,id){
	if($('#state_'+id).hasClass('checked')){
		$('#state_'+id).removeClass('checked');
		$('#'+id).attr('name','');
	}else{
		$('#state_'+id).addClass('checked');
		$('#'+id).attr('name',name);
	}
}
 
function test1(){
if ((document.getElementById("cap").value!='')&&(document.getElementById("f-name1").value!='')&&(document.getElementById("n-tel1").value!='')&&(document.getElementById("c-time1").value!='')&&(document.getElementById("x-text1").value!=''))
document.zayavka_zvonok.send1.disabled=false;
if ((document.getElementById("cap").value=='')||(document.getElementById("f-name1").value=='')||(document.getElementById("n-tel1").value=='')||(document.getElementById("c-time1").value=='')||(document.getElementById("x-text1").value==''))
document.zayavka_zvonok.send1.disabled=true;
}
function call_open(){
document.getElementById('lbOverlay').style.display='block';
}
function call_close(){
document.getElementById('lbOverlay').style.display='none';
}
