/*
/ All functions needed for Distributors module 
*/

//
// Check forms integrity
function	check_form	(form)
{
	if (form.form_check.value == '0')
	{
		return true;
	}

	switch	(form.name)
	{
		case	"login_form":
			// check email not empty
			if (form.email.value	==	'')
			{
				alert	('Email field is mandatory !');
				return false;
			}
			// Check password not empty
			if (form.password.value	==	'')
			{
				alert	('Password field is mandatory !');
				return false;
			}
			break;
		case	"update_news_item":
			// Domain must be selected ##
			var	domain	=	document.getElementById('user_domains');
			var	selected_index	=	domain.selectedIndex;
			if (selected_index == -1)
			{
				alert("You must select domain (or all) for news item !!! ");
				return false;
			}

			// Check for news title
			if (form.title.value	==	'')
			{
				alert	('You must fill in the news title !');
				return false;
			}

			break;
			case	"update_dealer":
				if (form.title.value	==	'')
				{
					alert	('You must fill in the dealer title !');
					return false;
				}
				break;
			case	"update_img":
				// domain must be selected
				var	domain	=	document.getElementById('user_domains');
				var	selected_index	=	domain.selectedIndex;
				if (selected_index == -1)
				{
					alert("You must select domain for image item !!! ");
					return false;
				}

				// Description must be filled
				if (form.description.value	==	'')
				{
					alert('Description must be filled !');
					return false;
				}

				switch	(form.action.value)
				{
					case	"add_img":
						if (form.filename.value	==	'')
						{
							alert('You must select file for upload !');
							return false;
						}
						break;
					case	"update_img":
						break;
				}
				break;
	}

	return true;
}

/*
/ Show the object by id
*/
function	show_hidden	(id)
{
	var	obj	=	document.getElementById(id);
	obj.style.display	=	'block';
}

function	set_img_id_to_img_list_id	(form)
{
	var	img_list_obj	=	form.img_list;
	var	index			=	img_list_obj.selectedIndex;
	var	img_list_id		=	img_list_obj.options[index].value;

	form.img_id.value	=	img_list_id;
}

function	open_img_window	(form,type)
{
	var	module	=	form.module.value;
	var	action	=	form.action.value;

	switch	(module)
	{
		case	"news":
			var	id	=	form.news_id.value;
			var	logo_obj	=	form.logo;
			var	index		=	logo_obj.selectedIndex;
			var	logo_id		=	logo_obj.options[index].value;

			var	url	= "img_module=" + module + "&item_id=" + id + "&item_id_name=news_id&back_action=" + action + "_form";
			if (logo_id > 0 && type == 'update')
			{
				url += "&img_id=" + logo_id ;
			}
			break;
	}

	url = '/distributors_admin/?module=image_manager&' + url;

//	alert(url);
	// Redirect to image manager
	window.location = url;
}

