// ===========
// ATTACHMENTS
// ===========

function addAttachment(startNum)
{
   if (!window.numAttachments) return;
   if (!window.maxAttachments) return;

   var addTo=document.getElementById('attachment_store');

   numAttachments++;

   var new_div=document.createElement('div');
   new_div.innerHTML=attachment_template.replace(/\_\_num_attachments\_\_/g,numAttachments);
   addTo.appendChild(new_div);
   document.getElementById('file'+numAttachments).setAttribute('unselectable','on');

   if (numAttachments==maxAttachments)
   {
      var btn=document.getElementById('add_another_button');
      if (btn) btn.disabled=true;
   }
}

function setAttachment(field_name,number)
{
   if (!window.areaedit_editors) return;
   if (!window.is_comcode_xml) return;
   if (!window.insertTextbox) return;
   if (!window.numAttachments) return;
   if (!window.maxAttachments) return;

   var post=document.getElementById(field_name);
   post=ensure_true_id(post,field_name);

   var tmp_form=post.form;
   if ((tmp_form) && (tmp_form.preview))
   {
      tmp_form.preview.checked=false;
      tmp_form.preview.disabled=true;
   }

   var done=(post.value.indexOf('[attachment]new_'+number+'[/attachment]')!=-1);
   var post_value=(post.style.display=='none')?areaedit_editors[post.id].getHTML():post.value;
   if ((!done) && (post_value.indexOf('[attachment]new_'+number+'[/attachment]')==-1))
   {
      var url=document.getElementById('file'+number).value;
      var ext=url.substring(url.length-4,4);

      if ((ext=='.png') || (ext=='.jpg') || (ext=='jpeg') || (ext=='.gif') || (ext=='.bmp'))
      {
         var vb=window.prompt("Would you like a full-size image rather than a thumbnail?",'');
         var l_yes="Yes";
         l_yes=l_yes.toLowerCase();
         var ins=(vb.toLowerCase()==l_yes)?" thumb=\"1\"":"";
      } else ins="";

      var add;
      if (is_comcode_xml(post))
      {
         add='<attachment'+ins+'>new_'+number+'</attachment>';
      } else
      {
         add='[attachment'+ins+']new_'+number+'[/attachment]';
      }

      if (post.style.display=='none')
      {
         areaedit_editors[post.id].setHTML(post_value+'<br /><br />'+add); // Can't use addHTML because it doesn't work from outside the editor when done twice
      } else
      {
         if (is_comcode_xml(post))
         {
            insertTextbox(post,'<br /><br />'+add);
         } else
         {
            insertTextbox(post,'\n\n'+add);
         }
      }
   }
   if ((number==numAttachments) && (numAttachments<maxAttachments))
      addAttachment(numAttachments+1);
}

// ====================
// COMCODE UI FUNCTIONS
// ====================

function doInput_html(field_name)
{
   if (!window.insertTextboxWrapping) return;

   var post=document.getElementById(field_name);
   post=ensure_true_id(post,field_name);
   insertTextboxWrapping(post,"html","");
}

function doInput_code(field_name)
{
   if (!window.insertTextboxWrapping) return;

   var post=document.getElementById(field_name);
   post=ensure_true_id(post,field_name);
   insertTextboxWrapping(post,"codebox","");
}

function doInput_quote(field_name)
{
   if (!window.insertTextboxWrapping) return;

   var post=document.getElementById(field_name);
   post=ensure_true_id(post,field_name);
   var va=window.prompt("Enter the quote source (e.g. a person's name), or leave blank for a sourceless quote.",'');
   if (!va) va='';
   insertTextboxWrapping(post,"[quote=\""+va+"\"]","[/quote]");
}

function doInput_menu(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va=window.prompt("Enter the name of the menu you would like to use. The following menus are defined: \"+(document.getElementById(field_name).form.menu_items.value)+\"",'');
   var vb=window.prompt("Enter the caption you would like to use for the menu",'');
   var add;
   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (is_comcode_xml(element))
   {
      add="<block><blockParam key=\"type\" val=\"tree\" /><blockParam key=\"caption\" val=\""+escape_html(vb)+"\" /><blockParam key=\"param\" val=\""+escape_html(va)+"\" />side_stored_menu</block>";
   } else
   {
      add="[block=\""+escape_comcode(va)+"\" caption=\""+escape_comcode(vb)+"\" type=\"tree\"]side_stored_menu[/block]";
   }
   insertTextbox(element,add);
}

function doInput_block(field_name)
{
	window.open('http://www.bbwparties.com/ocPortal/data/block_helper.php?field_name='+field_name+keep_stub(),'','width=500,height=500,status=no,resizable=yes,scrollbars=yes');
}

function doInput_list(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var post=document.getElementById(field_name);
   post=ensure_true_id(post,field_name);
   var va;
   var add=Array();
   insertTextbox(post,"\n");
   do
   {
      va=window.prompt("Enter the next list entry (or press enter or cancel to finish list)",'');
      if ((va!=null) && (va!='')) add.push(va);
   }
   while ((va!=null) && (va!=''));
   if (add.length==0) return;
   var i;
   if (is_comcode_xml(post))
   {
      insertTextbox(post,"<list>\n")
      for (i=0;i<add.length;i++)
      {
         insertTextbox(post,"<list_element>"+va+"</list_element>\n")
      }
      insertTextbox(post,"</list>\n")
   } else
   {
      for (i=0;i<add.length;i++)
      {
         insertTextbox(post," - "+va+"\n")
      }
   }
}

function doInput_hide(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va=window.prompt("Enter the warning to be shown in place of the text",'');
   if (va!=null) var vb=window.prompt("Enter the text that is only shown if you choose to view it",''); else return;
   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (vb!=null)
   {
      if (is_comcode_xml(element))
      {
         insertTextbox(element,"<hide><hideTitle>"+va+"</hideTitle>"+escape_html(vb)+"</hide>");
      } else
      {
         insertTextbox(element,"[hide=\""+escape_comcode(va)+"\"]"+escape_comcode(vb)+"[/hide]");
      }
   }
}

function doInput_thumb(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va="";
   var broken;
   do
   {
      broken=false;
      va=window.prompt("Enter the URL",va);
      if ((va!=null) && (va.indexOf('://')==-1))
      {
         window.alert("This was not a valid URL (URLs contain '://')");
         broken=true;
      }
   }
   while ((va!=null) && (broken));

   if (!va) return;

   var vb=window.confirm("Would you like a full-size image rather than a thumbnail?",'');

   var vc=window.prompt("Enter the caption you would like to use for the image. You can leave this blank, but for accessibility you can specify an appropriate summary for the image content.",'');
   if (!vc) vc='';

   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (vb)
   {
      if (is_comcode_xml(element))
      {
         insertTextbox(element,"<img url=\""+escape_html(va)+"\">"+escape_html(vc)+"</img>");
      } else
      {
         insertTextbox(element,"[img=\""+escape_comcode(vc)+"\"]"+escape_comcode(va)+"[/img]");
      }
   } else
   {
      if (is_comcode_xml(element))
      {
         insertTextbox(element,"<thumb caption=\""+escape_html(vc)+"\">"+escape_html(va)+"</thumb>");
      } else
      {
         insertTextbox(element,"[thumb caption=\""+escape_comcode(vc)+"\"]"+escape_comcode(va)+"[/thumb]");
      }
   }
}

function doInput_attachment(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va=window.prompt("Enter the attachment number (the new attachment you want to be placed at this position)",'');
   if (!isInteger(va))
   {
      window.alert("This is not a valid attachment number");
      return;
   }
   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (is_comcode_xml(element))
   {
      insertTextbox(element,"<attachment>new_"+va+"</attachment>");
   } else
   {
      insertTextbox(element,"[attachment]new_"+va+"[/attachment]");
   }
}

function doInput_url(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va="";
   var broken;
   do
   {
      broken=false;
      va=window.prompt("Enter the URL",va);
      if ((va!=null) && (va.indexOf('://')==-1))
      {
         window.alert("This was not a valid URL (URLs contain '://')");
         broken=true;
      }
   }
   while ((va!=null) && (broken));

   var vb;
   if (va!=null) vb=window.prompt("Enter the link name",''); else return;
   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (is_comcode_xml(element))
   {
      if (vb!=null) insertTextbox(element,"<url param=\""+escape_html(va)+"\">"+escape_html(vb)+"</url>");
   } else
   {
      if (vb!=null) insertTextbox(element,"[url=\""+escape_comcode(vb)+"\"]"+escape_comcode(va)+"[/url]");
   }
}

function doInput_page(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va=window.prompt("Enter the zone codename that the page is in",'');
   if (va!=null) var vb=window.prompt("Enter the page codename",''); else return;
   if (vb!=null) var vc=window.prompt("Enter the caption",''); else return;

   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (is_comcode_xml(element))
   {
      if (vb!=null) insertTextbox(element,"<page pageLink=\""+escape_html(va)+":"+escape_html(vb)+"\">"+escape_html(vc)+"</page>");
   } else
   {
      if (vb!=null) insertTextbox(element,"[page=\""+escape_comcode(va)+":"+escape_comcode(vb)+"\"]"+escape_comcode(vc)+"[/page]");
   }
}

function doInput_email(field_name)
{
   if (!window.insertTextbox) return;
   if (!window.is_comcode_xml) return;

   var va="";
   var broken;
   do
   {
      broken=false;
      va=window.prompt("Enter the e-mail address",va);
      if ((va!=null) && (va.indexOf('@')==-1))
      {
         window.alert("You specified a non-valid e-mail address (e-mail addresses contain '@')");
         broken=true;
      }
   }
   while ((va!=null) && (broken));

   var vb;
   if (va!=null) vb=window.prompt("Enter the caption",''); else return;
   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   if (is_comcode_xml(element))
   {
      if (vb!=null) insertTextbox(element,"<email address=\""+escape_html(va)+"\">"+escape_html(vb)+"</email>");
   } else
   {
      if (vb!=null) insertTextbox(element,"[email=\""+escape_comcode(vb)+"\"]"+escape_comcode(va)+"[/email]");
   }
}

function doInput_b(field_name)
{
   if (!window.insertTextboxWrapping) return;

   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   insertTextboxWrapping(element,"b","");
}

function doInput_i(field_name)
{
   if (!window.insertTextboxWrapping) return;

   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   insertTextboxWrapping(element,"i","");
}

function doInput_font(field_name)
{
   if (!window.insertTextboxWrapping) return;
   if (!window.is_comcode_xml) return;

   var element=document.getElementById(field_name);
   element=ensure_true_id(element,field_name);
   var form=element.form;
   var face=form.elements['f_face'];
   var size=form.elements['f_size'];
   var colour=form.elements['f_colour'];
   if ((face.value=='') && (size.value=='') && (colour.value==''))
   {
      window.alert("You must select at least one font option");
      return;
   }
   if (is_comcode_xml(element))
   {
      insertTextboxWrapping(document.getElementById(field_name),'<font param=\"'+escape_html(face.value)+'\" color=\"'+escape_html(colour.value)+'\" size=\"'+escape_html(size.value)+'\">','</font>');
   } else
   {
      insertTextboxWrapping(document.getElementById(field_name),'[font=\"'+escape_comcode(face.value)+'\" color=\"'+escape_comcode(colour.value)+'\" size=\"'+escape_comcode(size.value)+'\"]','[/font]');
   }
}

function setFontSizes(list)
{
   var i=0;
   for (i=1;i<list.options.length;i++)
   {
      list.options[i].style.fontSize=list.options[i].value+"em";
   }
}

function desetFontSizes(list)
{
   var i=0;
   for (i=1;i<list.options.length;i++)
   {
      list.options[i].style.fontSize="";
   }
}

function init_form_saving()
{
   var posting_form=document.getElementById('posting_form');
   var i,name,fields_to_do=[],cookie_name,fields_to_do_counter=0,cookie_value;
   for (i=0;i<posting_form.elements.length;i++)
   {
      name=posting_form.elements[i].name;
      if ((name!='') && ((posting_form.elements[i].nodeName.toLowerCase()=='textarea') || (posting_form.elements[i].getAttribute('type')=='text')))
      {
         cookie_name='ocp_autosave_'+window.location.pathname+window.location.search+':'+name;
         cookie_name=cookie_name.replace(/[\.=,; \t\r\n\013\014]/g,'');
         cookie_value=ReadCookie(encodeURIComponent(cookie_name));
         if ((cookie_value!='') && (cookie_value!='<<IGNOREME>>'))
         {
            fields_to_do[name]=ReadCookie(encodeURIComponent(cookie_name));
            fields_to_do_counter++;
         }
         addEventListenerAbstract(posting_form.elements[i],'keypress',handle_form_saving);
         posting_form.elements[i].externalonKeyPress=handle_form_saving;
      }
   }
   if (fields_to_do_counter!=0)
   {
      var key;
      if (window.confirm('You appear to have unsaved form data. Would you like to auto-fill this form with your prior data?'))
      {
         for (key in fields_to_do)
         {
            if ((posting_form.elements[key]) && (posting_form.elements[key].style))
            {
               if (posting_form.elements[key].style.display=='none')
               {
                  areaedit_editors[posting_form.elements[key].id].setHTML(fields_to_do[key]);
               } else
               {
                  posting_form.elements[key].value=fields_to_do[key];
               }
            }
         }
      } else
      {
         for (key in fields_to_do)
         {
            cookie_name='ocp_autosave_'+window.location.pathname+window.location.search+':'+key;
            cookie_name=cookie_name.replace(/[\.=,; \t\r\n\013\014]/g,'');
            SetCookie(encodeURIComponent(cookie_name),'<<IGNOREME>>',1);
         }
      }
   }
}

function handle_form_saving(event,target)
{
   if (!event) event=window.event;
   if (!target)
   {
      var target=(event.target)?event.target:event.srcElement;
   }
   var cookie_name='ocp_autosave_'+window.location.pathname+window.location.search+':'+target.name;
   cookie_name=cookie_name.replace(/[\.=,; \t\r\n\013\014]/g,'');
   if (target.style.display=='none')
   {
      SetCookie(encodeURIComponent(cookie_name),areaedit_editors[target.id].getHTML()+String.fromCharCode(event.keyCode?event.keyCode:event.charCode),1);
   } else
   {
      SetCookie(encodeURIComponent(cookie_name),target.value+String.fromCharCode(event.keyCode?event.keyCode:event.charCode),1);
   }
}


