/**
* Defines the quick login popup dialog.
* 
* The config parameter accepts the following items:
* 
* onLoginComplete:			The function to call when login completes successfully
* onRegisterComplete:		The function to call when registration completes successfully
* 
* @author Greg Pasquariello
*/
Ext.namespace("Society")

Ext.override(Ext.Window, {
	anchorTo: function(C,G,D,B,F){
		var E=function(){
			if (this.isVisible()) {
				this.alignTo(C, G, D);
			}
		};
		
		Ext.EventManager.onWindowResize(E,this);
		var A=typeof B;
		if(A!="undefined"){
			Ext.EventManager.on(window,"scroll",E,this,{buffer:A=="number"?B:50})
		}
		E.call(this);
		this[F]=E;
		return this;
	}
});

countries = [
	['1', 'United States'],
	['2', 'Canada'],
	['3', 'Japan'],
	['4', 'Australia'],
	['16', 'Andorra'],
	['12', 'Afghanistan'],
	['13', 'Albania'],
	['14', 'Algeria'],
	['15', 'American Samoa'],
	['17', 'Angola'],
	['18', 'Anguilla'],
	['19', 'Antarctica'],
	['20', 'Antigua and Barbuda'],
	['21', 'Argentina'],
	['22', 'Armenia'],
	['23', 'Aruba'],
	['24', 'Austria'],
	['25', 'Azerbaijan'],
	['26', 'Bahamas'],
	['27', 'Bahrain'],
	['28', 'Bangladesh'],
	['29', 'Barbados'],
	['30', 'Belarus'],
	['31', 'Belgium'],
	['32', 'Belize'],
	['33', 'Benin'],
	['34', 'Bermuda'],
	['35', 'Bhutan'],
	['36', 'Bolivia'],
	['37', 'Bosnia and Herzegovina'],
	['38', 'Botswana'],
	['39', 'Bouvet Island'],
	['40', 'Brazil'],
	['41', 'British Indian Ocean Territory'],
	['42', 'Brunei Darussalam'],
	['43', 'Bulgaria'],
	['44', 'Burkina Faso'],
	['45', 'Burundi'],
	['46', 'Cambodia'],
	['47', 'Cameroon'],
	['48', 'Cape Verde'],
	['49', 'Cayman Islands'],
	['50', 'Central African Republic'],
	['51', 'Chad'],
	['52', 'Chile'],
	['53', 'China'],
	['54', 'Christmas Island'],
	['55', 'Cocos (Keeling) Islands'],
	['56', 'Colombia'],
	['57', 'Comoros'],
	['58', 'Congo'],
	['59', 'Congo, the Democratic Republic of the'],
	['60', 'Cook Islands'],
	['61', 'Costa Rica'],
	['62', 'Cote DIvoire'],
	['63', 'Croatia'],
	['64', 'Cuba'],
	['65', 'Cyprus'],
	['66', 'Czech Republic'],
	['67', 'Denmark'],
	['68', 'Djibouti'],
	['69', 'Dominica'],
	['70', 'Dominican Republic'],
	['71', 'Ecuador'],
	['72', 'Egypt'],
	['73', 'El Salvador'],
	['74', 'Equatorial Guinea'],
	['75', 'Eritrea'],
	['76', 'Estonia'],
	['77', 'Ethiopia'],
	['78', 'Falkland Islands (Malvinas)'],
	['79', 'Faroe Islands'],
	['80', 'Fiji'],
	['81', 'Finland'],
	['82', 'France'],
	['83', 'French Guiana'],
	['84', 'French Polynesia'],
	['85', 'French Southern Territories'],
	['86', 'Gabon'],
	['87', 'Gambia'],
	['88', 'Georgia'],
	['89', 'Germany'],
	['90', 'Ghana'],
	['91', 'Gibraltar'],
	['92', 'Greece'],
	['93', 'Greenland'],
	['94', 'Grenada'],
	['95', 'Guadeloupe'],
	['96', 'Guam'],
	['97', 'Guatemala'],
	['98', 'Guinea'],
	['99', 'Guinea-Bissau'],
	['100', 'Guyana'],
	['101', 'Haiti'],
	['102', 'Heard Island and Mcdonald Islands'],
	['103', 'Holy See (Vatican City State)'],
	['104', 'Honduras'],
	['105', 'Hong Kong'],
	['106', 'Hungary'],
	['107', 'Iceland'],
	['108', 'India'],
	['109', 'Indonesia'],
	['110', 'Iran, Islamic Republic of'],
	['111', 'Iraq'],
	['112', 'Ireland'],
	['113', 'Israel'],
	['114', 'Italy'],
	['115', 'Jamaica'],
	['116', 'Jordan'],
	['117', 'Kazakhstan'],
	['118', 'Kenya'],
	['119', 'Kiribati'],
	['120', 'Korea, Democratic Peoples Republic of'],
	['121', 'Korea, Republic of'],
	['122', 'Kuwait'],
	['123', 'Kyrgyzstan'],
	['124', 'Lao Peoples Democratic Republic'],
	['125', 'Latvia'],
	['126', 'Lebanon'],
	['127', 'Lesotho'],
	['128', 'Liberia'],
	['129', 'Libyan Arab Jamahiriya'],
	['130', 'Liechtenstein'],
	['131', 'Lithuania'],
	['132', 'Luxembourg'],
	['133', 'Macao'],
	['134', 'Macedonia, the Former Yugoslav Republic of'],
	['135', 'Madagascar'],
	['136', 'Malawi'],
	['137', 'Malaysia'],
	['138', 'Maldives'],
	['139', 'Mali'],
	['140', 'Malta'],
	['141', 'Marshall Islands'],
	['142', 'Martinique'],
	['143', 'Mauritania'],
	['144', 'Mauritius'],
	['145', 'Mayotte'],
	['146', 'Mexico'],
	['147', 'Micronesia, Federated States of'],
	['148', 'Moldova, Republic of'],
	['149', 'Monaco'],
	['150', 'Mongolia'],
	['151', 'Montserrat'],
	['152', 'Morocco'],
	['153', 'Mozambique'],
	['154', 'Myanmar'],
	['155', 'Namibia'],
	['156', 'Nauru'],
	['157', 'Nepal'],
	['158', 'Netherlands'],
	['159', 'Netherlands Antilles'],
	['160', 'New Caledonia'],
	['161', 'New Zealand'],
	['162', 'Nicaragua'],
	['163', 'Niger'],
	['164', 'Nigeria'],
	['165', 'Niue'],
	['166', 'Norfolk Island'],
	['167', 'Northern Mariana Islands'],
	['168', 'Norway'],
	['169', 'Oman'],
	['170', 'Pakistan'],
	['171', 'Palau'],
	['172', 'Palestinian Territory, Occupied'],
	['173', 'Panama'],
	['174', 'Papua New Guinea'],
	['175', 'Paraguay'],
	['176', 'Peru'],
	['177', 'Philippines'],
	['178', 'Pitcairn'],
	['179', 'Poland'],
	['180', 'Portugal'],
	['181', 'Puerto Rico'],
	['182', 'Qatar'],
	['183', 'Reunion'],
	['184', 'Romania'],
	['185', 'Russian Federation'],
	['186', 'Rwanda'],
	['187', 'Saint Helena'],
	['188', 'Saint Kitts and Nevis'],
	['189', 'Saint Lucia'],
	['190', 'Saint Pierre and Miquelon'],
	['191', 'Saint Vincent and the Grenadines'],
	['192', 'Samoa'],
	['193', 'San Marino'],
	['194', 'Sao Tome and Principe'],
	['195', 'Saudi Arabia'],
	['196', 'Senegal'],
	['197', 'Serbia and Montenegro'],
	['198', 'Seychelles'],
	['199', 'Sierra Leone'],
	['200', 'Singapore'],
	['201', 'Slovakia'],
	['202', 'Slovenia'],
	['203', 'Solomon Islands'],
	['204', 'Somalia'],
	['205', 'South Africa'],
	['206', 'South Georgia and the South Sandwich Islands'],
	['207', 'Spain'],
	['208', 'Sri Lanka'],
	['209', 'Sudan'],
	['210', 'Suriname'],
	['211', 'Svalbard and Jan Mayen'],
	['212', 'Swaziland'],
	['213', 'Sweden'],
	['214', 'Switzerland'],
	['215', 'Syrian Arab Republic'],
	['216', 'Taiwan, Province of China'],
	['217', 'Tajikistan'],
	['218', 'Tanzania, United Republic of'],
	['219', 'Thailand'],
	['220', 'Timor-Leste'],
	['221', 'Togo'],
	['222', 'Tokelau'],
	['223', 'Tonga'],
	['224', 'Trinidad and Tobago'],
	['225', 'Tunisia'],
	['226', 'Turkey'],
	['227', 'Turkmenistan'],
	['228', 'Turks and Caicos'],
	['229', 'Tuvalu'],
	['230', 'Uganda'],
	['231', 'Ukraine'],
	['232', 'United Arab Emirates'],
	['233', 'United Kingdom'],
	['234', 'United States Minor Outlying Islands'],
	['235', 'Uruguay'],
	['236', 'Uzbekistan'],
	['237', 'Vanuatu'],
	['238', 'Venezuela'],
	['239', 'Viet Nam'],
	['240', 'Virgin Islands British'],
	['241', 'Virgin Islands US'],
	['242', 'Wallis and Futuna'],
	['243', 'Western Sahara'],
	['244', 'Yemen'],
	['245', 'Zambia'],
	['246', 'Zimbabwe'],
	['247', 'St Barthelemy'],
	['248', 'St Martin']
];

userTypes = [
	['2', 'Traveler'],
	['1', 'Property Manager'],
	['3', 'Charity'],
	['4', 'Travel Consultant']
];

referralSource = [
	['0', 'Select'],
	['8', 'Article'],
	['6', 'Find Rentals'],
	['7', 'Friend Referral'],
	['1', 'Google Adwords'],
	['2', 'Google Search'],
	['4', 'Luxury Real Estate'],
	['5', 'Luxury Travel + Lifestyle'],
	['13', 'Mom Central'],
	['9', 'News Story'],
	['11', 'Property Manager'],
	['10', 'Word Of Mouth'],
	['3', 'Yahoo Search'],
	['12', 'Other']
];

Society.LoginDialog = function(config) {
	Ext.apply(this, config);

	var body = Ext.getBody();
	
	if (!this.container) {
		this.container = body.createChild({tag: 'div', id: "holder"});
	}
	
	Ext.form.Field.prototype.msgTarget = 'under';
	
	this.win = new Ext.Window({
		el: this.container,
		bodyStyle: 'padding: 5px 5px 0px 5px; text-align: left;',
		width: 500,
		height: 600,
		closeAction: 'hide',
		plain: true,
		autoScroll: true,
		buttonAlign: "right",
		title: '<div style="text-align: left;">Login to The Society</div>',
		items: {
			xtype: "form",
			bodyStyle:'padding: 5px 5px 5px 5px; background: none; border: none;',
			url: "/components/users/services/loginService.php",
			id: "testForm",
			items: [
			{
				xtype: "fieldset",
				title: "Login",
				autoHeight: true,
				collapsible: true,
				items: [{
					tag: "div",
					html: '<div style="position: relative; padding: 0px 0px 10px 0px;">If you are already registered, enter your user id and password <span style="text-align: right; color: red; position: absolute; right: 0px;">* Required</span></div>',
					bodyStyle: "background: none; border: none; font-size: 9pt;"
				},{
					xtype: "textfield",
					id: "login_name",
					fieldLabel: '<span style="color: red;">*</span> Email'
				},{
					xtype: "textfield",
					id: "login_password",
					inputType: "password",
					fieldLabel: '<span style="color: red;">*</span> Password'
				},{
					tag: "div",
					html: "<a href='#' id='dialogLogin'>Login</a>",
					bodyStyle: "background: none; border: none; font-size: 9pt;text-align: right;"
				}]
			},{
				xtype: "fieldset",
				title: "Register",
				autoHeight: true,
				collapsible: true,
				collapsed: false,
				labelWidth: 150,
				items: [ {
					tag: "div",
					html: '<div style="position: relative; padding: 0px 0px 10px 0px;">To register enter the informaton below and click \'Register\' <span style="text-align: right; color: red; position: absolute; right: 0px;">* Required</span></div>',
					bodyStyle: "background: none; border: none; font-size: 9pt;"
				},{
					xtype: "textfield",
					id: "reg_first_name",
					fieldLabel: '<span style="color: red;">*</span> First Name'
				},{
					xtype: "textfield",
					id: "reg_last_name",
					fieldLabel: '<span style="color: red;">*</span> Last Name'
				},{
					xtype: "textfield",
					id: "reg_email",
					fieldLabel: '<span style="color: red;">*</span> Email'
				},{
					xtype: "textfield",
					id: "reg_verify_email",
					fieldLabel: '<span style="color: red;">*</span> Verify Email'
				},{
					xtype: "textfield",
					id: "reg_nickname",
					fieldLabel: '<span style="color: red;">*</span> Screenname'
				},{
					xtype: "textfield",
					id: "reg_password",
					inputType: "password",
					fieldLabel: '<span style="color: red;">*</span> Password'
				},{
					xtype: "textfield",
					id: "reg_verify_password",
					inputType: "password",
					fieldLabel: '<span style="color: red;">*</span> Verify Password'
				},{
					xtype: "textfield",
					id: "reg_phone",
					fieldLabel: 'Phone'
				},{
					xtype: "textfield",
					id: "reg_zipcode",
					maxLength: 20,
					fieldLabel: '<span style="color: red;">*</span> Zipcode'
				},{
					xtype: "combo",
					id: "reg_country",
					store: new Ext.data.SimpleStore({
						fields: ['countryID', 'country'],
						data: countries
					}),
            		valueField: 'countryID',
            		displayField: 'country',
					mode: 'local',
					editable: true,
					forceSelection: true,
					hiddenName: 'reg_country_id',
					fieldLabel: '<span style="color: red;">*</span> Country',
					value: '1',
					triggerAction: 'all',
					typeAhead: true,
					listClass: 'comboBoxClass',
					listWidth: 300
				},{
					xtype: "combo",
					id: "reg_type",
					store: new Ext.data.SimpleStore({
						fields: ['typeID', 'type'],
						data : userTypes
					}),
            		valueField:'typeID',
            		displayField:'type',
					mode: 'local',
					value: 2,
					hiddenName: 'reg_type_id',
					disableKeyFilter: true,
					editable: false,
					forceSelection: true,
					fieldLabel: '<span style="color: red;">*</span> User Type',
					triggerAction: 'all',
					listClass: 'comboBoxClass'
				},{	
					xtype: "combo",
					id: "reg_referral_source",
					store: new Ext.data.SimpleStore({
						fields: ['referralID', 'referral'],
						data : referralSource
					}),
            		valueField:'referralID',
            		displayField:'referral',
					mode: 'local',
					value: 0,
					hiddenName: 'reg_referral_source_id',
					disableKeyFilter: true,
					editable: false,
					forceSelection: true,
					fieldLabel: '<span style="color: red;">*</span> How did you find us?',
					triggerAction: 'all',
					listClass: 'comboBoxClass'
				},{	
					tag: "div",
					html: '<div><input type="checkbox" id="reg_emaillist" name="reg_emaillist" value="1" checked><label for="reg_emaillist">Send me travel deals, alerts and information from TheSociety.com</label></div>',
					bodyStyle: "background: none; border: none; font-size: 9pt;"
				},{
					tag: "div",
					html: "<a href='#' id='dialogRegister'>Register</a>",
					bodyStyle: "background: none; border: none; font-size: 9pt;text-align: right;"
				}]
			}]
		}
	})
	
	this.win.on("show", this.onWindowShow, this);
}


/**
* 	Pop the window.  First, set it all up, then display it. 
* 
*/
Society.LoginDialog.prototype.show = function() {
	this.win.show();
	this.win.anchorTo(document.body, 't-t', [0, 15], true);
	PopulateRegistrationForm();
}

/**
 * When the window is displayed, set up the event handlers for the 
 * login and register.
 */
Society.LoginDialog.prototype.onWindowShow = function() {
	var loginLink = Ext.get("dialogLogin");
	var registerLink = Ext.get("dialogRegister");
	
	loginLink.on("click", this.login, this);
	registerLink.on("click", this.register, this);
}
			
Society.LoginDialog.prototype.closeForm = function() {
	this.win.hide();
}
	
/**
 * Invoke the login on the server.  If successful, close the window
 * and call any client callbacks.
 */
Society.LoginDialog.prototype.login = function() {
	var testForm = Ext.getCmp("testForm").getForm();
	testForm.submit({
		params: {
			op: "login"
		},
		scope: this,
		success: function(form, action) {
			if (this.win) {
				this.win.hide();
				this.win.destroy();
				this.win = null;	
			}
			
			UpdateHeaderUserName(action.result.data.user.alias);
			
			if (this.onLoginComplete) {
				this.onLoginComplete(this, action.result.data.user);	// send back this dialog and the user data.
			}
		},
		failure: function(form, action) {
			return;
		}
	});
}
	
/**
 * Invoke registration on the server.  If successful, close the 
 * window and call any client callbacks.
 */		
Society.LoginDialog.prototype.register = function() {
	var testForm = Ext.getCmp("testForm").getForm();
	testForm.submit({
		params: {
			op: "register"
		},
		scope: this,
		success: function(form, action) {
			this.win.hide();
			this.win.destroy();
			this.win = null;
			
			UpdateHeaderUserName(action.result.data.user.alias);
			
			if (this.onRegisterComplete) {
				this.onRegisterComplete(this, action.result.data.user);	// send back this dialog and the user data.
			}
		},
		failure: function(form, action) {
			return;
		}
	});
}

function UpdateHeaderUserName(user_alias) {
	var btn = Ext.get("headerLoginButton");
	if (btn) {
		btn.dom.href = "/pass.php?target=account_logout";
		btn.dom.innerHTML = "Sign Out";
	}
	
	if (document.getElementById("pnlHeaderUserLogIn")) {
		document.getElementById("pnlHeaderUserLogIn").style.display = "none";
		document.getElementById("pnlHeaderUserAlias").innerHTML = user_alias;
		document.getElementById("pnlHeaderUserLogOut").style.display = "";
	}	
}

var _blnRegistrationFormPopulated = false;
function PopulateRegistrationForm() {
	var arrQueryString = new Array();
	var arrNameValue = new Array();
	var fieldname;
	var fieldvalue;
	var frmfield;
	
	if (_blnRegistrationFormPopulated == false) {
		var queryString = location.search.toString().replace('?', '');
		
		if (queryString.length > 0) {	
			for (var i=0; i < queryString.split("&").length; i++) {
				arrQueryString[i] = queryString.split("&")[i];
				arrNameValue[0] = arrQueryString[i].split("=");
				
				if (arrNameValue[0].length > 0) {
					fieldname = arrNameValue[0][0].toLowerCase();
					fieldvalue = decodeURI(arrNameValue[0][1]);
					
					if (fieldname == 'reg_emaillist') {
						frmfield = document.getElementById(fieldname);
						if (frmfield) {
							if (fieldvalue == 1) {
								frmfield.checked = true;
							} else {
								frmfield.checked = false;
							}
						}
					} else {
						frmfield = Ext.getCmp(fieldname);
						
						dd_data = null;
						if (fieldname == 'reg_country') {
							dd_data = countries;
						} else if (fieldname == 'reg_type') {
							dd_data = userTypes;
						} else if (fieldname == 'reg_referral_source') {
							dd_data = referralSource;
						}
						if (dd_data) {
							for (var x=0; x<dd_data.length; x++) {
								if (fieldvalue == dd_data[x][1]) {
									fieldvalue = dd_data[x][0];
								}
							}
						}
						
						if (frmfield && fieldname.toLowerCase().indexOf('password') == -1) {
							frmfield.setValue(fieldvalue);
						}
					}
				}
			}
		}
	}

	_blnRegistrationFormPopulated = true;
}
