function close_div(id){
	elem = document.getElementById(id);
	if (elem)
		elem.style.display = 'none';
}

function getPageCoords() {
	var coords = [0, 0, 0]; // height of window, document, scroll pos
	// all except IE
	if (window.innerHeight) {
		coords[0] = window.innerHeight;
		coords[2] = window.pageYOffset;
	}
	// IE 6 Strict
	else if (document.documentElement && document.documentElement.clientHeight != 0) {
		coords[0] = document.documentElement.clientHeight;
		coords[2] = document.documentElement.scrollTop;
	}
	else if (document.body) {
		coords[0] = document.body.clientHeight;
		coords[2] = document.body.scrollTop;
	}

	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) {
		coords[1] = document.body.scrollHeight;
	} else {
		coords[1] = document.body.offsetHeight;
	}
	if (coords[1] < coords[0]) coords[1] = coords[0];

	return coords;
}

function openCenteredWindow(id) {
		
	elem = document.getElementById(id);
	if (elem){
	if (elem.style.display=='block')
	{
		var pageCoords = getPageCoords();
	
		elem.style.top = parseInt(pageCoords[2] + (pageCoords[0] - 55) / 2 - 150) + 'px';
	}
}
}


function show_elem(id) {
	elem = document.getElementById(id);
	if (elem)
		elem.style.display = 'block';
}	


window.onscroll = function () {openCenteredWindow('dd');};

function showCallback(id) {
    var cb=document.getElementById(id);
    if (cb.style.display=="none")
    {
    	cb.style.display="block";
    	cb.style.visibility="visible";
    
    } else
    {
    	cb.style.display="none";
    	cb.style.visibility="hidden";
    }
}

function send_callback(num) {
    
    var fio=document.getElementById('callback_fio'+num).value;
	var phone=document.getElementById('callback_phone'+num).value;
	var comment = document.getElementById('callback_comment'+num).value;
    
    if (fio != '' && phone != '')
    {
        showCallback('callback_form'+num);
        showCallback('callback_link'+num);
        
        document.getElementById('callback_complete'+num).innerHTML="Отправка...";
        document.getElementById('callback_complete'+num).style.visibility='visible';			
    	document.getElementById('callback_complete'+num).style.display='block';
        
        
        comment = comment.replace(/\r\n|\r|\n/g,"<br>"); // перенос строки
        
        
        var url = '/set_callback.html?id='+num + '&fio=' + fio + '&phone=' + phone + '&comment=' + comment;
     	ajax_load(url, 'callback_complete'+num);
    }
    else
    {
        alert('Заполните пожалуйста поля \"Имя\" и \"Контактный телефон\"');
    }
   return false;
}

function bindSEvent(obj, eventName, func) {
	
		if (document.attachEvent)
			obj.attachEvent("on"+eventName, func);
		else if (document.addEventListener) {
			obj.addEventListener(eventName, func, 0);
		}
	
}

function isIE6(){
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer"){
		var b_version = navigator.appVersion;
		var re = /\MSIE\s+(\d\.\d\b)/;
		var res = b_version.match(re);
		if (res[1] <= 6){
			return true;
		}
	}
  return false;
}

function getWidth(id) {
	
	return (document.getElementById(id).scrollWidth > document.getElementById(id).offsetWidth)?document.getElementById(id).scrollWidth:document.getElementById(id).offsetWidth;
		
}

function getWidthByObj(obj) {
	
	return (obj.scrollWidth > obj.offsetWidth)?obj.scrollWidth:obj.offsetWidth;
		
}

function getSumWidth(els) {
	
	var sumWidth = 0;
	$$(els).each(function(elm) {
		sumWidth = sumWidth + getWidthByObj(elm);
	});
	
	return sumWidth;
	
}

var setWidth = function() {

    var _c = document.getElementById('content').offsetWidth;
    var tables = $$("#content table.web_ar_datagrid" );
    
    for(var i=0; i<tables.length; i++) {
        
        var _o =  tables[i];
        var _w = _o.clientWidth;   
        if (_w>_c){
			document.getElementById('wrapper').style.minWidth = (_w+280)+'px';
			if (isIE6()) {
				document.getElementById('wrapper').style.width = (_w+280)+'px';
			}
        }
    }

}

function check_answer(id) {
	$$('.check_answer').each(function(el) {    
		el.set('src','/images/cb.png');
	});
	$('check_answer_'+id).set('src','/images/cb_checked.png');
	$('golst_golan_id').value = id;
}


window.addEvent('domready', function() {
	$$('img').each(function(el) {    
		el.set('src',el.get('src').replace('/_sysimg/add.png','/images/icon_add.png'));
		el.set('src',el.get('src').replace('/_sysimg/delete.gif','/images/icon_del.png'));
	});
	bindSEvent(window, 'load', setWidth);
	bindSEvent(window, 'resize', setWidth);

});

