<!-- //

//Created:	22/06/2005
//Author:	Stuart Dodd
//Purpose:	to allow on js file to be included on all 
//			forms in the checkout and separate the js 
//			from the main code.

var vfa = new Array();

function fieldObject(fieldNamex, descriptionx, fldcheckx) 
{
	//Field name of the Form Element
	this.fieldname = fieldNamex;

	//International Language Descriptive text
	this.description = descriptionx;

	//Field Check to be applied
	//	0	=	is blank Field
	//	1	=	card number check
	//	2	=	dropdown check
	this.fldcheck = fldcheckx;

	return(this);
}

function NewFormValidate(Form, missingText, otherText)
{	  
	var itemVal;
	var badfield = "";
	
	var missingList = new Array();
	var otherError = new Array();

	for(i=0; i<vfa.length; i++)
	{
		itemObj = Form.elements[vfa[i].fieldname];
		fci = parseInt(vfa[i].fldcheck);
		if (fci == 0)
		{
			if (isblank(itemObj.value))
			{
				//badfield = badfield + "\n " + vfa[i].description;
				missingList.push(vfa[i].description);
			}
		}
		if (fci == 1)
		{
			if (isblank(itemObj.value))
			{
				missingList.push(vfa[i].description);
			}
			else
				if (!luhncheck(itemObj))
				{
					//badfield = badfield + "\n " + vfa[i].description;
					otherError.push(otherText.replace("@@FIELD@@", vfa[i].description));
				}
		}
		if (fci == 2)
		{
			if (!ddcheck(itemObj))
			{
				//badfield = badfield + "\n " + vfa[i].description;
				missingList.push(vfa[i].description);
			}
		}
	}

	if(missingList.length > 0)
	{
		badfield = missingText + missingList.join("\n");
	}

	if(otherError.length > 0)
	{
		badfield += otherError.join("\n");
	}

	if (badfield.length > 0)
	{
		//alert(msg_text + badfield);
		alert(badfield);
	}
	else
	{
		return true;
	}	
	return false;
}

//vfa[0] = new fieldObject('Surname', 'Surname Field', 0);

// Do a mod 10 check on the credit card number
function luhncheck(textObj)
{
	var ccNum2;
	ccNum2 = "";
	var calcCard=0;
	var ccNum=textObj.value;

	// ignore the number if the first character indicates taht the number is hidden behind i.e. '*'s
	if (isNaN(ccNum.substring(0,1)) && (ccNum.substring(0,1) != ' '))
	{
		return true;
	}

	for(var i=0;i!=ccNum.length;i++)
	{
		var aChar = ccNum.substring(i,i+1);
		if((aChar>='0')&&(aChar<='9'))  ccNum2 = ccNum2 + aChar;
	}
	ccNum  = ccNum2;
	var cc= parseInt(ccNum2);
	if(cc==0)
	{
		//alert("check CardNumber");
		return false;
	}
	var r = ccNum.length / 2;
	if(ccNum.length - (parseInt(r)*2) == 0)
	{
		var odd = 2;
		var even = 1;
	}
	else
	{
		var odd =1;
		var even =2;
	}
	for (var x = ccNum.length - 1;x>0;x--)
	{
		r = x/2;
		if (r<1)
			r++;
		if (x-(parseInt(r) *2) !=0)
			var calcs=(parseInt(ccNum.charAt(x - 1))) * odd;
		else
			calcs = (parseInt(ccNum.charAt(x - 1))) * even;
		if(calcs >= 10) 
			calcs = calcs - 10 + 1;
		calcCard = calcCard + calcs;
	}
	calcs = 10 - (calcCard % 10);
	if(calcs ==10)
		calcs =0;
	if (calcs == (parseInt(ccNum.charAt(ccNum.length - 1 ))))
	{
		return true;
	}
	else 
	{
		//alert("Your credit card number appears to be invalid.\n");    
		return false;
	}
}

function ddcheck(ItemObj) {
	if (ItemObj.selectedIndex == -1)
	{
		return false;
	}
	if ((ItemObj.selectedIndex != -1) && ((ItemObj.value == 0) || (ItemObj.value == "") || (ItemObj.value == " ")))
	{
		return false;
	}
	return true;
}

function ValidateForm(Form, msg_text)
   {       
	  var emptyfield = "";

	  //Old Code
	  for (var i = 0; i < Form.length; i++)
		 {
			var element = Form.elements[i];
			if (((element.type == "text") || (element.type == "textarea") ) && !element.optional)
			   {
				  if ((element.value == null) || (element.value == "") ||(element.value == " ") || isblank(element.value))
					 {
						emptyfield += "\n          " + element.name; 
						continue;
					 }
			   }   
			if ( (element.type == "select-one") && !element.optional)
			{
				if ( element.selectedIndex<1 )
				{
					emptyfield += " \n        " + element.name;
					continue;
				}
			}
		 }     

		 //if ((!emptyfield)&&(luhncheck(Form.elements["CardNumber"]))&&ValidateDate(Form)) return true;
		 //if ((!emptyfield)&&(Form.elements["CardTypeID"].value == btrID)) return true;
		 if (emptyfield)
		 {
			 msg  = "_________________________________________\n\n";
			 msg += "The following details are required please\n";
			 msg += "correct this and then continue\n";
			 msg += "_________________________________________\n\n";
			 msg += emptyfield;
			 alert(msg);
			 return false;
		}
		//add in check to account for Bank Transfer SWD
		if(!luhncheck(Form.elements["CardNumber"]) && Form.elements["CardTypeID"].value != btrID)
		{
			alert("Your card number is wrong");
		}
		//end
		if(!ValidateDate(Form) && Form.elements["CardTypeID"].value != btrID)
		{
			alert("There is a problem with your credit card dates.");
		}
		return false;
   }

function isblank(String) 
   {
	  for (var i = 0; i < String.length; i++) 
		 {
			var c = String.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		 }
	  return true;
   }

//-->