var err = 0;

function Validate() {
// expecting 3 arguments:
//	field = field to validate
//	text = text field name for error message
//	check = check string of format "X-Y"
//			X = R or O (required or optional)
//			Y = E - e-mail (checks for @ symbol)
//				T - text (checks for single or double quotes)
//				Nmin/max - number, min, max (min and max optional)
//				D - date

var i,j,k,n,field, text, check, required, type, errors='', focushere, min, max

// loop through each set of 3 arguments

	for (i=0; i<(Validate.arguments.length-2); i+=3) {

		field = Validate.arguments[i];
		text = Validate.arguments[i+1];
		check = Validate.arguments[i+2];

		if (!errors) {
			focushere = field;
		}

// parse check string:
		a = check.substring(0,1);
		if (a=="R") {
			required = true;
		}
		else {
			required = false;
		}

		type = check.substring(2,3);

// begin validation

	// Is this a required field?  Is it empty?
		if (required) {
			if (field.value=="") { // nothing in required field
				if (RepeatCheck(errors, text)) { // don't add to error message if already there
					errors += '- '+text+' is required.\n';
				}
				continue;
			}
		}
		else {
			if (field.value=="") { // empty field OK if optional
		 		continue;
			}
		}

	// Is this e-mail?
		if (type=='E') {
	        atPos = field.value.indexOf('@');
	        if (atPos < 1 || atPos == (field.value.length - 1)) {
				if (RepeatCheck(errors, text)) { // don't add to error message if already there
          			errors += '- '+text+' must contain an e-mail address.\n';
				} // end if Repeat Check
				continue;
			} // end check for @

		} // end if e-mail check

	// Is this a number?		
		if (type=='N') {
			if (check.length > 3) { // extra characters means min and max included
				minmax = check.substring(3,check.length);
				b = check.indexOf("/",3);
				min = check.substring(3,b);
				max = check.substring(b+1,check.length);
				err = NumberValidate(field, min, max);
			}
			else {
				err = NumberValidate(field);
			}

			if (err==1) {
				s = '- '+text+' requires a number';
				if (check.length > 3) {
					s = s+' between '+min+' and '+max;
				}
				s +="\n";
				if (RepeatCheck(errors, text)) { // don't add to error message if already there
					errors += s;
				}
			}
			continue; // finished number validation, continue with next field
		} // end if number check

		if (type=="D") {
			err = DateValidate(field);
			if (err==1) {
				if (RepeatCheck(errors, text)) { // don't add to error message if already there
					errors += '- '+text+" requires a valid date\n"
				}
			}
			continue;
		} // end if date check

	} // end for loop

  if (errors) {
		alert('The form was not submitted due to the following error(s):\n'+
               errors+'\nPlease make changes and submit the form again.');
		focushere.focus();
		}
  else {

// loop through each set of 3 arguments

	for (i=0; i<(Validate.arguments.length-2); i+=3) {

		field = Validate.arguments[i];
		text = Validate.arguments[i+1];
		check = Validate.arguments[i+2];

	// Check for single quotes - change to double
//		err = TextValidate(field);
	}

  }
  document.ValidateReturnValue = (errors == '');


} // end Validate

function RepeatCheck(e,n) {
	if (e.indexOf(n)==-1) {
		return true;
	}
	else {
		return false;
	}
} // end RepeatCheck

function NumberValidate(field, min, max) {
// checks to see if n is a number, then checks to see if it falls within min/max

	n = field.value;

	var err = 0;

	if (CheckNum(n)==false) {
			err = 1;
	}

// make sure n is treated as a number:
	n = n*10;
	n = n/10;
		
// Is this between min and max?
	if (NumberValidate.arguments.length > 1) {
		if ((n<min) || (n>max)) {
			err = 1;
		}
	}

	return err;

} // end NumberValidate

function CheckNum(n) {
// checks to see if n is a number

// Is this a number?
	for (i=0;i<n.length;i++) {
		found = false;
		t = n.substring(i,i+1);
		for (j=0; j<10; j++) {
			s = new String(j);
			if (t.indexOf(s)>=0) {
				found = true;
			}
			else {
				if (t.indexOf('.')>=0) {
					found = true;
				}

			} // end else
		}
		if (found==false) {	
			return false;
		}
	}
	return true;

} // end CheckNum

function DateValidate(field) {
// checks to see if date is in format "00/00/00"

	n = field.value;
	var err = 0

//Are there exactly two '/' or '-'?
	if (n.length==8)
		sep = new Array(8);
	else
		sep = new Array(10);


	count = 0;
	for (i=0;i<n.length;i++) {
		t = n.substring(i,i+1);
		if ((t.indexOf('/')>=0)||(t.indexOf('-')>=0)) {
			sep[count] = i;
			count++;
		}
	}
	if (count!=2) { // if not exactly 2 special characters, return error
		err=1;
	}
	else {	
	// otherwise, check for proper dates using numbers

		m = n.substring(0,sep[0]);
		if ((CheckNum(m)==false)||(m.length==0)) err=1;
		if (m<1 || m>12) err=1;
	
		d = n.substring(sep[0]+1, sep[1]);
		if ((CheckNum(d)==false)||(d.length==0)) err=1;
		if (d<1 || d>31) err=1;

		y = n.substring(sep[1]+1, n.length);

		if ((CheckNum(y)==false)||(y.length==0)) err=1;
		//if (y>=2000) y=y-2000;
		//if (y<0 || y>99) err=1;
		if (y<0 || (y>99 && (y<1990))) err=1;
	} // end else

	if (err==1) {
//		alert('Please enter a valid date using the format M/D/Y');
		return 1;
	}
// change all parts of date into length 2 strings
	if (m.length<2) {
		m = "0"+m;
	}
	if (d.length<2) {
		d = "0"+d;
	}
	if (y.length<2) {
		y = "0"+y;
	}
	field.value = m + "/" + d + "/" + y;

	return 0;

} // end DateValidate	

function TextValidate(field) {
	var q, front, i;
	var n = field.value;

// Replace single and double quotes with  ''

	front = "";
	while (n.indexOf("'")>=0) { // search for single quotes
		q = n.indexOf("'");
		front = front + n.substring(0,q) + "''"
		n = n.substring(q+1,n.length);
	}
	while (n.indexOf('"')>=0) { // search for double quotes
		q = n.indexOf('"');
		front = front + n.substring(0,q) + "''"
		n = n.substring(q+1,n.length);
	}
	
	field.value = front+n;
	return 0;

} // end TextValidate

function OldTextValidate(field) {
// checks to see if a value has been entered in field
	n = field.value;

// Is there a ' or " in the field?
	if ((n.indexOf('"')>=0)||(n.indexOf("'")>=0)) {
		//alert('Single or double quotes cannot be entered into the database.  Please edit your entry.');
		return 1; // signal calling function that there is an error
	}
	return 0;

} // end OldTextValidate

function ReadData(field, newfield)
{

		newtype = newfield.type;

//		alert ('newfield = '+newfield.name+'\n newtype = '+newtype);
		if ((newtype == 'text') || (newtype == 'textarea')) {
			newfield.value = field.value;
		}
		else {
			if (newtype == 'checkbox') {
//				check to see if value equals value of newfield -- means to check the box
				if (field.value == newfield.value) {
					newfield.checked = true;
				}
				else {
					newfield.checked = false;
				}
			//	changeProp(newfield);

			}
			else {
				if ((newtype =='select-one') || (newtype=='select-multiple')) {
//					alert('Selection menu! Length='+newfield.options.length);
//					check to see which was selected, then make that one selected in menu
					for (j=0; j < newfield.options.length; j++) {
						if (newfield.options[j].value == field.value) {
							newfield.options[j].selected = true;
						}
					} // end for j
				}
				else {
//					must be radio button!
//					alert('RADIO!! Length = '+newfield.length);
					for (j=0; j < newfield.length; j++) {
						if (newfield[j].value == field.value) {
							newfield[j].checked = true;
						//	changeProp(newfield[j]);

						}
					}

				}
			}
		}
		

} //end ReadData

function getFullYear(d) {
	var y = d.getYear();
	if (y > 100) {
		y = y - 100;
	}
	return y;
} // end getFullYear

function DateStamp() {
		n = new Date();
		day = n.getDate();
		month = n.getMonth()+1;
		year = getFullYear(n);
		s = month + '/' + day + '/' + year;
		return s;

} // end DateStamp

function TimeStamp() {
	n = new Date();
	d = DateStamp();
	h = n.getHours();
	if (h>12) {
		ampm = ' p.m.';
		h = h-12;
	}
	else {
		ampm = ' a.m.';
	}

	m = n.getMinutes();

	time = d+' '+h+ ':' + m + ampm;
	return time;

} // end TimeStamp

function PriceFormat(price) {
	var d = price.indexOf(".");
	if (d<0) {
		price = price + ".";
		d = price.length-1;
	}
	var delta = price.length - d - 1;
	while (delta < 2) {
		price = price+"0";
		delta = price.length - d - 1;
	}
	return price;	

} // end PriceFormat

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function CheckReload() {
//	alert("Current Cookie = "+Get_Cookie('approval'));
	if (Get_Cookie("approval")==null) {
		Set_Cookie("approval", "reload");
//		alert("Set Cookie! = "+Get_Cookie('approval'));
	}
	else {
		Delete_Cookie("approval");
		location.reload(true);
	}

} // end CheckReload
