/* 
Free Feedback Form for Google Analytics by Savio.no
http://www.savio.no/blogg/a/97/free-feedback-form-for-google-analytics

This Feedback Form is released under Creative Commons
You are free:
* to Share — to copy, distribute and transmit the work
* to Remix — to adapt the work
Under the following conditions:
* Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). 
* Share Alike —  If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. 
http://creativecommons.org/licenses/by-sa/3.0/

If you modify it or improve the Feedback Form, please let me know so I can update the Feedback Form article.
Savio.no can't be held liable for issues that arise from the download or use of the feedback form.
*/

/******************************************************
Error Handling if feedbackHeading Variable is missing 
If Variable is missing, Feedback Form will be disabled
******************************************************/
if (typeof(feedbackHeading) == 'undefined')
	{
	document.write('<link href="/feedback/feedback.css" rel="stylesheet" type="text/css" />');
	document.getElementById('fixeddiv').className="feedbackhidden"; 
	document.getElementById('feedbackHolder').className="feedbackhidden"; 
	document.getElementById('feedbackHolder').innerHTML = "";
}
else {
/******************************************************
Cookie handling
******************************************************/
// This function saves a cookie
function setCookie(name, value, expires) {
document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}
//  End -->

// This function retrieves a cookie
function getFeedbackCookie (name) {
	var cname = name + "=";               
	var dc = document.cookie;

    if (dc.length > 0) {              
		begin = dc.indexOf(cname);       
			if (begin != -1) {           
			begin += cname.length;       
			end = dc.indexOf(";", begin);
				if (end == -1) end = dc.length;
				return unescape(dc.substring(begin, end));
			} 
		}
	return '';
}
//  End -->

// This code sets cookie lifetime
var exp = new Date();                                   // make new date object
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * parseInt(feedbackTimeRestriction))); // set it x days ahead
//  End -->

// This function deletes a cookie
function delCookie(name) {
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}
//  End Cookie handling -->

/******************************************************
Show/Hide elements based on Click
******************************************************/
function showhidefeedback () { 
	overlayDiv = document.getElementById('fOverlay');  
		if (overlayDiv.className=="feedbackshown") { 
			overlayDiv.className="feedbackhidden"; 
			document.getElementById('hideDiv').className="feedbackhidden"; 
			document.getElementById('showDiv').className="feedbackshown";
			document.getElementById('feedbackHolder').className="feedbackhidden"; 
			setCookie('hideDivCookie','hideOverlay', exp);
				if ( feedbackErrorTracking == 'yes')
				{
				_gaq.push(['_trackEvent', 'Feedback','Feedback Closed']);
				}
		} 
		else { 
			overlayDiv.className="feedbackshown"; 
			document.getElementById('hideDiv').className="feedbackshown"; 
			document.getElementById('showDiv').className="feedbackhidden";
			document.getElementById('feedbackHolder').className="feedbackshown";
			delCookie('hideDivCookie');
				if ( feedbackErrorTracking == 'yes')
				{
				_gaq.push(['_trackEvent', 'Feedback','Feedback Opened']);
				}
		} 
}
//  End --> 

/******************************************************
Show/Hide remLenWrap onFocus
******************************************************/
function showhideremLenWrap () { 
	remLenWrapSpan = document.getElementById('remLenWrap'); 
	if (remLenWrapSpan.className=="feedbackhidden") { 
      remLenWrapSpan.className="feedbackshown"; 
   } 
}
//  End -->
// Limit the number of characters per textarea 
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}
//  End -->

/******************************************************
Comment Field Validation Text
******************************************************/
if (feedbackCommentValidation == "optional")
	{
	feedbackCommentValidationFeedback = "";
	}
else if (feedbackCommentValidation == "mandatory")
	{
	feedbackCommentValidationFeedback = " *";
}
// End -->
/******************************************************
Hide Feedback Form after Submitting feedback for Site Level Feedback
******************************************************/
function hideFeedback () { 
	document.getElementById('fOverlay').className="feedbackhidden"; 
	document.getElementById('hideDiv').className="feedbackhidden"; 
	document.getElementById('showDiv').className="feedbackshown";
	document.getElementById('feedbackHolder').className="feedbackhidden"; 
}
// End -->

/******************************************************
Send Feedback to Google Analytics + some Validation.
******************************************************/
function submit_feedback()
{
// validate myradiobuttons
myOption = -1;
for (rb=document.gafeedbackForm.rating.length-1; rb > -1; rb--) {
if (document.gafeedbackForm.rating[rb].checked) {
myOption = rb; rb = -1;
}
}
// If there were no selections made display an alert
if (myOption == -1) {
		document.getElementById('feedbackAlert').innerHTML = "<p style='color:red'>"+feedbackRadioButtonError+"</p>";
			if ( feedbackErrorTracking == 'yes')
			{		
			// Send result of Form Validation to Google Analytics
			_gaq.push(['_trackEvent', 'Feedback','Feedback Radio Button not Selected']);
			}
return false;
}

// Feedback Comment is Mandatory
if (feedbackCommentValidation == "mandatory" && feedbackCommentVisibility == 'show' && document.getElementById('fBack').value == "")
	{
		document.getElementById('feedbackCommentHeading').innerHTML = "<span style='color:red'>"+feedbackCommentHeading+ " "+feedbackCommentValidationFeedback+"</span>";
			if( feedbackErrorTracking == 'yes')
			{
			_gaq.push(['_trackEvent', 'Feedback','Feedback Text not Filled Out']);
			}
		return false;
}
var rad_val = document.gafeedbackForm.rating[myOption].value;
if (feedbackCommentVisibility == 'show')
	{
	var myFeedbackTextField = document.getElementById('fBack').value;
	}
	else if (feedbackRadioType = 'vertical' && feedbackCommentVisibility == 'hide')
		{
		var myFeedbackTextField = document.getElementById('ratingText'+[rad_val]).innerHTML;
	}
// Send Feedback Data to Google Analytics and Say Thank You
	document.getElementById('fContentWrapper').innerHTML = "<p>"+feedbackCommentThx+"</p>";
	_gaq.push(['_trackEvent','Feedback',feedbackHeading,myFeedbackTextField,parseInt(rad_val)]);
	if (feedbackGoalURL == 'yes') {
	_gaq.push(['_trackPageview','/feedback/savio/feedback-submitted']);
	}
// Set Cookie based on feedbackLevel "page" or "site"
	if (feedbackLevel == "page")
		{
		// Set Cookie for Page Level
			setCookie('feedbackCookie'+document.location.href,document.location.href, exp);
			}
		else if  (feedbackLevel == "site")  
			{
			// Set Cookie for Site Level and Close the Feedback form after 5 seconds
			setCookie('feedbackCookie'+feedbackHeading,feedbackHeading, exp);
			setCookie('hideDivCookie','hideOverlay', exp);
			// Hide Feedback Form after 5 seconds
			var closeAfterFeedback=setTimeout(hideFeedback,5000);
		}
}
//  End -->

/******************************************************
CSS (Cascading Style Sheet)
******************************************************/
document.write('\
<link href="/feedback/feedback.css" rel="stylesheet" type="text/css" />\
<style type="text/css">\
#fixeddiv h3, #fixeddiv h4, #fixeddiv input, #fixeddiv textarea, #fixeddiv #fOverlay {\
color: '+feedbackTextColor+';\
}\
#fixeddiv #remLenWrap {\
color: '+feedbackTextColor+';\
}\
#fixeddiv .remLen {\
background: '+feedbackBgColor+';\
}\
#fixeddiv #hideDiv{\
border: 2px solid '+feedbackBorder+';\
background: '+feedbackBgColor+';\
}\
#fixeddiv #showDiv{\
border: 2px solid '+feedbackBorder+';\
background: '+feedbackBgColor+';\
color: '+feedbackTextColor+';\
}\
#fixeddiv #fOverlay{\
border: 4px double '+feedbackBorder+';\
background: '+feedbackBgColor+';\
width: ' +feedbackWidth+'px;\
}\
#fixeddiv textarea {\
width: ' +(feedbackWidth-6)+'px;\
}\
#feedbackHolder {\
right: '+(feedbackWidth-34)+'px;\
}\
#fixeddiv #remLenWrap {\
right: '+(feedbackWidth-84)+'px;\
}\
</style>\
');
//  End CSS -->

/******************************************************
HTML codes
******************************************************/
document.write('\
<div id="hideDiv" class="feedbackshown" title="'+feedbackHideText+'" onclick="showhidefeedback();"></div>\
<div id="fOverlay" class="feedbackshown">\
<div id="fContent">\
<h3 id="feedbackHeading">'+feedbackHeading+'</h3>\
<div id="fContentWrapper">\
<form name="gafeedbackForm">\
<div id="gaRatingWrapper">\
'); 
// Split feedbackTypeText String
	var myFeedbackString = feedbackTypeText;
	var mySplitResult = myFeedbackString.split(",");
	for(fs = 0; fs < mySplitResult.length; fs++){
	}
// Radio Button Layout is Horizontal	
	if (feedbackRadioType == 'horizontal')
	{	
	document.write(mySplitResult[0]);
	for (vert=0;vert<parseInt(feedbackOptionsNumber);vert++)
		{
		document.write('<input name="rating" id="rating'+parseInt(vert+1)+'" type="radio" value="'+parseInt(vert+1)+'" />');
	}
		document.write(mySplitResult[1]);
	}
// Radio Button Layout is Vertical
	else if (feedbackRadioType = 'vertical')  
	{
	document.write('<p>');
	for (vert=0;vert<parseInt(feedbackOptionsNumber);vert++)
		document.write('<input name="rating" id="rating'+parseInt(vert+1)+'" type="radio" value="'+parseInt(vert+1)+'" /><label for="rating'+parseInt(vert+1)+'"><span id="ratingText'+parseInt(vert+1)+'">' + mySplitResult[parseInt(vert)] + '</span></label><br />');
	document.write('</p>');
	}
document.write('\
</div>\
<span id="feedbackAlert"></span>\
');
if (feedbackCommentVisibility == 'hide' && feedbackRadioType == 'horizontal')
{
	document.getElementById('fContentWrapper').innerHTML = "<p><strong>feedbackRadioType</strong> can't be set to <strong>vertical</strong> when <strong>feedbackCommentVisibility</strong> is set to <strong>hide</strong><br/><br/><br/></p>";
	}
else if (feedbackCommentVisibility == 'show')
{
document.write('\
<h4 id="feedbackCommentHeading">'+feedbackCommentHeading+'<span id="commentAlert">'+feedbackCommentValidationFeedback+'</span>\</h4>\
<textarea name="fBack" id="fBack" cols="20" rows="2" wrap="virtual" onkeydown="textCounter(document.gafeedbackForm.fBack,document.gafeedbackForm.remLen1,feedbackCharRestrictions)" onkeyup="textCounter(document.gafeedbackForm.fBack,document.gafeedbackForm.remLen1,feedbackCharRestrictions)"  onfocus="showhideremLenWrap();"></textarea>\
<span id="remLenWrap" class="feedbackhidden">\
<input class="remLen" readonly type="text" name="remLen1" size="2" maxlength="3" value="'+feedbackCharRestrictions+'" />\
 characters left\
</span>\
');
}
document.write('\
<input name="fButton" type="button" class="fButton" value="'+feedbackButtonText+'" title="'+feedbackButtonTitle+'" onclick="submit_feedback()" />\
</form>\
</div>\
</div>\
</div>\
<div id="showDiv" class="feedbackhidden" title="'+feedbackShowText+'" onclick="showhidefeedback();">+</div>\
');
// End HTML -->

/******************************************************
Load Overlay Div
******************************************************/
var divFeedback = document.getElementById("fOverlay");
	if(divFeedback.style.display == "block"){
		divFeedback.style.display="none";
	}else{
		divFeedback.style.display="block";
}
//  End Load Overlay function -->

/******************************************************
Show/Hide elements based on Cookie
******************************************************/
overlayDivCookie = document.getElementById('fOverlay');
	if (getFeedbackCookie('hideDivCookie') == "hideOverlay") {
		overlayDivCookie.className="feedbackhidden"; 
		document.getElementById('hideDiv').className="feedbackhidden"; 
		document.getElementById('showDiv').className="feedbackshown";
		document.getElementById('feedbackHolder').className="feedbackhidden"; 
	 }
	else { 
		overlayDivCookie.className="feedbackshown"; 
		document.getElementById('hideDiv').className="feedbackshown"; 
		document.getElementById('showDiv').className="feedbackhidden";
		document.getElementById('feedbackHolder').className="feedbackshown";
}
//  End Show/Hide element based on Cookie -->
/******************************************************
Check with Cookie if feedback has been given
******************************************************/
if (feedbackLevel == "page" && getFeedbackCookie('feedbackCookie'+document.location.href) == document.location.href)
	{
		document.getElementById('fContentWrapper').innerHTML = "<p>"+feedbackCommentGiven+"</p>";
	}
	else if  (feedbackLevel == "site" && getFeedbackCookie('feedbackCookie' +feedbackHeading) == feedbackHeading)  
	{
		document.getElementById('fContentWrapper').innerHTML = "<p>"+feedbackCommentGiven+"</p>";
}
//  End Check with Cookie if feedback has been given -->
}
// End Error Handling if feedbackHeading Variable is missing -->
