// declare new variables for each new div that you add, and link to the div by ID
// var region_div = document.getElementById("region_div");
// var doc = null;

function ajax(doc) 
{
	doc = null;
	    
	if (window.XMLHttpRequest)
	{
		try
		{
			doc = new XMLHttpRequest();
		}
		catch(e) 
		{
			if(SBDebug) alert("Ajax interface creation failure 1");
		}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			doc = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e)
		{
			try
			{
				doc = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)
			{
				if(SBDebug) alert("Ajax interface creation failure 2");
			}
		}
	}
	return doc;
}

var auction_item_contents;
var Extend_timer = new Array(); 
var Final_timer = new Array(); 
var Extend_status = new Array();
var Old_Bidcounter = new Array();
var Auction_ids="";
var Num_Auctions=0;
var Product_id=0;
var First_Pro_Id=0;
var Waiting_Previous_Call = 0;
// Auto_bidbutler();
setInterval('Update_Bid_Detail()', 1000);

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24911076-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function lookupOffset(pro_id)
{
	if(Auction_ids!="")
	{
		var arraylist = Auction_ids.split(",");

		for(i = 0; i < arraylist.length; i++)
		{
			if(arraylist[i] == pro_id)
				return i;
		}
	}
	return -1;
}

function Signal_Waiting()
{
	if(Auction_ids!="")
	{
		var arraylist=Auction_ids.split(",");

		for(i = 0; i < arraylist.length; i++)
		{
			var msg = document.getElementById('counter_index_div_' + i);
			if(msg)
				msg.innerHTML = '<font size="5">Verbinden..</font>';
		}
	}
}

function Update_Bid_Detail()
{		
	var doc=null;

	if(Waiting_Previous_Call)
	{
		if(Waiting_Previous_Call > 1)
			Signal_Waiting();
		Waiting_Previous_Call++;

		return;
	}
	if (window.XMLHttpRequest)
	{
	        try
		{
        		doc = new XMLHttpRequest();
        	}
		catch(e)
		{
		}
	}
	else if (window.ActiveXObject)
	{
        try
		{
			doc = new ActiveXObject('Msxml2.XMLHTTP');
       	}
		catch(e)
		{
			try
			{
				doc = new ActiveXObject('Microsoft.XMLHTTP');
			}
		  	catch(e)
			{
			}
       	}
	}
	doc.open('GET', "./timer_location.php?section=update_bid&r="+Math.floor(1000000000*Math.random()), true);
	doc.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 

	doc.onreadystatechange = function()
	{ 
		if (doc.readyState == 4)
		{
			Waiting_Previous_Call = 0;
			if (doc.status == 200)
			{
				if(doc.responseText == 0)
					reset_auction_list();
				else
				{
					var arraylist=doc.responseText.split("|@|");
					var ids = "";
					var c = 0;

					Num_Auctions = arraylist.length;
					for (l = 0; l < Num_Auctions; l++)
					{
						var arraylist1 = arraylist[l].split("**");
						var pro_id = parseInt(arraylist1[0]);

						if(arraylist1.length < 15)
						{
							var cbe = document.getElementById("credit_balance");

							if(cbe) cbe.innerHTML = arraylist1[0];
							continue;
						}
						if((Product_id == 0 || Product_id == pro_id))
						{
							add_auction_item(c, pro_id, arraylist1[9], arraylist1[10], arraylist1[11], arraylist1[12],
								arraylist1[13], arraylist1[14], arraylist1[8], arraylist1[15], arraylist1[3]);
							Set_the_Timer(c, pro_id, arraylist1[4], arraylist1[5]);
							c++;
							if(ids != "")
								ids += ",";
							ids += pro_id;
						}
					}

					for(;c < 90; c++)
					{
						clear_auction_item(c);
					}
					
					Auction_ids = ids;
					c = 0;
					for(l=0;l<arraylist.length;l++)
					{
						var arraylist1=arraylist[l].split("**");
						var pro_id = parseInt(arraylist1[0]);
						
						if(arraylist1.length < 15) continue;
						if(Product_id == 0 || Product_id == pro_id)
						{
							if(Extend_status[pro_id]!=arraylist1[2])
								Update_status(c, pro_id, arraylist[l]);

							if(Old_Bidcounter[pro_id] != parseInt(arraylist1[1]))
							{				
								Old_Bidcounter[pro_id] = parseInt(arraylist1[1]);

								if(document.getElementById("price_index_div_" + c))
								{
									setTimeout('document.getElementById("price_index_div_'+c+'").style.backgroundColor = "#B6DFF8"', 10);
									setTimeout('document.getElementById("price_index_div_'+c+'").style.backgroundColor = "#D7EDFB"', 70);
									setTimeout('document.getElementById("price_index_div_'+c+'").style.backgroundColor = "#ECF7FD"', 130);
									setTimeout('document.getElementById("price_index_div_'+c+'").style.backgroundColor = "#ECF7FD"', 230);
									setTimeout('document.getElementById("price_index_div_'+c+'").style.backgroundColor = ""', 320);
								}
							}
							c++;
						}
					}
				}
			}  
		}
	}


	Waiting_Previous_Call = 1;
	doc.send(null);
}


function Set_the_Timer(index, pro_id, final_timer, ext_timer)
{
	if(Extend_timer[pro_id]>0)
	{
		var	elem;

		if(Extend_status[pro_id]=='E')
		{
			elem = document.getElementById('counter_index_div_'+index);
			if(elem) elem.innerHTML = calc_counter_from_time_short(ext_timer, pro_id);
		}
		else if(Extend_status[pro_id]=='N')
		{
			elem = document.getElementById('counter_index_div_'+index);
			if(elem) elem.innerHTML = '' + calc_counter_from_time(final_timer, pro_id);
		}
		else if(Extend_status[pro_id]=='P')
		{
			elem = document.getElementById('counter_index_div_'+index);
			if(elem) elem.innerHTML =  'Pause';
		}
	}
	else
	{
		if(Extend_status[pro_id]=='E' || Extend_status[pro_id]=='S')
		{
			Extend_status[pro_id]='T';
//			Expire_extend_bid(index, pro_id);
		}
		else if(Extend_status[pro_id]=='N')
		{
			Final_timer[pro_id] = parseInt(final_timer);
			
			if(Final_timer[pro_id] <= 0)
			{
				Extend_status[pro_id]='E';
//				Expire_home_bid(pro_id);
			}
			else
			{
				var elem = document.getElementById('counter_index_div_'+index);
				if(elem) elem.innerHTML = '' + calc_counter_from_time(final_timer, pro_id);
			}
		}
		else if(Extend_status[pro_id]=='P')	
		{
			var elem = document.getElementById('counter_index_div_'+index);
			if(elem) elem.innerHTML =  'Gepauzeerd';	
			elem = document.getElementById('bid_btn_'+index);
			if(elem) elem.innerHTML ='<a href="index.php?show=product&id='+pro_id+'"><img src="images/info_btn.gif" border="0" id="Best Rated" /></a>';
		}
	}
}

function Update_status(index, pro_id, data)
{		
	var Main_array = data.split("**");

	var	btn = document.getElementById('bid_btn_' + index);
	
	if(btn)
		btn.innerHTML = Main_array[3];
	Final_timer[pro_id]=parseInt(Main_array[4]);
	Extend_timer[pro_id]=parseInt(Main_array[5]);
	Extend_status[pro_id]=Main_array[6];
}

function Update_auction_details(index, pro_id, data)
{		
	var Main_array = data.split("**");

	var elem = document.getElementById('price_index_div_'+index);
	if(elem) elem.innerHTML = Main_array[7];
	elem = document.getElementById('winner_index_div_'+index);
	if(elem) elem.innerHTML = Main_array[8];
}

function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function Valid_username(login)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	

	if (doc)
	{
		doc.open("GET", "./location_user.php?section=username&login="+login, false); 
		doc.send(null);
		return  doc.responseText;
	} 
}


function Valid_mobile(tel)
{
	var doc;
	
	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=mobile&tel="+tel, false); 
		doc.send(null);

		return  doc.responseText;
	}
}

function Edit_Valid_mobile(tel)
{
	var doc;
	
	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=edit_mobile&tel="+tel, false); 
		doc.send(null);

		return  doc.responseText;
	}
}

function Edit_Valid_username(login)
{
	var doc;
	
	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=edit_username&login="+login, false); 
		doc.send(null);

		return  doc.responseText;
	}
}

function Valid_email(email)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=email&email="+email, false);   
		doc.send(null);
		return  doc.responseText;
	}
}

function Edit_Valid_email(email)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=edit_email&email="+email, false);   
		doc.send(null);
		return  doc.responseText;
	}
}


function Admin_Valid_username(login)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=admin_username&login="+login, false); 
		doc.send(null);
		return  doc.responseText;
	}
}


function Admin_Valid_mobile(tel)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=admin_mobile&mobile="+mobile, false); 
		doc.send(null);
		return  doc.responseText;
	}
}

function Admin_Edit_Valid_mobile(mobile,edit_id)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=admin_edit_mobile&mobile="+mobile+"&user_id="+edit_id, false); 
		doc.send(null);

		return  doc.responseText;
	}
}

function Admin_Edit_Valid_username(login,edit_id)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=admin_edit_username&login="+login+"&user_id="+edit_id, false); 
		doc.send(null);

		return  doc.responseText;
	}
}

function Admin_Valid_email(email)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=admin_email&email="+email, false);   
		doc.send(null);
		return  doc.responseText;
	}
}

function Admim_Edit_Valid_email(email,edit_id)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=admin_edit_email&email="+email+"&user_id="+edit_id, false);   
		doc.send(null);
		return  doc.responseText;
	}
}

function Update_session()
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location_user.php?section=update_session&r="+Math.floor(1000000000*Math.random()), false);   
		doc.send(null);
	}
	//setTimeout('Update_session();',10000);		
}
	
	
function Auto_bid_expire()
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location.php?section=autobid_expire&r="+Math.floor(1000000000*Math.random()), false);   
		doc.send(null);
	}
}

function Start_auction()
{
}

function Show_advert()
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location.php?Response=show_advert&r="+Math.floor(1000000000*Math.random()), false);   
		doc.send(null);
		document.getElementById('advert_div').innerHTML = doc.responseText;
	}
	//setTimeout('Show_advert();',10000);		
}


function calc_counter_from_time(diff, pro_id)
{
	if (diff > 0)
	{
		days=Math.floor(diff / (3600 * 24));

		hours=Math.floor(diff / 3600);

		minutes=Math.floor((diff / 3600 - hours) * 60)

		seconds=Math.round((((diff / 3600 - hours) * 60) - minutes) * 60)

		hours=Math.floor(hours - 24 * days);
	}
	else
	{
		days = 0;
		hours = 0;
		minutes = 0;
		seconds = 0;
	}

	if (seconds == 60)
	{
		seconds = 0;
	}

	if (minutes < 10)
	{
		if (minutes < 0)
		{
			minutes = 0;
		}
	}
	if (seconds < 10)
	{
		if (seconds < 0)
		{
			seconds = 0;
		}
	}
	if (hours < 10)
	{
		if (hours < 0)
		{
			hours = 0;
		}
	}

	day_code = '<font color="#CC0000" size="5">'+days+'</font>&nbsp;<font color="#000000" size="0,5" face="Lucida Sans Unicode">dag(en)</font>&nbsp;';
	hour_code = '<font color="#CC0000" size="5">'+hours+'</font>&nbsp;<font color="#000000" size="0,5" face="Lucida Sans Unicode">uur</font>&nbsp;';
	min_code = '<font color="#CC0000" size="5">'+minutes+'</font>&nbsp;<font color="#000000" size="0,5" face="Lucida Sans Unicode">min</font>&nbsp;';
	sec_code = '<font color="#CC0000" size="5">'+seconds+'</font>&nbsp;<font color="#000000" size="0,5" face="Lucida Sans Unicode">sec</font>&nbsp;';
	if(days > 0)
		timer = day_code + hour_code;
	else if(hours > 0)
		timer = hour_code + min_code;
	else if(minutes > 0)
		timer = min_code + sec_code;
	else
	{
		timer = sec_code;
		if(seconds < 5) disableBidAll(pro_id);
	}

	return timer;
}

function disableBidAll(pro_id)
{
	var	btn = document.getElementById('bidallbut' + pro_id);

	if(btn)
	{
		btn.disabled = true;
		btn.className = "disabled";
	}
}

function calc_counter_from_time_short(diff, pro_id)
{
	if (diff > 0)
	{
		hours=Math.floor(diff / 3600)

		minutes=Math.floor((diff / 3600 - hours) * 60)

		seconds=Math.round((((diff / 3600 - hours) * 60) - minutes) * 60)
	}
	else
	{
		hours = 0;
		minutes = 0;
		seconds = 0;
	}

	if (seconds == 60)
	{
		seconds = 0;
	}

	if (minutes < 10)
	{
		if (minutes < 0)
		{
			minutes = 0;
		}
	}
	if (seconds < 10)
	{
		if (seconds < 0)
		{
			seconds = 0;
		}
	}
	if (hours < 10)
	{
		if (hours < 0)
		{
			hours = 0;
		}
		hours = '0' + hours;
	}
	
	if(minutes > 0)
	{
		min_code = '<font color="#FF0000" size="5">'+minutes+'</font>&nbsp;<font color="#FF0000" size="0,5" face="Lucida Sans Unicode">min</font>&nbsp;&nbsp;';
		sec_code = '<font color="#FF0000" size="5">'+seconds+'</font>&nbsp;<font color="#FF0000" size="0,5" face="Lucida Sans Unicode">sec</font>&nbsp;&nbsp;';
		return min_code + sec_code;
	}
	else
	{
		if(seconds < 5) disableBidAll(pro_id);
		if(seconds <= 1)
			sec_code = '<font color="grey" size="3">Controleren..</font>&nbsp;&nbsp;';
		else
			sec_code = '<font color="red" size="5">'+seconds+'</font>&nbsp;<font color="#000000" size="0,5" face="Lucida Sans Unicode">sec</font>&nbsp;&nbsp;';
		return sec_code;
	}
}

function	replaceButtonCode(auctionID, newButtonCode)
{
	var	oldContents, i;

	for(i = 0; ; i++)
	{
		var elem = document.getElementById('bid_btn_' + i);

		if(!elem)
			break;

		oldContents = elem.innerHTML;
		var nIndex = oldContents.indexOf("Add_bid(" + auctionID + ")");
		if(nIndex == -1)
			continue;
		elem.innerHTML = newButtonCode;
		return oldContents;
	}
	return '';
}

function Add_bid(pro_id)
{
	var doc;

	doc = ajax(doc);
	if (doc)
	{
		var old_contents = replaceButtonCode(pro_id, '<img src="images/spinner.gif" border="0"/>');

		var url = "./addbid.php?pro_id="+pro_id+"&r="+Math.floor(1000000000*Math.random());
		doc.open("GET", url, false);
		doc.send(null);
		var retText = doc.responseText.trim();

		if(parseInt(retText)==99999)
		{
			document.location="index.php?show=login";
		}
		else
		{
			var mainarraylist=retText.split("**");

			if(mainarraylist.length<2)
			{
				if(trim(retText) != '')
					alert(retText);
			}
			else
			{
/*				var aucRow = lookupOffset(pro_id);
				// alert("Pro_id=" + pro_id + ", ma0=" + mainarraylist[0] + " ma1=" + mainarraylist[1] + ", aucRow=" + aucRow);
				if(aucRow < 0)
					return;
				
				var	e = document.getElementById('price_index_div_'+ aucRow);
				e.innerHTML = mainarraylist[0];
				e = document.getElementById('winner_index_div_'+ aucRow);
				e.innerHTML = mainarraylist[1];
*/
			}
		}
		if(old_contents != '')
			changeIfDiff('bid_btn_' + pro_id, old_contents);
	}
}

function BidAll(pro_id, enable)
{
	var doc;

	doc = ajax(doc);
	if (doc)
	{
		var old_contents = replaceButtonCode(pro_id, '<img src="images/spinner.gif" border="0"/>');
		var url = "./bidall.php?pro_id="+pro_id+"&e="+enable+"&r="+Math.floor(1000000000*Math.random());
		doc.open("GET", url, false);
		doc.send(null);
		var	retText = doc.responseText.trim();
		
		if(parseInt(retText)==99999)
		{
			document.location="index.php?show=login";
		}
		else
		{
			var mainarraylist=retText.split("**");

			if(mainarraylist.length<2)
				if(retText != '')
					alert(retText);
		}
		if(old_contents != '')
			changeIfDiff('bid_btn_' + pro_id, old_contents);
	}
}

function	visible(index, viz)
{
	div = document.getElementById('auction_container' + index);

	if(!div)
		return 0;
		
	if(viz){
	div.style.visibility = 'visible'; 
	div.style.display="inline";
		for (var i=0, o; (o=div.getElementsByTagName('div')[i]); i++) {
			o.style.visibility="visible";
			o.style.display="block";
		}	
	}else{
		div.style.visibility="hidden";
		div.style.display="none";
		for (var i=0, o; (o=div.getElementsByTagName('div')[i]); i++) {
			o.style.visibility="hidden";
			o.style.display="none";
		}
	}
	return 1;
}

function	reset_auction_list()
{
	var	table = document.getElementById('auction_items');
	var	i;

	for(i = 0; visible(i, 0); i++);
}

function	clear_auction_item(l)
{
	visible(l, 0);
}

function	changeIfDiff(elemID, data)
{
	var	elem = document.getElementById(elemID);
	var	oldContents;

	if(elem)
	{
		if(elem.innerHTML != data)
		{
			oldContents = elem.innerHTML;
			elem.innerHTML = data;
		}
	}
	return oldContents;
}

function	add_auction_item(index, pro_id, title, image_path, auction_code, currency, max_bid,
	normal_rate, final_mobile_number, extends_time, button_code)
{	/**button_code += '<br>\n<a href="index.php?show=buyitnow&id=' + pro_id + '"><img border=0 src="images/buy.jpg"></a>';**/
	var cur = currency;
	visible(index, 1);
	changeIfDiff('title' + index, title);
	changeIfDiff('linkphoto' + index, image_path);
	changeIfDiff('extendstime' + index, extends_time);
	changeIfDiff('auctioncode' + index, auction_code);
	changeIfDiff('currsignnormal' + index, currency + ' ' + normal_rate);
	changeIfDiff('price_index_div_'+index, '&euro; ' + max_bid);
	changeIfDiff('bid_btn_'+index, button_code);
	changeIfDiff('winner_index_div_'+index, final_mobile_number);
}
/*
function checkTimeoutLogout()
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./timer_location.php?section=check_timeout&r="+Math.floor(1000000000*Math.random()), false);
		doc.send(null);

		if(parseInt(doc.responseText)==1)
			document.location="expired.php";
	}
	setTimeout('checkTimeoutLogout()', 30000); 
}
*/
function checkDuplicateLogin(login)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location.php?section=check_dupe_login&login=" + login, false);
		doc.send(null);

		if(parseInt(doc.responseText)==1)
			return false;
		else
			alert(doc.responseText);
	}
	return true;
}

function checkDuplicateAddress(street, city, zip)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location.php?section=check_dupe_address&street=" + escape(street) + "&city=" + escape(city) + "&zip=" + escape(zip), false);
		doc.send(null);

		if(parseInt(doc.responseText)==1)
			return false;
		else
			alert(doc.responseText);
	}
	return true;
}

function checkDuplicateEmail(email)
{
	var doc;

	doc = ajax(doc);
	// Load the result from the response page
	// ** As far a I know firefox will only load a document on the SAME domain!!	
	if (doc)
	{
		doc.open("GET", "./location.php?section=check_dupe_email&email=" + email, false);
		doc.send(null);

		if(parseInt(doc.responseText)==1)
			return false;
		else
			alert(doc.responseText);
	}
	return true;
}
