var MaxOK = true;
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function radPoption()
{
	var f = document.frmBasket;
	var paymentType = $('input[name=rdoPoption]:checked', '#frmBasket').val();
	var grandTotal = $('#txtGrandTotal').val();

	$('#payCC').hide();
	$('#payCheq').hide();
	$('#payTrans').hide();
	$('#payPaypal').hide();

	f.cpoption.value = paymentType;
	
	var postData = 'PaymentType=' + paymentType+ '&GrandTotal=' + grandTotal;
	
	
	$('#divUpdatingPaymentOptions').show();

	$.post(securebasehref + 'PaymentTypeChanged.cfm', postData, function(data){
		switch (paymentType){
			case '1':{
				$('#payCC').show();
				break;
			}
			case '2':{
				$('#payCheq').show();
				break;
			}
			case '3':{
				$('#payTrans').show();
				break;
			}
			case '4':{
				$('#payPaypal').show();
				// $surchargeLabel.show();
				// $surchargeDiv.show();
				break;
			}
		}
		
		$('#paymentTypeSurcharge').show();
		$('#paymentTypeSurcharge').html(data);
		$('#divUpdatingPaymentOptions').hide();
	});
}

function isInteger(s)
{
	var pattern = /^[0-9]+$/i;
	return pattern.test(s);
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1)
			returnString += c;
	}
	return returnString;
}
function daysInFebruary(year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n)
{
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31
			if (i == 4 || i == 6 || i == 9 || i == 11)
			{
				this[i] = 30
			}
			if (i == 2)
			{
				this[i] = 29
			}
	}
	return this
}
function isDate(dtStr)
{
	var dtCh = "/";
	var minYear = 1900;
	var maxYear = 2100;
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
	var strDay = dtStr.substring(0, pos1);
	var strMonth = dtStr.substring(pos1 + 1, pos2);
	var strYear = dtStr.substring(pos2 + 1)
		strYr = strYear;
	if (strDay.charAt(0) == "0" && strDay.length > 1)
		strDay = strDay.substring(1)
			if (strMonth.charAt(0) == "0" && strMonth.length > 1)
				strMonth = strMonth.substring(1)
					for (var i = 1; i <= 3; i++)
					{
						if (strYr.charAt(0) == "0" && strYr.length > 1)
							strYr = strYr.substring(1)
					}
					month = parseInt(strMonth);
			day = parseInt(strDay);
	year = parseInt(strYr);
	if (pos1 == -1 || pos2 == -1)
	{
		return false
	}
	if (month < 1 || month > 12)
	{
		return false
	}
	if (day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
	{
		return false
	}
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
	{
		return false
	}
	if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
	{
		return false
	}
	return true
}

function UseAbove()
{
	var f = document.frmBasket
	if (f.chk.checked)
	{
		f.ShipAdd1.value = f.Add1.value
			f.ShipAdd2.value = f.Add2.value
			f.ShipAdd3.value = f.Add3.value
			f.ShipAdd4.value = ""
			f.ShipCustName.value = f.FirstName.value + ' ' + f.LastName.value
			f.ShipTown.value = f.Town.value
			f.ShipCounty.value = f.County.value
			f.ShipPostcode.value = f.Postcode.value
			f.contacttel.value = f.Tel.value
	}
	else
	{
		f.ShipAdd1.value = ""
			f.ShipAdd2.value = ""
			f.ShipAdd3.value = ""
			f.ShipAdd4.value = ""
			f.ShipTown.value = ""
			f.ShipCounty.value = ""
			f.ShipCustName.value = ""
			f.ShipPostcode.value = ""
			f.contacttel.value = ""
	}
}

function UseAbove2()
{
	var f = document.frmBasket;
	if (f.chk2.checked)
	{
		f.InvCustName.value = f.FirstName.value + ' ' + f.LastName.value
	}
	else
	{
		f.InvCustName.value = ""
	}
}

function checkInternationalPhone(strPhone)
{
	var bracket=3
	strPhone= $.trim(strPhone);
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkCountryRules(roiID)
{
	if($('#CountryID').val() == roiID)
	{
		$('#Postcode').val() = '000';
		$('#PostCodeRow').hide();
	}
	else
		$('#PostCodeRow').show();
}

function BuyBasket()
{
	var f = document.frmBasket;
	var msg = "";
	var work;
	var dmark = 0;
	var frommark = 0;
	var now = new Date();
	var tmp = 0;
	
	now.setFullYear(now.getFullYear(), now.getMonth(), 1);
	
	var nowmonth = now.getMonth() + 1;
	var nowyear = now.getFullYear();
	if (nowmonth < 10)
	{
		nowmonth = '0' + nowmonth
	}
	
	var nowyymm = "01/" + nowmonth + "/" + nowyear;
	
	var workfrom = new Date;
	workfrom.setDate(1);
	var workexp = new Date;
	workexp.setDate(1);
	
	if (f.FirstName.value == "")
	{
		msg += "\n - first name."
	}
	if (f.LastName.value == "")
	{
		msg += "\n - surname."
	}
	
	if (f.Add1.value == "" && f.Add2.value == "" && f.Add3.value == "")
	{
		msg += "\n - address."
	}
	if (f.Town.value == "")
	{
		msg += "\n - town name."
	}
	
	if (f.Postcode.value == "" &&
		f.ShipCountry.options &&
		f.ShipCountry.options[f.ShipCountry.selectedIndex].value != ROICountryId)
	{
		msg += "\n - postcode."
	}
	
	if (f.Tel.value == "")
	{
		msg += "\n - telephone number."
	}
	else if (!checkInternationalPhone(f.Tel.value))
	{
		msg += "\n - telephone number is invalid, it must be at least 10 digits and only contain numbers, spaces or the characters ()-+"
	}
	
	if (f.Email.value == "")
	{
		msg += "\n - email address."
	}
	if (f.Email.value != f.RepEmail.value)
	{
		msg += "\n - email addresses differ"
	}
	
	// if(f.Country.value=="") { msg+="\n - address country." }
	if (document.getElementById('CountryID').selectedIndex < 1)
	{
		msg += "\n - address country."
	}
	
	//if(f.contacttel.value=="") { msg+="\n - contact telephone." }
	if (f.rdoPoption[0].checked)
	{
		if (f.CCValidDate_M.value.length == 1)
		{
			f.CCValidDate_M.value = '0' + f.CCValidDate_M.value;
		}
		if (f.CCExpDate_M.value.length == 1)
		{
			f.CCExpDate_M.value = '0' + f.CCExpDate_M.value;
		}
		if (f.CCValidDate_Y.value.length == 1)
		{
			f.CCValidDate_Y.value = '0' + f.CCValidDate_Y.value;
		}
		if (f.CCExpDate_Y.value.length == 1)
		{
			f.CCExpDate_Y.value = '0' + f.CCExpDate_Y.value;
		}
		
		f.CardExpiry.value = f.CCExpDate_M.value + "/20" + f.CCExpDate_Y.value;
		f.CardStartDate.value = f.CCValidDate_M.value + "/20" + f.CCValidDate_Y.value;
		if (f.CardStartDate.value == "/")
		{
			f.CardStartDate.value = ""
		}
		if (f.InvCustName.value == "")
		{
			msg += "\n - card holder name."
		}
		
		if (f.CCExpDate_M.value == "")
		{
			msg += "\n - month for expiry date.";
			dmark = 1;
		}
		else
		{
			if (isInteger(f.CCExpDate_M.value) == false)
			{
				msg += "\n -  expiry date month should be numeric";
				dmark = 1;
			}
		}
		if (f.CCExpDate_Y.value == "")
		{
			msg += "\n - year for expiry date.";
			dmark = 1;
		}
		else
		{
			if (isInteger(f.CCExpDate_Y.value) == false)
			{
				msg += "\n -  expiry date year should be numeric";
				dmark = 1;
			}
		}
		if (f.CCValidDate_M.value == "")
		{
			frommark = 1;
		}
		else
		{
			if (isInteger(f.CCValidDate_M.value) == false)
			{
				msg += "\n -  from date month should be numeric";
				dmark = 1;
			}
		}
		if (f.CCValidDate_Y.value == "")
		{
			frommark = 1;
		}
		else
		{
			if (isInteger(f.CCValidDate_Y.value) == false)
			{
				msg += "\n -  from date year should be numeric";
				dmark = 1;
			}
		}
		
		if (f.CCType.selectedIndex == 0)
		{
			msg += "\n - you have not chosen a card type."
		}
		
		work = "01/" + f.CardExpiry.value;
		if (work.length > 4)
		{
			if (isDate(work) == false)
			{
				msg += "\n - expiry date invalid (mm/yy)";
				dmark = 1;
			}
		}
		if (frommark == 0)
		{
			work = "01/" + f.CardStartDate.value;
			if (work.length > 4)
			{
				if (isDate(work) == false)
				{
					msg += "\n - from date invalid (mm/yy)";
					dmark = 1;
				}
			}
		}
		if (dmark == 0)
		{
			workfrom.setFullYear(1900, 0, 1);
			
			if (frommark == 0)
			{
				work = "20" + f.CCValidDate_Y.value;
				workfrom.setFullYear(work, f.CCValidDate_M.value - 1, 1);
			}
			
			work = "20" + f.CCExpDate_Y.value;
			workexp.setFullYear(work, f.CCExpDate_M.value - 1, 1);
			
			if (!(workexp > workfrom))
			{
				msg += "\n - expiry date should be greater than valid from date";
			}
			else
			{
				if (workfrom > now)
				{
					msg += "\n - valid from date is in the future (mm/yy)";
				}
			}
			if (workexp < now)
			{
				msg += "\n - expiry date is in the past (mm/yy)";
			}
			
		}
		if (f.CCNumber.value == "")
		{
			msg += "\n - credit card number.";
		}
		else if (!isInteger(f.CCNumber.value))
		{
			msg += "\n - your credit card number must contain only numbers (no spaces or dashes).";
		}
		else if (f.CCNumber.value.length < 13)
		{
			msg += "\n - your credit card number must be at least 13 digits in length.";
		}
		
		if (f.CCSecurityNum.value == "")
		{
			msg += "\n - security number for your credit card"
		}
		else if (f.CCSecurityNum.value.length != 3)
		{
			msg += "\n - security number must be 3 digits in length"
		}
		else if (!isInteger(f.CCSecurityNum.value))
		{
			msg += "\n - security number must be numeric"
		}
		
		if (f.CCType.selectedIndex == 4 || f.CCType.selectedIndex == 5)
		{
			if (f.CardIssueNum.value == '' && f.CardStartDate.value == '/20')
			{
				msg += "\n - you must either supply an issue number or valid from date."
				
			}
		}
		if ( $.trim(f.CardIssueNum.value) != "" && !isInteger(f.CardIssueNum.value))
		{
			msg += "\n - if specified, the issue number must be numeric"
		}
	}
	
	if (f.ShipAdd1.value == "" && f.ShipAdd1.value == "" && f.ShipAdd2.value == "" && f.ShipAdd3.value == "" && f.ShipAdd4.value == "")
	{
		msg += "\n - delivery address."
	}
	
	if (f.ShipTown.value == "")
		msg += "\n - delivery town name."
	
	if (f.ShipPostcode.value == "" && f.ShipCountry.value != ROICountryId)
		msg += "\n - delivery postcode."
	
	if (f.ShipCountry.selectedIndex == 0)
		msg += "\n - delivery country."
		
	if( $('#rdoVatExemptTrue') !== null && $('#rdoVatExemptTrue').is(':visible') ){
		var vatExemptTrue = $('#rdoVatExemptTrue').is(':checked');
		var vatExemptFalse = $('#rdoVatExemptFalse').is(':checked');
		
		if( !vatExemptTrue && !vatExemptFalse )
			msg += '\n- you must select yes or no for VAT exempt';
			
		if( vatExemptTrue ){
			if( $('#txtVatExemptFullName').val() === '' )
				msg += '\n- please enter a full name for the VAT exemption';
			if( $('#txtVatExemptAddress').val() === '' )
				msg += '\n- please enter an address for the VAT exemption';
			if( $('#txtVatExemptPostcode').val() === '' )
				msg += '\n- please enter a postcode for the VAT exemption';
			if( $('#txtDisablingCond').val() === '' )
				msg += '\n- please enter a disabling condition for the VAT exemption';
		}
	}
/*
	if(msg=="") {
		document.frmBasket.action="#securebasehref#payment.cfm?a=#URLEncodedFormat(url.a)#&b=#URLEncodedFormat(url.b)#&c=#URLEncodedFormat(url.c)#";
		document.frmBasket.submit();
	} else {
		alert("Please supply the following details:"+msg);
	}		
*/	
	
	if( msg != '' )
	{
		alert("Please supply the following details:" + msg);
		return;
	}
	
	$('#buyBasketCell').html('Please Wait Processing...');
	f.action = securebasehref + 'default.cfm/loadfile.BuyConf.cfm';
	f.submit();
}
 
