
/************************************************************************/
/* Try to give the first relevant field the focus.                      */
/************************************************************************/

function TrySetFocus()
{
  if ( ! document.forms                ) return;
  if ( ! document.forms[0]             ) return;
  if ( ! document.forms[0].elements    ) return;

  var elems = document.forms[0].elements;

  for ( var i = 0 ; i < elems.length ; i++ )
    {
//	if ( elems[i].type == 'text' || elems[i].type == 'password' ) {
	if ( elems[i].type != 'hidden' && elems[i].type != 'select-one' ) 
    {
//	    alert(elems[i].type);
//        alert(elems[i].name);
        elems[i].focus();
	    return;
	}
 }
//  if ( document.all && document.all.enter ) {
//      document.all.enter.focus();
//  }
}

function TrySubmitForm()
{
  if ( ! window.event ) return;
  if ( window.event.keyCode != 13 ) return;

  if ( ! document.forms ) return;
  if ( ! document.forms[0] ) return;

  if ( ! window.hasSave ) {
    // Do not submit if we already have our own handler.
    document.forms[0].submit();
  }
}

/************************************************************************/

function byebye()
{
    alert("Je kunt altijd terugkomen naar deze site om je assessment af te maken.");
}

/************************************************************************/
/* Prevent being framed.                                                */
/************************************************************************/

if ( self != top ) top.location.href = self.location.href;

/************************************************************************/
/* Define the roll over functions.                                      */
/************************************************************************/

function mouse_over(label, count, body1, body2) 
{
    if ( document.images ) {
	if ( count ) {
	    name = 'button_' + count;
	} else {
	    name = label;
	}
	document.images[name].src = over[label].src;
	if ( body1 ) {
	    document.images[body1].src = over[body1].src;
	}
	if ( body2 ) {
	    document.images[body2].src = over[body2].src;
	}
    }
}

function mouse_out(label, count, body1, body2) {
    if ( document.images ) {
	if ( count ) {
	    name = 'button_' + count;
	} else {
	    name = label;
	}
	document.images[name].src = out[label].src;
	if ( body1 ) {
	    document.images[body1].src = out[body1].src;
	}
	if ( body2 ) {
	    document.images[body2].src = out[body2].src;
	}
    }
}

function text_color(label, color)
{
    if ( document.anchors ) {
	for ( var i = 0 ; i < document.anchors.length ; i++ ) {
	    var anch = document.anchors[i];
	    if ( anch.name == label && anch.style ) {
		anch.style.color = color;
	    }
	}
    }
}

function popupPrint(id)
{
  msg = window.open('../home/popup_feedback.php',
		    'feedback',
		    'toolbar=yes,width=770,height=500,directories=no,resizable=yes,status=no,scrollbars=yes,menubar=yes,location=no');
  msg.focus();
}

function myPopup( url, width, height )
{
	var popup = window.open(	url,
												 		"",
												 		"toolbar=no,scrollbars=yes,width="+width+",height="+height+",directories=no,resizable=yes,status=no,menubar=no,location=no" );
	if ( popup.focus ) popup.focus();
}

function addListener(obj, event, func) {
	if (window.addEventListener)
		obj.addEventListener(event, func, false);
	else
		obj.attachEvent('on' + event, func);  
}

function utf8_encode(s) {
  return unescape(encodeURIComponent(s));
}

function utf8_decode(s) {
  return decodeURIComponent(escape(s));
}

var rating_description = [];
rating_description[10] = 'Slecht';
rating_description[20] = 'Nichts Besonderes';
rating_description[30] = 'Ziemlich gut';
rating_description[40] = 'Sehr hübsch';
rating_description[50] = 'Genial!';

var original_message = '';
var allow_rating = true;

function setRatingDesc(msg) {
	var div = document.getElementById('rating_counter');
	if ( !original_message )
	  	original_message = div.innerHTML;
	if ( msg )
		div.innerHTML = msg;
	else if ( original_message )
		div.innerHTML = original_message;
}

function setRatingMessage(msg) {
	var div = document.getElementById('rating_message');
	div.innerHTML = msg;
}

function showStars(rating) {
	if ( allow_rating ) {
		document.getElementById('hover_rating').className = 'rating stars rating-' + rating;
		setRatingDesc(rating_description[rating]);
	}
}

function clearStars() {
	document.getElementById('hover_rating').className = '';
	setRatingDesc();
}

function setRating(rating, counter) {
	var half = (rating % 10 != 0);
	var class_name = half ? 'half-stars' : 'stars';
	document.getElementById('current_rating').className = 'rating done ' + class_name + ' rating-' + rating;

	var div = document.getElementById('rating_counter');
	if ( counter == 1 )
		var text = 'Bewertung';
	else
		var text = 'Bewertungen';
	div.innerHTML = counter + ' ' + text;
	
	var hover_div = document.getElementById('hover_rating');
	var parent = hover_div.parentNode; 
	parent.removeChild(hover_div);
}

function setStars(category_id, rating) {
	if ( allow_rating ) {
		allow_rating = false;
		new Ajax.Request('rate.php?category_id=' + category_id + '&rating=' + rating,
		  {
		    method: 'get',
		    onSuccess: function(transport,json){
		    	// alert('rate.php OK');
					if ( transport.responseText ) {
						try {
							var json = transport.responseText.evalJSON();
						} catch(e) {
							alert(e.message);
						}
						if ( json ) {
							return_value = json;
							if ( return_value.success )
							  setRating(return_value.new_rating, return_value.counter);
							setRatingMessage(return_value.msg);
						} else {
							alert(transport.responseText);
						}

					} else {
						alert(transport.responseText);
					}
		    },
		    onFailure: function(){
				}
		  });
	}
}

function createCommentForm(name) {
	var html = '';
	var url = document.location.href;
	if ( url.indexOf('#') == -1 )
		url += '#comments';
	html += '	<form name="commentform" method="post" action="' + url + '" onsubmit="postComment();">';
	html += '		<input type="hidden" name="action" value="" />';
	html += '		<label>Name</label><input type="text" name="name" size="30" value="' + name + '" /><br />';
	html += '		<label>Review</label><textarea name="comment" cols="20" rows="5"></textarea><br />';
	html += '		<input type="submit" value="Senden" /><br />';
	html += '	</form>';
	document.write(html);
}

function postComment() {
	document.commentform.elements['action'].value = 'post_comment';
	document.commentform.submit();
}
