//									BUYNOW.JS
//								Author: Thomas Duc
//								created: Jan 1 2008
//
//						must have the scriptaculous.j installed
//-----------------------------------------------------------------------------
// this file contains both the buynow functions and the bidding functions
//



//--  BUY_NOW OBJECT
//----------------------------------------------------------------------------
//  1: loads the content of buynowtest.php into a div
//  2: if confirmed, does click on the paypal button
function buynow() 
{
	//PROPERTIES
	
	//page 1
	this.id = 0;
	this.sellerid = 0;
	this.auctionid = 0;
	
 	//--FUNCTIONS
	this.buy	= buyItem;
}


//-- OBJECT FOR BIDDING
//-----------------------------------------------------------
function bidnow() 
{
	//PROPERTIES
	this.auctionid   = 0 ;
	this.maxbid      = 0 ;
	this.action		 = '';
	this.quant_avail = 1 ;
	this.confirmbidok = '';
	
 	//--FUNCTIONS
	this.bid	= bidItem;
}
//--------------------------- END OF OBJECTS --------------------------------
var buy_item                       = new buynow() ;
var bid_item                       = new bidnow() ;





//--------------------------- function to buy item --------------------------------
function buyItem(id,status)
{
	url='http://www.dev.idonowidont.com/buynowtest.php?id=' + id;
	layer = 'auction_' + id;
	
	if (status == 1)
	{
		var form = $('final_step_buynow');
		
		this.sellerid   =  $('sellerid').value;
		this.auctionid  =  Form.Element.getValue(form['id']);
		this.id			=  Form.Element.getValue(form['id']);	
		
		post_data = 'quantity=1&buynowok=yes&sellerid=' + this.sellerid + 				'&auctionid=' + this.auctionid +			'&id=' + this.id; 	
	} 
	else	post_data = '';
	
	$(layer).style.display= "block";			
	$(layer).update('Authenticating...');
								
	$('buynow_detail').update("<div style='text-align:center;width:100%;height:100%;margin-top:250px' <img src='themes/idonowidont/img/load.gif' height='32' > </div>");
		
	
	new Ajax.Request( url, 
	{
		method: 'post',
		postBody:post_data,
  		onSuccess: function(transport) 
  		{
			var	inSwitch = false ;
			switch (transport.responseText)
			{
				case 'LOGIN'  : 			
								$(layer).style.display= "block";			
								$(layer).update('Please login or register in order to purchase');
								setTimeout ( "Effect.Fade($(layer), {duration: 1.0,afterFinish: function(evt) {$(layer).style.display = 'none';}})", 3000 );
			
								inSwitch = true ;
								break;
				
				case 'REFRESH': $('buynow_detail').innerHTML = 'You already submitted your request, please be patient';
								inSwitch = true ;
								document.getElementById('fade').style.display='none';
								break;
			
				case 'BANNED' : $('buynow_detail').innerHTML = "You've been banned from this site, sorry";
								inSwitch = true ;
								document.getElementById('fade').style.display='none';								
								break;
								
				case 'SOLD'   : $('buynow_detail').innerHTML = "Sorry but this item has just been purchased, please try another item";
								inSwitch = true ;
								document.getElementById('fade').style.display='none';								
								break;
								
				case 'TOOMANY': $('buynow_detail').innerHTML = "You are only allowed to buy one item at a time, thank you";
								inSwitch = true ;
								document.getElementById('fade').style.display='none';								
								break;
			}
			
			//THIS PART FUNCTIONS BUT IS UNDEROPTIMIZEDss
			if (((transport.responseText).indexOf("REDIRECTING TO PAYMENT SITE") != 1) && (inSwitch == false))
			{
				document.getElementById('buynow_layout').style.display='block';
				document.getElementById('fade').style.display='block';
				document.getElementById('auction_detail').style.display='none';
				$(layer).style.display= "none";			

				$('buynow_detail').innerHTML = transport.responseText ;	
				$('final_payment').submit();

			}
			else if (inSwitch == false)
			{
				document.getElementById('buynow_layout').style.display='block';
				document.getElementById('fade').style.display='block';
				document.getElementById('auction_detail').style.display='none';
				$(layer).style.display= "none";			
			  $('buynow_detail').innerHTML = transport.responseText ;	
			}
				
		},
    	onFailure: function(transport)
		{
    		alert('failure');
    		$('buynow_detail').innerHTML = "Your find request couldn't be fulfilled, please try again";
    	}
   	});

	
}



//--------------------------- function to bid an item --------------------------------
function bidItem(id)
{
	url='http://www.dev.idonowidont.com/bidtest.php?id=' + id;
	layer = 'auction_bid_' + id;
	
	var form = $('bidding_now');
		
	this.auctionid    = Form.Element.getValue(form['auctionid']);
	this.maxbid       = Form.Element.getValue(form['maxbid']);
	this.action		  = Form.Element.getValue(form['action']);
	this.quant_avail  = Form.Element.getValue(form['quant_avail']);
	this.confirmbidok = Form.Element.getValue(form['confirmbidok']);
				
	post_data = 'auctionid=' + this.auctionid +	'&maxbid=' + this.maxbid + '&action=' + this.action + '&quant_avail=' + this.quant_avail + '&confirmbidok=' + this.confirmbidok;
		
	$('buynow_detail').update("<div style='text-align:center;width:100%;height:100%;margin-top:250px' <img src='themes/idonowidont/img/load.gif' height='32' > </div>");
		
	new Ajax.Request( url, 
	{
		method: 'post',
		postBody:post_data,
  		onSuccess: function(transport) 
  		{
			switch (transport.responseText)
			{
				case 'LOGIN'  : 			
								$('bid_details').update('Please login in order to bid');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';}})", 3000 );
								break;
								
				case 'QUANTITY':
								$('bid_details').update('Please bid once only');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';}})", 3000 );
								break;
				
			case 'BUYNOWONLY':  
								$('bid_details').update('This is a buynow only item');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';}})", 3000 );
								break;
			
				case 'BANNED' : $('bid_details').innerHTML = "You've been banned from this site, sorry";
								document.getElementById('fade').style.display='none';								
								break;
								
				case 'TOOLATE': 
								$('bidding_on_item').update('Sorry but it is too late to bid');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none'; $('fade').style.display='none';}})", 3000 );
								break;
								
				case 'DELETED': 
								$('bidding_on_item').update('Sorry but this item has been deleted');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';}})", 3000 );
								break;
								
		   case 'BIDTOOLITTLE': 
								$('last_bid').style.display= "none";
								$('bid_details_message').style.display= "block";			
								$('bid_details_message').update('You must bid higher than previous bids, thank you');
								setTimeout ( "Effect.Fade($('bid_details_message'), {duration: 1.0,afterFinish: function(evt) {$('bid_details_message').style.display = 'none';$('last_bid').style.display= 'block';}})", 2000 );
								break;
			 
		   case 'UNDERINCREMENT':	
								$('last_bid').style.display= "none";	
								$('bid_details_message').style.display= "block";		
								$('bid_details_message').update('You must bid in bigger increments, thank you');
								setTimeout ( "Effect.Fade($('bid_details_message'), {duration: 1.0,afterFinish: function(evt) {$('bid_details_message').style.display = 'none';$('last_bid').style.display= 'block';}})", 3000 );
								break;
								
		  case 'BID_IncPROXY' :	
								$('bidding_on_item').update('Re-bid registered');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';$('fade').style.display='none';}})", 3000 );	
								break;
								
		  case 'RESERVE' :		alert('reserve');
								$('bid_details_result').update('The reserve price has been reach already, this auction is closed');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';$('fade').style.display='none';}})", 3000 );	
								break;
								
				default:		
								//document.getElementById('auction_detail').style.display='block';
								$('bid_details_result').update('Your bid has been registered');
								setTimeout ( "Effect.Fade($('bid_details'), {duration: 1.0,afterFinish: function(evt) {$('bid_details').style.display = 'none';$('fade').style.display='none';}})", 3000 );							
								break;
			}
				
		},
    	onFailure: function(transport)
		{
    		alert('failure');
    		$('buynow_detail').innerHTML = "Your find request couldn't be fulfilled, please try again";
    	}
   	});

	
}
