$.ajaxSetup({
    error: function(x, e) {
        if (x.status == 0) {
            alert('You are offline!!\n Please Check Your Network.');
        } else if (x.status == 404) {
            alert('Requested URL not found.');
        } else if (x.status == 500) {
            alert('Internal Server Error. ' + x.responseText);
        } else if (e == 'parsererror') {
            alert('Error.\nParsing JSON Request failed.');
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }
});

(function() {

    jQuery.queryString = r = {};
    var q = location.search;
    q = q.replace(/^\?/, ''); // remove the leading ? 
    q = q.replace(/\&$/, ''); // remove the trailing & 
    jQuery.each(q.split('&'), function() {
        var key = this.split('=')[0];
        var val = this.split('=')[1];
        r[key] = val;
    });

})();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var objDealers = {

    showLogin: function() {
        $.get('/lib/ajax/dealerService.asp', { callMethod: 'showLogin' },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divDealerLogin').html(arrResponse[1])
                } else {
                    alert(arrResponse[1]);
                }
            }
        )
    },

    launchEditor: function(vDealerID,vCarID) {
        window.open('/dealers/carmanager.asp?dealerid='+vDealerID+'&carid='+vCarID, 'CarManager', 'width=900,height=700,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,top=0,left=0');
    }

}

var objAuth = {

    authenticate: function() {
        $.post('/lib/ajax/authenticateService.asp', $('#frmDealerLogin').serialize(),
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    location.href = '/dealers/?cachecuster=' + arrResponse[2];
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    signOut: function() {

        $.get('/lib/ajax/authenticateService.asp',
            {
                callMethod: 'signOut'
            },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    location.href = '/';
                } else {
                    alert(arrResponse[1]);
                }
            }
        );

    }

}

var objSearch = {

    getMakes: function() {
        $.get('/lib/ajax/searchService.asp', { callMethod: 'getMakes' },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    arrMakes = arrResponse[1].split(',');
                    for (a = 0; a < arrMakes.length; a++) {
                        if ($.queryString.mk == arrMakes[a]) {
                            $('#searchMake').append('<option SELECTED value="' + arrMakes[a] + '">' + arrMakes[a] + '</option>');
                        } else {
                            $('#searchMake').append('<option value="' + arrMakes[a] + '">' + arrMakes[a] + '</option>');
                        }
                    }
                    if ($('#searchMake').val() != '') {
                        objSearch.getModels($('#searchMake').val());
                    }
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    getModels: function(vMake) {
        if (vMake != '') {
            $.get('/lib/ajax/searchService.asp', { callMethod: 'getModels', make: vMake },
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        $('#searchModel option').remove();
                        $('#searchModel').append('<option value="">All Models</option>');
                        arrModels = arrResponse[1].split(',');
                        for (a = 0; a < arrModels.length; a++) {
                            if (arrModels[a] != '') {
                                if ($.queryString.md == arrModels[a]) {
                                    $('#searchModel').append('<option SELECTED value="' + arrModels[a] + '">' + arrModels[a] + '</option>');
                                } else {
                                    $('#searchModel').append('<option value="' + arrModels[a] + '">' + arrModels[a] + '</option>');
                                }
                            }
                        }
                        if ($('#searchMake').val() != '' && $('#searchModel').val() != '') {
                            objSearch.getYears($('#searchMake').val(), $('#searchModel').val());
                        }
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );
        } else {
            $('#searchModel; option').remove();
            $('#searchModel').append('<option value="">All Models</option>');
            $('#searchYear option').remove();
            $('#searchYear').append('<option value="">All Years</option>');
        }
    },

    getYears: function(vMake, vModel) {
        if (vMake != '' && vModel != '') {
            $.get('/lib/ajax/searchService.asp', { callMethod: 'getYears', make: vMake, model: vModel },
                function(vResponse) {
                    arrResponse = vResponse.split('||');
                    if (arrResponse[0] == '1') {
                        $('#searchYear option').remove();
                        $('#searchYear').append('<option value="">All Years</option>');
                        arrYears = arrResponse[1].split(',');
                        for (a = 0; a < arrYears.length; a++) {
                            if (arrYears[a] != '') {
                                if ($.queryString.yr == arrYears[a]) {
                                    $('#searchYear').append('<option SELECTED value="' + arrYears[a] + '">' + arrYears[a] + '</option>');
                                } else {
                                    $('#searchYear').append('<option value="' + arrYears[a] + '">' + arrYears[a] + '</option>');
                                }
                            }
                        }
                    } else {
                        alert(arrResponse[1]);
                    }
                }
            );
        } else {
            $('#searchYear option').remove();
            $('#searchYear').append('<option value="">All Years</option>');
        }
    }

}

var objLead = {

    renderForm: function() {
        $.get('/lib/ajax/leadService.asp', { callMethod: 'renderForm' },
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divLeadForm').html(arrResponse[1]);
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    submitForm: function() {
        $.post('/lib/ajax/leadService.asp', $('#frmLeadForm').serialize(),
            function(vResponse) {
                arrResponse = vResponse.split('||');
                if (arrResponse[0] == '1') {
                    $('#divLeadForm').html(arrResponse[1]);
                } else {
                    alert(arrResponse[1]);
                }
            }
        );
    },

    setFieldContent: function(objField, vDefaultValue, vAction) {
        if (vAction == 'focus') {
            if (objField.value == vDefaultValue) {
                objField.value = '';
            }
            objField.style.color = '#000000';
        } else {
            if (objField.value == '') {
                objField.value = vDefaultValue;
            }
            objField.style.color = '#999999';
        }
    }

}
