/* DO NOT EDIT. THIS IS A CACHE FILE AND WILL GET OVERWRITTEN RANDOMLY.
INSTEAD EDIT THE TEMPLATE FROM WITHIN THE ADMIN ZONE, OR BY MANUALLY EDITING A TEMPLATES_CUSTOM OVERRIDE. */

/* Form editing code (mostly stuff only used on posting forms) */

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

function addAttachment(startNum)
{
	if (typeof window.numAttachments=='undefined') return;
	if (typeof window.maxAttachments=='undefined') return;

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

	numAttachments++;

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

	replaceFileInput('attachment','file'+numAttachments);

	if (numAttachments==maxAttachments)
	{
		var btn=document.getElementById('add_another_button');
		if (btn) btn.disabled=true;
	}
	
	if (typeof window.trigger_resize!='undefined') trigger_resize();
}

function setAttachment(field_name,number,filename)
{
	if (typeof window.areaedit_editors=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') return;
	if (typeof window.insertTextbox=='undefined') return;
	if (typeof window.numAttachments=='undefined') return;
	if (typeof window.maxAttachments=='undefined') 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) || (post.value.indexOf('[attachment_safe]new_'+number+'[/attachment_safe]')!=-1);
	var post_value=((post.style.display=='none') || ((window.wysiwyg_on) && (wysiwyg_on()) && (post.parentNode.id=='container_for_post')))?areaedit_editors[post.id].getHTML():post.value;
	if ((!done) && (post_value.indexOf('[attachment]new_'+number+'[/attachment]')==-1) && (post_value.indexOf('[attachment_safe]new_'+number+'[/attachment_safe]')==-1))
	{
		var url=filename;
		if ((!filename) && (document.getElementById('file'+number)))
		{
			url=document.getElementById('file'+number).value;
		}

		var ext=url.substr(url.length-4,4).toLowerCase();
		var ins='',ins2='';

		
		var caption_field=document.getElementById('caption'+number);
		if (caption_field.value=='')
		{
			if (url.indexOf('.x/')!=-1)
			{
				caption_field.value=url.substr(url.indexOf('.x/')+3);
			}
			else if (url.indexOf('.x\\')!=-1)
			{
				caption_field.value=url.substr(url.indexOf('.x\\')+3).replace(/\\/g,'/');
			}
		}

		if ((ext=='.tar') || (ext=='.zip') || (ext=='.png') || (ext=='.jpg') || (ext=='jpeg') || (ext=='.gif') || (ext=='.bmp'))
		{
			var vb=generate_question_ui("Would you like a full-size image rather than a smaller thumbnail?",['Thumbnail','Image'],'Attachment');
			ins=(vb=='Thumbnail')?" thumb=\"1\"":"_safe thumb=\"0\"";
			ins2=(vb=='Thumbnail')?"":"_safe";
		}

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

		if ((post.style.display=='none') || ((window.wysiwyg_on) && (wysiwyg_on()) && (post.parentNode.id=='container_for_post')))
		{
			areaedit_editors[post.id].insertHTML(areaedit_editors[post.id].getSelectedHTML()+'<span class="ocp_keep">'+add+'</span>'/*+'<br /><br />'*/);
		} 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 (typeof window.insertTextboxWrapping=='undefined') return;

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

function doInput_code(field_name)
{
	if (typeof window.insertTextboxWrapping=='undefined') return;

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

function doInput_quote(field_name)
{
	if (typeof window.insertTextboxWrapping=='undefined') 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===null) return;
	insertTextboxWrapping(post,"[quote=\""+va+"\"]","[/quote]");
}

function doInput_box(field_name)
{
	if (typeof window.insertTextboxWrapping=='undefined') return;

	var post=document.getElementById(field_name);
	post=ensure_true_id(post,field_name);
	var va=window.prompt("Enter the box title",'');
	if (va===null) return;
	insertTextboxWrapping(post,"[box=\""+va+"\" type=\"light\"]","[/box]");
}

function doInput_menu(field_name)
{
	if (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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)
{
	var url='http://www.wakustomz.net'.replace(/^http:/,window.location.protocol)+'/data/block_helper.php?field_name='+field_name+keep_stub();
	url=url+'&block_type='+(((field_name.indexOf('edit_panel_')==-1) && (window.location.href.indexOf(':panel_')==-1))?'main':'side');
	window.open(url,'','width=750,height=600,status=no,resizable=yes,scrollbars=yes');
}

function doInput_list(field_name)
{
	if (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') return;

	var post=document.getElementById(field_name);
	post=ensure_true_id(post,field_name);
	var va;
	var add=[];
	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>"+add[i]+"</list_element>\n")
		}
		insertTextbox(post,"</list>\n")
	} else
	{
		if (post.value.indexOf('[semihtml')!=-1)
			insertTextbox(post,"[list]\n")
		for (i=0;i<add.length;i++)
		{
			if (post.value.indexOf('[semihtml')!=-1)
			{
				insertTextbox(post,"[*]"+add[i]+"\n")
			} else
			{
				insertTextbox(post," - "+add[i]+"\n")
			}
		}
		if (post.value.indexOf('[semihtml')!=-1)
			insertTextbox(post,"[/list]\n")
	}
}

function doInput_hide(field_name)
{
	if (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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 (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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=generate_question_ui("Would you like a full-size image rather than a smaller thumbnail?",['Thumbnail','Image'],'Attachment');

	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=='Image')
	{
		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 (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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 (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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 (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') return;

	var result;

	if (typeof window.showModalDialog!='undefined')
	{
		result=window.showModalDialog('http://www.wakustomz.net/data/page_link_chooser.php'+keep_stub(true),null,'dialogWidth=600;dialogHeight=400;status=no;unadorned=yes');
		if ((typeof result=="undefined") || (result===null)) return;
	} else
	{
		var va=window.prompt("Enter the zone codename that the page is in",'');
		if (va==null) return;
		var vb=window.prompt("Enter the page codename",'');
		if (vb==null) return;
		
		result=va+':'+vb;
	}

	var vc=window.prompt("Enter the caption",'');

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

function doInput_email(field_name)
{
	if (typeof window.insertTextbox=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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 (typeof window.insertTextboxWrapping=='undefined') return;

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

function doInput_i(field_name)
{
	if (typeof window.insertTextboxWrapping=='undefined') return;

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

function doInput_font(field_name)
{
	if (typeof window.insertTextboxWrapping=='undefined') return;
	if (typeof window.is_comcode_xml=='undefined') 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,biggest_length_data='',cookie_value,result;
	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.replace(/[\?&]redirect=.*/,'')+':'+name;
			cookie_name=cookie_name.replace(/[\.=,; \t\r\n\013\014]/g,'');
			cookie_value=ReadCookie(encodeURIComponent(cookie_name));

			if ((cookie_value!='') && (cookie_value!='0'))
			{
				result=load_XML_doc('http://www.wakustomz.net/data/autosave.php?type=retrieve'+keep_stub(),false,'key='+window.encodeURIComponent(cookie_name));
				if ((result) && (result.responseText) && (posting_form.elements[i].value.length<result.responseText.length))
				{
					fields_to_do[name]=result.responseText;
					fields_to_do_counter++;
					if (result.responseText.length>biggest_length_data.length)
					{
						biggest_length_data=result.responseText;
					}
				}
			}
			window.last_autosave=new Date();
			addEventListenerAbstract(posting_form.elements[i],'keypress',handle_form_saving);
			posting_form.elements[i].externalonKeyPress=handle_form_saving;
		}
	}
	if ((fields_to_do_counter!=0) && (biggest_length_data!=''))
	{
		var key;
		if (biggest_length_data.length>100) biggest_length_data=biggest_length_data.substr(0,100)+'...';
		if (window.confirm('You appear to have unsaved form data. Would you like to auto-fill this form with your prior data?\n\n'+biggest_length_data.replace(/<[^>]*>/g,'')))
		{
			for (key in fields_to_do)
			{
				if (typeof fields_to_do[key]!='string') continue;
				
				if ((posting_form.elements[key]) && (posting_form.elements[key].style))
				{
					if (((posting_form.elements[key].style.display=='none') || ((window.wysiwyg_on) && (wysiwyg_on()))) && (areaedit_editors[posting_form.elements[key].id]))
					{
						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)
			{
				if (typeof fields_to_do[key]!='string') continue;
				
				cookie_name='ocp_autosave_'+window.location.pathname+window.location.search.replace(/[\?&]redirect=.*/,'')+':'+key;
				cookie_name=cookie_name.replace(/[\.=,; \t\r\n\013\014]/g,'');
				SetCookie(encodeURIComponent(cookie_name),'0',1);
			}
		}
	}
}

function handle_form_saving(event,target)
{
	var this_date=new Date();
	if ((this_date.getTime()-window.last_autosave.getTime())<20000) return; // Only save every 20 seconds

	if (!event) event=window.event;
	if (!target)
	{
		target=(event.target)?event.target:event.srcElement;
	}

	var cookie_name='ocp_autosave_'+window.location.pathname+window.location.search.replace(/[\?&]redirect=.*/,'')+':'+target.name;
	cookie_name=cookie_name.replace(/[\.=,; \t\r\n\013\014]/g,'');
	var value;
	if (((target.style.display=='none') || ((window.wysiwyg_on) && (wysiwyg_on()))) && (areaedit_editors[target.id]))
	{
		value=areaedit_editors[target.id].getHTML()+String.fromCharCode(event.keyCode?event.keyCode:event.charCode);
	} else
	{
		value=target.value+String.fromCharCode(event.keyCode?event.keyCode:event.charCode);
	}
	SetCookie(encodeURIComponent(cookie_name),'1',1);
	require_javascript('javascript_ajax');
	load_XML_doc('http://www.wakustomz.net/data/autosave.php?type=store'+keep_stub(),function() { },'key='+window.encodeURIComponent(cookie_name)+'&value='+window.encodeURIComponent(value));

	window.last_autosave=this_date;
}

