// JavaScript Document
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var get_time = createObject();
function clock(){
	nocache = Math.random();
	get_time.open('get', 'get_time.php?nocache='+nocache);
	get_time.onreadystatechange = function(){
		if (get_time.readyState == 4){
			var response = get_time.responseText;
			document.getElementById('now').innerHTML = response;
		}
	}
	get_time.send(null);
	setTimeout("clock()",1000)
}

function changeOver(obj){
	obj.innerHTML='Зарегистрироваться'
}

function changeOut(obj){
	obj.innerHTML='Остановить аукцион'
}

function getLotTime(id, lot_id)
{
	var get_lot_time = createObject();
	nocache = Math.random();
	get_lot_time.open('get', 'get_lot_time.php?id='+lot_id+'&nocache='+nocache);
	get_lot_time.onreadystatechange = function(){
		if (get_lot_time.readyState == 4){
			var response = get_lot_time.responseText;
			if(Number(response)!=0){
				if(Number(response)==1){
					
					var lot = document.getElementById('item_'+id)
					lot.parentNode.removeChild(lot);
				}
				document.getElementById('time_'+id).innerHTML = response;
			}
		}
	}
	get_lot_time.send(null);
	setTimeout("getLotTime("+id+","+lot_id+")",1000)
}

function getLotTimeView(lot_id)
{
	var get_lot_time = createObject();
	nocache = Math.random();
	get_lot_time.open('get', 'get_lot_time.php?id='+lot_id+'&nocache='+nocache);
	get_lot_time.onreadystatechange = function(){
		if (get_lot_time.readyState == 4){
			var response = get_lot_time.responseText;
			if(Number(response)==1){
				var lot = document.getElementById('lot')
				var parent = lot.parentNode
				parent.removeChild(lot)
				newDiv = document.createElement('div')
				newDiv.innerHTML = 'Время предназначеное этому лоту истекло и лот был удален.'
				newDiv.setAttribute('style', 'text-align: center; margin-top: 20px;')
				parent.appendChild(newDiv)
			}
		}
	}
	get_lot_time.send(null);
	setTimeout("getLotTimeView("+lot_id+")",3000)
}

function getLotPrice(id, lot_id)
{
	var get_lot_price = createObject();
	nocache = Math.random();
	get_lot_price.open('get', 'get_lot_price.php?id='+lot_id+'&nocache='+nocache);
	get_lot_price.onreadystatechange = function(){
		if (get_lot_price.readyState == 4){
			var response = get_lot_price.responseText;
			if(Number(response)!=0){
				document.getElementById('price_'+id).innerHTML = response;
			}
		}
	}
	get_lot_price.send(null);
	setTimeout("getLotPrice("+id+","+lot_id+")",1000)
}

function getLotAcc(id, lot_id, login)
{
	var get_lot_acc = createObject();
	nocache = Math.random();
	get_lot_acc.open('get', 'get_lot_acc.php?id='+lot_id+'&nocache='+nocache);
	get_lot_acc.onreadystatechange = function(){
		if (get_lot_acc.readyState == 4){
			var response = Number(get_lot_acc.responseText);
			
			var button = document.getElementById('button_'+id)
			var lot = document.getElementById('item_'+id)
			if(response!=0){
				if(button.className == 'stop'){
					button.className = 'nostop'
					button.setAttribute('href', 'javascript:void();')
					button.innerHTML = 'Приостановлен'
					button.setAttribute('onmouseover', '')
					button.setAttribute('onmouseout', '')
					
					lot.setAttribute('style', 'opacity:.5;	filter:alpha(opacity=50)')
				}
			}
			else{
				if(button.className == 'nostop'){
					button.className = 'stop'
					lot.removeAttribute('style')
					if(login==0){
						button.setAttribute('href', 'registration.php')
						button.innerHTML = 'Остановить аукцион'
						button.setAttribute('onmouseover', 'changeOver(this)')
						button.setAttribute('onmouseout', 'changeOut(this)')
					}
					else{
						button.setAttribute('href', 'stop.php?id='+lot_id)
						button.innerHTML = 'Остановить аукцион'
					}
				}
			}
		}
	}
	get_lot_acc.send(null);
	setTimeout("getLotAcc("+id+","+lot_id+","+login+")",5000)
}

function checkLot(id, lot_id)
{
	var check_lot = createObject();
	nocache = Math.random();
	check_lot.open('get', 'check_lot.php?id='+lot_id+'&nocache='+nocache);
	check_lot.onreadystatechange = function(){
		if (check_lot.readyState == 4){
			var response = check_lot.responseText;
			if(Number(response)!=0){
				var lot = document.getElementById('item_'+id)
				lot.parentNode.removeChild(lot);
			}
		}
	}
	check_lot.send(null);
	setTimeout("checkLot("+id+","+lot_id+")",10000)
}

function checkLotView(lot_id, status)
{
	var check_lot = createObject();
	nocache = Math.random();
	check_lot.open('get', 'check_lot.php?id='+lot_id+'&nocache='+nocache);
	check_lot.onreadystatechange = function(){
		if (check_lot.readyState == 4){
			var response = check_lot.responseText;
			if(Number(response)==0 && Number(status)==1){
				window.location.reload()
			}
			if(Number(response)!=0 && Number(status)==0){
				window.location.reload()
			}
		}
	}
	check_lot.send(null);
	setTimeout("checkLotView("+lot_id+", "+status+")",5000)
}

function checkLots(list)
{
	var check_lots = createObject();
	nocache = Math.random();
	check_lots.open('get', 'check_lots.php?list='+list+'&nocache='+nocache);
	check_lots.onreadystatechange = function(){
		if (check_lots.readyState == 4){
			var response = check_lots.responseText;
			if(Number(response)!=0){
				window.location.reload()
			}
		}
	}
	check_lots.send(null);
	setTimeout("checkLots('"+list+"')",10000)
}

function checkTime()
{
	var check_time = createObject();
	nocache = Math.random();
	check_time.open('get', 'check_time.php?nocache='+nocache);
	check_time.send(null);
	setTimeout("checkTime()",10000)
}

function getLots(nr, login, list){
	if(login=='') login=0
	for(var i=0; i<nr; i++){
		var lot_id = document.getElementById('lotid_'+i).value
		getLotTime(i, lot_id)
		getLotPrice(i, lot_id)
		getLotAcc(i, lot_id, login)
		checkLot(i, lot_id)
		checkTime()
		checkLots(list)
	}
}

function get_img(photo){
	document.getElementById('view_img').innerHTML='<img src="photo/'+photo+'" />'
}
