function getHTTPObject(){
    if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");else if (window.XMLHttpRequest) return new XMLHttpRequest();else {
        alert("Your browser does not support AJAX."); return null;
    }
}

// refresh_tabs
function _refresh_tabs(id){
    if ( document.getElementById('nav_home') != null ) {
        document.getElementById('nav_home').className='nav-default';
    }
    if ( document.getElementById('nav_images') != null ) {
        document.getElementById('nav_images').className='nav-default';
    }
    if ( document.getElementById('nav_recommend')!=null ) {
        document.getElementById('nav_recommend').className='nav-default';
    }
    if ( document.getElementById('nav_video') != null ) {
        document.getElementById('nav_video').className='nav-default';
    }
    if ( document.getElementById('nav_list_view') != null ) {
        document.getElementById('nav_list_view').className='nav-default';
    }
    if ( document.getElementById('nav_gallery_view') != null ) {
        document.getElementById('nav_gallery_view').className='nav-default';
    }
    document.getElementById(id).className='nav-selected';
    return true;
}
// Load Ports Ajax Call
function ajaxCall(action, id, container_id) {
    httpObject= getHTTPObject();
    if (httpObject != null) {
        // if ( document.getElementById('nav_home') != null ) {
        //    document.getElementById('nav_home').className='nav-default';
        // }
        var container;
        container= document.getElementById(container_id);

        httpObject.open( "GET", "ajax_call.php?action="+action+"&id="+id, true);
        httpObject.send(null);
        httpObject.onreadystatechange= function() {
            if(httpObject.readyState == 4) {
                container.innerHTML= httpObject.responseText;
                // document.getElementById('tab_content').innerHTML= httpObject.responseText;
            }
        }
    }
}

// Load Tab vehicle view
function load_tab(tab,id,action) {
    httpObject= getHTTPObject();
    if (httpObject != null) {
        _refresh_tabs(tab);
        httpObject.open("GET", "vehicle-view.php?tab="+tab+"&id="+id+"&action="+action, true);
        httpObject.send(null);
        httpObject.onreadystatechange = setVehicleViewOutput;
    }
}

// Send recommendation
function send_recommendation(tab,id,action,from,to,message) {
    httpObject= getHTTPObject();
    if (httpObject != null) {
        _refresh_tabs(tab);
        msg= message;
        var intIndexOfMatch= msg.indexOf( "\n" );
        while (intIndexOfMatch != -1){
            msg= msg.replace("\n","<br>");
            intIndexOfMatch= msg.indexOf( "\n" );
        }
        httpObject.open("GET", "vehicle-view.php?tab="+tab+"&id="+id+"&action="+action+"&from="+from+"&to="+to+"&message="+msg, true);
        httpObject.send(null);
        httpObject.onreadystatechange = setVehicleViewOutput;
    }
}

// CIF Calculator
function calculate( fob, size, promo_fob ) {
    // clear contents
    document.getElementById("cif-total").innerHTML="";
    document.getElementById("cif-error").innerHTML="";

    var port_id= document.calculator.port.options[document.calculator.port.selectedIndex].value;
    var error= true;
    if ( !isNaN(port_id) ) {
        var port= document.calculator.elements["port"+port_id];
        if ( !isNaN(port.value) && !isNaN(size ) ) {
            var freight= port.value*size;
            if ( freight > 0 ) {
                var total= 0;
                if ( (!isNaN(promo_fob)) && (promo_fob>0) ) {
                    total= freight + 90 + fob - promo_fob;
                    document.getElementById("cif-total").innerHTML="\
<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><TR><TD>FOB</TD><TD class=\"secondcol\"><span style=\"text-decoration:line-through;\">$ "+fob+"</span> <span style=\"color:#C11B17;\">Now $ "+(fob-promo_fob)+"</span></TD></TR><TR><TD>Insurance</TD><TD>$ 90</TD></TR><TR><TD>Freight</TD><TD>$ "+freight+ "</TD></TR><TR><TD class=\"sum\">Total CIF</TD><TD class=\"sum\">$ "+total+ "</TD></TR></TABLE>";
                    document.getElementById("cif-error").innerHTML="1. CIF includes vehicle price insurance and transport costs until port of delivery.<br>2. Required JEVIC/JAAI inspections for Kenya, Zambia and Tanzania are not included.";
                    error= false;
                }
                else {
                    total= freight + 90 + fob;
                    document.getElementById("cif-total").innerHTML="<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><TR><TD>FOB</TD><TD class=\"secondcol\">$ "+fob+"</TD></TR><TR><TD>Insurance</TD><TD>$ 90</TD></TR><TR><TD>Freight</TD><TD>$ "+freight+ "</TD></TR><TR><TD class=\"sum\">Total CIF</TD><TD class=\"sum\">$ "+total+ "</TD></TR></TABLE>";
                    document.getElementById("cif-error").innerHTML="1. CIF includes vehicle price insurance and transport costs until port of delivery.<br>2. Required JEVIC/JAAI inspections for Kenya, Zambia and Tanzania are not included.";
                    error= false;
                }
            }
        }
    }
    if ( error==true ) {
        document.getElementById("cif-total").innerHTML="";
        document.getElementById("cif-error").innerHTML="This function is not available online for this port.<br/>Please contact us to get a non-obligation quote.";
    }
}

//Change the value of the outputText field
function setVehicleViewOutput(){ 
    if(httpObject.readyState == 4){
        document.getElementById('tab_content').innerHTML= httpObject.responseText;
    }
}
// Implement business logic - Load tabs browse page
function load_browse_tab(tab,params,page) {
    httpObject= getHTTPObject();if (httpObject != null) {
        _refresh_tabs(tab);httpObject.open("GET", page+"?tab="+tab+params, true);httpObject.send(null);httpObject.onreadystatechange= setListViewOutput;
    }
}

function setListViewOutput(){ 
    if(httpObject.readyState == 4){
        document.getElementById('vehicle_list').innerHTML= httpObject.responseText;
    }
}

function restore_main(){
    var temp= document.getElementById('medium_image_main').innerHTML;document.getElementById('main_image').innerHTML=temp;
}

function over_button(id){
    var temp= document.getElementById('medium_image_'+id).innerHTML;if (temp!=null && temp!='' ) {
        document.getElementById('main_image').innerHTML=temp;document.getElementById('image_'+id).setAttribute('class','selected');
    }
}

function out_button(id){
    document.getElementById('image_'+id).setAttribute('class','default');
}

// add to favourites
function fav() {
    var lnkText= document.createTextNode('  Add to Favourites');var lnkImage= document.createElement('img');lnkImage.setAttribute('src', 'js/img/favs.png');lnkImage.setAttribute('align', 'top');
    var s = document.getElementById('fav');
    if (window.sidebar) {
        s.appendChild(lnkImage);s.appendChild(lnkText);s.style.cursor = 'pointer';s.onclick= function() {
            window.sidebar.addPanel(document.title,self.location,'')
        };
    }
    else if (window.external) {
        s.appendChild(lnkImage);s.appendChild(lnkText);s.style.cursor = 'pointer';s.onclick = function() {
            window.external.AddFavorite(self.location,document.title)
        };
    }
    else if (window.opera) {
        s.appendChild(lnkImage);s.appendChild(lnkText);s.onclick = function(){
            var e = document.createElement('a');e.setAttribute('href',self.location);e.setAttribute('title',document.title);e.setAttribute('rel','sidebar');e.click();
        }
    }
}