var pageId = read_cookie('pageid');
var positionId = read_cookie('positionid');
var pageType = '1';
var quantity = '1';
var comments = '';
var itemToAdd = '';
var addId = '';
function AddSelectedProduct(productId,productType)
{
	itemToAdd = productId;
	comments = '&lt;options&gt;'
	comments += '&lt;category&gt;'
	comments += getcategory(productType);
	comments += '&lt;/category&gt;'
	comments += '&lt;/options&gt;';
	
	if(productType != 'paymentProduct')
	{
		if(read_cookie(productType))
		{
			var action = 'remove';
			var prodToRemove = read_cookie(productType);
			if(prodToRemove != productId)
			{
				var soapBody = new SOAPObject("processBasket");
				soapBody.ns = "http://tempuri.org/";
				soapBody.appendChild(new SOAPObject("pageId")).val(pageId);
				soapBody.appendChild(new SOAPObject("positionId")).val(positionId);
				soapBody.appendChild(new SOAPObject("pageType")).val(pageType);
				soapBody.appendChild(new SOAPObject("productId")).val(prodToRemove);
				soapBody.appendChild(new SOAPObject("quantity")).val(quantity);
				soapBody.appendChild(new SOAPObject("comments")).val(comments);
				soapBody.appendChild(new SOAPObject("action")).val(action);
				soapBody.appendChild(new SOAPObject("option")).val(""); 
				var sr = new SOAPRequest("http://tempuri.org/processBasket", soapBody); //Request is now ready to be sent to a web-service
				sr.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
				sr.addNamespace("xsd","http://www.w3.org/2001/XMLSchema");
				SOAPClient.Proxy = "/designtorget/editor/soap/ecbusiness.asmx"; //Specify web-service address (if local to your domain) or a proxy file
				SOAPClient.SOAPServer = "/designtorget/editor/soap/ecbusiness.asmx";
				SOAPClient.SendRequest(sr,processAddResponse); //Send request to server and assign callback function
				//createordercookie(productType,productId);
				create_cookie(productType,productId);
				showUpdatingWindow();
			}
		}
		else
		{
			var action = 'add';
			var soapBody = new SOAPObject("processBasket");
			soapBody.ns = "http://tempuri.org/";
			soapBody.appendChild(new SOAPObject("pageId")).val(pageId);
			soapBody.appendChild(new SOAPObject("positionId")).val(positionId);
			soapBody.appendChild(new SOAPObject("pageType")).val(pageType);
			soapBody.appendChild(new SOAPObject("productId")).val(productId);
			soapBody.appendChild(new SOAPObject("quantity")).val(quantity);
			soapBody.appendChild(new SOAPObject("comments")).val(comments);
			soapBody.appendChild(new SOAPObject("action")).val(action);
			soapBody.appendChild(new SOAPObject("option")).val(""); 
			var sr = new SOAPRequest("http://tempuri.org/processBasket", soapBody); //Request is now ready to be sent to a web-service
			sr.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
			sr.addNamespace("xsd","http://www.w3.org/2001/XMLSchema");
			SOAPClient.Proxy = "/designtorget/editor/soap/ecbusiness.asmx"; //Specify web-service address (if local to your domain) or a proxy file
			SOAPClient.SOAPServer = "/designtorget/editor/soap/ecbusiness.asmx";
			SOAPClient.SendRequest(sr,processResponse); //Send request to server and assign callback function
			//createordercookie(productType,productId);
			create_cookie(productType,productId);
			showUpdatingWindow();
		}
	
    }
    else
    {
		AddPaymentToBasket(productType);
		
    }
	
}
function processResponse(respObj)
{
	try 
	{
		var responseMessage = respObj.Body[0].processBasketResponse[0].processBasketResult[0].root[0];
		var htmlData = responseMessage.htmldata[0].Text;
		largeCartStart = htmlData.indexOf('<LARGECART>');
		largeCartEnd = htmlData.indexOf('</LARGECART>');
		smallCartStart = htmlData.indexOf('<SMALLCART>');
		smallCartEnd = htmlData.indexOf('</SMALLCART>'); 	
		document.getElementById("large_cart").innerHTML = htmlData.substring(largeCartStart,largeCartEnd); 
		document.getElementById("small_cart_container").innerHTML = htmlData.substring(smallCartStart,smallCartEnd);
		closeUpdatingWindow();

		/** to assign the free freight price in postpaket shipping method **/
		var url = document.location.href;
		if(url.indexOf('/steg2/')>-1)
		 {
		    if(document.getElementById("postpaket").checked)
			document.getElementById("postpaket_price").innerHTML=trim(document.getElementById("frieghtPrice").innerHTML).replace('kr','');
 		
		 }


    } 
    catch(e) 
    { 
            alert(e); 
    }
 document.getElementById("shippingLabel").innerHTML = read_cookie('shippingOption');
}
function processAddResponse(respObj)
{
	try 
	{
		var responseMessage = respObj.Body[0].processBasketResponse[0].processBasketResult[0].root[0];
		var htmlData = responseMessage.htmldata[0].Text;
		largeCartStart = htmlData.indexOf('<LARGECART>');
		if(largeCartStart > 0)
		{
			var action = 'add';
			var soapBody = new SOAPObject("processBasket");
			soapBody.ns = "http://tempuri.org/";
			soapBody.appendChild(new SOAPObject("pageId")).val(pageId);
			soapBody.appendChild(new SOAPObject("positionId")).val(positionId);
			soapBody.appendChild(new SOAPObject("pageType")).val(pageType);
			soapBody.appendChild(new SOAPObject("productId")).val(itemToAdd);
			soapBody.appendChild(new SOAPObject("quantity")).val(quantity);
			soapBody.appendChild(new SOAPObject("comments")).val(comments);
			soapBody.appendChild(new SOAPObject("action")).val(action);
			soapBody.appendChild(new SOAPObject("option")).val(""); 
			
			var sr = new SOAPRequest("http://tempuri.org/processBasket", soapBody); //Request is now ready to be sent to a web-service
			sr.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
			sr.addNamespace("xsd","http://www.w3.org/2001/XMLSchema");
			
			SOAPClient.Proxy = "/designtorget/editor/soap/ecbusiness.asmx"; //Specify web-service address (if local to your domain) or a proxy file
			SOAPClient.SOAPServer = "/designtorget/editor/soap/ecbusiness.asmx";

			SOAPClient.SendRequest(sr,processResponse);
		}

			
    } 
    catch(e) 
    { 
            alert(e); 
    }
}

//to add or remove product script start

function AddPaymentToBasket(productType)
{
	
		
	var action = 'add';
	comments = '';

	if(document.getElementById('postoffice').checked || document.getElementById('Faktura').checked)
	{
		action = 'add';
		var addedPayment = read_cookie(productType);
		createordercookie(productType,itemToAdd);
		//alert(addedPayment+":"+itemToAdd);
	}


	

	else
	{
		action = 'remove';
		if(document.getElementById('creditcard').checked)
		{
			createordercookie(productType,'creditcard');
		}
		if(document.getElementById('directbanking').checked)
		{
			createordercookie(productType,'directbanking');
		}
	}
	
	if(addedPayment != itemToAdd)
	{
		//alert("xxxx");
		showUpdatingWindow();
		var soapBody = new SOAPObject("processBasket");
		soapBody.ns = "http://tempuri.org/";
		soapBody.appendChild(new SOAPObject("pageId")).val(pageId);
		soapBody.appendChild(new SOAPObject("positionId")).val(positionId);
		soapBody.appendChild(new SOAPObject("pageType")).val(pageType);
		soapBody.appendChild(new SOAPObject("productId")).val(itemToAdd);
		soapBody.appendChild(new SOAPObject("quantity")).val(quantity);
		soapBody.appendChild(new SOAPObject("comments")).val(comments);
		soapBody.appendChild(new SOAPObject("action")).val(action);
		soapBody.appendChild(new SOAPObject("option")).val(""); 
		var sr = new SOAPRequest("http://tempuri.org/processBasket", soapBody); //Request is now ready to be sent to a web-service
		sr.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
		sr.addNamespace("xsd","http://www.w3.org/2001/XMLSchema");
		SOAPClient.Proxy = "/designtorget/editor/soap/ecbusiness.asmx"; //Specify web-service address (if local to your domain) or a proxy file
		SOAPClient.SOAPServer = "/designtorget/editor/soap/ecbusiness.asmx";
		SOAPClient.SendRequest(sr,processResponse);
	}

}
//to add or remove product script end

//to add and remove product script start

function AddRemovePaymentProduct(producttoadd,producttoremove)
                        {
                                  var productId = producttoremove; 
                                  addId = producttoadd;
                                  var comments = '';
                                  var action = 'remove';  
                                  var soapBody = new SOAPObject("processBasket");
                                  soapBody.ns = "http://tempuri.org/";
                                  soapBody.appendChild(new SOAPObject("pageId")).val(pageId);
                                  soapBody.appendChild(new SOAPObject("positionId")).val(positionId);
                                  soapBody.appendChild(new SOAPObject("pageType")).val(pageType);
                                  soapBody.appendChild(new SOAPObject("productId")).val(productId);
                                  soapBody.appendChild(new SOAPObject("quantity")).val(quantity);
                                  soapBody.appendChild(new SOAPObject("comments")).val(comments);
                                  soapBody.appendChild(new SOAPObject("action")).val(action);
                                  soapBody.appendChild(new SOAPObject("option")).val(""); 
                                  var sr = new SOAPRequest("http://tempuri.org/processBasket", soapBody); //Request is now ready to be sent to a web-service
                                  sr.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
                                  sr.addNamespace("xsd","http://www.w3.org/2001/XMLSchema");
                                  SOAPClient.Proxy = "/designtorget/editor/soap/ecbusiness.asmx"; //Specify web-service address (if local to your domain) or a proxy file
                                  SOAPClient.SOAPServer = "/designtorget/editor/soap/ecbusiness.asmx";
                                  SOAPClient.SendRequest(sr,processResponse1); //Send request to server and assign callback function
                                  
                         }

                       function processResponse1(orderRespObj)
                       {  
                                  var productId = addId; 
                                  var comments = '';
                                  var action = 'add';  
                                  var soapBody = new SOAPObject("processBasket");
                                  soapBody.ns = "http://tempuri.org/";
                                  soapBody.appendChild(new SOAPObject("pageId")).val(pageId);
                                  soapBody.appendChild(new SOAPObject("positionId")).val(positionId);
                                  soapBody.appendChild(new SOAPObject("pageType")).val(pageType);
                                  soapBody.appendChild(new SOAPObject("productId")).val(productId);
                                  soapBody.appendChild(new SOAPObject("quantity")).val(quantity);
                                  soapBody.appendChild(new SOAPObject("comments")).val(comments);
                                  soapBody.appendChild(new SOAPObject("action")).val(action);
                                  soapBody.appendChild(new SOAPObject("option")).val(""); 
                                  var sr = new SOAPRequest("http://tempuri.org/processBasket", soapBody); //Request is now ready to be sent to a web-service
                                  sr.addNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance");
                                  sr.addNamespace("xsd","http://www.w3.org/2001/XMLSchema");
                                  SOAPClient.Proxy = "/designtorget/editor/soap/ecbusiness.asmx"; //Specify web-service address (if local to your domain) or a proxy file
                                  SOAPClient.SOAPServer = "/designtorget/editor/soap/ecbusiness.asmx";
                                  SOAPClient.SendRequest(sr,processResponse2); //Send request to server and assign callback function

                       }
                       function processResponse2(respObj)
                        {
                              try 
                                    {
                                     var responseMessage = respObj.Body[0].processBasketResponse[0].processBasketResult[0].root[0];
                                     var htmlData = responseMessage.htmldata[0].Text;
                                     largeCartStart = htmlData.indexOf('<LARGECART>');
                                     largeCartEnd = htmlData.indexOf('</LARGECART>');
                                     smallCartStart = htmlData.indexOf('<SMALLCART>');
                                     smallCartEnd = htmlData.indexOf('</SMALLCART>');  
                                     document.getElementById("large_cart").innerHTML = htmlData.substring(largeCartStart,largeCartEnd); 
                                     document.getElementById("small_cart_container").innerHTML = htmlData.substring(smallCartStart,smallCartEnd);
                                     createordercookie("paymentProduct",addId);   
                                     closeUpdatingWindow();
                                    } 
                             catch(e) { alert(e); }
                         }

//to add and remove product script end

function getcategory(productType)
{
	if(productType)
	{
		if(productType == 'wrapper')
		{
			return 'Inslagning';
		}
		else if(productType == 'greeting')
		{
			return 'Gratulationskort';
		}
		else if(productType == 'shippingProduct')
		{
			return 'Frakt';
		}
		else
		{
			return '';
		}
	}
}
function productguid(){
	
	var productname = document.getElementById('firstproduct_sno').value;
	return productname;
}
function productquantity(){
	var productquantity = document.getElementById('firstproduct_quantity').value;
	return productquantity;
}
function createordercookie(name,value)
{
	checkandAddCookie(name,value)
}

function SetSelectedVoucher()
{
	var wrapper_product = read_cookie('wrapper');
	var greeting_product = read_cookie('greeting');
	var shipping_product = read_cookie('shippingProduct').toUpperCase();
	var payment_product = read_cookie('paymentProduct');
	var formelements = document.getElementsByTagName('input');
	for(var i = 0; i < formelements.length; i++)
	{
		if(formelements[i].type == 'radio')
		{
			if(formelements[i].getAttribute('prodid') != null)
			{
				if(formelements[i].getAttribute('prodid') == wrapper_product)
				{
					formelements[i].checked = 'true';
				}
				if(formelements[i].getAttribute('prodid') == greeting_product)
				{
					formelements[i].checked = 'true';
					document.getElementById('notes').disabled = false;
					document.getElementById('noteslabel').style.color = 'black';
				}
				if(formelements[i].getAttribute('prodid') == shipping_product)
				{
					formelements[i].checked = 'true';
				}
				if(formelements[i].getAttribute('prodid') == payment_product)
				{
					formelements[i].checked = 'true';
				}
			}
		}
	}
	
	/*if(payment_product == 'creditcard')
	{
		document.getElementById('creditcard').checked = 'true';
		document.getElementById('creditcard_details').style.display = '';
	}
	else
	{
		document.getElementById('creditcard_details').style.display = 'none';
	}
	if(payment_product == 'directbanking')
	{
		document.getElementById('directbanking').checked = 'true';
		document.getElementById('bankdetails').style.display = 'block';
	}
	if(payment_product == 'Faktura')
	{
		document.getElementById('Faktura').checked = 'true';
		document.getElementById('Faktura_details').style.display = 'block';
	}*/
	
}

function CreateOptions(obj)
{
	document.getElementById('careof').value = obj.id;
}

function restoreOption()
{
	var restore = document.getElementById('careof').value;
	if(restore.length > 0)
	document.getElementById(restore).checked = true;
	
}
function needRestore(checkname)
{
	var flag = false;
	var options = document.getElementById('careof').value;
	var selectedOption = options.split('|');
	for(var count = 0; count < selectedOption.length-1; count++)
	{
		if(selectedOption[count] == checkname)
		{
			flag = true;
			break;
		}
	}
	return flag;
}

function checkandAddCookie(name,value)
{
	var cookievalue = read_cookie(name);
	if (cookievalue != null && cookievalue != "")
	{
		createSecureCookie(name,value,-1);
	}
	createSecureCookie(name,value,1);
}

function clearShopCookie()
{
	createSecureCookie('wrapper','',-1);
	createSecureCookie('greeting','',-1);
	createSecureCookie('shippingProduct','',-1);
	createSecureCookie('paymentProduct','',-1);
}

function createSecureCookie(name,value,days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
