
function getViewportSize()
{
    if ( typeof window.innerWidth != 'undefined' )
    {
        screenW = window.innerWidth;
        screenH = window.innerHeight;
    }
    else if ( typeof document.documentElement != 'undefined'
              && typeof document.documentElement.clientWidth != 'undefined'
              && document.documentElement.clientWidth != 0 )
    {
        screenW = document.documentElement.clientWidth;
        screenH = document.documentElement.clientHeight;
    }
    else if ( document.body
              && typeof document.body != 'undefined'
              && typeof document.body.clientWidth != 'undefined' )
    {
        screenW = document.body.clientWidth;
        screenH = document.body.clientHeight;
    } 
}

var wnd = '';
function openWindow( url, winid )
{
    if ( wnd != '' )
        wnd.close();
        
    wnd = window.open( url, winid, "status=0,toolbar=0,menubar=0,width=600,height=600,scrollbars=1" );
}

window.onresize = getViewportSize;
var mouseX = 10;
var mouseY = 10;
var scrOfX = 0, scrOfY = 0;

// Decide browser version
var ns4 = (document.layers)? true:false;
var ns6 = (document.getElementById)? true:false;
var ie4 = (document.all)? true:false;
var ie5 = false;

var isdrag = false;

// Microsoft Stupidity Check(tm).
if (ie4) {
    if ((navigator.userAgent.indexOf('MSIE 5') > 0) || (navigator.userAgent.indexOf('MSIE 6') > 0)) {
        ie5 = true;
    }
    if (ns6) { ns6 = false; }
}


// Capture events, alt. diffuses the overlib function.
if ( (ns4) || (ie4) || (ns6)) {
    document.onmousemove = mouseMove
    if (ns4) document.captureEvents(Event.MOUSEMOVE)
}

function mouseMove(e) {
   if ( (ns4) || (ns6) ) {mouseX=e.pageX; mouseY=e.pageY;}
   if (ie4) {mouseX=event.x; mouseY=event.y;}
   if (ie5) {mouseX=event.x+self.document.body.scrollLeft; mouseY=event.y+self.document.body.scrollTop;}

   if (mouseX < 0) { mouseX = 0; }
   if (mouseY < 0) { mouseY = 0; }  
   getScrollXY();
}

function getScrollXY() {
  if( typeof( window.pageYOffset ) == 'number' ) {
  //Netscape compliant
      scrOfY = window.pageYOffset;
      scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  //DOM compliant
      scrOfY = document.body.scrollTop;
      scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
  //IE6 standards compliant mode
      scrOfY = document.documentElement.scrollTop;
      scrOfX = document.documentElement.scrollLeft;
  }
}

function disableInput( theId )
{
return;
    var children = document.getElementById(theId).getElementsByTagName('input');
    for( i=0; i<children.length; i++ )
        children[i].setAttribute( 'disabled', true );
    
    var children = document.getElementById(theId).getElementsByTagName('select');
    for( i=0; i<children.length; i++ )
        children[i].setAttribute( 'disabled', true );
}

function updateLiters( idd )
{
    var x = document.getElementById( 'idBoxFuelPrice4Liter' );
    var p1 = parseFloat( x.value.replace(',','.') );

    var x = document.getElementById( 'idBoxFuelPrice' );
    var p2 = parseFloat( x.value.replace(',','.') );

    if ( ( p1 > 0 ) && ( p2 > 0 ) )
    {
        var p = p2 / p1;
        x = p.toFixed( 2 );
    }
    else
    {
        x = "n/a";
    }
    
    document.getElementById( idd ).innerHTML = x;
}

function enableInput( theId )
{
return;
    var children = document.getElementById(theId).getElementsByTagName('input');
    for( i=0; i<children.length; i++ )
        children[i].setAttribute( 'disabled', false );
    
    var children = document.getElementById(theId).getElementsByTagName('select');
    for( i=0; i<children.length; i++ )
        children[i].setAttribute( 'disabled', false );
}

function hideBox( id )
{
    var x = document.getElementById( id );
    if ( x ) 
    {
        x.style.display = 'none';
    }
}

function showBoxW( id, w, h )
{
    var x = document.getElementById( id );
    if ( x )
    {
        center_box( x, w, h );
        x.style.display = 'block';
    }
}

function showBox( id, left, top )
{
    var xCorr = 50;
    var yCorr = 50;
    
    var x = document.getElementById( id );
    if ( x )
    {
        /*
        var tw = x.style.width ;
        var th = x.style.height ;
        var w = parseInt( tw );
        var h = parseInt( th );

        if ( left + w + xCorr > screenW )
                left = left - ( left + w + xCorr - screenW );

        if ( top - scrOfY + h + yCorr > screenH )
                top = top - ( top - scrOfY + h + yCorr - screenH );

        x.style.left = left;
        x.style.top = top;
        */

        center_box( x, 450, 300 );
        x.style.display = 'block';
    }
}

function updateBoxContent( id, content )
{
    var x = document.getElementById( id );
    if ( x )
    {
        if ( x.innerHTML )
        {
                x.innerHTML = content;
        }
    }
}

function updateField( id, val )
{
    var x = document.getElementById( id );
    if ( x )
    {
        x.value = val;
    }
}

function updateSelect( id, values )
{
    var x = values.split( "|" );

    var s = document.getElementById( id );
    if ( ! s ) 
        return;

    s.length = 0;
    for( i=0; i<x.length; i++ )
    {
        var y = document.createElement( 'option' );

        y.value = x[i];
        y.text = x[i];
        try {
            s.add(y,null);
        }
        catch(ex) {
            s.add(y);
        }

    }
}

function showFuelBox( car, vehicle )
{
    hideAllBoxes();

    updLastFuelType( car );
    updateField( 'idBoxFuelCarID', car );
    if ( vehicle != '' )
        updateField( 'idBoxFuelVehicleType', vehicle );
    showBox( 'idBoxFuel', mouseX, mouseY );
    getHistory( 'idboxhistoryfuel', 'fuel', car );
}

function showChangeBox( title, parttype, f, t, v, tp, notif )
{
    hideAllBoxes();

    if ( parttype == 'tyres' )
    {
        document.location = 'index.php?page=tyres';
        return;
    }
    
    if ( parttype == 'fuel' )
    {
        showFuelBox( f, '' );
        return;
    }

    if ( parttype == 'leasing' )
    {
        setCheckBox( 'idBoxLeaseNotifCheck', notif );
        getHistory( 'idBoxLeaseHistory', parttype, -1 );
        showBox( 'idBoxLease', mouseX, mouseY );
        return;
    }

    if ( parttype == 'tyresh' )
    {
        updateField( 'idBoxTyresCarId', f );
        showBox( 'idBoxTyres', mouseX, mouseY );
        return;
    }
    
    if ( parttype == 'mileage' )
    {
        updateField( 'idBoxMileageCarId', f );
        showBox( 'idBoxMileage', mouseX, mouseY );
        return;
    }

    if ( parttype == 'repair' )
    {
        showBox( 'idBoxNewFreeRepair', mouseX, mouseY );
        return;
    }

    if ( parttype == 'accessories' )
    {
        showBox( 'idBoxNewAccessories', mouseX, mouseY );
        return;
    }

    if ( parttype == 'vignette' )
    {
        updateBoxContent( 'idBoxNewVignetteCap', title );
        setCheckBox( 'idBoxVigNotifCheck', notif );
        getHistory( 'idBoxVigHistory', parttype, -1 );
        showBox( 'idBoxNewVignette', mouseX, mouseY );
        return;
    }

    if ( parttype == 'tax' )
    {
        updateBoxContent( 'idBoxNewTaxCap', title );
        setCheckBox( 'idBoxTaxNotifCheck', notif );
        getHistory( 'idBoxTaxHistory', parttype, -1 );
        showBox( 'idBoxNewTax', mouseX, mouseY );
        return;
    }
    
    if ( parttype == 'inspection' )
    {
        updateBoxContent( 'idBoxNewInspectionCap', title );
        setSlider( 'idSliderInsp', f, t, v );
        setCheckBox( 'idBoxInspNotifCheck', notif );
        getHistory( 'idBoxInspHistory', parttype, -1 );
        showBox( 'idBoxNewInspection', mouseX, mouseY );
        return;
    }

    if ( parttype == 'cinspection' )
    {
        updateBoxContent( 'idBoxNewCinspectionCap', title );
        setSlider( 'idSliderCinsp', f, t, v );
        setCheckBox( 'idBoxCinspNotifCheck', notif );
        getHistory( 'idBoxCinspHistory', parttype, -1 );
        showBox( 'idBoxNewCinspection', mouseX, mouseY );
        return;
    }

    if ( ( parttype == 'ins_casco' ) || ( parttype == 'ins_public' ) )
    {
        setCheckBox( 'idBoxInsNotifCheck', notif );
        updateBoxContent( 'idBoxNewInsuranceCap', title ); 
        updateField( 'idInsurancePartTypeField', parttype ); 

        if ( ( f > t ) && ( t == 0 ) )
            updateField( 'idBoxNewInsuranceCarId', f );
    
        getHistory( 'idBoxInsHistory', parttype, -1 );
        showBox( 'idBoxNewInsurance', mouseX, mouseY );
        return;
    }
    
    updateBoxContent( 'idBoxNewChangeCap', title ); 
    updateField( 'idPartTypeField', parttype ); 
    updateBoxContent( 'idSliderChangeType', tp );
    setSlider( 'idSliderChange', f, t, v );
    setCheckBox( 'idBoxNewChangeNotifCheck', notif );
    getHistory( 'idboxhistory', parttype, -1 );
    getPartBrands( parttype );
    showBox( 'idBoxNewChange', mouseX, mouseY );
}

function setCheckBox( id, state )
{
    var x = document.getElementById( id );
    if ( x )
    {
        x.checked = state;
    }
}

function setSlider( id, f, t, v )
{
    var m = Math.round( ((v-f) / (t-f)) * 100 );
    var x;
    
    x = document.getElementById( id );
    x.style.left = m;
    
    x = document.getElementById( id + "_mark" );
    x.style.cssText = "width:" + m + "px;";
   
    x = document.getElementById( id + "Display" );
    x.setAttribute( "value", v );
    x.setAttribute( "defaultValue", v );
    x.setAttribute( "from", f );
    x.setAttribute( "to", t );

    carpeInit();
}

function showAddNote()
{
    showBox( 'idBoxNewNote', mouseX, mouseY );
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/,"");
}

function getPartBrands( parttype )
{
    var xmlHttpReq23 = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq23 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq23 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    url =  "/index.php?action=getPartBrands&pt=" + parttype;
    self.xmlHttpReq23.open('GET', url, false);
    self.xmlHttpReq23.onreadystatechange = function() {
        if (self.xmlHttpReq23.readyState == 4) {
            updateSelect( "idBoxNewChangeBrandSelect", self.xmlHttpReq23.responseText );
        }
    }
   
    updateSelect( "idBoxNewChangeBrandSelect", "loading..." );
    self.xmlHttpReq23.send( url );

    if (self.xmlHttpReq23.readyState == 4)
    {
        updateSelect( "idBoxNewChangeBrandSelect", self.xmlHttpReq23.responseText );
    }
}

function getHistory( idDisplay, parttype, car ) 
{
    var xmlHttpReq1 = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq1 = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    url =  "/index.php?action=printHistory&pt=" + parttype;
    if ( car > 0 )
        url = url + "&car=" + car;

    self.xmlHttpReq1.open('GET', url, true);
    self.xmlHttpReq1.onreadystatechange = function() {
        if (self.xmlHttpReq1.readyState == 4) {
            updateBoxContent( idDisplay, self.xmlHttpReq1.responseText );
        }
    }
    
    updateBoxContent( idDisplay, "loading..." );
    self.xmlHttpReq1.send( url );
}
                        
function getMileage( idMileage, idDate, idcarid, strURL) 
{
    var xmlHttpReq = false;
    var self = this;

    var x = document.getElementById( idcarid );
    if ( x )
        car = x.value;
    else
        car = idcarid;
    
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    x = document.getElementById( idDate );
    url =  "?action=printMileage&car=" + car + "&dt=" + x.value;
    self.xmlHttpReq.open('GET', strURL + url, true);
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            var x = document.getElementById( idMileage );
            x.value = trim( self.xmlHttpReq.responseText );
        }
    }
    self.xmlHttpReq.send( url );
}

function execRequest( key, url, callback )
{
    eval( "var xmlHttpReq" + key + " = false;" ); 
    var self = this;

    if (window.XMLHttpRequest) {
        eval( "self.xmlHttpReq" + key + " = new XMLHttpRequest();" );
    }
    else if (window.ActiveXObject) {
        eval( 'self.xmlHttpReq' + key + ' = new ActiveXObject("Microsoft.XMLHTTP");' );
    }

    eval( "self.xmlHttpReq" + key + ".open('GET', url, false);" );
    eval( "self.xmlHttpReq" + key + ".onreadystatechange = function() {\n if (self.xmlHttpReq" + key + ".readyState == 4) {\n " + callback + "( trim( self.xmlHttpReq" + key + ".responseText ) );\n}\n}" );

    eval( "self.xmlHttpReq" + key + ".send( url );" );
}

function doBrandSelect( sel, id )
{
    var f = document.getElementById( id + 'F' );
    var d = document.getElementById( id + 'D' );

    if ( sel.selectedIndex >= 0 )
    {
        f.value = sel.options[ sel.selectedIndex ].value;
        if ( f.value == 'other' )
        {
             f.value = '';
             d.style.display = 'inline';
        }
        else
        {
             d.style.display = 'none';
        }
    }
    
}

function center_box(box, width, height)  
{ 
    var has_inner = typeof(window.innerWidth) == 'number';  
    var has_element = document.documentElement  
        && document.documentElement.clientWidth;

    cleft = has_inner  
        ? pageXOffset +   
          (window.innerWidth - width)/2  
        : has_element  
          ? document.documentElement.scrollLeft +   
            (document.documentElement.clientWidth - width)/2  
          : document.body.scrollLeft +   
            (document.body.clientWidth - width)/2;  
  
    ctop = has_inner  
        ? pageYOffset + (window.innerHeight - height)/2  
        : has_element  
          ? document.documentElement.scrollTop +   
            (document.documentElement.clientHeight - height)/2  
          : document.body.scrollTop +   
            (document.body.clientHeight - height)/2;  
  
    box.style.left = cleft > 0 ? cleft + 'px' : '0px';  
    box.style.top = ctop > 0 ? ctop + 'px' : '0px';
}

function areYouSure( text, location )
{
    var answer = confirm( text );

    if ( answer == true )
    {
        document.location = location;
    }

    return true;
}

function retrieveInfo( pt, id )
{
     x = "get" + pt + "Info(" + id + ");";
     eval( x );
}

function getTyresHistInfo( id )
{
    execRequest( 'TyresHistInfo', "/index.php?action=printTyresHistInfo&id=" + id, 'fillTyresHistInfo' );
}

function fillTyresHistInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxTyres';

    setInputValue( id + 'ThId', x[k++] );
    setInputValue( id + 'Mileage', x[k++] );
    setInputValue( id + 'ChangePrice', x[k++] );
    setInputValue( 'iddtdate_change', x[k++] );
}

function getKmStoreService( calendar, dt )
{
    if ( dt.length == 10 )
        execRequest( 'idKmSS', "/index.php?action=getKmStoreService&dt=" + dt, 'fillKmStoreService' );

    if ( calendar.dateClicked ) 
    {
         setInputValue( 'iddtdt_change', dt );
         calendar.callCloseHandler(); // this calls "onClose" (see above)
    }
}

function fillKmStoreService( info )
{
    var x = info.split( "|" );
    var k = 0;
    
    setInputValue( 'idboxmileaged', x[k++] );
    setInputValue( 'idBoxNewChangeMarket', x[k++] );
    setInputValue( 'idBoxNewChangeRepairShop', x[k++] );
}

function getPhInfo(phid) 
{
    execRequest( 'PhInfo', "/index.php?action=printPHInfo&id=" + phid, 'fillPhInfo' );
}

function fillPhInfo( info )
{
    var x = info.split( "|" );
    var id = 'idBoxNewChange';
    var k = 0;
    
    setInputValue( id + 'PHId', x[k++] );
    setInputValue( 'iddtdt_change', x[k++] );
    
    setSelectValue( id + 'BrandSelect', x[k] );
    doBrandSelect( document.getElementById( id + 'BrandSelect' ), 'newChangeBrand' ); 
    setInputValue( 'newChangeBrandF', x[k++] );
    
    setInputValue( id + 'Market', x[k++] );
    setInputValue( id + 'PriceParts', x[k++] );
    setInputValue( id + 'PriceTotal', x[k++] );
    setInputValue( 'idboxmileaged', x[k++] );
    setInputValue( id + 'RepairShop', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function getFuelInfo(phid)
{
    execRequest( 'FuelInfo', "/index.php?action=printFuelInfo&id=" + phid, 'fillFuelInfo' );
}

function fillFuelInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxFuel';

    setInputValue( 'iddtfuel_date', x[k++] );
    setInputValue( 'idboxmileagef', x[k++] );
    setInputValue( id + 'Id', x[k++] );
    setSelectValue( id + 'Type', x[k++] );
    setInputValue( id + 'Station', x[k++] );
    setInputValue( id + 'Price', x[k++] );
    setInputValue( id + 'Price4Liter', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function getMilInfo(phid)
{
    execRequest( 'MilInfo', "/index.php?action=printMilInfo&id=" + phid, 'fillMilInfo' );
}

function fillMilInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxMileage';

    setInputValue( 'iddtmileage_date', x[k++] );
    setInputValue( id + 'Id', x[k++] );
    setInputValue( id + 'Value', x[k++] );
}

function getCinspInfo(phid) 
{
    execRequest( 'CinspInfo', "/index.php?action=printCinspInfo&id=" + phid, 'fillCinspInfo' );
}

function fillCinspInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewCinspection';

    setInputValue( 'iddtcinsp_date', x[k++] );
    setInputValue( id + 'CinspId', x[k++] );
    setInputValue( id + 'CinspPrice', x[k++] );
    setInputValue( id + 'CinspNote', x[k++] );
    setInputValue( id + 'CinspMileage', x[k++] );
    setInputValue( id + 'CinspPlace', x[k++] );
}

function getInspInfo(phid) 
{
    execRequest( 'InspInfo', "/index.php?action=printInspInfo&id=" + phid, 'fillInspInfo' );
}

function fillInspInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewInspection';

    setInputValue( 'iddtinsp_date', x[k++] );
    setInputValue( id + 'InspId', x[k++] );
    setInputValue( id + 'InspPrice', x[k++] );
    setInputValue( id + 'InspNote', x[k++] );
}

function getInsInfo(phid) 
{
    execRequest( 'InsInfo', "/index.php?action=printInsInfo&id=" + phid, 'fillInsInfo' );
}

function fillInsInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewInsurance';

    setInputValue( id + 'InsId', x[k++] );
    setInputValue( 'idInsurancePartTypeField', x[k++] );
    setInputValue( 'iddtin_date', x[k++] );
    setInputValue( id + 'InsPeriod', x[k++] );
    setInputValue( id + 'InsPrice', x[k++] );
    setInputValue( id + 'InsNote', x[k++] );
    setSelectValue( id + 'Insurers', x[k] );
}

function getVigInfo(phid) 
{
    execRequest( 'VigInfo', "/index.php?action=printVigInfo&id=" + phid, 'fillVigInfo' );
}

function fillVigInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewVignette';

    setInputValue( 'iddtvig_date', x[k++] );
    setInputValue( id + 'VigId', x[k++] );
    setInputValue( id + 'Period', x[k++] );
    setInputValue( id + 'Price', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function getAccessInfo(phid) 
{
    execRequest( 'AccessInfo', "/index.php?action=printAccessInfo&id=" + phid, 'fillAccessInfo' );
}

function fillAccessInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewAccessories';

    setInputValue( 'iddtaccess_date', x[k++] );
    setInputValue( id + 'Id', x[k++] );
    setInputValue( id + 'Where', x[k++] );
    setInputValue( id + 'What', x[k++] );
    setInputValue( id + 'Price', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function getRepInfo(phid) 
{
    execRequest( 'RepInfo', "/index.php?action=printRepInfo&id=" + phid, 'fillRepInfo' );
}

function fillRepInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewFreeRepair';

    setInputValue( 'iddtrep_date', x[k++] );
    setInputValue( id + 'Id', x[k++] );
    setInputValue( id + 'Where', x[k++] );
    setInputValue( id + 'What', x[k++] );
    setInputValue( id + 'Price', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function getLeaseInfo(phid) 
{
    execRequest( 'LeaseInfo', "/index.php?action=printLeaseInfo&id=" + phid, 'fillLeaseInfo' );
}

function fillLeaseInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxLease';
    
    setInputValue( id + 'Id', x[k++] );
    setInputValue( 'iddtlease_date', x[k++] );
    setInputValue( id + 'Payment', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function getTaxInfo(phid) 
{
    execRequest( 'TaxInfo', "/index.php?action=printTaxInfo&id=" + phid, 'fillTaxInfo' );
}

function fillTaxInfo( info )
{
    var x = info.split( "|" );
    var k = 0;
    var id = 'idBoxNewTax';

    setInputValue( 'iddttax_date', x[k++] );
    setInputValue( id + 'Id', x[k++] );
    setInputValue( id + 'Year', x[k++] );
    setInputValue( id + 'Price', x[k++] );
    setInputValue( id + 'Note', x[k++] );
}

function setInputValue( id, val )
{
    var x = document.getElementById( id );
    if ( x )
    {
        x.value = val;
    }
    else
       alert( "missing " + id );
}

function setSelectValue( id, val )
{
    var x = document.getElementById( id );
    if ( x )
    {
        var found = false;
        for( i=0; i<x.options.length; i++ )
            if ( x.options[i].value == val )
            {
                x.selectedIndex = i;
                found = true;
                break;
            }
            
        if ( ! found )
        {
            x.selectedIndex = x.options.length - 1;
        }
    }
}

function hideAllBoxes()
{
    hideBox( 'idBoxNewVignette' );
    hideBox( 'idBoxNewChange' );
    hideBox( 'idBoxNewInsurance' );
    hideBox( 'idBoxNewInspection' );
    hideBox( 'idBoxNewCinspection' );
    hideBox( 'idBoxFuel' );
    hideBox( 'idBoxNewTax' );
    hideBox( 'idBoxNewFreeRepair' );
    hideBox( 'idBoxMileage' );
    hideBox( 'idBoxLease' );
}

function loading( state )
{
    if ( state )
    {
        hideAllBoxes();
        showBoxW( 'idLoading', 100, 100);    
    }
    else
        hideBox( 'idLoading' );
}

function notChecked( id, cap )
{
    x = document.getElementById( id );
    if ( x )
    {
        if ( ! x.checked )
        {
             x.focus();
             alert( cap );
             return false;
        }
    }

    return true;
}

function notStrGreater( id, val, cap )
{
    x = document.getElementById( id );
    if ( x && x.value )
    {
        if ( x.value > val )
        {
            x.className = 'reqlogf';
            alert( cap );
            return false;
        }
    }

    return true;
}

function notEmpty( id, cap )
{
    x = document.getElementById( id );
    if ( x )
    {
        if ( (x.value == null) || (x.value.length < 1) )
        {
            x.className = 'reqlogf';
            alert( cap );
            return false;
        }
    }

    return true;
}

function initFileUploads() {
        var W3CDOM = (document.createElement && document.getElementsByTagName);
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
        var inp = document.createElement('input');
        inp.className = 'inpfile';
	fakeFileUpload.appendChild(inp);
	var image = document.createElement('img');
	image.src='/im/open_icon.gif';
        image.align='absmiddle';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

function changeTyres( id )
{
   setInputValue( 'idchtrid', id );
   if ( document.tyresChangeForm.onsubmit() )
   {
       document.tyresChangeForm.submit();
   }
}

function updateNotif( name )
{
    var val = 1;
    if ( $('input[name='+name+']').attr('checked') )
        val = 1;
    else
        val = 0;

    $.get( "index.php", { action: "updNotif", notif: name, val: val }, function(data) {
        if ( data != "OK" )
        {
            alert( 'Update failed. Please try again.' + data );
            $('input[name='+name+']').attr('checked', ! $('input[name='+name+']').attr('checked') );
        }
    } );
}

function updateAttrib( name )
{
    var val = 1;
    if ( $('input[name='+name+']').attr('checked') )
        val = 1;
    else
        val = 0;

    $.get( "index.php", { action: "updAttr", att: name, val: val }, function(data) {
        if ( data != "OK" )
        {
            alert( 'Update failed. Please try again.' );
            $('input[name='+name+']').attr('checked', ! $('input[name='+name+']').attr('checked') );
        }
    } );
}

function checkBet( msg )
{
    for( i=1; i<=10; i++ )
        $('#idBetP'+i).removeClass( 'error1' );

    for( i=1; i<=10; i++ )
      for( j=i+1; j<=10; j++ )
      {
          b1 = $('#idBetP'+i).val();
          b2 = $('#idBetP'+j).val();
          if ( (  b1 == b2 ) && ( b1 != 0 ) )
          {
             $('#idBetP'+i).addClass( 'error1' );
             $('#idBetP'+j).addClass( 'error1' );

             if ( msg.length > 0 )
                alert( msg );

             return false;
          }
      }

    return true;
}

function updateFrontIndexCars()
{
    document.getElementById( 'idFrontIndexCars' ).innerHTML = '<img src="/im/loading.gif" border=0 align=absmiddle/> Loading...';

    $.get( "/index.php", { action: "updIndexCars" }, function(data) {
        document.getElementById( 'idFrontIndexCars' ).innerHTML = data;
        } );
}

function checkFuelAmount( id, idtype, caption )
{
    var s = document.getElementById( id ).innerHTML;
    if ( s == '' )
        s = '0';
    var n = parseInt( s );    

    type = $('#'+idtype).val();

    if ( ( type == 'car' ) && ( n > 120 ) )
    {
        return confirm( caption );
    }

    if ( ( type == 'truck' ) && ( n > 300 ) )
    {
        return confirm( caption );
    }

    return true;
}

function checkMileage( id, idtype, caption )
{
    var s =  $( '#' + id ).val();
    if ( s == '' )
        s = '0';
    var n = parseInt( s );    

    type = $( '#' + idtype ).val();

    if ( ( type == 'car' ) && ( n > 400000 ) )
    {
        return confirm( caption );
    }

    if ( ( type == 'truck' ) && ( n > 800000 ) )
    {
        return confirm( caption );
    }

    return true;
}

function tryUpdateRouteMileage2( f )
{
    var m1 =  parseInt( $('#idRouteMileage1').val() );
    var d = parseInt( eval( 'routeDist' + $('#idRouteID').val() + ';' ) );

    $('#idRouteMileage2').val( m1 + d );
}

function doTrackChange( trk )
{
   if ( $('#idTrackChange').val() == trk )
   {
        $('#idTrackDt1').css( 'display', 'inline' );
        $('#idTrackDt2').css( 'display', 'inline' );
        $('#idTrackChanged').val(0);
   }
   else
   {
        $('#idTrackDt1').css( 'display', 'none' );
        $('#idTrackDt2').css( 'display', 'none' );
        $('#idTrackChanged').val(1);
   }
}

function updLastFuelType( id )
{
    var urlParam = "/index.php?action=lft&c=" + id ;
    $.ajax( { url : urlParam, success : function(data) {
                $('#idBoxFuelType').val( data );
        } } );
}

function getModels( bbID, mmID, act )
{
    var urlParam = "/index.php?action="+act+"&b=" + escape( $('#'+bbID).val() );
    $.ajax( { url : urlParam , success : function(data) {
        
        var x = data.split( '|' );
        var s = document.getElementById( mmID );

        s.options.length = 0;
        s.options[ 0 ] = new Option( "--select--", "" );
        for( i = 0; i < x.length; i++ )
            s.options[ i+1 ] = new Option( x[i], x[i] );

        if ( x.length == 1 )
        {
            s.selectedIndex = 1;
            if ( codeSingle.length > 0 )
                eval( codeSingle );
        }
   
        } } );
}

