# Surgeweb plain HTML interface
# This file handles ui actions either as whole page requests/updates (or in the case of ajax to page updates)

function swcmd_s(input,e)
{
	swcmd(input.value,e,input);
	if(input.value=='sw_copy' || input.value=='sw_move'){
#		do nothing
	}else{
		input.options[0].selected = true;
	}
}
function swcmd(action,e,input)
{
#	alert("requested to:"+action+" the messages="+sel.join(','));
	if (action=='sw_delete') 	do_delete();
	else if (action=='sw_read')		do_mark_read();
	else if (action=='sw_unread')	do_mark_unread();
	else if (action=='sw_reply_all')	do_reply_all();
	else if (action=='sw_forward') 	do_forward();
	else if (action=='sw_forward_attach') 	do_forward_attach();
	else if (action=='sw_redirect') 	do_redirect();
	else if (action=='sw_modify') 	do_edit();
	else if (action=='sw_reply') 	do_reply();
	else if (action=='sw_copy') 	do_copy_show(e,input);
	else if (action=='sw_move') 	do_move_show(e,input);
	else alert('Sorry  don\'t know how to '+action+' yet');
}

function S(cmd,page)
{
	simple_submit(cmd,page);
}
function M(item)
{
	document.main.msg_id.value=item
	simple_submit('show','msg.htm');
}
function FolderShow(item, start, refresh)
{
	document.main.refresh.value="true";
	document.main.fld_id.value=item
	simple_submit('show','sw.htm');
}

function del(item)
{
	var f=document.main;
	f.cmd.value='delete';
	f.msg_id.value="";
	f.sel.value=item;
#	f.ui.value='raw';
	f.submit();
}


function show_raw(mid)
{
	open_popup('cmd=msgpart&sid='+sid+'&fld_id='+xescape(sw.fld_id)+'&msg_id='+mid+'&part_id=0');
}
function show_text(mid,pid)
{
#	headers now say text/plain instead of text/html so no web encoding should be required
	open_popup('cmd=msgpart&sid='+sid+'&fld_id='+xescape(sw.fld_id)+'&msg_id='+mid+'&part_id='+pid);
}
function show_html(mid,pid)
{
	open_popup('cmd=msgpart&sid='+sid+'&fld_id='+xescape(sw.fld_id)+'&msg_id='+mid+'&part_id='+pid+'&process=simple_wash');
}


#||send_type|| [compose / reply / reply_all / forward / forward_attach / redirect]

function do_compose(){
	document.main.send_type.value='compose';
	document.main.msg_id.value='';	
	simple_submit('show','edit.htm');
}
function do_reply(){
	document.main.send_type.value='reply';
	simple_submit('show','edit.htm');
}
function do_forward(){
	document.main.send_type.value='forward';
	simple_submit('show','edit.htm');
}
function do_forward_attach(){
	document.main.send_type.value='forward_attach';
	simple_submit('show','edit.htm');
}
function do_redirect(){
	document.main.send_type.value='redirect';
	simple_submit('show','edit.htm');
}
function do_edit(){
	document.main.send_type.value='edit';
	simple_submit('show','edit.htm');
}

function simple_submit(cmd,page,epage)
{
	var f=document.main;
	f.cmd.value=cmd;
	f.page.value=page;
	if (epage)
		f.epage.value=epage;
	if(isIOS)
		f.with_plain.value='true';
	f.submit();
}
function edit_discard(){
	if (!confirm("$$st_opt_discard_q$$")) return;
	simple_submit('show','sw.htm');
}
function do_save_draft(){
	msg_from_editor();
	set_compose_refresh();
	simple_submit('save_draft,show','edit.htm');
}
function do_send_move(){
	alert("Not implemented");
}
function do_send(){
	msg_from_editor();	
	set_compose_refresh(); // only used if send error
	simple_submit('send','sw.htm','edit.htm');
}
function do_delete(){
	simple_submit('delete,show','sw.htm');
}
function do_spam(){
	if (!confirm("$$st_spam_unknown$$"))
		return;		
	do_delete();
}
function do_mark_read(){
	document.main.flags_action.value='set';
	document.main.flags.value='seen';
	simple_submit('set_flags,show','sw.htm');
}
function do_mark_unread(){
	document.main.flags_action.value='clear';
	document.main.flags.value='seen';
	simple_submit('set_flags,show','sw.htm');
}
function app_menu(type){
	if(type=='mail'){
		document.main.iframe_content.value='';
		simple_submit('show','sw.htm');
	}else if(type=='contacts'){
		document.main.iframe_content.value=type;
		simple_submit('show','contacts.htm');
	}else{
		document.main.iframe_content.value=type;
		simple_submit('show','iframe.htm');
	}
}
function do_refresh(fld_id){
	FolderShow(fld_id,undefined,true);
}
function do_purge(fld_id){
	var o=document.main;
	o.fld_id.value=fld_id;
	o.sel_all.value='true';
	simple_submit('delete,show','sw.htm');
}
function manage_folders(fld_id){
	simple_submit('show','folders.htm');
}
function do_history(mid){
	alert("Not implemented");
}
function do_options(){
	simple_submit('show','options.htm');
}

function options_save(){
	simple_submit('save_options,show','sw.htm','options.htm');
}
function options_discard(){
	simple_submit('show','sw.htm');
}
function do_reply_all(){
	alert("$$st_reply_only$$");
	do_reply();
}

function nav(item){
	document.main.start.value=item;	
	simple_submit('show','sw.htm');
}

function do_attach()
{
	msg_from_editor();
	set_compose_refresh(); // only used if send error
	simple_submit('show','attach.htm');
}


function do_attach_upload()
{
	attach_uploading();
	simple_submit('attach_upload,show','attach.htm');
}
function do_attach_remove()
{
	var new_ids=[];
	var ids=document.main.attach_ids.value.split(',');
	for (var i=0;i<ids.length;i++){
		var o=dge('sel_'+ids[i]);
		if(o.checked) {
			class_add(o.parentNode,'hidden');
		}else{
			new_ids.push(ids[i]);
		}
	}
	document.main.attach_ids.value=new_ids.join(',');
}
var dots=1;
function attach_uploading(){
	var val="Uploading ";
	if (dots>=1) val=val+".";else val=val+" ";
	if (dots>=2) val=val+".";else val=val+" ";
	if (dots>=3) val=val+".";else val=val+" ";
	document.main.do_upload.value=val;
	dots=(dots+1)%4;
	setTimeout('attach_uploading()',500);
}
function live_refresh(){
	switch_to('html');
}



function do_copy_show(e,input)
{
	do_folder_menu_setup(e,'folder_menu',true);
#	do_generic_menu_setup('folder','folder_menu',(e&&e.shiftKey));
	do_menu_show('folder_menu', e, true, function(result,fld_id){
		if(result!='ok'){
			input.value='';
			return;	
		}
		ignore_folderFF=true;
		document.main.destination.value=fld_id;
		simple_submit('msg_copy','sw.htm');
	});
}
function do_move_show(e,input)
{
	do_folder_menu_setup(e,'folder_menu',true);
#	do_generic_menu_setup('folder','folder_menu',(e&&e.shiftKey));
	do_menu_show('folder_menu',e,true,	function(result,fld_id){
		if(input) input.value='';
		if(result!='ok'){
			return;	
		}
		ignore_folderFF=true;
		document.main.destination.value=fld_id;
		simple_submit('msg_move','sw.htm');
	});
}


function show_contact(abook,group,cid)
{
	document.main.abook.value=abook;
	document.main.group.value=group;
	document.main.server_cid.value=cid;
	simple_submit('show','contacts.htm');
}

function edit_contact(abook,group,cid)
{
	document.main.abook.value=abook;
	document.main.group.value=group;
	document.main.server_cid.value=cid;
	document.main.editing.value='true';
	simple_submit('show','contacts.htm');
}
function email_contact(name,email)
{
	if(name.indexOf(',')!=-1 || name.indexOf(';')!=-1 && name.substring(0,1)!='"')
		name='"'+name+'"';
	if(name.length>0) email=name+' <'+email+'>';
	document.main.to.value=email;
	document.main.msg_id.value="";
	simple_submit('show','edit.htm');
}
function save_contact(abook,group,cid)
{
	document.main.abook.value=abook;
	document.main.group.value=group;
	document.main.server_cid.value=cid;

	document.main.contact_name.value="{'first':'"+encode_quotes(document.main.x_firstname.value)+"','last':'"+encode_quotes(document.main.x_lastname.value)+"'}]";
#	document.main.contact_email.value="[{type:'work',value:'"+encode_quotes(document.main.x_email.value)+"'}]";
	document.main.contact_phone.value="[{'type':'work',value:'"+encode_quotes(document.main.x_phone.value)+"'}]";
	document.main.contact_address.value="[{'type':'work',value:'"+encode_quotes(document.main.x_address.value)+"'}]";
	
	simple_submit('contact_update,show','contacts.htm');
}

function new_contact(abook,group)
{
	document.main.abook.value=abook;
	document.main.group.value=group;
	document.main.server_cid.value='new';
	document.main.editing.value='true';
	simple_submit('show','contacts.htm');
}
function create_group()
{
	val=prompt('$$st_ct_newgroup$$:','');	
	if(!val || val.length==0) return;
	document.main.contact_name.value=val;
	document.main.group.value='true';
	simple_submit('contact_add,show','contacts.htm');
}
function delete_contact(abook,group,cid)
{
	document.main.abook.value=abook;
	document.main.group.value=group;
	document.main.server_cid.value=cid;
	simple_submit('contact_delete,show','contacts.htm');
}

function do_usercgi(type)
{
	document.main.iframe_type.value=type;
	app_menu('usercgi')
}

function contact_click(e)
{
	var t=target(e);
	var email=t.getAttribute('rcpt_addr');
	var name=t.getAttribute('rcpt_name');
	email_contact(name,email);
}
