
function update_shipping(ID, ids) {
	
	$.ajax({
	   type: "POST",
	   cache: false,
	   url: "/index.php/ajax/updateshipping/",
	   data: "id="+ID,
	   success: function(msg){
		 splHTML = msg.split("|");
		 document.getElementById("fraktavgift").innerHTML = splHTML[0];
		 document.getElementById("input_fraktavgift").value = splHTML[1];
		 document.getElementById("input_fraktavgift_moms").value = splHTML[2];
		 
		 reset_choose_boxes(ids, "delivery");
		 
		 document.getElementById("delivery_"+ID).className = "choose_box_marked";
		 document.getElementById("delivery").value = splHTML[3];

		 calc_total_sum();
	   }
	 });
	
	

}

function update_payment(ID, ids) {
	$.ajax({
	   type: "POST",
	   cache: false,
	   url: "/index.php/ajax/updatepaymentinfo/",
	   data: "id="+ID,
	   success: function(msg){
		 
		 var splHTML = msg.split("|");
		 document.getElementById("betalavgift").innerHTML = splHTML[0];
		 document.getElementById("input_betalavgift").value = splHTML[1];
		 document.getElementById("input_betalavgift_moms").value = splHTML[2];
		 reset_choose_boxes(ids, "payment");
		 
		 document.getElementById("payment_"+ID).className = "choose_box_marked";
		 document.getElementById("payment").value = splHTML[3];
		 
		 calc_total_sum();
	   }
	 });

}

function reset_choose_boxes(boxes, type) {

	var splBox = boxes.split("|");

	for (i=0;i<splBox.length;i++) {
		
		if (splBox[i] != "") {

			document.getElementById(type+"_"+splBox[i]).className = "choose_box";
			
		}
		
	}
	
}



function add_to_cart(id,qty) {
	$.ajax({
	   	type: "POST",
		cache: false,
	   	url: "/index.php/cart/add/",
	   	data: "id="+id+"&qty="+qty, 
	   	success: function(msg){
		 document.getElementById("mini_cart").innerHTML = msg;
	   }
	 });
}

function clear_mini_cart() {
	$.ajax({
	   	type: "POST",
		cache: false,
	   	url: "/index.php/cart/clearminicart/",
	   	success: function(msg){
		 document.getElementById("mini_cart").innerHTML = msg;
	   }
	 });
}

function find_username() {
	$.ajax({
	   	type: "POST",
		cache: false,
	   	url: "/index.php/user/findusername/",
		data: "user="+document.getElementById("checkout_username").value, 
	   	success: function(msg){
		if(msg == "TRUE"){
			document.getElementById("password").style.display='table-row';
			document.getElementById("login_next").style.display='none';
			document.getElementById("login_submit").style.display='block';
		}else if(msg == "VALID"){
			location.href='/index.php/checkout/order'
		}else{
			alert("Inte giltig");	
		}
	   }
	 });
}

function checkout_login(){
	$.ajax({
	   	type: "POST",
		cache: false,
	   	url: "/index.php/user/logging_in/",
		data: "user="+document.getElementById("checkout_username").value+"&password="+document.getElementById("checkout_pwd").value, 
	   	success: function(msg){
		if(msg != "FALSE"){
			location.href='/index.php/checkout/order';
		}else{
			alert("Fel anvÃ¤ndarnamn eller lÃ¶senord.");	
		}
	   }
	 });
}

function login(){
	$.ajax({
	   	type: "POST",
		cache: false,
	   	url: "/index.php/user/logging_in/",
		data: "user="+document.getElementById("username").value+"&password="+document.getElementById("pwd").value, 
	   	success: function(msg){
		if(msg != "FALSE"){
			 document.getElementById("login_div").innerHTML = msg; 
		}else{
			alert("Fel anvÃ¤ndarnamn eller lÃ¶senord.");	
		}
	   }
	 });
}

function logout(){
	$.ajax({
	   	type: "POST",
		cache: false,
	   	url: "/index.php/user/logout/",
	   	success: function(msg){
		if(msg == "TRUE"){
			location.href="http://www.buskulbarn.se/";
			//document.getElementById("login_div").innerHTML = "<br/><br/><div class='heading-right'><h2>Logga in</h2></div><form action='/shop/index.php/user/login/' method='post' style='clear:both;'>E-post: <input type='text' name='username' id='username' /><br />L&ouml;senord: <input type='password' name='pwd' id='pwd' /><p/><input type='button' value='Logga in' onclick='javascript:login()' /></form>";
		}
	   }
	 });
}

function verify_order() {
    
    var error_msg = "";
    
    if (document.form_checkout.Fnamn.value == "") {
        error_msg += "Du måste fylla i ditt förnamn!\n";
    }
    
    if (document.form_checkout.Enamn.value == "") {
        error_msg += "Du måste fylla i ditt efternamn!\n";
    }
    
    if (document.form_checkout.Adress.value == "") {
        error_msg += "Du måste fylla i din adress!\n";
    }
    
    if (document.form_checkout.Postnr.value == "") {
        error_msg += "Du måste fylla i ditt postnummer!\n";
    }
    
    if (document.form_checkout.Postort.value == "") {
        error_msg += "Du måste fylla i din postort!\n";
    }
    
    if (document.form_checkout.Epost.value == "") {
        error_msg += "Du måste fylla i din e-postadress!\n";
    }
    
    if (document.form_checkout.new_user.value == 1 && document.form_checkout.Losenord.value == "") {
        error_msg += "Du måste fylla i ett lösenord!\n";
    }
    
    if (document.form_checkout.delivery.value == 0) {
        error_msg += "Du måste välja ett leveransmetod!\n";
    }
    
    if (document.form_checkout.payment.value == 0) {
        error_msg += "Du måste välja ett betalsätt!\n";
    }
    
    if (error_msg != "") {
        alert(error_msg);
        return false;
    } else {
        
        $.ajax({
            type: "POST",
            cache: false,
               url: "/index.php/ajax/verify_order/",
               success: function(msg){

                   if (msg != "") {
                        
                        document.getElementById('dialog').innerHTML = "Ditt antal av förjande varor överskrider lagerstatus:<p><table class='popup-checkout-table'><tr><td><strong>Produkt</strong></td><td><strong>Pris/st</strong></td><td><strong>Ditt önskade antal</strong></td><td><strong>Antal i lager</strong></td></tr>"+msg+"</table></p><br>Vi kontaktar dig via e-post/telefon om ny leveranstid.<br><br>Är du säker på att du vill genomföra ditt köp?";
                        $('#dialog').dialog('open');
                        
                        return false;
 
                    }
                    
                    
                    
               }
         });
         
         
        
    }

}

function check_epost(epost) {
    
    $.ajax({
        type: "POST",
        cache: false,
        url: "/index.php/user/findusername/",
        data: "user="+epost,   
        success: function(msg){
            if (msg == "TRUE") {
                
                var button = document.getElementById("button-new-member-dis");
                
                if (button == null) {
                    document.getElementById("button-new-member").disabled = true;
                    document.getElementById("button-new-member").id = "button-new-member-dis";
                }
                
                document.getElementById("epost-msg").style.display = "block";
                document.getElementById("epost").className = "input-text-marked";
                
            } else {
                document.getElementById("button-new-member-dis").disabled = false;
                document.getElementById("button-new-member-dis").id = "button-new-member";
                document.getElementById("epost-msg").style.display = "none";
                document.getElementById("epost").className = "input-text";
            }
        }
    });
    
}

