// Create ActiveXObject
function getHTTPObject() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}

var xmlHttp = getHTTPObject();
var params = "";

var baseAddr = null;

if( document.getElementsByTagName ) {
    var elems = document.getElementsByTagName( 'base' );

    if( elems.length ) {
	baseAddr = elems[ 0 ].href;
    }
}


function getPrinterTypes(brand_id) {
   
		xmlHttp.open("POST", baseAddr + "ajax/get_printer_types/" + brand_id, true);
        

		xmlHttp.onreadystatechange=function()
		  {

		    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			    document.getElementById('printerchoice_typebox').innerHTML=xmlHttp.responseText;
            }
		
		}
		
		xmlHttp.send(null);
}


function getPrinters(category_id){
    
		xmlHttp.open("POST", baseAddr + "ajax/get_printers/" + category_id, true);
        
		xmlHttp.onreadystatechange=function()
		  {

		    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			    document.getElementById('printerchoice_printerbox').innerHTML=xmlHttp.responseText;
		    }
		    else {
			    document.getElementById('printerchoice_printerbox').innerHTML="L&auml;ser in skrivare...";
		    }
		
		}
		
		xmlHttp.send(null);
}

function toggleVat(value) {
    var location_href = window.location.href;;
    xmlHttp.open("POST", baseAddr + "ajax/toggle_vat/" + value, true);
    
    xmlHttp.onreadystatechange=function()
    {

        if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            window.location.href = location_href;
        }
    
    }
    
    xmlHttp.send(null);
}


function search_ajax(search_string) {

    if(search_string.length < 4) {
        document.getElementById('search_results').innerHTML = "";    
        document.getElementById('search_results').style.paddingBottom = 0;    
        return;
    }
    
    var params = "search_string=" + search_string;

    xmlHttp.open("POST", baseAddr + "search/ajax/", true);

    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");


    xmlHttp.onreadystatechange=function()
    {

        if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            var response = xmlHttp.responseText;

            document.getElementById('search_results').innerHTML = response;
            document.getElementById('search_results').style.paddingBottom = "2em";    


        } else {
            document.getElementById('search_results').innerHTML = "Vänta lite...";
        }
    }
    
    xmlHttp.send(params);
    
}

var account = new function() {
    
    this.klarna_send_invoice = function(order_id) {
        xmlHttp.open("POST", baseAddr + "account/klarna_send_invoice/" + order_id, true);
        
            
        xmlHttp.onreadystatechange=function()
        {

            if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                var response = xmlHttp.responseText;
                if(response == 1) {
                    alert("Fakturan har nu skickats till din e-postadress!");
                
                } else {
                    alert("Ett fel uppstod när fakturan skulle skickas till din e-postadress. Var god kontakta vår kundtjänst!");
                }
            }
        
        }
        
        xmlHttp.send(null);
        
    }
    
    this.delete_printer = function(printer_id) {
            if(confirm("Är du säker på att du vill ta bort denna skrivare från listan?")) {
            xmlHttp.open("POST", baseAddr + "account/delete_printer/" + printer_id, true);
            
                
            xmlHttp.onreadystatechange=function()
            {

                if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    location.reload("true");    
                }
            
            }
            
            xmlHttp.send(null);
        }
    }
    
}


var cart = new function() {
    var cart_functions = this;


    /*
     * Increase quantity of this product in cart
     * @param string article_number
     */
    this.increase_product_quantity = function(article_number) {
        var xmlHttpIncrease = getHTTPObject();
        xmlHttpIncrease.open("POST",'ajax/cart_increase_product_quantity/' + article_number, true);
        xmlHttpIncrease.onreadystatechange=function()
        {
            if(xmlHttpIncrease.readyState == 4 && xmlHttpIncrease.status == 200) {
                var response = xmlHttpIncrease.responseText;
            
                if(!response) { 
                    alert("Ett fel har uppstått. Ladda om sidan och försök igen! Om problemet kvarstår, var god kontakta vår kundtjänst!");
                } else {    
                    document.getElementById('cart_container').innerHTML = response;
                }
            }	
        }
        xmlHttpIncrease.send(null);
    }
    
    /*
     * Increase quantity of this product in cart
     * @param string article_number
     */
    this.decrease_product_quantity = function(article_number) {
        var xmlHttpDecrease = getHTTPObject();
        xmlHttpDecrease.open("POST",'ajax/cart_decrease_product_quantity/' + article_number, true);
        xmlHttpDecrease.onreadystatechange=function()
        {
            if(xmlHttpDecrease.readyState == 4 && xmlHttpDecrease.status == 200) {
                var response = xmlHttpDecrease.responseText;
            
                if(!response) { 
                    alert("Ett fel har uppstått. Ladda om sidan och försök igen! Om problemet kvarstår, var god kontakta vår kundtjänst!");
                } else {    
                    document.getElementById('cart_container').innerHTML = response;
                }
            }
        }
        xmlHttpDecrease.send(null);
    }
    
    
    this.getTotal = function() {
        xmlHttp.open("POST", baseAddr + 'ajax/cart_get_total_price', true);
        xmlHttp.onreadystatechange=function() {
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            var response = xmlHttp.responseText;
                document.getElementById('basketTotal').innerHTML = response;
            }
        }
        xmlHttp.send(null);
    }

    this.addProduct = function(article_number, category_id) {
        
        xmlHttp.open("POST", baseAddr + 'ajax/cart_add_product/' + article_number + "/" + category_id, true);
        
        xmlHttp.onreadystatechange=function() {

            if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            
                var response = xmlHttp.responseText;
              
                if(!response) { 
                    alert("Ett fel har uppstått. Varan kunde inte läggas in i kundvagnen. Ladda om sidan och försök igen! Om problemet kvarstår, var god kontakta vår kundtjänst!");
                } else {    
                    document.getElementById('cart_container').innerHTML = response;
                }
            }
        }
        xmlHttp.send(null);
    }
    




}


