if(document.location.href == 'http://www.btplc.com/paralympicsgb/' || document.location.href == 'http://www.btplc.com/paralympicsgb/index.htm')
{
	document.location.href = 'https://www.btplc.com/paralympicsgb/'; 
}

	var clicks=0;
	var FORM_ID='BPA08';
	var EMAIL_REQ = new Boolean(true) ;
		
    function gup( name )
    {  
       name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
       var regexS = "[\\?&]"+name+"=([^&#]*)";  
       var regex = new RegExp( regexS );  
       var results = regex.exec( window.location.href );  
       if( results == null )    
           return "";  
       else     
           return results[1];
    }

	function show_help()
	{
		var win = null;
		win=window.open('/security_code.html',"Help",'status=no,scrollbars=no,resizable=no,width=420,height=310');
	}

	function resetAmounts(value)
	{
		if (value > 0 )
			document.ccform.p_amount.value = value
		else
			document.ccform.p_amount.value = 0;
	}

	function trimAll(str)
	{
		size = str.length
		while (str.slice(0,1) == " ") //Strip leading spaces
		{
			str = str.substr(1,size-1);
			size = str.length;
		}
		while(str.slice(size-1,size)== " ") //Strip trailing spaces
		{
			str = str.substr(0,size-1);
			size = str.length;
		}
		return str;
	}
	
	function postit(pCodeString)
	{ //check postcode format is valid
		test = pCodeString.value;
		size = test.length;
		test = test.toUpperCase(); //Change to uppercase
		while (test.slice(0,1) == " ") //Strip leading spaces
	  	{
	  		test = test.substr(1,size-1);size = test.length
		}
		while(test.slice(size-1,size)== " ") //Strip trailing spaces
	  	{
	  		test = test.substr(0,size-1);size = test.length
		}
		if(test.slice(0,4) != "BFPO")
		{
			document.forms[0].p_postcode.value = test; //write back to form field
			if (size < 6 || size > 8)
			{ //Code length rule
				return (test + " is not a valid postcode - wrong length");
				document.details.pcode.focus();
				return false;
			}
			if (!(isNaN(test.charAt(0))))
			{ //leftmost character must be alpha character rule
				return(test + " is not a valid postcode - cannot start with a number");
				document.details.pcode.focus();
				return false;
			}
			if (isNaN(test.charAt(size-3)))
			{ //first character of inward code must be numeric rule	
				return(test + " is not a valid postcode - alpha character in wrong position");
				document.details.pcode.focus();
				return false;
			}
			if (!(isNaN(test.charAt(size-2))))
			{ //second character of inward code must be alpha rule
				return(test + " is not a valid postcode - number in wrong position");
				document.details.pcode.focus();
				return false;
			}
			if (!(isNaN(test.charAt(size-1))))
			{ //third character of inward code must be alpha rule
				return(test + " is not a valid postcode - number in wrong position");
				document.details.pcode.focus();
				return false;
			}
			if (!(test.charAt(size-4) == " "))
			{//space in position length-3 rule
				return(test + " is not a valid postcode - no space or space in wrong position");
				document.details.pcode.focus();
				return false;
			}
			count1 = test.indexOf(" ");
			count2 = test.lastIndexOf(" ");
			if (count1 != count2)
			{ //only one space rule
				return(test + " is not a valid postcode - only one space allowed");
				document.details.pcode.focus();
				return false;
			}
		}
		return "";
	}

function checkEmail(mailString)
{
	var newstr = "";
	var at = false;
	var dot = false;
	if (mailString.length != 0 )
	{
		if (mailString.indexOf("@") != -1)
   		{
      		at = true;
    	}
    	else if (mailString.indexOf(".") != -1)
    	{
    	  dot = true;
    	}
      	for (var i = 0; i < mailString.length; i++)
      	{
       	ch = mailString.substring(i, i + 1)
        	if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9"))
          {
          		newstr += ch;
				if (ch == "@")
				{
              	at=true;
				}
				if (ch == ".")
				{
					dot=true;
				}
			}
		}
		if ((at == true) && (dot == true))
       	return true;
    	else
      		return false;
	}
	else return false;
}

function luhnCheck(CardNumberString)
{
	//var argv = luhnCheck.arguments;
	//var argc = luhnCheck.arguments.length;
	//already validated all digits
	var no_digit = CardNumberString.length;
	var oddoeven = no_digit & 1;
	var sum = 0;

	for (var count = 0; count < no_digit; count++)
	{
		var digit = parseInt(CardNumberString.charAt(count));
		if (!((count & 1) ^ oddoeven))
		{
			digit *= 2;
			if (digit > 9)
				digit -= 9;
		}
		sum += digit;
	}
	if (sum % 10 == 0)
		return true;
	else
		return false;
}

function validateForm()
{

	var expSelectFlag = new Boolean(true);
	var thisDate = new Date();
	var thisYear = parseInt(thisDate.getYear(),10);

	if(thisYear < 2000)
		thisYear = 1900 + thisYear;
	var thisMonth = parseInt(thisDate.getMonth() + 1,10); //Month index starts at zero
	var strErr = '';
	var test1;
	var errorFocus=-1;
	with (window.document.ccform)
	{
		// Credit card verification
		var carderr = 0;
		var cardno = '';
		var clength = 0;
		p_card_no.value=trimAll(p_card_no.value);
		cardno = p_card_no.value;

		var cardtype = p_card_type[p_card_type.selectedIndex].value;
		var carddigits = "";

		//************
		if(p_amount.value.length < 1)
		{
			strErr = strErr + '\n\t- Donation amount is blank ';
			if (errorFocus ==-1)
			{
				p_amount.focus();
				errorFocus=0;
			}
		}
		else
		{
			if(parseInt(p_amount.value*100,10)!=p_amount.value*100 )
			{
				strErr = strErr + '\n\t- Donation amount is invalid (pounds/pence only)';
				if (errorFocus ==-1)
				{
					p_amount.focus();
					errorFocus=0;
				}
			}
			else
			{
				if(p_amount.value < 1)
				{
					strErr = strErr + '\n\t- The minimum donation amount is £1(ukp)';
					if (errorFocus ==-1)
					{
						p_amount.focus();
						errorFocus=0;
					}
				}
				if (p_amount.value >= 500)
				{
					if ( ! window.confirm('Are you sure you want to donate £' + p_amount.value) )
					{
						window.alert('Please correct the amount and click "Send Donation" again');
						p_amount.focus();
						return false;
					}
				}
			}
		}
		if (p_gift_aid.value == 'Choose')
		{
			if (errorFocus ==-1)
			{
				p_gift_aid.focus();
				errorFocus=0;
			}
			strErr = strErr + '\n\t- Please select Yes or No for Gift Aid';
		}
		if(p_initial.value.length < 1)
		{
			if (errorFocus ==-1)
			{
				p_initial.focus();
				errorFocus=0;
			}
			strErr = strErr + '\n\t- Donor initial is blank';
		}
		if(p_surname.value.length < 1)
		{
			if (errorFocus ==-1)
			{
				p_surname.focus();
				errorFocus=0;
			}
			strErr = strErr + '\n\t- Donor surname is blank';
		}
		if(p_name_on_card.value.length < 1)
		{
			strErr = strErr + '\n\t- Name on card is blank';
			if (errorFocus ==-1)
			{
				p_name_on_card.focus();
				errorFocus=0;
			}
		}

		// *********
		for( var i = 0; i < cardno.length; ++i )
		{		// make sure the number is all digits.. (by design)
			var c = cardno.charAt(i);
			if( c >= '0' && c <= '9' )
			{
				carddigits = carddigits + c;
				clength = clength + 1;
			}
			else
			{
				if(c != ' ')
				{
					carderr = 1;
				}
			}
		}
		if( p_card_type[p_card_type.selectedIndex].value =='jcb')
		{
			var card_prefix = parseInt( carddigits.substring(0,6));
			// if( card_prefix != 564193 || clength !=16)
			//	strErr = strErr + '\n\t- Please enter a valid JCB number';
		}
		else
		{
			if(carderr != 0  || !luhnCheck(carddigits))
			{
				strErr = strErr + '\n\t- Please enter a valid card number';
			}
			else
			{
				if( p_card_type[p_card_type.selectedIndex].value =='amex')
				{
					strErr = strErr + '\n\t- American Express Cards are not accepted';
				}
				if( p_card_type[p_card_type.selectedIndex].value =='mastercard')
				{
					if( clength != 16 )
						strErr = strErr + '\n\t- Please enter a valid MasterCard number';
					else
					{
						var card_prefix = parseInt( carddigits.substring(0,2));
						if( card_prefix < 51 || card_prefix > 55)
							strErr = strErr + '\n\t- Please enter a valid MasterCard number';
					}
				}
				if( p_card_type[p_card_type.selectedIndex].value =='visa')
				{
					if( clength != 16 && clength != 13 )
						strErr = strErr + '\n\t- Please enter a valid Visa Card number.';
					else
					{
						var firstchar = carddigits.substring(0,1)
						if( firstchar != '4' )
							strErr = strErr + '\n\t- Please enter a valid Visa Card Number';
					}
				}
				if( p_card_type[p_card_type.selectedIndex].value =='delta')
				{
					if( clength != 16 && clength != 13 )
						strErr = strErr + '\n\t- Please enter a valid Delta Card number';
					else
					{
						var firstchar = carddigits.substring(0,1);
						if( firstchar != '4' )
							strErr = strErr + '\n\t- Please enter a valid Delta Card number';
					}
				}
				if( p_card_type[p_card_type.selectedIndex].value =='caf')
				{
					var card_prefix = carddigits.substring(0,6);
					if( card_prefix != '564193')
						strErr = strErr + '\n\t- Please enter a valid CAF number';
				}
				if( (p_card_type[p_card_type.selectedIndex].value =='switch') ||
                                    (p_card_type[p_card_type.selectedIndex].value =='solo') )
				{
					if(( clength != 16 ) && ( clength != 18 ) && ( clength != 19 ))
						strErr = strErr + '\n\t- Please enter a valid Switch/Solo Card number';
				}
				if( p_card_type[p_card_type.selectedIndex].value =='maestro')
				{
					var card_prefix = parseInt( carddigits.substring(0,6));
					if( ( clength != 16 ) && ( clength != 18 ) && ( clength != 19 ) )
						strErr = strErr + '\n\t- Please enter a valid Maestro number';
					if ( p_issue_no.value.length == 0 && card_prefix == '675940' )
						strErr = strErr + '\n\t- Please enter the cards issue number';
				}
			}
			if(strErr !='' && errorFocus ==-1)
			{
				p_card_no.focus();
				errorFocus=0;
			}
		}
		if(p_card_no.value.length < 1)
		{
			strErr = strErr + '\n\t- Card number is blank';
			if (errorFocus ==-1)
			{
				p_name_on_card.focus();
				errorFocus=0;
			}
		}
		if (p_card_security_code.value.length > 0)
		{
			if(parseInt(p_card_security_code.value,10)!=p_card_security_code.value )
			{
				strErr = strErr + '\n\t- Card Security Code is invalid (digits only)';
				if (errorFocus ==-1)
				{
					p_card_security_code.focus();
					errorFocus=0;
				}
			}
		}
		if (p_exp_month.selectedIndex == 0)
		{
			strErr = strErr + '\n\t- Select a card expiry date (Month)';
			expSelectFlag = false;
			if (errorFocus ==-1)
			{
				p_exp_month.focus();
				errorFocus=0;
			}
		}
		if (p_exp_year.selectedIndex == 0)
		{
			strErr = strErr + '\n\t- Select a card expiry date (Year)';
			expSelectFlag = false;
			if (errorFocus ==-1)
			{
				p_exp_year.focus();
				errorFocus=0;
			}
		}
		if(p_address_1.value.length < 1 )
		{
			strErr = strErr + '\n\t- Card holders address is blank';
			if (errorFocus ==-1)
			{
				p_address_1.focus();
				errorFocus=0;
			}
		}
		// Check valid date > today
		var TMonth = parseInt(p_exp_month.options[p_exp_month.selectedIndex].value,10);
			var TYear = parseInt(p_exp_year.options[p_exp_year.selectedIndex].value,10);
		//RUPEHS CardExpiry.value = p_exp_month.options[p_exp_month.selectedIndex].value + "/" + p_exp_year.options[p_exp_year.selectedIndex].value;
		if	((TYear < thisYear) || ((TYear == thisYear) && (TMonth < thisMonth))	)
		{
			if(expSelectFlag == true) //prevent dual card expiry error
			{
				strErr = strErr + '\n\t- Your card has expired';
				if (errorFocus ==-1)
				{
					p_exp_year.focus();
					errorFocus=0;
				}
			}
		}
		if( p_card_type[p_card_type.selectedIndex].value =='maestro')
		{
			p_issue_no.value=trimAll(p_issue_no.value);
			if(p_issue_no.value == '' && strErr == '')
			{
				if(!confirm("You haven't entered isssue number. Press Ok to make donation. Press Cancel to enter an issue number."))
				{
				   p_issue_no.focus();
				   return false;
				}
			}
			else
			{
			   for (i = 0; i < p_issue_no.value.length;i++ )
		   		   if(p_issue_no.value.charAt(i) <"0" || p_issue_no.value.charAt(i)>"9")
					{
						strErr = strErr + '\n\t- Please enter a valid Switch Issue number ';
						p_issue_no.focus();
						break;
			   		}
			}
		}
		// Post code validation if UK based
		if (p_country[p_country.selectedIndex].value=='England' ||
			 p_country[p_country.selectedIndex].value=='Wales'   ||
			 p_country[p_country.selectedIndex].value=='Scotland' ||
			 p_country[p_country.selectedIndex].value=='Northern Ireland' ||
			 p_country[p_country.selectedIndex].value=='Channel Isles'||
			 p_country[p_country.selectedIndex].value=='Isle of Man')
		{
			if(p_postcode.value.length < 1)
			{
				strErr = strErr + '\n\t- Card holders postcode is blank';
				if (errorFocus ==-1)
				{
					p_postcode.focus();
					errorFocus=0;
				}
			}		
			else
			{			
				test= postit(p_postcode);
				if(test!="")
					strErr = strErr + '\n\t -'+ test;
				if (errorFocus ==-1)
				{
					p_postcode.focus();
					errorFocus=0;
				}
			}
		}
		if(p_email_addr.value.length > 0)
		{
			var bool = null;
			var str = p_email_addr.value;
			if(!checkEmail(str))
			{
				if (errorFocus ==-1)
				{
					p_email_addr.focus();
					errorFocus=0;
				}
				strErr = strErr + '\n\t- Your Email address is invalid';
			}
			/*
			if(str != email_addr_confirm.value)
			{
				strErr = strErr + '\n\t- Your confirmed Email address is invalid';
			}
			email_addr_confirm = undefined;
			*/
		}
		else if ( EMAIL_REQ )
		{
			strErr = strErr + '\n\t- You must enter your eMail address.';
			if (errorFocus ==-1)
			{
				p_email_addr.focus();
				errorFocus=0;
			}
		}

		
		/*
		if ( p_whereheard.value == 'NotSpecified' )
		{
			strErr = strErr + '\n\t- Where you found out about donating is not set';
			if (errorFocus ==-1)
			{
				p_whereheard.focus();
				errorFocus=0;
			}
		}
		if ( p_newsletter.checked == false )
			p_newsletter.value='Y'
		else
			p_newsletter.value='N';
		if ( p_email_req.checked == false )
			p_email_req.value='Y'
		else
			p_email_req.value='N';
			*/
		if (strErr != '')
		{
			strErr = 'Sorry, but for your donation to be processed, you must correct the following :' + strErr;
			alert(strErr);
			return false;
		}
		else
		{
			p_platform_id.value=FORM_ID; //THIS ID WILL evaluate JS IS OK
			return true;
		}
	}
	return false;
}

	function check_clicks()
	{
		if (clicks=='0')
		{
			clicks='1';
	  		var v_resp = validateForm();
	  		if (!v_resp)
	  			clicks=0;
			return v_resp;
		}
		else
	        alert('Your request is being processed........please be patient......');
		return false;
	}
