//////////// date validation /////////////////////
highLightColor = "#FAFFBD";
highLightBorder = "#806128";

function createErrorMsg(obj, errMsg)
{
	if(document.getElementById(obj.name+'_error'))
	{
		span = document.getElementById(obj.name+'_error');
		if(errMsg != 1)
		span.innerHTML = errMsg;
		else
		span.innerHTML = "Invalid entry! Please try again";
	}
	else
	{
		var span = document.createElement('p');	
		span.className ='errorMsg';
		span.setAttribute('id', obj.name+'_error');
		
		if(errMsg != 1)
		span.innerHTML = errMsg;
		else
		span.innerHTML = "Invalid entry. Please try again.";
		obj.parentNode.appendChild(span);	
	}
	
}

function removeErrorMsg(obj)
{
	if(document.getElementById(obj.name+'_error'))
	{
		span = document.getElementById(obj.name+'_error');
		obj.parentNode.removeChild(span);
		obj.style.backgroundColor = '';
		obj.style.borderColor = '';
	}	
}
function formvalid(frmObj) 
{ 
	var i,p,q,nm,test,num,min,errors='',errorR='',max,args=formvalid.arguments;
	j=0;
	var regBlank = /[^\s]/;
	var regAlphaNum = /^([a-zA-Z0-9_]+)$/;
	var regAlpha = /^([a-zA-Z]+)$/;
	var regDate = /^([0-9_]+-[0-9][0-9]+-[0-9][0-9]+)$/;
	
	var formtype = frmObj.elements;
	
	var checked = '';
	for (i=0; i<(formtype.length); i++) 
	{	
		mesg = formtype[i].title;
		test = formtype[i].className;
		
		val = formtype[i];
		val_new = formtype[i];
		val_new.style.backgroundColor = '';
		val_new.style.borderColor = '';
		if (val) 
		{	nm=mesg; 
			
			val = trim(val.value);
			if(regBlank.test(val))
			{
				if(test.indexOf('isAlphaNum')!=-1)
				{
					if(!regAlphaNum.test(val))
					{
						errors = nm+': Only Alpha Numeric and "_" characters Allowed.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
				}
				if(test.indexOf('isAlpha')!=-1)
				{
					if(!regAlpha.test(val))
					{
						errors = nm+': Only alphabaetic characters Allowed.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
				}
				else if(test.indexOf('isZip')!=-1)
				{
					reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
					if (!reZip.test(val)) {
						errors = nm+': is not a valid zip.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
					
				}
				else if(test.indexOf('isPhone')!=-1)
				{
					var rePhone = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
					if (!rePhone.test(val)) {
						errors = nm+': is not a valid phone number.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
					
				}
				else if(test.indexOf('isSpeciality')!=-1)
				{
					if(val == 'Other')
					{
						val = formtype['speciality1'].value;
						if(val == '')
						{
							errors = nm+' is required.'; 
							createErrorMsg(formtype['speciality1'],1);
							formtype['speciality1'].style.backgroundColor = highLightColor;
							formtype['speciality1'].style.borderColor = highLightBorder;
							
						}
						else
						{
							removeErrorMsg(formtype['speciality1']);
						}
					}
					else
					{
						removeErrorMsg(formtype['speciality1']);
					}
				}
				else if (test.indexOf('isEmail')!=-1) 
				{ 
					var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
					if(!regEmail.test(val)){
						errors = nm+' must be a valid email address.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
			    }
				else if (test.indexOf('isUserID')!=-1) 
				{ 
					var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
					if(!regEmail.test(val)){
						errors = nm+' Needs to be a valid email address.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else if(val.length < 6)
					{
						errors = nm+' is below the 6 character minimum limit.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else if(val.length > 50)
					{
						errors = nm+' is above the 50 character maximum limit.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);	
					}
					
			    }
				else if (test.indexOf('isPassword')!=-1) 
				{ 
					var regAlphaNum = /^([a-zA-Z0-9_]+)$/;
					if(!regAlphaNum.test(val)){
						errors = 'Invalid '+nm+' format.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else if(val.length < 8)
					{
						errors = nm+' Must be at least eight characters in length.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else if(val.length > 50)
					{
						errors = nm+' is above the 50 character maximum limit.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
			    }
				else if (test.indexOf('isConfPassword')!=-1) 
				{ 
					pass1 = document.getElementById(val_new.getAttribute('match')).value;
					pass2 = val;
					tit1 = document.getElementById(val_new.getAttribute('match')).title;
					if(pass1 != pass2)
					{
						errors = nm+' must be same as the '+tit1+'.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
			    }
				else if (test.indexOf('isSecurityAnswer')!=-1) 
				{ 
					if(val.length > 50)
					{
						errors = nm+' is above the 50 character maximum limit.';
						createErrorMsg(val_new,errors);
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
					else
					{
						removeErrorMsg(val_new);
					}
			    }
				else if (test.charAt(0)=='R')
				{
					result = trim(val);
					if(result.length==0)
					{
						errorR = 'Required'; 
						val_new.style.backgroundColor = highLightColor;
						val_new.style.borderColor = highLightBorder;
					}
				}
			}
			else if (test.charAt(0) == 'R'){
				errorR = 'Required'; 
				val_new.style.backgroundColor = highLightColor;
				val_new.style.borderColor = highLightBorder;
			}
			
			
		}
		if(errors !="" || errorR != "")
		{	if(j<=0)
			{
				focusitem = formtype[i];
				j++;
			}
		}
	} 
	
//return errors;
  if (errors || errorR)
  {
	//alert_box('The following error(s) occurred:\n\n'+errors);
	if(errorR != '')
		document.getElementById("err_msg").innerHTML = "Please fill in all required information.";
	else
		document.getElementById("err_msg").innerHTML = "";
		
	focusitem.focus();
	return false;
  }
  else
	return true;

//  document.MM_returnValue = (errors == '');
	
}



// End form validation New ///



function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the doc
} // Ends the "trim" function


function getWordCount(val) {
	val = val.toString()
	if(val.length == 0) return 0
	var wordslen = val.split(' ')
	return wordslen.length
}


