// Return this string value representing a price as a double - just remove commas 

function getPriceDouble(str)
{
	return str.replace(',', '')*1;
}

// Return this double value representing a price as a String - just add commas 

function getStringDouble(value)
{
	var totalPrice = value.split('.');
	var fullPrice = totalPrice[0];
	var decimalPrice = totalPrice[1];
	var length = fullPrice.length;
	
	var priceString = "";

	while(fullPrice.length > 0)
	{
		var index = fullPrice.length-3;
		if(index < 0)
			index = 0;
			
		if(length == fullPrice.length)
			priceString = fullPrice.substring(index);
		else
			priceString = fullPrice.substring(index) + "," + priceString;
		fullPrice = fullPrice.substring(0, index);
	}
		
	return priceString + "." + decimalPrice;
}

// Hide or show the detailed currency section explanation
		
function displayCurrency()
{
	var expand = document.getElementById("expandedCurrency");
	var unexpand = document.getElementById("unexpandedCurrency");
	var display = document.getElementById("displayCurrencyAction");
									
	if(unexpand.style.visibility == "visible")
	{
		unexpand.style.visibility = "hidden";
		unexpand.style.display = "none";
		expand.style.visibility = "visible";
		expand.style.display = "block";
		display.innerHTML = "<a href='javascript:displayCurrency()'>Click to Minimise</a>";
	} 
								
	else 
	{
		unexpand.style.visibility = "visible";
		unexpand.style.display = "block";
		expand.style.visibility = "hidden";
		expand.style.display = "none";
		display.innerHTML = "<a href='javascript:displayCurrency()' >Click to Read More</a>";
	}
}

// Submit the frequent booker form

function submitCustomerDetailsForm()
{
	document.customerRefForm.customerRef.value=document.paymentForm.customerRef.value;
 	if(document.customerRefForm.customerRef.value!="")
 		document.customerRefForm.submit();
}

// Copy the contact details from the selected voucher number into the billing contact details form

function copyContactDetails(select) 
{
	if(select.selectedIndex > 1) 
	{
		var index = select.options[select.selectedIndex].value;
		if(document.getElementById("recipientFirstName"+index).value != "not supplied"){document.paymentForm.billingFirstName.value = document.getElementById("recipientFirstName"+index).value;}
		if(document.getElementById("recipientLastName"+index).value != "not supplied"){document.paymentForm.billingLastName.value = document.getElementById("recipientLastName"+index).value;}
		if(document.getElementById("recipientAddress"+index).value != "not supplied"){document.paymentForm.billingAddress.value = document.getElementById("recipientAddress"+index).value;}
		if(document.getElementById("recipientTownCity"+index).value != "not supplied"){document.paymentForm.billingTownCity.value = document.getElementById("recipientTownCity"+index).value;}
		if(document.getElementById("recipientPostcodeZip"+index).value != "not supplied"){document.paymentForm.billingPostCodeZip.value = document.getElementById("recipientPostcodeZip"+index).value;}
		if(document.getElementById("recipientCountyState"+index).value != "not supplied"){document.paymentForm.billingCountyState.value = document.getElementById("recipientCountyState"+index).value;}
		if(document.getElementById("recipientEmail"+index).value != "not supplied"){document.paymentForm.billingEmail.value = document.getElementById("recipientEmail"+index).value;}
		if(document.getElementById("recipientPhone"+index).value != "not supplied"){document.paymentForm.billingPhone.value = document.getElementById("recipientPhone"+index).value;}
		document.paymentForm.billingCountry.selectedIndex = 0;
		
		for(var i=0; i<document.paymentForm.billingCountry.length; i++)
			if(document.paymentForm.billingCountry.options[i].value == document.getElementById("recipientCountry"+index).value) 
			{
				document.paymentForm.billingCountry.selectedIndex = i;
				break;
			}
	}
	
	else 
	{
		document.paymentForm.billingFirstName.value = "";
		document.paymentForm.billingLastName.value = "";
		document.paymentForm.billingAddress.value = "";
		document.paymentForm.billingTownCity.value = "";
		document.paymentForm.billingPostCodeZip.value = "";
		document.paymentForm.billingCountyState.value = "";
		document.paymentForm.billingEmail.value = "";
		document.paymentForm.billingPhone.value = "";		
		document.paymentForm.billingCountry.selectedIndex = 0;
	}
}

// Copy the customer contact name into the card holder name field

function copyFormNameToCardName(checkbox)
{
	if(checkbox.checked == true)
		document.paymentForm.CardName.value = document.paymentForm.billingFirstName.value + " " + document.paymentForm.billingLastName.value;
	else
		document.paymentForm.CardName.value = "";
}

// Validate the payment Form - called when submitting the form

function validateForm()
{	
	// Is the expiry date in the past?
	
	if(date_past()) 
	{
		alert("Credit card expiry date is in the past");
		return false;
	} 
	
	set_expirydate();
	
	// Did they fill in all required fields?
	
	var msg = "";
	var focus = "";
	
	if(document.paymentForm.billingFirstName.value == ""){msg += "You must enter a first name";if(focus==""){focus=document.paymentForm.billingFirstName;}}
	if(document.paymentForm.billingLastName.value == ""){msg += "\nYou must enter a last name";if(focus==""){focus=document.paymentForm.billingLastName;}}
	if(document.paymentForm.billingAddress.value == ""){msg += "\nYou must enter an address";if(focus==""){focus=document.paymentForm.billingAddress;}}
	if(document.paymentForm.billingTownCity.value == ""){msg += "\nYou must enter a town or city";if(focus==""){focus=document.paymentForm.billingTownCity;}}
	if(document.paymentForm.billingCountyState.value == ""){msg += "\nYou must enter a county or state";if(focus==""){focus=document.paymentForm.billingCountyState;}}
	if(document.paymentForm.billingCountry.selectedIndex == 0 || document.paymentForm.billingCountry.value == ""){msg+="\nYou must enter a country";if(focus==""){focus=document.paymentForm.billingCountry;}}	
	if(document.paymentForm.billingEmail.value == ""){msg += "\nYou must enter an email address";if(focus==""){focus=document.paymentForm.billingEmail;}}
	if(document.paymentForm.billingPhone.value == ""){msg += "\nYou must enter a phone number";if(focus==""){focus=document.paymentForm.billingPhone;}}
	if(document.paymentForm.CardName.value == ""){msg += "\nYou must enter a credit card name";if(focus==""){focus=document.paymentForm.CardName;}}
	if(document.paymentForm.CardNum.value == ""){msg += "\nYou must enter your credit card number";if(focus==""){focus=document.paymentForm.CardNum;}}
	
	if(document.paymentForm.CardExpiryDateM.value == "MM"){msg += "\nYou must select a credit card expiry Date";}
	else if(document.paymentForm.CardExpiryDateY.value == "YYYY"){msg += "\nYou must select a credit card expiry Date";}
	
	// Is the email addres valid?
	
	if(document.paymentForm.billingEmail.value != "" && !check_email(document.paymentForm.billingEmail.value))
	{
		msg += "\n Email address is not valid";
		if (focus == "")
			focus = document.paymentForm.billingEmail;
	}
	
	// Is the credit card number valid?
	
	if (!validateCreditCard(document.paymentForm.CardNum.value)){
		msg += "\nCredit card number is not valid or is not supported";
		if (focus == "")
			focus = document.paymentForm.CardNum;
	}
	
	//Derive the card tpe
	
	document.paymentForm.CardType.value = getCardType(document.paymentForm.CardNum.value);		
		
	// Check that the entered the correct amount of digits for the cvv

	var cvvStatus = validateCVV(document.paymentForm.CardNum.value, document.paymentForm.cvv.value);

	if(cvvStatus != 0)
	{
		if(cvvStatus == 1)
			msg = msg + "\nYou must enter a valid 3-digit Security code";
		else if(cvvStatus == 2)
			msg = msg + "\nYou must enter a valid 4-digit Security code";
		else if(cvvStatus == 3)
			msg = msg + "\nYou must enter a valid 1-digit Issue Number";
		else if(cvvStatus == 4)
			msg = msg + "\nYou must enter a valid 2-digit Issue Number";
	
 		if (focus=="")
   			focus=document.paymentForm.cvv;
	}
	
	// If something failed the check, display the info and return false
	
	if(msg != "")
	{
		alert(msg);
		
		if(focus != "")
			focus.focus();
		
		return false;
	}
	
	return true;
}

// Check that the email address is valid

function check_email(e) 
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
  
	for(i=0; i < e.length ;i++)
	{
		if(ok.indexOf(e.charAt(i))<0)
			return false;
	}	 
	
  	if (document.images) 
	{
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	 	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	 	if (!e.match(re) && e.match(re_two)) 
   			return -1;		
	}
}

// Check if the expiry date is in the past

function date_past()
{       
	d = new Date();
	this_year = d.getFullYear();
	this_month = d.getMonth()+1;
	entered_year = parseInt(document.paymentForm.CardExpiryDateY.value,10)+2000;
	entered_month = document.paymentForm.CardExpiryDateM.value;

 	if (entered_year<this_year)
	{
		return true;
	}
	else if (entered_year==this_year)
	{
		if(entered_month<this_month)
		{
			return true;
		}
	}
	else 
	{
		return false;
	}
}

// Set the expiry date

function set_expirydate()
{
	document.paymentForm.CardExpiryDate.value = document.paymentForm.CardExpiryDateM.value + document.paymentForm.CardExpiryDateY.value
}

// Update the expiry date

function updateCardExpiry()
{
	var month = "";
	var year = "";
	
	if(document.paymentForm.CardExpiryDateM.value != "MM")
		month = document.paymentForm.CardExpiryDateM.value;
	
	if(document.paymentForm.CardExpiryDateY.value != "YYYY")
		year = document.paymentForm.CardExpiryDateY.value;
	
	document.paymentForm.CardExpiryDate.value = month + year;
}

// Filter the string by stripping out problematic charatcers

function stringFilter(input) 
{ 	
	s = input.value;
	filteredValues = "\"'";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) 
	{ 
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) 
			returnString += c;
 	}
	input.value = returnString;
}

// Opens a page - used for terms and conditions

function open_page(page)
{
	window.open(page,"",config="width=500,height=750,resizable=yes,location=no,directories=no,status=no,scrollbars=yes")
}

// Opens a page - used for cvv info

function openpagecvv(pagename)
{
	window.open(pagename,"TermsAndConditions",config="width=550,height=730,top=150,left=200,toolbar=no,menubar=no,resizable=yes,location=no,directories=no,status=no")
}

// Opens a page - used for feedback form

function popUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=500');");
}

// Opens a page - used for report an error

function popUp2(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=500');");
}

// Opens a page - used for customer reference

function openInfo()
{
	infoWindow = window.open("", "_blank", config="width=476px,height=250px,left=200px,top=150px,toolbar=no,resizable=no,location=no,directories=no,status=no,scrollbars=no");
	infoWindow.document.write("<html><head><title>Frequent Booker Reference</title><LINK REL='StyleSheet' HREF='../newsite/css/booking/color_scheme_Blue.css' TYPE='text/css'></head><body><span style='font-size:9pt;font-family:Arial,Verdana,Times New Roman;text-align:justify;'><table width='100%' cellpadding='0' cellspacing='0' class='primary_color' style='padding:2px 4px 2px 4px;font-weight:bold;font-size:9pt;'><tr><td>What's this? - Frequent Booker Reference</td><td align='right'><a href='javascript:window.close();' style='color:#FFF;font-weight:normal;font-size:8pt;'>Close this window</a></td></tr></table><p>If you have made a booking before then you will have received an email with your unique Customer Reference code on it.</p><p>If you wish, you may enter your code here. This will recall your personal information from last time and repopulate the booking form below</p><p>This is not compulsory, but it means you will not need to repeatedly type in this information, making the process faster and more convenient next time you make a reservation.</p></span></body></html>");
}

// Opens a page - used for thawte

function openPrivacyStatement()
{
	window.open("../html/privacyStatement.htm", "_blank",config="width=500px,height=400px,left=200px,top=150px,toolbar=no,resizable=no,location=no,directories=no,status=no,scrollbars=yes");
}
