

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();
    
	sb_windowTools.centerElementOnScreen(document.getElementById('infobox'));
    
}

function showCarDetails() {
    
    lb = new lightbox();
    lb.initCallable('infobox');
    lb.activate();

    var  divelem = $$('div.CONTENT_next_step_button');

    //alert(getOs(divelem[0]));
    getCarDetails();
    
	sb_windowTools.centerElementOnScreen(document.getElementById('infobox'));
    
}


function closeBox() {
    lb = new lightbox();
    lb.initCallable('infobox');
    lb.deactivate();
          
}

function showBoxInfo() {
    lb = new lightbox();
    lb.initCallable('infobox');
    lb.activate();

	 sb_windowTools.centerElementOnScreen(document.getElementById('infobox'));




}

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 'EF':	
        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();
	}
}


if (!sb_windowTools) {var sb_windowTools = new Object();};

sb_windowTools = {
    scrollBarPadding: 17, // padding to assume for scroll bars

    // EXAMPLE METHODS

    // center an element in the viewport
    centerElementOnScreen: function(element) {
        var pageDimensions = this.updateDimensions();
        element.style.top = ((this.pageDimensions.verticalOffset() + this.pageDimensions.windowHeight() / 2) - (this.scrollBarPadding + element.offsetHeight / 2)) + 'px';
        element.style.left = ((this.pageDimensions.windowWidth() / 2) - (this.scrollBarPadding + element.offsetWidth / 2)) + 'px';
        element.style.position = 'absolute';
    },

    // INFORMATION GETTERS

    // load the page size, view port position and vertical scroll offset
    updateDimensions: function() {
        this.updatePageSize();
        this.updateWindowSize();
        this.updateScrollOffset();
    },

    // load page size information
    updatePageSize: function() {
        // document dimensions
        var viewportWidth, viewportHeight;
        if (window.innerHeight && window.scrollMaxY) {
            viewportWidth = document.body.scrollWidth;
            viewportHeight = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) {
            // all but explorer mac
            viewportWidth = document.body.scrollWidth;
            viewportHeight = document.body.scrollHeight;
        } else {
            // explorer mac...would also work in explorer 6 strict, mozilla and safari
            viewportWidth = document.body.offsetWidth;
            viewportHeight = document.body.offsetHeight;
        };
        this.pageSize = {
            viewportWidth: viewportWidth,
            viewportHeight: viewportHeight
        };
    },

    // load window size information
    updateWindowSize: function() {
        // view port dimensions
        var windowWidth, windowHeight;
        if (self.innerHeight) {
            // all except explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            // explorer 6 strict mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) {
            // other explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        };
        this.windowSize = {
            windowWidth: windowWidth,
            windowHeight: windowHeight
        };
    },

    // load scroll offset information
    updateScrollOffset: function() {
        // viewport vertical scroll offset
        var horizontalOffset, verticalOffset;
        if (self.pageYOffset) {
            horizontalOffset = self.pageXOffset;
            verticalOffset = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            // Explorer 6 Strict
            horizontalOffset = document.documentElement.scrollLeft;
            verticalOffset = document.documentElement.scrollTop;
        } else if (document.body) {
            // all other Explorers
            horizontalOffset = document.body.scrollLeft;
            verticalOffset = document.body.scrollTop;
        };
        this.scrollOffset = {
            horizontalOffset: horizontalOffset,
            verticalOffset: verticalOffset
        };
    },

    // INFORMATION CONTAINERS

    // raw data containers
    pageSize: {},
    windowSize: {},
    scrollOffset: {},

    // combined dimensions object with bounding logic
    pageDimensions: {
        pageWidth: function() {
            return sb_windowTools.pageSize.viewportWidth > sb_windowTools.windowSize.windowWidth ?
                sb_windowTools.pageSize.viewportWidth :
                sb_windowTools.windowSize.windowWidth;
        },
        pageHeight: function() {
            return sb_windowTools.pageSize.viewportHeight > sb_windowTools.windowSize.windowHeight ?
                sb_windowTools.pageSize.viewportHeight :
                sb_windowTools.windowSize.windowHeight;
        },
        windowWidth: function() {
            return sb_windowTools.windowSize.windowWidth;
        },
        windowHeight: function() {
            return sb_windowTools.windowSize.windowHeight;
        },
        horizontalOffset: function() {
            return sb_windowTools.scrollOffset.horizontalOffset;
        },
        verticalOffset: function() {
            return sb_windowTools.scrollOffset.verticalOffset;
        }
    }
};


/*
window.onscroll = function() {
    // update the scroll information
    sb_windowTools.updateScrollOffset();
    // update the vertical position of the element
    var element = document.getElementById('infobox');
    element.style.top = ((sb_windowTools.pageDimensions.verticalOffset() + sb_windowTools.pageDimensions.windowHeight() / 2) - (sb_windowTools.scrollBarPadding + element.offsetHeight / 2)) + 'px';
};
*/

window.onresize = function() {
	sb_windowTools.centerElementOnScreen(document.getElementById('infobox'));
};


