/* 
 * Product Sorting callbacks
 */
var price_regex=/\.|\,/g;
function cleanPrice(value){
   return value.replace(price_regex,'');
}

function sort_price_asc(ia,ib){

    try{
        a = products[ia];
        b = products[ib];
    }catch(e){ return -1; }
    return (cleanPrice(a['FullPrice']) - cleanPrice(b['FullPrice']));
}

function sort_price_desc(ia,ib){
    var a,b;
    try{
        a = products[ia];
        b = products[ib];
    }catch(e){ return -1; }
    return (cleanPrice(b['FullPrice']) - cleanPrice(a['FullPrice']));
}

function sort_name_asc(ia,ib){
    var a,b;
    try{
        a = products[ia];
        b = products[ib];
    }catch(e){ return -1; }

    str_a = sort_prepare_string(a['Name']);
    str_b = sort_prepare_string(b['Name']);

    return strcmp(str_a,str_b);

}

function sort_name_desc(ia,ib){
    var a,b;
    try{
        a = products[ia];
        b = products[ib];
    }catch(e){ return -1; }

    str_a = sort_prepare_string(a['Name']);
    str_b = sort_prepare_string(b['Name']);

    return strcmp(str_b,str_a);
}

function sort_popularity_desc(ia,ib){
    var a,b;
    try{
        a = products[ia];
        b = products[ib];
    }catch(e){ return -1; }
    return ( parseInt(b['Popularity']) - parseInt(a['Popularity']) );
}

function strcmp(a,b){
    return (a==b)?0:(a<b)?-1:1;
}

function sort_prepare_string(str){
    return str.toLowerCase().replace(/\s+|\+/g,'');
}
