function validRequired(formField,fieldLabel)
{
	var result = true;

	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}

	return result;
}


function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}

	return result;
}

function isValidExpDate(formField,fieldLabel,required)
{
	var result = true;
	var formValue = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result && (formField.value.length>0))
 	{
 		var elems = formValue.split("/");

 		result = (elems.length == 2); // should be two components
 		var expired = false;

 		if (result)
 		{
 			var month = parseInt(elems[0],10);
 			var year = parseInt(elems[1],10);

 			if (elems[1].length == 2)
 				year += 2000;

 			var now = new Date();

 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();

 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));

			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
 		}

  		if (!result)
 		{
 			alert('Please enter a date in the format MM/YY for the "' + fieldLabel +'" field.');
			formField.focus();
		}
		else if (expired)
		{
 			result = false;
 			alert('The date for "' + fieldLabel +'" has expired.');
			formField.focus();
		}
	}

	return result;
}

function isValidCreditCardNumber(formField,ccType,fieldLabel,required)
{
	var result = true;
 	var ccNum = formField.value;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

  	if (result && (formField.value.length>0))
 	{
 		if (!allDigits(ccNum))
 		{
 			alert('Please enter only numbers (no dashes or spaces) for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}

		if (result)
 		{

 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				alert('Please enter a valid card number for the "' + fieldLabel +'" field.');
				formField.focus();
				result = false;
			}
		}

	}

	return result;
}

function LuhnCheck(str)
{
  var result = true;

  var sum = 0;
  var mul = 1;
  var strLen = str.length;

  for (i = 0; i < strLen; i++)
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;

  return result;
}



function GetRadioValue(rArray)
{
	for (var i=0;i<rArray.length;i++)
	{
		if (rArray[i].checked)
			return rArray[i].value;
	}

	return null;
}


function validateCCNum(cardType,cardNum)
{
	var result = false;
	cardType = cardType.toUpperCase();

	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

function validCCForm(ccTypeField,ccNumField,ccExpField)
{
	var result = isValidCreditCardNumber(ccNumField,ccTypeField.value,"Credit Card Number",true) &&
		isValidExpDate(ccExpField,"Expiration Date",true);
	return result;
}

function CheckForm( theform )
{
	var bMissingFields = false;
	var strFields = "";

	if( theform.b_first.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: First Name\n";
	}
	if( theform.b_last.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: Last Name\n";
	}
	if( theform.b_addr.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: Address\n";
	}
	if( theform.b_city.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: Town\n";
	}
	if( theform.b_state.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: County\n";
	}
	if( theform.b_zip.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: Postcode\n";
	}
	if( theform.b_phone.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: Phone\n";
	}
	if( theform.b_email.value == '' ){
		bMissingFields = true;
		strFields += "     Billing: Email\n";
	}

	if( theform.ccName.value == '' ){
		bMissingFields = true;
		strFields += "     Creditcard Name\n";
	}
	
	if( theform.ccNum.value == '' ){
		bMissingFields = true;
		strFields += "     Creditcard Number\n";
	}

	if( theform.ccExp.value == '' ){
		bMissingFields = true;
		strFields += "     Creditcard Expiry Date\n";
	}

	if( theform.ccCode.value == '' ){
		bMissingFields = true;
		strFields += "     Creditcard Code\n";
	}
	
	if( bMissingFields ) {
		alert( "I'm sorry, but you must provide the following field(s) before continuing:\n" + strFields );
		return false;
	}

	//if (validCCForm(theform.ccType,theform.ccNum,theform.ccExp) == false) {;
		//return false
	//}

	return true;
}

function gotext() {
	mytext = new Array();
	number = 0;

	mytext[number++] = "Buying was so easy. Thank you"
	mytext[number++] = "Can I recommend you to a friend?"
	mytext[number++] = "Buying from you, was a wonderful experience"
	mytext[number++] = "I can't believe how easy it was to buy from you.  Thank you very much"
	mytext[number++] = "Thank you for your help and advice, and for delivering so quickly"
	mytext[number++] = "You said it was easy - thank you for renewing our faith in customer care"
	mytext[number++] = "Wow! I have to say thank you. We love our new coffee maker. Oh greetings to Emma."
	mytext[number++] = "It was a pleasure doing business with you."
	mytext[number++] = "You were not like the other stores we visited."
	mytext[number++] = "I love your website, it gave us all the information we wanted - so clean and simple."
	mytext[number++] = "Great price! Lucky we shopped around and found you."
	mytext[number++] = "Thank you for sending me a replacement so quickly"

	increment = Math.floor(Math.random() * number);

	document.write(mytext[increment]);
}

// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this notice.

// SETUPS:
// ===============================

// Set the horizontal and vertical position for the popup

PositionX = 100;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)

defaultWidth  = 700;
defaultHeight = 700;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=100-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();
}}
