
$('document').ready( function()
{
    var sub_string = location.search.substring(1);
    var pairs = sub_string.split('&');
    var args = '';
    var state = '';
    var city = '';
    var quote_error = false;
    var quote_success = false;
    
    
    for(var i = 0; i < pairs.length; i++)
    {
        var pos = pairs[i].indexOf('=');
        var argname = pairs[i].substring(0,pos);
        var val = pairs[i].substring(pos+1);
        
        if (pos == -1) continue;
        if(argname == 'state' && val != '') state = unescape(val);
        if(argname == 'city' && val != '') city = unescape(val.replace('+', ' '));
        if(argname == 'submit_quote_error') quote_error = true;
        if(argname == 'submit_quote_success') quote_success = true;
    }
    
    if(state != '') buildCity(state, city);
    if(quote_error === true) submit_quote_error();
    if(quote_success === true) submit_quote_success();
    /*
    $('table.sort_options > tbody > tr > td').hover(
        function()
        {
            $(this).addClass('hover');
        },
        function()
        {
            $(this).removeClass('hover');
        }
    );
    */
    $('input[name="zip"]').keypress( function() {
        if($(this).val() != '' && $('.zip_options').css('display') == 'none') {
            $('.zip_options').css('display', 'block');
        }
    }); 
    
    $('.associations_certifications').click( function()
    {
        var goto = $(this).attr('href');
        window.open(goto,"Associations", "menubar=no,width=430,height=360,toolbar=no,scrollbars=yes");
        return false;
    });
});

function submit_quote_error() {
    window.alert('Place a checkmark in the orange “Request a Quote” boxes of 1 to 20 companies before clicking here.');
}
function submit_quote_success() {
    window.alert("Congratulations! You've sucessfully requested custom quotes from bus companies in the BusRates.com database.  You should receive an email confirmation shortly.");
}

function NewWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function doPhoneLink(listingID)
{
    var rtn = true;
    if(showObject('phone'+listingID)) {
        rtn = false;
        hideObject('phoneLink'+listingID);
        logClick(listingID, 'Phone');
    }
    return rtn;
}
function doPhoneLink1(listingID)
{
    var rtn = true;
    if(showObject('phone1'+listingID)) {
        rtn = false;
        hideObject('phoneLink1'+listingID);
        logClick(listingID, 'Phone');
    }
    return rtn;
}

function doEmailLink(listingID)
{
    var rtn = true;
    if(showObject('email'+listingID)) {
        rtn = false;
        hideObject('emailLink'+listingID);
        logClick(listingID, 'Email');
    }
    return rtn;
}

function dowebsiteLink(listingID)
{
    var rtn = true;
    if(showObject('website'+listingID)) {
        rtn = false;
        hideObject('websiteLink'+listingID);
        logClick(listingID, 'Website');
    }
    return rtn;
}

function hideObject(objID)
{
    var obj = getObject(objID);
    var rtn = false;
    if (obj != null) {
        obj.style.display = "none";
        rtn = true;
    }
    return rtn;
}

function showObject(objID)
{
    var obj = getObject(objID);
    var rtn = false;
    if (obj != null) {
      obj.style.display = "inline";
      obj.style.position = "static";
      obj.style.width = "auto";
      obj.style.height = "auto";
      obj.style.overflow = "visible";
      rtn = true;
    }
    return rtn;
}

function getObject(objID)
{
   if (document.all)
   {
      var obj = document.all[objID];
   }
   else if (document.getElementById)
   {
      var obj = document.getElementById(objID);
   }
    return obj;
}

function logClick(listingID, action)
{
   var img = new Image();
   var d = '<?=time()?>';
   var positionString = "";
   img.src = "/logclick.php?OP_Number="+listingID+"&Action="+action+'&'+d;
}

function popitup(url)
{
    var newwindow = window.open(url,'name', config='height=500,width=1000, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, directories=yes, status=yes');
    if (window.focus) {newwindow.focus()}
    return false;
}

function ini_paid_listing_details()
{
    var button_row = document.createElement('tr');
    var button_cell = document.createElement('td');
    var button_detail = document.createElement('input');
    var button_div = document.createElement('div');
    
    $(button_detail).attr('type', 'button');
    $(button_detail).attr('value', '... More Details.');
    $(button_detail).attr('class', 'more_details');
    
    $(button_div).css({'background-color': '#FFFFFF', 'padding': '3px 0px'});
    
    //$(button_cell).attr('colspan', '15');
    //$(button_cell).html(button_detail);
    //$(button_row).append(button_cell);
    
    $(button_div).append(button_detail);
    
    $('table.listing_info_rs tbody > tr:not(:first-child)').hide();
    
    //$('table.listing_info_rs').append(button_row);
    $('table.listing_info_rs').after(button_div);
    $('table.paid_listing').find('.more_details').toggle(
        function ()
        {
            $(this).parent().prev('table').children('tbody').children('tr:hidden').show();
            $(this).attr('value', '... Less Details.');
        },
        
        function ()
        {
            $(this).parent().prev('table').children('tbody').children('tr:not(:first)').hide();
            $(this).attr('value', '... More Details.');
        }
    );
}