//script för kundvagn

function add_to_basket(productid)
{
	
	var pid = productid;
	var action = "add";
	var amount = 1;
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{	
			if(xmlhttp.status == 200)
			{
				
				var res;
				res = xmlhttp.responseText;
				location.reload();
			}

		}

	}	
	
	xmlhttp.open("GET","./basket.php?productid=" + productid + "&call=" + action + "&amount=1&dummy=" + new Date().getTime(),true);
	xmlhttp.send(null);
		

	/* Slut på AJAX-block */	

}

function increaseBasket(productid)
{

	var pid = productid;
	var action = "increase";
	var amount = document.getElementById("increase" + productid).value;
	
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}
	
	xmlhttp.onreadystatechange = function()
	{

		if(xmlhttp.readyState == 4)
		{
			location.reload();
		}
			
	};	
	
	xmlhttp.open("GET","./basket.php?productid=" + productid + "&call=" + action + "&amount=" + amount,true);
	xmlhttp.send(null);

	/* Slut på AJAX-block */	

}

function deleteFromBasket(productid)
{

	var pid = productid;
	var action = "delete";
	var amount = document.getElementById("delete" + productid).value;
	
	/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

	var xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new XMLHttpRequest();
			}
			catch(e)
			{
				alert("Couldn't create needed object");

			}
		}
	}

	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			var res = xmlhttp.responseText;
			
			if(res != "")
			{
				alert(res);
					
			}
			location.reload();
		}
	
	};	
	
	
	xmlhttp.open("GET","./basket.php?productid=" + productid + "&call=" + action + "&amount=" + amount,true);
	xmlhttp.send(null);
	

	/* Slut på AJAX-block */	

}

function send_payment_form(form,paymentmethod)
{
	
	var pnr_orgnr = form.pnr_orgnr.value;
	var fn = form.firstname.value;
	var ln = form.lastname.value;
	var address_1 = form.address_1.value;
	var address_2 = form.address_2.value;
	var pc = form.postalcode.value;
	var city = form.city.value;
	var country = form.country.value;
	var phone_home = form.phone_home.value;
	var phone_work = form.phone_work.value;
	var mobile = form.phone_mobile.value;
	var email = form.email.value;
	
	/*if(pnr_orgnr == "" || pnr_orgnr.length < 10)
	{
		alert("Var god fyll i ditt personnr eller orgnr");
		
		return false;
		
	}*/	
	if(fn == "")
	{
		alert("Var god fyll i ditt förnamn");
		
		return false;
		
	}
	else if(ln == "")
	{
		alert("Var god fyll i ditt efternamn");
		
		return false;
		
	}
	else if(address_1 == "")
	{
		alert("Var god fyll i din gatuadress");
		
		return false;
		
	}
	else if(pc == "")
	{
		alert("Var god fyll i ditt postnummer");
		
		return false;
	}
	else if(city == "")
	{
		alert("Var god fyll i din postort");
		
		return false;
		
	}
	else if(phone_home == "" && mobile == "")
	{
		alert("Var god fyll i telefonnummer eller mobilnummer");
		
		return false;
		
	}
	else if(email == "")
	{
		alert("Var god fyll i din e-mailadress");
		
		return false;
		
	}
	else
	{
		if(paymentmethod == "card")
		{	
			form.action = "./payment.php";
			form.payment_method.value = paymentmethod;
			form.submitted.value = "yes";
		
		}
		if(paymentmethod == "invoice")
		{
			form.action = "./svea_payment.php";
			form.payment_method.value = paymentmethod;
			form.submitted.value = "yes";
		}
		if(paymentmethod == "bank")
		{
			form.action = "./payment.php";
			form.payment_method.value = paymentmethod;
			form.submitted.value = "yes";
		}	
	
		form.submit();
		
	}
	
}

//Kontrollerar landskoden för att bestämma om kund kan beställa mot faktura

function checkCountryCode(cc)
{
	
	if(cc == "SE" || cc == "NO")
	{
	
		document.checkout_form.paymethodBtnFaktura.disabled = false;

	}
	else
	{
	
		document.checkout_form.paymethodBtnFaktura.disabled = true;
	}
}

//Funktion som lägger in mailadresser i databasen
function add_email_to_db()
{
	var form = document.getElementById("email_form");
	
	var name = form.name.value;
	var email = form.email.value;
	
	if(name == "")
	{
		alert("Var god fyll i ditt namn!");
		form.name.focus();
		return false;
	}
	else if(email == "")
	{
		alert("Var god fyll i din epostadress!");
		form.email.focus();
		return false;
	}
	else
	{
		/* Detta block gör ett AJAX-anrop till servern för att lägga till varan i kundvagnen */

		var xmlhttp = false;

		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					xmlhttp = new XMLHttpRequest();
				}
				catch(e)
				{
					alert("Couldn't create needed object");
	
				}
			}
		}
	
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4)
			{	
				if(xmlhttp.status == 200)
				{
				
					var res;
					res = xmlhttp.responseText;
					alert(res);
				}

			}

		}	
	
		xmlhttp.open("GET","./insert_email_to_db.php?name=" + name + "&email=" + email + "&dummy=" + new Date().getTime(),true);
		xmlhttp.send(null);
		

		/* Slut på AJAX-block */	
	}
}

function checkContactForm(form)
{
	
	var namn = form.textfield7.value;
	var adress = form.textfield22.value;
	var postadress = form.textfield32.value;
	var epost = form.textfield52.value;
	
	if(namn == "")
	{
		alert("Var god fyll i ditt namn");
		
		return false;
		
	}
	else if(adress == "")
	{	
		alert("Var god fyll i din adress");
		
		return false;
		
	}
	else if(postadress == "")
	{
		alert("Var god fyll i din postadress");
		
		return false;
	
	}
	else if(epost == "")
	{
		alert("Var god fyll i din e-postadress");
		
		return false;
		
	}
	else
	{
		return true;
		
	}
}
