/*
RATING JAVASCRIPT for USE WITH RATING STARS, AND FORM SUBMITTAL

1.  Requires two images: star-empty.gif and star-full.gif

2.  Embed this into the HTML of the page, each line is a star, defaulted to "unselected":
    ID = "rating_" + the star rating value (1-5) + "_" + the rating instance (default 0, repeat for multiple groups of stars)

	        <IMG ID='rating_1_0' SRC='images/star-empty.gif' onMouseover='sampleRating(this);' onMouseout='clearRating(this);' onClick='setRating(this);'>
	        <IMG ID='rating_2_0' SRC='images/star-empty.gif' onMouseover='sampleRating(this);' onMouseout='clearRating(this);' onClick='setRating(this);'> 
	        <IMG ID='rating_3_0' SRC='images/star-empty.gif' onMouseover='sampleRating(this);' onMouseout='clearRating(this);' onClick='setRating(this);'> 
	        <IMG ID='rating_4_0' SRC='images/star-empty.gif' onMouseover='sampleRating(this);' onMouseout='clearRating(this);' onClick='setRating(this);'> 
	        <IMG ID='rating_5_0' SRC='images/star-empty.gif' onMouseover='sampleRating(this);' onMouseout='clearRating(this);' onClick='setRating(this);'>

3.  Create a form element for each product being rated, with ID of "rating_" + group id being rated
    Can be hidden, text or select box form element
    Example (hidden): <INPUT TYPE=HIDDEN NAME='rating_0' VALUE='0' ID='rating_0'>
    Example (select):
	        <SELECT NAME='rating_2' VALUE='0' ID='rating_2' onChange='setRating(this);'>
	            <OPTION VALUE='0'>Select Rating</OPTION>
	            <OPTION VALUE='1'>1 of 5 Stars</OPTION>
	            <OPTION VALUE='2'>2 of 5 Stars</OPTION>
	            <OPTION VALUE='3'>3 of 5 Stars</OPTION>
	            <OPTION VALUE='4'>4 of 5 Stars</OPTION>
	            <OPTION VALUE='5'>5 of 5 Stars</OPTION>
	       </SELECT>

*/


function sampleRating(oImg){
    var vID = oImg.id;
    var abuffer = vID.split("_");
    var i = 0;
    var iSelIndex = (abuffer[1] * 1);
    var iGroup = abuffer[2];
    
    for(i=1;i<6;i++){
        if(i<=iSelIndex){
            document.getElementById("rating_" + i + "_" + iGroup).src = sImageURL + "/star-full.gif";
        }else{
            document.getElementById("rating_" + i + "_" + iGroup).src = sImageURL + "/star-empty.gif";
        }
    }
}

function clearRating(oImg){
    var vID = oImg.id;
    var abuffer = vID.split("_");
    var i = 0;
    var iGroup = abuffer[2];
    var oControl = document.getElementById("rating_" + iGroup);
    
    if(oControl.type == "select-one"){
        var iSelIndex = oControl.selectedIndex;
    }else{
        var iSelIndex = oControl.value * 1;
    }
    
    for(i=1;i<6;i++){
        if(i<=iSelIndex){
            document.getElementById("rating_" + i + "_" + iGroup).src = sImageURL + "/star-full.gif";
        }else{
            document.getElementById("rating_" + i + "_" + iGroup).src = sImageURL + "/star-empty.gif";
        }
    }
}

function setRating(oImg){
    var vID = oImg.id;
    var abuffer = vID.split("_");
    var i = 0;
    
    if(oImg.type != "select-one"){
        var iSelIndex = (abuffer[1] * 1);
        var iGroup = abuffer[2];
        var oControl = document.getElementById("rating_" + iGroup);
    }else{
        var iSelIndex = oImg.selectedIndex;
        var iGroup = abuffer[1];
    }
    
    for(i=1;i<6;i++){
        if(i<=iSelIndex){
            document.getElementById("rating_" + i + "_" + iGroup).src = sImageURL + "/star-full.gif";
        }else{
            document.getElementById("rating_" + i + "_" + iGroup).src = sImageURL + "/star-empty.gif";
        }
    }
    
    
    if(oImg.type != "select-one"){
        if(oControl.type == "select-one"){
            oControl.selectedIndex = iSelIndex;
        }else{
            oControl.value = iSelIndex;
        }
    }
}

function setupComments(oControl, bAdjustSize){
    if(oControl.value == "Your comments..."){
        oControl.value = "";
        if(bAdjustSize){
            oControl.style.width = "620px";
            oControl.style.height = "60px";
        }
        
    }
    checkCommentLength(oControl);
}

function checkCommentLength(oControl){
    //maxnote_i
    var vMaxLength = 500;
    var vFormVal = oControl.value;
    var vCharsLeft = 500 - vFormVal.length;
    var vID = oControl.id;
    
    document.getElementById("maxnote_" + vID).innerHTML = vCharsLeft
    
    if(vFormVal.length > 500){
        oControl.value = vFormVal.substring(0,499);
    }
}