// Declare these global variables so that we can use the for the buy popups
var SELECTED_SUBCAT_ID = "";
var SELECTED_IMAGE_ID = "";
var SELECTED_IMAGE_FILENAME = "";
var SELECTED_BOOK_TITLE = "";
var SELECTED_BOOK_PRICE = "";
var SELECTED_BOOK_AUTHOR = "";


var SELECTED_BOOK = "";

function setBook(param){
	 for (var key in myBookshop.books){
		 //alert(myBookshop.books[key].code);
		if (myBookshop.books[key].code == param.code){
		   SELECTED_BOOK_PRICE = myBookshop.books[key].price;
		   SELECTED_BOOK_AUTHOR = myBookshop.books[key].author;
		  }
	 }
}

//REMOTE DATA FUNCTIONS	
 function JQdoDataAction(state,params){
   var  strAjaxPage = "ajax_data.php";
   var sUrl = strAjaxPage; 
   var thisLanguageChoice = "gb"
	switch (state){
	 case 'check_discount':
	 
	  var str = $("#frm_checkdiscount").serialize();
	  var pars = '&state=' + state + '&' +str +  '&lang=' + thisLanguageChoice + '&rnd='+ Math.random( );	
	  sUrl = sUrl + "?" + pars;
	  jQuery.getJSON( sUrl, function(data){
		  var this_title = thisArray[0].subnav[SELECTED_SUBCAT_ID].subnav_title;
		  var this_author = SELECTED_BOOK_AUTHOR;
		  var this_price = SELECTED_BOOK_PRICE;
						//for (v in thisPortfolio_Collection){
						 //	if (thisPortfolio_Collection[v]["TitleCode"]==thisTitleChoice){
							//var this_title = thisPortfolio_Collection[v]["TitleLong"];
							 //var this_author = thisPortfolio_Collection[v]["TitleAuthor"];
							 //var this_price = thisPortfolio_Collection[v]["Price"];
							// }
						 //} 
						
						var build_str =  "<div id='order_butt_page'><p class='order_butt_title'>" + this_title + "</p><p class='order_butt_author'>" + this_author + "<\/p>";
						build_str = build_str +"<p class='order_butt_price'>&pound;" + this_price + "<\/p>";
							//if (data.discount_text[0].amount!="0"){
							 build_str = build_str +"<p class='order_butt_discount'>" + data.discount_text[0].description+"<\/p>";
							//}
						build_str = build_str +"<p><!-- Because of their weight and to minimise costs to you, all book deliveries are made by surface mail within specific geographical regions. -->Delivery charges are per copy of each title.<\/p><table class='order_butt_table'>";
								for (v in data.book_buttons_text){
								 build_str = build_str + '<tr><td><form method="post" action="https://www.paypal.com/cgi-bin/webscr" target="paypal" id="frm_' + data.book_buttons_text[v].paypal_id + '"><input type="hidden" value="_s-xclick" name="cmd"/><input type="hidden" value="http://www.thepicture.co.uk" name="return"/><input type="hidden" value="'+data.book_buttons_text[v].paypal_id +'" name="hosted_button_id"/><a onclick="submitMe(\''+data.book_buttons_text[v].paypal_id +'\');" href="javascript:nada();"><table><tr><td>' + data.book_buttons_text[v].butt_text + '<\/td><\/tr><\/table><\/a><\/form><\/td><td>' + data.book_buttons_text[v].help_text  + '<\/td><\/tr>' ;		
								}
						build_str = build_str + "<\/table><p><!-- If you wish to receive your order faster by airmail, please contact sales@thePICTURE.co.uk for prices.-->You will receive an Order Confirmation with the anticipated delivery date.<\/p><\/div>";
						
						$("#dialog_orderalert").html(build_str);
						$("#dialog_orderalert").dialog('option', 'buttons', { "Cancel": function() { $(this).dialog("close"); } });
						
						}
					)
		 break;
   } 
}


function checkTerms(v){
	var html_str = "<div id='order_discount_page'>";
	//html_str += thisSITE_TEXT["ST10M"];
	html_str += "<p>All payments are handled by PayPal. You can use your credit/debit card or a PayPal account.</p><p>The information you give is private and secure.By proceeding with your order, you confirm that you have read my Terms and Conditions.</p><p>You can benefit from a discount on my books.Please enter your discount code here.</p>";
	var html_frm = '<form id="frm_checkdiscount"><input name="title_code" type="hidden" value="' + thisArray[0].subnav[SELECTED_SUBCAT_ID].text_short_1 +'" /><input name="discount_code" type="text" /></form>';
	html_str += html_frm + "<p>Lost or forgotten your code? Please send your name to <a href='mailto:discounts@thePICTURE.co.uk'>discounts@thePICTURE.co.uk<\/a> for a reminder.</p><\/div>";
	$("#dialog_orderalert").html(html_str);
		if (v==1){
		 euFlag = false;
		}else{
		 euFlag = true;
		}
	$("#dialog_orderalert").dialog("open");
	$("#dialog_orderalert").dialog('option', 'buttons', { 'Continue': function() {JQdoDataAction("check_discount",{});},"Cancel": function() { $(this).dialog("close"); } });
	
   }
   
   function submitMe(v){
	var str;
	str = "#frm_" + v;
	$(str).submit(); 	
	$("#dialog_orderalert").dialog('close');
 }


function closeDialog (v){
	 $("#dialog_benefit").dialog('close');
	};

function run_edhealey(){
	$("#dialog_orderalert").dialog({
			autoOpen: false,
			height:500,
			width:440,
			modal: true,
			buttons: {
				'Continue': function() {
				 JQdoDataAction("check_discount",{});
					if (euFlag){
					 //document.frm_paypal_eu.submit();
					}else{
					//document.frm_paypal_xeu.submit();
					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
		});

	
	$("#Printroom").delegate("a.orderButton", "click", function(){
		var buyWin = window.open ("buy.php?folio_id="+SELECTED_SUBCAT_ID+"&image_no="+SELECTED_IMAGE_ID+"&thisImg=" + SELECTED_IMAGE_FILENAME , "buywin",'width=615,height=450,scrollbars=yes,menubar=0')
		buyWin.focus();
		return false;
	});
	 

	$("#Bookshop").delegate("a.orderButton", "click", function(){
	
		checkTerms(0);
		return false;
	});

}

