/*
-----------------------------------------------
Script name
validform.js

Language
JavaScript 1.2

Created By
AntiType Studio, copyright 2000

Function
Repository for commonly used form validation functions
-----------------------------------------------
*/


/*
checks to make sure that a valid item has been selected
in an select list. this method works because we always
set any non-returnable item values to null
*/
function isSelected(field) {
  selecteditem = field.selectedIndex;
  selecteditem = field[selecteditem].value;

  if (selecteditem != '') {
    return true;
  }
  else {
    return false;
  }
}

/*
checks to make sure that the contents of 2 fields are the same
and not null
*/
function isSame(field,field1) {
  if (field.value == field1.value &&
      field.value != null &&
	  field.value != '')
  return true;
  else return false;
  }


/*
return the selected value in an option list.
*/
function getSelected(field) {
	if (field) {
		var selectedstatus = field.selectedIndex;
		if (selectedstatus != -1) {
			selectedstatus = field[selectedstatus].value;
			return selectedstatus;
		} else {
			return '';
		}
	} else {
		return '';
	}
}

/*
return the selected value in an option list.
*/
function getSelectedValue(field) {
    var selectedstatus = field.selectedIndex;
    selectedstatus = field[selectedstatus].value;
    return selectedstatus;
}


/*
return the selected text in an option list.
*/
function getSelectedText(field) {
    var selectedstatus = field.selectedIndex;
    selectedstatus = field[selectedstatus].text;
    return selectedstatus;
}


/*	
checks if date passed is valid
*/
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function isValidDate(field) {
	var fldval = field.value;

	var month = (fldval.substring(0,2)-1) - 0;
	var day = (fldval.substring(3,5)) - 0;
	var year = (fldval.substring(6,10)) - 0;

	if (month > 12)	{
		return false;
	}

	if (day > 31) {
		return false;
	}
    
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}


/*
checks to make sure that something has been entered
in a text, password, file, or textarea field
*/
function isFilled(field) {
	if (field) {
		if (field.value != '' && field.value != null) {
			return true;
		}
		else {
			return false;
		}
	}
}

/*
checks to make sure that at least 1 item has been
selected in a radio or checkbox group
*/
function isChecked(field) {  
  var checkedbutton = ""

  if (field.length > 0) {
	  for (var i=0; i < field.length; i++) {
		if (field[i].checked) {
		  checkedbutton=field[i].value;
		}
	  }
  } else {
	if (field.checked) {
		checkedbutton = field.value;
	}
  }

  if (checkedbutton == "") {
    return false;
  }
  else {
    return true;
  }
}


/*
returns the value of the checked item in a check
or radio group
*/
function getChecked(field) {  
  var checkedbutton = "";
  for (var i=0; i < field.length; i++) {
    if (field[i].checked) {
      checkedbutton=field[i].value;
    }
  }

  return checkedbutton;
}

function getDefaultChecked(field) {
  var checkedbutton = "";

  for (var i=0; i < field.length; i++) {
    if (field[i].defaultChecked) {
      checkedbutton=field[i].value;
    }
  }
	
	return checkedbutton;
}



/*
checks to make sure that the contents of a text
field match the profile of an email address
ex. someone@somedomain.com
*/
function isEmail(field) {
	//^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$ 
  if (field.value.indexOf("@") == -1 ||
      field.value.indexOf(".") == -1 ||
      field.length == 2 ||
      field.value == "") {
    return false;
  }
  else {
    return true;
  }
}

/*
checks to make sure that the contents of a field
are numeric and do not contain and non-numeric
characters. accepts the "-" to designate negative
values
*/
function isNumeric(field,required) {
  var nonre = /[^0-9]+/gi
  var re = /\-?[0-9]+.?[0-9]+|\-?[0-9]+/i
  if (required == "y") {
    if (field.length == 0 || field.value == null) {
      return false;
    }
  }
  else {
    if (nonre.test(field.value) == true) {
      return false;
    }
    else if (re.test(field.value) == true) {
      return true;
    }
    else {
      return false;
    }
  }
}

function isValidCreditCard(type, ccnum) {
	str = new String(ccnum);
	rExp = /\-/gi;
	replstr = new String ('');
	newstr = str.replace(rExp, replstr)
	ccnum = newstr;

	//strip out slashes
	ccnum = ccnum.replace('-', '');
	if (type == "Visa") {
		// Visa: length 16, prefix 4, dashes optional.
		var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	} else if (type == "MC") {
		// Mastercard: length 16, prefix 51-55, dashes optional.
		var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	} else if (type == "Disc") {
		// Discover: length 16, prefix 6011, dashes optional.
		var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	} else if (type == "AmEx") {
		// American Express: length 15, prefix 34 or 37.
		var re = /^3[4,7]\d{13}$/;
	} else if (type == "Diners") {
		// Diners: length 14, prefix 30, 36, or 38.
		var re = /^3[0,6,8]\d{12}$/;
	}
	
	if (!re.test(ccnum)) {
		return false;
	} else {
		return true;
	}
}