// common javascript for cambridgereprints.co.uk Website
//<meta name="author" CONTENT="Stephen Hall - Crescent Services">
//<meta name="copyright" CONTENT="Crescent Services 2006">

//----------------------------------------------------------------

var laminat = "0.50"; // cost to laminate
var shipadd = 0.00; // cost of shipping additional prints
var shipmult = 1.5; // multiplier for overseas postage
var lamop = "";

function disp_date() {
var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
document.write('' + weekday[d.getDay()] + ", ");
document.write('' + d.getDate() + " ");
document.write('' + monthname[d.getMonth()] + " ");
document.write('' + d.getFullYear());
window.name="CSMain";
}

function disp_copy() {
var d=new Date()	
document.write('Copyright &copy; '+d.getFullYear()+' Crescent Services');
}

function disp_cookie() {
cookieGet("cresreg");
if(firstname != ""){
document.write('<h2>Welcome - '+firstname+'</h2>');
document.write('you are registered with us');
document.write('<br>for <b>'+usercountry+'</b> deliveries,');
document.write('<br>with the email address of <b>'+useremail+'</b>');
document.write('<br>If this information is incorrect please re-register below.');
}else{
document.write('<h2>You are currently not registered</h2>');
document.write('To enable us to calculate your correct postage costs you must register your details below.');
document.write('<br>You will only need to complete this registration once as the information is stored on your computer as a "cookie".');
document.write('<br>This allows us to know when you have returned to this website.');
}
}

function Db(pfile,pdate,ptitle,ptext,pcat,psize,pprice,ppost,psize2,pprice2,ppost2) { 
  this.pfile	= pfile
  this.pdate	= pdate
  this.ptitle	= ptitle
  this.ptext	= ptext
  this.pcat	= pcat
  this.psize	= psize
  this.pprice	= pprice
  this.ppost	= ppost
  this.psize2	= psize2
  this.pprice2	= pprice2
  this.ppost2	= ppost2
 return this;
}

function getDb(id) {
 dfile = DocDb[id].pfile
 ddate = DocDb[id].pdate
 dtitle = DocDb[id].ptitle
 dtext = DocDb[id].ptext
 dprice = DocDb[id].pprice
 dsize = DocDb[id].psize
 dcat = DocDb[id].pcat
 dprice2 = DocDb[id].pprice2
 dsize2 = DocDb[id].psize2
 dpost = DocDb[id].ppost
 dpost2 = DocDb[id].ppost2
}

function paypal_cart()
{
	document.paypal.submit();
}

//----------------------------------------------------------------
// Login & Cookie Handler
//----------------------------------------------------------------

var expDays = 300;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function login(Lform) {
var pname = Lform.realname.value;
var plocation = Lform.location.value;
var pemail = Lform.email.value;
var Ffail = 0;
	if (pname == ""){
		alert("Please enter your first name")
		Lform.realname.focus();
		Ffail = Ffail + 1;
		}
	if (plocation == ""){
		alert("Please select your location")
		Lform.location.focus();
		Ffail = Ffail + 1;
		}
	if (!Check_email(pemail)) {
		Lform.email.focus();
		Ffail = Ffail + 1;
		}
if (Ffail == 0){
cookieSave("cresreg",pname,plocation,pemail);
Lform.submit();
}
}

function sub_load(Lform) {
var pemail = Lform.email.value;
	if (!Check_email(pemail)) {
		Lform.email.focus();
		}else{
		Lform.submit();
		}
}

function Check_email(pmail) {
	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	if (pmail != '' && objRE.test(pmail)){
		return true;
	}else{
		alert('Please enter a valid email address');
		return false;
	}
}

function cookieSave() {  
	cName = cookieSave.arguments[0];
	cfirstname = cookieSave.arguments[1];
	ccountry = cookieSave.arguments[2];
	cemail = cookieSave.arguments[3];
	value = cfirstname + '#cf#' + ccountry + '#cf#' + cemail;
	SetCookie(cName, value, exp);		
}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

function cookieGet (Gname) {  
cookieValue = GetCookie (Gname);
if(cookieValue == null) {
	firstname = "";
	usercountry = "";
	useremail = "";
}else{
	firstname = cookieValue.split('#cf#')[0];
	usercountry = cookieValue.split('#cf#')[1];
	useremail = cookieValue.split('#cf#')[2];
}
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function deleteCookie(name) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    location.reload(true);
  }
}

//----------------------------------------------------------------
// Display thumbnails
//----------------------------------------------------------------

function getNumcat(cat)
{
var imn = 0;
var totalImages = DocDb.length;
var totCat = 0;

while (imn < totalImages) {
	getDb(imn);	
	if (dcat==cat){totCat++};
	imn++;
	}
return totCat;
}

function write_IMarray(cat,SiN,colSize,MaxRowCount)
{
StartImageNum=SiN;
var ThSize = 150;
var totalImages = DocDb.length;
colCount = 0;
CellsonPage = (colSize * MaxRowCount);

if (!colSize){
	document.write('<p>Error in column array size');
	return false;
}
if (!MaxRowCount){
	document.write('<p>Error in row array size');
	return false;
}

if (cat == "All") 
{
totCat = totalImages;
}else{
totCat = getNumcat(cat);
if (totCat==0){
	document.write('<p>Error in accessing database<br>Unknown Category');
	return false;
}
if (!StartImageNum){
	document.write('<p>Error in accessing database<br>Illegal Image');
	return false;
}
}

lastid = write_NImage(cat,ThSize,StartImageNum,totalImages,colCount,colSize,colCount,MaxRowCount);
return lastid;
}


function write_cat_detail()
{
	document.write('<p class="bodyt">Total of '+totCat+' Images in this Category');
}


function write_NImage(cat,ThSize,StartImageNum,totalImages,colCount,colSize,colCount,MaxRowCount)
{

RowCount=0;
lastid=0;
while (StartImageNum < totalImages) {
	getDb(StartImageNum);
	if (RowCount==MaxRowCount){StartImageNum = totalImages};
	if (cat == "All") { dcat = cat };
	if (dcat==cat && StartImageNum<totalImages) 
	{
		id = StartImageNum;
		getDb(StartImageNum++);
		if (colCount++ != colSize) {
			document.write('<td nowrap class="small" align="center" valign="top" width="'+ThSize+'" height="'+ThSize+'">');
			document.write(''+id+'<br><a href="http://www.cambridgereprints.co.uk/display.html?'+id+'">');
			lastid=id;
			CellsonPage--;
			var dalt = ddate+" - "+dtitle;
			document.write('<img border="0" src='+dfile+' width="'+ThSize+'" height="'+ThSize+'" alt="'+dalt+'"><br>'+ddate+'<br>'+dtitle+'</a></td>');
		}else{
			colCount=0;
			RowCount++;
			StartImageNum--;
			document.write('</tr><tr>');
		}
	}else{
	StartImageNum++;
	}
}
if (lastid==0){return false};
tdleft = colSize-colCount;
if (tdleft==colSize){
	if (RowCount<colSize || CellsonPage>0){lastid=0};
	return lastid;
	}
while (tdleft-- != 0) {
	RowCount++;
	document.write('<td><br>&nbsp;</td>');
}
if (RowCount<colSize || CellsonPage>0){lastid=0};
return lastid;
}

function disp_rand_image(rsize)
{
var totalImages = DocDb.length;
var id = Math.round(Math.random()*(totalImages-1));
disp_one_image(rsize,id);
}

function disp_one_image(rsize,id)
{
var totalImages = DocDb.length;
if (id<0  || id>totalImages-1) 
{
document.write('Invalid Print specified');
return false;
}
getDb(id);
var dalt = ddate+" - "+dtitle;
document.write('<a href="http://www.cambridgereprints.co.uk/display.html?'+id+'">');
document.write('<img src="'+dfile+'"border="0" width="'+rsize+'" height="'+rsize+'" alt="'+dalt+'"></a>');
}

//----------------------------------------------------------------
// Display selected Image + information
//----------------------------------------------------------------

function disp_poster()
{
var passed = window.location.search.substring(1);
var totalImages = DocDb.length;
if (passed<0  || passed>totalImages-1) 
{
document.write('Invalid Print specified');
}else{
open_image(passed);
}
}

function open_image(id)
{
getDb(id);
cookieGet("cresreg");
  pid = dcat+"-"+id;
  document.write('<table border="0" width="100%"><tr><td colspan="2" align="center">');
  document.write('<b>'+id+'</b><br><img src="'+dfile+'"border="1"><br>'+dtext+'');
if(dsize == "0"){
	if(dcat == "S1"){
		document.write('<br><a href="http://www.cambridgereprints.co.uk/auto/easy_office.html">More Information</a>');
		}else{
	  document.write('<br>');
	}
	  }else{  
	  document.write('<br>Print size: '+dsize+'');  
	}
  document.write('<br>Price: £'+dprice+'</td></tr>');  
if(usercountry != ""){
  document.write('<tr><td width="25%" align="center">');
  document.write('<form name="paypal" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
  document.write('<input type="hidden" name="cmd" value="_cart">');
  //document.write('<input type="hidden" name="upload" value="1">');
  document.write('<input type="image" src="http://www.cambridgereprints.co.uk/images/x-click-but22.gif" border="0" width="94" height="21" name="SubmitPayPal" onclick="Psub(this.form)" alt="Make payments with PayPal - it\'s fast, free and secure!">');  
  document.write('<input type="hidden" name="add" value="1">');
  //document.write('<input type="hidden" name="display" value="1">');
  document.write('</td><td width="75%" align="left">');
if(usercountry == "UK"){
  document.write('<input type="radio" name="post" value="first" checked> 1st Class Mail - £'+dpost+'<br>');
}
if(usercountry == "EUR"){
	dpost = (dpost*shipmult).toFixed(2);
  document.write('<input type="radio" name="post" value="euairm" checked> Europe AirMail - £'+dpost+'<br>');
}
if(usercountry == "ROW"){
	dpost = (dpost*shipmult*shipmult).toFixed(2);
  document.write('<input type="radio" name="post" value="rowairm" checked> Rest Of World AirMail - £'+dpost+'<br>');
}
if(dsize == "30cm x 21cm"){
  document.write('<input type="checkbox" name="laminate" onclick="get_Lam(this.form)"> Laminate (extra £'+laminat+' per print)');
  }
  document.write('<input type="hidden" name="business" value="paypal@cambridgereprints.co.uk">');
  document.write('<input type="hidden" name="item_name" value="'+dtitle+'">');
  document.write('<input type="hidden" name="item_number" value="'+pid+'">');
  document.write('<input type="hidden" name="amount" value="'+dprice+'">');
  document.write('<input type="hidden" name="quantity" value="1">');  
  //document.write('<input type="hidden" name="no_shipping" value="2">');
  document.write('<input type="hidden" name="currency_code" value="GBP">');
  document.write('<input type="hidden" name="handling_cart" value="'+dpost+'">');
  document.write('<input type="hidden" name="shipping" value="'+shipadd+'">');
  //document.write('<input type="hidden" name="shipping2" value="'+shipadd+'">');
  document.write('<input type="hidden" name="lc" value="GB">');
  document.write('<input type="hidden" name="cpp_headerback_color" value="C0C0FF">');
  document.write('<input type="hidden" name="cpp_headerborder_color" value="ff0000">');
  document.write('<input type="hidden" name="cpp_payflow_color" value="EEEEEE">');
  document.write('<input type="hidden" name="on0" value="'+lamop+'">');
  document.write('</form></td></tr>');
}else{
  document.write('<tr><td colspan="2" align="center">You need to <a href="http://www.cambridgereprints.co.uk/contact.html#register" target="CSMain" onMouseOver="window.status=\'register\'; return true;" onMouseOut="window.status=\' \'; return true;">register</a> with us to purchase this item<br>');
  document.write('</td></tr>');
}
  document.write('<tr><td colspan="2" align="left">');
if(dsize == "30cm x 21cm"){
  document.write('<br>These images are professionally printed on heavy-weight, silk finish card, ready for framing. They can be supplied laminated, giving you a durable, wipe clean poster for your garage, workshop, office or shop.');
  }
  document.write('<center><br><FORM><INPUT TYPE="button" VALUE="Return to previous page" onClick="history.back()"></FORM></center>');
  document.write('</td></tr>');

  document.write('</td></tr></table>');
}

//----------------------------------------------------------------
// Write to pages
//----------------------------------------------------------------

function get_Lam(form){
var price = form.amount.value;
if (form.laminate.checked){
	var LamVal = laminat;
	form.on0.value ="Laminate";
}else{
	var LamVal = 0;
}
var dprice = (price*1)+(LamVal*1);
form.amount.value = dprice;
}

function Psub(form){
//var test1 = form.amount.value;
//var test2 = form.handling_cart.value;
//var test6 = form.shipping.value;
//var test3 = form.shipping2.value;
//var test4 = form.on0.value;
//var test5 = form.item_number.value;
//alert("Price = "+test1+"\nHandling cost = "+test2+"\nShipping cost = "+test6+"\nShip add = "+test3+"\nOptions = "+test4+"\nNumber = "+test5+"");
form.submit();
}


function write_terms()
{
	document.write('<div class="bodyt" id="terms"><h3>Terms &amp; Conditions</h3><h2><u>Satisfaction Guaranteed</u> (refunds)</h2>');
	document.write('We pride ourselves on our satisfied customers, therefore if any of our products or services do not meet or exceed your expectations, we will refund the price of your purchase.');
	document.write('<br>You must notify us within seven days of your purchase and goods must be returned in the same condition as you received them.');
	document.write('<h2><u>Payment</u></h2>The prefered method of payment is by Credit Card, through PayPal.');
	document.write('<br>Your order with a cheque, can be posted to us. Just note the numbers of the posters you require on the <a href="http://www.cambridgereprints.co.uk/order_form.html" target="_blank">Order Form</a>, add the correct postage and send to us.');
	document.write('<br>Bank transfer is also possible for larger orders. Please email or telephone for details.');
	document.write('<h2><u>Postage &amp; Packing</u></h2>Your order will usually be despatched within 3 days of clearance of funds.');
	document.write('<br><b>Prints</b> are carefully packed in heavy-weight, card backed manilla envelopes, marked with "Do Not Bend".');
	document.write('<br>The cost of postage is shown on the description of the item, This is a fixed fee and will remain the same regardless of how many prints you order');
	document.write('<br><b>Note:</b> We cannot be held responsible for loss/damage once posted, if insurance is not taken (email or telephone for details).');
}

function write_rhs_auto_ads()
{
	document.write('<table border="2" cellpadding="3" cellspacing="0" bgcolor="#eeeeff" width="100%"><tr><td align="center" valign="top" class="small">');
	document.write('<iframe src="http://rcm-uk.amazon.co.uk/e/cm?t=successbooksho01&o=2&p=8&l=as1&asins=B0000649LY&=1&fc1=000000&IS2=1&&#108;&#116;1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>');
	document.write('</td></tr><tr><td valign="top" class="small">');

	document.write('<a href="http://www.lambretta.co.uk/page.html" target="_blank" onMouseOver="window.status=\'Cambridge Lambretta\'; return true;" onMouseOut="window.status=\' \'; return true;"><b>Cambridge Lambretta</b></a><br>Cambridge Lambretta Workshops aim to cater for all you Lambretta needs, "from a rivet to a restoration".');
	document.write('</td></tr><tr><td valign="top" class="small">');
	document.write('<a href="http://www.lcgb.co.uk/" target="_blank" onMouseOver="window.status=\'Lambretta Club of Great Britain\'; return true;" onMouseOut="window.status=\' \'; return true;"><b>Lambretta Club of Great Britain</b></a><br>the largest Lambretta club in the world with nearly 2,000 members.');
	document.write('</td></tr><tr><td valign="top" class="small">');
	document.write('<a href="http://www.motorcycle-uk.com/VespaClubBritain.html" target="_blank" onMouseOver="window.status=\'Vespa Club of Britain\'; return true;" onMouseOut="window.status=\' \'; return true;"><b>Vespa Club of Britain</b></a><br>the longest running club of its kind in the United Kingdom.');
	document.write('</td></tr><tr><td valign="top" class="small">');
	document.write('<a href="http://www.motorcycle.co.uk/" target="_blank" onMouseOver="window.status=\'Motorcycle Co UK\'; return true;" onMouseOut="window.status=\' \'; return true;"><b>Motorcycle Co UK</b></a><br>list motorcycle and scooter parts, insurance, clothing, gifts, dealers, accessories, clubs...');
	document.write('</td></tr><tr><td valign="top" class="small">');
	document.write('<a href="http://www.fordanglia105eownersclub.co.uk/" target="_blank" onMouseOver="window.status=\'Ford Anglia Owners Club\'; return true;" onMouseOut="window.status=\' \'; return true;"><b>Ford Anglia Owners Club</b></a><br>formed to cater for all owners and enthusiasts of the Anglia range, in an effort to help preserve and maintain these vehicles.');
	document.write(' </td></tr></table>');
}


