/*!
 * @author Gproxy Design Inc.
 * @copyright (c) 2009, Gproxy Design Inc. All rights reserved.
 * @version 2.5
 *
 * Gproxy doesn't allow to copy or change this code without Gproxy authorization.
 * See http://www.gproxy.com/licenses/license01.pdf for the full license governing this code.
 */

function crvShowItemReviews(strItemId, strReviewsCnt){    
    $j("#crv_cnt_loading").show();
    $j("#" + strReviewsCnt).hide();
    var src = '/app/site/hosting/scriptlet.nl?script=customscript_gpr_aae_ss_crv_showitemrvws&deploy=customdeploy_gpr_aae_ss_crv_showitemrvws&itemid=' + strItemId + '&sitenumber=' + gprSiteNumber + '&callback=?';
    
    $j.ajax({
        url: src,
        type: "GET",
        dataType: "jsonp",
        success: function(data){
            if (data.Errors.length > 0) {
                $j.each(data.Errors, function(i, val){
					if(val.code == -2){
						$j("#" + strReviewsCnt).html('Customer Reviews are Disabled');	
					}else{
						crvErrorManager('Show Reviews', val.code, val.details);	
					}                    
                });
            }
            else {
                $j.each(data.Results, function(i, val){										
                    $j("#" + strReviewsCnt).html(unescape(val.html));
                });
				$j("#" + strReviewsCnt).show();
            }
        },
        complete: function(XMLHttpRequest, textStatus){
            $j("#crv_cnt_loading").hide();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            crvErrorManager('Show Reviews', textStatus, errorThrown);
        }
    });
    
}

function crvShowForm(strObjId){	
    if ($j('#' + strObjId).is(':hidden')) {
        $j('#' + strObjId).slideDown(500);
    }
    else {
        $j('#' + strObjId).slideUp(500);
    }
}

function crvGetFormURL(strFormUrl, strItemId, strCustomerId, strIFrameId, strLinkId){	
    var src = '/app/site/hosting/scriptlet.nl?script=customscript_gpr_aae_ss_crv_checklicense&deploy=customdeploy_gpr_aae_ss_crv_checklicense&sitenumber=' + gprSiteNumber + '&callback=?';    
    $j.ajax({
        url: src,
        type: "GET",
        dataType: "jsonp",
        success: function(data){
            if (data.Errors.length > 0) {
                $j.each(data.Errors, function(i, val){					
					crvErrorManager('Show Form', val.code, val.details);					                    
                });
            }
            else {
                $j.each(data.Check, function(i, val){
					if (val.value == 'true'){
						$j('#' + strIFrameId).attr('src', strFormUrl + '&custrecord_gpr_aae_crv_itemid=' + strItemId + '&custrecord_gpr_aae_crv_customerid=' + strCustomerId + '&custrecord_gpr_aae_crv_sitenumber=' + gprSiteNumber + '&custrecord_gpr_aae_crv_state=3');
    					$j('#' + strLinkId).attr('href', strFormUrl + '&custrecord_gpr_aae_crv_itemid=' + strItemId + '&custrecord_gpr_aae_crv_customerid=' + strCustomerId + '&custrecord_gpr_aae_crv_sitenumber=' + gprSiteNumber + '&custrecord_gpr_aae_crv_state=3');
					}else{
						$j("#crv_savereviews").html('Customer Reviews are Disabled');
					}                    
                });
            }
        },        
        error: function(XMLHttpRequest, textStatus, errorThrown){
            crvErrorManager('Show Form', textStatus, errorThrown);
        }
    });    
}

function crvErrorManager(strSource, strCode, strDetails){
    $j("#crv_cnt_msgerror").append("<li>Customer Reviews, " + strSource + ", code: " + unescape(strCode) + ", details: " + unescape(strDetails) + "</li>");
}

function crvCreateRatingLinks(strContainerId, strRatingInputId){
    for (var i = 1; i <= 5; i++) {
        var objARating = $j("<a>");
        objARating.attr({
            title: "Rating " + i,
            href: "javascript:crvClickRating(" + i + ",'" + strRatingInputId + "');"
        });
        objARating.attr("class", "crv_stars");
        objARating.bind('click', function(e){
            crvSetRating(e, strContainerId, strRatingInputId);
        });
        objARating.bind('mouseover', function(e){
            crvHoverRating(e, strContainerId);
        });
        objARating.appendTo("#" + strContainerId);
    }
    $j("#" + strContainerId).bind('mouseout', function(e){
        crvSelectedRating(e, strContainerId, strRatingInputId);
    });
}

function crvClickRating(intVal, strRatingInputId){
    $j("#" + strRatingInputId).val(intVal);
}

function crvHoverRating(obj, strContainerId){
    var ok = true;
    $j("#" + strContainerId + ">a").each(function(i, val){
        if (ok) {
            $j(val).attr('class', 'crv_stars_hover');
            if (val == obj.target) {
                ok = false;
            }
        }
        else {
            $j(val).attr('class', 'crv_stars');
        }
    });
}

function crvSetRating(obj, strContainerId, strRatingInputId){
    var ok = true;
    $j("#" + strContainerId + ">a").each(function(i, val){
        if (ok) {
            $j(val).attr('class', 'crv_stars_selected');
            if (val == obj.target) {
                $j("#" + strRatingInputId).val(i + 1);
                ok = false;
            }
        }
        else {
            $j(val).attr('class', 'crv_stars');
        }
    });
}

function crvSelectedRating(obj, strContainerId, strRatingInputId){
    var ok = true;
    $j("#" + strContainerId + ">a").each(function(i, val){
        if ($j("#" + strRatingInputId).val() == i) {
            ok = false;
        }
        if (ok) {
            $j(val).attr('class', 'crv_stars_selected');
        }
        else {
            $j(val).attr('class', 'crv_stars');
        }
    });
}

function crvSetAnonymous(obj){
    var bolAnonymus = obj.checked;
    var objNameInput = $j("#custrecord_gpr_aae_crv_reviewer");
    var objContainer = $j("#crv_input_name");
    if (bolAnonymus) {       
		objContainer.html(objNameInput.val());
        objNameInput.val("Your Identity is Anonymous");
		objNameInput.attr('class', 'crv_input_disable');
		objNameInput.attr('disabled', true);
    }
    else {		
		objNameInput.val(objContainer.html());
		objNameInput.removeAttr('disabled');
        objNameInput.attr('class', 'inputreq');
        objContainer.html("");        
    }
}
