function city_ajax_load(val, type)
{
	if (val != 0)
	{
		query = "city_from="+encodeURIComponent(val)+"&type="+(type);
                Request(query, type);
	}
	else
	{
		document.getElementById("city_to").innerHTML = '<select name="city_to" onchange="this.form.submit();"><option value="0">Выберите город...</option></select>';
	}
}

function Create()
{  
	if (navigator.appName == "Microsoft Internet Explorer")  
		object = new ActiveXObject("Microsoft.XMLHTTP");  
	else
		object = new XMLHttpRequest();  
return object;  
}

function Request(query, type)
{ 
	var req = Create();
	req.open("post", "/schedule/ajax.php", true)
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = function () 
	{
		if (req.readyState == 4 && req.status == 200)
		{
			if (type == 0)
			{
				document.getElementById("city_to").innerHTML = '<select name="city_to" onchange="this.form.submit();"><option value="0">Выберите город...</option>'+req.responseText+'</select>';
			}
                        else if (type == 1)
			{
				document.getElementById("city_to").innerHTML = '<select name="city_to"><option value="0">Выберите город...</option>'+req.responseText+'</select>';
			}
			else
			{
				alert(req.responseText);
			}
		}
	}
	req.send(query);
}

function send_moderation_data(old_value, string, row, id)
{
	if (row == 0) //Trip duration
	{
		regexp = /^(\d\s){1}([\d]|[0-1][\d]|[2][0-3])(:([0-5][\d])){1,2}$/;

		dt = string.split(" ");
		time = dt[1].split(":");
		new_value = parseInt(dt[0], 10)*1440 + parseInt(time[0], 10)*60 + parseInt(time[1], 10);
	}
	if (row == 1) //Price
	{
		regexp = /^\d+((\.|\,)\d+)?$/;

		string = string.replace(',', '.');
		new_value = string*100;
	}
	
	if (regexp.test(string) && (new_value - old_value) && new_value)
	{
		query = "string="+encodeURIComponent(new_value)+"&moderation=true&row="+(row)+"&id="+(id);
		Request(query);
	}
	else { alert("Неверно введено значение!"); }
}

function moderation_time(tr, id, time_in)
{
    if(document.getElementById) 
	{
		while (tr.tagName != "TR")
		{
			tr = tr.parentNode
		}
		
		thisChilds = tr.getElementsByTagName("td");
		
		for (var i = 0; i < thisChilds.length; i++)
		{
			if (thisChilds[i].className == "action")
			{
				thisChilds[i].innerHTML = '<input type="image" src="ok.png" onclick="send_moderation_data('
				+time_in
				+', document.getElementById(\'days\').value+\' \'+document.getElementById(\'time\').value, 0, '
				+id
				+')" title="Изменить" style="cursor: hand;">';
			}
			
			if (thisChilds[i].id == "time_in")
			{
				days = parseInt(time_in/1440, 10);
				hours = parseInt((time_in - days*1440)/60, 10);
				minutes = time_in - days*1440 - hours*60;
				
				thisChilds[i].innerHTML = '<input id="days" type="text" size="1" maxlength="2" value="'+days+
				'"> дней <input id="time" type="text" size="4" maxlength="5" value="'+hours+':'+minutes+'"> чч:мм';
			}
		}
	}
}

function moderation_price(tr, id, cash)
{
	
    if(document.getElementById) 
	{
		while (tr.tagName != "TR")
		{
			tr = tr.parentNode;
		}
		
		thisChilds = tr.getElementsByTagName("td");
		
		for (var i = 0; i < thisChilds.length; i++)
		{
			if (thisChilds[i].className == "action")
			{
				thisChilds[i].innerHTML = '<input type="image" src="ok.png" onclick="send_moderation_data('+cash+', document.getElementById(\'cash\').value, 1, '+id+')" title="Изменить" style="cursor: hand;">';
			}
			
			if (thisChilds[i].id == "price")
			{
				thisChilds[i].innerHTML = '<input id="cash" type="text" size="5" maxlength="6" value="'+(cash/100)+'"> грн.';
			}
		}
	}
}