var defaultEmptyOK = false;

// Check that a string contains only numbers
function allDigits(string, ignoreWhiteSpace) {
if (ignoreWhiteSpace == "undefined")
ignoreWhiteSpace = true;
if (string.search) {
if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
}
return true;
} 

//function to check the validity of date
function validDate(formField,fieldLabel,required)
{
var result = true; 
if (required && !validRequired(formField,fieldLabel))
return false;
else if(!required && isEmpty(formField.value))
return true;
if (result)
{
var elems = formField.value.split("/");

result = (elems.length == 3); // should be three components

if (result)
{
var month = parseInt(elems[0],10);
var day = parseInt(elems[1],10);
var year = parseInt(elems[2],10);
result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
allDigits(elems[1]) && (day > 0) && (day < 32) &&
allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
}

if (!result)
{
alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
formField.focus(); 
}
} 

return result;
} 


// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false. 
function warnInvalid (theField, s)
{ 
theField.focus()
theField.select()
alert(s)
return false
}

//function that validates the
function isEmpty(s)
{
if ((s == "") || (s == null) || (s.length == 0))
return true;
return false;
}

function validRequired(formField,fieldLabel)
{
var result = true;
if ((formField.value == "") || (formField.value == null) || (formField.value.length == 0))
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
return result;
} 
// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
if (string.search) {
if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
}
return true;
} 
// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
if (string.search) {
if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
}
return true;
} 
 
function inValidCharSet(str,charset)
{
var result = true; 
// Note: doesn't use regular expressions to avoid early Mac browser bugs 
for (var i=0;i<str.length;i++)
if (charset.indexOf(str.substr(i,1))<0)
{
result = false;
break;
}

return result;
} 
function validEmail(formField,fieldLabel,required)
{
var result = true;

if (required && !validRequired(formField,fieldLabel))
result = false; 
if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
{
alert("Please enter a complete email address in the form: yourname@yourdomain.com <mailto:yourname@yourdomain.com>");
formField.focus();
result = false;
}

return result; 
} 
function validLength(formField,fieldLabel,maxLength,required)
{
var result = true;

if (required && !validRequired(formField,fieldLabel))
result = false; 
if (result && ((formField.value.length > maxLength)))
{
alert('Please use less than "' + maxLength + '" for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}

return result; 
} 


function GetObj(n)
 { 
var p,i,x; 
d=document;
 if((p=n.indexOf("?"))>0&&parent.frames.length) 
{
d=parent.frames[n.substring(p+1)].document; 
n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n];
 for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=GetObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); 
return x;
} 
 
// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
var newString = '';
var substring = '';
beginningFound = false;

// copy characters over to a new string
// retain whitespace characters if they are between other characters
for (var i = 0; i < string.length; i++) {

// copy non-whitespace characters
if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {

// if the temporary string contains some whitespace characters, copy them first
if (substring != '') {
newString += substring;
substring = '';
}
newString += string.charAt(i);
if (beginningFound == false) beginningFound = true;
}

// hold whitespace characters in a temporary string if they follow a non-whitespace character
else if (beginningFound == true) substring += string.charAt(i);
}
return newString;
} 
// Remove all spaces from a string
function removeSpaces(string) {
var newString = '';
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) != ' ') newString += string.charAt(i);
}
return newString;
} 
function charCount(tArea,maxNo,ctrField,maxField,ctrBlock,allowExtra){
textArea = document.getElementById(tArea);
ctrF = document.getElementById(ctrField);
maxF = document.getElementById(maxField);
ctrBlk = document.getElementById(ctrBlock);
pctFull = textArea.value.length / maxNo * 100;

if (allowExtra != 'Y')
{if (textArea.value.length >= maxNo)
{textArea.value = textArea.value.substring(0, maxNo);
textArea.style.color = 'red';
}
else
{msg = null;
textArea.style.color = 'black';}
}
ctrF.innerHTML = textArea.value.length;
maxF.innerHTML = maxNo;
if (textArea.value.length > 0)
{ctrBlk.style.visibility = 'visible';}
else
{ctrBlk.style.visibility = 'hidden';}

if (pctFull >= 90)
{ctrBlk.style.color='red';}
else if (pctFull >= "80")
{ctrBlk.style.color='#EAA914';}
else
{ctrBlk.style.color='black';}
} // End charCount() 

