function changeratetext() {
        if (document.getElementById('ratebutton').value == 'Rate This') {
                document.getElementById('ratebutton').value = 'View All Ratings';
        } else {
                document.getElementById('ratebutton').value = 'Rate This';
        }

}

function getdesc(colorbox, box, rating) {
        if (box == 'Usabilitydesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Difficult to use and backward';
        } else if (box == 'Usabilitydesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Hard to use and requires unnecessary effort';
        } else if (box == 'Usabilitydesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Fairly easy to use but requires effort';
        } else if (box == 'Usabilitydesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Easy to use';
        } else if (box == 'Usabilitydesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Easy to use and naturally intuitive';
        } else

        if (box == 'Popularitydesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Small and diminishing user base';
        } else if (box == 'Popularitydesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Small to medium user base. Uncertain longevity';
        } else if (box == 'Popularitydesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Established user base and projected growth';
        } else if (box == 'Popularitydesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Large user base and sustainable growth';
        } else if (box == 'Popularitydesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Dominate user base and highly sustainable';
        } else

        if (box == 'Valuedesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Ridiculous price for value received';
        } else if (box == 'Valuedesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='High price for value received';
        } else if (box == 'Valuedesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Decent price for value received';
        } else if (box == 'Valuedesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Fair price for value received';
        } else if (box == 'Valuedesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Excellent price for value received';
        } else

        if (box == 'Supportdesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='No support offered';
        } else if (box == 'Supportdesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Very limited support offered';
        } else if (box == 'Supportdesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Some support available';
        } else if (box == 'Supportdesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Reliable support with options available';
        } else if (box == 'Supportdesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Excellent quick support available with multiple options';
        } else

        if (box == 'Securitydesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Insecure. No security. Data at risk';
        } else if (box == 'Securitydesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='No security measures taken. No information available';
        } else if (box == 'Securitydesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Moderate security in place. Basic security measures taken';
        } else if (box == 'Securitydesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Reliable security in place for information at risk';
        } else if (box == 'Securitydesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Proactively highly secure. Information safe. Hacker proof';
        } else

        if (box == 'Integrationdesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Does not want to integrate with others';
        } else if (box == 'Integrationdesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='No available integration but potential for future integration';
        } else if (box == 'Integrationdesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Integrate with a few other sites. No open APIs';
        } else if (box == 'Integrationdesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Integrate with other sites. Limited open APIs available';
        } else if (box == 'Integrationdesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Robust open APIs highly utilized and proactive integration';
        } else

        if (box == 'Accessdesc' && rating == '1') {
                document.getElementById(colorbox).style.width='19px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Very limited access via browsers and mobile devices';
        } else if (box == 'Accessdesc' && rating == '2') {
                document.getElementById(colorbox).style.width='35px';
                document.getElementById(colorbox).style.backgroundColor='#FF3F3F';
                document.getElementById(box).innerHTML='Limited access via browsers and mobile devices';
        } else if (box == 'Accessdesc' && rating == '3') {
                document.getElementById(colorbox).style.width='51px';
                document.getElementById(colorbox).style.backgroundColor='#FFCC66';
                document.getElementById(box).innerHTML='Decent access via browsers and mobile devices';
        } else if (box == 'Accessdesc' && rating == '4') {
                document.getElementById(colorbox).style.width='67px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Works on multiple browsers and mobile devices';
        } else if (box == 'Accessdesc' && rating == '5') {
                document.getElementById(colorbox).style.width='84px';
                document.getElementById(colorbox).style.backgroundColor='#74C369';
                document.getElementById(box).innerHTML='Works on all browsers and capable mobile devices';
        } else {
                document.getElementById(box).innerHTML='';
        }
}

function votefor(site_id, type, rating, overall_rating) {
	if('Save' != $('#rateThis').val())
		return;
	switch(type) {
		case 'Usability':
			$('#usability_rating').val(rating);
			break;
		case 'Popularity':
			$('#popularity_rating').val(rating);
			break;
		case 'Value':
			$('#value_rating').val(rating);
			break;
		case 'Support':
			$('#support_rating').val(rating);
			break;
		case 'Security':
			$('#security_rating').val(rating);
			break;
		case 'Integration':
			$('#integration_rating').val(rating);
			break;
		case 'Access':
			$('#access_rating').val(rating);
			break;
	}
	$($($("#maindiv" + type).children().get(1)).children().get(1)).mouseout(function() {
		getdesc('rating' + type + site_id, type + 'desc', rating);
		document.getElementById('rating' + type + site_id).style.width=(19 + (16*(rating-1))) + 'px';
	});
	$($($("#maindiv" + type).children().get(1)).children().get(2)).mouseout(function() {
		getdesc('rating' + type + site_id, type + 'desc', rating);
		document.getElementById('rating' + type + site_id).style.width=(19 + (16*(rating-1))) + 'px';
	});
	$($($("#maindiv" + type).children().get(1)).children().get(3)).mouseout(function() {
		getdesc('rating' + type + site_id, type + 'desc', rating);
		document.getElementById('rating' + type + site_id).style.width=(19 + (16*(rating-1))) + 'px';
	});
	$($($("#maindiv" + type).children().get(1)).children().get(4)).mouseout(function() {
		getdesc('rating' + type + site_id, type + 'desc', rating);
		document.getElementById('rating' + type + site_id).style.width=(19 + (16*(rating-1))) + 'px';
	});
	$($($("#maindiv" + type).children().get(1)).children().get(5)).mouseout(function() {
		getdesc('rating' + type + site_id, type + 'desc', rating);
		document.getElementById('rating' + type + site_id).style.width=(19 + (16*(rating-1))) + 'px';
	});
	$('#myrating' + type + site_id).children().css('left', (rating * 16 - 12) + 'px');
	$('#rating' + type + site_id).width((19 + (16*(rating-1))) + 'px');

	modify_overall_rating(site_id);
}

function modify_overall_rating(site_id) {
	var total_rating = parseInt($('#usability_rating').val()) + parseInt($('#popularity_rating').val()) + parseInt($('#value_rating').val()) + parseInt($('#support_rating').val()) + parseInt($('#security_rating').val()) + parseInt($('#integration_rating').val()) + parseInt($('#access_rating').val());

	var avg_rating = total_rating/7;
}

function clearRating(site_id) {
    $.post(base_url + 'rating/clear_rating', {site_id:site_id}, 
		function(result) {
			$('#intermediateRating').html(result);
			$('#clearRatingMessage').show();
			$('#clearRating').hide();
		}
	);
}
active_user_id = -1;
function expand_user_rating(element, user_id, site_id) {
	if(active_user_id != -1) {
		$('#intermediate_user_rating_' + active_user_id).hide();
		$('#collapse_image_' + active_user_id).hide();
		$('#expand_image_' + active_user_id).show();
	}
	$('#expand_image_' + user_id).hide();
	$('#collapse_image_' + user_id).show();
	if(active_user_id == user_id)
		active_user_id = -1;
	else
		active_user_id = user_id;
    $.post(base_url + 'rating/get_user_rating', {site_id:site_id,user_id:user_id}, 
		function(result) {
			$('#intermediate_user_rating_' + user_id).html(result);
			$('#intermediate_user_rating_' + user_id).attr('style','display:block');
		}
	);
}
function collapse_user_rating(element, user_id, site_id) {
	if(active_user_id != -1) {
		$('#intermediate_user_rating_' + active_user_id).hide();
		$('#collapse_image_' + active_user_id).show();
		$('#expand_image_' + active_user_id).hide();
	}
	$('#collapse_image_' + user_id).hide();
	$('#expand_image_' + user_id).show();
	if(active_user_id == user_id)
		active_user_id = -1;
	else
		active_user_id = user_id;
}

function readyToSave(site_id) {
	if('Save' == $('#rateThis').val()) {
        // generate form and update t
        var form = document.createElement("form");
        var form = document.createElement("form");
        form.setAttribute("method", "post");
        form.setAttribute("action", base_url + "rating/add_categorised_rating");

        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "site_id");
        hiddenField.setAttribute("value", site_id);
        form.appendChild(hiddenField);

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "usability_rating");
        hiddenField.setAttribute("value", $('#usability_rating').val());
        form.appendChild(hiddenField);    

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "popularity_rating");
        hiddenField.setAttribute("value", $('#popularity_rating').val());
        form.appendChild(hiddenField);    

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "value_rating");
        hiddenField.setAttribute("value", $('#value_rating').val());
        form.appendChild(hiddenField);    

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "support_rating");
        hiddenField.setAttribute("value", $('#support_rating').val());
        form.appendChild(hiddenField);    

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "security_rating");
        hiddenField.setAttribute("value", $('#security_rating').val());
        form.appendChild(hiddenField);    

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "integration_rating");
        hiddenField.setAttribute("value", $('#integration_rating').val());
        form.appendChild(hiddenField);    

        hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "access_rating");
        hiddenField.setAttribute("value", $('#access_rating').val());
        form.appendChild(hiddenField);    

        document.body.appendChild(form);    // Not entirely sure if this is necessary
        form.submit();
    }
	else {
        $('#rateThis').val('Save');
        $('#myratingUsability' + site_id).show();
        $('#myratingPopularity' + site_id).show();
        $('#myratingValue' + site_id).show();
        $('#myratingSupport' + site_id).show();
        $('#myratingSecurity' + site_id).show();
        $('#myratingIntegration' + site_id).show();
        $('#myratingAccess' + site_id).show();
        $('#usability_rating').val('');
        $('#popularity_rating').val('');
        $('#value_rating').val('');
        $('#support_rating').val('');
        $('#security_rating').val('');
        $('#integration_rating').val('');
        $('#access_rating').val('');
    }
}
