emptyString = /^\s*$/

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};

function msg(ifld) // string to display
{
  // setting an empty string can give problems if later set to a
  // non-empty string, so slip in an nbsp if needed


 var elem = document.getElementById(ifld);

 elem.className = elem.className + ' error';
};

var proceed =2;
function commonCheck    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld);
      vfld.focus();
      return false;
    }
    else {
   	 	var elem = document.getElementById(ifld);
 		elem.className = '';
      return true;
    }
  }
  return proceed;
}

function chkValue(vfld,   // element to be validated
                         ifld )  // id of element to receive info/error msg
{
  var stat = commonCheck (vfld, ifld, true);
  if (stat != proceed) return stat;
   	 	var elem = document.getElementById(ifld);
 		elem.className = '';
  return true;
};
 
 
function chkEmail  (vfld,   // element to be validated
		  ifld,   // id of element to receive info/error msg
		  reqd, multiple)   // true if required
{
	var stat = commonCheck (vfld, ifld, reqd);
	if (stat != proceed) return stat;

	if (chkValue(vfld,ifld)) { // check to see if anything is entered

		var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
		var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
		if (!email.test(tfld)) {
			alert("Invalid email");
			msg (ifld);
			vfld.focus();
			return false;
		}

		var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
		if (!email2.test(tfld)) {
			return confirm("Unusual e-mail address - proceed anyway?");
		}else{
			return true;
		}

	} else {
		alert("Invalid email");
		msg (ifld);
		return false;
	}
};

function chkSelect(vfld,ifld) {
    
    var success = true;
    var sText = vfld.value;

    if ((!sText) || (sText<0)) {
		msg (ifld);
		vfld.focus();
		return false;
	}
	
	var elem = document.getElementById(ifld);
	elem.className = '';
	return true;
}

function chkList(vfld,ifld) {
    
    var success = true;
    var sText = vfld.value;

	if (vfld.length>0) {
		var elem = document.getElementById(ifld);
		elem.className = '';
		return true;
	}
	
	msg (ifld);
	vfld.focus();
	return false;
}

function chkDate(vfld,ifld) {
	
	if (chkValue(vfld,ifld)) { // check to see if anything is entered
	
		var arrDate = vfld.value.split('/');
		if ((arrDate.length != 3) || (arrDate[0].length != 2) || (arrDate[1].length != 2) || (arrDate[2].length != 4) || (arrDate[0]>12) || (arrDate[1]>31)){

			msg (ifld);
			vfld.focus();
			return false;
		}
		var elem = document.getElementById(ifld);
		elem.className = '';
		return true;
	} else {
		msg (ifld);
		return false;
	}
}

function chkNumeric(vfld,ifld) { //  check for valid numeric strings
	
	if (chkValue(vfld,ifld)) { // check to see if anything is entered

		var strValidChars = "0123456789.";
		var strChar;
		var blnResult = true;
		
		var strVal = vfld.value

		//  test strString consists of valid characters listed above
		for (i = 0; i < strVal.length && blnResult == true; i++) {
			strChar = strVal.charAt(i);
			if (strValidChars.indexOf(strChar) == -1) {
				blnResult = false;
			}
		}
		
		if (blnResult) {
			var elem = document.getElementById(ifld);
			elem.className = '';
			return true;
		} else {
			msg (ifld);
			vfld.focus();
			return false;
		}
		
	} else {
		msg (ifld);
		vfld.focus();
		return false;
	}
}

function chkRadio(vfld,ifld) {
	for(i=0;i<vfld.length;i++){
		if (vfld[i].checked == true) {
		if(ifld) {
			var elem = document.getElementById(ifld);
			elem.className = '';
		}
			return true;
		}
	}
	
	if(ifld) msg (ifld);
	return false;
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function formatField(text) {
	// Need all for proper cut and paste into form then insert to DB
    
    text = replace(text,"<br>","");
  	
  	var lnBrks = "\n";
  	for (i=1;i<6;i++){
  		text = replace(text,lnBrks+"<li>","<li>");
  		lnBrks = lnBrks+"\n"
    }
    text = replace(text,"\n","<br>");
    text = replace(text,"\t"," ");
    text = replace(text,"·","<li>");
    text = replace(text,"<li>","\n<li>");
    text = replace(text,"<li>\n","<li>");
    
    //alert(text);
    
    //handle apostrophes
    text = replace(text,"'","&#39;");
    text = replace(text,"‘","&#39;");
    text = replace(text,"’","&#39;");
    text = replace(text,"´","&#39;");
    text = replace(text,"`","&#39;");
    text = replace(text,"’","&#39;");
    text = replace(text,"‘","&#39;");
	text = replace(text,"’","&#39;");
	
	// ampersands
	//text = replace(text,"&","&#38;");
	
	//handle dashes
    text = replace(text,"-","&#45;");
    text = replace(text,"­","&#45;");
    text = replace(text,"–","&#45;");
    text = replace(text,"–","&#45;");
    text = replace(text,"—","&#45;");
    text = replace(text,"–","&#45;");

    //handle quotes
    text = replace(text,"”","&#34;");
    text = replace(text,"“","&#34;");
    text = replace(text,"“","&#34;");
    text = replace(text,"”","&#34;");
    
    //2nd lvl bullet
    text = replace(text,"§","\n<br>- ");
    
    return text;
}

