function stringReplace(originalString, findText, replaceText) {
	var pos = 0;
	var len = findText.length;

	pos = originalString.indexOf(findText);

	while (pos != -1) {
		preString = originalString.substring(0, pos);
		postString = originalString.substring(pos + len, originalString.length);
		originalString = preString + replaceText + postString;
		pos = originalString.indexOf(findText);
	}

	return originalString;
}

function isEmptyString(text) {
	var strTrim;

	strTrim = stringReplace(text, new String(" "), new String(""));

	if(strTrim.length == 0) {
		return true;
	}
	else
	{
		return false;
	}
}

function isEmail(str) {
  var supported = 0;

  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);

    if (tempReg.test(tempStr)) supported = 1;
  }

  if (!supported)
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");

  return (!r1.test(str) && r2.test(str));
}

function checkEmail (textfield) {
    if(!isEmail (textfield.value)) {
		alert ("Please enter a valid email address. It will be used for order confirmation.");
		return false;
	}
		return true;
}

function checkAmount (textObj)
{
   var newValue = textObj.value;
   var decAmount = "";
   var dolAmount = "";
   var decFlag = false;
   var aChar = "";

   // Ignore all but digits and decimal points.
   for(i=0; i < newValue.length; i++) 
   {
       aChar = newValue.substring(i,i+1);
       if(aChar >= "0" && aChar <= "9")
       {
       }
	else
	{
		return false;
	}
   }
   return true;
}

function checkCreditCard()
{
	var blnOK;
	var strErrorMessage;

	blnOK = true;
	strErrorMessage = 'The following fields are mandatory but do not contain a value:\n\n';
	if (isEmptyString(formCreditCardEnter.txtOrderId.value))
	{
		blnOK = false;
		strErrorMessage = strErrorMessage + "Order number.\n";
	}
	if (isEmptyString(formCreditCardEnter.txtOrderBedrag.value))
	{
		blnOK = false;
		strErrorMessage = strErrorMessage + "Amount.\n";
	}
	if (blnOK == true)
	{
		if (checkAmount(formCreditCardEnter.txtOrderBedrag) == true)
		{
			return true;
		}
		else
		{
			strErrorMessage = 'The amount should be entered in eurocents without decimal comma or dot. For example, enter 5600 for payment of EURO 56,00.\n';
			alert(strErrorMessage);
			return false;
		}
	}
	else
	{
		strErrorMessage = strErrorMessage + '\nPlease enter the mandatory fields.\n';
		alert(strErrorMessage);
		return false;
	}
	return false;
}
function checkPaymentAndDeliveryMethod()
{
	return true;
}

function checkCustomerFields(textEmail)
{
	var strErrorMessage;

	blnOK = true;
	strErrorMessage = 'The following fields are mandatory but not yet filled:\n\n';

	if (isEmptyString(frmCheckoutAddress.txtNameCompany.value))
	{
		blnOK = false;

		strErrorMessage = strErrorMessage + "Company Name\n";
	}	
	if (isEmptyString(frmCheckoutAddress.txtNameCustomer.value))
	{
		blnOK = false;

		strErrorMessage = strErrorMessage + "Name\n";
	}	
	if (isEmptyString(frmCheckoutAddress.txtAddress.value))
	{
		blnOK = false;
		strErrorMessage = strErrorMessage + "Address\n";
	}
	if (isEmptyString(frmCheckoutAddress.txtHouseNumber.value))
	{
		blnOK = false;
		strErrorMessage = strErrorMessage + "Number\n";
	}
	if (isEmptyString(frmCheckoutAddress.txtPostalCode.value))
	{
		blnOK = false;
		strErrorMessage = strErrorMessage + "Postal code\n";
	}
	if (isEmptyString(frmCheckoutAddress.txtTown.value))
	{
		blnOK = false;
		strErrorMessage = strErrorMessage + "City\n";
	}
	if (isEmptyString(frmCheckoutAddress.txtEmail.value))
	{
		strErrorMessage = strErrorMessage + "Email\n";
		blnOK = false;
	}
	if (blnOK == true)
	{
		if (checkEmail(textEmail) == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		strErrorMessage = strErrorMessage + '\nPlease enter these fields before continuing.\n'
		alert(strErrorMessage);
		return false;
	}
}

function checkControlConfirm()
{
	var blnOK;
	var strErrorMessage;

	blnOK = true;

	strErrorMessage = 'To confirm the order you must accept the General Terms & Conditions..\n\n';

	if (frmCheckoutControlConfirm.chkAcceptGeneralTerms.checked == false)
	{
		blnOK = false;

	}	

	if (blnOK == true)
	{
		return true;
	}
	else
	{
		alert(strErrorMessage);
		return false;
	}
}


