var $global_item = new Object();
function bike(parts)
{
    this.bike = {};
    this.$weight = 0;
    this.$pounds = 0;
    this.$total = 0;
    this.null_item = {price:0,product_id:0,name:'null',weight:0,pweight:0,netPrice:0,cat:''};
    this.form = '';
    this.parts = parts;
    this.ajax = ''

    for(c=0;c<this.parts.length;c++)
    {
    this.bike[this.parts[c]] = this.null_item;

    }


    bike.prototype.display_basket = display_basket;
    bike.prototype.update_basket = update_basket;
    bike.prototype.calc_basket = calc_basket;
    bike.prototype.set = set;
    //bike.prototype.get_item = get_item;
        bike.prototype.get_item = ajaxGet;


}
function ajaxGet($idvar)
{
        ajaxFunction($idvar);
    //alert($idvar+ ' ' + $global_item.product_id);
    //return($global_item);
}

function set($name,$value)
{
    this[$name] = $value;
}



function update_basket(name,$id)
{



    this.get_item($id)//Uses ajax to return object representing the selected part

    this.bike[name] =  this.ajax

}

function calc_basket()
{
    this.$total = 0;
    this.$weight = 0;
    for (dz = 0;dz<this.parts.length;dz++)
    {

        eval('this.$total +=this.bike.'+parts[dz]+'.netPrice');
        eval('this.$weight += this.bike.'+parts[dz]+'.weight - 0');

        this.$pounds = this.$weight * 2.2;
        this.$total = round(this.$total,2);

        this.$weight = round(this.$weight,2);
        this.$pounds = round(this.$pounds,2);

    }

    //alert('Total' + this.$total)
}
function display_basket()
{

    if(this.$weight == Math.floor(this.$weight))
    {
        this.form.weight.value = this.$weight + ".00 Kg";
    }
    else
    {
        this.form.weight.value = this.$weight + " Kg";
    }
    if(this.$total == Math.floor(this.$total))
    {
        this.form.total.value = '£ '+ this.$total + ".00";

    }
    else
    {
        this.form.total.value = '£ ' + this.$total

    }
    if((this.$pounds - 0) == Math.floor(this.$pounds - 0))
    {
        this.form.pounds.value = "("+ round((this.$pounds - 0),2) + ".00 pounds)";
    }
    else
    {
        this.form.pounds.value =  "("+ round(this.$pounds,2) + " pounds)";
    }
    $credit = document.getElementById('credit')

    $monthly =  round(this.$total * 0.023275,2);
    $monthly = $monthly + '';
           if(($monthly.length - $monthly.indexOf('.')) == 2)
    {
            $monthly += '0';
    }
           if($monthly.indexOf('.') == -1)
    {
            $monthly += '.00';
    }

    $credit.innerHTML = $monthly



}

function get_item($id)
{

    for($c = 0;$c<parts.length;$c++)
        {
            //alert(parts[$c])
            $category = eval(parts[$c])//Create handle to select object in bike builder form

            for($p = 0;$p<$category.length;$p++ )
            {
                if ($category[$p].product_id == $id)
            {
                $category[$p].netPrice = ($category[$p].price - 0)*(1-($category[$p].pweight - 0)/100)
                //return($category[$p]);
                this.ajax = $category[$p];
                return;
            }
            }
        }
    //return(this.null_item);
    this.ajax = this.null_item
    return
}


function round($value,$digits)
{
    factor = Math.pow(10,$digits)
    return(Math.round($value*factor)/factor)
}
function doUpdateBasket($objItem)
{

    $objItem.form.edited.value = 'edited';
    basket.update_basket($objItem.name,$objItem.options[$objItem.selectedIndex].value);
    basket.calc_basket();
    basket.display_basket();
    $objItem.desc.value = $objItem.options[$objItem.selectedIndex].innerHTML;
    //$objItem.options[$objItem.selectedIndex].value = $objItem.$parts[$objItem.selectedIndex].product_id;
    $objItem.image.src = 'http://shop.devercycles.co.uk/images/prod_thu_'+$objItem.options[$objItem.selectedIndex].value+'.jpg';
    $objItem.link.href = 'http://shop.devercycles.co.uk/images/prod_'+$objItem.options[$objItem.selectedIndex].value+'.jpg'
    $objItem.detail.href = 'http://shop.devercycles.co.uk/index.html?action=detail&prodID='+ $objItem.options[$objItem.selectedIndex].value
}
function setHelp(e)
{
    if(!e){e = window.event}
    document.getElementById('text1').innerHTML = this.options[this.selectedIndex].innerHTML;
    mailhelp.showHelp('text1',e);


}


function ajaxFunction($id)
  {
  var xmlHttp;


  try
    {

            // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
          catch (e)
    {
    // Internet Explorer
            try
              {

                      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
              }
            catch (e)
              {
                try
                {

                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
                      catch (e)
                {
                    alert("Your browser does not support AJAX!");
                        return false;
            }
        }
    }

  xmlHttp.onreadystatechange= getProd
  function getProd()
  {
  var $_item = new Object()
  if(xmlHttp.readyState==4)
    {


             $text =  xmlHttp.responseText;
        $c = $text.split('|');
        $_item.price = $c[0];
        $_item.product_id = $c[1];
        $_item.weight = $c[2];
        $_item.name = $c[3];
        $_item.pweight = $c[4];
        $_item.cat = $c[5];
        $_item.netPrice = round(($_item.price - 0)*(1-($_item.pweight - 0)/100),2);


        basket.ajax = $_item;


    }

  }

    xmlHttp.open("GET","getproducts.php?cmd=bbpinfo&bbid="+$id,false);
    xmlHttp.send(null);

  var $_item = new Object()
  if(xmlHttp.readyState==4)
    {


             $text =  xmlHttp.responseText;
        $c = $text.split('|');
        $_item.price = $c[0];
        $_item.product_id = $c[1];
        $_item.weight = $c[2];
        $_item.name = $c[3];
        $_item.pweight = $c[4];
        $_item.cat = $c[5];
        $_item.netPrice = round(($_item.price - 0)*(1-($_item.pweight - 0)/100),2);


        basket.ajax = $_item;


    }

}