// JavaScript Document
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	if(x2.length==0)
	   x2 = '.00';
	return x1 + x2;
}

sprintfWrapper = {
 
	init : function () {
 
		if (typeof arguments == "undefined") { return null; }
		if (arguments.length < 1) { return null; }
		if (typeof arguments[0] != "string") { return null; }
		if (typeof RegExp == "undefined") { return null; }
 
		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;
 
		while (match = exp.exec(string)) {
			if (match[9]) { convCount += 1; }
 
			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
 
			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount]) < 0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);
 
		if (matches.length == 0) { return string; }
		if ((arguments.length - 1) < convCount) { return null; }
 
		var code = null;
		var match = null;
		var i = null;
 
		for (i=0; i<matches.length; i++) {
 
			if (matches[i].code == '%') { substitution = '%' }
			else if (matches[i].code == 'b') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'c') {
				matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'd') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'f') {
				matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'o') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 's') {
				matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
				substitution = sprintfWrapper.convert(matches[i], true);
			}
			else if (matches[i].code == 'x') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]);
			}
			else if (matches[i].code == 'X') {
				matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
				substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
			}
			else {
				substitution = matches[i].match;
			}
 
			newString += strings[i];
			newString += substitution;
 
		}
		newString += strings[i];
 
		return newString;
 
	},
 
	convert : function(match, nosign){
		if (nosign) {
			match.sign = '';
		} else {
			match.sign = match.negative ? '-' : match.sign;
		}
		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l < 0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == "0" || nosign) {
				return match.sign + pad + match.argument;
			} else {
				return pad + match.sign + match.argument;
			}
		} else {
			if (match.pad == "0" || nosign) {
				return match.sign + match.argument + pad.replace(/0/g, ' ');
			} else {
				return match.sign + match.argument + pad;
			}
		}
	}
}
 
sprintf = sprintfWrapper.init;


 function showRegistration()
 {
	   Ext.onReady(function(){
			
			
			Ext.QuickTips.init();
			
			var storeCountries = new Ext.data.JsonStore({
	  			url:'all.php',
    			id: 'id',

    			root: 'rows',
				fields: [
				  {name:'countryno'}, 
				  {name:'name'}
				]
			});
			
			storeCountries.load({params:{mode: 'COUNTRY_LIST'}});
			
			
			var frm = new Ext.FormPanel({
				frame:false,
				border:false,
				url: 'all.php',
				items:
				[
				 	{
						xtype: 'fieldset',
						title: 'Application Information',
						defaultType: 'textfield',
						frame:false,
						border:false,
						defaults: {width:188},
						height:528,
						items:
							[
							 	{
									xtype: 'panel',
									frame:false,
									border:false,
									autoWidth:true,
									html: '<strong>All information must be completed  to ensure processing of your application.</strong><br><br>'
								},
							 	{
									fieldLabel: 'First Name*',
									name: 'firstname',
									allowBlank: false
								},
								{
									fieldLabel: 'Last Name*',
									name: 'lastname',
									allowBlank: false
								},
								{
									fieldLabel: 'Middle Name',
									name: 'middlename'
								},
								{
									fieldLabel: 'Nick Name',
									name: 'nickname'
								},
								{
									fieldLabel: 'Spouse Name',
									name: 'spousename'
								},
								{
									fieldLabel: 'Gender*',
									xtype: 'combo',
									mode:'local',
									store: new Ext.data.SimpleStore({fields:['code','description'], data:[['M','Male'],['F','Female']]}),
									hiddenName: 'gender',
									allowBlank:false,
									triggerAction:'all',
									displayField: 'description',
									valueField: 'code',
									forceSelection:true
								},
								{
									fieldLabel: 'Civil Status*',
									xtype: 'combo',
									store: new Ext.data.SimpleStore({fields:['code','description'], data:[['Single','Single'],
																										  ['Married','Married'],
																										  ['Widow','Widow'],
																										  ['Widower','Widower'],
																										  ['Separated','Separator']]}),
									hiddenName: 'civilstatus',
									allowBlank:false,
									triggerAction:'all',
									mode:'local',
									displayField: 'description',
									valueField: 'code',
									forceSelection:true
								},
								{
									xtype: 'datefield',
									fieldLabel: 'Date of Birth*',
									name: 'dateofbirth',
									allowBlank:false
								},
								{
									fieldLabel: 'Citizenship*',
									name: 'citizenship',
									allowBlank:false
								},
								{
									fieldLabel: 'Phone Number*',
									name: 'phone',
									allowBlank:false
								},
								{
									fieldLabel: 'Mobile Number',
									name: 'mobile'
								},
								{
									fieldLabel: 'Street Address*',
									name: 'streetaddress',
									allowBlank:false
								},
								{
									fieldLabel: 'City*',
									name: 'city',
									allowBlank:false
								},
								{
									fieldLabel: 'Province*',
									name: 'province',
									allowBlank:false
								},
								{
									fieldLabel: 'Country*',
									xtype: 'combo',
									mode:'local',
									store: storeCountries,
									hiddenName: 'country',
									allowBlank:false,
									triggerAction:'all',
									displayField: 'name',
									valueField: 'countryno',
									forceSelection:true
								},
								{
									fieldLabel: 'Postal Code*',
									name: 'postalcode',
									allowBlank:false
								},
								{
									fieldLabel: 'Email Address*',
									name: 'emailaddress',
									vtype: 'email',
									allowBlank:false
								}
						 	]
					},
					{
						xtype: 'fieldset',
						title: 'Employment Information',
						defaults: {width:188},
						frame:false,
						border:false,
						height:188,
						defaultType: 'textfield',
						items:
							[
							 	{
									fieldLabel: 'Employer*',
									name: 'employer'
								},
								{
									fieldLabel: 'Position',
									name: 'position'
								},
								{
									fieldLabel: 'T.I.N.*',
									name: 'tin',
									allowBlank:false
								},
								{
									fieldLabel: 'Office Tel No',
									name: 'officetelno'
								},
								{
									fieldLabel: 'Fax Number',
									name: 'officefax'
								}
						 	]
					},
					{
						xtype:'fieldset',
						title: 'Hierarchial Information',
						frame:false,
						border:false,

						height: 188,
						defaults: {width:188},
						defaultType: 'textfield',
						items:
							[
							 	{
									fieldLabel: 'Recruiter Name*',
									name: 'recruitername',
									allowBlank:false
								},
								{
									fieldLabel: 'Recruiter Code*',
									name: 'recruitercode',
									allowBlank:false
								},
								{
									fieldLabel: 'SD Code',
									name: 'sdcode'
								},
								{
									fieldLabel: 'SD Name',
									name: 'sdname'
								}
							]
					},
					{
						xtype: 'textfield',
						inputType: 'hidden',
						name:'mode',
						value:'REGISTER'
					}
				],
			buttons:
				[
				 	{
						
						text: 'Submit Now',
						handler: function()
						{
							frm.getForm().submit({
												 success:function(f,a)
												 {
													 frm.getForm().reset();
													 showCartSmall();
												 },
												 failure: function(f,a)
												 {
													 if(a.failureType==Ext.form.Action.CONNECT_FAILURE)
													{
														Ext.Msg.alert('Warning',a.response.status + ' ' + 
																	  a.response.statusText);
													}
													else if(a.failureType==Ext.form.Action.SERVER_INVALID)
													{
														Ext.Msg.alert('Warning',a.result.errorMsg);
													}
												 },
												 waitMsg: 'Saving data....'
												 });
						}
					
					}
				]
				
										
										
										});
							
							
							
						frm.render('register');	
							});
   }
function showCartSmall()
{
	var conn =new Ext.data.Connection({});
	conn.request({
		 url: 'all.php',
		 success:function(resp,opt)
		 {
			try
			{
				var result = Ext.util.JSON.decode(resp.responseText);
				if(result.success==false)
				{
					Ext.Msg.alert('Result',result.errorMsg);		
				}
				else
					{
						
						Ext.get('total').update(result.count);
						Ext.get('subtotal').update(addCommas(result.total));
					}
				}
				
			catch(err)
			{
				Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
			}		   
	 },
	 failure: function(resp,opt)
	 {
		 Ext.Msg.alert('ERROR',resp.statusText);
	 },
	 params:{mode: 'CART_INFO'}
	 });	 
}
function empty_cart()
{
	
		Ext.Msg.show({title: 'XANTHONE CORP',
		 msg: 'Do you wish to delete all items?',
		 buttons: {
				yes:true,
				no: true
			 },
		fn: function(btn)
		{
				switch(btn)
				{
						case "yes":
							var gridItems = Ext.getCmp('gridCartItems');
							Ext.Ajax.request({
								   url: 'all.php',
								   success: function(resp, opt)
								   {
										try
										{
											var result = Ext.util.JSON.decode(resp.responseText);
												if(result.success==false)
												{
													Ext.Msg.alert('Result',result.errorMsg);		
												}
												else
												{
													gridItems.getStore().removeAll();
													 Ext.get('totalamount').update('0.0');
													 showCartSmall();
												}
											}
											
										catch(err)
										{
											Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
										}		   
									   
									   
								   },
								   failure: function(resp, opt)
								   {
										Ext.Msg.alert('ERROR',resp.statusText);
								   },
								   params: { mode: 'DELETE_ALL_CART_ITEMS' }
								});
							break;
				}
		}
	});
									
}
function continueShopping()
{
	window.location.href='estore.php';
}
function deleteItem()
{
	var gridCartItems = Ext.getCmp('gridCartItems');
	var sm = gridCartItems.getSelectionModel();
	if(sm.hasSelection())
	{
		var sel = sm.getSelected();
		var id = sel.data.itemid;
		Ext.Ajax.request({
		   url: 'all.php',
		   success: function(resp, opt)
		   {
				try
				{
					var result = Ext.util.JSON.decode(resp.responseText);
					if(result.success==false)
					{
						Ext.Msg.alert('Result',result.errorMsg);		
					}
					else
					{
						gridCartItems.getStore().remove(sel);
						showCartSmall();
					}
					}
					
				catch(err)
				{
					Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
				}		   
			   
			   
		   },
		   failure: function(resp, opt)
		   {
				Ext.Msg.alert('ERROR',resp.statusText);
		   },
		   params: { id: id, mode: 'DELETE_CART_ITEM' }
		});
	}
}

function checkOut()
{
	
	var conn =new Ext.data.Connection();
	var msg = Ext.Msg.wait('Please wait...','Checking cart...');

	conn.request({
		 url: 'all.php',
		 success:function(resp,opt)
		 {
			 msg.hide();
			 try
			{
				var result = Ext.util.JSON.decode(resp.responseText);
				if(result.success==false)
				{
					Ext.Msg.alert('Result','Please add item(s) to your cart');
				}
				else
					{
						document.location.href='cr_shipping.php';
					}
				}
				
			catch(err)
			{
				Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
			}		   
		 },
		 failure: function(resp,opt)
		 {
			 msg.hide();
			 Ext.Msg.alert('ERROR',resp.statusText);
		 },
		 params:{mode: 'CHECKCART'}
		 });

}

function show_cart_page()
{
	//document.location.href='cr_cart.php';
	//window.location.href="cr_cart.php";
	document.location="http://96.0.80.197/cr_cart.php";
	return false;
	
}

function show_cart1()
{

	
	Ext.onReady(function(){
			var storeCart = new Ext.data.JsonStore({
	  	url:'all.php',
    	id: 'id',
   		root: 'rows',
		fields: [
			  {name:'id'}, 
			  {name:'description'}, 
			  {name:'qty', type: 'float'}, 
			  {name:'price', type: 'float'}, 
			  {name:'amount', type: 'float'},
			  {name: 'hmqp'}
			]
	});
	
	function peso_rendrer(val, x, store)
	{
			return  'PHP ' + addCommas(val);
	}
	var yes_no = new Ext.form.ComboBox({mode: 'local', store: new Ext.data.SimpleStore({fields: ['name','description'], 
																						 data:[['Y', 'Yes'], ['N', 'No']]}), valueField: 'name',
									   displayField: 'description', triggerAction: 'all'});
	
	var pnl = new Ext.Panel({
							//title: 'Your Shopping Cart',
							width:445,
							height: 328,
							frame: true,
							items:
							[
							 	
								{
									xtype: 'editorgrid',
									id: 'gridCartItems',
									title: 'Cart Items',
									layout: 'fit',
//									width: 658,
									height: 288,
									store: storeCart,
									selModel: new Ext.grid.RowSelectionModel(),
									columns:
										[
											{header: 'DESCRIPTION',dataIndex: 'description', width: 168},
											{header: 'QTY',dataIndex: 'qty',editor: 
											new Ext.form.NumberField({minValue:1, allowBlank: false}), sortable:true},
											{header: 'PRICE',dataIndex: 'price',renderer: peso_rendrer},
											{header: 'AMOUNT',dataIndex: 'amount',renderer: peso_rendrer},
											{header: 'HMQP',dataIndex: 'hmqp'}
									 	],
									listeners: 
											{
											afteredit: function(e)
												{
														var conn = new Ext.data.Connection();
																	conn.request({
																			url: 'all.php',
																			params: {
																					mode: 'UPDATE_CART',
																					id: e.record.id,
																					field: e.field,
																					value: e.value
																					},
																			success: function(resp,opt) 
																					{
																						e.record.commit();
																						var grid = Ext.getCmp('gridCartItems');
																						grid.store.reload();
																						showCartSmall();
																					},
																			failure: function(resp,opt) 
																					{
																						e.record.reject();
																					}
																				});
												}
											}
									//////
								},
								{
									xtype: 'panel',
									border:false,
									frame:false,
									height:50,
									html:'<div style="text-align:right">Total Amount:<span id="totalamount">PHP 0.00</span></div>'
									
								}
							]
							});
	
	
	
	//gridGames.render('container');
	storeCart.load({params: { mode: 'SHOW_CART'}} );	
	storeCart.on('load',function(){
									 var total = storeCart.sum('amount');
									 Ext.get('totalamount').update(addCommas(total));
									 
									 });
	storeCart.on('remove',function(){
									 var total = storeCart.sum('amount');
									 Ext.get('totalamount').update(addCommas(total));
									 });
	Ext.get('cartmain').update('');
	pnl.render('cartmain');


	});
	


}
function add_item_cart_xanthone(itemid,price)
{
	var msg = Ext.Msg.wait('Checking status','Please wait...');
	
	var conn =new Ext.data.Connection({});
	conn.request({
				 url: 'all.php',
				 success:function(resp,opt)
				 {
					 msg.hide();
					 try
					{
						var result = Ext.util.JSON.decode(resp.responseText);
						if(result.success==false)
						{
							add_item_cart(itemid,price);
						}
						else
						{
								Ext.Msg.show({
								   title:'Use as HMQP?',
								   msg: 'Would you like to make this as an HMQP?',
								   buttons:  Ext.Msg.YESNO,
								   fn: function hmqp(button){
									   		if(button=="yes")
												add_item_cart(itemid,price,'Y');
											else
												add_item_cart(itemid,price,'N');
									   },
								   icon: Ext.MessageBox.QUESTION
								});
						}
					}
						
					catch(err)
					{
						Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
					}		   
				 },
				 failure: function(resp,opt)
				 {
					 msg.hide();
					 Ext.Msg.alert('ERROR',resp.statusText);
				 },
				 params:{itemid: itemid,price: price, mode: 'IS_AGENT'}
				 });
}
function add_item_cart(itemid,price,hmqp)
{
	
	if(hmqp == null)
	  hmqp = 'N';
	var msg = Ext.Msg.wait('Adding item to cart','Please wait...');
	
	var conn =new Ext.data.Connection({});
	conn.request({
				 url: 'all.php',
				 success:function(resp,opt)
				 {
					 msg.hide();
					 try
					{
						var result = Ext.util.JSON.decode(resp.responseText);
						if(result.success==false)
						{
							Ext.Msg.alert('Result',result.errorMsg);		
						}
						else
							{
								showCartSmall();
							}
						}
						
					catch(err)
					{
						Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
					}		   
				 },
				 failure: function(resp,opt)
				 {
					 msg.hide();
					 Ext.Msg.alert('ERROR',resp.statusText);
				 },
				 params:{itemid: itemid,price: price, mode: 'ADD_CART',hmqp: hmqp}
				 });
}

function showPaymentForm(orno,amount)
{
	var win = new Ext.Window({
			title: 'Xanthone Juice Corporation - Checkout',
			frame:true,
			border:true,
			width: 588,
			height: 588,
			layout: 'form',
			items:
				[
				 	{
						xtype: 'panel',
						frame:false,
						border:false,
						html: 'Please do not close window until you have successfully finish the payment process<br><br>'
					},
					{
						xtype: 'textfield',
						fieldLabel: 'Invoice Number',
						readOnly:true,
						value: sprintf("%08d",orno)
					},
					{
						xtype: 'textfield',
						fieldLabel: 'Amount',
						readOnly:true,
						value: addCommas(amount)
					},
					{
						xtype: 'panel',
						html: '<form  id="formPay" name="formPay" method="POST" action="http://222.127.253.90:8080/MerchantApp/MerchantServletFront" >' + 
								'<input type = "hidden" name="invnum" value="' + sprintf("%08d",orno) + '" />' + 
								'<input type = "hidden" name="trnamt"></" value="' + amount + '" />' + 
							   '</form>'

					}
			 	],
				buttons:
					[
					 	{
							text: 'Submit Payment',
							handler: function()
							{
								//frm.getForm().getEl().dom.action='https://www.paypal.com/cgi-bin/webscr';
								//frm.getForm().getEl().dom.submit();
								Ext.get('formPay').dom.submit();
							}
						}
				 	]
		
		});
	 
	 win.show();
	 
}

function showShipping()
{
	var storeCountries = new Ext.data.JsonStore({
	  			url:'all.php',
    			id: 'id',

    			root: 'rows',
				fields: [
				  {name:'countryno'}, 
				  {name:'name'}
				]
			});
			
	
	
	var frm = new Ext.FormPanel({
		frame:false,
		border:false,
		url: 'all.php',
		defaultType: 'textfield',
		defaults:{width: 188},
		items:
				[
				 	{
						xtype: 'textfield',
						height: 58,
						width: 208,
						name: 'remarks',
						fieldLabel: 'Remarks'
					},
				 	{
						xtype: 'panel',
						frame:false,
						border:false,
						autoWidth:true,
						html: '<br><p align="center">Please provide a valid shipping address.</p><br><br>'
					},
			 		{
						fieldLabel: 'First Name*',
						name: 'firstname',
						allowBlank:false
					},
					{
						fieldLabel: 'Last Name*',
						name: 'lastname',
						allowBlank:false
					},
					{
						fieldLabel: 'Street*',
						name: 'street',
						allowBlank:false
					},
					{
						fieldLabel: 'Street 2',
						name: 'street2'
					},
					{
						fieldLabel: 'City*',
						name: 'city',
						allowBlank:false
					},
					{
						fieldLabel: 'State/Province*',
						name: 'state',
						allowBlank:false
					},
					{
						fieldLabel: 'Zip/Postal*',
						name: 'zip',
						allowBlank:false
					},
					{
						fieldLabel: 'Country*',
						xtype: 'combo',
						mode:'local',
						store: storeCountries,
						hiddenName: 'country',
						allowBlank:false,
						triggerAction:'all',
						displayField: 'name',
						valueField: 'countryno'
					},
					{
						inputType: 'hidden',
						name: 'mode',
						value: 'SHIPPING'
					}
			 	],
			buttons:
				[
				 	{
						
						text: 'Continue',
						handler: function()
						{
							frm.getForm().submit({
												 success:function(f,a)
												 {
													 
													 document.location.href='cr_payconfirmation.php';

												 },
												 failure: function(f,a)
												 {
													 if(a.failureType==Ext.form.Action.CONNECT_FAILURE)
													{
														Ext.Msg.alert('Warning',a.response.status + ' ' + 
																	  a.response.statusText);
													}
													else if(a.failureType==Ext.form.Action.SERVER_INVALID)
													{
														Ext.Msg.alert('Warning',a.result.errorMsg);
													}
												 },
												 waitMsg: 'Saving data....'
												 });
						}
					
					}
				]
		
								
		});
	Ext.get('shipping').update('');
	storeCountries.load({params:{mode: 'COUNTRY_LIST'}});
	storeCountries.on('load',function(){
		frm.getForm().load({url: 'all.php', params:{mode: 'GET_SHIPPING'}});
	});	
	frm.render('shipping');
	
			
}

function login()
{
	var code = Ext.get('agentcode');
    var pwd   = Ext.get('password');

	var agentcode = code.getValue();
	var password  = pwd.getValue();
	
    if(agentcode.length==0)
        Ext.Msg.alert('Warning','Please input a valid agentcode');
	else if(password.length==0)
		Ext.Msg.alert('Warning','Please input a valid password');
    else
    {
			var conn =new Ext.data.Connection();
			var msg = Ext.Msg.wait('Please wait...','Checking User Information');

			conn.request({
				 url: 'all.php',
				 success:function(resp,opt)
				 {
					 msg.hide();
					 try
					{
						var result = Ext.util.JSON.decode(resp.responseText);
						if(result.success==false)
						{
							Ext.Msg.alert('Result',result.errorMsg);		
						}
						else
							{
								document.location.href='index.php';
							}
						}
						
					catch(err)
					{
						Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
					}		   
				 },
				 failure: function(resp,opt)
				 {
					 msg.hide();
					 Ext.Msg.alert('ERROR',resp.statusText);
				 },
				 params:{agentcode: agentcode,password: password, mode: 'LOGIN'}
				 });
    }
}


function forgotPassword()
{
		var frm = new Ext.FormPanel({

				frame:true,
				border:true,
				height: 288,
				width: 388,
				url: 'all.php',
				defaults: {width: 188},
				items:
						[
							{
								xtype: 'textfield',
								name: 'agentcode',
								fieldLabel: 'Agent Code',
								allowBlank: false
							},
							{
								xtype: 'textfield',
								name: 'agentname',
								fieldLabel: 'Agent Name',
								allowBlank: false
							},
							{
								xtype: 'textfield',
								name: 'email',
								fieldLabel: 'Email Address',
								allowBlank: false,
								vtype: 'email'
							},
							{
								xtype: 'textarea',
								height: 118,
								name: 'remarks',
								fieldLabel: 'Remarks',
								allowBlank: false
							},
							{
								xtype: 'textfield',
								inputType: 'hidden',
								name: 'mode',
								value: 'FORGOT'
							}
						],
				buttons:
						[
							{
								text: 'Submit',
								handler: function()
								{
							
									frm.getForm().submit({
												 success:function(f,a)
												 {
													 
													 Ext.Msg.alert('Result','Please check your email in a few minutes, we will send you your password using your email address provided');
													 win.close();
													 win.destroy();

												 },
												 failure: function(f,a)
												 {
													 if(a.failureType==Ext.form.Action.CONNECT_FAILURE)
													{
														Ext.Msg.alert('Warning',a.response.status + ' ' + 
																	  a.response.statusText);
													}
													else if(a.failureType==Ext.form.Action.SERVER_INVALID)
													{
														Ext.Msg.alert('Warning',a.result.errorMsg);
													}
												 },
												 waitMsg: 'Sending data....'
												 });



								}
							}
						]
	
		});

		var win = new Ext.Window({
			title: 'Forgot Password',
			items: frm

			});
		win.show();
}

function logout()
{

   	var conn =new Ext.data.Connection();
	var msg = Ext.Msg.wait('Please wait...','Signing out.');

	conn.request({
		 url: 'all.php',
		 success:function(resp,opt)
		 {
			 msg.hide();
			 try
			{
				var result = Ext.util.JSON.decode(resp.responseText);
				if(result.success==false)
				{
					Ext.Msg.alert('Result',result.errorMsg);		
				}
				else
					{
						document.location.href='index.php';
					}
				}
				
			catch(err)
			{
				Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
			}		   
		 },
		 failure: function(resp,opt)
		 {
			 msg.hide();
			 Ext.Msg.alert('ERROR',resp.statusText);
		 },
		 params:{mode: 'LOGOUT'}
		 });
}


function showCHPWD()
{
		var frm = new Ext.FormPanel({

				frame:true,
				border:true,
				height: 108,
				width: 388,
				url: 'all.php',
				defaults: {width: 188},
				items:
						[
							{
								xtype: 'textfield',
								name: 'currentpwd',
								fieldLabel: 'Current Password',
								inputType: 'password',
								allowBlank: false
							},
							{
								xtype: 'textfield',
								name: 'newpwd',
								fieldLabel: 'New Password',
								allowBlank: false,
								inputType: 'password'
							},
							{
								xtype: 'textfield',
								inputType: 'hidden',
								name: 'mode',
								value: 'CHANGEPWD'
							}
						],
				buttons:
						[
							{
								text: 'Submit',
								handler: function()
								{
									//check if current password is correct
									
									var pwd = frm.getForm().findField('currentpwd').getValue();
										var conn =new Ext.data.Connection();
									var msg = Ext.Msg.wait('Please wait...','Checking current password.');
								
									conn.request({
										 url: 'all.php',
										 success:function(resp,opt)
										 {
											 msg.hide();
											 try
											{
												var result = Ext.util.JSON.decode(resp.responseText);
												if(result.success==false)
												{
													Ext.Msg.alert('Result',result.errorMsg);		
												}
												else
													{
														//success
															frm.getForm().submit({
															 success:function(f,a)
															 {
																 
																 Ext.Msg.alert('Result','Password changed successfully');
																 win.close();
																 win.destroy();
			
															 },
															 failure: function(f,a)
															 {
																 if(a.failureType==Ext.form.Action.CONNECT_FAILURE)
																{
																	Ext.Msg.alert('Warning',a.response.status + ' ' + 
																				  a.response.statusText);
																}
																else if(a.failureType==Ext.form.Action.SERVER_INVALID)
																{
																	Ext.Msg.alert('Warning',a.result.errorMsg);
																}
															 },
															 waitMsg: 'Sending data....'
															 });
													}
												}
												
											catch(err)
											{
												Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
											}		   
										 },
										 failure: function(resp,opt)
										 {
											 msg.hide();
											 Ext.Msg.alert('ERROR',resp.statusText);
										 },
										 params:{mode: 'CHECKPASS',password: pwd}
										 });

								}
							}
						]
	
		});

		var win = new Ext.Window({
			title: 'Change Password',
			items: frm

			});
		win.show();
}

