﻿function phonemask(t)
{var patt1 = /(\d{3}).*(\d{3}).*(\d{4})/;
var patt2 = /^\((\d{3})\).(\d{3})-(\d{4})$/;
var str = t.value;
var result;
if (!str.match(patt2))
{result = str.match(patt1);
if (result!= null)
{t.value = t.value.replace(/[^\d]/gi,'');
str = '(' + result[1] + ') ' + result[2] + '-' + result[3];
t.value = str;
}else{
if (t.value.match(/[^\d]/gi))
t.value = t.value.replace(/[^\d]/gi,'');
}}}

var nbsp = 160;
var node_text = 3;
var emptyString = /^\s*$/ ;
var global_valfield;

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}

function msg(fld,
             msgtype,
             message)
{
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  elem.className = msgtype;
}

var proceed = 2;  

function commonCheck    (valfield,
                         infofield,
                         required)
{
  if (!document.getElementById) 
    return true;
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;
  if (elem.firstChild.nodeType != node_text) return true;

  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "error", "Required");  
      setfocus(valfield);
      return false;
    }
    else {
      msg (infofield, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

function validateAlphanumeric(valfield, infofield){

	var stat = commonCheck (valfield, infofield, true);
	if (stat != proceed) return stat;

	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(!valfield.value.match(alphaExp)){
	    msg (infofield, "error", "Letters and numbers only!");
	    setfocus(valfield);
	    return false;
  }

  msg (infofield, "warn", "");  
  return true;
}

function validateCompare	(valfield1,
							valfield2,
							infofield)
{
  var stat = commonCheck (valfield1, infofield, true);
  if (stat != proceed) return stat;

  if (valfield1.value != valfield2.value) {
    msg (infofield, "error", "Passwords do not match!");
    setfocus(valfield1);
    return false;
  }

  msg (infofield, "warn", "");  
  return true;
}

function commonCheck2   (vfld,
                         ifld)
{
  if (!document.getElementById) 
    return true;
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;
  if (elem.firstChild.nodeType != node_text)
    return true;

  msg (ifld, "warn", "");
  return proceed;
}

function validatePresent(valfield,
                         infofield)
{
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");  
  return true;
}

function validateEmail  (valfield,
                         infofield,
                         required)
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "error", "Not a valid e-mail address");
    setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) 
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
  else
    msg (infofield, "warn", "");
  return true;
}

function validateTelnr  (valfield,
                         infofield,
                         required)
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "error", "Not a valid telephone number.");
    setfocus(valfield);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (infofield, "error", "" + numdigits + " digits - too short");
    setfocus(valfield);
    return false;
  }

  if (numdigits>14)
    msg (infofield, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (infofield, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (infofield, "warn", "");
  }
  return true;
}

function validateAge    (valfield,
                         infofield,
                         required)
{
  var stat = commonCheck (valfield, infofield, required);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  var ageRE = /^[0-9]{1,3}$/
  if (!ageRE.test(tfld)) {
    msg (infofield, "error", "Not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>=200) {
    msg (infofield, "error", "Not a valid age");
    setfocus(valfield);
    return false;
  }

  if (tfld>110) msg (infofield, "warn", "Older than 110: check correct");
  else {
    if (tfld<7) msg (infofield, "warn", "Bit young for this, aren't you?");
    else        msg (infofield, "warn", "");
  }
  return true;
}

function validateConfirm   (vfld,
                            ifld)
{
  var stat = commonCheck2(vfld, ifld);
  if (stat != proceed) return stat;

  if (vfld.checked) return true;

  var errorMsg = 'Please confirm you agree to message.';

  msg (ifld, "error", errorMsg);
  return false;
}

function validateRadio   (vfld,
                            ifld)
{
  var stat = commonCheck2(vfld, ifld);
  if (stat != proceed) return stat;

  var cnt = -1;
  for (var i=vfld.length-1; i > -1; i--) {
      if (vfld[i].checked) {cnt = i; i = -1;}
  }
  if (cnt > -1) return true;

  var errorMsg = 'Select One.';

  msg (ifld, "error", errorMsg);
  return false;
}

//auto tab script
//format to use -- onKeyUp="return autoTab(this, 3, event);"
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
if(isNN)
//  document.captureEvents(Event.KEYPRESS);
	function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
	var found = false, index = 0;
		while(!found && index < arr.length)
	if(arr[index] == ele)
		found = true;
	else
		index++;
	return found;
	}
	function getIndex(input) {
	var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
	}
return true;
}

function valFirstName() {
	if (document.userManage.firstName.value.length < 2) {
			document.getElementById("inf_firstName").innerHTML="Please enter a first name.";
			return false;
	}else{
			document.getElementById("inf_firstName").innerHTML="*";
			return true;
	}
}

function valLastName() {
	if (document.userManage.lastName.value.length < 2) {
			document.getElementById("inf_lastName").innerHTML="Please enter a last name.";
			return false;
	}else{
			document.getElementById("inf_lastName").innerHTML="*";
			return true;
	}
}

function valuserName() {
	if (document.userManage.username.value.length < 4) {
			document.getElementById("inf_username").innerHTML="Min username length is 4.";
			return false;
	} else if (document.userManage.username.value.length > 10) {
			document.getElementById("inf_username").innerHTML="Max username length is 10.";
			return false;
	}else{
			document.getElementById("inf_username").innerHTML="*";
			return true;
	}
}

function valPassword() {
	if (document.userManage.password.value.length < 4) {
			document.getElementById("inf_password").innerHTML="Min password length is 4.";
			return false;
	} else if (document.userManage.password2.value.length < 4) {
			document.getElementById("inf_password2").innerHTML="Min password length is 4.";
			return false;
	} else if (document.userManage.password2.value != document.userManage.password.value) {
			document.getElementById("inf_password").innerHTML="Passwords do not match.";
			document.getElementById("inf_password2").innerHTML="Passwords do not match.";
			return false;
	}else{
			document.getElementById("inf_password").innerHTML="*";
			document.getElementById("inf_password2").innerHTML="*";
			return true;
	}
}

function valEmail() {
	if (document.userManage.email.value.length < 2) {
			document.getElementById("inf_email").innerHTML="Please enter an email address.";
			return false;
	}else{
			document.getElementById("inf_email").innerHTML="*";
			return true;
	}
}

function valNumber() {
	if (document.userManage.contactNum.value.length < 14) {
			document.getElementById("inf_contactNum").innerHTML="Please enter a valid contact phone number.";
			return false;
	}else{
			document.getElementById("inf_contactNum").innerHTML="*";
			return true;
	}
}

function valSponsorName() {
	if (document.sponsorManage.sponsorName.value.length < 3) {
			document.getElementById("inf_sponsorName").innerHTML="Please enter a sponsor business name.";
			return false;
	}else{
			document.getElementById("inf_sponsorName").innerHTML="*";
			return true;
	}
}

function valSponsorSeason() {
	if (document.sponsorManage.sponsorId.value=0 ) {
			document.getElementById("inf_season").innerHTML="Please select a season.";
			return false;
	}else{
			document.getElementById("inf_season").innerHTML="*";
			return true;
	}
}

function valSponsorContactNum() {
	if (document.sponsorManage.contactNum.value.length < 14) {
			document.getElementById("inf_contactNum").innerHTML="Please enter a valid contact phone number.";
			return false;
	}else{
			document.getElementById("inf_contactNum").innerHTML="*";
			return true;
	}
}


