var sErrorFields = '';
var sErrorTexts = '';

function setFormFocus(f, e) {
	if(e) {
		f.elements[e].focus();
	}
}
function highlightErrors(f, s) {
	if(s.length > 0) {
		a = s.split(',');
		if(a.length > 0) {
			for(i = 0; i < (a.length -1); i++) {
				if(a[i].toString().length > 0) { f.elements[a[i]].style.backgroundColor = '#EBF8E6'; }
			}
			setFormFocus(f, a[0])
		}
	}
}

function unhighlightErrors(f, s) {
	a = s.split(',');
	if(a.length > 0) {
		for(i = 0; i < (a.length -1); i++) {
			if(a[i].toString().length > 0) { f.elements[a[i]].style.backgroundColor = ''; }
		}
	}
}


function addError(errorText, errorField) {
	sErrorTexts += '   ' + errorText + '\n';
	sErrorFields += errorField + ',';
}



function emailValid(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){ return false}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false }
	if (str.indexOf(at,(lat+1))!=-1){ return false }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false }
	if (str.indexOf(dot,(lat+2))==-1){ return false }		
	if (str.indexOf(" ")!=-1){ return false }
	return true
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
    var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        return false;
    } else {
        return true;
    }
}
   
//validates that the entry is a positive or negative number
function isNumber(elem) {
    var str = elem.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString( );
    if (!str.match(re)) {
        return false;
    }
    return true;
}
   
// validates that the entry is 16 characters long when
// input field's maxlength attribute is set to 16
function isLen16(elem) {
    var str = elem.value;
    var re = /\b.{16}\b/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }
}
   
// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
    var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }
}
