/*Here we will add the common JS function for Adding,Removing and Updating the product to Basket*/
function AddProductToBasket(productId,quantity,comments,name,price)
{
	/*var loc;
	if(comments.indexOf('|flash|')>-1)
	 {
		loc="flash";
		//alert(loc);
	 }

	else
	 {
		loc="";	
	 }*/

	var pageId = read_cookie('pageid');
	var positionId = read_cookie('positionid');
	var pageType = 2;
	var productId = productId;
	var quantity = quantity;
	var comments = GetCommentsInXMLFormat(comments);
	var action = "add";

	var startLocation = comments.indexOf('&lt;imageurl&gt;');
	var endLocation = comments.indexOf('&lt;/imageurl&gt;');
	var imageurl = comments.substring(startLocation+16,endLocation);

	var productName = quantity+"x"+name;

	AjaxUploader(imageurl,productName,price,"");
	showUpdatingWindow();

	var startLocationcat = comments.indexOf('&lt;category&gt;');
	var endLocationcat = comments.indexOf('&lt;/category&gt;');
	var shippingcat = comments.substring(startLocationcat+16,endLocationcat);

	if(shippingcat == 'Frakt'){
		create_cookie('fraktcookie',productId,1);
		create_cookie('shippingproductexist','yes',1);
	}

	if(productId != read_cookie('fraktcookie'))
		CallSoapReqForUpdatingBasket(pageId,positionId,pageType,productId,quantity,comments,action);

	
	
}
function RemoveProductFromBasket(productId,productCount)
{
	var remove = "remove";
	var comments = '';
	
	if(document.getElementById('remove_product_alert_'+productId))
		document.getElementById('remove_product_alert_'+productId).className = document.getElementById('remove_product_alert_'+productId).className.replace('show','hidden');
	if(productCount == 1)
	{
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,0,comments,'clear');
		create_cookie('shippingProduct','',-1);
		create_cookie('paymentProduct','',-1);
		create_cookie('greeting','',-1);
		create_cookie('wrapper','',-1);	
		create_cookie('voucherProductAdded','',-1);
		document.getElementById('large_cart').style.display = 'none';
		document.getElementById('teaser_area').style.display = 'none';
		document.getElementById('small_cart_hide').style.display = 'none';
		document.getElementById('small_cart_container').style.display = 'block';
		if(document.getElementById("order_button"))
			document.getElementById("order_button").style.display = 'block';
	}
	else
	{
	CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,0,comments,remove);
	IsWrapGreetDeleted(productId);   //Remove wrapper product
	}
}
function IncrementProductCount(productId,currentQty)
{

	document.getElementById('largecart_quantity').value = " ";
	document.getElementById('largecart_productid').value = " ";
	CallSoapReqForLogin(productId,(parseInt(currentQty)+1));
	//CallSoapForUpdatingCustomProperty(productId,currentQty,'increment');

}
function DecrementProductCount(productId,currentQty)
{
	if(currentQty == '1')
		document.getElementById('remove_product_alert_'+productId).className = document.getElementById('remove_product_alert_'+productId).className.replace('hidden','show');
	else
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,(parseInt(currentQty)-1),'decrement','update');
	//CallSoapForUpdatingCustomProperty(productId,currentQty,'decrement');
}
function UpdateProductCount(productId,currentQty)
{

	document.getElementById('largecart_quantity').value = " ";
	document.getElementById('largecart_productid').value = " ";
	if(currentQty!=""){
		if(currentQty=="0")
			//AjaxUploader("","","","largecart");
			document.getElementById('remove_product_alert_'+productId).className = document.getElementById('remove_product_alert_'+productId).className.replace('hidden','show');
		else{
			
			//CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,currentQty,'','update');
			CallSoapReqForLogin(productId,currentQty);
			
		}
	}
	//CallSoapForUpdatingCustomProperty(productId,currentQty,'update');	
}



function UpdateCurrentBasket(productId,quantity,comments,name,price)
{
	CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,0,comments,remove);
}

function AddSelectedSHippingProduct(productId)
{
	var shippingcomments = '&lt;options&gt;&lt;category&gt;Frakt&lt;/category&gt;&lt;/options&gt;';
	var shippingProduct = read_cookie('shippingProduct');
	CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,shippingProduct,1,'comments','remove');
	CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,1,shippingcomments,'add');
	create_cookie('shippingProduct',productId,1);
	
}

function AddSelectedWrappingProduct(productId)
{
	var wrappingProduct = read_cookie('wrappingProduct');
	
	if(wrappingProduct == "")
	{
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,1,'comments','add');
		create_cookie('wrappingProduct',productId,1);
		
	}
	else
	{
		
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,1,'comments','add');
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,wrappingProduct,1,'comments','remove');
		create_cookie('wrappingProduct',productId,1);
		
	}
	
	
}
function AddSelectedGreetingCard(productId)
{
	var greetingCard = read_cookie('greetingCard');
	if(greetingCrad == "")
	{
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,1,'comments','add');
		create_cookie('greetingCard',productId,1);
		
	}
	else
	{
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,greetingCard,1,'comments','remove');
		CallSoapReqForUpdatingBasket(read_cookie('pageid'),read_cookie('positionid'),2,productId,1,'comments','add');
		create_cookie('wrappingProduct',productId,1);
		
	}
	
	
}



function GetCommentsInXMLFormat(comments)
{
	
	var commentArray ="";
	commentArray = comments.split('|');
	//var commentsInXml = '<options><size>'+commentArray[0]+'</size>'+'<color>'+commentArray[1]+'</color>'+'<category>'+commentArray[2]+'</category>'+'<imageurl>'+commentArray[3]+'</imageurl>'+'<detailurl>'+escape(commentArray[4])+'</detailurl></options>';
	var commentsInXml = '<options><size>'+commentArray[0]+'</size>'+'<color>'+commentArray[1]+'</color>'+'<category>'+commentArray[2]+'</category>'+'<imageurl>'+commentArray[3]+'</imageurl>'+'<detailurl>'+escape(commentArray[4])+'</detailurl>'+'<basket>'+commentArray[5]+'</basket>'+'<bought>'+commentArray[6]+'</bought></options>';
	return (commentsInXml.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'));
}

function AjaxUploader(imageurl,name,price,location){
	/*var link;
	if(location=="flash"){
		link = "/designtorget/upload/images/produkter/flash";
	}
	else
	{
		link = "/designtorget/upload/images/produkter";
	}*/
	if(document.getElementById("order_button"))
		document.getElementById("order_button").style.display = 'none';
	if(location=="largecart"){
		document.getElementById('small_basket_name').innerHTML = "Enter Amount more than 0[!Need se translation]";
		document.getElementById('small_basket_image').style.display = "none";
		document.getElementById('small_basket_text').style.display = "none";
		document.getElementById('small_basket_price').style.display = "none";
	}
	else{
		var price = Math.round(price);
		document.getElementById('small_basket_image').src = '/designtorget/upload/images/produkter/80x59_'+imageurl+'.jpg';
		document.getElementById('small_basket_name').innerHTML = name;
		document.getElementById('small_basket_price').innerHTML = price+":-";
	}
		document.getElementById('small_open').className = document.getElementById('small_open').className.replace('hidden','show');
		document.getElementById('small_cart_container').className = 'hidden';

		window.setTimeout(HideSmallBasket, 4000);
}

function HideSmallBasket(){
	
	document.getElementById('small_open').className = document.getElementById('small_open').className.replace('show','hidden');
	document.getElementById('small_cart_container').className = ' ';
	if(document.getElementById("order_button"))
		document.getElementById("order_button").style.display = 'block';
	if(document.getElementById("small_cart_hide").style.display == 'block')
	{
	   document.getElementById("order_button").style.display = 'none';
		
	}
}


function AddSelectedProduct(guid,prodName)
{
	this.guid = guid;
	this.ProdName = prodName;
	var comments ='';
	if(ProdName == 'wrapper')
	{
		document.getElementById('wrapper_product').value = "";
		document.getElementById('wrapper_product').value = guid;
	}
	if(ProdName == 'greeting')
	{
		document.getElementById('greeting_product').value = "";
		document.getElementById('greeting_product').value = guid;
	}
	if(ProdName == 'shipping')
	{
		document.getElementById('shipping_product').value = "";
		document.getElementById('shipping_product').value = guid;
	}
	if(ProdName == 'payment')
	{
		document.getElementById('payment_product').value = "";
		document.getElementById('payment_product').value = guid;
	}
	CallSoapReqForRemovingShippingProduct(read_cookie('pageid'),read_cookie('positionid'),2,readCookie('shippingProduct'),0,comments,'remove');
document.getElementById("shippingLabel").innerHTML = read_cookie('shippingOption');

}

function showLargeCart(){
        document.getElementById("large_cart").style.display = 'block';
	document.getElementById("teaser_area").style.display = 'block';
        document.getElementById("small_cart_hide").style.display = 'block';
	if(document.getElementById("order_button"))
		document.getElementById("order_button").style.display = 'none';
	//document.getElementById("small_cart_image").className = "red_basket_up";
	document.getElementById('small_cart_container').style.display = 'none';
}

function hideLargeCart() {
        document.getElementById("large_cart").style.display = 'none';
	document.getElementById("teaser_area").style.display = 'none';
	document.getElementById("small_cart_hide").style.display = 'none';
	//document.getElementById("small_cart_image").className = "red_basket";
	document.getElementById('small_cart_container').style.display = 'block';
	if(document.getElementById("order_button"))
		document.getElementById("order_button").style.display = 'block';
}

function removeProductConfirm(guid,display){
         if(display == 'show')
                 document.getElementById('remove_product_alert_'+guid).className = document.getElementById('remove_product_alert_'+guid).className.replace('hidden','show');
         else if(display == 'hide')
                document.getElementById('remove_product_alert_'+guid).className = document.getElementById('remove_product_alert_'+guid).className.replace('show','hidden');
}

function addParams(param,paramname,value)
{
   this.param = param;
   this.paramname = paramname;
   this.value = value;
}
var Parameters;

var Guid;
var Quantity;
var Timer;

function KeyupEvent(guid,quantity){
	Guid = guid;
	Quantity = quantity;
	Timer = setTimeout("setTimer()",3000);
}

function setTimer() {
        var Productid = Guid;
	var Quan = Quantity;
	UpdateProductCount(Productid,Quan);  
}

function KeydownEvent(){
	clearTimeout(Timer);
}


function ValidateVoucherCode(email,productId){
	
 	Parameters = new Array();
   	Parameters[Parameters.length]= new addParams('searchparam1','email',email);
   	Parameters[Parameters.length]= new addParams('searchparam2','rabattkod',productId);
	CallSoapReqForVoucherTracking(Parameters,productId);
}

function showUpdatingWindow(){
                               var browser = navigator.appName;
                              var b_version = navigator.appVersion;
                              var version = parseFloat(b_version);
                              if((browser == "Microsoft Internet Explorer") && (b_version.indexOf('MSIE 6.0') > -1 )){
                                        var body = document.body;
                                        var hoverContainer = document.createElement("div");
                                        hoverContainer.className = "checkout_hover_container";
                                        hoverContainer.setAttribute("id", "hover_img");
                                        body.insertBefore(hoverContainer, body.firstChild);
				        document.getElementById('popup_area').style. display="block"; 
					document.getElementById('popup_area').className = "update_area";  
					document.getElementById('popup_area').className = "update_window";                                           
                                        document.getElementById('popup_area').innerHTML='<h4>Uppdaterar</h4>' + '<img class="global_update_img" src="/designtorget/upload/files/templates/update.gif" />';
                                        var bodyheight= document.getElementById('body_frame').scrollHeight; 
                                        document.getElementById('hover_img').style.height = bodyheight;
                                        var focusobj= document.getElementById('popup_area');
                                        focusobj.focus();       
                                   }
                              else{
                                        var body = document.body;
                                        var hoverContainer = document.createElement("div");
                                        hoverContainer.className = "checkout_hover_container";
                                        hoverContainer.setAttribute("id", "hover_img");
                                        var alertText = document.createElement('div');
                                        alertText.className = "update_window";
                                        alertText.innerHTML = '<h4>Uppdaterar</h4>' + '<img class="global_update_img" src="/designtorget/upload/files/templates/update.gif" />';
                                        hoverContainer.appendChild(alertText);
                                        body.insertBefore(hoverContainer, body.firstChild);
                                   }

          }
function closeUpdatingWindow()
                   {

                       /* var popup_id = document.getElementById('hover_img');
                        var body = document.body;
                        body.removeChild(popup_id);*/
			if(document.getElementById('hover_img'))
				document.getElementById('hover_img').style. display="none";
                        document.getElementById('popup_area').style. display="none";    
 }


var NewShippingProd = '';
var NewWrapperProd = '';
var NewGreetingProd = '';
var ProductDeleted = false;

function IsWrapGreetDeleted(ProdID)
{
	var AllProducts = document.getElementsByTagName('input');
	for(var i = 0; i < AllProducts.length; i++)
	{
		if(AllProducts[i].type == 'radio')
		{
			if(AllProducts[i].getAttribute('prodid') != null)
			{
				if(AllProducts[i].name == 'wrapper' || AllProducts[i].name == 'greetingcard')
				{
					if(AllProducts[i].getAttribute('prodid') == ProdID.toUpperCase())
					{
						AllProducts[i].checked = false;
					}
				}
			}
		}
	}
}

 function setCount(guid){ 
      if(document.getElementById('quantity_'+guid)){
            //alert("VALUE:"+document.getElementById('quantity_'+guid).value);
            create_cookie('ProductCount_'+guid,document.getElementById('quantity_'+guid).value);
	    //alert("COOKIE:"+readCookie('ProductCount_'+guid));	
       }
 }
function trim(str) {
 return str.replace(/^\s+|\s+$/g,"");
}
