function calculate()
{
    var price = parseInt(document.getElementById('vehicle-price').value);

    var location = document.getElementById('location').value;
    var location_tax = parseInt(document.getElementById('portname').value);
    var uae_transport_tax = parseInt(document.getElementById("shippingDestination").innerHTML.substring(1));

    var calcTypeRadio = document.forms['calculatorForm'].elements['calcType'];
    var radioLength = calcTypeRadio.length;
    var calcType = "";

    if(radioLength == undefined)
        if(calcTypeRadio.checked)
            calcType = calcTypeRadio.value;
    for(var i = 0; i < radioLength; i++) {
        if(calcTypeRadio[i].checked) {
            calcType = calcTypeRadio[i].value;
        }
    }


    var virtual_bid_tax = 30;
    var gate_tax = 35;
    var mailing_fee = 25;
    var transaction_fee = 200;
    var auction_tax;
    var total_sum;
    var regex = /^([\d]+)$/;
    var i;
    var j = 0;

    if ((regex.test(price) != true) || (price <= 0))
    {
        alert('Please enter vehicle price');
        return;
    }
    if (location == 'NONE')
    {
        alert('Please select location of the vehicle');
        return;
    }

    prices = new Array(12);

    prices[0] = 1;
    prices[1] = 100;
    prices[2] = 200;
    prices[3] = 400;
    prices[4] = 600;
    prices[5] = 800;
    prices[6] = 1000;
    prices[7] = 1200;
    prices[8] = 1400;
    prices[9] = 1700;
    prices[10] = 2000;
    prices[11] = 2500;
    prices[12] = 3000;
    prices[13] = 4000;
    prices[14] = 17500;

    taxes = new Array(12);

    taxes[0] = 20;
    taxes[1] = 45;
    taxes[2] = 75;
    taxes[3] = 95;
    taxes[4] = 125;
    taxes[5] = 150;
    taxes[6] = 175;
    taxes[7] = 205;
    taxes[8] = 225;
    taxes[9] = 250;
    taxes[10] = 275;
    taxes[11] = 300;
    taxes[12] = 325;
    taxes[13] = 350;
    taxes[14] = 1000000;

    /*  for (i = 0; i < prices.length; i++)
        {
        if ((prices[i] > price) && (i != prices.length - 1))
        {
        auction_tax = taxes[i-1];
        break;
        } else
        if ((prices[i] == price) || (i == prices.length - 1))
        {
        auction_tax = taxes[i];
        break;
        }
        }  */

    for (i = 0; i < prices.length; i++)
    {
        if ((price > prices[i]) && (i < prices.length - 1))
        {
            j = i;
        } else
            if (price < prices[i])
            {
                auction_tax = taxes[j];
                break;
            } else
            {
                auction_tax = price*0.02;   //2% of Sales price
                break;
            }
    }

    if (calcType == 'auction'){
        total_sum = price + location_tax + auction_tax + transaction_fee + virtual_bid_tax + gate_tax + mailing_fee + uae_transport_tax;

        document.getElementById('auction-tax').innerHTML = '$' + auction_tax;
        document.getElementById('virtual-bid-tax').innerHTML = '$' + virtual_bid_tax;
        document.getElementById('gate-tax').innerHTML = '$' + gate_tax;
        document.getElementById('mailing-fee').innerHTML = '$' + mailing_fee;
        document.getElementById('transaction-fee').innerHTML = '$' + transaction_fee;
        document.getElementById('container-transport').innerHTML = '$' + location_tax;
        document.getElementById('total').innerHTML = '$' + total_sum;
    }
    else if (calcType="final"){
        total_sum = price + location_tax + transaction_fee + mailing_fee + uae_transport_tax;

        document.getElementById('auction-tax').innerHTML = 'included';
        document.getElementById('virtual-bid-tax').innerHTML = 'included';
        document.getElementById('gate-tax').innerHTML = 'included';
        document.getElementById('mailing-fee').innerHTML = '$' + mailing_fee;
        document.getElementById('transaction-fee').innerHTML = '$' + transaction_fee;
        document.getElementById('container-transport').innerHTML = '$' + location_tax;
        document.getElementById('total').innerHTML = '$' + total_sum;
    }
}


var xmlhttp;
var xmlhttp2;

function change_location(){
    var location = document.getElementById('location').value;
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="getPortsForLocation.php";
    url=url+"?location="+location;
    xmlhttp.onreadystatechange=locationStateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function change_port(){
    var portSelect = document.getElementById('portname');
    var selIndex = portSelect.selectedIndex;
    var port = portSelect.options[selIndex].text;
    //var port = document.getElementById('portname').value;


    xmlhttp2=GetXmlHttpObject();

    if (xmlhttp2==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="getDestinationsForPort.php";
    url=url+"?port="+port;
    xmlhttp2.onreadystatechange=portStateChanged;
    xmlhttp2.open("GET",url,true);
    xmlhttp2.send(null);

}

function change_destination(){
    var portSelect = document.getElementById('portname');
    var selIndex = portSelect.selectedIndex;
    var port = portSelect.options[selIndex].text;

    var destSelect = document.getElementById('destinationname');
    var selIndex = destSelect.selectedIndex;
    var destination = destSelect.options[selIndex].text;

    document.getElementById("selectedDestination").innerHTML=destination;
    document.getElementById("shippingDestination").innerHTML="$"+destSelect.value;
    calculate();
}

function locationStateChanged()
{
    if (xmlhttp.readyState==4)
    {
        var port = document.getElementById("port");
        var result = xmlhttp.responseText;
        port.innerHTML = result;
    }
}
function portStateChanged()
{
    if (xmlhttp2.readyState==4)
    {
        document.getElementById("container-transport").innerHTML='$'+document.getElementById('portname').value;
        var destination = document.getElementById("shippingDestination");
        var result = xmlhttp2.responseText;
        destination.innerHTML = '$'+result;
        showRouteOnMap();
    }
}

function destinationStateChanged()
{
    if (xmlhttp3.readyState==4)
    {
        document.getElementById("shippingDestination").innerHTML='$'+xmlhttp2.responseText;
    }
}


function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

var map;
var gdirections;

function loadCalculatorMap(){
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(38.5419, -102.1419), 3);
    }
    gdirections = new GDirections(map, null);

}
function showRouteOnMap()
{
    var portSelect = document.getElementById('portname');
    var selIndex = portSelect.selectedIndex;
    var address2 = portSelect.options[selIndex].text;
    var address1 = ""+document.getElementById('location').value;
    if (gdirections) {
        gdirections.load("from: "+address1+" to: "+address2, getSteps=false);
    }
}

