// //////////////////////////////////////////////////////////////////////////////

// Keepers Nursery client-side scripts

// //////////////////////////////////////////////////////////////////////////////


function sessionCookieTest () {

  if (testSessionCookie()) {
    return true;
  }
  else {
	location.href='cookies-disabled.htm';
    return false;
  }
}  

function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
    return true 
  else
    return false;
}


function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}


/* Checks input on the credit card payment section on the order status update and main checkout pages */
function orderstatus_check_input(f)

{
var result;
result=true;
var thisdelivery;

var cardfrom, cardto;


// Set the value of delivery.value - differs depending on calling from order amendment or checkout
if (f.calledfrom.value == "orderamendment")
	{

	if (f.delivery[0].checked)
			thisdelivery=0;
		else
			thisdelivery=1;
	}
else
	{
	thisdelivery = (f.delivery.value);
	}



// If there is a delivery charge, we need to check the delivery address ...
if (thisdelivery == 0 )
	{

// Get the outcode - the first 1 or 2 character part of the postcode
// Remove all spaces
// If 2nd character is numeric, remove it

	var outcode=replace(f.deliverypostalcode.value,' ','').substring(0,2).toUpperCase();
	var prefix=f.deliverypostalcode.value.substring(0,f.deliverypostalcode.value.indexOf(' ')).toUpperCase();
	var notallowed="BT,IM,JE,GY";
	var allowed1="B,E,G,L,M,N,S,W";
	var allowed2="AB,AL,BA,BB,BD,BH,BL,BN,BR,BS,BT,CA,CB,CF,CH,CM,CO,CR,CT,CV,CW,DA,DD,DE,DG,DH,DL,DN,DT,DY,EC,EH,EN,EX,FK,FY,GL,GU,GY,HA,HD,HG,HP,HR,HS,HU,HX,IG,IM,IP,IV,JE,KA,KT,KW,KY,LA,LD,LE,LL,LN,LS,LU,ME,MK,ML,NE,NG,NN,NP,NR,NW,OL,OX,PA,PE,PH,PL,PO,PR,RG,RH,RM,SA,SE,SG,SK,SL,SM,SN,SO,SP,SR,SS,ST,SW,SY,TA,TD,TF,TN,TQ,TR,TS,TW,UB,WA,WC,WD,WF,WN,WR,WS,WV,YO,ZE";
	var surchargeareas3="AB4,AB5,IV1,IV2,IV3,IV4,IV5,IV6,IV7,IV8,IV9,KW1,KW2,KW3,KW4,KW5,KW6,KW7,KW8,KW9,G84"
	var surchargeareas4="AB31,AB33.AB35,AB36,AB37,AB38,AB44,AB45,AB51,AB52,AB53,AB54,AB55,AB56,FK18,FK19,FK20,FK21,IV10,IV11,IV12,IV13,IV14,IV15,IV16,IV17,IV18,IV19,IV20,IV21,IV22,IV23,IV24,IV25,IV26,IV27,IV28,IV30,IV31,IV32,IV33,IV34,IV35,IV36,IV40,IV44,IV47,IV51,IV52,IV53,IV54,IV55,IV63,KA27,KA28,KW10,KW11,KW12,KW13,KW14,KW15,KW17,PA20,PA21,PA22,PA23,PA24,PA25,PA26,PA27,PA28,PA29,PA30,PA31,PA32,PA33,PA34,PA35,PA36,PA37,PA38,PA39,PA40,PA41,PA42,PH19,PH20,PH21,PH22,PH24,PH25,PH26,PH29,PH30,PH31,PH32,PH33,PH34,PH35,PH36,PH37,PH38,PH39,PH40,PH41,PH49,PH50,PO30,PO31,PO32,PO33,PO38,TR21,TR22,TR23,TR24,TR25"
	var surchargeareas1="G84";
	var surchargeareas="";

	var outcodeallowed=true;
	
	if (isNaN(outcode.substr(1,1)) == false)
		outcode=outcode.substr(0,1);
		

	if (f.deliverypostalcode.value == "" )
		{
		alert('Please enter the delivery postcode ');
		f.deliverypostalcode.focus();
		result= false;
		}

// Check for postcode being in the not allowed list (which are all 2-character outcodes)
	if (outcode.length==2 && notallowed.indexOf(outcode) != -1)
		{
		alert('Sorry, we cannot deliver to Northern Ireland, Isle of Man, Guernsey, or Jersey because of Plant Health restrictions');
		f.deliverypostalcode.focus();
		result= false;
		}
	

//	Check for outcode being in the allowed lists
	if ( outcode.length==2 && allowed2.indexOf(outcode) == -1)
		outcodeallowed = false;

	if (outcode.length==1  && allowed1.indexOf(outcode) == -1)
		outcodeallowed = false;


	if (outcodeallowed == false)
		{
		alert('Please enter a valid UK delivery postcode\nPlease also note that we do not export outside the UK - your delivery address must have a valid UK postcode for the order to be accepted');
		f.deliverypostalcode.focus();
		result= false;
		}

	}


if (result == true)
	{

// If the payment method is card
	if (f.paymentmethod.selectedIndex == 0)
		{
			if  (f.balancedue.value > 0 ) 
				{

				if (f.paymenttype.value == 0) 
					{
					alert('Please select your credit or debit card type');
					f.paymenttype.focus();
					result= false;
					}

				else if (f.cardnumber.value == "") 
					{
					alert('Please enter the card number');
					f.cardnumber.focus();
					result= false;
					}

				else if   (f.cardholder.value == "") 
					{
					alert('Please enter the card holder name');
					f.cardholder.focus();
					result= false;
					}

				else if (f.cardsecurityno.value == "") 
					{
					alert('Please enter the 3-digit security number, this is the last 3 digits of the number printed on the signature strip on the back of your credit card');
					f.cardsecurityno.focus();
					result= false;
					}

				else if  (f.cardexpiry.value == 0) 
					{
					alert('Please enter the card expiry date');
					f.cardexpiry.focus();
					result= false;
					}

				}
		}
	}


// Provide information to the customer if there is or was a delivery surcharge
//	We don't do this if there are other validation problems to deal with first though
if (result)
	{

	if (prefix.length == 3)
			surchargeareas=surchargeareas3;
		else if (prefix.length == 1)
			surchargeareas=surchargeareas1;
		else
			surchargeareas=surchargeareas4;
// Customer has a surcharge delivery post code, and current value of post code in surcharge area - so don't do anything
// Neither current value of delivery post code or original value have surcharges - so don't do anything

// Customer has changed the delivery post code to an area which does not have a surcharge
	if (f.originaldeliverysurcharge.value != 0 && surchargeareas.indexOf(prefix) == -1)
		alert('The delivery surcharge shown at the checkout no longer applies and will not be charged');

// Customer does not have a surcharge, but has changed delivery postcode to a surcharge one
	if (f.originaldeliverysurcharge.value == 0 && surchargeareas.indexOf(prefix) != -1)
		{
		result = confirm('This postcode attracts a delivery surcharge which will be added to the delivery charge shown above. If you wish to see the amount of the delivery charge please click Cancel and then the Delivery Charge link in the menubar at the bottom of the page. Please click OK to proceed with the order or Cancel not to proceed.');
		}

	
	}


if (!result)
	{
	if ( f.calledfrom.value == "orderamendment")
		f.action="orderstatus2.aspx";
	}
	
return result;
}





function helpinfo(info)
{
	var mywin=window.open( 'helpinfo.aspx?info=' + info, 'helpinfo','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=450,height=320' );
	mywin.focus();
}

function helpglossary(glossary)
{
	var mywin=window.open( 'helpglossary.aspx?glossaryterm=' + glossary, 'glossary','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=450,height=320' );
	mywin.focus();
}

function quickinfo(variety)
{
	var WinID = 0
	if (variety.charCodeAt)
		for(iLoop=0;iLoop<variety.length;iLoop++) 
			WinID += variety.charCodeAt(iLoop); 
	var mywin=window.open( 'quickinfo.aspx?variety=' + variety, WinID, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=450,height=320' );
	mywin.focus();
}

function rootstocks(fruittype)
{
	var mywin=window.open( 'rootstocks.aspx?fruittype=' + fruittype, 'rootstocks', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=450,height=320' );
	mywin.focus();
	
}

function sendfriend(id,v)
{
	var mywin=window.open( 'sendfriend.aspx?id=' + id + '&v=' + v, 'sendfriend', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=550,height=470' );
	mywin.focus();
	
}

function check_savebasket(f)
	{
	if ( (f.newcustomername.value=="") || (f.newcustomeremail.value=="") )
			{
			alert('Please enter your name and email address to save the shopping basket');
			return false;
			}
		else
			return true;
	}


function login_check_input(f)
	{
	if  (f.email.value=="")
			{
			alert('Please enter your email address');
			return false;
			}
	if  (f.password.value=="")   
			{
			alert('Please enter your password');
			return false;
			}
		else
			return true;
	
	}


function login_sendemail(f)
	{

	if  (f.email.value=="")
			{
			alert('Please enter your email address');
			return false;
			}

		else
			{
			f.action.value=3;
			f.submit();
			return true;
			}
		
		
	}


function login_passwordreminder(f)
{
	if  (f.email.value=="")
			{
			alert('Please enter your email address first');
			return false;
			}

		else
			{
			var mywin=window.open( 'passwordreminder.aspx?email=' + f.email.value, 'reminder','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=350,height=130' );
			mywin.focus();
			return true;
			}
}


function customerinfo_check_input(f)
	{
	if ( (f.firstname.value=="") || (f.lastname.value=="") )
			{
			alert('Please enter your name');
			return false;
			}

	if  ( f.addressline1.value == "" )  
			{
			alert('Please enter your address');
			return false;
			}



	if (f.city.value == "") {
	    alert('Please enter your postal city / town');
	    return false;
    	}






	if  ( f.postalcode.value == "" )  
			{
			alert('Please enter your postcode');
			return false;
			}

	if  ( (f.email.value=="") || (f.password2.value=="")  )
			{
			alert('Please enter and confirm your email address');
			return false;
			}

	if  ( f.email.value != f.email2.value )  
			{
			alert('Please enter your email address again and confirm it');
			return false;
			}


	if ( (f.password.value=="") || (f.password2.value=="")  )
			{
			alert('Please enter and confirm your password');
			return false;
			}

	if  ( f.password.value != f.password2.value )  
			{
			alert('Please enter your password again and confirm it');
			return false;
			}

		else
			return true;
		
		
	}


function search_resetdata(f)
	{
		f.firstdate.value="";
		f.firstdate2.value="";
		f.fruitsize.selectedIndex=0;
		f.colour.selectedIndex=0;
		f.seasoncatg.selectedIndex=0;
		f.uses.selectedIndex=0;
		f.polgroup.selectedIndex=0;
		f.selfcompat.selectedIndex=0;
		f.triploid.selectedIndex=0;
		f.vigour.selectedIndex=0;
		f.habit.selectedIndex=0;
		f.bearer.selectedIndex=0;
		for (var i=0;i < f.specials.length;i++)
			{			f.specials[i].checked=false;			}
			for (var i=0;i < f.conditions.length;i++)
			{			f.conditions[i].checked=false;			}
		for (var i=0;i < f.diseases.length;i++)
			{			f.diseases[i].checked=false;			}
		return false;
	}



function replace(string,text,by)
	{
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
   }
    