String.prototype.trim = function()
{ //Trim, as in VB
  return this.replace(/(^\s+)|(\s+$)/g, '');
}

function validRadio(formName,elementName,errorMessage) {
	var endIndex=eval("document."+formName+"."+elementName+".length");
	for (i=0; i<endIndex; i++) {
		if (eval("document."+formName+"."+elementName+"["+i+"].checked==true")) {
			return false;
		}
		else if (i==(endIndex-1)) {
			alert(errorMessage); eval("document."+formName+"."+elementName+"[0].focus()"); return true;
		}
	}
}

function validAnswer(formName,elementname,errorMessage) {
//	Checks that the value is not empty.
	var passedValue = eval("document."+formName+"."+elementname+".value");
	var trimmedValue = passedValue.trim();
	while (trimmedValue=='') {
		alert(errorMessage); eval("document."+formName+"."+elementname+".focus()"); return true;
	}
	return false;
}

function emailCheck (emailStr) {

var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("Email address seems incorrect (check @ and .'s)")
	return true
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The email address doesn't seem to be valid.")
    return true
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Email address is invalid!")
		return true
	    }
    }
    return false
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The email address doesn't seem to be valid.")
    return true
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The email address must end in a three-letter domain, or two letter country.")
   return true
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This eamil address is missing a hostname!"
   alert(errStr)
   return true
}

// If we've gotten this far, everything's valid!
return false;
}
//  End -->

function validatePwd(pswd1,pswd2) {
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
	var ok = "yes";	
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	var pw1 = pswd1;
	var pw2 = pswd2;
	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
	alert('Please enter your password twice.');
	return true;
	}
	// check for minimum length
	if (pw1.length < minLength) {
	alert('Your password must be at least ' + minLength + ' characters long. Try again.');
	return true;
	}
	
	// check for invalid characters
	for (var i=0; i<pw1.length; i++) {
		temp = "" + pw1.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Sorry, only letters and numbers are allowed in the password."); return true;
    }
	
	// check for spaces
	if (pw1.indexOf(invalid) > -1) {
	alert("Sorry, spaces are not allowed in the password.");
	return true;
	}
	else {
	if (pw1 != pw2) {
	alert ("You did not enter the same password twice. Please re-enter your password.");
	return true;
	}
	else {
	return false;
	      }
	   }
}
function validateUsername(uname) {
	var invalid = " "; // Invalid character is a space
	var minLength = 3; // Minimum length
	
	// check for spaces
	if (uname.indexOf(invalid) > -1) {
	alert("Sorry, spaces are not allowed in the User Name.");
	return true;
	}
	// check for minimum length
	if (uname.length < minLength) {
	alert('The User Name must be at least ' + minLength + ' characters long. Try again.');
	return true;
	}
	return false;
}
function validateNumber(passedNum,errorMessage) {
	var invalid = " "; // Invalid character is a space
	// check for spaces
	if (passedNum.indexOf(invalid) > -1) {
	alert(errorMessage);
	return true;
	}
	if (isNaN(passedNum) && passedNum.length > 0){
		alert(errorMessage);
		return true;
	}
	else
		return false;

}

function validateURL(passedURL,errorMessage) {
  var url = /\w+:\/\/\w+/;    
  if (!passedURL.match(url)) {                           
    alert(errorMessage);
    return true;
  }
  return false;
}
// VALIDATE MEDICAL RECORD NUMBER
function validateMRN(mrn1,mrn2) {
	var invalid = " "; // Invalid character is a space
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	var minLength = 6; // Minimum length
	var mn1 = mrn1;
	var mn2 = mrn2;
	var temp;
	var ok = "yes";
	// check for a value in both fields.
	if (mn1 == '' || mn2 == '') {
		alert('Please enter the Medical Record Number twice.');
		return true;
	}
	// check for illegal characters
	for (var i=0; i<mn1.length; i++) {
		temp = "" + mn1.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Sorry, the Medical Record Number can only contain letters and numbers");
		return true;
	}
	// check for minimum length
//	if (mn1.length < minLength) {
//		alert('The Medical Record Number must be at least ' + minLength + ' characters long. Try again.');
//		return true;
//	}
	if (mn1 != mn2) {
		alert ("You did not enter the same Medical Record Number twice. Please re-enter the Medical Record Number.");
		return true;
	}
	
return false;
}

// VALIDATE ZIP CODE
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;


if (field.length!=0 && field.length!=5 && field.length!=10) {
alert("Please enter only a valid 5 digit or 5 digit+4 zip code.");
return true;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code. Please try again.");
return true;
}
if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
return true;
   }
}
return false;
}

// VALIDATE Name Field
function validateName(formName,elementname,errormessage) {
var field = eval("document."+formName+"."+elementname+".value");
var trimmedValue = field.trim();
var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.' "
var ok = "yes";
var temp;
for (var i=0; i<field.length; i++) {
temp = "" + field.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert(errormessage); eval("document."+formName+"."+elementname+".focus()"); return true;
   }
if (trimmedValue.length == 0) {
alert(errormessage); eval("document."+formName+"."+elementname+".focus()"); return true;
   }   
return false;   
}

// CHECK LENGTH
function checkLength(formName,elementname,maxlength,displayname) {
var field = eval("document."+formName+"."+elementname+".value");
if (field.length > Number(maxlength)){
	alert("Please limit your "+displayname+" to "+ maxlength + " characters"); eval("document."+formName+"."+elementname+".focus()"); return true;
}
return false;   
}

// COMPARE TIMES
function compareTimes(formName,elementname1,elementname2,displayname1,displayname2) {
var time1 = eval("document."+formName+"."+elementname1+".value");
var time2 = eval("document."+formName+"."+elementname2+".value");
var hour1 = (time1.substring(0,time1.indexOf(':'))-0);
var minute1 = (time1.substring(time1.indexOf(':')+1,time1.length)-0)
var hour2 = (time2.substring(0,time2.indexOf(':'))-0);
var minute2 = (time2.substring(time2.indexOf(':')+1,time2.length)-0)

if (time1.length == 0 || time2.length == 0)
	return false;
	else{
		if (time1 > time2){
			alert("The time you "+displayname1+" should be before the time you "+ displayname2 + "."); eval("document."+formName+"."+elementname2+".focus()"); return true;
		}
	}
return false;   
}
