if (document.images) {
	val_x = new Image(); 
	val_x.src = "/images/x.gif";
	val_ok  = new Image(); 
	val_ok.src  = "/images/ok.gif";
}

// =============    globals   ====================

var activeEle = null;  // essentially used as "this" to make an almost OO on inputs
var isBlurValidating; 
var inputDb = new Object();
var submitClicked = false;


// ===============================================

/*
function myGetElementById(id) {
  for (i=0; i<document.forms.length; i++) {
    f = document.forms[i];
    if (typeof(f[id]) != "undefined") {
      return (f[id]);
    }
  }
}
*/
function isIe() {
  if (document.all) {
	  return true;
	}
	return false;
}

function hideStateIfNotUsCa(stateFieldName, countryFieldName) {
	// skip if NS
	if (! isIe()) {
	  return true;
	}
  eleState = myGetElementById(stateFieldName);
  eleCountry = myGetElementById(countryFieldName);
	if (eleCountry.value == 'US' || eleCountry.value == 'CA') {
		//eleState.style.visibility = "visible";
		eleState.disabled = false;
	}
	else {
		//eleState.style.visibility = "hidden";
		eleState.selectedIndex = 0;
		eleState.disabled = true;
	}
	return true;
}

function blurValidate(ele) {
	isBlurValidating = true;
	eleName = ele.name;
	validateName(eleName);
}

function validateAll() {
	var ok;
	var firstProblemElement; // for focus later
	ok = true;
  isBlurValidating = false;
	if (typeof(inputDb) == "undefined") {
	  return (true);
	}
	for (n in inputDb) {
		if(! validateName(n)) {
			//alert("" + ok);
			ok = false;
			if(typeof(firstProblemElement)=="undefined") {
				//alert(typeof(firstProblemElement));
				//alert(activeEle.name);
				firstProblemElement = activeEle;
				//alert(typeof(firstProblemElement));
			}
		}
	}
	//alert ("validateAll about to return: " + ok);
	if(typeof(firstProblemElement) != "undefined") {
		firstProblemElement.focus();
	}
	return (ok);
}
                                                                                              
function validateName(eleName) {
	var strFunc;
	activeEle = myGetElementById(eleName);
	if (typeof(inputDb[eleName]["validationFuncs"]) == "undefined") {
	  return true;
	}
	if (inputDb[eleName]["validationFuncs"].length > 0) {
		for (n in inputDb[eleName]["validationFuncs"]) {
			strFunc = inputDb[eleName]["validationFuncs"][n];
			isOk = eval(strFunc)                                                                      
			if (! isOk) {
				break;
			}
		}
		markIsOk(activeEle, isOk);
	}
	return(isOk);
}

function markIsOk(ele, isOk) {
	if (typeof(ele) == "undefined") {
	  return (false);
	}
  if (isOk) {
		myGetElementById('req_' + ele.name).src="/images/ok.gif";
	}
	else {
		myGetElementById('req_' + ele.name).src="/images/x.gif";
	}
}

function submitIfSure(f, message) {
  if (confirm(message, false)) {
	  f.submit();
	}
	else {
	  return (false);
	}
}

function submitIfValid(f) {
  //alert (f.name);
	if (validateAll()) {
		if (! submitClicked) {
			submitClicked = true;
			f.submit();
		}
	}
	else {
	  alert("Some fields are invalid or missing. Please fill out all required fields and try again.");
	}
}

// ===========  specific validations =============

function doesExist() {
	if (typeof(activeEle) != "undefined") {
		if (activeEle.type == 'select-one') {
			return (activeEle.options[activeEle.selectedIndex].value != '');
		}
		return (! activeEle.value == '');
	}
	else {
	  return (false);
	}
}

function isValidEmail() {
	str = activeEle.value;
	//alert(str);
	//return (str.indexOf(".") > 2) && (str.indexOf("@") >= 1);
	return str.indexOf("@") >= 1;
}

function foc(id){
	id.focus();
}

function matchesField(fieldName) {
	str1 = activeEle.value;
	str2 = myGetElementById(fieldName).value;
	ok = (str1 == str2);
  return (ok);
}

function hasAtLeast(x) {
  ok = (activeEle.value.length >= x);
	if (! ok) {
	  //alert ("You need at least " + x + " characters in this field");
	}
  return (ok);
}

function checkLimit(ele, limit) {
	num_checked=0;
  for (i=0; i<ele.form.length; i++) {
	  //currEle = ele.form.elements(i);
	  currEle = ele.form[i];
	  if (currEle.name == ele.name) {
      if (currEle.checked) {
			  num_checked++;
				//alert(currEle.name + ": " + currEle.value + ": " + currEle.checked);
			}
		}
	}
	//alert("checked: " + num_checked);
	if (num_checked > limit) {
		alert("You may only select " + limit + " items. \n\n If you wish to add this keyword, " + "you must \n uncheck a a previous selection before continuing");
	  ele.checked = false;
		ele.focus();
	}
}

