// JavaScript Document

function LaunchProfileWindow (url, title, h, w) {
	new Ext.Window(
		{	id:'addwin',
			 title:title,
			 width:w,
			 height:h,
			 autoScroll:true,
			 modal:true,
			 autoLoad:{url:url, text:'loading', scripts:true}
		}
	).show();
}
 
 
 
function uploadImageForm (customer, am_id, amt_id, server) {
formPanel = new Ext.FormPanel({
	autoHeight:true,
	width: 550, 
	applyTo : 'upimage',								
	url: server + 'upload_profile_media.cfm', // where to post to
	fileUpload: true, //form will include a file upload (sets enctype?)
	method: 'POST',
	timeout: 15000, //allow for big files on slow connections
	baseParams: { customer:customer,am_id:am_id,amt_id:amt_id}, // hidden values to pass along
	items: [
		{ tabIndex: 1,  xtype:'textfield', fieldLabel: 'Image', name: 'Filedata', inputType: 'file', width: 350, height: 25},
	new Ext.form.Hidden({
	id:'filename',
	name:'filename',
	value:''})	
	],
	buttons: [{
	id:'absUploadImage',
	name:'absUploadImage',
	text: 'Upload Image',
	method:'POST',
	handler: function() {
		var form = formPanel.getForm();
		var dosubmit = true;
		var isJpeg = false;
		//Do your validation here..findField('pm_webR').getValue()
		if (form.findField('Filedata').getValue().length == 0){
				Ext.MessageBox.alert('Alert:' , 'Please enter a FILE to UPLOAD!');
				form.findField('Filedata').focus();
				dosubmit = false;
		} else if (form.findField('Filedata').getValue().length >= 1) {
			//Check if the uploaded file is a JPEG
			filePartArray = form.findField('Filedata').getValue().split(".");
			fileName = filePartArray[filePartArray.length - 2];
			fileExt = filePartArray[filePartArray.length - 1];
			
			var FindChars = /,|'|"/g;
			var testChars = FindChars.test(fileName);
						
			if(fileExt == 'jpg' || fileExt == 'jpeg' || fileExt == 'JPG' || fileExt == 'JPEG' || fileExt == 'pjpeg' || fileExt == 'PJPEG'){
				isJpeg = true;	
				
			} 
			
			
			if (!isJpeg || testChars) {
				Ext.MessageBox.alert('Important:' , '<div align="center">Only <b>JPG</b> image files allowed. Also make sure filename has<br>no <b>invalid</b> characters such as commas, apostrophes, or slashes.<br> Filename: <font color="red">' + form.findField('Filedata').getValue() + '</font><br><b>File not uploaded. Try again.</b></div>');
				dosubmit = false;
			} 				
		}
		
		//If submission is True, run the following.
		if (dosubmit) {
			//generate a new filename (This makes sure we have a standing imagenamimg system the CF server will not reject)
			var chars = "abcdefghijklmnopqrstuvwxyz123456789";
			var string_length = 4;
			var randomstring = '';
			for (var i=0; i<string_length; i++) {
				var rnum = Math.floor(Math.random() * chars.length);
				randomstring += chars.substring(rnum,rnum+1);
			}
			
			form.findField('filename').setValue(customer + '_' + randomstring + '.jpg'); // We would remame filename to this on the server.
			form.submit({
				waitMsg: 'Uploading...',
				success: function(form, action) {
					//Ext.MessageBox.alert('Alert:' ,'success'); 
					//remove the image node if it exists
					if(Ext.get('imageNode') != null){
						Ext.get('imageNode').remove();	
					}
					
					//create a node to display the new image.
					Ext.get('imageArea').createChild( {tag:'div',id:'imageNode'});
					loadPageDiv ('imageArea', '/plugins/profile/thumbnails.cfm');
					},
				failure: function(form, action) {Ext.MessageBox.alert('Alert:' ,'failed')},
				scope: this
			}); 
		}
	},
	scope: this
	}]
	/*,listeners:{actioncomplete : function(form,action){		
				var rep = action.response.responseText;
				var rep2 = action; //console.log(action);
				debugger;}}*/
	});
 }
 

function loadPageDiv (div, url, loadHeader) {
	//allows the reloading of header.
	if (loadHeader != undefined) {
		var header_url = '/header.cfm'; 
		var reloadheader = Ext.get('showHeader');
			reloadheader.load({url:header_url,scripts:true});
	}
	
	//allows the reloading of thumbnails.
	var showPage = Ext.get(div);
		showPage.mask('Loading');	
		showPage.load({url:url,scripts:true,callback:callback,timeout : 180000});
		
		//Call back function
		var callback = function(){
			showPage.unmask();
	}
}

function delete_confirm(action_url, div, load_url) {
	Ext.Msg.confirm('Confirm Delete'  , 'Are you sure you want to remove this image?', function(btn, text){
			if (btn == 'yes'){
				// If Yes button is clicked. 
				process(action_url, div, load_url);
			} ///end if
		}//end function 
	);// end Ext
}


function process(action_url, div, load_url) {
	Ext.Ajax.request({url:action_url,method: 'GET'
	  ,success: function (result, request) {
		  		//Ext.MessageBox.alert('Alert', action_url); 
				loadPageDiv (div, load_url, 'yes')
				}
	  ,failure: function ( result, request) { 
				Ext.MessageBox.alert('Failed', result.responseText); 
				} 
	  });
}

function launchWindow (url) {
	var win;
	if (navigator.appName == "Microsoft Internet Explorer") {
	  win = window.open(url,'','location=1,status=0,scrollbars=1,toolbar=0,width=1024,height=850');
	} else {
	  win = window.open(url,'','location=0,status=0,scrollbars=1,toolbar=1,width=1024,height=850');
	}
	
	win.focus();
}

function ShowProfileMenuList (e, url, customer) {	
	var menuXY = new Array();
		menuXY[0] = e.clientX;
		menuXY[1] = e.clientY;

	var menu = new Ext.menu.Menu({
			id: 'basicMenu',
			minWidth:135,
			items: [{text: 'Edit Profile Picture'
						,iconCls:'iconProfile'
						,handler: function(){LaunchProfileWindow('/plugins/profile/main.cfm','Upload & Manage Profile Pictures',500,640);}}
				    
					,{text:'Account Settings',iconCls:'iconUserPassword'
					  	,menu:{items:[
								    {text: 'Change Name',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/demographics/change_name.cfm?bSNS=FALSE','Change Name',480,640);}}
									,{text: 'Change Username',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/demographics/tnt_changeuserid.cfm?bSNS=FALSE','Change User Name',480,640);}}
									,{text: 'Change Password',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/demographics/tnt_passwordupdate.cfm?bSNS=FALSE','Change Password',480,640);}}
									,{text: 'Change Address, Phone etc...',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/demographics/cascontact.cfm?bSNS=FALSE','Change Address, Phone, Fax, Email',480,640);
								}}
									,{text: 'Specialities',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/demographics/rsna_special.cfm?bSNS=FALSE','Specialties',480,640);}}
									,{text: 'Spouse',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/demographics/rsna_spouse_form.cfm?bSNS=FALSE','Spouse',480,640);}}
									,{text: 'MOC Registry',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/moc/moc_reg.cfm?bSNS=FALSE','MOC Registry',500,640);}}
								]}
					  }
					  
					,{text:'RSNA Information',iconCls:'viewer-INFO'
					  	,menu:{items:[
									{text: 'Membership Renewal',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/mbrapp/appfiles/membercheck.cfm?bSNS=FALSE','Membership Renewal',480,640);}}
									,{text: 'Current Online Enrollments',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchWindow('http://rsnamx.rsna.org/angel/check_account.cfm?memberid='+ customer);}}
									,{text: 'Disclosure Form',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/disclosure/disclosure.cfm?bSNS=FALSE','Disclosure Form',480,640);}}
									,{text: 'Publisher Partners',iconCls:'viewer-BULLET-BLACK'
										,handler: function(){launchExtWindow(url + '/timssnet/partners/partners.cfm?bSNS=FALSE','Publisher Partners',480,640);}}
								]}
					  }
						
					,'-',{text:'Cancel',iconCls:'menu-CANCEL',handler: function(){this.destroy();}}
				]
		  });
	
	menu.showAt(menuXY);
}
