﻿var appPath = '/'
function IsDate(str)
{
    var regex = new RegExp("^(0[1-9]|[1-9]|1[012])/(0[1-9]|[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)$");
    var match = regex.exec(str);    
    if (match != null)
    {
        var dateArr = str.split('/');
        var date = new Date(dateArr[2], dateArr[0]-1, dateArr[1]);
        
        var convertedDate = "" + date.getFullYear() + (date.getMonth()+1) + date.getDate();
        var givenDate = "" + parseInt(dateArr[2], 10) + parseInt(dateArr[0], 10) + parseInt(dateArr[1], 10);
        
        return convertedDate == givenDate ? date : null;
    }
    
    return null; 
}

function CreateSelectOption(val, txt, select)
{
    if( select != null && select == true )
    {
        return '<option value="' + val + '" selected="true">' + txt + '</option>';
    }
    
    return '<option value="' + val + '">' + txt + '</option>';
}

function PopulateAutoMake(ddl, load, prompt)
{
    $('#' + ddl).attr('disabled', true).html(CreateSelectOption('', load));
    
    $.ajax({ type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: "/QuotesAtHomeNew/WebServices/AJAXHelper.asmx/GetAutoMake"
            , dataType: "text"
            , dataFilter: function(data) {
                var msg;
                if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
                    msg = JSON.parse(data);
                }
                else {
                    msg = eval('(' + data + ')');
                }
                if (msg.hasOwnProperty('d')) {
                    return msg.d;
                }
                else {
                    return msg;
                }
            }
            , success: function(msg) {
                var r = eval((msg));     
                
                var options = '';
                if( prompt != null && prompt.length > 0 )
                {
                    options = CreateSelectOption('-1', prompt);
                }
                
                if( r != null ) 
                {                  
                    for( i=0; i<r.length; i++ ) 
                    {
                        options += CreateSelectOption(r[i].id, r[i].name);
                    }
                    
                    $('#' + ddl).attr('disabled', false).html(options);
                }
            }
    });
}

function PopulateAutoModel(makeid, ddl, load, prompt)
{
    if( makeid != null ) 
    {
        $('#' + ddl).attr('disabled', true).html(CreateSelectOption('', load));
        
        $.ajax({ type: "POST"
                , contentType: "application/json; charset=utf-8"
                , url: "/QuotesAtHomeNew/WebServices/AJAXHelper.asmx/GetAutoModelByMake"
                , data: "{ 'makeID' : '" + makeid + "'}"
                , dataType: "text"
                , dataFilter: function(data) {
                    var msg;
                    if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
                        msg = JSON.parse(data);
                    }
                    else {
                        msg = eval('(' + data + ')');
                    }
                    if (msg.hasOwnProperty('d')) {
                        return msg.d;
                    }
                    else {
                        return msg;
                    }
                }
                , success: function(msg) {
                    var r = eval((msg));     
                    
                    var options = '';
                    if( prompt != null && prompt.length > 0 )
                    {
                        options = CreateSelectOption('-1', prompt);
                    }
                    
                    if( r != null ) 
                    {                  
                        for( i=0; i<r.length; i++ ) 
                        {
                            options += CreateSelectOption(r[i].id, r[i].name);
                        }
                        
                        $('#' + ddl).attr('disabled', false).html(options);
                    }
                }
        });
    }
}

function PopulateAutoYearByModelAndMake(makeid, modelid, ddl, load, prompt)
{
    if( makeid != null && modelid != null )
    {
        $('#' + ddl).attr('disabled', true).html(CreateSelectOption('', load));
        
        $.ajax({ type: "POST"
                , contentType: "application/json; charset=utf-8"
                , url: "/QuotesAtHomeNew/WebServices/AJAXHelper.asmx/GetAutoYearByModelAndMake"
                , data: "{ 'makeID' : '" + makeid + "', 'modelID' : '" + modelid + "' }"
                , dataType: "text"
                , dataFilter: function(data) {
                    var msg;
                    if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
                        msg = JSON.parse(data);
                    }
                    else {
                        msg = eval('(' + data + ')');
                    }
                    if (msg.hasOwnProperty('d')) {
                        return msg.d;
                    }
                    else {
                        return msg;
                    }
                }
                , success: function(msg) {
                    var r = eval((msg));     
                    
                    var options = '';
                    if( prompt != null && prompt.length > 0 )
                    {
                        options = CreateSelectOption('-1', prompt);
                    }
                    
                    if( r != null ) 
                    {                  
                        for( i=0; i<r.length; i++ ) 
                        {
                            options += CreateSelectOption(r[i].yearof, r[i].yearof);
                        }
                        
                        $('#' + ddl).attr('disabled', false).html(options);
                    }
                }
        });
    }
}

function ChangeInputTypes(ddlmake, ddlmodel, ddlyear
            , txtmake, txtmodel, txtyear
            , hfmake, hfmodel, hfyear
            , lblmake, lblmodel, lblyear, lblchange, lnkchange
            , rfvddlmake, rfvddlmodel, rfvddlyear
            , rfvtxtmake, rfvtxtmodel, rfvtxtyear, revtxtyear)
{
    if ($('#' + ddlmake).is(":visible") == true ) 
    {
        $('#' + ddlmake).hide();        
        $('#' + ddlmodel).hide();        
        $('#' + ddlyear).hide();
        
        $('#' + hfmake).val('');
        $('#' + hfmodel).val('');
        $('#' + hfyear).val('');
        
        $('#' + txtmake).show();
        $('#' + txtmodel).show();
        $('#' + txtyear).show();
                
        $('#' + lblmake).text('Enter the vehicle make (example Toyota):');
        $('#' + lblmodel).text('Enter the vehicle model (example Camry):');
        $('#' + lblyear).text('Enter the vehicle year (example 2010):');        
        $('#' + lblchange).text('If you would like to select your vehicle from a drop-down,');
        $('#' + lnkchange).text('click here to select your vehicles information.');
        
        ValidatorEnable($('#' + rfvddlmake)[0], false);
        ValidatorEnable($('#' + rfvddlmodel)[0], false);
        ValidatorEnable($('#' + rfvddlyear)[0], false);
        
        ValidatorEnable($('#' + rfvtxtmake)[0], true);
        ValidatorEnable($('#' + rfvtxtmodel)[0], true);
        ValidatorEnable($('#' + rfvtxtyear)[0], true);
        ValidatorEnable($('#' + revtxtyear)[0], true);
    }
    else
    {
        $('#' + ddlmake).show();
        $('#' + ddlmodel).show();
        $('#' + ddlyear).show();
        
        $('#' + ddlmake).val(-1);
        $('#' + ddlmodel).attr('disabled', true).html(CreateSelectOption('', 'Select Auto Model....'));
        $('#' + ddlyear).attr('disabled', true).html(CreateSelectOption('', 'Select Auto Model....'));
        
        $('#' + txtmake).hide();
        $('#' + txtmodel).hide();
        $('#' + txtyear).hide();
        
        $('#' + lblmake).text('Please select the make of your vehicle:');
        $('#' + lblmodel).text('Please select the model of your vehicle:');
        $('#' + lblyear).text('Please select the year of your vehicle:');        
        $('#' + lblchange).text('If you cannot find your vehicle in the drop-down lists below,');
        $('#' + lnkchange).text('click here to type in your vehicles information.');
        
        ValidatorEnable($('#' + rfvddlmake)[0], true);
        ValidatorEnable($('#' + rfvddlmodel)[0], true);
        ValidatorEnable($('#' + rfvddlyear)[0], true);
        
        ValidatorEnable($('#' + rfvtxtmake)[0], false);
        ValidatorEnable($('#' + rfvtxtmodel)[0], false);
        ValidatorEnable($('#' + rfvtxtyear)[0], false);
        ValidatorEnable($('#' + revtxtyear)[0], false);
    }      
    
    $('#' + txtmake).val('');  
    $('#' + txtmodel).val('');
    $('#' + txtyear).val('');
}


function GetCityAutoComplete( state, city, zip, prev ) 
{
    var len = $('#' + city).val().length;
    var cur = $('#' + prev).val()
    if( len >= 2 && len > cur ) 
    {
        $.ajax({ type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: appPath + "WebServices/AJAXHelper.asmx/GetCityAutoComplete"
            , data: "{ 'city' : '" + $('#' + city).val() + "', 'stateID' : '" + $('#' + state).val() + "' }"
            , dataType: "text"
            , dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') { msg = JSON.parse(data); } else { msg = eval('(' + data + ')'); } if (msg.hasOwnProperty('d')) { return msg.d; } else { return msg; } }
            , success: function(msg) {
                var r = eval((msg));
                $('#' + city).autocomplete(r, {
                    formatItem: function(item) {
                        return item.c;
                    }
                  , max: 50
                }).result(function(event, item) {
                    if (zip == null) {
                        parent.SetZipFromZipFinder(item.z);
                    } else {
                        $('#' + zip).val(item.z);
                    }
                });
            }
            , error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('error');
            }
        });
    }
    
    $('#' + prev).val(len);        
}