function check_del_block(cid, id, type)
{
	if(confirm("Удалить этот блок?"))
	{
		location="/?c=content&cid=" + cid + "&delid=" + id + "&type=" + type;
	}
	else
	{
		return false;
	}
}

function check_form(form, elementList)
{	
	if(elementList != '*')
	{
		var elementArray = elementList.split(',');
	}
	
	for(i=0; i<form.length; i++)
	{
		check = false;
		currentElement = form.elements[i];

		if(elementList != '*')
		{
			for(q=0; q<elementArray.length; q++)
			{
				if(elementArray[q] == currentElement.name)
				{
					check = true;
				}
			}
		}
		else
		{
			check = true;
		}
		
		if(check)
		{
			if(currentElement.tagName == "INPUT" && currentElement.type == "text" && currentElement.value == "")
			{
				alert("Не указано значение");
				currentElement.focus();
				return false;
			}
			else if(currentElement.tagName == "SELECT" && currentElement.value == -1)
			{
				alert("Не выбрано значение");
				currentElement.focus();
				return false;
			}
		}
	}
}

function check_folder_new(form)
{
	if(form.newName.value == '' && form.newAddfolder.value == -1)
	{
		alert('Не указан раздел');
		return false;
	}
}

function check_file_new(form)
{
	if(form.newFile.value == '')
	{
		alert('Не выбран файл');
		form.newFile.focus();
		return false;
	}
	else if(form.newName.value == '')
	{
		alert('Не указано наименование');
		form.newName.focus();
		return false;
	}
}

function check_file_del(fileID)
{
	if(confirm('Удалить файл?'))
	{
		location = '/?c=files&f=1&delid=' + fileID;
	}
	return false;
}

function showSubmenu(sectionID)
{
	submenu = document.getElementById("submenu" + sectionID);
	
	if(submenu)
	{
//alert(overDiv.style.visibility);
		submenu.style.display = "block";
	}
	
}

function hideSubmenu(sectionID)
{
	submenu = document.getElementById("submenu" + sectionID);
	if(submenu)
	{
//alert(submenu);
		submenu.style.display = "none";
	}
}

function check_products_new(form)
{
	if(form.newName.value == '')
	{
		alert('Не указано наименование');
		form.newName.focus();
		return false;
	}
}

function check_products_edit(form)
{
	if(form.editName.value == '')
	{
		alert('Не указано наименование');
		form.editName.focus();
		return false;
	}
}

function check_form(form, elementList)
{
	if(elementList == undefined)
	{
		elementList = '*';
	}
	
	if(elementList != '*')
	{
		var elementArray = elementList.split(',');
	}
	
	for(i=0; i<form.length; i++)
	{
		check = false;
		currentElement = form.elements[i];

		if(elementList != '*')
		{
			for(q=0; q<elementArray.length; q++)
			{
				if(elementArray[q] == currentElement.name)
				{
					check = true;
				}
			}
		}
		else
		{
			check = true;
		}
		
		if(check)
		{
			if(currentElement.tagName == "INPUT" && currentElement.type == "text" && currentElement.value.replace(/\s+/, '') == '')
			{
				alert("Не указано значение");
				currentElement.focus();
				return false;
			}
			else if(currentElement.tagName == "SELECT" && currentElement.value == -1)
			{
				alert("Не выбрано значение");
				currentElement.focus();
				return false;
			}
		}
	}
}

function show_add()
{
	if(newForm.style.display == 'none')
	{
		newForm.style.display = 'block';
	}
	else
	{
		newForm.style.display = 'none';
	}
	
	return false;
}

function switchSubmenu(sectionID, submenuName)
{
	submenu = document.getElementById(submenuName + sectionID);
//alert(submenu);

	if(submenu)
	{
		if(submenu.style.display == 'none')
		{
	//alert(submenu.style.display);
			submenu.style.display = "block";
		}
		else
		{
			submenu.style.display = "none";
		}
		
		return false;
	}
}

function recolor(row, color, checkboxID)
{
	if(checkboxID == undefined)
	{
		row.style.background = color;
	}
	else
	{
		checkbox = document.getElementById(checkboxID);
	
		if(checkbox.disabled == false)
		{
			if(checkbox.checked == false)
			{
				row.style.background = color;
			}
		}
	}
}

function showImage(fileID)
{
	window.open("/includes/showImage.inc.php?browse=" + fileID,"showImage",'width=580,height=400,status=yes,menubar=no,resizable=yes,scrollbars=yes,left='+String((screen.width-580)/2)+',top=0');
}

function insert_text(fieldName, open, close, no_focus)
{
	msgfield = document.getElementById(fieldName + '_id');

    var bSelStart = msgfield.selectionStart, text;
    var st = msgfield. scrollTop, sh = msgfield. scrollHeight;

    // IE support
    if (document.selection && document.selection.createRange && !bSelStart && msgfield.caretPos)
    {
        text = open;
        if (close != "") text += document.selection.createRange().text;
        text += close;
        msgfield.caretPos.text = text;
    }

    // Moz support
    else if (bSelStart || msgfield.selectionStart == '0')
    {
        var startPos = msgfield.selectionStart;
        var endPos = msgfield.selectionEnd;
        text = msgfield.value.substring(0, startPos) + open;
        if (close != "") text += msgfield.value.substring(startPos, endPos);
        text += close + msgfield.value.substring(endPos, msgfield.value.length);
        msgfield.value = text;
		endPos = close. length? endPos: startPos;
		msgfield.selectionStart = endPos + open.length + close.length;
		msgfield.selectionEnd = endPos + open.length + close.length;
    }

    // Fallback support for other browsers
    else
    {
        msgfield.value += open + close;
    }
    msgfield. scrollTop = st + msgfield. scrollHeight - sh;
    if (no_focus != '1' ) msgfield.focus();
    return;
}

function setCaret (textObj)
{
        if(textObj.createTextRange)
        {
	        textObj.caretPos = document.selection.createRange().duplicate();
	        operastart=textObj.selectionEnd;
		}
}

function insert_image(fieldName)
{
	imageID = document.getElementById('imageID_' + fieldName).value;
	imageAlign = document.getElementById('imageAlign_' + fieldName).value;
	
	if(imageID == -1)
	{
		alert('Не выбрана картинка');
		return false;
	}
	text = '[img align=' + imageAlign + ']' + imageID + '[/img]';
	insert_text(fieldName, text, '');
}

function showActual(actualID)
{
	window.open("/includes/showActual.inc.php?browse=" + actualID,"showActual",'width=580,height=400,status=yes,menubar=no,resizable=yes,scrollbars=yes,left='+String((screen.width-580)/2)+',top=0');
	return false;
}
