// //////////////////////////////////////////////////////////////////////////////

// Keepers Nursery client-side scripts

// //////////////////////////////////////////////////////////////////////////////





function ValidateCheckoutBillingAddress(f) {

    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;
    }

    else
        return true;


}



function ValidateCheckoutDeliveryAddress(f) {

    var result = true;
    var copydelivery = 0;
    for (i = 0; i < f.copydeliveryaddress.length; i++) {
        if (f.copydeliveryaddress[i].checked == true) {
            copydelivery = f.copydeliveryaddress[i].value;
            break;
        }
    }
    if (copydelivery == 1)
        {
            if (f.deliverylastname.value == "") {
                alert('Please enter the name and address for delivery');
                result = false;
                return false;
            }

            if (f.deliveryaddressline1.value == "") {
                alert('Please enter a delivery address');
                result = false;
                return false;
            }

            if (f.deliverycity.value == "") {
                alert('Please enter the town / city of the delivery address');
                result = false;
                return false;
            }
        }

    if (copydelivery == 1 ) {

        // 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;
        }

    }
    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.actionx.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.email.value == "") || (f.email2.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;
   }

   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;
   }


