
var basket;
var calendarPopup = null;
var selectedBasketDesign = null;
var col2 = 143;
var col3 = 308;
var col4 = 471;
var col5 = 610;

var line1 = null;
var line2 = null;
var line3 = null;

/*Initialize and reload basket if page is reloaded*/
function loadBasket() {
	var getBasket = function (data) {
		if (data != null) {
			basket = data;
			updateTotalPrice();
		}else{
			basket = null;
		}
	};
	AjaxService.loadBasket(getBasket);
}

function goToBasket(){
	if(dnaDesignerComp != null && dnaDesignerComp.webDesign != null){
		if(getBaskeDesign(dnaDesignerComp.designId()) != null && 
		!dnaDesignerComp.webDesign.isSaved){
			addToBasket(null, true);
		} else {
			showBasket();
		}
	}else{
		showBasket();
	}
}
/*Show basket*/
function showBasket() {
	var callBack = function(data){
		if (basket && (basket.userBasketDesigns.length > 0 || basket.imagesToBuy.length > 0)) {
			setMenuItems("menuItem6");	
			var screen = createBlockedScreen("_screenPopup");
			
			var header = createCustomScreen("stepFourHeader", createProperties(560, 20, 16, 16));
			var help = getHelp("stepFiveHelp");
			header.innerHTML = help.caption;
			setClassName(header, "title");
			header.onmouseover = function () {
				showHelpPopup("stepFiveHelp");
			};
			header.onmouseout = function () {
				hidePopup();
			};
			screen.appendChild(header);
			var columnTitle = createCustomScreen("_screenPopupHeader", createProperties(580, 20, 16, 51));
			columnTitle.style.borderBottom = "1px #D8D8D8 solid";
			screen.appendChild(columnTitle);
			
			var screens = new Object();
			screens.menu = columnTitle;
			createMenuItem("subMenu1", screens, basket_preview, "111");
			createMenuItem("subMenu2", screens, basket_media, "", col2);
			createMenuItem("subMenu3", screens, basket_req_files, "", col3);
			
			if(data.showPrice){
				createMenuItem("subMenu4", screens, basket_cost, "", col4);	
			}
			
			var body = createCustomScreen("_basketBody", createProperties(700, 250, 16, 76));
			
			loadBasketDesigns(body, screen.closeButton, data.showPrice, data.urnMaxLength, data.urnRegExp, data.imagedropdowninbasket);
			screen.appendChild(body);
			
			var calendarAnchor = document.createElement("div");
			calendarAnchor.zIndex = 1000;
			calendarAnchor.style.position = "absolute";
			calendarAnchor.style.background = "#FFFFFF";
			calendarAnchor.style.top = "0px";
			calendarAnchor.style.left = "0px";
			calendarAnchor.style.width = "0px";
			calendarAnchor.style.height = "0px";
			calendarAnchor.id = "basketCalendar";
			body.appendChild(calendarAnchor);
			
			screen.appendChild(createBasketFooter(data.showPrice, data.skippPayment));
		} else {
			createMsgBox(basket_empty_title, basket_empty_message, 250, 200);
			addMsgBoxCloseAction();
		}
	}
	AjaxService.getMediaOwner(callBack);
}


/*Load basket designs*/
function loadBasketDesigns(parent, closeButton, showPrice, urnMaxLength, urnRegExp, showBasketBool) {

	function createBasketHolder(id){
		var basketItemHolder = document.createElement("div");
		basketItemHolder.id = id;
		basketItemHolder.style.position = "static";
		basketItemHolder.style.height = "120px";
		basketItemHolder.style.width = "650px";
		basketItemHolder.style.borderBottom = "1px #D8D8D8 solid";
		return basketItemHolder;
	}

	function createBasketItem(){
		var basketItem = document.createElement("div");
		basketItem.style.position = "relative";
		basketItem.style.height = "120px";
		basketItem.style.width = "650px";
		var spacer = document.createElement("div");
		spacer.style.height = "5px";
		basketItem.appendChild(spacer);
		return basketItem;	
	}

	var sectionSizeName = new Array();
	var mapBamsItem = new Array();
	
	for (i = 0; i < basket.userBasketDesigns.length; i++) {
		var basketDesign = basket.userBasketDesigns[i];
		var basketItemHolder = createBasketHolder(basketDesign.designId);
		var basketItem = createBasketItem();
		basketItemHolder.appendChild(basketItem);

		var preview = document.createElement("div");
		preview.style.position = "absolute";
		
		//preview.style.border = "1px black solid";
		
		preview.style.width = "100px";
		img = document.createElement("img");
		preview.onmouseover = function(){
			showHelpPopup("basketThumbnailHelp");
		};
		preview.onmouseout = function(){
			hidePopup();
		};
		var now = new Date();
		img.src = contextPath + "/bams-file/preview?id=" + basketDesign.designId + "&fileName=SmallThumb&" + now.getTime();
		preview.appendChild(img);
		basketItem.appendChild(preview);
		
		var media = document.createElement("div");
		media.style.position = "absolute";
		media.style.width = "150px";
		media.style.left = col2 + "px";
		
		var publication = basketDesign.publicationDTO;
		var publicationName = document.createElement("div");
		publicationName.innerHTML = limitText(publication.pubcName, 20);
		media.appendChild(publicationName);
		
		var publicationTel = document.createElement("div");
		publicationTel.innerHTML = "Tel: " + ((publication.phonenumber) ? publication.phonenumber : "");
		media.appendChild(publicationTel);
		var section = basketDesign.sectionDTO;
		var sectionName = document.createElement("div");
		sectionName.innerHTML = section.sectionName;
		media.appendChild(sectionName);
		
		var designIdDesign = basketDesign.designId;
		var sectionSize = basketDesign.sectionSizeDTO;
				
		var sectionSizeName = document.createElement("div");
		
		sectionSizeName.innerHTML = sectionSize.sizeName  + " <br>(" + (basketDesign.designWidth/10) + "cm x " + 
		(basketDesign.designHeight/10) +"cm)";
		
		media.appendChild(sectionSizeName);
		
		//mapBamsItem[i] = designIdDesign;
		//sectionSizeName[i] = document.createElement("div");
		//media.appendChild(sectionSizeName[i]);	

//		var getDesignDetails = function(data){
//			var mdata = data.mdata;
//			var height = mdata.mdata_design_column_height;
//			var designIdDesign = data.id;
//			var labelSize;
//			if(height != null && height != -1 && height != ""){
//				height = height/10 + "cm";
//				labelSize = sizeName + "<br>" + "Depth " + height;
//			}else{
//				labelSize = sizeName;
//			}
//			if (data.item.itemDatecreate) {					
//				labelSize = labelSize + "<br>" + formatDate(data.item.itemDatecreate, "MMM d, y, hh:mm:ss");
//			}			
//			for (j = 0; j < 50; j++) {
//				if (designIdDesign == mapBamsItem[j]){
//					sectionSizeName[j].innerHTML = labelSize;
//				}
//			}
//			
//		};

		//AjaxService.getBamsItemWithMetadata(designIdDesign, getDesignDetails)		
		
		media.onmouseover = function(){
			showHelpPopup("basketmediaHelp");
		};
		media.onmouseout = function(){
			hidePopup();
		};
		
		basketItem.appendChild(media);
		var reqFields = document.createElement("div");
		reqFields.style.position = "absolute";
		reqFields.style.width = "150px";
		reqFields.style.left = col3 + "px";
		var dateLabel = document.createElement("div");
		dateLabel.innerHTML = basket_date + " <span style=color:red>*</span>";
		dateLabel.style.position = "relative";
		var calendar = document.createElement("div");
		calendar.style.position = "relative";
		
		if ((section.outputType == 'FLASH') || (section.outputType == 'IMAGE-JPG')
			|| (section.outputType == 'IMAGE-PNG')
			|| (section.outputType == 'IMAGE-GIF')
		) {

		} else
		{
			var inputDate = document.createElement("input");
			inputDate.onmouseover = function(){
				showHelpPopup("basketDateHelp");
			};
			inputDate.onmouseout = function(){
				hidePopup();
			};		
			inputDate.style.position = "relative";
			inputDate.id = "insertion-" + basketDesign.designId;
			inputDate.readOnly = true;
			if (basketDesign.insertionDate) {
				inputDate.value = formatDate(basketDesign.insertionDate, "dd/MM/yyyy");
				inputDate.style.border = "1px #D8D8D8 solid";
			} else {
				inputDate.style.border = "1px red solid";
			}
			setClassName(inputDate, "basketTextBox");
			inputDate.style.width = "134px";
			var calendarLink = document.createElement("img");
			calendarLink.style.position = "relative";
			calendarLink.src = interfaceImagesPath + "/calendar.gif";
			if(!isFireFox){
				calendarLink.style.cursor = "hand";	
			}		
			calendarLink.style.cursor = "pointer";
			calendarLink.id = "calendar-" + basketDesign.designId;
			calendarLink.align = "absmiddle";
			
			function calendarLinkOnmouseover(){
				showHelpPopup("basketDateHelp");
			}
			calendarLink.onmouseover = calendarLinkOnmouseover;
			
			function calendarLinkOnmouseout(){
				hidePopup();
			}	
			calendarLink.onmouseout = calendarLinkOnmouseout;
			
			
			function calendarLinkOnclick(){
				if (calendarPopup == null) {
					calendarPopup = new CalendarPopup("basketCalendar");
					calendarPopup.onmouseover = calendarHelpPopup;
					function calendarHelpPopup(){
						showHelpPopup("calendarHelp");
					}
					calendarPopup.setCssPrefix("calendar");
				}
				calendarPopup.onmouseover = calendarHelpPopup;
				function calendarHelpPopup(){
					showHelpPopup("calendarHelp");
				}
				
				calendarPopup.setReturnFunction("updateInsertionDate");
				
				var id = this.id.substr(this.id.indexOf("calendar-") + "calendar-".length);			
				selectedBasketDesign = id;			
				setCalendarDates(id);			
				calendarPopup.select(document.getElementById("insertion-" + id), "insertion-" + id, "dd NNN yyyy");
			}
			calendarLink.onclick = calendarLinkOnclick;
			
			calendar.appendChild(calendarLink);
			calendar.appendChild(inputDate);
			reqFields.appendChild(dateLabel);
			reqFields.appendChild(calendar);
			var spacer = document.createElement("div");
			spacer.style.position = "relative";
			spacer.style.height = "5px";
			spacer.style.width = "5px";
			reqFields.appendChild(spacer);
			
			var urnLabel = document.createElement("div");
			urnLabel.style.position = "relative";
			var urnInput = document.createElement("input");
			if (basketDesign.bookingRef) {
				urnInput.value = basketDesign.bookingRef;
				urnInput.style.border = "1px #D8D8D8 solid";
			} else {
				urnInput.style.border = "1px red solid";
			}
			
			
			function urnInputOnmouseover(){
				showHelpPopup("basketUrnHelp");
			}
			urnInput.onmouseover = urnInputOnmouseover;
					
			function urnInputOnmouseout(){
				hidePopup();
			}			
			urnInput.onmouseout = urnInputOnmouseout;
			
			urnInput.id = "urn-" + basketDesign.designId;
			urnInput.readOnly = true;
			urnInput.style.position = "relative";
			setClassName(urnInput, "basketUrnHelp");
			
	
			function urnInputOnclick(){
				var help = getHelp("urn");
				var msgText = "<br><img src=\""+interfaceImagesPath+"warning004.gif\" align=\"left\"><div style=\"color:red\">" + 
				help.body + "</div><br><br>";
				var msg = createMsgBox(help.caption, msgText, 400, 250, null, true);
				
				var urn = createInputBox("urn", msg.body, "text", true);
				urn.input.maxLength = urnMaxLength;
				urn.holder.style.position = "absolute";
				urn.holder.style.top = "60px";
				if (this.value) {
					urn.input.value = this.value;
				}
				
				function createInputBoxOnmouseover(){
					showHelpPopup("basketUrnHelp");
				}
				
				urn.input.onmouseover = createInputBoxOnmouseover;
					
				function createInputBoxOnmouseout(){
					hidePopup();
				}
				var obj = this;
				
				var submit = addMsgBoxAction("urnClick", "Submit", null, createProperties(100, 30, 145, 0, null, "absolute"), interfaceImagesPath + "/forward004-white.gif", 40);
	
				
				function submitOnclick(){
					if (urn.input.value) {
						if(urnRegExp != null && urnRegExp.length > 0){
							var re= new RegExp(urnRegExp);
							if(re.test("" + urn.input.value + "")){
								var id = obj.id.substr(obj.id.indexOf("urn-") + "urn-".length);
								var updateBasket = function(errorMessage){
									var message = errorMessage;
										if(message != null && message.length > 0){
											if(message == "URN_USED_SAME_DATE"){
												alert(URN_USED_SAME_DATE_MESSAGE)
												obj.style.border = "1px #D8D8D8 solid";
												obj.value = urn.input.value;
												closeMsgBox();
											}else if(message == "URN_USED_DIFF_DATE"){
												alert(URN_USED_DIFF_DATE_MESSAGE);
												closeMsgBox();
											}else if(message == "DATE_REQUIRED"){
												alert(DATE_REQUIRED_MESSAGE);
												closeMsgBox();
											}else if(message == "URN_USED"){
												alert(URN_USED_MESSAGE);
												closeMsgBox();
											}
										}else{
											obj.style.border = "1px #D8D8D8 solid";
											obj.value = urn.input.value;
											closeMsgBox();	
										}
								};
								AjaxService.updateDesignBookingReference(id, urn.input.value, updateBasket);
							}else{
								alert(getHelp("urn_validation").caption)
							}

						}else{

							var id = obj.id.substr(obj.id.indexOf("urn-") + "urn-".length);
								var updateBasket = function(errorMessage){
									var message = errorMessage;
										if(message != null && message.length > 0){
											if(message == "URN_USED_SAME_DATE"){
												alert(URN_USED_SAME_DATE_MESSAGE)
												obj.style.border = "1px #D8D8D8 solid";
												obj.value = urn.input.value;
												closeMsgBox();
											}else if(message == "URN_USED_DIFF_DATE"){
												alert(URN_USED_DIFF_DATE_MESSAGE);
												closeMsgBox();
											}else if(message == "DATE_REQUIRED"){
												alert(DATE_REQUIRED_MESSAGE);
												closeMsgBox();
											}else if(message == "URN_USED"){
												alert(URN_USED_MESSAGE);
												closeMsgBox();
											}
										}else{
											obj.style.border = "0px #D8D8D8 solid";
											obj.value = urn.input.value;
											closeMsgBox();	
										}
								};
							AjaxService.updateDesignBookingReference(id, urn.input.value, updateBasket);
											
						}
					} else {	
						alert(getHelp("urn").caption + is_required);
					}
				}
				submit.onclick = submitOnclick;
				addMsgBoxCloseAction();
			}
			if(basketDesign.editBookingRef == true){
				urnInput.onclick = urnInputOnclick;	
			}
			urnLabel.innerHTML = basket_urn + " <span style=color:red>*</span>";
			reqFields.appendChild(urnLabel);
			reqFields.appendChild(urnInput);
			basketItem.appendChild(reqFields);
		} 
		
		var cost = document.createElement("div");
		cost.style.position = "absolute";
		cost.style.width = "90px";
		cost.style.left = col4 + "px";
						
		if(showPrice){
			var baseCostTxt = "";
			if(basketDesign.baseCost != null){
				if(basketDesign.baseCost > 0){
					if(basketDesign.hideAdPrice){
						var totalMediaCost = 0;
						totalMediaCost += basketDesign.baseCost*1 + basketDesign.designCost*1;
							if(basketDesign.discountOrSurcharges != null){
								for(var k = 0; k < basketDesign.discountOrSurcharges.length; k++){
									totalMediaCost += basketDesign.discountOrSurcharges[k].amount;				
								}
							}
							baseCostTxt += "<span style=\"width:160px\">Media price:</span><span>" + formatCurrency((totalMediaCost) , basket.currencySymbol) + "</span>";	
							
					}else{
						baseCostTxt += "<span style=\"width:160px\">Design price:</span><span> " + formatCurrency(basketDesign.designCost, basket.currencySymbol) +
						"</span><br><span style=\"width:160px\">Media price:</span><span>" + formatCurrency(basketDesign.baseCost, basket.currencySymbol) + "</span>";				
							if(basketDesign.discountOrSurcharges != null){
								for(var k = 0; k < basketDesign.discountOrSurcharges.length; k++){
									baseCostTxt += "<br><span style=\"width:160px\">" + basketDesign.discountOrSurcharges[k].description + ": </span><span>" + 
									formatCurrency(basketDesign.discountOrSurcharges[k].amount, basket.currencySymbol) + "</span>";
								}
							}
						}
				}else{
					baseCostTxt = "<span style=\"width:160px\">Design price:</span><span>" + formatCurrency(basketDesign.designCost, basket.currencySymbol) +
					"</span><br><span style=\"width:160px\">Media price:</span> Not included</span>";
				}
			}else{
				baseCostTxt = "<span style=\"width:160px\">Image price:</span><span> " + formatCurrency(basketDesign.designCost, basket.currencySymbol) +"</span>";
			}
			cost.innerHTML = baseCostTxt;		
		}		

		basketItem.appendChild(cost);

		var actions = document.createElement("div");
		actions.style.position = "absolute";
		actions.style.width = "55px";
		actions.style.left = col5 -5 + "px";
		var open = document.createElement("img");
		
		
		function openOnmouseover(){
			showHelpPopup("basketOpen");
		}
		open.onmouseover = openOnmouseover;
		
		
		function openOnmouseout(){
			hidePopup();
		}
		open.onmouseout = openOnmouseout;
			
		open.src = interfaceImagesPath + "open004.gif";
		open.id = basketDesign.designId;
		open.style.position = "absolute";
		open.style.top = "0px";
		open.style.left = "-20px";
		if(!isFireFox){
			open.style.cursor = "hand";	
		}			
		open.style.cursor = "pointer";
		
		function openOnclick(){
			hidePopup();
			openDesign(this.id, closeButton);
		}
		open.onclick = openOnclick;
		
		actions.appendChild(open);
		var bin = document.createElement("img");
		
		
		function binOnmouseover(){
			showHelpPopup("basketRemove");
		}
		bin.onmouseover = binOnmouseover;
		
		
		function binOnmouseout(){
			hidePopup();
		}	
		bin.onmouseout = binOnmouseout;
			
		bin.src = interfaceImagesPath + "bin004.gif";
		bin.style.position = "absolute";
		bin.style.right = "0px";
		bin.style.top = "0px";
		bin.id = basketDesign.designId;
		if(!isFireFox){
			bin.style.cursor = "hand";	
		}			
		bin.style.cursor = "pointer";
		
		function binOnclick(){
			var designId = this.id;
			var updateBasket = function (data) {
				dnaDesignerComp.webDesign.isAddedToBasket = data.addedToBasket;
				dnaDesignerComp.webDesign.changeBasketIcon(designId);
				basket = data;
				closeButton.onclick();
				updateTotalPrice();
				showBasket();
			};
			AjaxService.removeDesignFromUserBasket(this.id, updateBasket);
		}
		bin.onclick = binOnclick;
		
		actions.appendChild(bin);
		basketItem.appendChild(actions);
		parent.appendChild(basketItemHolder);
	}
		
	//LOAD BASKET IMAGES
	for(i = 0; i < basket.imagesToBuy.length; i++){
		var image = basket.imagesToBuy[i];
		//alert(DWRUtil.toDescriptiveString(image, 1));
		var basketItemHolder = createBasketHolder(image.id);
		basketItemHolder.style.border.style = "black 1px solid";

		var basketItem = createBasketItem();
		basketItemHolder.appendChild(basketItem);
		
		var preview = document.createElement("div");
		preview.style.position = "absolute";
		preview.style.width = "100px";
		preview.style.height = "100px";
		preview.style.overflow = "hidden";
				
		var img = document.createElement("img");
		var now = new Date();
		img.src = contextPath + "/bams-file/preview?id=" + image.id + "&fileName=SmallThumb&" + now.getTime();
		preview.appendChild(img);
		basketItem.appendChild(preview);
		
		var reqFields = document.createElement("div");
		reqFields.style.position = "absolute";
		reqFields.style.width = "150px";
		reqFields.style.left = col3 + "px";
		
		//basketItem.appendChild(reqFields);
				
		var itemDescription = document.createElement("div");
		itemDescription.style.position = "absolute";
		itemDescription.style.width = "150px";
		itemDescription.style.left = col2 + "px";
		itemDescription.innerHTML = image.name;
		basketItem.appendChild(itemDescription);
		
		//alert(showBasket);
		
		if(showPrice && showBasketBool){
			var cost = document.createElement("div");
			cost.style.position = "absolute";
			cost.style.width = "90px";
			cost.style.left = col4 + "px";
			basketItem.appendChild(cost);

			var priceDropDown = createSelectBox("", cost, 0, false, null);
								
			priceDropDown.holder.style.width = "110px";
			priceDropDown.select.style.width = "110px";
			priceDropDown.select.id = image.id;
			var priceDropDownOnChange = function(){
				var updateBasketCall = function (data) {
					basket = data;
					updateTotalPrice();
				};
				AjaxService.addImageToBasket(this.id, this.value, updateBasketCall);
			}
			priceDropDown.select.onchange = priceDropDownOnChange;
				
			if(image.sizeNamePrice){	
				for(j = 0; j < image.sizeNamePrice.length; j++){
					var pp = image.sizeNamePrice[j];
					var name = pp.substr(0, pp.indexOf(":"));
					var value = pp.substr(pp.indexOf(":")+1, pp.length);
					priceDropDown.select.options[j] = new Option(basket.currencySymbol + name , value);
					if(value == image.size){
						priceDropDown.select.options[j].selected = true;
					}							
				}
			}
			
		}
		var actions = document.createElement("div");
		actions.style.position = "absolute";
		actions.style.width = "55px";
		actions.style.left = col5 -5 + "px";

		var binImage = document.createElement("img");
				
		function binOnmouseover(){
			showHelpPopup("basketRemove");
		}
		binImage.onmouseover = binOnmouseover;
		
		
		function binOnmouseout(){
			hidePopup();
		}	
		
		binImage.onmouseout = binOnmouseout;
			
		binImage.src = interfaceImagesPath + "bin004.gif";
		binImage.style.position = "absolute";
		binImage.style.right = "0px";
		binImage.style.top = "0px";
		binImage.id = image.id;
		if(!isFireFox){
			binImage.style.cursor = "hand";	
		}			
		binImage.style.cursor = "pointer";

		function binImageOnclick(){
			var updateBasket = function (data) {
				basket = data;
				closeButton.onclick();
				updateTotalPrice();
				showBasket();
			};
			AjaxService.removeImageFromUserBasket(this.id, updateBasket);
		}

		binImage.onclick = binImageOnclick;		
	
		if(image.basketImageStatus == 1){
			actions.appendChild(binImage);	
		}
		
		basketItem.appendChild(actions);
		
		parent.appendChild(basketItemHolder);
		
	}	
}

function displayPrice(showPrice){
	if(showPrice){
		if(line1 && line2 && line3){
			line1.innerHTML = formatCurrency(basket.totalBasketCost, basket.currencySymbol);		
			line2.innerHTML = formatCurrency(basket.vat, basket.currencySymbol);
			line3.innerHTML = "<b>" + formatCurrency((basket.totalBasketCost + basket.vat), basket.currencySymbol) + "</b>";
		}
	}
}

/*Create footer and calculate basket total*/
function createBasketFooter(showPrice, skippPayment) {
	var footer = createCustomScreen("_screenPopupFooter", createProperties(750, 110, 16, 330));
	var totalBox = document.createElement("div");
	totalBox.style.position = "absolute";
	totalBox.style.border = "0px black solid"
	totalBox.style.width = "150px";
	totalBox.style.height = "77px";
	totalBox.style.left = col4 + 33 + "px";
	var line1Label = document.createElement("div");
	line1Label.style.textAlign = "right";
	if(showPrice){
		line1Label.innerHTML = "<b>" + basket_sub_total + ":</b>";
	}
	line1Label.style.position = "absolute";
	line1Label.style.width = "150px";
	var line1Value = document.createElement("div");
	line1 = line1Value;	
	if(showPrice){
		line1Value.innerHTML = "" + formatCurrency(basket.totalBasketCost, basket.currencySymbol) + "";		
	}
	line1Value.style.position = "absolute";
	line1Value.style.left = "160px";
	totalBox.appendChild(line1Label);
	totalBox.appendChild(line1Value);
	var line2Label = document.createElement("div");
	
	line2Label.style.textAlign = "right";
	if(showPrice){
		line2Label.innerHTML = "<b>" + basket_vat + ":</b>";
	}
	line2Label.style.position = "absolute";
	line2Label.style.top = "20px";
	line2Label.style.width = "150px";
	var line2Value = document.createElement("div");
	line2Value.id = "line2";
	line2 = line2Value;
	if(showPrice){
		line2Value.innerHTML = formatCurrency(basket.vat, basket.currencySymbol);
	}
	line2Value.style.position = "absolute";
	line2Value.style.top = "20px";
	line2Value.style.left = "160px";
	totalBox.appendChild(line2Label);
	totalBox.appendChild(line2Value);
	var line3Label = document.createElement("div");
	line3Label.style.textAlign = "right";
	if(showPrice){
		line3Label.innerHTML = "<b>" + basket_total + ":</b>";
	}
	line3Label.style.position = "absolute";
	line3Label.style.top = "40px";
	line3Label.style.width = "150px";
	var line3Value = document.createElement("div");
	line3Value.id = "line3";
	line3 = line3Value;
	if(showPrice){
		line3Value.innerHTML = "<b>" + formatCurrency((basket.totalBasketCost + basket.vat), basket.currencySymbol) + "</b>";
	}
	line3Value.style.position = "absolute";
	line3Value.style.top = "40px";
	line3Value.style.left = "160px";
	totalBox.appendChild(line3Label);
	totalBox.appendChild(line3Value);
	footer.appendChild(totalBox);
	
	if(!skippPayment){
		var checkout = createAction("checkOut", "checkOutHelp", createProperties(100, 20, 115, 56), null, interfaceImagesPath + "/checkout004.gif", 45);
		function checkoutOnclick1(){	
			var pullBasket = function(basketData){
				basket = basketData;	
				var error = "";
				for (i = 0; i < basket.userBasketDesigns.length; i++) {
					var errorPerItem = "";
					var basketDesign = basket.userBasketDesigns[i];
					var section = basketDesign.sectionDTO;
					if ((section.outputType == 'FLASH') || (section.outputType == 'IMAGE-JPG')
						|| (section.outputType == 'IMAGE-PNG')
						|| (section.outputType == 'IMAGE-GIF')
					) {
					} else {
						if (!basketDesign.insertionDate) {
							errorPerItem += basketDesign.name + " : " + basket_date + is_required;
						}
						if (!basketDesign.bookingRef) {
							errorPerItem += basketDesign.name + " : " + basket_urn + is_required;
						}
						if (errorPerItem != "") {
							errorPerItem += "\n\n";
						}
						error += errorPerItem;
						errorPerItem = "";
					}
				}
				if (error != "") {
					alert(error);
				} else {
					var msgBox = createMsgBox(basket_payment_option, "", 300, 250, null, true);
					addMsgBoxCloseAction();
					var msg = document.createElement("div");
					msg.style.position = "absolute";
					msg.style.top = "10px";
					msg.style.left = "5px";
					msg.innerHTML = basket_payment_option_msg
					msg.style.width = "250px";
					msgBox.body.appendChild(msg);
						
					var byCreditCard = createAction("payByCreaditCard", "payByCreaditCard", createProperties(250, 35, 5, 35), null, interfaceImagesPath + "/Card-Icon-.png", 38);
					msgBox.body.appendChild(byCreditCard);
					
					function byCreditCardOnmouseover(){
						if (this.className == "deSelected" || this.className == "") {
							setClassName(this, "selected");
						}
					}
					byCreditCard.onmouseover = byCreditCardOnmouseover;
					
					function byCreditCardOnmouseout(){
						if (this.className == "selected" || this.className == "") {
							setClassName(this, "deSelected");
						}
					}
					byCreditCard.onmouseout = byCreditCardOnmouseout;
					
					function byCreditCardOnclick(){
						var callBack = function(url){
							if(url){		
								window.document.location.href = url;
							}else{
								alert("something is null");
								//not logedin or no basket
							}
						};
						AjaxService.packBasketAndGo("false", callBack);
					}
					
			 		byCreditCard.onclick = byCreditCardOnclick;
			 		
			 		var byAccount = createAction("payByAcc", "payByAcc", 
			 		createProperties(250, 35, 5, 75), null, interfaceImagesPath + "/Account-Icon-.png", 38);
			 		
					msgBox.body.appendChild(byAccount);	
					
					function byAccountOnmouseover(){
						if (this.className == "deSelected" || this.className == "") {
							setClassName(this, "selected");
						}
					}
					byAccount.onmouseover = byAccountOnmouseover;
					
					function byAccountOnmouseout(){
						if (this.className == "selected" || this.className == "") {
							setClassName(this, "deSelected");
						}
					}
					byAccount.onmouseout = byAccountOnmouseout;
					
					function byAccountOnclick(){
						var callBack = function(url){
							if(url){						
								window.document.location.href = url;
							}else{
								alert("something is null");
								//not logedin or no basket
							}
						};
						AjaxService.packBasketAndGo("true", callBack);
					}
					byAccount.onclick = byAccountOnclick;			
				}					
			}
			AjaxService.loadBasket(pullBasket);
		}
		checkout.onclick = checkoutOnclick1;
		totalBox.appendChild(checkout);		
	}else{
		
		var checkout2 = createAction("checkOut", "altCheckOutHelp", createProperties(100, 20, 115, 56), null, interfaceImagesPath + "/checkout004.gif", 45);
		
		function checkoutOnclick2(){
			var pullBasket = function(basketData){
				basket = basketData;
				var error = "";
				for (i = 0; i < basket.userBasketDesigns.length; i++) {
					var errorPerItem = "";
					var basketDesign = basket.userBasketDesigns[i];
					var section = basketDesign.sectionDTO;
					if ((section.outputType == 'FLASH') || (section.outputType == 'IMAGE-JPG')
						|| (section.outputType == 'IMAGE-PNG')
						|| (section.outputType == 'IMAGE-GIF')
					) {
					} else {
						if (!basketDesign.insertionDate) {
							errorPerItem += basketDesign.name + missingDateOrUrn +" : " + basket_date + is_required;
						}
						if (!basketDesign.bookingRef) {
							errorPerItem += basketDesign.name + " : " + basket_urn + is_required;
						}
						if (errorPerItem != "") {
							errorPerItem += "\n\n";
						}
						error += errorPerItem;
						errorPerItem = "";
					}
				}
				if (error != "") {
					alert(error);
				}else{
					var isUrnValid = function(urnErrors){
						//alert(urnErrors)
						if(urnErrors.length == 0){
							var screenOne = createBlockedScreen("_screenPopup");
							var headerOne = createCustomScreen("stepOneHeader", createProperties(560, 40, 10, 10));
							var help = getHelp("skippPaymentDisclaimer");
							headerOne.innerHTML = help.caption;
							setClassName(headerOne, "title");
					
							function headerOnmouseout(){
								hidePopup();
							}
							headerOne.onmouseout = headerOnmouseout;
							
							screenOne.appendChild(headerOne);
							bodyOne = createCustomScreen("_screenPopupBody1", createProperties(655, 330, 10, 61));
							
							var text = document.createElement("div");
							text.innerHTML = skippPaymentDisclaimerBody;
							
							bodyOne.appendChild(text);
							
							var acceptTC = createInputBox("acceptNewsLetter2", bodyOne, "checkbox");
							acceptTC.label.style.width= "250px";
							acceptTC.inputDiv.style.left = "250px";
							screenOne.appendChild(bodyOne);
							
		
							var accept = createAction("acceptDisclaimer", "acceptDisclaimer", createProperties(120, 20, 640, 400), null, interfaceImagesPath + "/register004.gif", 45);
							screenOne.appendChild(accept);
							accept.disabled = true;
							acceptTC.input.onclick = function(){
								if(this.checked == true){
									accept.disabled = false;
								}else{
									accept.disabled = true;
								}
							}
											
							function acceptOnClick(){
								if(acceptTC.input.checked == true){
									var backCall = function(data){
										if(data){
											var help = getHelp("urnUsed");
											headerOne.innerHTML = "<img src='"+interfaceImagesPath+"warning004.gif' absalign='middle'/> " + help.caption;							
											bodyOne.innerHTML = urnUsedBody;
											screenOne.removeChild(accept);
											var yesButton = createAction("acceptDisclaimer", "urnYes", createProperties(120, 20, 640, 400), null, interfaceImagesPath + "/register004.gif", 45);
											var noButton = createAction("acceptDisclaimer", "urnNo", createProperties(120, 20, 640, 355), null, interfaceImagesPath + "/cancel004.gif", 45);
											function noButClick (){
												screenOne.closeButton.onclick();
											}
											noButton.onclick = noButClick;
											
											screenOne.appendChild(yesButton);
											screenOne.appendChild(noButton);
																		
											function yesButClick (){
												var callBack = function(data){
													headerOne.innerHTML = "<img src='"+interfaceImagesPath+"happy.gif' align='absmiddle'/> <font color=\"blue\">Thank you</font>";
													bodyOne.innerHTML = skippCompleted1 + " " + data + " " + skippCompleted2;
													screenOne.removeChild(yesButton);
													screenOne.removeChild(noButton);
													
													
													var callBack = function(mediaOwner){
														var totalCost = document.getElementById("totalCost");
														if(mediaOwner.showPrice){
															totalCost.innerHTML = "&pound;0.00";
														}else{
															totalCost.innerHTML = "Send";
														}
													}
													
													AjaxService.getMediaOwner(callBack);											
													
													DesignerComponentService.removeDesign();
													basket = null;
													var closeButton = document.getElementById("btn_screenPopup");
													function closeAndReturn(){
														destroyScreen("_screenPopup");
														setMenuItems("menuItem1", true);
														menuItem1.onclick();
														
													}
													closeButton.onclick = closeAndReturn;
												}
												AjaxService.processSkippPayment(callBack);
											}							
											yesButton.onclick = yesButClick;
										}else{
											var callBack = function(data){
													headerOne.innerHTML = "<img src='"+interfaceImagesPath+"happy.gif' align='absmiddle'/> <font color=\"blue\">Thank you</font>";
													bodyOne.innerHTML = skippCompleted1 + " " + data + " " + skippCompleted2;
													DesignerComponentService.removeDesign();
													basket = null;
													
													var callBack = function(mediaOwner){
														var totalCost = document.getElementById("totalCost");
														if(mediaOwner.showPrice){
															totalCost.innerHTML = "&pound;0.00";
														}else{
															totalCost.innerHTML = "Send";
														}
													}
													
													AjaxService.getMediaOwner(callBack);
																								
													screenOne.removeChild(accept);
													var closeButton = document.getElementById("btn_screenPopup");
													function closeAndReturn(){
														destroyScreen("_screenPopup");
														setMenuItems("menuItem1", true);
														menuItem1.onclick();
													}
													closeButton.onclick = closeAndReturn;									
											}
											AjaxService.processSkippPayment(callBack);													
										}
									};
									AjaxService.checkUrns(backCall)							
								}else{
									alert("Accept TC");							
								}
							}
							accept.onclick = acceptOnClick;
						}else{
							alert(urnErrors);
						}
					};
					AdminComponentService.validateUrns(isUrnValid);					
				}				
			}	
			AjaxService.loadBasket(pullBasket);
		}	
		checkout2.onclick = checkoutOnclick2;
		totalBox.appendChild(checkout2);
	}
	
	return footer;
}
/*Upadte total of the basket display*/
function updateTotalPrice() {
	var total = document.getElementById("totalCost");
	var callBack = function(mediaOwner){
		if(mediaOwner.showPrice){
			total.innerHTML = formatCurrency(basket.totalBasketCost, basket.currencySymbol);
			displayPrice(true); 
		}else{
			if(basket.totalBasketCost > 0){
				totalCost.style.whiteSpace = "nowrap";
				totalCost.innerHTML = "Item/s in basket";
			}else{
				totalCost.innerHTML = "Send";
			}
		}
	}	
	AjaxService.getMediaOwner(callBack);
}

/*Limit text to the certain number of characters*/
function limitText(text, length) {
	return text.substring(0, length) + "...";
}
/*Set calendar dates*/
function setCalendarDates(id) {
	var design = getBaskeDesign(id);
	var frequency = design.publicationDTO.frequency;
	var publishingDay = design.publicationDTO.publishingday;
	var reSetCalendarDates = function () {
		for (i = 0; i < 7; i++) {
			calendarPopup.disabledWeekDays[i] = false;
		}
	};
	var now = new Date();
	calendarPopup.addDisabledDates(null, formatDate(now, "MM/dd/yy"));
	reSetCalendarDates();
	if (frequency) {
		if (frequency == "MONTHLY") {
		} else {
			if (frequency == "WEEKLY" || frequency == "BI-WEEKLY" || frequency == "DAILY") {
				if (publishingDay.indexOf("SU") == -1) {
					calendarPopup.disabledWeekDays[0] = true;
				}
				if (publishingDay.indexOf("MO") == -1) {
					calendarPopup.disabledWeekDays[1] = true;
				}
				if (publishingDay.indexOf("TU") == -1) {
					calendarPopup.disabledWeekDays[2] = true;
				}
				if (publishingDay.indexOf("WE") == -1) {
					calendarPopup.disabledWeekDays[3] = true;
				}
				if (publishingDay.indexOf("TH") == -1) {
					calendarPopup.disabledWeekDays[4] = true;
				}
				if (publishingDay.indexOf("FR") == -1) {
					calendarPopup.disabledWeekDays[5] = true;
				}
				if (publishingDay.indexOf("SA") == -1) {
					calendarPopup.disabledWeekDays[6] = true;
				}
			}
		}
	}
}
/*Get basked design by it's id*/
function getBaskeDesign(id) {
	if(basket != null){
		for (i = 0; i < basket.userBasketDesigns.length; i++) {
			var basketDesign = basket.userBasketDesigns[i];
			if (basketDesign.designId == id) {
				return basketDesign;
			}
		}
	}
}
								
function updateInsertionDate(year, month, day) {
	if (selectedBasketDesign != null) {
		var dateBox = document.getElementById("insertion-" + selectedBasketDesign);
		var urnBox = document.getElementById("urn-" + selectedBasketDesign);
		dateBox.value = day + "/" + month + "/" + year;
		dateBox.style.border = "1px #D8D8D8 solid";
		var updateBasket = function(errorMessage){
			var message = errorMessage;
			if(message != null && message.length > 0){
				if(message == "URN_USED_SAME_DATE"){
					alert(URN_USED_SAME_DATE_MESSAGE)
				}else if(message == "URN_USED_DIFF_DATE"){
					 alert(URN_USED_DIFF_DATE_MESSAGE);
					 urnBox.value = "";
				}
			}	
		};		
		AjaxService.updateDesignInsertionDate(selectedBasketDesign, year, month, day, updateBasket);
		loadBasket();
		selectedBasketDesign = null;
	}
}

