// JavaScript Library 
// *************************************************
// by Tyler Queen
//
// Last edited 04/11/2002
//
// FUNCTIONS
// -------------------------------------------------
// * popupMsg - This function activates a system alert message.
//
// * startField - This function places the cursor in the designated text field
// 
// * checkMatch - This function compares two fields to see if they match.
//
// * popupWindow - This function opens a new popup window with specified attributes in the center of the screen.
//
// * checkBirthday - This function checks to see if the user is over 21
//
// * validateForm,findObj - These two functions work together to check for required fields, to see if fields 
//                           contain a valid email addresses, numbers, or the lentgh of the string passed.
//                           It can also check for a number between a certain range of numbers.
//                           (These two functions were inspired be the two Macromedia functions  MM_validateForm and MM_findObj)
//
// * validateDate - this function checks for a valid date.

// This function activates a system alert message.
function popupMsg(msg) { 
  alert(msg);
}

// This function places the cursor in the designated text field
function startField(theField) {
	theField.focus();
	}
	
// This function compares two fields to see if they match.
function checkMatch(fieldOne,fieldTwo) {
if (fieldOne != fieldTwo) {	
		alert (fieldOne+' doesn\'t match '+fieldTwo);
		return false; 
		} else { 
		return true; 
		}
	}

// This function opens a new popup window with specified attributes in the center of the screen.
function popupWindow(theURL,winName,w,h,features){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+features+'';
	window.open(theURL,winName,settings);
}

// This function checks to see if the user is over 21
function checkBirthday(userMonth,userDate,userYear){

	//converts form values into integers
	var newUserDate = parseInt(userDate);
	var newUserYear = parseInt(userYear);
	var newUserMonth = parseInt(userMonth);

	//adds form values to make one number
	var birthday = newUserYear+newUserDate+newUserMonth;

	//make a new variable to hold a date
	var date = new Date();

	//assigns a variable to month // one is added because js starts count at 0
	var todayMonth = date.getUTCMonth() + 1;

	//assigns a variable to the year 21 years ago represented in four digits
	var todayYear = date.getFullYear() - 21;

	//assings a variable to the date 
	var todayDate = date.getUTCDate();

	//adds the previous three variables together to make one number to compare against the users birthday
	var ofAgeYear = todayYear+todayMonth+todayDate;

	//checks to see if all items were selected
	if ((userMonth=='') || (userDate=='') || (userYear=='')){
		alert('Please select all Birthday Fields'); 
		return false;
	} else {	
		//compares the users birthday with 21 years ago today
		if (birthday > ofAgeYear) {
			alert('Your are under 21, and therefore cannot register');
			return false;
		} else {
			alert('You are over 21');
			return true;
		}
	}
}

// These two functions check for required fields, to see if fields contain a valid email addresses, numbers, or the lentgh of the string passed.
// It can also check for a number between a certain range of numbers.

function findObj(n, d) { 
  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=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { 
  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 (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(val)==false) errors+='- '+nm+' must contain a valid e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) 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 (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


function validateForm() { 
  var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      
      if (test.indexOf('isMonth')!=-1) {
       	var theMonth = val;
      }
      if (test.indexOf('isDay')!=-1) {
       	var theDay = val;
      }
      if (test.indexOf('isYear')!=-1) {     
      var theYear = val;       	
		if (validateDate(theMonth,theDay,theYear)==false) {
     	var shortName = nm.replace('_year','');
     	errors+='- Please enter a valid '+shortName+'.\n';
     	var shortName = '';
     	var theYear = '';
     	var theMonth = '';
     	var theDay = '';
     	}
      }
      if (test.indexOf('isEmail')!=-1) { 
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val)==false) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) 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 (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.returnValue = (errors == '');
}



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(dtStr){
	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)
	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){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function validateDate(userMonth,userDay,userYear){
	
	//converts form values into integers
	//var newUserMonth = parseInt(userMonth);
	//var newUserDay = parseInt(userDay);
	//var newUserYear = parseInt(userYear);
	

	//adds form values to make one number
	var dt = (userMonth+"/"+userDay+"/"+userYear);
	if (isDate(dt)==false){
		return false
	}else{
    return true}
 }
 
function setFocus(objectName) {
    objectName.focus();
}

function ValidateEmail(strEmail) {
		if (strEmail.indexOf("@") < 1) {
			alert("I'm sorry. This email address seems wrong. Please check the username or include the '@' sign.");
			return false;
		}
		if (strEmail.indexOf(".") < 3) {
			alert("I'm sorry. This email address seems wrong. Please check the domain for accuracy. (It should include a valid suffix.)");
			return false;
		}
		if (strEmail.length < 6) {
			alert("I'm sorry. This email address seems wrong. It is too short to be a valid email address.");
			return false;
		}
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(strEmail)==false) {
			alert("I'm sorry. The suffix on this email address does not appear to be valid. (It should be something like .com or .net)");
			return false;
		}
		return true;
	}
 
