function bilde( id, preceId, lang, host ){
 var lAdd = '';
 if( typeof lang != 'undefined' && lang != '' ){
  lAdd += '/' + lang;
 }
 window.open( 'http://' + host + lAdd + '/bilde.php?id=' + id + '&prece_id=' + preceId, 'bilde', 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=660px,height=750px' );
 return false;
}

function apraksts( p, pid, lang, host ){
 var lAdd = '';
 if( typeof lang != 'undefined' && lang != '' ){
  lAdd += '/' + lang;
 }
 window.open( 'http://' + host + lAdd + '/apraksts?parent=' + p + '&parent_id=' + pid, 'apraksts', 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=660,height=550,top=50,left=200' );
 return false;
}

function skaidrojums( p, pid, lang, host ){
 var lAdd = '';
 if( typeof lang != 'undefined' && lang != '' ){
  lAdd += '/' + lang;
 }
 window.open( 'http://' + host + lAdd + '/skaidrojums?parent=' + p + '&parent_id=' + pid, 'apraksts', 'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=660,height=550,top=50,left=200' );
 return false;
}

function lizings( id, atl, p, komp, lang, host ){
 var lAdd = '';
 if( typeof lang != 'undefined' && lang != '' ){
  lAdd += '/' + lang;
 }
 if( p && komp ){
  window.open( 'http://' + host + lAdd + '/lizinga_kalkulators.php?p=' + p + '&k_id=' + komp, 'lizings', 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no,width=430px,height=360px' );
 }else{
  window.open( 'http://' + host + lAdd + '/lizinga_kalkulators.php?id=' + id + ( ( atl )? '&atl=1': '' ) + ( ( p )? '&p=' + p: '' ), 'lizings', 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no,width=430px,height=360px' );
 }
 return false;
}

function prece_description( id, lang, host ){
 var lAdd = '';
 if( typeof lang != 'undefined' && lang != '' ){
  lAdd += '/' + lang;
 }
 window.open( 'http://' + host + lAdd + '/prece/'+id,'prece_description','menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=620px,height=620px');
 return false;
}

function komponente_description( id, lang, host ){
 var lAdd = '';
 if( typeof lang != 'undefined' && lang != '' ){
  lAdd += '/' + lang;
 }
 window.open( 'http://' + host + lAdd + '/komponente/'+id,'komponente_description','menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=620px,height=620px');
 return false;
}

function doNothing(){}

var afterTime;
function makeTimeout( check ){
 if( ! check ){
  afterTime = function( func, time, ob ){
   return setTimeout( function(){ func( ob ); }, time );
  }
 }else{
  afterTime = setTimeout;
 }
}
setTimeout( makeTimeout, 0, true );
var noAfterTime = clearTimeout;

var ieMoveTime;
function ieFixPos( nod ){
 if( ! nod ) nod = this.fixBlock;
 if( ieMoveTime ){
  noAfterTime( ieMoveTime );
 }
 ieMoveTime = afterTime( ieMoveTop, 20, nod );
 return nod.top;
}

function makeEl(){
 if( document.getElementById ){
  return function( str, fr ){
   var par = (fr)? fr: window;
   return par.document.getElementById( str );
  }
 }else if(document.all){
  return function( str, fr ){
   var par = (fr)? fr: window;
   return par.document.all[ str ];
  }
 }else if( document.layers ){
  return function( str, fr ){
   var par = (fr)? fr: window;
   var el = par.document.layers[ str ];
   el.style = el;
   return el;
  }
 }else return doNothing;
}
var el = makeEl();

function radioVal( forma, name ){
 var elems = forma.elements[ name ];
 if( forma.elements ){
  var elems = forma.elements[ name ];
  if( elems ){
   if( elems.value ) return elems.value;
   var len = elems.length;
   for( var i = 0; i < len; ++i ){
    var box = elems[ i ];
    if( box.checked ){
     return box.value;
    }
   }
  }
 }
 return false;
}

function ieMoveTop( nod ){
 nod.style.top = ( document.documentElement.scrollTop + 10 ) + 'px';
}

top.cenas = new Object();

function format_number(summa){
 var repl = /\./;
 var summa = Math.round( summa * 100 ) / 100;
 summa += '';
 var point_pos = summa.indexOf( '.' );
 if( point_pos == -1 ){
  summa += ',00';
 }else{
  summa += '0';
  summa = summa.substring( 0, point_pos + 3 );
  summa = summa.replace( repl, ',' );
 }
 return summa;
}

function calc_cena(){
 var summa = 0;
 var uzcsumma = 0;
 var id = 0;
 var forma = document.forms[ 'kompl_items' ];
 for( name in top.cenas ){
  var cen   = top.cenas[ name ];
  var ids   = new Array();
  var typId = radioVal( forma, name );
  if( typId ){
   var prodId = radioVal( forma, name + '_' + typId );
   if( prodId ){
    if( prod = top.cenas[ name ][ 'types' ][ typId ][ 'products' ][ prodId ] ){
     summa += prod.price;
     uzcsumma += prod.upprice;
     if( top.cenas[ name ][ 'view' ] ){
      if( prod.swf ){
       komplSwf( prod.swf, prod.swf );
      }else if( prod.img ){
       komplImg( prod.img, '' );
      }
     }
    }
   }
  }
 }
 return { 'summa': summa, 'uzcsumma': uzcsumma };
}

var kompl_cena = 0;
var kompl_uzcena = 0;
function show_cena(){
 var prices = calc_cena();
 var cena = prices.summa;
 var uzcena = prices.uzcsumma;
 var atl = uzcena - cena;
 kompl_cena = cena * 100;
 kompl_uzcena = uzcena * 100;
 var lizcena = hansalizings( 1, 36, 25/12/100, cena );

 atl = format_number( atl );
 cena = format_number( cena );
 uzcena = format_number( uzcena );
 lizcena = format_number( lizcena );

 el( 'kompl_cena_cip' ).innerHTML = 'Ls ' + cena;
 el( 'kompl_cena_cip_box' ).innerHTML = 'Ls ' + cena;
//  el( 'kompl_atl_cip' ).innerHTML = '15% (Ls ' + atl + ')';
//  el( 'kompl_atl_cip_box' ).innerHTML = '15% (Ls ' + atl + ')';
//  el( 'kompl_uzcena_cip' ).innerHTML = 'Ls ' + uzcena;
//  el( 'kompl_uzcena_cip_box' ).innerHTML = 'Ls ' + uzcena;
//  el( 'kompl_cena_liz' ).innerHTML = ( kompl_cena > 10000 )? kompl_leasing_name + ' Ls ' + lizcena + ' ' + kompl_leasing_month: '';
//  el( 'kompl_cena_liz_box' ).innerHTML = ( kompl_cena > 10000 )? kompl_leasing_name + ' Ls ' + lizcena + ' ' + kompl_leasing_month: '';
}

function hansalizings( m, n, R, principal ){ //hansalizings( 1, 36, 25/12/100, 1000 )
 var Z = 1 / ( 1 + ( R / m ) );
 return ( ( 1 - Z ) * principal ) / ( Z * ( 1 - Math.pow( Z, n ) ) );
}

function click_type( nod ){
 var nam   = nod.name;
 var ind   = nam.lastIndexOf( '_' );
 var name  = nam.substring( 0, ind );
 var forma = document.forms[ 'kompl_items' ];
 var id    = nam.substring( ind + 1, nam.length );
 radioUncheck( forma, name );
 radioUncheckProduct( forma, name );
 radioCheckVal( forma, name, id );
 nod.checked = true;
 top.cenas[ name ][ 'types' ][ id ][ 'defProduct' ] = nod.value;
 show_cena();
}

function radioUncheckProduct( forma, name ){
 if( forma.elements ){
  var elems = forma.elements;
  var len = elems.length;
  for( var i = 0; i < len; ++i ){
   var box = elems[ i ];
   if( box.name.indexOf( name ) != -1 ){
    if( box.checked ){
     box.checked = false;
    }
   }
  }
 }
 return true;
}

function click_item( nod ){
 var forma = document.forms[ 'kompl_items' ];
 if( forma.elements ){
  var name = nod.name + '_' + nod.value;
  radioUncheckProduct( forma, nod.name );
  var elems = forma.elements[ name ];
  var defProduct = top.cenas[ nod.name ][ 'types' ][ nod.value ][ 'defProduct' ];
  var len = elems.length;
  for( var i = 0; i < len; ++i ){
   var box = elems[ i ];
   if( box.value == defProduct ){
    box.checked = true;
   }
  }
 }
 nod.checked = true;
 show_cena();
 return true;
}

function radioCheckVal( forma, name, val ){
 var elems = forma.elements[ name ];
 if( forma.elements ){
  var elems = forma.elements[ name ];
  if( elems ){
   if( elems.value == val ) return elems.checked = true;
   var len = elems.length;
   for( var i = 0; i < len; ++i ){
    var box = elems[ i ];
    if( box.value == val ){
     return box.checked = true;
    }
   }
  }
 }
 return false;
}

function radioUncheck( forma, name ){
 var elems = forma.elements[ name ];
 if( forma.elements ){
  var elems = forma.elements[ name ];
  if( elems ){
   if( elems.checked ) return elems.checked = false;
   var len = elems.length;
   for( var i = 0; i < len; ++i ){
    var box = elems[ i ];
    if( box.checked ){
     return box.checked = false;
    }
   }
  }
 }
 return true;
}

function showHideNode( nod ){
 if( nod ){
  if( nod.style ){
   if( nod.style.display ){
    if( nod.style.display == 'block' ){
     nod.style.display = 'none';
    }else{
     nod.style.display = 'block';
    }
   }
  }
 }
}

function checkDef(){
 var itemName, nr, len, elem;
 var forma = document.forms[ 'kompl_items' ];
 for( itemName in top.cenas ){
  var elems = forma.elements[ itemName ];
  if( elems.checked ){
   click_item( elems );
  }else{
   len = elems.length;
   for( nr = 0; nr < len; ++nr ){
    elem = elems[ nr ];
    if( elem.checked ){
     click_item( elem );
    }
   }
  }
 }
}

function getStyle( x, styleProp ){
 if( x.currentStyle ){
  var y = x.currentStyle[ styleProp ];
 }else if( window.getComputedStyle ){
  var y = document.defaultView.getComputedStyle( x, null ).getPropertyValue( styleProp );
 }
 return y;
}
