// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

function checkEmail (strng, err) {
var error="";
if (strng.value == "") {
   error = err + "You didn't enter an email address.\n";
   strng.className = 'invalidInput';
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng.value))) { 
       error = err;
	   strng.className += ' invalidInput';
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.value.match(illegalChars)) {
          error = err + " Email contains illegal characters.";
		  strng.className = 'invalidInput';
       }
    }
if (error == "") strng.className.replace(' invalidInput','');
return error;    
}

// Equal values (confirm password)
function isEqual(field1, field2, err)
{
	var error = "";
	if (field1.value != field2.value)
	{
		error = err;	
		field1.className += ' invalidInput';
		field2.className += ' invalidInput';
	}
	if (error == "")
	{
		field1.className.replace(' invalidInput','');
		field2.className.replace(' invalidInput','');
	}
	return error;
}

// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng, err) {
var error = "";
if (strng.value == "") {
   error = err;
   strng.className += ' invalidInput';
}

var stripped = strng.value.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = err + "The phone number contains illegal characters.";
	   strng.className += ' invalidInput';
  
    }
    if (!(stripped.length == 10)) {
	error = err + "The phone number is the wrong length. Make sure you included an area code.\n";
	strng.className += ' invalidInput';
    } 
if (error == "") strng.className.replace(' invalidInput','');
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng, err) {
var error = "";
if (strng.value == "") {
   error = err;
   strng.className += ' invalidInput';
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.value.length < 6) || (strng.value.length > 8)) {
       error = err + "The password is the wrong length.\n";
	   strng.className += ' invalidInput';
    }
    else if (illegalChars.test(strng.value)) {
      error = err + "The password contains illegal characters.\n";
	  strng.className += ' invalidInput';
    } 
    else if (!((strng.value.search(/(a-z)+/)) && (strng.value.search(/(A-Z)+/)) && (strng.value.search(/(0-9)+/)))) {
       error = err + "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
	   strng.className += ' invalidInput';
    }  
if (error == "") strng.className.replace(' invalidInput','');
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng, err) {
var error = "";
if (strng.value == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.value.length < 4) || (strng.value.length > 10)) {
       error = err + "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng.value)) {
    error = err + "The username contains illegal characters.\n";
	strng.className += ' invalidInput';
    } 
if (error == "") strng.className.replace(' invalidInput','');
return error;
}       


// non-empty textbox

function isEmpty(strng, err) {
var error = "";
  if (strng.value.length == 0) {
     error = err;
	 strng.className += ' invalidInput';
  }

if (error == "") strng.className.replace(' invalidInput','');
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
	 strng.className += ' invalidInput';
  }
if (error == "") strng.className.replace(' invalidInput','');
return error;
}

// minimum textbox length
function isMinimum(strng, len, err) {
	var error="";
	if (strng.value.length < len)
	{
		error = err;
		strng.className += ' invalidInput';
	}
	if (error == "") strng.className.replace(' invalidInput','');
	return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue, err) {
var error = "";
   if (!(checkvalue.selectedIndex)) {
       error = err;
	   checkvalue.className += ' invalidInput';
    }
	if (error == "") checkvalue.className.replace(' invalidInput','');
return error;
}

// valid selector from dropdown list

function checkDropDown(choice, err)
{
	var error = "";
    if (choice.selectedIndex == 0)
	{
		error = err;
		choice.className += ' invalidInput';
    }    
	if (error == "") choice.className.replace(' invalidInput','');
	return error;
}

// numbers only
function isNumeric(checkvalue, err)
{
	var error = "";
	var charpos = checkvalue.value.search("[^0-9]"); 
	if(checkvalue.value.length > 0 &&  charpos >= 0) 
	{ 
		error = err;
		checkvalue.className += ' invalidInput';
	}
	if (error == "") checkvalue.className.replace(' invalidInput','');
	return error;
}

// is currency
function isCurrency(checkvalue, err)
{
	var error = "";
	var charpos = checkvalue.value.search("[^0-9.]"); 
	if(checkvalue.value.length > 0 &&  charpos >= 0) 
	{ 
		error = err;  
		checkvalue.className += ' invalidInput';
	}
	if (error == "") checkvalue.className.replace(' invalidInput','');
	return error;
}

// alpha numbers only
function isAlpha(checkvalue, err)
{
	var error = "";
	var charpos = checkvalue.value.search("[^A-Za-z]"); 
	if(checkvalue.value.length > 0 &&  charpos >= 0) 
	{ 
		error = err;
		checkvalue.className += ' invalidInput';
	}
	if (error == "") checkvalue.className.replace(' invalidInput','');
	return error;
}    

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(checkvalue, err){
	var dtStr = checkvalue.value
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	var error = ""
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		error = err;  
		checkvalue.className += ' invalidInput'
	}
	if (strMonth.length<1 || month<1 || month>12){
		error = err;  
		checkvalue.className += ' invalidInput'
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error = err;  
		checkvalue.className += ' invalidInput'
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error = err;  
		checkvalue.className += ' invalidInput'
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error = err;  
		checkvalue.className += ' invalidInput'
	}

if (error == "") checkvalue.className.replace(' invalidInput','')
return error
}

