function loadFrontpageContent(make_id)
{
	if (document.getElementById('frontpageControlContainer') != null)
	{
		var dt = new Date();
		//$('#frontpageControlContainer').slideUp('fast');
		$.get('Controls/Content/Frontpage.aspx?make_id='+make_id + '&avoidcache=' + dt.getTime(), function(data){
			document.getElementById('frontpageControlContainer').innerHTML = data;
			//$('#frontpageControlContainer').slideDown('fast');
		});
	}
}

/**
 * Make selection changed in vehicle edit page. Populate Model dropdown accordingly
 * @param selMake	- Make dropdown
 */
function chMake(selMake, selModelId)
{
	return chMake(selMake, selModelId, false);
}

function chMake(selMake, selModelId, allowUnselected)
{
	selModel = document.getElementById(selModelId);

	// Clear options
	while (selModel.options.length > 0)
		selModel.options[0] = null;
	
	selModel.options[0] = new Option("Loading...", "-1");

	var rescontext = { "sel": selModel, "allowUnselected": allowUnselected };

	var makeId = selMake.options[selMake.options.selectedIndex].value;
	if (makeId == '') makeId = -1;

	$.getJSON('/Ajax' + _mvc + '/GetModelsByMakeId?makeId=' + makeId,
		function(data)
		{
			// Clear options
			while (selModel.options.length > 0)
				selModel.options[0] = null;

			if (allowUnselected)
				selModel.options[0] = new Option(langvars['Common.Unselected'], '-1');

			if ((data.error) || (data.data == null) || (data.data.length == 0))
			{
				selModel.options[0] = new Option(langvars['Common.Unselected'], '-1');
			}
			else
			{
				for (i = 0; i < data.data.length; i++)
				{
					parts = data.data[i].split(';');
					selModel.options[selModel.options.length] = new Option(parts[1], parts[0]);
				}
			}
			if (data.msg)
				alert(data.msg);
		});	
}

/**
 * Category selection changed in vehicle edit page. Populate Body dropdown accordingly
 * @param selCategory	- Category dropdown
 */
function chCategory(selCategory)
{
	selBody = document.getElementById('ucMainLayout_ucContentSection_contentControl_ucGeneral_selBody');

	// Clear options
	while (selBody.options.length > 0)
		selBody.options[0] = null;

	selBody.options[0] = new Option("Loading...", "-1");

	$.getJSON('/Ajax' + _mvc + '/GetBodiesByCategoryId?categoryId=' + selCategory.options[selCategory.options.selectedIndex].value,
		function(data)
		{
			// Clear options
			while (selBody.options.length > 0)
				selBody.options[0] = null;
		
			if ((data.error) || (data.data == null) || (data.data.length == 0))
			{
				selBody.options[0] = new Option(langvars['Common.Unselected'], '-1');
			}
			else
			{
				for (i = 0; i < data.data.length; i++)
				{
					parts = data.data[i].split(';');
					selBody.options[selBody.options.length] = new Option(parts[1], parts[0]);
				}
			}
			if (data.msg)
				alert(data.msg);
		});	
}

/**
 * Send article to friend function
 * @param aId		- article ID
 * @param rootUrl	- root url of current site
 */
function submitSendArticleToFriend(aId, rootUrl)
{
	if (document.getElementById("stfButton").disabled) return;
	
	var stfField = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfField");
	var stfEmail = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfEmail");
	var stfMyName = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfMyName");
	var stfMyEmail = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfMyEmail");
	
	// Validate
	if (stfField.value.length > 0) { return false; }
	if (stfEmail.value.length < 1) { alert(stfEmail.getAttribute("valmsg")); stfEmail.focus(); return; }
	if (stfMyName.value.length < 1) { alert(stfMyName.getAttribute("valmsg")); stfMyName.focus(); return; }
	if (stfMyEmail.value.length < 1) { alert(stfMyEmail.getAttribute("valmsg")); stfMyEmail.focus(); return; }

	document.getElementById("stfButton").disabled = true;

	var params = '?to=' + encodeURIComponent(stfEmail.value);
	params += '&from=' + encodeURIComponent(stfMyEmail.value);
	params += '&name=' + encodeURIComponent(stfMyName.value);
	params += '&articleId=' + encodeURIComponent(aId);
	params += '&rootUrl=' + encodeURIComponent(rootUrl);
	
	$.post('/Ajax' + _mvc + '/SendArticleToFriend' + params,
		function(data)
		{
			if (!data.error)
			{
				document.getElementById("stfButton").disabled = false;
				hs.close(document.getElementById('closeSendToFriend'));
			}
			if (data.msg)
				alert(data.msg);
		}, 'json');	
}


/**
 * Send ad to friend function
 * @param vId		- vehicle ID
 * @param rootUrl	- root url of current site
 */
function submitSendToFriend(vId, rootUrl)
{
	if (document.getElementById("stfButton").disabled) return;
	
	var stfField = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfField");
	var stfEmail = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfEmail");
	var stfMyName = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfMyName");
	var stfMyEmail = document.getElementById("ucMainLayout_ucContentSection_contentControl_stfMyEmail");
	
	// Validate
	if (stfField.value.length > 0) { return false; }
	if (stfEmail.value.length < 1) { alert(stfEmail.getAttribute("valmsg")); stfEmail.focus(); return; }
	if (stfMyName.value.length < 1) { alert(stfMyName.getAttribute("valmsg")); stfMyName.focus(); return; }
	if (stfMyEmail.value.length < 1) { alert(stfMyEmail.getAttribute("valmsg")); stfMyEmail.focus(); return; }
	
	document.getElementById("stfButton").disabled = true;
	
	var params = '?to=' + encodeURIComponent(stfEmail.value);
	params += '&from=' + encodeURIComponent(stfMyEmail.value);
	params += '&name=' + encodeURIComponent(stfMyName.value);
	params += '&vehicleId=' + encodeURIComponent(vId);
	params += '&rootUrl=' + encodeURIComponent(rootUrl);

	$.post('/Ajax' + _mvc + '/SendToFriend' + params,
		function(data)
		{
			if (!data.error)
			{
				document.getElementById("stfButton").disabled = false;
				hs.close(document.getElementById('closeSendToFriend'));
			}
			if (data.msg)
				alert(data.msg);
		}, 'json');	
}


/**
 * Ask seller for info
 * @param vId		- vehicle ID
 * @param rootUrl	- root url of current site
 */
function submitAskForInfo(vId, rootUrl)
{
	if (document.getElementById("afiButton").disabled) return;
	
	var afiField = document.getElementById("ucMainLayout_ucContentSection_contentControl_afiField");
	var afiMyName = document.getElementById("ucMainLayout_ucContentSection_contentControl_afiMyName");
	var afiMyEmail = document.getElementById("ucMainLayout_ucContentSection_contentControl_afiMyEmail");
	var afiText = document.getElementById("afiText");
	
	// Validate
	if (afiField.value.length > 0) { return false; }
	if (afiMyName.value.length < 1) { alert(afiMyName.getAttribute("valmsg")); afiMyName.focus(); return; }
	if (afiMyEmail.value.length < 1) { alert(afiMyEmail.getAttribute("valmsg")); afiMyEmail.focus(); return; }	
	
	document.getElementById("afiButton").disabled = true;

	var params = '?fromEmail=' + encodeURIComponent(afiMyEmail.value);
	params += '&name=' + encodeURIComponent(afiMyName.value);
	params += '&text=' + encodeURIComponent(afiText.value);
	params += '&vehicleId=' + encodeURIComponent(vId);
	params += '&rootUrl=' + encodeURIComponent(rootUrl);

	$.post('/Ajax' + _mvc + '/AskForInfo' + params,
		function(data)
		{
			if (!data.error)
			{
				document.getElementById("afiButton").disabled = false;
				hs.close(document.getElementById('closeAskForInfo'));
			}
			if (data.msg)
				alert(data.msg);
		}, 'json');	
}


/**
 * Send lease app email
 * @param rootUrl	- root url of current site
 */
function submitSendLvLeaseApp(vId, rootUrl)
{
	if (document.getElementById("lafButton").disabled) return;
	
	var lafType = document.getElementById("leaseAppType").value;
	var lafName = document.getElementById("unm").value + ' ' + document.getElementById("usnm").value;
	var lafIdCode = document.getElementById("upk1").value + '-' + document.getElementById('upk2').value;
	var lafPhone = document.getElementById("uph").value;
	var lafEmail = document.getElementById("uem").value;
	var lafEmployer = document.getElementById("uwork").value;
	var lafPosition = document.getElementById("uprof").value;
	var lafWorkperiod = document.getElementById("ust").value;
	var lafIncome = document.getElementById("usumma").value + ' ' + document.getElementById('uvaluta').options[document.getElementById('uvaluta').selectedIndex].value;

	var lafMake = document.getElementById("amarka").value;
	var lafModel = document.getElementById("amodel").value;
	var lafYear = document.getElementById("ayear").value;
	var lafPrice = document.getElementById("summa").value + ' ' + document.getElementById('valuta').options[document.getElementById('valuta').selectedIndex].value;

	document.getElementById("lafButton").disabled = true;

	var params = '?lafType=' + encodeURIComponent(lafType);
	params += '&lafName=' + encodeURIComponent(lafName);
	params += '&lafIdCode=' + encodeURIComponent(lafIdCode);
	params += '&lafPhone=' + encodeURIComponent(lafPhone);
	params += '&lafEmail=' + encodeURIComponent(lafEmail);
	params += '&lafEmployer=' + encodeURIComponent(lafEmployer);
	params += '&lafPosition=' + encodeURIComponent(lafPosition);
	params += '&lafWorkperiod=' + encodeURIComponent(lafWorkperiod);
	params += '&lafIncome=' + encodeURIComponent(lafIncome);
	params += '&lafMake=' + encodeURIComponent(lafMake);
	params += '&lafModel=' + encodeURIComponent(lafModel);
	params += '&lafYear=' + encodeURIComponent(lafYear);
	params += '&lafPrice=' + encodeURIComponent(lafPrice);
	params += '&vehicleId=' + encodeURIComponent(vId);
	params += '&rootUrl=' + encodeURIComponent(rootUrl);

	$.post('/Ajax' + _mvc + '/SendLvLeaseApp' + params,
		function(data)
		{
			if (!data.error)
			{
				document.getElementById("lafButton").disabled = false;
				hs.close(document.getElementById('closeLeaseAppForm'));
			}
			if (data.msg)
				alert(data.msg);
		}, 'json');	
}