
//loadRatings does the initial loading of ratings.
function loadRatings(otherRatings)
{
	var currRatings = otherRatings.split( "#" );
	
	for ( var i = 0; i < currRatings.length; i++ ) {
		var temp = currRatings[i];
		var rateStar = temp.substring(temp.length-1);
		
		for(j=1;j<=rateStar;j++) {
			MM_swapImage((i+1)+'star' + j,'/en_US/products/new_layout/images/bv/bv_ratingStar_sel.gif');
		}
		
		setRating((i+1), rateStar);
	}
}
function showRating(intStar, intRating)
{
	if(!(document.getElementById('Rating'+intStar).value > 0)){
		document.getElementById('Rating'+intStar).value = 0;
	}
	for(i=1;i<=5;i++){
		if(i<=intRating){
			MM_swapImage(intStar + 'star' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_sel.gif');
		} else {
			MM_swapImage(intStar + 'star' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
		}
	}
	var text = '';
	if (intRating == 1) { text = '(Poor)'; }
	else if (intRating == 2) { text = '(Fair)'; }
	else if (intRating == 3) { text = '(Average)'; }
	else if (intRating == 4) { text = '(Good)'; }
	else if (intRating == 5) { text = '(Excellent)'; }
	document.getElementById('ratingText' + intStar).innerHTML = text;
}
function returnRating(intStar){
	var currRating = document.getElementById('Rating'+intStar).value;
	if(currRating == 0)	{
		for(i=1;i<=5;i++){
			MM_swapImage(intStar +'star' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
		}
	} else {
		for(i=1;i<=5;i++) {
			if(i<=currRating){
				MM_swapImage(intStar+'star' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_sel.gif');
			} else {
				MM_swapImage(intStar+'star' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
			}
		}
	}
	var text = '';
	if (currRating == 1) { text = '(Poor)'; }
	else if (currRating == 2) { text = '(Fair)'; }
	else if (currRating == 3) { text = '(Average)'; }
	else if (currRating == 4) { text = '(Good)'; }
	else if (currRating == 5) { text = '(Excellent)'; }
	document.getElementById('ratingText' + intStar).innerHTML = text;
}
function setRating(intStar, intRating)
{
	document.getElementById('Rating'+intStar).value = intRating;
	var text = '';
	if (intRating == 1) { text = '(Poor)'; }
	else if (intRating == 2) { text = '(Fair)'; }
	else if (intRating == 3) { text = '(Average)'; }
	else if (intRating == 4) { text = '(Good)'; }
	else if (intRating == 5) { text = '(Excellent)'; }
	document.getElementById('ratingText' + intStar).innerHTML = text;
	var form = document.getElementById('reviewForm');
	var rate = '';
	var ratingLength = 0;
    while(document.getElementById('Criteria'+ (ratingLength+1)) != null){
    	ratingLength++;
    }
    for(var k = 1; k <= ratingLength ; k++) {
    	rate = rate + document.getElementById('Criteria'+k).value +':'+ document.getElementById('Rating'+k).value + '#';
    }
    rate = rate.substring(0,rate.length - 1);
    form.otherRatings.value = rate;
}
function validateForm(form)
{
	var errorMsg = '';
    var ratingLength = 0;
    while(document.getElementById('Criteria'+ (ratingLength+1)) != null){
    	ratingLength++;
    }
    
    for(var k = 1; k <= ratingLength ; k++) {
    	if (!(document.getElementById('Rating'+k).value > 0)) {
    		errorMsg = 'Please complete the Category Rating Section. <br/>';
    	}
    }
    
    if (!(document.getElementById('overallRating').value > 0)) {
    	if(errorMsg  == '')
    		errorMsg += 'Please complete the Category Rating Section. <br/>';
	}
    
    if (!validRequired(form.title)) {
  	  		errorMsg += 'Please complete the Review Title Section. <br/>';
    }
    
    if (!validRequired(form.description)) {
	  		errorMsg += 'Please complete the Review Section with at least 50 characters. <br/>';
	}else if (!validLength(form.description, 50)) {
	  		errorMsg += 'Please complete the Review Section with at least 50 characters. <br/>';
	}

    if (errorMsg == '') {
    	try{ document.getElementById('revFormErrorBox').style.display = 'none'; }catch(err){};
      	return true;
    } else {
    	try{ document.getElementById('revFormErrorBox').style.display = 'block';}catch(err){};
    	document.getElementById('revFormError').innerHTML = errorMsg;
      	return false;
    }
}
function MM_swapImage(id,newimg) {
	var x;
 	if ((x=MM_findObj(id))!=null) {
		x.src=newimg;
    }
}

function reviewTextCounter(formId) {
	
	var maxlimit = 10000;
	var minlimit = 50;
	var msg = '';
	var form = document.getElementById(formId);
	var fieldValue = form.description.value;
	var fieldLength = fieldValue.length;
	
	if (fieldLength < minlimit) {
		msg = 'Minimum required characters left: '+ (minlimit-fieldLength) +'.';
	} else if (fieldLength >= maxlimit) {
		msg = 'Minimum Reached.';
		form.description.value = fieldValue.substring(0, maxlimit);
	} else {
		msg = 'Minimum Reached.';
	}
	
	document.getElementById('textCounter').innerHTML = msg;
}

function feedbackCounter(formId) {
	
	var limit = 10000;
	var form = document.getElementById(formId);
	var fieldValue = form.morePampersFeedback.value;
	var fieldLength = fieldValue.length;
	
	if (fieldLength >= limit) {
		form.morePampersFeedback.value = fieldValue.substring(0, limit);
	}	
}

function trim(s) {
    while (s.substring(0,1) == ' ') {
      s = s.substring(1,s.length);
    }
    while (s.substring(s.length-1,s.length) == ' ') {
      s = s.substring(0,s.length-1);
    }	
    return s;
}

function validRequired(formField)
{
	var result = true;
	if (trim(formField.value) == "")	{
		result = false;
	}
	return result;
}

function validLength(formField , reqLength)
{
	var result = true;
	if (trim(formField.value).length < reqLength)	{
		result = false;
	}
	return result;
}

function submitPreviewForm(formId)
{
	var form = document.getElementById(formId);
	form.mode.value = 'preview';
	if(validateForm(form)) {
		document.getElementById(formId).submit();
		return true;
	} 
}

function submitEditForm(formId)
{
	var form = document.getElementById(formId);
	form.mode.value = 'edit';
	document.getElementById(formId).submit();
}

function submitFinalReview(formId)
{
	var form = document.getElementById(formId);
	form.mode.value = 'submit';
	var tnc = document.getElementById('termsAndConditions').checked;
	if(tnc) {
		document.getElementById(formId).submit();
	} else {
		try{ document.getElementById('prevFormError').style.display = 'block';}catch(err){};
		document.getElementById('prevFormErrorText').innerHTML = 'You must agree to the Customer Ratings and Review Terms of Use. Please check the box above.';
	}
}


/* Overall  rating functions start here .. */

var currentOverallRating = 0;

function loadOverallRating(intRating)
{
	for(i=1;i<=5;i++) {
		if(i<=intRating){
			MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_sel.gif');
		} else {
			MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
		}
	}
	setOverallRating(intRating);
}

function showOverallRating(intRating)
{
	for(i=1;i<=intRating;i++) {
		MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_sel.gif');
	}
	for(i=intRating+1;i<=5;i++) {
		MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
	}
	
	var text = '';
	if (intRating == 1) { text = '(Poor)'; }
	else if (intRating == 2) { text = '(Fair)'; }
	else if (intRating == 3) { text = '(Average)'; }
	else if (intRating == 4) { text = '(Good)'; }
	else if (intRating == 5) { text = '(Excellent)'; }
	document.getElementById('overallRatingText').innerHTML = ' '+text;
}
function returnOverallRating(){
	if(currentOverallRating == 0)	{
		for(i=1;i<=5;i++){
			MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
		}
	} else {
		for(i=1;i<=5;i++) {
			if(i<=currentOverallRating){
				MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_sel.gif');
			} else {
				MM_swapImage('overallStar' + i,'/en_US/products/new_layout/images/bv/bv_ratingStar_normal.gif');
			}
		}
	}
	var text = '';
	if (currentOverallRating == 1) { text = '(Poor)'; }
	else if (currentOverallRating == 2) { text = '(Fair)'; }
	else if (currentOverallRating == 3) { text = '(Average)'; }
	else if (currentOverallRating == 4) { text = '(Good)'; }
	else if (currentOverallRating == 5) { text = '(Excellent)'; }
	document.getElementById('overallRatingText').innerHTML = ' '+text;
}
function setOverallRating(intRating)
{
	currentOverallRating = intRating;
	document.getElementById('overallRating').value = currentOverallRating;
	var text = '';
	if (intRating == 1) { text = '(Poor)'; }
	else if (intRating == 2) { text = '(Fair)'; }
	else if (intRating == 3) { text = '(Average)'; }
	else if (intRating == 4) { text = '(Good)'; }
	else if (intRating == 5) { text = '(Excellent)'; }
	document.getElementById('overallRatingText').innerHTML = ' '+text;
}

	function showHelp(currentdiv){
		if (document.getElementById(currentdiv) != null )
			document.getElementById(currentdiv).style.display = "block";
	}
	
	function hideHelp(currentdiv) {
		if (document.getElementById(currentdiv) != null )
			document.getElementById(currentdiv).style.display = "none";
	}
	
	
	function open_bv_win(url, width, height, top) {
		window.open(url,'PopUp','width='+width+',height='+height+', top='+top+',left=100,resizable=no,toolbar=no,scrollbars=yes, status=no, titlebar=no');
	}
	
	function open_bv_window(url, width, height, top) {
		window.open(url,'PopUp','width='+width+',height='+height+', top='+top+',left=100,resizable=no,toolbar=no,scrollbars=no, status=no, titlebar=no');
	}

	function submitBvFeedback(formId, fb)
	{
		var form = document.getElementById(formId);
		form.feedback.value = fb;
		document.getElementById(formId).submit();
	}
	
	function wordWrapNew(text, m, b, c){
	    var i, j, s, r = text.split("\n");
	    if(m > 0) for(i in r){
	        for(s = r[i], r[i] = ""; s.length > m;
	            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
	            || m,
	            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
	        );
	        r[i] += s;
	    }
	    return r.join("\n");
	};


