	var okay 			= true;
	var result_ok 		= 'Takk for din stemme ...';
	var result_error 	= 'Du har allerede stemt på dette klippet';

	function rate( url, img, clip_id, current_rating ) {
		
		cookie = readCookie("clip");
		rating = readCookie(cookie);

		if(cookie != null) {
	    	// leder efter ID i cookie
	        myString = new String(readCookie("clip"));
			if(myString.search(clip_id)>=0) {
				// hvis fundet
				okay = false;
			} else {
				// hvis ikke fundet
				okay = true;						
			}
		}
		
		if(okay && cookie != clip_id || cookie == null ) {
			makeRequest( url, img, clip_id );
		} else {
			
			for( i=1; i<=current_rating; i++ ) {
				document.getElementById(i).src="./images/stars6.jpg";
			}			
			document.getElementById("xstream_result").innerHTML = '<font class="xstream_result_error">' + result_error + '</font>';
		}
	}
	
    function makeRequest( url, img, clip_id ) {
        var httpRequest;

        if ( window.XMLHttpRequest ) { // Mozilla, Safari...
            httpRequest = new XMLHttpRequest();
            if ( httpRequest.overrideMimeType ) {
                httpRequest.overrideMimeType( 'text/xml' );
            }
        } else if ( window.ActiveXObject ) { // IE
            try {
                httpRequest = new ActiveXObject( "Msxml2.XMLHTTP" );
            } catch (e) {
            	try {
                	httpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
                } catch (e) {}
            }
        }

        if ( !httpRequest ) {
            alert('Error');
            return false;
        }

        httpRequest.onreadystatechange = function() { alertContents(httpRequest,clip_id); };
        httpRequest.open( 'POST', url, true );
        httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
        httpRequest.send( 'xstream_rating='+img.id+'&xstream_clip='+clip_id );
    }

    function alertContents( httpRequest,clip_id ) {
        if ( httpRequest.readyState == 4 ) {
            if ( httpRequest.status == 200 ) {
                //alert(httpRequest.responseText);
                document.getElementById("xstream_result").innerHTML = '<font class="xstream_result_ok">'+ result_ok +'</font>';
                
                okay = false;
                
                var image_id = httpRequest.responseText;
                
                for( i=1; i<=image_id; i++ ) {
                	document.getElementById(i).src="./images/stars6.jpg";
                }

                if(readCookie("clip")) {
                	// tilføjer ID til cookie
                	createCookie("clip", readCookie("clip") + ',' + clip_id, "1");
                	createCookie(clip_id, image_id, "1");
                } else {
                	createCookie("clip", clip_id, "1");
                	createCookie(clip_id, image_id, "1");
                }
                
            } else {
                alert('There was a problem with the request.');
            }
        }
    }
    
    function mouseOver( img ) {
    	if( okay ) {
			for( i=1; i<=img.id; i++ ) {
				document.getElementById(i).src="./images/stars7.jpg";
			}
    	}
	}
	
	function mouseOut( img ) {		
		if( okay ) {
			for( i=1; i<=5; i++ ) {
				document.getElementById(i).src="./images/stars4.jpg";
			}
		}
	}
	
	function createCookie( name, value, days ) {
		if ( days ) {
			var date = new Date();
			date.setTime( date.getTime()+(days*24*60*60*1000) );
			var expires = "; expires="+date.toGMTString();
		} else 
			var expires = "";
			
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie( name ) {
		var nameEQ = name + "=";
		var ca = document.cookie.split( ';' );
		for( i=0; i<ca.length; i++ ) {
			var c = ca[i];
			while ( c.charAt(0) == ' ' ) 
				c = c.substring( 1, c.length );
			if ( c.indexOf(nameEQ) == 0 ) 
				return c.substring( nameEQ.length, c.length );
		}
		return null;
	}
