$(document).ready(function(){
    Calendar.setup({
        inputField     :    "ShipDate",
        ifFormat       :    "%m/%d/%Y",
        displayArea    :    "img_pickup_date",
        align          :    "Tl",
        singleClick    :    true
    });
    preloadImage= new Image(); 
    preloadImage.src=HTTP_PATH+"InfoQueryService/129.gif"; 
});

var InfoQueryServiceProcess = false;
function InfoQueryServiceFormSubmit(){
    if(InfoQueryServiceProcess) return;
    InfoQueryServiceProcess = true;
    
    setPreloader();
    
    $('input, select').removeClass('error');
    var url = HTTP_PATH + 'InfoQueryService/ajax.php?'+$("#InfoQueryServiceForm").formSerialize();
    $.get(url,function(data){
        if(data.status){
            showResult(data.responce);
        }else{
            showError(data.error_code, data.errors);
        }
        InfoQueryServiceProcess = false;
    }, "json");
}

function showError(error_code, errors){
    var text;
    if(error_code == 1){
        for(id in errors){
            $('#'+ id).addClass('error');
        }
        text = 'Fill in all required fields.';
    }
    if(error_code == 0 || error_code == 2){
        text = errors;
    }
    $('#result').html(text).addClass('errorMessage');
    $('#InfoQueryService').show();
}

function showResult(res){
    var Origin = res.Shipment.Origin;
    var Destination = res.Shipment.Destination;
    //    var FreightItems = res.Shipment.FreightItems.Freight;
    //    var PickupDateTime = res.Shipment.PickupDateTime;
    
    var html = '<table>';
    html += '<tr>';
    html += '<td width="200"><b>Origin:</b>&nbsp;<u>'+Origin.City+'&nbsp;'+Origin.State+'&nbsp;'+Origin.Zip+'</u></td>';
    html += '</tr>';
    html += '<tr>';
    html += '<td width="200"><b>Destination:</b>&nbsp;<u>'+Destination.City+'&nbsp;'+Destination.State+'&nbsp;'+Destination.Zip+'</u></td>';
    html += '</tr>';
    html += '</table>';
    html += '<br/>';
    
    if(res.ServiceOptions){
        html += '<table id="data" cellspacing="0" cellpadding="6">';
        html += '<tr><td width="196" class="head" colspan="6"><strong>Info Query Service Result</strong></td></tr>';
        html += '<tr>';
        html += '<td class="subhead" width="50%">Carrier Name</td>';
        html += '<td class="subhead" align="center">Carrier SCAC</td>';
        html += '<td class="subhead" align="center">Total Price</td>';
        html += '<td class="subhead" align="center">Transit Days</td>';
        html += '</tr>';
        var className = 'even';
        for(n in res.ServiceOptions.ServiceOption){
            className = (className == 'even') ? 'odd' : 'even';
            obj = res.ServiceOptions.ServiceOption[n];
            html += '<tr class="'+className+'">';
            html += '<td>'+obj.CarrierName+'</td>';
            html += '<td align="center">'+obj.CarrierSCAC+'</td>';
            html += '<td align="center">'+obj.TotalPrice+'</td>';
            html += '<td align="center">'+obj.TransitDays+'</td>';
            html += '</tr>';
        }
        html += '</table>';
    }else{
        html += '<table id="data" cellspacing="0" cellpadding="6">';
        html += '<tr><td class="head"><strong>Info Query Service Result</strong></td></tr>';
        html += '<tr><td class="subhead" align="center">Carriers not found!</td></tr>';
        html += '</table>';
    }
    
    
    setResult(html);
}

function setPreloader(){
    $('#InfoQueryService').hide();
    $('#result').html('<img src="'+HTTP_PATH+'InfoQueryService/129.gif"  alt="preloader" /><br/>We are generating your quote.<br/>One momemt please.').removeClass('errorMessage').addClass('center').show();
}

function setResult(html){
    html += '<br/><a href="javascript:BackToInfoQueryService();">&laquo;&nbsp;Back to Options</a>';
    $('#result').html(html).removeClass('errorMessage').removeClass('center');
}

function BackToInfoQueryService(){
    $('#result').html('').hide();
    $('#InfoQueryService').show();
}
