function checkContact(missingurl) {
	fm = document.forms['contact'];
	if (fm.email.value != "") {
		temp = fm.email.value + " ";
		found = temp.search(/[\w\.-]+@([\w-]+\.){1,3}[a-zA-Z]{2,}\s{1}/);
		if (found != 0)
			fm.missing.value += "," + "E-mail must be in the form username@domain.com"
	}
//	fm.phone.value = fm.phone.value.replace(/^ */g, '').replace(/ *$/g, '');
//	if (fm.phone.value != "") {
//		temp = fm.phone.value + " ";
//		if (temp.search(/\d{10}\s{1}/) == 0)
//			fm.phone.value = fm.phone.value.substring(0, 3) + "-" + fm.phone.value.substring(3, 6) + "-" + fm.phone.value.substring(6);
//		temp = fm.phone.value + " ";		
//		if (temp.search(/[\d-]+\s{1}/) != 0)
//			fm.missing += "Telephone must be digits and dashes only in the form 123-456-7890\n"
//		else if (fm.phone.value.length != 12)
//			fm.missing += "Telephone must be digits and dashes only in the form 123-456-7890\n"
//	}
	
	if (fm.missing.value != "")
		fm.action = missingurl;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_validateForm() { //v3.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (val!=''+num) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
  // jevo 2003/01/14
  // add code to validate credit card ONLY if paymenttype="Creditcard"
	//pmtType=checkState(document.CFForm_1,'paymenttype');
	if (document.CFForm_1.paymenttype.value == 'Creditcard') {
		if (document.CFForm_1.ccnum.value == '') {
			errors+='- you must enter a credit card number\n'; }
		else {
			if ( ! verifyCC(document.CFForm_1.ccnum.value) ) {
				errors+='- you must enter a valid credit card number\n';
			}
		}  
		if (trim(document.CFForm_1.ccexp.value) == '') {
			errors+='- you must enter your credit card expiration date\n';
		} 
		if (trim(document.CFForm_1.ccsc.value) == '') {
			errors+='- you must enter your credit card security code\n';
		} else if (! verifyCSC(trim(document.CFForm_1.ccsc.value))) {
			errors+='- credit card security code must be 3 or 4 digits\n';
		}
	}
	//  errors+='Stop here';
  
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1,strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0,strText.length-1);
    return strText;
} 

function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;
	for( i = 0; i < cardNumber.length; ++i ) {
		ar[i] = parseInt(cardNumber.charAt(i));
	}
	for( i = ar.length -2; i >= 0; i-=2 ) {  // you have to start from the right, and work back.
		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
	}										 // if the double digit is > 9, add those individual digits together 
	for( i = 0; i < ar.length; ++i ) {
		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
	}
	return (((sum%10)==0)?true:false);	  	
}
function verifyCC(aCcnum)  {
  
  if (aCcnum == '') {
		return false;
  }
  cardnumber = aCcnum.replace(/-/g,'');
  // alert('cardnumber = '+cardnumber);
  // var cardtype   = f.cardtype.options[f.cardtype.selectedIndex].value;

	if (! mod10(cardnumber)) {
		return false;
	}
	return true;
  // Initialise some temporary variables
  var result = 0;
  var ccresu = 0;
  var ccOK = true;
  cc = new Array();

	if ((cardnumber.length !=13) && (cardnumber.length != 15) && (cardnumber.length != 16)) {
		alert("Check the credit card number and re-enter");
		ccOK = false;
	}
  // Check if the cardnumber length of Mastercard and Novus are correct!
 //  if ((cardtype == "MC") || (cardtype == "NOVUS") || (cardtype == "DIS") || (cardtype == "EC"))
 //  {
 //    if (cardnumber.length!=16)
 //    {
 //      alert("Your Mastercard, Novus, Discovery or Eurocard number must have 16 digits!");
 //      ccOK = false;
 //    }
 //  }

  // Check if the cardnumber length of Visa is correct!
//   if (cardtype == "VISA")
//   {
 //    if ((cardnumber.length!=16) && (cardnumber.length!=13))
 //    {
 //      alert("Your Visa card number must have either 13 or 16 digits!");
 //      ccOK = false;
 //    }
//   }

  // Check if the cardnumber length of Mastercard and Novus are correct!
 //  if (cardtype == "AMEX")
 //  {
  //   if (cardnumber.length!=15)
   //  {
   //    alert("Your American Express card number must have 15 digits!");
   //    ccOK = false;
  //   }
//   }

  if (ccOK == true)
  {
    if (cardnumber.length == 16)
    {
      start  = 0;
      end    = 8;
      offset = 0;
    }

    if (cardnumber.length == 13)
    {
      start  = 0;
      end    = 6;
      offset = 1;
    }

    if (cardnumber.length == 15)
    {
      start  = 0;
      end    = 7;
      offset = 1;
    }

    for (var i=start;i<end; i++)
    {
      cc[i] = cardnumber[(i*2)+offset] *2;
      if (cc[i]>9)
      {
        ccresu += Math.floor((cc[i]/10) + (cc[i]%10));
      }
      else
      {
        ccresu += cc[i];
      }
			// alert('ccresu = '+ccresu);
    }

    for (var i=start;i<end+offset; i++)
    {
      result += cardnumber[(i*2)+1-offset]-0;
    }

    result += ccresu;
    result = result%10;

    if (result == 0)
    {
    //  alert("Your credit card information is correct");
      return true;
    }
    else
    {
      // alert("Your credit card information is not correct");
      return false;
    }
  }
}
function verifyCSC(csc) {
	return (csc + " ").search(/\d{3,4}\s{1}/) == 0;
}
