

function change_model_image(value) {

    var pattern = /([a-zA-Z0-9]{3})$/;
            
    var re = new RegExp(pattern);
    var m = re.exec(value);
    value = m[1];
                
    makeRequest(3, value);
}
        
function change_model_type(car_id) {
    document.location.href='?carid='+car_id.substring(6);
}
        
function change_model_variant(modelid) {
    
    makeRequest(0, modelid);
          
}
        
function change_pren_time(time) {
    makeRequest(1, time);
}
        
function change_pren_miles(miles) {
    makeRequest(2, miles);
}
        
function add_option(option) {
    var elements = $$('#model_options input');
    for (var i = 0; i < elements.length; i++) {
	elements[i].disable();
    }
    makeRequest(4, option);
}
        
function add_kit(kit) {
    var elements = $$('#model_kits input');
    for (var i = 0; i < elements.length; i++) {
	elements[i].disable();
    }
    makeRequest(5, kit);

}

function add_wheels(wheel) {
    makeRequest(6, wheel);
}
                           
function makeRequest(taction, tvalue) {
    new Ajax.Request('liveCarConfig.php', {
        method: 'post',
        parameters: {
            action: taction,
            value: tvalue
        },
       
        
        onSuccess: function(transport) {
            
            var json = transport.responseText.evalJSON();
            //alert('' + taction);
            if ((taction == 0) || (taction == 1) || (taction ==2)) {

                $('modelvariant_info').update(json.modelinfo);
                $('modellvariant').update(json.models);
                $('CONTENT_monthcost_box_mid').update(json.costinfo);
                $('colour_info').update(json.colours);
                $('colordescription').update(json.colourinfo);
                $('model_kits').update(json.kits);
                $('model_options').update(json.options);
				$('model_wheels').update(json.wheels);
                $('short_costinfo').update(json.shortcostinfo);
                changeModelImage(json.colourinfo, json.modelname);
            
            } else if(taction == 3) {
                
                $('colour_info').update(json.colours);
                $('colordescription').update(json.colourinfo);
                $('CONTENT_monthcost_box_mid').update(json.costinfo);
                $('short_costinfo').update(json.shortcostinfo);
                changeModelImage(json.colourinfo, json.modelname);
    
            } else if(taction == 4) { //Add option
                $('CONTENT_monthcost_box_mid').update(json.costinfo);
                $('model_options').update(json.options);
                $('model_kits').update(json.kits);
				$('model_wheels').update(json.wheels);
                $('short_costinfo').update(json.shortcostinfo);

            } else if(taction == 5) { //Add kit
                $('CONTENT_monthcost_box_mid').update(json.costinfo);
                $('model_options').update(json.options);
                $('model_kits').update(json.kits);
				$('model_wheels').update(json.wheels);
                $('short_costinfo').update(json.shortcostinfo);
                
            } else if(taction == 6) { //Wheels
                $('CONTENT_monthcost_box_mid').update(json.costinfo);
                $('short_costinfo').update(json.shortcostinfo);
				
            }
        //$('ajax_progress').hide();
        }
                
    });
}

        
function changeModelImage(colourinfo, modelname) {
    
   
    var pattern = /\(([a-zA-Z0-9]{3})\)/;
    var re = new RegExp(pattern);
    var m = re.exec(colourinfo);
    

    var newimage = "grafik/" + modelname.toLowerCase() + "_" + m[1] + ".jpg";
    
    $('model_image').src = newimage;
    
    
    $('model_image').setAttribute('alt', modelname);
    $('c_' + m[1]).className="color_image_active";
    

}
        
function getCarDetails() {
    //$('infobox').down('div').update('');
    new Ajax.Request('carDetails.php', {
        method: 'post',
        onSuccess: function(transport) {
            var json = transport.responseText.evalJSON();
            $('infobox').down('div').update(json.cardetails);
        }
    });
}

function showCarInfo(modelid) {
    $('infobox').down('div').update('');
    lb = new lightbox();
    lb.initCallable('infobox');

    lb.activate();
    if (divelem[0].viewportOffset().top) {


        $('infobox').style.top =  (divelem[0].viewportOffset().top - 200) + 'px';

    }
    
}

function showCarDetails() {
    
    lb = new lightbox();
    lb.initCallable('infobox');
    lb.activate();

    var  divelem = $$('div.CONTENT_next_step_button');

    //alert(getOs(divelem[0]));
    getCarDetails();
    if (divelem[0].viewportOffset().top) {

        
        $('infobox').style.top =  (divelem[0].viewportOffset().top - 200) + 'px';

    }
    
}


function closeBox() {
    lb = new lightbox();
    lb.initCallable('infobox');
    lb.deactivate();
          
}

function showBoxInfo() {
    lb = new lightbox();
    lb.initCallable('infobox');
    lb.activate();
    if (divelem[0].viewportOffset().top) {


        $('infobox').style.top =  (divelem[0].viewportOffset().top - 200) + 'px';

    }


}

function remember(obj) {
    var name  = obj.getAttribute('name');
    var value = obj.value;
    var image = new Image();
    image.src = 'remember.php?name=' + escape(name) + '&amp;value=' + escape(value) + '&amp;rnd=' + new Date();
}


function oppna(url, width, height) {
  
    var fonster = null;
    var x = 0; var y = 0;
    if(parseInt(navigator.appVersion) > 3) {
        x = (screen.availWidth - 500) / 2;
        y = (screen.availHeight - 460) / 2;
    }
            
    x = screen.width;
    y = screen.height;
    if(parseInt(navigator.appVersion) > 3 || navigator.appName == 'Netscape') {
        if(!fonster || fonster.closed) {
            fonster = window.open(url,"subwin","top=" + parseInt(y/2-height/2-16) + ",left=" + parseInt(x/2-width/2-5) + ",width=" + width + ",location,scrollbars,height=" + height);
            fonster.location.href = url;
            fonster.focus();
        } else {
            fonster.location.href = url;
            fonster.focus();
        }
    }else{
        location.href = url;
    }
}

function changeCustomerType(obj) {

    switch(obj.getAttribute('value')) {
        case 'FP':
            ($('first_name_row').childElements())[1].update('F&ouml;rnamn:*');
            //$('last_name_row').show();
            ($('last_name_row').childElements())[1].update('Efternamn:*');
            ($('person_nr_row').childElements())[1].update('Personnr:*');
            $('villkorlink').setAttribute('href', 'TFS_villkor_kon.pdf');
            
            for (var i = 1; i < 6; i++) {
                //name = ;
                $('Employerinfo' + i).show();
            }
            break;

        case 'AB':
            ($('first_name_row').childElements())[1].update('F&ouml;retagsnamn:*');
            ($('last_name_row').childElements())[1].update('Kontaktperson:*');
            ($('person_nr_row').childElements())[1].update('Org. nr:*');
            $('villkorlink').setAttribute('href', 'TFS_villkor_for.pdf');
            for (i = 1; i < 6; i++) {
                //name = ;
                $('Employerinfo' + i).hide();
            }
   
            break;
    }
}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

function submitOrder() {
    $('infobox').update('<h2>Behandlar Din Prenumeration</h2><p><img src="grafik/preloader.gif" width="31" height="31" alt="preloader" /></p><p>Var god v&auml;nta</p>');
    new Ajax.Request('step3Submit.php', {
        method: 'post',
        parameters: {},


        onSuccess: function(transport) {
            closeBox();
            //top.window.scrollTo(0,0);
            location.href = 'pren_step4.php#CONTENT_top';
        }
    });
}

function showInfo(kit) {

    var elem = $('a_kit' + kit).next('img');
    elem.setAttribute('src', 'grafik/close_info.gif');
    elem.onclick = closeLink;
    elem.setAttribute('alt', 'Stäng info');
    elem.setAttribute('title', 'Stäng info');


    elem = $('a_kitinfo' + kit);

    var label = $('a_kit' + kit).next('label');

    var elem2 = new Element('td', {
        'class': 'kit_infotext'
    });
    var elem3 = new Element('span').update(label.getAttribute('title'));
    elem2.insert(elem3);
    elem.insert({
        after: new Element('tr').insert({
            bottom: elem2
        })
    });

    function closeLink() {
        closeInfo(kit);
    }


}

function closeInfo(kit) {
    var elem = $('a_kitinfo' + kit);
    var image = $('a_kit' + kit).next('img');
    image.setAttribute('src', 'grafik/open_info.gif');
    image.onclick = openLink;
    image.setAttribute('alt', 'Mer info');
    image.setAttribute('title', 'Mer info');

    elem.next().remove();

    function openLink() {
        showInfo(kit);
    }
}

Event.observe(window, 'load', function () {
    if ($('step2form') != null) {
        var obj = Form.getInputs('step2form','radio','custtype').find(function(radio) {
            return radio.checked;
        });
        changeCustomerType(obj);
        

        if ($('infobox').down('p').innerHTML.length > 0) {
            lb = new lightbox();
            lb.initCallable('infobox');
            lb.activate();
        }

    }

    externalLinks();

    $$('#top_menu a').each(function(link) {
		Event.observe(link, 'mouseover', showBox);
	});

	$$('#cars_holder a').each(function(link) {
		Event.observe(link, 'mouseout', hideAllBoxes);
	});
    
});

function showHeight() {
    alert(document.body.scrollHeight);
}


function hideAllBoxes() {
	$$('#cars_holder li').each(function(element) {
		element.hide();
	});
}

function showBox(event) {
	var e = Event.element(event);
	hideAllBoxes();
	box = e.id.replace('link_', 'box_');
	if (box) {
		$(box).up().show();
	}
}


