function calcPercentage(source, finalPrice) {
    //source = (source * 100) / 100;
    finalPrice = (finalPrice * 100);

    var res = (( finalPrice/ source)-100);
    //res = Math.round(res * 100 ) / 100;
    if (!isFinite(Number(res))) res = 0;
    return res;
}


function set_tax(obj){
    var taxIndex = obj.selectedIndex;
    //var idTax = obj.options[taxIndex].id;
    //alert(idTax+taxIndex);
    $('#product_id_tax').val(taxIndex);
}


function set_force_brand_to_product(obj){
    var brandIndex = obj.selectedIndex;
    var idBrand = obj.options[brandIndex].value;
    $('#product_forced_id_brand').val(idBrand);
}


function ValidateNumber(elem){
    var fieldName = elem.name;
    if(elem.validate({
        rules: {
            fieldName: {
            required: true,
            number: true
            }
        }
    })){
        alert('válido');
    }else{alert('inválido');}
}


function orderByPrice(elem){
    var orderPriceIndex = elem.selectedIndex;
    var orderPriceValue = elem.options[orderPriceIndex].value;
    document.location=orderPriceValue;
}


function orderByName(elem){
    var orderNameIndex = elem.selectedIndex;
    var orderNameValue = elem.options[orderNameIndex].value;
    document.location=orderNameValue;
}


function selectBannerType(elem, targetDiv){
    if ($(elem).is(':checked')){$('.'+targetDiv).hide();
    }else{$('.'+targetDiv).show();}
}


function resizeWindow(){
	if ($(window).width() < 1189) {$('#right-column').hide();$('#content').css('padding-right', '0px');
	} else {$('#right-column').show();$('#content').css('padding-right', '180px');}
}


function test(){
	alert('test');
}


function setInactiveVisible(siteUrl){
	var targetUrl = '';
	if ($('#display_inactive').attr('checked')){
        targetUrl = siteUrl+"/product/setProductVisible/1";
    }else{
        targetUrl = siteUrl+"/product/setProductVisible/0";
    }
	$.ajax({type: "POST",url: targetUrl,cache:false,complete : function(msg){location.reload(true);}});
}


function isChecked(isitchecked){
	if (isitchecked == true){
		document.admin_form.boxchecked.value++;
	} else {
		if(document.admin_form.boxchecked.value > 0)document.admin_form.boxchecked.value--;
	}
}


/* checkbox style */
function toogleChb(elem, btn){
	$objElem = $(elem); $btn = $(btn);
	if($btn.hasClass('readonly')){return false;}
	if($objElem.val() == '0'){$objElem.val('1'); $btn.removeClass('unchecked'); $btn.addClass('checked');
	}else{$objElem.val('0'); $btn.removeClass('checked'); $btn.addClass('unchecked');}
}

/* ************ Comparator functions ************* */

/**
 * Function to remove an item from the comparator List
 */
function removeComparatorItem( itemId, siteUrl, productName, productImage){
	var $elem = $('#item-to-comparate-'+itemId);
	var targetUrl = siteUrl+"/comparator/remove/"+itemId;

	$.ajax({
	   type: "POST",
	   cache: false,
	   url: targetUrl,
	     
	   complete: function(msg){
		   var numItems = parseInt($('#num_comparator_items').html());
			numItems = numItems-1;
			$('#num_comparator_items').html(numItems);
			//$('.row_'+itemId).fadeOut(300, function() { $('.row_'+itemId).remove(); });
			//\''+siteUrl+'\'/product/show_details/'+itemId+'/'+productName+', 
			//'+productImage+);  
			//return false;
			
			if(numItems < 1){
				$elem.fadeOut(300, function() { $('#comparator').fadeOut(300, function() { $elem.remove();$('#comparator').hide(); }); });
				//$('#comparator').fadeOut(300, function() { $('#comparator').hide(); });
		     	$('#footer').css({'height':'100px', 'margin':'-100px auto 0', 'padding-bottom': '0px'});
			}else{
				$elem.fadeOut(300, function(){$elem.remove();});
			}
	   }
	   
	 });
}


function toogleCmpBtnProductView(itemId, productName, siteUrl, productImage, status){
	$comparar = $('.comparar-'+itemId);
	$comparar.empty();
	if(status == 0){
		$comparar.append('<button class="btn_comparator" onclick="removeComparatorItem('+itemId+', \''+siteUrl+'\', \''+productName+'\', \''+productImage+'\');toogleCmpBtnProductView('+itemId+', \''+productName+'\', \''+siteUrl+'\', \''+productImage+'\', 1); return false;" >Remover</button>');
	}else{
		$comparar.append('<button class="btn_comparator" onclick="addComparatorItem(' + itemId + ', \'' + productName + '\', \''+siteUrl+'/product/show_details/'+itemId+'/'+productName+'\',\''+productImage+'\'); toogleCmpBtnProductView('+itemId+', \''+productName+'\', \''+siteUrl+'\', \''+productImage+'\', 0); return false;"><img src="http://new.sonigate.com/images/icon-comparator.png">Comparar</button><br /><span class="btn_comparator_desc">Pode comparar até 28 produtos, de forma a analisar lado a lado as caracteristicas de cada um dos produtos que estiverem no comparador.</span>');
	}
}



function toogleCmpBtnListView(itemId, productName, siteUrl, productImage, status){
	$comparar = $('.comparar-'+itemId);
	$comparar.empty();
	if(status == 0){
		$comparar.append('<button class="lv-btn-cmp remove" onclick="removeComparatorItem('+itemId+', \''+siteUrl+'\', \''+productName+'\', \''+productImage+'\');toogleCmpBtnListView('+itemId+', \''+productName+'\', \''+siteUrl+'\', \''+productImage+'\', 1); return false;" ></button>');
	}else{
		$comparar.append('<button class="lv-btn-cmp add" onclick="addComparatorItem(' + itemId + ', \'' + productName + '\', \''+siteUrl+'/product/show_details/'+itemId+'/'+productName+'\',\''+productImage+'\'); toogleCmpBtnListView('+itemId+', \''+productName+'\', \''+siteUrl+'\', \''+productImage+'\', 0); return false;"></button>');
	}
}











