// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function sendToEditor(n, thestring) {
	//alert("Tring to insert, editor:"+n+'/'+thestring);
	
	var win = window.opener ? window.opener : window.dialogArguments;
	if (!win) win = top;
	tinyMCE = win.tinyMCE;
	/*
	o = document.getElementById(n);
	h = o.innerHTML.replace(new RegExp('^\\s*(.*?)\\s*$', ''), '$1'); // Trim
	h = h.replace(new RegExp(' (class|title|width|height|id|onclick|onmousedown)=([^\'"][^ ]*)( |/|>)', 'g'), ' $1="$2"$3'); // Enclose attribs in quotes
	h = h.replace(new RegExp(' (width|height)=".*?"', 'g'), ''); // Drop size constraints
	h = h.replace(new RegExp(' on(click|mousedown)="[^"]*"', 'g'), ''); // Drop menu events
	h = h.replace(new RegExp('<(/?)A', 'g'), '<$1a'); // Lowercase tagnames
	h = h.replace(new RegExp('<IMG', 'g'), '<img'); // Lowercase again
	h = h.replace(new RegExp('(<img .+?")>', 'g'), '$1 />'); // XHTML
	*/
	win.tinyMCE.execCommand('mceInsertContent', false, thestring);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function addUnloadEvent(func) {
	var oldonload = window.onbeforeunload;
	if (typeof window.onbeforeunload != 'function') {
		window.onbeforeunload = func;
	} else {
		window.onbeforeunload = function() {
			oldonload();
			func();
		}
	}
}

function warningOnLeavingUnsaved() {
	return !confirm('You are about to leave unsaved page. Do you wish to continue?')
}

var commentFormTitle;
var commentFormContent;
var commentFormButton;
var commentFormSubmitted;
var commentCodeIn;
var commentCodeOut;

function initCommentForm() {
	var theform = $('comment_form');
	commentFormTitle = $('field_title');
	commentFormContent = $('field_content');	
	commentFormButton = $('btnComment');
	commentFormSubmitted = false;
	commentCodeIn = $('pin_challenge');
	commentCodeOut = $('pin_reply');
	new PeriodicalExecuter(commentFormObserver, 1);
	Event.observe(theform, 'submit', function() {
		commentFormButton.disable();
		$('commentNotice').hide();
		$('commentLoader').show();
	})
}

function codeOK() {
	try {
		if (sha1(commentCodeOut.value)==commentCodeIn.value) {
			return true;
		} else {
			return false;
		}
	 } catch(err) {
		return false;
	}
}

function commentFormObserver() {
	//console.log("observing");
	var allok = false;
	if (!commentFormSubmitted) {
		if (commentFormTitle.value.length>1 && commentFormContent.value.length>1) {
			//activate form
			$('commentNotice').hide();
			allok = true;
		} else {
			//deactivate form
			allok = false;
			$('commentNotice').show();
		}
	}
	
	if (codeOK()) {
		$('code_check').hide();
		$('code_ok').appear();
		allok &= true;
	} else {
		$('code_check').show();
		$('code_ok').hide();
		allok = false;
	}
	
	if (allok) {
		commentFormButton.enable();
	} else {
		commentFormButton.disable();
	}
}







