﻿var BUYNOW_HEIGHT = 0;
var ecomXML = null;
var defImgUrl = "/NOKIA_COM_1/Web_Applications/nokiaa/BuyNow/pics/";
var isLtr = !(document.getElementsByTagName('body')[0].getAttribute('dir') === 'rtl');
$().ready(function()
{
	$.ajaxSetup({type:'GET',cache:true,dataType:'xml',async:false,timeout:5000});
	try{
		$.ajax({
			url:ecomUrl,
			cache:true,
			success:function(xml)
			{
				check = $(xml).find('parameters ecomEnabled').text();
				if (check=="true" && isProduct)
					ecomXML = $(xml).find('parameters priceDataTemplateUrlProduct').text();
				else if (check=="true" && !isProduct)
					ecomXML = $(xml).find('parameters priceDataTemplateUrlAccessory').text();
				else ecomXML = null;
			}
		});
	}catch(err){}
	
	var priceExists = false;
	var linkExists = false;
	var buyWithPlan = false;
	var price3 = null;
	var price1 = null;
	var price2 = null;
	var buyNowUrl1 = null;
	var buyNowUrl2 = null;
	var buyNowUrl3 = null;
	var accInBox = new Array();
	if(ecomXML != null) {
		ecomXML = ecomXML.replace(/@prodId@/,productId);
		$.ajax({
			url:ecomXML,
			success:function(xml) {	
				if(isProduct) {
					ecomContent = $(xml).find('productDetail product ecom');
					if (template == "3" || template == "4") { 
						ecomWitb = $(xml).find('productDetail accessories included');
						$(ecomWitb).find('accessory').each(function(){	
							accInBox[accInBox.length] = $(this).find('accessoryName').text();
						});
					}
				} else {
					ecomContent = xml;
				}
				price1 = $('productPrice1',ecomContent).text();
				price3 = $('productPrice3',ecomContent).text();
				price2 = $('productPrice2',ecomContent).text();
				buyNowUrl1 = $('buyNowUrl1',ecomContent).text();
				buyNowUrl3 = $('buyNowUrl3',ecomContent).text();
				buyNowUrl2 = $('buyNowUrl2',ecomContent).text();
				textField2 = $('textField2', ecomContent).text();

				if ((price1 != "" && price1 != null) || (price3 != "" && price3 != null)) {
					priceExists = true;
				}
				if ((buyNowUrl1 != "" && buyNowUrl1 != null) || (buyNowUrl3 != "" && buyNowUrl3 != null)) {
					linkExists = true;
				}
				if (buyNowUrl2 != null && buyNowUrl2 != "" && (buyNowUrl3 == null || buyNowUrl3 == "")) {
					buyWithPlan = true;
				}
				if (textField2 != null && textField2 != "") {
					infoText = textField2;
				}
			}	
		});
	}
	$('#buyNowModule').html("");
	
	if (priceExists || linkExists || buyWithPlan) {
		
		if(template == "1") {
			r5Normal(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists);
		} else if(template == "2") {
			r5Classic(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists);
		} else if (template == "3") { 
			r6(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists, accInBox);
		} else {
			nSeries(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists, accInBox);
		}
	}
	else if(isPanEnabled && findStoreUrl.length > 0 && findStore.length > 0) {
		if(template == '4') {
			var defImgUrlF = defImgUrl;
			var defImgTop = "r6_buynow_btn_top.png";
			var defImgBtm = "r6_buynow_btn_btm.png";
			if (!isLtr) {
				defImgTop = "r6_buynow_btn_top_rtl.png";
				defImgBtm = "r6_buynow_btn_btm_rtl.png";
			}
			defImgTop = defImgUrlF + defImgTop;
			defImgBtm = defImgUrlF + defImgBtm;
			$('#buyNowModule').append('<div class="nsBnContentAreaTop"><img width="196" height="6" src="' + defImgTop + '" alt=""/><br/></div>'
									+'<div class="nsBnContentAreaMiddle"><a href="' + findStoreUrl + '" class="nsBnButton"><span>' + findStore + '</span></a></div>'
									+'<div class="nsBnContentAreaBtm"><img width="196" height="6" src="' + defImgBtm + '" alt=""/><br/></div>'
								);
			BUYNOW_HEIGHT=13+26+12;
		}
		else if (template == '3') {
			var defImgUrlF = defImgUrl;
			var defImgTop = "r6_buynow_btn_top.png";
			var defImgBtm = "r6_buynow_btn_btm.png";
			if (!isLtr) {
				defImgTop = "r6_buynow_btn_top_rtl.png";
				defImgBtm = "r6_buynow_btn_btm_rtl.png";
			}
			defImgTop = defImgUrlF + defImgTop;
			defImgBtm = defImgUrlF + defImgBtm;
			$('#buyNowModule').append('<div class="ppBnContentTop"><img width="196" height="6" src="' + defImgTop + '" alt=""/><br/></div>'
									+'<a href="' + findStoreUrl + '" class="ppBnFindStore">' + findStore + '</a>'
									+'<div class="ppBnContentBtm"><img width="196" height="6" src="' + defImgBtm + '" alt=""/><br/></div>'
								);
		}
	}
	//alert('price1='+price1+' price3='+price3+' url1='+buyNowUrl1+' url2='+buyNowUrl3);
});

function r6(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists, accInBox) {
	var isFindAStrOnly = false;
	var output = new Array();
	var defImgUrlF = defImgUrl;
	var defImgTop = "r6_buynow_btn_top.png";
	var defImgBtm = "r6_buynow_btn_btm.png";
	if (!isLtr) {
		defImgTop = "r6_buynow_btn_top_rtl.png";
		defImgBtm = "r6_buynow_btn_btm_rtl.png";
	}
	defImgTop = defImgUrlF + defImgTop;
	defImgBtm = defImgUrlF + defImgBtm;
	
    output[output.length] = '<div class="ppBnContentTop"><img width="196" height="6" src="' + defImgTop + '" alt=""/><br/></div>';
	output[output.length] = '<div class="ppBnContentContainer">';
	
	if (priceExists || linkExists) {
		var vatOut = "";
		if (vat.length > 0) vatOut = '<div class="ppBnPriceContentAreaVAT">'+vat+'</div>';
		if (price1!=null && price3!=null && price1!="" && price3!="") {
			output[output.length] = '<div class="ppBnPriceContentArea"><div id="ppBnOldPrice" class="ppBnPriceContentAreaPrice">'+price1+'</div></div>';
			output[output.length] = '<div class="ppBnPriceContentArea"><div id="ppBnDiscPrice" class="ppBnPriceContentAreaPrice">'+price3+'</div>';
			output[output.length] = vatOut + '</div>';
		} else if (price1!=null && price1!="") {
			output[output.length] = '<div class="ppBnPriceContentArea"><div id="ppBnRegPrice" class="ppBnPriceContentAreaPrice">'+price1+'</div>';
			output[output.length] = vatOut + '</div>';
		} 
			
		if (buyNowUrl3!=null && buyNowUrl3!="") {
			output[output.length] = '<div class="ppBnPriceContentArea"><a href="'+buyNowUrl3+'" class="ppBnButtonArea"><span>'+preOrder+'</span></a></div>';
		} else if(buyNowUrl1!=null && buyNowUrl1!="") {
			output[output.length] = '<div class="ppBnPriceContentArea"><a href="'+buyNowUrl1+'" class="ppBnButtonArea"><span>'+buyNow+'</span></a></div>';
		} else {
			if((price1!=null && price1!="") || (price3!=null && price3!="")) {
				isFindAStrOnly = true;
				output[output.length] = '<div class="ppBnPriceContentArea"><a href="'+findStoreUrl+'" class="ppBnButtonArea"><span>'+findStore+'</span></a></div>';
				if (outOfStock.length > 0) output[output.length] = '<div class="ppBnOutOfStockArea"><span>'+outOfStock+'</span></div>';
			}
		}
	}

	if (buyWithPlan) {
		if (!priceExists && !linkExists) buyWithPlan1 = buyWithPlan2;
		if (buyNowUrl2.length > 0 && buyWithPlan1.length > 0) {
			output[output.length] = '<div class="ppBnWithPlan"><a href="' + buyNowUrl2 + '">' + buyWithPlan1 + '</a><br/>';
			if (price2 != null && price2.length != "") output[output.length] = infoText + ' <b>' + price2 + '</b>';
			output[output.length] = '</div>';
		}
	}

	if (whatinthebox != null && whatinthebox.length > 0 && accInBox.length > 0) {
		output[output.length] = '<div id="witb" class="ppBnWitb" onclick="showElement();">' + whatinthebox + '</div>';
		output[output.length] = '<div id="allresults" class="ppBnWitbResults">';
		for (var i = 0; i < accInBox.length; i++) {
			if (accInBox[i].length > 0) output[output.length] = '<div>' + accInBox[i] + '</div>';
		}
		output[output.length] = '</div>';
	}	
	output[output.length] = '</div>';
	output[output.length] = '<div class="ppBnContentBtm"><img width="196" height="6" src="' + defImgBtm + '" alt=""/><br/></div>';
	
	if ((buyNowUrl3==null || buyNowUrl3=="") && !isFindAStrOnly) {
		if (findStoreUrl.length > 0 && findStore.length > 0) {
			output[output.length] = '<div class="ppBnContentTop"><img width="196" height="6" src="' + defImgTop + '" alt=""/><br/></div>';
			output[output.length] = '<a href="' + findStoreUrl + '" class="ppBnFindStore">' + findStore + '</a>';
			output[output.length] = '<div class="ppBnContentBtm"><img width="196" height="6" src="' + defImgBtm + '" alt=""/><br/></div>';
		}
	}

	document.getElementById("buyNowModule").innerHTML = output.join("");
	pngfix();
}

function r5Normal(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists) {
	var output = new Array();
	output[output.length] = '<div class="bn_btn_r5n_containerTop"><div></div></div>';
	if (priceExists || linkExists) {
		var vatOut = "";
		if (vat.length > 0) vatOut = '<div class="bn_price_r5n_vat">'+vat+'</div>';
		
		if(price1!=null && price3!=null && price1!="" && price3!="") {
			output[output.length] = '<div id="bnOldPriceCtr" class="bn_price_r5n_regular"><div class="bn_price_r5n_container">'+price1+'</div></div>';
			output[output.length] = '<div id="bnDiscPriceCtr" class="bn_price_r5n_regular"><div class="bn_price_r5n_container">'+price3+'</div>';
			output[output.length] = vatOut + '</div>';
		} else if(price1!=null && price1!="") {
			output[output.length] = '<div id="bnRegPriceCtr" class="bn_price_r5n_regular"><div class="bn_price_r5n_container">'+price1+'</div>';
			output[output.length] = vatOut + '</div>';
		} else {
			if(!linkExists){
				output[output.length] = '<div class="bn_price_r5n_regular">'+noLinkText+'</div>';
			}
		}
		
		if(buyNowUrl3!=null && buyNowUrl3!="") {
			output[output.length] = '<a href="'+buyNowUrl3+'" id="bnBtnCtr" class="bt_btn_r5n_button"><span>'+preOrder+'</span></a>';
		} else if(buyNowUrl1!=null && buyNowUrl1!="") {
			output[output.length] = '<a href="'+buyNowUrl1+'" id="bnBtnCtr" class="bt_btn_r5n_button"><span>'+buyNow+'</span></a>';
		} else if (!linkExists) {
			if((price1!=null && price1!="") || (price3!=null && price3!="")) {
				output[output.length] = '<a href="'+findStoreUrl+'" id="bnBtnCtr" class="bt_btn_r5n_button"><span>'+findStore+'</span></a>';
				if (outOfStock.length > 0) output[output.length] = '<div class="bn_r5n_outofstock"><span>'+outOfStock+'</span></div>';
			}
		}
	}
	if (buyWithPlan && (buyNowUrl3==null || buyNowUrl3=="")) {
		if (!priceExists && !linkExists) buyWithPlan1 = buyWithPlan2;
		if (buyNowUrl2 != null && buyNowUrl2.length > 0) {
			output[output.length] = '<div class="bn_r5n_withPlan"><a href="' + buyNowUrl2 + '">' + buyWithPlan1 + '</a><br/>';
			if (price2 != null && price2 != "") output[output.length] = infoText + ' <b>' + price2 + '</b>';
			output[output.length] = '</div>';
		}
	}
	
	output[output.length] = '<div class="bn_btn_r5n_containerBtm"><div></div></div>';
	document.getElementById("buyNowModule").innerHTML = output.join("");
}

function r5Classic(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists) {
	var output = new Array();
	if (priceExists || linkExists) {
		var vatOut = "";
		if (vat.length > 0) vatOut = '<div class="bn_price_r5_alv">'+vat+'</div>';
		if(price1!=null && price3!=null && price1!="" && price3!="") {
			output[output.length] = '<div id="bnOldPriceCtr" class="bn_price_r5_regular"><div class="bn_price_r5_cont">'+price1+'</div></div>';
			output[output.length] = '<div id="bnDiscPriceCtr" class="bn_price_r5_regular"><div class="bn_price_r5_cont">'+price3+'</div>';
			output[output.length] = vatOut + '</div>';
		} else if (price1!=null && price1!="") {
			output[output.length] = '<div id="bnRegPriceCtr" class="bn_price_r5_regular"><div class="bn_price_r5_cont">'+price1+'</div>';
			output[output.length] = vatOut + '</div>';
		} else {
			if(!linkExists)	output[output.length] = '<div class="bn_price_r5_ctr">'+noLinkText+'</div>';
		}	
			
		if(buyNowUrl3!=null && buyNowUrl3!="") {
			output[output.length] = '<a href="'+buyNowUrl3+'" id="bnBtnCtr" class="bn_btn_link_r5"><span>'+preOrder+'</span></a>';
		} else if(buyNowUrl1!=null && buyNowUrl1!="") {
			output[output.length] = '<a href="'+buyNowUrl1+'" id="bnBtnCtr" class="bn_btn_link_r5"><span>'+buyNow+'</span></a>';
		} else if (!linkExists) {
			if((price1!=null && price1!="") || (price3!=null && price3!="")) {
				output[output.length] = '<a href="'+findStoreUrl+'" id="bnBtnCtr" class="bn_btn_link_r5"><span>'+findStore+'</span></a>';
				if (outOfStock.length > 0) output[output.length] = '<div class="bn_r5_outofstock"><span>'+outOfStock+'</span></div>';
			}
		}
	}
	if (buyWithPlan && (buyNowUrl3==null || buyNowUrl3=="")) {
		if (!priceExists && !linkExists) buyWithPlan1 = buyWithPlan2;
		output[output.length] = '<div class="bn_r5_withPlan"><a href="' + buyNowUrl2 + '">' + buyWithPlan1 + '</a><br/>';
		if (price2 != null && price2 != "") output[output.length] = infoText + ' <b>' + price2 + '</b>';
		output[output.length] = '</div>';
	}
	document.getElementById("buyNowModule").innerHTML = output.join("");
}

function nSeries(price1, price2, price3, buyNowUrl1, buyNowUrl2, buyNowUrl3, buyWithPlan, priceExists, linkExists, accInBox) {
	var output = new Array();
	var isFindAStrOnly = false;
	var defImgUrlF = defImgUrl;
	var defImgTop = "r6_buynow_btn_top.png";
	var defImgBtm = "r6_buynow_btn_btm.png";
	if (!isLtr) {
		defImgTop = "r6_buynow_btn_top_rtl.png";
		defImgBtm = "r6_buynow_btn_btm_rtl.png";
	}
	defImgTop = defImgUrlF + defImgTop;
	defImgBtm = defImgUrlF + defImgBtm;
	
	output[output.length] = '<div class="nsBnContentAreaTop"><img width="196" height="6" src="' + defImgTop + '" alt=""/><br/></div>';
	BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 13; //add extra 13 pixels that nsBnContentAreaMiddle brings
	output[output.length] = '<div class="nsBnContentAreaMiddle">';
	if (priceExists || linkExists) {	
		if (price1!=null && price3!=null && price1!="" && price3!="") {
			BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 56; 
			output[output.length] = '<div class="nsBnCurrentPrice">'+price3+'</div>';
			output[output.length] = '<div class="nsBnOldPrice">'+price1+'</div>';
		} else if (price1!=null && price1!="") {
			BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 38;
			output[output.length] = '<div class="nsBnCurrentPrice">'+price1+'</div>';
		} 
		if (vat.length > 0) {
			BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 12; //add VAT element height
			output[output.length] = '<div class="nsBnVAT">' + vat + '</div>';
		}
		if(buyNowUrl3!=null && buyNowUrl3!="") {
				output[output.length] = '<a href="'+buyNowUrl3+'" id="bnBtnCtr" class="nsBnButton"><span>'+preOrder+'</span></a>';
		} else if(buyNowUrl1!=null && buyNowUrl1!="") {
				output[output.length] = '<a href="'+buyNowUrl1+'" id="bnBtnCtr" class="nsBnButton"><span>'+buyNow+'</span></a>';
		} else {
			if((price1!=null && price1!="") || (price3!=null && price3!="")) {
				isFindAStrOnly = true;
				output[output.length] = '<a href="'+findStoreUrl+'" id="bnBtnCtr" class="nsBnButton"><span>'+findStore+'</span></a>';
				if (outOfStock.length > 0) output[output.length] = '<div class="nsBnOutOfStock">'+outOfStock+'</div>';
			}
		}
	}
	if (buyWithPlan && (buyNowUrl3==null || buyNowUrl3=="")) {
		if (!priceExists && !linkExists) buyWithPlan1 = buyWithPlan2;
		if (buyWithPlan1.length > 0) {
			BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 15; //add buy with plan height
			output[output.length] = '<div class="nsBnWithPlan"><a href="' + buyNowUrl2 + '">' + buyWithPlan1 + '</a>';
			if (price2 != null && price2.length != "") {
				BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 15; //add buy with plan height 2
				output[output.length] = infoText + ' <b>' + price2 + '</b>';
			}
			output[output.length] = '</div>';
		}
	}
	output[output.length] = '</div>';
	
	if (whatinthebox != null && whatinthebox.length > 0 && accInBox.length > 0) {
		BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 15; //add whats in the box height
		output[output.length] = '<div class="nsBnContentAreaMiddleB">';
		output[output.length] = '<div id="witb" class="nsBnWitb" onclick="showElement();">' + whatinthebox + '</div>';
		output[output.length] = '<div id="allresults" class="nsBnWitbResults">';
		for (var i = 0; i < accInBox.length; i++) {
			if (accInBox[i].length > 0) output[output.length] = '<div>' + accInBox[i] + '</div>';
		}
		output[output.length] = '</div></div>';
	}
	
	output[output.length] = '<div class="nsBnContentAreaBtm"><img width="196" height="6" src="' + defImgBtm + '" alt=""/><br/></div>';
	
	if ((buyNowUrl3==null || buyNowUrl3=="") && !isFindAStrOnly) {
		if (findStoreUrl.length > 0 && findStore.length > 0) {
			output[output.length] = '<div class="nsBnContentAreaTop"><img width="196" height="6" src="' + defImgTop + '" alt=""/><br/></div>';
			output[output.length] = '<div class="nsBnContentAreaMiddleC"><a href="' + findStoreUrl + '" class=""><span>' + findStore + '</span></a></div>';
			output[output.length] = '<div class="nsBnContentAreaBtm"><img width="196" height="6" src="' + defImgBtm + '" alt=""/><br/></div>';
			BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 26; //add find a store link area
		}
	}
	BUYNOW_HEIGHT = parseInt(BUYNOW_HEIGHT) + 12; //add bottom and top rounded corners
	document.getElementById("buyNowModule").innerHTML = output.join("");
	pngfix();
}

function showElement() {
	var isLtr = !(document.getElementsByTagName('body')[0].getAttribute('dir') === 'rtl');
    if ($("#allresults").is(":hidden")) {
        $("#allresults").slideDown("fast");
		$("#allresults").css("background-repeat", "no-repeat").css("background-image", "url(/NOKIA_COM_1/Web_Applications/nokiaa/BuyNow/pics/r6_buynow_shade.png)");
		$("#witb").css("background-image", "url(/NOKIA_COM_1/Web_Applications/nokiaa/BuyNow/pics/buynow_arrow_up.gif)").css("background-repeat","no-repeat");
		if (isLtr) {
			$("#witb").css("background-position","right 5px");
		} else {
			$("#witb").css("background-position","0px 5px");
		}
    } else {
        $("#allresults").slideUp("fast");
		$("#witb").css("background-image", "url(/NOKIA_COM_1/Web_Applications/nokiaa/BuyNow/pics/buynow_arrow_dwn.gif)").css("background-repeat","no-repeat");
		if (isLtr) {
			$("#witb").css("background-position","right 5px");
		} else {
			$("#witb").css("background-position","0px 5px");
		}
    }
}

function ctrlBnDisplay(stat) {
	if (stat) {
		$('#buyNowModule').show("fast");
	} else {
		$('#buyNowModule').hide("fast");
	}
}