// Include querystring.js
document.write('<script type="text/javascript" src="'
    + "/assets/site/js/querystring.js" + '"></scr' + 'ipt>');

/**
 * Functions for user comments on pages
 */
 
/**
* Gets page alias from querystring. Returns the pagealias of the page to be commented.
* If returns empty string no comments are allowed
*/
function getPageAlias() {
	var qs = new Querystring();
	var q = qs.get("q", "");
	
	switch (q) {
		// Use same comment for follwing pages
		case "hdfilmer-arkosund":
			q = "filmer-arkosund";
			break;
			
		// dont use comments for following pages
		case "bli-medlem":
		case "bli-medlem-anmalan-skickad":
		case "adresser":
		case "lagg-in-annons":
		case "lagg-in-annons-steg2":
		case "annonsen-ar-inlagd":
		case "fel-annonsen-kunde-inte-bekraftas":
		case "annonsen-ar-bekraftad":
		case "ta-bort-eller-andra-annons":
		case "annonsen-ar-borttagen":
		case "andra-annons-steg2":
		case "annonsen-ar-andrad":
		case "membership":
		case "your-application-has-been-sent":
		case "the-swedish-if-boat-association":
		case "about-the-if-boat":
		case "class-rules":
		case "addresses":
		case "adressandra":
		case "adressandra-steg2-ejhittad":
		case "adressandra-steg2-hittad":
		case "adressandra-steg2-ejssif":
		case "adressandra-klart":
		case "artiklarao":
		case "innehall":
		return;
	}
	return q;
} 


/**
* Init comments from xml when loading page
*/
function initComments() { 
	var q = getPageAlias();
	if(q=="")
		return;			
	YAHOO.util.Connect.asyncRequest("GET","http://www.ifboat.com/assets/site/php/comments-xml.php?pageurl=" + q , { success:responseSuccess_initComments, failure:responseFailure_initComments  });
} 
YAHOO.util.Event.onDOMReady(initComments);
var responseSuccess_initComments = function(o) {
	// Process ajax response
	var doc = o.responseXML;
	
	var resultNode = doc.getElementsByTagName('action')[0];     
	var success = resultNode.getAttribute("success");
	if(resultNode.lastChild != undefined )
		errormessage = resultNode.lastChild.nodeValue;
	
	if(success == "false") {
		// Could not get comments for page
		return;
	}

	// Get event entries
	var nodes =  doc.getElementsByTagName('comment');
	var lihtml = "";
	for( i=0; i < nodes.length; i++ ) {
		lihtml += '<li>';
		lihtml += '<span class="from">' + nodes[i].getAttribute("user") + '</span>';
		lihtml += '<span class="when">' + nodes[i].getAttribute("created") + '</span>';
		lihtml += '<span class="what">' + nodes[i].lastChild.nodeValue  + '</span>';
		if(nodes[i].getAttribute("guid") != null) {
			lihtml += '<a href="javascript:void(0);" onclick="return deleteComment(\''+nodes[i].getAttribute("guid")+'\',\''+nodes[i].getAttribute("email")+'\');">Raderera kommentaren</a>'; 
		}
		lihtml += '</li>';
	}
	
	// Get comments UL list
	var objUL = document.getElementById("comments");
	if(lihtml != "") {
		// and insert li list
		objUL.innerHTML = lihtml;
		objUL.style.display="block";
	} else {
		// or hide the ul list
		objUL.style.display="none";
	}

	// Display comments div
	var objComments = document.getElementById("commentpage");
	objComments.style.display="block";
}
var responseFailure_initComments = function(o) {
	// Hide comments div
	var objComments = document.getElementById("commentpage");
	objComments.style.display="none";
}
/**
 * User clicked comment page button
 */
function showCommentForm() {
	var obj = document.getElementById("comment");
	obj.focus();
	return false;
}

/**
 * User clicked send comment button
 */
function postComment() {
	// Set current page alias or id
	var q = getPageAlias();
	var obj = document.getElementById("comment-pageurl");
	obj.value = q;
	var objForm = document.getElementById("sendcomment");
	YAHOO.util.Connect.setForm(objForm.id);
	YAHOO.util.Connect.asyncRequest(objForm.method,objForm.action, { success:responseSuccess_postComment, failure:responseFailure_postComment  });
	return false;
}
var responseSuccess_postComment = function(o) {
	var doc = o.responseXML;
	var resultNode = doc.getElementsByTagName('action')[0];     
	var success = resultNode.getAttribute("success");
	var redirectUrl = resultNode.getAttribute("redirectUrl");
	var errormessage = "";
	if(resultNode.lastChild != undefined )
		errormessage = resultNode.lastChild.nodeValue;
	var errorfield = resultNode.getAttribute("errorfield");
	
	if(success == "false") {
		displayCommentError( errormessage, errorfield );
	} else {
		// Comment saved, reload comments
		initComments();
		// and clear form
		var obj = document.getElementById("comment");
		obj.value = "";
		obj = document.getElementById("comment-email");
		obj.value = "";
		obj = document.getElementById("comment-name");
		obj.value = "";
		displayCommentError("");
	}
		

}
var responseFailure_postComment = function(o) {
	displayCommentError("Dina kommentarer kunde inte sparas.\nF&ouml;rs&ouml;k igen senare eller kontakta <a href='mailto:webmaster@ifboat.com'>Webmaster</a>.");
}
function displayCommentError(errmsg, errorfield) {
	var div = document.getElementById("comment-error");
	div.innerHTML=errmsg;
	if(errorfield != undefined) {
		var field = document.getElementsByName(errorfield)[0];
		// Set focus to specified element and select if it's a textfield
		field.focus();
		if(field.type=="text")
			field.select();
	}

}

/**
 * User clicked delete comment link
 */
function deleteComment(guid, email) {
	YAHOO.util.Connect.asyncRequest("GET","http://www.ifboat.com/assets/site/php/comments-delete.php?guid=" + guid +"&email=" + email, { success:responseSuccess_deleteComment, failure:responseFailure_deleteComment  });
	return false;
}
var responseSuccess_deleteComment = function(o) {
	var doc = o.responseXML;
	var resultNode = doc.getElementsByTagName('action')[0];     
	var success = resultNode.getAttribute("success");
	var redirectUrl = resultNode.getAttribute("redirectUrl");
	var errormessage = "";
	if(resultNode.lastChild != undefined )
		errormessage = resultNode.lastChild.nodeValue;
	var errorfield = resultNode.getAttribute("errorfield");
	
	if(success == "false") {
		displayCommentError( errormessage, errorfield );
	} else {
		// Comment saved, reload comments
		initComments();
		displayCommentError("");
	}
}
var responseFailure_deleteComment = function(o) {
	displayCommentError("Kommentaren kunde inte tas bort.\nF&ouml;rs&ouml;k igen senare eller kontakta <a href='mailto:webmaster@ifboat.com'>Webmaster</a>.");
}

