////////////////////////////////////////////////////////////////////
//					 				 JavaScript Functions			//
//											ver. 2.0				//
//										Author: Alex Kvak			//
////////////////////////////////////////////////////////////////////

// initializing a browser
var aks_isIE=0,aks_isNN=0, aks_isMF=0;
if (navigator.userAgent.indexOf("Firefox")!=-1) aks_isMF=1;
if	(document.all) 
	aks_isIE=1;
else 
	if(document.layers) 
		aks_isNN=1;
var aks_isOpera = (navigator.userAgent.indexOf("Opera") > 0);
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
/*
	Sets active OPTION in SELECT element
	@param tag_id 	id of the SELECT element
	@param value 	value of OPTION element to be selected
*/
//////////////////////////////////////////////////////////////
function aks_set_select_value(tag_id,value) {
	if (!(obj = $(tag_id))) return false;
	var lo_len = obj.options.length;
	var lo = obj.options;

	for(i=lo_len-1;i>=0;i--)
		if (lo[i].value == value)
		{
			lo[i].selected = true;
			break;
		}
	return true;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// return Element by Id 
function aks_getElement(n) {
   if (typeof document.getElementById(n) != "undefined")
	   return document.getElementById(n);
   else
   	   return false;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// return Element by Id 
function $(n) {
	return aks_getElement(n);
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// creates a popUp
function aks_popupWnd(url,title,width,height,scrollbars) {
   if (title == "random")
   	 title = "wnd"+(Math.random() * 10).toString();
	 
   var options = "status=off,menubar=off,height="+height+",width="+width;
   var xtop = (screen.height-height)/2-15, xleft = (screen.width-width)/2;
   options += ",top="+xtop+",left="+xleft;
   
   if ( (scrollbars == "on") || (scrollbars == true) )
   	options += ",scrollbars=1";
   else
   	if (scrollbars == "auto")
		options += ",scrollbars=1";
	else
	   	options += ",scrollbars=0";
   
   var popup = window.open(url,title,options); 
   popup.document.close();
   return popup;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// changes the CheckBox flag of Element num
function aks_changeCheck(num) {
	if (obj = aks_getElement(num))
		obj.checked = !obj.checked;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// set the display property of Element num
function aks_setView(num, show) {
	var obj = aks_getElement(num);
	obj.style.display = (show) ? 'block':'none';
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// set the visibility of Element num	
function aks_setVisibility(num, show) {
	var obj = $(num);
    obj.style.visibility = (show) ? "visible":"hidden";
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// returns the display property of Element num			
function aks_getView(num) {
    var obj = $(num);
    return obj.style.display;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// changes the visibility ogf Element num		
function aks_changeView(num) {
    visibility_ = aks_getView(num);
	show = (visibility_ == 'none') ? true : false;
	aks_setView(num, show);
	return true;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// sets the color farbe to all cells of table row obj
function aks_setFarbe(obj,farbe)
{
    theCells = obj.cells;
    var rowCellsCnt = theCells.length;
    for (c = 0; c < rowCellsCnt; c++) 
        theCells[c].style.backgroundColor=farbe;
}
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// sets the color farbe to specified cell obj
function aks_setFarbeCell(obj,farbe) {
     obj.style.backgroundColor = farbe;
}
//////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////

/* CROSS-BROWSER EVENT HANDLER */
function addEvent(obj, evType, fn){
     if (obj.attachEvent){
         var r = obj.attachEvent("on"+evType, fn);
        return r;
     } else if (obj.addEventListener){
         obj.addEventListener(evType, fn, true);
         return true;
     } else {
        return false;
     }
}

////////////////////////
// добавление обработчиков к строкам таблицы
function setHandlers(id,title_) {
	var obj;
	if (!(obj = $(id))) return false;

	theRows = obj.rows;
	for(var i=1;i<theRows.length-2;i++) {
		theRows[i].onmouseover = function () { this.className = 'tabelle_hover'; }
		theRows[i].onmouseout  = function () { this.className = 'tabelle_normal';}

		theRows[i].kod = theRows[i].title;
		theRows[i].onclick  = function (e) { 
		    var evt = (e) ? e : event;
		    var elem = (evt.target) ? evt.target : ((evt.srcElement) ? 
		        evt.srcElement : null);
			if (elem.cellIndex < this.cells.length-1)
				showGood(this.kod);
		};
		theRows[i].title = (typeof title_ != "undefined") ? title_ : 'детали';
	}
}
////////////////////////

////////////////////////
// проверка значений формы добавления данных о пользлователе
function checkNewUserForm() {
	var elem = $('organization');
	/*if (elem.value == '') {
		alert('Введите организацию'); elem.focus(); return false;
	}*/
	elem = $('name');
	if (elem.value == '') {
		alert('Введите имя'); elem.focus(); return false;
	}
	elem = $('address');
	if (elem.value == '') {
		alert('Введите адрес'); elem.focus(); return false;
	}
	elem = $('telefon');
	if (elem.value == '') {
		alert('Введите телефон'); elem.focus(); return false;
	}
	elem = $('email');
	if (!aks_isEmail(elem.value)) {
		alert('Неверный электронный адрес'); elem.focus(); return false;
	}
	elem = $('login');
	if (elem.value == '') { 
		alert('Введите логин'); elem.focus(); return false;
	}
	elem = $('pass1');
	if (elem.value == '') {
		alert('Введите пароль'); elem.focus(); return false;
	}
	elem = $('pass2');
	if (elem.value == '') { 
		alert('Введите пароль пользователя'); elem.focus(); return false;
	}
	if ($('pass1').value != $('pass2').value) {		
		alert('Пароли не совпадают'); $('pass2').focus(); return false;
	}
	return true;	
}
////////////////////////

////////////////////////
function setAllCheckBoxes() {
	var inputs = document.getElementsByTagName("INPUT");
	for(var i=0;i<inputs.length;i++) {
		if (inputs[i].type == "checkbox") 
			inputs[i].checked = true;
	}
}
////////////////////////

////////////////////////
function clearAllCheckBoxes() {
	var inputs = document.getElementsByTagName("INPUT");
	for(var i=0;i<inputs.length;i++) {
		if (inputs[i].type == "checkbox") 
			inputs[i].checked = false;
	}
}
////////////////////////

////////////////////////
function checkCheckBoxes() {
	var inputs = document.getElementsByTagName("INPUT");
	for(var i=0;i<inputs.length;i++) {
		if ( (inputs[i].type == "checkbox") 
			&& inputs[i].checked) return true;
	}
	return false;
}
////////////////////////

////////////////////////
function confirmAction() {
	if ($('kataloglist_action').value == "delete")
		return confirm("Вы действительно хотите удалить эти записи?");
	return true;
}

function checkLoginForm() {
	var elem = $('login');
	if (elem.value == '') {
		alert('Введите логин'); elem.focus(); return false;
	}
	elem = $('password');
	if (elem.value == '') {
		alert('Введите пароль'); elem.focus(); return false;
	}
	return true;
}

function showGood(id_good) {
	location.href = '?id_good='+id_good;
}

function checkCount(n) {
	if ($(n)) {
		return aks_checkInteger(n,'Количество');
	}
	else return false;
}

// sets the color farbe to specified cell obj
function aks_goToURL(url) {
     location.href = url;
}


function toggleFilter(div_,img_) {
	var visibility_ = aks_getView(div_);
	show = (visibility_ == 'none') ? true : false;
	aks_setView(div_,show);
	$(img_).src = (show) ? 'img/s_desc.png' : 'img/arrow.png'; 
}

function checkFAQ() {
	var elem = $('fio');
	if (elem.value == '') {
		alert('Введите ФИО'); elem.focus(); return false;
	}
	elem = $('email');
	if (!aks_isEmail(elem.value)) {
		alert('Неверный электронный адрес'); elem.focus(); return false;
	}
	elem = $('question');
	if (elem.value == '') {
		alert('Введите вопрос'); elem.focus(); return false;
	}
	var elem = $('code');
	if (elem.value == '') {
		alert('Введите код подтверждения'); elem.focus(); return false;
	}
	return true;
}

