// javaCommon.js
function alertshow(formIn) {
	      alert ("The formIn is [" + formIn + "]. Hey, you are at the function")
}

function email2User(u)
   {
      if (userForm.useEmail.checked) {

		document.userForm.newusername.value = document.userForm.newemail.value;
		} else {
		document.userForm.newusername.value = u;
	  }
}

function goAction(actn) {

    if (actn = 'userForm') {
		document.userForm.newusername.value = 'operatorCancel';
    }
    else alert ('Action for [ ' + actn + ' ] not defined!')
//	str = "Form Elements of form : \n "   
//	for (i = 0; i < 10; i++)       
//		str += formIn.elements[i].name + "[" + formIn.elements[i].value + " \n"   
//    alert(str)
} 


function userNameValid(formIn) {
	  //var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	  //var sRegExTest= /^[@_0-9a-z]{6,}$/i
	  var sRegExTest= /^[@._0-9A-Za-z]{6,}$/i
	  //var lengthTest= /^[_0-9a-z]{6,}$/i
      var v = formIn.newusername.value

      if (v == formIn.newemail.value) {
	    return true
	  }


	  if (v == "") {
		alert ("Username must NOT BE BLANK!")
		formIn.newusername.focus()
		return false
	  }

 	  //if(v.indexOf(' ') != -1 ){
	  //   alert("Your username cannot have spaces!");
	  //	 return false
      // }

//      if(sRegExTest.test(v)){
//	  }

//      if(lengthTest.test(v)){
//		alert("Your username must be six (6) characters long!")
//		return false
//	  }
	  
	  if(sRegExTest.test(v)){
	     // alert("validated true");
	     return true
	  } else{
	     alert("Your username does not meet the required criteria.")
	     return false
	  }
} 
// FIXTHIS:  I am not sure if this is ever used???
function jumpPage(newLoc) {
  	newPage = newLoc.options[newLoc.selectedIndex].value
	if (newPage != "") {
		window.location.href = newPage
	}
}

function disableEnterKey() { 
   if (window.event.keyCode == 13) {
     alert ("You must use the TAB or ARROW keys to move between fields.  The ENTER key cannot be used!");
	 window.event.keyCode = 0; 
   }
}  

function checkAll(formIn, eleIn) {
   var setto = false;
   if (document.forms[formIn].elements['check_all'].checked) {
     setto=true; 
   }
    
   chk = document.forms[formIn].elements[eleIn];
   for (i = 0; i < chk.length; i++) {
      chk[i].checked = setto ;
   }
} // end function checkAll(formIn, eleIn)

function cancelAttendee()  {
//	  var cancel = document.userSubmit.cancel
//	  var cancel = "yes"  
	  if (document.userSubmit.cancel.checked == true) {
          alert ('Registration cancelled! \n\n You must hit the ENTER RECORD button to finalize cancellation.')
	      document.userSubmit.a_typeSelect.value = ""
	  }
	  if (document.userSubmit.cancel.checked == false) {
	      document.userSubmit.a_typeSelect.value = ""
      }
}

function skipIt() {
//   alert ('You have choosen to SKIP this registration!')
//	  var cancel = document.userSubmit.cancel
//	  var cancel = "yes"  
   document.userSubmit.entryType.value = "Skip";
   document.userSubmit.submit();
}

function attendEditCk(p_form) {
// note: if you the p_form MUST HAVE these field names or the Java script returns to the main script with an undefined value...
// you also need to make sure you only have ONE field with this name.
// there is special handling for check boxes...
  var typeSelect = p_form.a_typeSelect.value;
  var cancelBox = p_form.cancel.value;
  var first = p_form.a_tagF.value;
  var last = p_form.a_tagL.value;
  if (p_form.cancel.checked == true) {
     var cancelBox = "yes"
  } else {
     var cancelBox = "no"
  }  
  
  var skipper = p_form.entryType.value;
  
  if (skipper == "Skip") { return true }
  if (skipper == "Cancel") { return true }
//  alert ('Cancel box is [' + cancelBox + '] and TypeSelect is (' + typeSelect + ')')
//  alert ('Value of action is [' + action + ']')
//  alert ('Value of firstsubmit is [' + firstSubmit + '] and second is [' + secondSubmit + '] with Skipper at [' + skipperVal + ']')
  
//  if (submitVal == "Skip") { return true }
  
  if (first == "") {
	alert ("You must enter a FIRST name.")
	p_form.a_tagF.focus()
	return false
  }
	
  if (last == "") {
	alert ("You must enter a LAST name.")
	p_form.a_tagL.focus()
	return false
  }


  if (typeSelect == "expCanCell" && cancelBox == "no") {
	 alert ("You must check the 'cancel box' to cancel this attendee!")
	 return false;
  }

  if (typeSelect == "expCanCell" && cancelBox == "yes") {
	 alert ("By checking the box you have acknowledged that you understand\n\n" +
			"and accept any cancellation implications.  If you have questions,\n\n" +
			"please use the contact link in the lower left menu bar \n\n" +
			"for additional information and questions.")
	 return true;
  }
			
	  
  if (typeSelect > "" && cancelBox == "yes") {
    alert ("You have checked the cancel box, but you also have selected a \n\n" +
		   "Registration Type. Hit ENTER RECORD if you wish to cancel this \n\n" +
		   "registration.  You must uncheck the cancel box if you wish this\n\n" +
		   "remain a valid registration.")
    p_form.a_typeSelect.value = "Cancelled"			
	p_form.a_typeSelect.focus()
	return false
  }

  if (typeSelect == "" && cancelBox == "no") {
       alert ('You must choose one of the valid registration types in order\n\n' +
			  'to continue. If you are uncertain, make your best guess and come\n\n' +
			  'back later to edit or change it.\n\n' +
			  'To quit, use the Skip key.')
	   p_form.a_typeSelect.focus()
	   return false
  }
}


function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var yrStrt = 1900;
var yrRng = 200;
var SiteKey = 1000;
var rtnFld = '';
var year1  = y2k(today.getYear())*2;
var sc_typeRtn = '';
var sc_schoolRtn = '';
var sc_shortRtn = '';
var sc_classRtn = '';

function padout(number) { return (number < 10) ? '0' + number : number; }

function restart() {
// FIXTHIS: Change so that you can send in the variables for the form and field name...
   switch(rtnFld) {
     case "p_bday" : document.userSubmit.p_bday.value = '' + year + '-' + padout(month - 0 + 1) + '-' + padout(day); break;
     case "arrival" : document.userSubmit.arrival.value = '' + year + '-' + padout(month - 0 + 1) + '-' + padout(day); break;
     case "depart" : document.userSubmit.depart.value = '' + year + '-' + padout(month - 0 + 1) + '-' + padout(day); break;
	 case "p_school" : {
//	 		document.userSubmit.sc_type.value = sc_typeRtn; 
	 		document.userSubmit.p_school.value = sc_schoolRtn; 
	 		document.userSubmit.sc_short.value = sc_shortRtn; 
	 		document.userSubmit.sc_class.value = sc_classRtn; break; }
	 default : alert ('jCom.135: the action for field [ ' + rtnFld + ' ] is not defined.  Script will not work!  Contact Webmaster.');
	 } 
    mywindow.close();
}

function schoolUpdate(URL, params, rtnForm, SiteKey) {
 //change this back to not include rtnForm...
 //make a separate function this that brings in the size.. etc.
//declare rtnForm as you do the next thing...
// alert ('javaCommon.143: The p_school value [' + document.userSubmit.p_school.value + '].');
    sc_schoolRtn = document.userSubmit.p_school.value;
	sc_shortRtn = document.userSubmit.sc_short.value;
    sc_classRtn = document.userSubmit.sc_class.value;
    newWindow(URL, params, rtnForm, SiteKey);
}

function calWindow(URL,startYear, yrRange, rtnForm, SiteKey) {
    if (rtnForm == "p_bday") {
	  yrStrt = year-110; yrRng = 110; 
	} else {
      yrStrt = startYear;
	  yrRng = yrRange;
	}
    calSite = SiteKey;
    newWindow(URL,'resizable=no,width=350,height=270',rtnForm,SiteKey);
}

function newWindow(URL, params, rtnForm, SiteKey,SessVal) {
 //change this back to not include rtnForm...
 //make a separate function this that brings in the size.. etc.
//declare rtnForm as you do the next thing...
//    sc_schoolRtn = document.userSubmit.p_school.value;
//    sc_classRtn = document.userSubmit.sc_class.value;
//  SiteKey = URL.substring(19,30);
//	SiteKey = SiteKey;
	rtnFld = rtnForm;
    mywindow=open(URL,'myname',params); 
    mywindow.location.href = URL;
    if (mywindow.opener == null) mywindow.opener = self;
}

function openPopCenter(pageURL, title,w,h,s) { //w = width; h=height s=scroll (yes or no)
  var left = (screen.width/2)-(w/2);
  var top = (screen.height/2)-(h/2);
  var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars='+s+', resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 


function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

