var xmlHttp=null;
var xmlHttp_save=null;
var global_key;


function trim(stringToTrim) {
	
	return stringToTrim.replace(/^\s+|\s+$/g,'');
}

function isFloat(s)
{
	if (/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/.test(s))
	{
		return true
	}
	else
	{
		return false;
	}
}

function GetXmlHttpObject()
{
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
	{
	var aVersions = [ "MSXML2.XMLHttp.5.0",
					  "MSXML2.XMLHttp.4.0",
					  "MSXML2.XMLHttp.3.0",
					  "MSXML2.XMLHttp",
					  "Msxm12.XMLHTTP",
					  "Microsoft.XMLHttp"];

	for (var i = 0; i < aVersions.length; i++) 
	 {
		try {
			var xmlHttp = new ActiveXObject(aVersions[i]);
			return xmlHttp;
			} 
		catch (oError) 
		   {
			//Do nothing
		   }
	}
	}
  catch (e)
	{
	}
  }
  if (xmlHttp==null)
  {
  alert ('Your browser does not support AJAX!');
  return;
  } 
return xmlHttp;
}


var no_balance="Sorry - your bid account balance is ZERO - You will need to purchase a bid pack to bid on this item";
var no_balance1="Sorry - your bid account balance is low - You will need to purchase a bid pack to bid on this item";

var highest_bidder="Sorry - you are highest bidder - You will not bid again";


			window.onload=function()
			{
				start_business();
			}
			function start_business()
			{
				var max_limit=document.getElementById('max_limit').value;
				var auction_id_list="";
				for(i=1;i<=max_limit;i++)
				{
					auction_id_list += document.getElementById('auction_id_'+i).value+"*";
					
				}
				var url='start_business.php?action=ajax_start_business&auction_id_list='+auction_id_list;
				xmlHttp=GetXmlHttpObject();
				xmlHttp.onreadystatechange=response_business;
				xmlHttp.open('get',url,true);
				xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xmlHttp.send(null);
			}
			
			function response_business()
			{
				
				if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete')
				{
					var max_limit=document.getElementById('max_limit').value;
					var result=xmlHttp.responseText;
					result_arr=result.split('NEXT');
					for(i=1;i<=max_limit;i++)
					{
					var auction_data=result_arr[i-1].split('AJAX');
					var new_time=auction_data[0];
					var new_price=auction_data[1];
					var new_username=auction_data[2];
					var totalbids=auction_data[3];
					//alert(totalbids);
					var placedbids=auction_data[4];
					var placedbids;
					var placedamt=auction_data[5];
					//document.getElementById('mplacedamt').innerHTML=placedamt;
				/*if(placedbids!='NO_PLACEDBIDS' && document.getElementById('session_user_name').value !=''){
					document.getElementById('mplacedbids').innerHTML=placedbids;
					document.getElementById('mplacedamt').innerHTML=placedamt;
					document.getElementById('total_placedamt').innerHTML=placedamt*placedbids;
					}*/
					document.getElementById('total_bids').innerHTML=totalbids;
					var old_price=document.getElementById('price_'+i).innerHTML;
					document.getElementById('time_'+i).innerHTML='<strong>'+new_time+'</strong>';
					document.getElementById('price_'+i).innerHTML=new_price;
					
					if(document.getElementById('placedbidsno') && document.getElementById('placedbidsno').value !='')
					{
					placedbids=	document.getElementById('placedbidsno').value;
					}
					else
					{
					placedbids=0	
					}
					
				 if(document.getElementById('totalbids') && document.getElementById('session_user_name').value !='')
                 {
					document.getElementById('totalbids').innerHTML=totalbids; 
				 }
				 
				 if(document.getElementById('s_bid') && document.getElementById('session_user_name').value !='')
                 {
					 if(placedbids=='NO_PLACEDBIDS')
					 {
					placedbids=0;	 
					 }
					document.getElementById('s_bid').innerHTML=placedbids; 
					
					amt=placedamt;
					//alert(amt);
					if(amt!='')
					{
						amt=placedbids*amt
						if(amt<=0)
					{
						amt='$0.00';
					}
					else
					{
						amt='$'+amt;
						var index_point=amt.indexOf('.');
						if(index_point==-1)
						{
							amt=amt+'.00';
						}
						else
						{
							amt=amt.substr(0,index_point+3);
							if(amt.substr(index_point+1).length == 1)
							amt=amt+'0';
						}
					}
						document.getElementById('s_bidprice').innerHTML=amt;
					}
					else
					{
						document.getElementById('s_bidprice').innerHTML='$'+'0.00';
					}
					
					
					
					
				 }

					if(new_username!="")
					{				
					document.getElementById('username_'+i).innerHTML=new_username;
					}
					else
					document.getElementById('username_'+i).innerHTML=new_username;
						
					
						
					
					time_arr=new_time.split(':');
					if(time_arr[0] <= '00' && time_arr[1] <= '00' && time_arr[2] <= 10)
					{
						document.getElementById('time_'+i).style.color='red';
					}
					else
					{
						document.getElementById('time_'+i).style.color='black';
					}
					
				
					
					
					if(trim(new_time) == '00:00:01' || trim(new_time) == '00:00:00')
					{
						if(parseInt(auction_data[7])==1){
						document.getElementById('time_'+i).innerHTML='<font color="#000000"><strong>ENDED</strong></font>';
						document.getElementById('bid_'+i).innerHTML='Sold';
						}
						else{
							document.getElementById('time_'+i).innerHTML='<font color="#FF0000"><strong>00:00:01</strong></font>';
						}
						/*mano begin*/
						/*var divert;
						if(document.getElementById('username_'+i).innerHTML!=document.getElementById('session_user_name').value || document.getElementById('session_user_name').value=='')
						divert='index.php';
						else
						{
							if(confirm('CONGRATULATIONS! You have won! Do you want to buy now?'))					
							divert='won_auction.php';
							else
							divert='index.php';
						}
						setTimeout ( "window.location.href='"+divert+"'", 1000 );*/
						
						/*mano end*/
						
						
//						document.getElementById('time_'+i).innerHTML=auction_data[3];
						/*mano begin*/
						/*var image_src=document.getElementById('button_'+i).src;
						var last=image_src.lastIndexOf('/');
						var sold_image_src='images/sold_'+image_src.substring(last+1,image_src.length);*/
						/*mano end*/
						
//						document.getElementById('button_'+i).src=sold_image_src;
//						business($i,'bid');
						
						/*mano begin*/
						/*document.getElementById('submit_'+i).onclick=function()
						{
							return false;
						}
						return false;*/
						
						/*mano end*/
						
						
						if(document.getElementById('auction_type') && document.getElementById('auction_type').value!=1)
						{
							
					var s_worth=document.getElementById('s_worth').value;
					document.getElementById('s_price').innerHTML=new_price;
					var s_price=new_price.substr(1);
					var s_saving;
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					var sssss;
					sssss=document.getElementById('s_bidprice').innerHTML;
					ssss=sssss.substr(1);
					//alert(sssss);
				 }
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					 s_saving=parseFloat(s_worth) - parseFloat(s_price)-parseFloat(ssss);
				 }
				 else
				 {
					s_saving=parseFloat(s_worth) - parseFloat(s_price); 
				 }
					
					
					if(s_saving<=0)
					{
						s_saving='$0.00';
					}
					else
					{
						s_saving='$'+s_saving;
						var index_point=s_saving.indexOf('.');
						if(index_point==-1)
						{
							s_saving=s_saving+'.00';
						}
						else
						{
							s_saving=s_saving.substr(0,index_point+3);
							if(s_saving.substr(index_point+1).length == 1)
							s_saving=s_saving+'0';
						}
					}
					document.getElementById('s_savings').innerHTML=s_saving;
				}
				else if(document.getElementById('auction_type') && document.getElementById('auction_type').value==1)
						{
							
					var s_worth=document.getElementById('s_worth').value;
					new_price=document.getElementById('s_price').innerHTML;
					var s_price=new_price.substr(1);
					var s_saving;
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					var sssss;
					sssss=document.getElementById('s_bidprice').innerHTML;
					ssss=sssss.substr(1);
					//alert(sssss);
				 }
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					 s_saving=parseFloat(s_worth) - parseFloat(s_price)-parseFloat(ssss);
				 }
				 else
				 {
					s_saving=parseFloat(s_worth) - parseFloat(s_price); 
				 }
					
					
					if(s_saving<=0)
					{
						s_saving='$0.00';
					}
					else
					{
						s_saving='$'+s_saving;
						var index_point=s_saving.indexOf('.');
						if(index_point==-1)
						{
							s_saving=s_saving+'.00';
						}
						else
						{
							s_saving=s_saving.substr(0,index_point+3);
							if(s_saving.substr(index_point+1).length == 1)
							s_saving=s_saving+'0';
						}
					}
					document.getElementById('s_savings').innerHTML=s_saving;
				}
					}
					else if(trim(new_time)=='PAUSED')
					{
						document.getElementById('time_'+i).style.color='red';
						if(document.getElementById('submit_'+i))
						{
							document.getElementById('submit_'+i).onclick=function()
							{
								alert('PAUSED');
							}
						}
					}
					else
					{
						if(document.getElementById('auction_type') && document.getElementById('auction_type').value!=1)
						{
							
					var s_worth=document.getElementById('s_worth').value;
					document.getElementById('s_price').innerHTML=new_price;
					var s_price=new_price.substr(1);
					var s_saving;
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					var sssss;
					sssss=document.getElementById('s_bidprice').innerHTML;
					ssss=sssss.substr(1);
					//alert(sssss);
				 }
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					 s_saving=parseFloat(s_worth) - parseFloat(s_price)-parseFloat(ssss);
				 }
				 else
				 {
					s_saving=parseFloat(s_worth) - parseFloat(s_price); 
				 }
					
					
					if(s_saving<=0)
					{
						s_saving='$0.00';
					}
					else
					{
						s_saving='$'+s_saving;
						var index_point=s_saving.indexOf('.');
						if(index_point==-1)
						{
							s_saving=s_saving+'.00';
						}
						else
						{
							s_saving=s_saving.substr(0,index_point+3);
							if(s_saving.substr(index_point+1).length == 1)
							s_saving=s_saving+'0';
						}
					}
					document.getElementById('s_savings').innerHTML=s_saving;
				}
				else if(document.getElementById('auction_type') && document.getElementById('auction_type').value==1)
						{
							
					var s_worth=document.getElementById('s_worth').value;
					new_price=document.getElementById('s_price').innerHTML;
					var s_price=new_price.substr(1);
					var s_saving;
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					var sssss;
					sssss=document.getElementById('s_bidprice').innerHTML;
					ssss=sssss.substr(1);
					//alert(sssss);
				 }
					 if(document.getElementById('s_bidprice') && document.getElementById('s_bidprice').innerHTML!='' && document.getElementById('session_user_name').value !='')
                 {
					 s_saving=parseFloat(s_worth) - parseFloat(s_price)-parseFloat(ssss);
				 }
				 else
				 {
					s_saving=parseFloat(s_worth) - parseFloat(s_price); 
				 }
					
					
					if(s_saving<=0)
					{
						s_saving='$0.00';
					}
					else
					{
						s_saving='$'+s_saving;
						var index_point=s_saving.indexOf('.');
						if(index_point==-1)
						{
							s_saving=s_saving+'.00';
						}
						else
						{
							s_saving=s_saving.substr(0,index_point+3);
							if(s_saving.substr(index_point+1).length == 1)
							s_saving=s_saving+'0';
						}
					}
					document.getElementById('s_savings').innerHTML=s_saving;
				}
				
						if(document.getElementById('bidsubmit_'+i).style.display=='block')
						{
							document.getElementById('submit_'+i).onclick=function()
							{
								business(i,'bidsubmit');
							}
						}
					}
					if(auction_data.length==3 && trim(auction_data[5])=='NO_BIDS')
					{
						document.getElementById('submit_'+i).onclick=function()
						{
							alert('Sorry! You don\'t have enough balance!');
						}
					}
					var new_price=document.getElementById('price_'+i).innerHTML;
					if( parseFloat(old_price.substr(1)) == parseFloat(new_price.substr(1)) )
					document.getElementById('price_'+i).style.color='';
					else
					document.getElementById('price_'+i).style.color='red';
					
					var load='<font color=\'#ff0000\'>Loading...</font>';
					if(document.getElementById('price_'+i).innerHTML=='undefined')
					document.getElementById('price_'+i).innerHTML=load;
					if(document.getElementById('time_'+i).innerHTML=='undefined')
					document.getElementById('time_'+i).innerHTML=load;
					if(document.getElementById('username_'+i).innerHTML=='undefined')
					document.getElementById('username_'+i).innerHTML=load;
					
					if(document.getElementById('s_worth'))
					{
						if(document.getElementById('s_savings').innerHTML=='undefined')
						{
							document.getElementById('s_price').innerHTML='<font color=red>Loading...</font>';
							document.getElementById('s_savings').innerHTML='<font color=red>Loading...</font>';
						}
					}
				}
					start_business();		
				}
			}

function business(key,task)
{
	if(xmlHttp_save==null)
	{
		var org_price=document.getElementById('price_'+key).innerHTML;
		if(org_price=='')
		{
			alert("Loading...");
			return false;
		}
		global_key=key;
		var auction_id=document.getElementById('auction_id_'+key).value;
		var auction_min_bid_rate=document.getElementById('auction_min_bid_rate_'+key).value;
		var bidprice=parseFloat(org_price.substr(1))+parseFloat(auction_min_bid_rate);
		var url='start_business.php?action=ajax_business&auction_id='+auction_id+"&bidprice='"+bidprice+"'";
		xmlHttp_save=GetXmlHttpObject();
		xmlHttp_save.onreadystatechange=business;
		xmlHttp_save.open('get',url,true);
		xmlHttp_save.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttp_save.send(null);
	}
	else
	{
		if (xmlHttp_save.readyState==4 || xmlHttp_save.readyState=='complete')
		{
			var response=xmlHttp_save.responseText;
			xmlHttp_save=null;
			
			if(response==11)
			{
				alert(no_balance1);
				document.getElementById('submit_'+global_key).onclick=function()
				{
					alert(no_balance1);
				}
			}
				
		}
	}
	return false;
}
