
// Most of this is borrowed from
// http://www.tizag.com/javascriptT/javascriptform.php
// FIXME our zip code validation is minimal, only numeric



function isStateAbbrev(elem, helperMsg){
  var stateExpression = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
  if(elem.value.match(stateExpression)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function isZip(elem, helperMsg){
  var zipExpression = /^\d{5}$|^\d{5}-\d{4}$/;
  if(elem.value.match(zipExpression)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// One xor the other element must have content
function exclusiveEntry(elem1, elem2, helperMsg){
  if(
     (elem1.value.length == 0 && elem2.value.length == 0)
     || (elem1.value.length != 0 && elem2.value.length != 0)
     ){
    alert(helperMsg);
    elem1.focus();
    return false;
  }
  else{
    return true;
  }
}

function isEmpty(elem, helperMsg){
  if(elem.value.length == 0){
  //if(elem.value == ''){
    alert(helperMsg);
    elem.focus();
    return true;
  }
  return false;
}

function isAlphabet(elem, helperMsg){
  var alphaExp = /^[a-zA-Z]+$/;

  if(elem.value.match(alphaExp)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}


function testpopup(elem, helperMsg){
  alert(elem.value);
  elem.focus();  
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function emailValid(elem, helperMsg){
  // From http://regexlib.com/REDetails.aspx?regexp_id=541
  // var emailExp = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
  var emailExp =/^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/;
  //var emailExp =/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
  // var emailExp =/^\w+.*@\w+/;

  if(elem.value.match(emailExp)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function emailSame(elem1, elem2, helperMsg){
      if( elem1.value == elem2.value ){
	return true;
      }
      else{
	alert(helperMsg);
	elem1.focus();
	return false;
      }
}

function madeSelection(elem, defaultValue, helperMsg){
	if(elem.value == defaultValue || elem.value == ""){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function radioChecked(elem, helperMsg){
  // Loop from zero to the one minus the number of radio button selections
  for (counter = 0; counter < elem.length; counter++){
    // If a radio button has been selected it will return true
    // (If not it will return false)
    if (elem[counter].checked)
      return true; 
  }
  alert(helperMsg);
  return false;
}

function checkboxChecked(elem, helperMsg){
  if(elem.checked){
    return true;
  }
  else{
    alert(helperMsg);
    return false;
  }
}

function popup (url, name, height, width){
  newwindow=window.open(url, name, 'height=' + height + ',width=' + width + ',resizable=yes,scrollbars=no,toolbar=no,status=yes');
 if (window.focus) {newwindow.focus()}
}
