
function checkText (string,  field, lower, upper, IsPassword) {
	var error = "";
 	if (string.value == "") {
    	error = "You left " + field + " blank.\n";
 	}
	if (lower != -1){ // make sure the length is at least this value
		if (string.value.length < lower) {
			error = field + " must be at least " + lower + " characters long.\n";
		}
	}
	if (upper != -1){ // make sure the length is at least this value
		if (string.value.length > upper) {
			error = field + " must be no more then " + upper + " characters long.\n";
		}
	}
	if (IsPassword = true){
		if (!((string.value.search(/(a-z)+/)) && (string.value.search(/(A-Z)+/)) && (string.value.search(/(0-9)+/)))) {
  			error = "The password must contain at least one uppercase letter, one lowercase letter,and one numeral.\n";
  		}
	}
	return error;
}

function checkPhone(string){
	var error = "" ;
	var stripped = string.value.replace(/[^0-9]*/g, '');
	string.value = stripped
	
    if (stripped == "") {
   		error = "You must enter a phone number.\n" ;
	}
	else if (isNaN(parseInt(stripped))) {
   		error = "The phone number contains illegal characters.\n" ;
	}

	//if (!(stripped.length == 10)) {
	//	error = "The phone number is the wrong length.\n";
	//}
	return error ;
}
function checkMobile(string){
	var error = "" ;
	var stripped = string.value.replace(/[^0-9]*/g, '');
	string.value = stripped
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
   		error = "The phone number contains illegal characters.";
	}
	if ((stripped.length != 10)||(stripped.length != 12)) {
		error = "You have entered an invalid mobile number ie. 3538XXXXXXXXX or 08XXXXXXXX\n";
	}
}
function checkRadio(checkvalue, field) {
	var error = "";
   	if (!(checkvalue)) {
       error = "Please select an option for " + field + ".\n";
    }
	return error;    
}
function checkTick(checkvalue, field) {
	var error = "";
   	if (checkvalue == false) {
       error = "Please tick the " + field + " box.\n";
    }
	return error;    
}
function checkDropdown(choice, field) {
    var error = "";
    if (choice == 0) {
       error = "You didn't choose an option from the " + field + " drop-down list.\n";
    }    
	return error;
}
function checkCVS (string) {
 	var error = "" ;
	var stripped = string.value.replace(/[^0-9]*/g, '');
	string.value = stripped
 	if (string.value.length != 3) {
    	error = "The CVS Number is not valid\n";
 	}
	else if (isNaN(string.value.length)) {
    	error = "The CVS Number contains illegal characters\n";
 	}
	return error;
}
function checkCC(cardNumber, cardType)
{
  var isValid = false;
  var stripped = cardNumber.value.replace(/[^0-9]*/g, '');
  cardNumber.value = stripped
  isValid = !isNaN(stripped);
  if (cardNumber.value == "") {
	return false ;
  }

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.value.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;
    switch(cardType.value)
    {
      case "MC":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;
      case "VISA":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;
      case "AMEX":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;
	  case "LASER":
        lengthIsValid = (cardNumberLength == 19);
		if (isNaN(cardNumber.value)) {
		  return false ;
		}
		if (lengthIsValid == true ){
		  return true ;
		}
		else {
		  return false ;
		}
		//prefixRegExp = //;
        break;
      default:
        prefixRegExp = /^$/;
        //alert("Card type not found");
    }
    prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    isValid = prefixIsValid && lengthIsValid;
  }
  if (isValid)
  {

    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }
    isValid = (checkSumTotal % 10 == 0);
  }
  return isValid;
}
function checkDate(dateStr){
	var result = "";
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
    var matchArray = dateStr.value.match(datePat); // is the format ok?

	if (dateStr.value.length != 8){result = "Please enter date as DD/MM/YY.\n";return result;}
    if (matchArray == null) {result = "Please enter date as DD/MM/YY.\n";return result;}

    month = matchArray[3]; // parse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) {result = "Month must be between 1 and 12.\n";return result;}
    if (day < 1 || day > 31) {result = "Day must be between 1 and 31.\n";return result;}
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {result = "Month "+month+" doesn't have 31 days!\n";return result;}
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {result = "February " + year + " doesn't have " + day + " days!\n";return result;}
    }
	return result;
}

function checkDateYYYY(dateStr){
	var result = "";
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.value.match(datePat); // is the format ok?

	if (dateStr.value.length != 10){result = "Please enter date as DD/MM/YYYY.\n";return result;}
    if (matchArray == null) {result = "Please enter date as DD/MM/YYYY.\n";return result;}

    month = matchArray[3]; // parse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) {result = "Month must be between 1 and 12.\n";return result;}
    if (day < 1 || day > 31) {result = "Day must be between 1 and 31.\n";return result;}
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {result = "Month "+month+" doesn't have 31 days!\n";return result;}
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {result = "February " + year + " doesn't have " + day + " days!\n";return result;}
    }
	return result;
}

function checkExpires(months, years){
	var error = "" ;
	var NewMonth = months.value.replace(/[0]*/g, '');
	var NewYear = "20" + years.value
	var NewDate = parseInt(NewYear+NewMonth)

	var now = new Date();
	var CurYear = now.getYear()
	var CurMonth = now.getMonth()+1
	var CurDate = (""+CurYear+CurMonth)
	CurDate = parseInt(CurDate)
	
	if (CurDate > NewDate){
		error = "Your Expiry date cannot be eariler than the current month\n"
	}
	return error;
}

function checkEmail(emailAddress) {
	var error = "";
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	if (re.test(emailAddress.value) == false){
	 	error = "The email address you entered is not valid.\n";
	 }
	 return error;
}



