
function GetLang()
{	return "EN";
}

function mArray()
{
    this.Count = 0;
    return(this);
}

function setCookie(doc, cookie_name, cookie_value, cookie_life, cookie_path, cookie_domain)
{
  var today = new Date()
  var expiry = new Date(today.getTime() + cookie_life * 24*60*60*1000)
  if (cookie_value != null && cookie_value != "")
  {
    var cookie_string = cookie_name + "=" + escape(cookie_value)

    if(cookie_life)
    { cookie_string += "; expires=" + expiry.toGMTString()
    }

    if(cookie_path)   { cookie_string += "; path=" + escape(cookie_path); }
    if(cookie_domain) { cookie_string += "; domain=" + escape(cookie_domain); }
    
 	doc.cookie = cookie_string
  }
}

function getCookie(doc, name)
{
  var index = doc.cookie.indexOf(name + "=")
  if (index == -1)
  {  return null
  }

  index = doc.cookie.indexOf("=", index) + 1
  var end_string = doc.cookie.indexOf(";", index)
  if (end_string == -1)
  { end_string = doc.cookie.length
  }
  return unescape(doc.cookie.substring(index, end_string))
}

function Item(ItemID, ItemTitle, ItemPriceGBP, ItemPriceEUR, ItemPriceUSD)
{
	this.ID       	= ItemID;
	this.Title    	= ItemTitle;
	this.PriceGBP 	= ItemPriceGBP;
	this.PriceEUR 	= ItemPriceEUR;
	this.PriceUSD 	= ItemPriceUSD;
}

function BasketAddItem(doc, ItemID, ItemName, ItemPriceGBP, ItemPriceEUR, ItemPriceUSD)
{
	for (var n = 1; n <= this.Items.Count; n++)
	{	if (this.Items[n].ID == ItemID)
			return;
	}

	this.Items.Count++;
	this.Items[this.Items.Count] = new Item(ItemID, ItemName, ItemPriceGBP, ItemPriceEUR, ItemPriceUSD);

    this.StoreCookie(doc)
}

function BasketDeleteItem(doc, ItemID)
{
	for (var n = 1; n <= this.Items.Count; n++)
	{	if (this.Items[n].ID == ItemID)
		   break;

	}

	for (; n < this.Items.Count; n++)
	{	this.Items[n] = this.Items[n+1];
	}

	this.Items.Count--;

    this.StoreCookie(doc);
}

function BasketStoreCookie(doc)
{
    var cookie = '';

	for (var n = 1; n <= this.Items.Count; n++)
	{
        cookie += this.Items[n].ID + '\t';
        cookie += this.Items[n].Title + '\t';
        cookie += this.Items[n].PriceGBP + '\t';
        cookie += this.Items[n].PriceEUR + '\t';
        cookie += this.Items[n].PriceUSD + '\t';
	}

    if (this.Items.Count == 0)
    {	cookie = 'empty';
    }

    setCookie(doc, 'order_items', cookie, 360, '/');
}


function BasketReadCookie(doc)
{
    var cookie = getCookie(doc, 'order_items');
    if (cookie != null)
    {
        var items  = cookie.split('\t');

        for (n = 0; n + 5 < items.length; n += 5)
        {
    	    this.Items.Count++;
    	    this.Items[this.Items.Count] = new Item(items[n], items[n+1], items[n+2], items[n+3], items[n+4]);
        }
    }
}

function BasketDisplayItems(doc)
{
	if (this.Items.Count > 0)
	{
    	doc.write('<div id=basket>');
		doc.write('<table width="100%">');
        doc.write('<colgroup><col width="8px"><col width="80px"><col width="*"><col width="80px"></colgroup>');
		doc.write('	<tr>');
		doc.write('		<th class="np-pad">&nbsp;</th>');
		doc.write('		<th>Code</th>');
		doc.write('		<th>Title</th>');
		doc.write('		<th>Price</th>');
		doc.write('	</tr>');

		var TotalGBP   = 0;
		var TotalEUR   = 0;
		var TotalUSD   = 0;

		for (var n = 1; n <= this.Items.Count; n++)
		{   var txt1 = "JavaScript:_Basket.DeleteItem(document, '" + this.Items[n].ID + "'); history.go(0);";
			var txt2 = "self.status='Delete from basket'; return true;";
			var txt3 = "self.status='';";

			doc.write('<tr>');
			doc.write('	 	<td class="no-pad"><a href="' + txt1 + '" onMouseOver = "' + txt2 + '" onMouseOut = "' + txt3 + '";><img src="images/del.gif" alt="Delete from basket"></a></td>');
			doc.write('	 	<td>' + this.Items[n].ID + '</td>');
			doc.write('	  	<td>' + this.Items[n].Title + '</td>');
			doc.write('	  	<td class="right">' + DecToStr(this.Items[n].PriceGBP, this.Items[n].PriceEUR, this.Items[n].PriceUSD) + '</td>');
			doc.write('</tr>');

			TotalGBP   += 1 * this.Items[n].PriceGBP;
			TotalEUR   += 1 * this.Items[n].PriceEUR;
			TotalUSD   += 1 * this.Items[n].PriceUSD;
		}

		doc.write('  <tr>');
		doc.write('  	<td colspan="3" class="total">Total</td>');
		doc.write('  	<td class="total">' + DecToStr(TotalGBP, TotalEUR, TotalUSD) + '</td>');
		doc.write('  </tr>');

		doc.write('</table>');
        doc.write('</div>');

        doc.write('<br>');

	    doc.write('<form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="paypal">\n');
	    doc.write('<input type="hidden" name="cmd" value="_cart">\n');
	    doc.write('<input type="hidden" name="upload" value="1">\n');
	    doc.write('<input type="hidden" name="business" value="sales@old-photographs.com">\n');
	    doc.write('<input type="hidden" name="currency_code" value="' + GetCurr() + '">\n');
		doc.write('<input type="hidden" name="charset" 		 value="UTF-8">\n');
	    doc.write('<input type="hidden" name="rm" 			 value="2">\n');
	    doc.write('<input type="hidden" name="return" 		 value="http://www.old-photographs.com/index.php?id=confirmed">\n');
	    doc.write('<input type="hidden" name="cancel_return" value="http://www.old-photographs.com">\n');

		for (var n = 1; n <= this.Items.Count; n++)
		{
		    doc.write('<input type="hidden" name="item_number_'+n+'" value="' + this.Items[n].ID + '">\n');
		    doc.write('<input type="hidden" name="item_name_'+n+'" value="'   + this.Items[n].Title + '">\n');

	        if (GetCurr() == String("GBP"))
	        {    doc.write('<input type="hidden" name="amount_'+n+'" value="' + this.Items[n].PriceGBP + '">\n');
	        }
	        else if (GetCurr() == String("EUR"))
	        {    doc.write('<input type="hidden" name="amount_'+n+'" value="' + this.Items[n].PriceEUR + '">\n');
       		}
	        else if (GetCurr() == String("USD"))
	        {    doc.write('<input type="hidden" name="amount_'+n+'" value="' + this.Items[n].PriceUSD + '">\n');
	        }

		    doc.write('<input type="hidden" name="item_name_'+n+'" value="'   + this.Items[n].Title + '">\n');
        }

		doc.write('<p class=\"terms\"><b>TERMS AND CONDITIONS:</b></p>\n');
        doc.write('<p class=\"terms\"><b>These photographs are intended to be used for private and/or educational use only</b> \n');
        doc.write('(for display in a library, museum, or educational establishment; or use in degree theasis), full ackowlegdement music be given next to the photograph.</p>\n');
        doc.write('<p class=\"terms\">If you wish to use them for <b>commercial use</b> (publication in a book, broadcasting, use in films etc.), \n');
        doc.write('please contact us with details of your project.</p>\n');
        doc.write('<p class=\"terms\">Under any circumstances these photographs are <b>not for resale in digital format</b>.</p>\n');

		doc.write('<input type="hidden" name="no_note" value="1">\n');
        doc.write('<center><input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but5.gif" border="0" name="submit" alt="Make payments with PayPal - it is fast, free and secure!"></center>\n');
		doc.write('</form>\n');

		return true;
	}
	else
	{   return false;
	}
}

function BasketGetTotal(doc)
{
	var Total = 0;

	if (this.Items.Count > 0)
	{
		for (var n = 1; n <= this.Items.Count; n++)
		{   Total += this.Items[n].Qty * this.Items[n].PriceGBP;
		}
	}

	var s = new String();
	s     = "" + Math.round(Total);
    return s;
}


function BasketClearOrder()
{   this.Items = new mArray();
}

function Basket()
{
	this.Items	 	            = new mArray();
	this.AddItem                = BasketAddItem;
	this.DeleteItem             = BasketDeleteItem;
	this.DisplayItems           = BasketDisplayItems;
    this.StoreCookie            = BasketStoreCookie;
    this.ReadCookie             = BasketReadCookie;
	this.GetTotal				= BasketGetTotal;
    this.ClearOrder				= BasketClearOrder;
    this.NoBuy					= false;
}

_Basket = new Basket();



