/**
 * @author Ben Bonnefoy extends Greg
 */
Ext.namespace("prop_send_msg");
	
/**
 * The Dialog is the dialog that provides the user interaction.
 * 
 * @param {Object} config
 *
 */
prop_send_msg.SelectionDialog = function(config) {
	this.config = config;
}

prop_send_msg.SelectionDialog.prototype.save = function() {
		var today = new Date();
		today = new Date((today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear());
		var startDate = new Date(Ext.get('start_date').dom.value);
		startDate.setFullYear(startDate.getFullYear() + 100);
		var endDate = new Date(Ext.get('end_date').dom.value);
		endDate.setFullYear(endDate.getFullYear() + 100);
		
		if(startDate < today || endDate <= startDate){
		Ext.MessageBox.show({
			title: 'Date Error',
			msg: '<font color="red">To/From date error - Please verify the date requested</font>',
			width:400,
			buttons: Ext.MessageBox.OK
			//animEl: paragraph
		});
		}
		else{
		//1: for user
		entity_type_id = 1;
		//user_id of the person sending the message session['account_id']  or blank
		entity_id = Ext.get('user_id').dom.value;
		//to: user_id or user_email - $company_manager
		message_to = Ext.get('prop_mgr_id').dom.value;
		message_from = Ext.get('user_name').dom.innerHTML;
		message_subject = "Property Information Requested"
		//+ '<u>Number Sleeps</u>: '+Ext.get('nmbr_sleep').dom.value+'<br>'
		message_body = Ext.get('msg').dom.value+'<br><br><br>'
					+ '<u>Number of People</u>: '+Ext.get('nmbr_people').dom.value+'<br>'
					+ '<u>Number of Bedrooms</u>: '+Ext.get('nmbr_bedrooms').dom.value+'<br>'
					+ '<u>From</u>: '+Ext.get('start_date').dom.value+' - <u>To</u>: '+Ext.get('end_date').dom.value+'<br><br>';
//					+ '<u>Contact information</u>: '+Ext.get('user_name').dom.innerHTML+' - '+Ext.get('user_email').dom.innerHTML+' - '+Ext.get('user_phone').dom.innerHTML+'<br>';

		Ext.Ajax.request({
			url: "/alert.php",
			failure: function (){
					 Ext.MessageBox.show({
						title: 'Error',
						msg: '<font color="red">Sorry an error occured - Please try again!</font>',
						width:400,
						buttons: Ext.MessageBox.OK
						//animEl: paragraph
					});
			},
			success: function () { 
				Ext.MessageBox.show({
					title: 'Inquiry Sent!',
					msg: '<iframe src="/components/property/postInquiry.php" style="width: 100%; height: 190px; border: none;"></iframe>',
					width:400,
					buttons: Ext.MessageBox.OK
				});
			},
			//params: { item: this.config.companyId, group: rec.data.affiliate_id }
			params: 'target=inbox&action=email&entity_type_id='+entity_type_id+'&entity_id='+entity_id+'&message_to='+message_to+'&message_type_id=1&message_from='+message_from+'&message_subject='+message_subject+'&message_body='+message_body+'&message_url=&message_remove=&company_id='+this.config.companyId+'&sid='+Math.random()
		});
		
		this.win.hide();
		
		Ext.MessageBox.show({
			title: 'Sending message...',
			msg: '<font color="red">Please wait while the inquiry is being sent...</font>',
			width:400
			//animEl: paragraph
		});
		
		copyspeed=1;
		
		}
}

//prop_send_msg.SelectionDialog.prototype.login = function() {
//		var regex = /^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]+))$/;
//		if(Ext.get('login_email').dom.value=='' || Ext.get('login_password').dom.value==''){
//			Ext.MessageBox.show({
//				title: 'Incorrect Login',
//				msg: '<center><font color="red">Email and/or Password field cannot be left blank</font></center>',
//				width:300,
//				buttons: Ext.MessageBox.OK
//				//animEl: paragraph
//			});
//		}
//		else if(!Ext.get('login_email').dom.value.match(regex)){
//			Ext.MessageBox.show({
//				title: 'Incorrect Login',
//				msg: '<center><font color="red">Please enter a valid email address such as:<br> username@internet.com</font></center>',
//				width:300,
//				buttons: Ext.MessageBox.OK
//				//animEl: paragraph
//			});		
//		}
//		else{
//		
//		Ext.Ajax.request({
//			url: "/pass.php",
//			success: this.showForm,
//			failure: function (){
//					 Ext.MessageBox.show({
//						title: 'Error',
//						msg: '<center><font color="red">Sorry an error occured - Please try again!</font></center>',
//						width:300,
//						buttons: Ext.MessageBox.OK
//						//animEl: paragraph
//					});
//			},
//			//params: { item: this.config.companyId, group: rec.data.affiliate_id }
//			params: 'target=account_login&redirect_querystring=JSON&login_email='+Ext.get('login_email').dom.value+'&login_password='+Ext.get('login_password').dom.value+'&sid='+Math.random(),
//			scope: this
//		});
//		
//		
//		}
//}

//prop_send_msg.SelectionDialog.prototype.showForm = function(JSONhttp) {
//		eval('var data = '+JSONhttp.responseText+';');
//		if(data.success==='false' || data.success=='undefined'){
//			Ext.MessageBox.show({
//				title: 'Incorrect Login',
//				msg: '<center><font color="red">The previous request could not be processed due to the following error:<br> Invalid login credentials</font></center>',
//				width:450,
//				buttons: Ext.MessageBox.OK
//				//animEl: paragraph
//			});
//		}
//		else{
//			Ext.get('user_name').dom.innerHTML = data.session.account_name;
//			Ext.get('user_id').dom.value = data.session.account_id;
//			Ext.get('user_email').dom.innerHTML = data.session.account_email;
//			Ext.get('user_phone').dom.innerHTML = data.session.account_phone;
//			this.win_login.hide();
//			this.win.show();
//		}
//}
//
//


prop_send_msg.SelectionDialog.prototype.show = function(user) {
	copyspeed=0;
	if(!this.win){
        this.win = new Ext.Window({
            contentEl: this.config.contentEl2,
            layout:'fit',
            width:	this.config.width,
            height: this.config.height,
            closeAction: 'hide',
            plain: true,
         	autoScroll: false,
			title: this.config.title,
            buttons: [{
                text:'Send',
                scope: this,
				handler: this.save
            },{
                text: 'Cancel',
				scope: this,
                handler: function(){
                    this.win.hide();
			copyspeed=1;
                }
            }]
        });
	 	var fromDatePicker = new Ext.form.DateField({
					applyTo: 'start_date'
			});
	  	var toDatePicker = new Ext.form.DateField({
					applyTo: 'end_date'
			});
    }
//	if(!this.win_login && this.config.login){
//        this.win_login = new Ext.Window({
//            contentEl: this.config.contentEl,
//            layout:'fit',
//            width:	this.config.width,
//            height: this.config.height,
//            closeAction: 'hide',
//            plain: true,
//         	autoScroll: false,
//			title: "the Society Login",
//            buttons: [{
//                text:'Login',
//                scope: this,
//				handler: this.login
//            },{
//                text: 'Cancel',
//				scope: this,
//                handler: function(){
//                    this.win_login.hide();
//			copyspeed=1;
//                }
//            }]
//        });
//		Ext.get('get_pass').on('click', function() {
//		var href_redirect = 'index.php?level2=account&level3=account_password_retrieval' + sessionAppend();
//			if (Ext.get('login_email').dom.value != ""){
//				href_redirect += '&email=' + Ext.get('login_email').dom.value;
//			}
//			
//			gohref(href_redirect);
//		});
//    }
//	if(this.config.login)
//		this.win_login.show();
//	else
	
	this.win.show();
	
	if (user) {
		Ext.get("user_name").dom.innerHTML = user.first_name + " " + user.last_name;
		Ext.get("user_email").dom.innerHTML = user.email;
		Ext.get("user_id").dom.value = user.id;
	}
}


