/**
 * Author: Liviu <liviu@iseom.com>
 */

function sortBrand(brand, cat) {
    window.location = site_url + 'pro-' + cat + '/m-' + brand + '.htm';
}

function sortRetailer(retailer, cat) {
    window.location = site_url + 'pro-' + cat + '/s-' + retailer + '.htm';
}

function setRetailerBrand(brand, url, retailer) {
    window.location = site_url + 'pro-' + url + '/s-' + retailer + '.htm/' + brand;
}
function setBradnRetailerCat(retailer, url, brand) {
    window.location = site_url + 'pro-' + url + '/m-' + brand + '.htm/' + retailer;
}

function setRetailerPrice(price, url, retailer) {
    window.location = site_url + 'pro-' + url + '/s-' + retailer + '.htm/' + price;
}

function setBrandCatPrice(price, url, brand) {
    window.location = site_url + 'pro-' + url + '/m-' + brand + '.htm/' + price;
}

function setBrandRetailer(retailer, url) {
    window.location = site_url + 'marque-' + url + '/' + retailer;
}

function setBrandPrice(price, url) {
    window.location = site_url + 'marque-' + url + '/' + price;
}

function sortCatPrice(price, url) {
    window.location = site_url + 'prix-' + url + '.htm/' + price;
}

function sortBrandSearch(brand, src) {
    if(brand != '') {
        window.location = site_url + 'achat-' + src + '.htm/m-' + brand;
    } else {
        window.location = site_url + 'achat-' + src + '.htm';
    }
}

function sortRetailerSearch(ret, src) {
    if(ret != '') {
        window.location = site_url + 'achat-' + src + '.htm/r-' + ret;
    } else {
        window.location = site_url + 'achat-' + src + '.htm';
    }
}

function sortPriceSearch(price, src) {
    window.location = site_url + 'achat-' + src + '.htm/' + price;
}

jQuery(document).ready(function($){
    var fields = ['name', 'title', 'comment'];
    
    $.each(fields, function(index, value) {
        $("#" + value).blur(function() {
            var the_value = $("#" + value).val();
            
            if(the_value == '') {
                $('#'+ value +'_label').addClass('error');
            } else {
                $('#'+ value +'_label').removeClass('error').addClass('valid');
            }
        });
    });
    
    $("#comment").blur(function() {
        var the_comm_value = $("#comment").val();
        
        if(the_comm_value.length < 25 || the_comm_value.length > 1000) {
            $('#comment_label').addClass('error');
            $('#msg_lenght').show();
        } else {
            $('#comment_label').removeClass('error').addClass('valid');
            $('#msg_lenght').hide();
        }
    });
    
    $('#goComm').click(function(event) {
        event.preventDefault();
        
        var form_errors = false;
        
        $.each(fields, function(index, value) {
            var the_value = $("#" + value).val();
            
            if(the_value == '') {
                $('#'+ value +'_label').addClass('error');
                form_errors = true;
            } else {
                $('#'+ value +'_label').removeClass('error').addClass('valid');
            }
        });
        
        var the_comm_value = $("#comment").val();
        
        if(the_comm_value.length < 50 || the_comm_value.length > 1000) {
            $('#comment_label').addClass('error');
            $('#msg_lenght').show();
            form_errors = true;
        } else {
            $('#comment_label').removeClass('error').addClass('valid');
            $('#msg_lenght').hide();
        }
        
        if(form_errors === false) {
            $('#fcomm').submit();
        }
    });
    
    var contact_fields = ['firstname', 'lastname', 'email', 'society', 'phone', 'message', 'captcha_value'];
    
    $.each(contact_fields, function(index, value) {
        $("#" + value).blur(function() {
            var the_value = $("#" + value).val();
            
            if(the_value == '') {
                $('#'+ value +'_label').addClass('error');
            } else {
                $('#'+ value +'_label').removeClass('error').addClass('valid');
            }
        });
    });
    
    $('#goContact').click(function(event) {
        event.preventDefault();
        
        var form_errors = false;
        
        $.each(contact_fields, function(index, value) {
            var the_value = $("#" + value).val();
            
            if(the_value == '') {
                $('#'+ value +'_label').addClass('error');
                form_errors = true;
            } else {
                $('#'+ value +'_label').removeClass('error').addClass('valid');
            }
        });
        
        if(form_errors === false) {
            $('#contact').submit();
        }
    });
});
