blnSearchAdvanced = false;

function searchAdvanced()
{
	var objDiv = document.getElementById("search-advanced");
	objDiv.innerHTML = ''
		+ '<input type="radio" name="words" value="all" id="words-all" checked="checked" />'
		+ '<label for="words-all" title="Return posts containing all of the keywords">All words</label>'
		+ '<input type="radio" name="words" value="any" id="words-any" />'
		+ '<label for="words-any" title="Return posts containing any of the keywords">Any word</label>'
		+ '<br />'
		+ '<br />'
		+ 'Posts between:'
		+ '<br />'
		+ '<input type="text" name="from" maxlength="10" size="10"'
		+ ' title="Enter a date in DD/MM/YYYY format, or leave this box empty for no restriction"/>'
		+ ' <span style="color:silver;">DD/MM/YYYY</span>'
		+ '<br />'
		+ 'and:'
		+ '<br />'
		+ '<input type="text" name="to" maxlength="10" size="10"'
		+ ' title="Enter a date in DD/MM/YYYY format, or leave this box empty for no restriction"/>'
		+ ' <span style="color:silver;">DD/MM/YYYY</span>'
		+ '<br />'
		+ '<a href="javascript:;" onclick="searchSimple();" title="Get simple">&#0171 Simple</a>';
	blnSearchAdvanced = true;
}

function searchSimple()
{
	var objDiv = document.getElementById("search-advanced");
	objDiv.innerHTML = ''
		+ '<a href="javascript:;" onclick="searchAdvanced();" title="Go advanced">'
		+ 'Advanced &#0187'
		+ '</a>';
	blnSearchAdvanced = false;
}

function validateSearch()
{
	var objSearch = document.forms.search.search;
	if (objSearch.value.length == 0) {
		alert('Please enter some keywords');
		objSearch.focus();
		return false;
	}
	if (blnSearchAdvanced) {
		var objFrom = document.forms.search.from;
		var objTo = document.forms.search.to;
		if (objFrom.value.length > 0 && !isValidDate(objFrom.value)) {
			alert('Please enter a valid minimum date');
			objFrom.focus();
			return false;
		}
		if (objTo.value.length > 0 && !isValidDate(objTo.value)) {
			alert('Please enter a valid maximum date');
			objTo.focus();
			return false;
		}
	}
	return true;
}

function isValidDate(strDate)
{
	return /^\d{1,2}\/\d{1,2}\/\d{2,4}$/.test(strDate);
}

function loadCalendar(intMonth, intYear)
{
    new Ajax.Request('/ajax.php',
        {
            parameters  : {
                mode    : 'calendar',
                month   : intMonth,
                year    : intYear
            },
            method : 'get',
            onSuccess : function(objRequest) {
                document.getElementById('divCalendar').innerHTML = objRequest.responseText || "";
            },
            onFailure : function() {
                alert('An error occurred while attempting to load the calendar.')
            }
        }
    );
}

function submitComment(intPostID)
{
    document.forms['comment'].submit.disabled = true;

    var strName = document.forms['comment'].name.value;
    var strEmail = document.forms['comment'].email.value;
    var strWebsite = document.forms['comment'].website.value;
    var strComment = document.forms['comment'].comment.value;

    new Ajax.Request('/ajax.php',
        {
            parameters  : {
                mode    : 'comment',
                post    : intPostID,
                name    : strName,
                email   : strEmail,
                website : strWebsite,
                comment : strComment
            },
            method : 'post',
            onSuccess : function(objRequest) {
                document.getElementById('divComment').innerHTML = objRequest.responseText || "";
            },
            onFailure : function() {
                alert('An error occurred while attempting to process your comment.')
            }
        }
    );
}

function subscribeToPost(intPostID)
{
    document.forms['subscribe-post'].subscribe.disabled = true;

    var strEmail = document.forms['subscribe-post'].email.value;

    new Ajax.Request('/ajax.php',
        {
            parameters  : {
                mode    : 'subscribe',
                sub     : 'post',
                post    : intPostID,
                email   : strEmail
            },
            method : 'get',
            onSuccess : function(objRequest) {
                document.getElementById('divSubscribePost').innerHTML = objRequest.responseText || "";
            },
            onFailure : function() {
                alert('An error occurred while attempting to process your subscription.')
            }
        }
    );
}


function subscribeToBlog()
{
    document.forms['subscribe-blog'].subscribe.disabled = true;

    var strEmail = document.forms['subscribe-blog'].email.value;

    new Ajax.Request('/ajax.php',
        {
            parameters  : {
                mode    : 'subscribe',
                sub     : 'blog',
                email   : strEmail
            },
            method : 'get',
            onSuccess : function(objRequest) {
                document.getElementById('divSubscribeBlog').innerHTML = objRequest.responseText || "";
            },
            onFailure : function() {
                alert('An error occurred while attempting to process your subscription.')
            }
        }
    );
}
