/*
 *	San Luis Print and Copy
 *	Customer Multiple File Upload
 *
 *	Created By
 *	Mike Holliday
 *	GFL Systems, Inc.
 *
 *
 */

// initialize QuickTips
Ext.QuickTips.init();
Ext.QuickTips.enable();

Ext.form.Field.prototype.msgTarget = 'side';

// reference local blank image
Ext.BLANK_IMAGE_URL = '/ext/resources/images/default/s.gif';

// create namespace
Ext.namespace('SLPC');

// create application
SLPC.app = function() {

    // do NOT access DOM from here; elements don't exist yet


    // private variables


    // private functions
	var filelist = new Array();	//list of all the files uploaded
	var goodUpload = false;		//keep of status of upload across multiple files

	var frmUploader = new Ext.ux.UploadDialog.Dialog({
		url: 'upload-dialog-request.php',
		id: 'uploadDialog',
		post_var_name: 'file',
		reset_on_hide: false,
		allow_close_on_upload: false,
		upload_autostart: false,
		draggable: false,
		Width: 400,
		Height: 400,
		modal: true,
		constraintoviewport: true,
		resizable: false
	});

	frmUploader.on({
		uploadfailed: function(frmUploader, filename) {
			goodUpload = false;
			alert('The upload failed!  Please try again and contact us if you continue having problems.');
		},
		uploaderror: function(frmUploader, filename, response, record) {
			goodUpload = false;
			alert(record.note);
			alert('There was an error while uploading the files!  Please try again and contact us if you continue having problems.');
		},
		uploadcanceled: function(frmUploader, filename) {
			goodUpload = false;
			alert('You have canceled the upload');
		},
		uploadstop: function(frmUploader, filename) {
			goodUpload = false;
			alert('You have canceled the upload');
		},
		uploadsuccess: onUploadSuccess,
		uploadcomplete: onUploadComplete
	});

	function onUploadSuccess(frmUploader, filename, response){
			filelist.push(filename);  //add file name to file list

			if (response.success)	//if success: true
			{
				goodUpload = true;	//mark upload as completed (true)
			};
		}

	function onUploadComplete(){

		if (goodUpload)
		{
			Ext.Ajax.request({
			url: 'upload-dialog-emails.php',
			method : 'POST',
			params: {
	      			CompanyName: Ext.get('txtCompanyName').getValue(),
	      			ContactName: Ext.get('txtContactName').getValue(),
	      			Phone: Ext.get('txtPhone').getValue(),
	      			EmailAddress: Ext.get('txtEmail').getValue(),
	      			Comments: Ext.get('txtComments').getValue(),
					filelist: Ext.encode(filelist)
				},
			success:function(){
					Ext.Msg.alert('Upload Status', 'Upload Completed. We have received your files.  You will be receiving a confirmation email shortly.  Thank You.', function(btn, text){
						if (btn == 'ok'){
							window.location = 'http://www.sanluisprintandcopy.com/';
						}
		        	});
				},
		   	failure: function(){
		   			Ext.Msg.alert('Upload Status', 'Upload Completed. We have received your files, but there was a problem updating our system.  Please call us at 805-546-0704 to inform us of your upload.');
					}

			});
		}
		else
		{
			alert('There was an error while uploading the files!  Please try again and contact us if you continue having problems.');
		}
	}

	var frmCustomerInfo = new Ext.FormPanel({
		formId: 'frmCustomerInfo',
		labelWidth: 110,
		url: 'postCustomerInfo.php',
		title: 'Customer Information',
		bodyStyle: 'padding:5px 5px 0',
		frame: false,
		bodyBorder: true,
		border: true,
		width: 400,
        defaults: {width: 200},
        defaultType: 'textfield',
        monitorValid: true,

        items: [{
               fieldLabel: 'Company Name',
               name: 'txtCompanyName',
               id: 'txtCompanyName',
               allowBlank:true
           },{
               fieldLabel: '<span class="RequiredStar">*</span>Contact Name',
               name: 'txtContactName',
               id: 'txtContactName',
               allowBlank:false
           },{
               fieldLabel: '<span class="RequiredStar">*</span>Phone Number',
               name: 'txtPhone',
               id: 'txtPhone',
               allowBlank:false
           },{
               fieldLabel: '<span class="RequiredStar">*</span>Email Address',
               name: 'txtEmail',
               id: 'txtEmail',
               vtype:'email',
               allowBlank:false
           },{
			fieldLabel: 'Comments',
               name: 'txtComments',
               id: 'txtComments',
               width: 270,
               xtype: 'textarea',
               allowBlank: true
           }],

       	buttons: [{
       		text: 'Continue to Upload',
       		formBind: true,
       		scope: this,
            handler: function(){
					frmUploader.show()
			}
       	}]

	});



    // public space
    return {
        // public properties, e.g. strings to translate

        // public methods
        init: function() {
            frmCustomerInfo.render('CustomerInformation');
        }


    };
}(); // end of app


