$(document).ready(function() {
    var task = ''; task = $.query.get('filter'); 
    task = task.replace(/[^a-zA-Z 0-9]+/g, '').replace(/ /g, '').toLowerCase();
    var type = $('#category').val();
    if (type.length > 0) {
        $('.styled').html(''); $('.styled').append($('<option selected></option>').val('').html('Select Task'));
        $.getJSON('/datahandler/subcategory.ashx?_id=' + type, function(taskdata) {
            if (isNaN(taskdata)) {
                $.each(taskdata, function() {
                    var _filter = this['Task'].replace(/[^a-zA-Z 0-9]+/g, '').replace(/ /g, '').toLowerCase();
                    if (_filter == task) { $('.styled').append($('<option selected></option>').val(this['ID']).html(this['Task'])); $('.styled').change(); }
                    else $('.styled').append($('<option></option>').val(this['ID']).html(this['Task']));
        });}});
    }
   
    $('.styled').change(function() {
        var _not_found = true;
        var artid = $.trim($('.styled').find('option').filter(':selected').val());
        $('.article_box').each(function() {
            if ($(this).attr('id') == artid) {
                $(this).attr("style", "visibility:visible;display:block;"); _not_found = false;
            } else { $(this).attr("style", "visibility:hidden;display:none;"); }
        }); if (_not_found) {$('#roofing_default').attr("style", "visibility:visible;display:block;");}
    });
    
    $('.btn_continue').click(function() {
        var cscategory = $.trim($('#category').val());
        var cssubcategory = $.trim($('.styled').find('option').filter(':selected').text());
        var cszipcitystate = $.trim($('.input_get').val());
        if (cssubcategory == '' || cssubcategory == 'Select Task') {alert('Please select a Task');return false;}
        if (cszipcitystate == 'Enter Zip or City, State' || cszipcitystate == '') { alert('Please enter Zip or City, State.');return false;}
        $.get('/datahandler/zipcityvalidator.ashx?_loc=' + escape(cszipcitystate), function(geo) {
            var _geo = geo.toString().split('|');
            var rc_ok = $.trim(_geo[0].toString());
            var rc_zip = $.trim(_geo[1].toString());
            var citystate = $.trim(_geo[2].toString());
            if (rc_ok == 1) {
                rc_click_submit();
                $('.cs_sel_desc').html('');
                $('.cs_ttl_text1').html('');
                var catname = $.trim($('#categoryname').val());
                var _catdescription = (cssubcategory != '' ? cssubcategory : catname);
                var cssearchtxt = 'You Selected: <br>' + _catdescription;
                var csdescription = '<span style=\'color:#000;font-size:' + (catname.length > 22 ? '16' : '18') + 'px;\'>Searching for ' + catname + ' Estimates<br>Presented by a Contractor</span>';
                $('.cs_sel_desc').html(cssearchtxt);
                $('.cs_ttl_text1').html(csdescription);
                $('.cs_frame_url').val('');
				var cshtml = '';
				var _host=(window.location.hostname!=''? window.location.hostname :'www.fastroofingbids.com');
                cshtml = 'http://www.reply.com/clicks/distributable/inline/cs_inlineinteractive/navgraphs/GENERIC/process.aspx?ng=1805G';
                cshtml = cshtml + '&width=480&paddingtop=20&category=' + escape(cscategory) + '&Task=' + escape(cssubcategory) + '&rand=' + Math.floor(Math.random() * 10000) + '&adid=' + $('#adid').val() + '&mkwid=' + escape($('#mkwid').val()) + '&cid=' + escape($('#cid').val()) + '&subid=' + escape($('#subid').val()) + '&zip=' + rc_zip;
                cshtml = cshtml + '&_csexec=' + escape('http://'+ _host +'/inpage/rexec.htm');
                $('.cs_frame_url').val(cshtml);
                $('.cs_vl_url').val('');
                $('.cs_vl_url').val('http://www.reply.com/clicks/?cs_fm=16&ng=1809&category=' + escape(cscategory) + '&task=' + escape(cssubcategory) + '&zipcode=' + rc_zip + '&adid=' + $('#adid').val() + '&_lfsdn=landing.fastroofingbids.com');
                _csshow();
            }
            else {
                alert('Please enter a valid Zip or City, State.');
                return false;
            }
        });
        return false;
    });
});
function GoEnterKey(e) {
    var code = (window.event) ? window.event.keyCode : e.which;
    if (code == 13) {
        if (window.event) window.event.keyCode = 0;
        if (e.which) e.which = 0;
        $('.btn_continue').click();
    }
    return true;
}
