function isNumeric(value) {
  if (value != null && !value.toString().match(/^[-]?\d*\.?\d*$/))
  {
	return false;
   }
   else
   {
	 if(!(value<1000000)) 
	 {
		return false;
	 }
   }   
  return true;
}


var bid_win;

function createBidWindow()
{
	 bid_win = new Ext.Window({
        layout:'fit',
        closable: true,
        resizable: false,
        plain: true,
        border: false,
		modal: true      
	});	
}


function setBidAmountToZero(e)
{
	Ext.getCmp('bid_form_quote').setValue("0.00");
}

function removeBidWindow()
{
	bid_win.close();
	bid_win.destroy();
	bid_win = null;
}

function BidStep1()
{		
		createBidWindow();
		CreateBidStep1Form();
		bid_win.add(objBidStep1Form);	
		bid_win.width=500;	
		bid_win.height=350;
		bid_win.title="HazValue - unlocking the profit in responsibility";
		bid_win.show();		
}

function BidStep2()
{		
		createBidWindow();
		CreateBidStep2Form();
		bid_win.add(objBidStep2Form);	
		bid_win.width=500;	
		bid_win.height=150;
		bid_win.title="HazValue - unlocking the profit in responsibility";
		bid_win.show();		
}

function BidStep3()
{		
		createBidWindow();
		CreateBidStep3Form();
		bid_win.add(objBidStep3Form);	
		bid_win.width=500;	
		bid_win.height=125;
		bid_win.title="HazValue - unlocking the profit in responsibility";
		bid_win.show();		
}


var objBidStep1Form;
function CreateBidStep1Form()
{	
        objBidStep1Form = new Ext.FormPanel({ 
        labelWidth:175,       
        frame:true, 
        title:'Quote', 
        defaultType:'textfield',
		closable: true,
		defaults: {autoHeight: true,hideMode:'offsets'},
		monitorValid:true,
        items:[ 
				

			   new Ext.form.Label({
							name:'bid_form_detail',
							id:'bid_form_detail',	                        	                          	                        
	                       	html : 'You are quoting on Item number <b>' + itemid + '</b> for <b>' + descr + '</b> <br>Quantity: <b>' + qty + ' ' + vol + '</b> <br>Pricing requested: <b>' + rqd + '</b> Quote to be fully inclusive of transport and EA fees, but net of VAT.<br><br><br>',													
							colspan: 2
	                    }),	
			

				new Ext.form.RadioGroup( {
					   name: 'bid_form_type',
					   id: 'bid_form_type',
					   fieldLabel: 'Please select your bid type',
					   colspan: 2,					   
					   
								items: [
									{boxLabel: 'Payment', tabIndex: 2, labelSeparator: 'test',  inputValue: '1', name: 'bid_form_type',checked:true},
									{boxLabel: 'Free', tabIndex: 3, labelSeparator: '', inputValue: '2', name: 'bid_form_type',handler: setBidAmountToZero},
									{boxLabel: 'Charge', tabIndex: 4, labelSeparator: '', inputValue: '3', name: 'bid_form_type'}
								]
					  }),


				new Ext.form.TextField({
							name: 'bid_form_quote',
							id:'bid_form_quote',							
							fieldLabel: 'Please enter quote value',						
							allowBlank:false 							
					}),
				

				new Ext.form.TextField({
							name: 'bid_form_refernce',
							id:'bid_form_refernce',							
							fieldLabel: 'Your Reference',						
							allowBlank:false 							
					}),

				new Ext.form.TextArea({
							name:'bid_form_additional_information',
							id:'bid_form_additional_information',
							wrap:'off',
							fieldLabel: 'Additional Information',
							width: 280,
							height:120,
							grow:true,
							growMin:120,
							allowBlank:false 							
					})

			

			
			],
 

        buttons:[{ 
                text:'Bid',
                formBind: true,	 
                
                handler:function(){       
					
						

						if(Ext.getCmp('bid_form_type').items.items[0].checked){
							top.numericChargeType=1;
						}

						if(Ext.getCmp('bid_form_type').items.items[1].checked){
							top.numericChargeType=2;
						}

						if(Ext.getCmp('bid_form_type').items.items[2].checked){
							top.numericChargeType=0;
						}

						if(Ext.getCmp('bid_form_type').items.items[1].checked)
						{
							bidAmount=0;
						}
						else
						{
							bidAmount=Ext.getCmp('bid_form_quote').getValue();

							if(isNumeric(bidAmount))
							{
								bidAmount=Math.round((bidAmount/1)*100)/100;

								if(bidAmount<0)
								{
									alert("Please enter only numeric positive figure in bid amount (o or greater)");
									return;
								}

								if ( (bidAmount+"").indexOf(".") == -1)
								{
									bidAmount=bidAmount+".00"
								}
							}
							else
							{
								alert("Please enter only numeric figure in bid amount");
								return;
							}							
						}
						

						bidder_reference=Ext.getCmp('bid_form_refernce').getValue();
						additionalInfo=Ext.getCmp('bid_form_additional_information').getValue();						
						
						removeBidWindow();
						BidStep2();
                } 
            }] 
    });  
}

var objBidStep2Form;
function CreateBidStep2Form()
{	
		var formTitle="";
		var termsConditionLink="By selecting Confirm below you accept and agree to our <a href=\"javascript:window.open('terms.asp','terms','width=800,height=550,scrollbars=yes');void(''); \">Terms and Conditions</a>";
		var reqPricing=" <b> " + rqd + "</b> for Item Number <b>" + itemid + " " + descr + "</b>.";

		if(top.numericChargeType==0)
		{
			formTitle="You have quoted to charge <b>\u00a3" + bidAmount + '</b>' + reqPricing;
		}
		else if(top.numericChargeType==1)
		{
			formTitle="You have quoted to pay  <b>\u00a3 " + bidAmount + '</b>' + reqPricing;
		}		
		else
		{
			formTitle="You have quoted to collect item number <b>" + itemid + "</b>  description for free.";
		}
        objBidStep2Form = new Ext.FormPanel({ 
        labelWidth:175,       
        frame:true, 
        title:'Confirmation', 
        defaultType:'textfield',
		closable: true,
		defaults: {autoHeight: true,hideMode:'offsets'},
		monitorValid:true,
        items:[ 
				

			   new Ext.form.Label({
							name:'bid_form_detail',
							id:'bid_form_detail',	                        	                          	                        
	                       	html : formTitle + " <br><br>  " + termsConditionLink ,													
							colspan: 2
	                    })
			],
 

        buttons:[{ 
                text:'Confirm',
                formBind: true,	 
                
                handler:function(){     
						removeBidWindow();	
						BidStep3();	
						top.postbid.location.href="./bid.asp?itemid="+itemid+"&bidamt="+bidAmount+"&qty=1&bidtype="+top.numericChargeType+"&additionalInfo="+additionalInfo+"&bidder_reference="+bidder_reference;
                }},
					
				{
					text:'Cancel',
					formBind: true,	
						
				 handler:function(){     
						removeBidWindow();
                }
            }] 
    });  
}


var objBidStep3Form;
function CreateBidStep3Form(){	
	
        objBidStep3Form = new Ext.FormPanel({ 
        labelWidth:175,       
        frame:true, 
        title:'Thank You', 
        defaultType:'textfield',
		closable: true,
		defaults: {autoHeight: true,hideMode:'offsets'},
		monitorValid:true,
        items:[ 
				

			   new Ext.form.Label({
							name:'bid_form_detail',
							id:'bid_form_detail',	                        	                          	                        
	                       	html : 'Your Quote has been registered' ,													
							colspan: 2
	                    })
			],
 

        buttons:[{ 
                text:'OK',
                formBind: true,	 
                
                handler:function(){     
						removeBidWindow();	
                } 
            }] 
    });  
}