
// BBBonline window popper
function Certify(URL) {
  popupWin = window.open(URL, 'Participant', 'location,scrollbars,width=800,height=600')
  window.top.name = 'opener';
}

function allTrim(cValue){
  var lDone=false;
  while (lDone==false){
    if (cValue.length==0) {return cValue;}
    if (cValue.indexOf(' ')==0){cValue=cValue.substring(1);lDone=false; continue;}
    else {lDone=true;}
    if (cValue.lastIndexOf(' ')==cValue.length-1){cValue=cValue.substring(0, cValue.length-1);lDone=false;continue;}
    else {lDone=true;}
  }
  return cValue;
}

function valPhone(pValue) {
	
	// validates for a ###-###-#### input value structure
	// returns true if it matches the pattern, false if not
	
	// split the input value into an array on the hyphens
	var tPhoneArr = pValue.split('-');
	
	// check the array for 3 items
	if (tPhoneArr.length < 3) {
		return false;
	}
	
	// check each of the items for numeric data type
	for ( var i=0; i<tPhoneArr.length; i++ ) {
		if ( isNumber(tPhoneArr[i]) == false ) {
			return false;
		}
		
		switch (i) {
			case 0 :
				if (tPhoneArr[0].length != 3) {
					return false;
				}
				break
			case 1 :
				if (tPhoneArr[1].length != 3) {
					return false;
				}
				break
			case 2 :
				if (tPhoneArr[2].length != 4) {
					return false;
				}
				break
			default : statement
		}

	}
	
	// all is good, return true
	return true;
	
}

function isNumber(inputname) 
{
	var valid = "0123456789" ;
	var temp;
	
	if ((inputname != "") || (inputname != "undefined"))
	{
		for (var i=0; i<inputname.length; i++) 
		{
			temp = "" + inputname.substring(i, i+1);
			if (valid.indexOf(temp) == -1) 
				return false;
	
		}
		return true ;
	}
	
	return false;
}

function valInputNumber(lcValue, lnMinLen, lnMaxLen, lnErrorOnEmpty) 
{ 
  // check if empty 
  if (StrTran(lcValue, ' ', '').length < 1) 
	{
	if (lnErrorOnEmpty==1)
		return 'ERROR: empty1'; 
	else
		return lcValue ;
	}
  //replace spaces with commas so consecutive values are not concatenated  
  lcValue = StrTran(lcValue, ' ', ',') ;
  lcValue = StrTran(lcValue, ',,', ',') ;

  // empty check if only entered commas
  if (StrTran(lcValue, ',', '').length < 1) 
	return 'ERROR: empty2'; 

  //remove comma if last char
  if (lcValue.substring(lcValue.length-1, lcValue.length) == ',') 
	lcValue = lcValue.substring(0, lcValue.length-1) ;

  var laString = lcValue.split(','); 
  for (i=0;i<laString.length;i++)
  {
	if (isNumber(laString[i]) == false)
		return 'ERROR: notnumber' ;
	if (laString[i].length < lnMinLen || laString[i].length > lnMaxLen)
		return 'ERROR: length' ;
  }
  return lcValue ;
} 

function StrTran(lcValue, lcToReplace, lcReplaceWith) 
{ 
	while (lcValue.indexOf(lcToReplace) > -1) 
	{ 
		lcValue = lcValue.replace(lcToReplace, lcReplaceWith) ;
	} 
		return lcValue ;
} 

function isValidDate (iYear, iMonth, iDay) {
 if ( isNaN(iYear) || isNaN(iMonth) || isNaN(iDay) ){
  return false;
 }
 
 var myDate = new Date(iYear, iMonth-1, iDay);
 if ( myDate.getFullYear() != iYear || myDate.getMonth()+1 != iMonth || myDate.getDate() != iDay ) {
  return false
 }
 
 return true;
}

function checkYear(iYear) {
	iYear.value = allTrim(iYear.value)
	if ( isNaN(iYear.value) ) { // is a number?
		return false;
	}
	//if ( iYear.value.length ==2 ){ // 2 digit year?
	//	iYear.value = "20" + iYear.value.toString();
	//}
	if ( iYear.value.length !=4 ){ // not 4 digit year?
		return false;
	}
	return true;
}

function approveAge(birthYear,birthMonth,birthDay) {

  now = new Date();
  now_year = now.getFullYear();
  now_month = now.getMonth() + 1;
  now_day = now.getDate();


  //check input
  var passAge = 18;
  var passed = 0;
  if (birthDay >= 1 && birthDay <= 31 && birthMonth >= 1 && birthMonth <= 12 && birthYear >= 1899 && birthYear <= now.getFullYear()){
    if ((now_year - birthYear) == passAge){
      if (birthMonth < now_month){
        passed = 1;
      } else if (birthMonth == now_month && birthDay <= now_day){
        passed = 1;
      }
    } else if (now_year - birthYear > passAge){
      passed = 1;
    } else {
      passed = 0
    }

  } else {
    passed = 0;
  }
  return passed;
}

// browser detection
var ns4 = (document.layers) ? true:false // Netscape 4.x
var ie4 = (document.all) ? true:false // IE 4.x
var ie5 = (document.getElementById) ? true:false // Browers that really matter

// hides an element
function hideElement(strName) { 
 if (ie5) {
  document.getElementById(strName).style.display='none';
  document.getElementById(strName).style.visibility = "hidden"; 
 } else if (ns4) {
  document.layers[strName].visibility = "hide"; 
  // for this to work with ns4, you must set your div to absolute positioning
  //else if (ns4) eval('document.'+strName+'.visibility = "hide"'); // alternate method
 } else if (ie4) { 
  eval(strName+'.style.visibility ="hidden"');
 } 
}

function bwwNewWindow(strMyPage, strMyName, intWidth, intheight, scroll) {
var intWidth;   // width and height of the popup window, these values are in the anchor link
var intheight;  //

var intWinLeft = 200; // location of the popup window, this is set at the center of screen
var intWinTop = 200;
var strWinprops = 'height='+intheight+',width='+intWidth+',top='+intWinTop+',left='+intWinLeft+',scrollbars='+scroll+',resizable=no'; 
var strwin = window.open(strMyPage, strMyName, strWinprops);
if (parseInt(navigator.appVersion) >= 4) { strwin.window.focus(); }
}
