function searchUser(name, resultId, tableId, rowName){
	$.ajax({
		url: 'ajax/ajax_controller.php',
		type: 'POST',
		data: {model: 'search_user', search: name, table_id: tableId, row_name: rowName},
		dataType: 'html',
		success: function(data){
			InsertUser(resultId, data);
		}
	});
}
function searchUserGroup(name, group, resultId, tableId, rowName){
	$.ajax({
		url: 'ajax/ajax_controller.php',
		type: 'POST',
		data: {model: 'search_user', search: name, group: group, table_id: tableId, row_name: rowName},
		dataType: 'html',
		success: function(data){
			insertUser(resultId, data);
		}
	});
}
function insertUser(resultId, data){
	$(resultId).html(data);
}
function addUser(rowName, insertId, userId, userName){
	var html;
	if (!$('#' + rowName + '_' + userId).hasClass('OK')){
		html = '<tr><td class="new">' + userName + '</td><td><input class="OK" type="checkbox" id="' + rowName + '_' + userId + '" name="' + rowName + '[' + userId + ']" checked></td></tr>'
		$(insertId).append(html);
	}
}
/*http://genpas.narod.ru (c)*/
function genPass(length){
	var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
	var res = '';
	var r;
	var i;
	for (i = 1; i <= length; i++){
		r = Math.floor(Math.random() * chars.length);
		res += chars.substring(r, r + 1);
	}
	res = res.replace('&', '&amp;');
	res = res.replace('>', '&gt;');
	res = res.replace('<', '&lt;');
	return res;
}
function genString(resultId, length){
	$(resultId).val(genPass(length));
}
function OnCheckDisplay(displayId, checkboxId){
	e = document.getElementById( displayId );
	if (checkboxId.checked){
		ShowArea(displayId)
	}
	else {
		HideArea(displayId)
	}
}
function OnCheckHide(displayId, checkboxId){
	e = document.getElementById(displayId);
	if (checkboxId.checked){
		HideArea(displayId)
	}
	else {
		ShowArea(displayId)
	}
}
function OnCheckDisable(inputId, checkboxId){
	if (checkboxId.checked){
		inputId.disabled = true;
	}
	else {
		inputId.disabled = false;
	}
}
function changeImg(resultId, value){
	if (document.images){
		$(resultId).attr('src', value);
	}
}
function ShowArea(displayID){
	display = document.getElementById(displayID);
	display.style.visibility = "visible";
	display.style.display = "block";
}
function HideArea(displayID){
	display = document.getElementById(displayID);
	display.style.visibility = "hidden";
	display.style.display = "none";
}
function ToggleHide(displayID, linkID, showLink, hideLink){
	display = document.getElementById(displayID);
	if (linkID){
		link = document.getElementById(linkID);
	}
	if (display.style.display == "none")	{
		if (linkID){
			link.innerHTML = hideLink;
		}
		ShowArea(displayID);
	}
	else
	{
		if (linkID){
			link.innerHTML = showLink;
		}
		HideArea(displayID)
	}
}
function checkAll(oForm, cbName, checked){
	if (oForm[cbName].length){
		for (var i=0; i < oForm[cbName].length; i++){
			if (oForm[cbName][i].disabled == false){
				oForm[cbName][i].checked = checked;
			}
		}
	}
	else if (oForm[cbName].disabled == false){
		oForm[cbName].checked = checked;
	}
}
function CheckSelect(inputId, BadValue, ReplaceWith){
	if (inputId.value == BadValue){
		inputId.value = ReplaceWith;
	}
}
function sprintf(){
	if (!arguments || arguments.length < 1 || !RegExp){
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str)){
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		numMatches++;
		if (pType == '%'){
			subst = '%';
		}
		else {
			numSubstitutions++;
			if (numSubstitutions >= arguments.length){
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			if (pPad && pPad.substr(0,1) == "'"){
				pad = leftpart.substr(1,1);
			}
			else if (pPad){
				pad = pPad;
			}
			var justifyRight = true;
			if (pJustify && pJustify === "-"){
				justifyRight = false;
			}
			var minLength = -1;
			if (pMinLength){
				minLength = parseInt(pMinLength);
			}
			var precision = -1;
			if (pPrecision && pType == 'f'){
				precision = parseInt(pPrecision.substring(1));
			}
			var subst = param;
			if (pType == 'b'){
				subst = parseInt(param).toString(2);
			}
			else if (pType == 'c'){
				subst = String.fromCharCode(parseInt(param));
			}
			else if (pType == 'd'){
				subst = parseInt(param) ? parseInt(param) : 0;
			}
			else if (pType == 'u'){
				subst = Math.abs(param);
			}
			else if (pType == 'f'){
				subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			}
			else if (pType == 'o'){
				subst = parseInt(param).toString(8);
			}
			else if (pType == 's'){
				subst = param;
			}
			else if (pType == 'x'){
				subst = ('' + parseInt(param).toString(16)).toLowerCase();
			}
			else if (pType == 'X'){
				subst = ('' + parseInt(param).toString(16)).toUpperCase();
			}
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}
function MarkCheckboxFromGroup(formId, checkboxId, checkboxValue, inputOld, inputNew){
	if (inputOld != inputNew){
		if (formId[checkboxId].length){
			for (var i=0; i < formId[checkboxId].length; i++){
				if (formId[checkboxId][i].value == checkboxValue){
					formId[checkboxId][i].checked = true; break;
				}
			}
		}
		else {
			formId[checkboxId].checked = true;
		}
	}
}
function inputPlaceHolder(e, value, css){
	if ($(e).val() == ''){
		$(e).val(value).addClass(css);
	}
	$(e)
		.focus(function(){
		$(this).removeClass(css);
		if ($(this).val() == value){
			$(this).val('');
		}
	})
	.blur(function(){
		if ($(this).val() == value || $(this).val() == ''){
			$(this).addClass(css);
			$(this).val(value);
		}
	});
}

function checkPermissions(form, array){
	var parent;
	var t = 0;
	if ($.browser.msie){
		t = 250;
	}
	setTimeout(function(){
		for (permission in array) {
			parent = array[permission];
			if ($(form).find('#' + parent).attr('checked') == false){
				$(form).find('#' + permission).attr('disabled', true);
				$(form).find('#' + permission).attr('checked', false);
			}
			else {
				$(form).find('#' + permission).attr('disabled', false);
			}
		}
	}, t);
}