<!--//
//----this function checks to see if an item has been selected from the drop-down box
function validateSelectBoxRequired(selected,e,msg)
{
	if(selected.value=='')
	{
		alert('This is a required field.' + msg);
		e.style.background = 'gold';
		return;
	}
	else
	{
	e.style.background = 'white';
	}
}
///end of function that checks that item has been selected in drop-down box

//this function will deEncode urlString parameters (others if needed)
//fixes problem where users use & (ampersands in titles of their custom fields)
function deEncode(code)
	{
		   //convert char to ampersand
		   code = code.replace('%26','&');
		   return code.toString();
		  
	}
//-----This Function will check field for valid date and alert user if data is invalid
//------------------------------------------------------------------------------
function checkDate(dateToBeChecked,bRequired,showAlert)
{
	if(showAlert == '' || showAlert == null)
	{
		showAlert = 'yes';
	}
  //---this sets bRequired to false if the field is not required on form and not specified as being required
	if(bRequired==null || bRequired == '')
	{
		bRequired = 'false';
	}
  	
  	var iDate
    var sDate
    var oDate

    // Test if field is not blank.
    if (dateToBeChecked.value.length > 0)
    {
      iDate = Date.parse(dateToBeChecked.value);
      if (isNaN(iDate))
      {
		if(showAlert == 'yes')
			{
			alert('The date you have entered is not valid. Please re-enter your date');
			
			dateToBeChecked.value='';
			dateToBeChecked.style.background='gold';
			}
		return false;
      }
      else
      {
       dateToBeChecked.style.background='white';
        
      }
    }
    else
    {
		if(bRequired == 'true')
		{
			if(showAlert =='yes')
			{
			alert('This is a required field.');
			
			dateToBeChecked.value='';
			dateToBeChecked.style.background='gold';
			}
			
			return false;
		}
		else
		{
		dateToBeChecked.style.background='white';
		}
	}
	
  }
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
 
  
//-----This Function validates and formats in HH:MM:SS AM/PM format.(seconds are optional)

function isValidTime(timeStr,bRequired,showAlert) {
if(showAlert == '' || showAlert == null)
	{
		showAlert = 'true';
	}
	
	if(bRequired == '' || bRequired == null || bRequired == 'false')
	{
		bRequired = 'false';
	}
	else{
		bRequired = 'true';
	}
		
		
	if(!(timeStr.value == ''))
	{
	  //var timePattern = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	  var timePattern = /^(\d{1,2})(:|\s?)(\d{2})?(\s?(AM|am|PM|pm))?$/;

		var matchArray = timeStr.value.match(timePattern);
		
		if (matchArray == null) 
		{
		timeStr.style.background='gold';
			timeStr.value='';
			if(showAlert == 'true'){
				alert('You have entered an invalid time.');
			}
			else{
				return false;
			}
				

		//return false;
		}
		hr = matchArray[1];
		min = matchArray[3];
		ampm = matchArray[5];


		//if (sec=='') { sec = null; }
		if(min=='') {min='00';}
		if (ampm=='') 
		{ 
		ampm = null 
		}
		
		else
		{
		ampm = ampm.toUpperCase();
		}
		
		if (hr < 0  || hr > 23) {
			timeStr.value= 12 + ':' + min + ' ' + ampm;
			timeStr.style.background='gold';
			if(showAlert == 'true'){
				alert('hr must be between 1 and 12 for standard time or from  0 to 23 for military time.)');
				if(ampm==null){
					ampm='AM'
				}
			}
			else{
				return false;
			}
			
			
		}
		if (hr < 12 && ampm == null) {

		timeStr.value= hr + ':' + min + ' AM';
		timeStr.style.background='white';

		return;

		}

		if  (hr > 12) {

		timeStr.value= parseInt(hr)-12 + ':' + min + ' PM';

		timeStr.style.background='white';

		return;
		}
		if (min<0 || min > 59 || min == null) {
			timeStr.style.background='gold';
			if(showAlert == 'true'){
				alert ("Minutes must be between 0 and 59.");
				timeStr.value=hr + ':' + '00' + ' ' + ampm.toUpperCase();
				
			}
			else{
				return false;
			}
			
		}
		timeStr.style.background='white';
		return;
		}
	else
	{
		if(bRequired == 'true'){
			return false;
			timeStr.style.background='gold';
		}
		else{
			timeStr.style.background='white';
		}
	
	}
}


//----This function checks that field is numeric only
function isValidNumber(e,showAlert)
{
//showAlert would be no if you were doing validation at the form level
//and didn't want to show the alert(), but were creating an error message instead
if(showAlert == '' || showAlert == null)
{
	showAlert = 'yes';
}
	if(!(e.value == ''))
	{
		if(isNaN(e.value))
		{
			if(showAlert == 'yes')
			{
				alert('Only numbers are allowed');
			}
			e.value ='';
			e.style.background='gold';
			//e.focus();
		}
		else
		{
		e.style.background='white';
		}
		return;
	
	}
else
	{
		e.style.background='white';
	}
}
//----This function checks that field is numeric 
//field can contain white space and numbers only
function isValidCCNumber(e,showAlert)
{
//showAlert would be no if you were doing validation at the form level
//and didn't want to show the alert(), but were creating an error message instead
if(showAlert == '' || showAlert == null)
{
	showAlert = 'yes';
}
	var strCC = e.value.replace(/\s/g,'');
	
	if(!(strCC == ''))
	{
		if(isNaN(strCC))
		{
			if(showAlert =='yes')
			{
			alert('Only numbers are allowed');
			}
			e.value ='';
			e.style.background='gold';
			
			//e.focus();
		}
		else
		{
		e.style.background='white';
		}
		return;
	
	}
else
	{
		e.style.background='white';
	}
}
//----This function checks that field is numeric/decimal only
//e = element (this)
//minQty = default value to set e.value to if invalid characters is entered
function isValidQty(e,minQty,showAlert)
{
//showAlert would be no if you were doing validation at the form level
//and didn't want to show the alert(), but were creating an error message instead
if(showAlert == '' || showAlert == null)
{
	showAlert = 'yes';
}

	if(!(minQty))
	{
		minQty = 1;
	}
	if(!(e.value == ''))
	{
		if(isNaN(e.value))
		{
			if(showAlert == 'yes')
			{
			alert('Invalid Data');
			}
			e.value = minQty;
			e.style.background='white';
			e.focus();
		}
		else
		{
		e.style.background='white';
		}
		return;
	
	}
else
	{
		e.style.background='white';
	}
}
//------------------------------------------------
//---This function checks that field is not empty------
//--usage:
//--e = element to test
//--strErrText = error message to return
function isValidNotBlank(e,strErrText,showAlert)
{
//showAlert would be no if you were doing validation at the form level
//and didn't want to show the alert(), but were creating an error message instead
if(showAlert == '' || showAlert == null)
{
	showAlert = 'yes';
}

var trimmed = trim(e.value);
if(trimmed.length ==0 || trimmed==null || trimmed.charAt(0) ==' ')
{
	if(showAlert == 'yes')
	{
	alert('This is a required field.\n' + strErrText);
   }
   e.style.background='gold';
   //str.focus();
   return;
}
else
{
 e.style.background='white';

}
}
//----This function will format currency fields automatically
function formatCurrency(fld, milSep, decSep, e) 
{

var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
fld.value = '$' + fld.value;

}
return false;
}
//--end currency function-------------------------------------------------



//----validate phone number(us and international)
//---------------------------------------------------------
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()-. ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validIntlChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 6;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var cur = s.charAt(i);
        if (((cur < "0") || (cur > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


function stripChars(s, vIntlChars)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in vIntlChars, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var cur = s.charAt(i);
        if (vIntlChars.indexOf(cur) == -1) returnString += cur;
    }
    return returnString;
}

function checkIntlTel(strPhone)
{
  if(!(strPhone==""))
  {
	s=stripChars(strPhone,validIntlChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}
}


function validatePhone(formName,e,showAlert){
	var tel=document.forms[formName].elements[e.name];
	//showAlert would be no if you were doing validation at the form level
//and didn't want to show the alert(), but were creating an error message instead
if(showAlert == '' || showAlert == null)
{
	showAlert = 'yes';
}
	/*if ((tel.value==null)||(tel.value=="")){
		alert("Please Enter your Phone Number");
		tel.style.background='gold';
		//tel.focus();
		return false;
	}*/
	if (checkIntlTel(tel.value)==false){
		if(showAlert =='yes')
		{
			alert("The phone number you entered is invalid.");
		}
		tel.value="";
		tel.style.background='gold';
		//tel.focus();
		return false;
		
	}
	else
	{
	tel.style.background='white';
	}
	return true
 }

//----END OF PHONE VALIDATION

//----Validate Email functions
function validateEmail(e,bRequired,showAlert)
{
//showAlert would be no if you were doing validation at the form level
//and didn't want to show the alert(), but were creating an error message instead
if(showAlert == '' || showAlert == null)
{
	showAlert = 'yes';
}
if((e.value=='' || e.value==null) && bRequired=='true')
{
	if(showAlert =='yes')
		{
		alert('Email is a required field');
		}
	e.background='gold';
	return;
}
if (!((e.value == "") || (e.value ==null)))
{
var emailExp = 
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (! e.value.match(emailExp)) {
if(showAlert =='yes')
		{
alert("Invalid email address");
}
//e.style.background='gold';
//e.value='';
//e.select();
return false;
}
else
{
e.style.background='white';
}
}
}
//----end of number one

//---END OF EMAIL VALIDATION
//BEGIN SIMPLE VALIDATE OFEMAIL
function validateEmail2(e)
{

	//if (!((e == "") || (e ==null)))
	//{
	    var emailExp = 
	    /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	    if (! e.match(emailExp)) 
	    {
			  
			return false;
		}
		else
		{
			return true;
		}
	//}

}
//END SIMPLE VALIDATE OF EMAIL
//--------------------------------------------------
//-This FUnction trims beginning and trailing spaces
function trim(e) {
  // Remove leading spaces
  e = e.replace(/^\s+/g, "")
  // Remove trailing spaces
  e = e.replace(/\s+$/g, "")
  return e;
}
//----this function counts words onclick
//this function counts words
function countWords_ORIG(form,txtToCount,counter)
{

var frm = document.forms[form];
var charToReplace = /(\s+)/g;//white space characters
txtToCount = trim(frm.elements(txtToCount).value);//remove leading and trailing white spaces

txtToCount = txtToCount.replace(charToReplace," "); //replaces tab and line breaks

if(txtToCount == '')
	frm.elements(counter).value = 0;
else
{
	var arrText = txtToCount.split(" ");
	
	frm.elements(counter).value = arrText.length;
}
}
function countWords(form,txtToCount,counter,limit)
{
	var browser = getBrowserVersion();
	
	if(browser == 'IE')
	{
		countWordsOnKeyPress_IE(form,txtToCount,counter,limit)
	}
	else
	{
		countWordsOnKeyPress_NS(form,txtToCount,counter,limit)
	}
	
}
//-------------------------------------------------
function getBrowserVersion()
{
var browser     = '';
var version     = '';
var entrance    = '';
var cond        = '';
// BROWSER?
if (browser == ''){
if (navigator.appName.indexOf('Microsoft') != -1)
browser = 'IE'
else if (navigator.appName.indexOf('Netscape') != -1)
browser = 'Netscape'
else browser = 'Mozilla';
return browser;
}
}

//--this function counts words onKeyPress and updates counter text box in real time
//it also uses a limit for one its parameters to specify a maximum amount of words that be entered
//into a text box
function countWordsOnKeyPress(form,txtToCount,counter,limit)
{
	var browser = getBrowserVersion();
	
	if(browser == 'IE')
	{
		countWordsOnKeyPress_IE(form,txtToCount,counter,limit)
	}
	else
	{
		countWordsOnKeyPress_NS(form,txtToCount,counter,limit)
		
	}
	
}



function countWordsOnKeyPress_IE(form,txtToCount,counter,limit)
{
	var frm = document.forms[form];
	
	//var charToReplace = /(\s+)/g;//white space characters
	var charToReplace = /(\s+)|(,)|(;)/g;//white space characters
	var	spaceCounter = 0;
	var strTrimmedText = '';	
	
	textArea = trim(frm.elements[txtToCount].value);//remove leading and trailing white spaces
	textArea = compressWhiteSpace(textArea);
	//textArea = textArea.replace(',',' '); //replaces tab and line breaks
	//textArea = textArea.replace(';',' '); //replaces tab and line breaks

	if(textArea=='')
	{
		frm.elements[counter].value = 0;
	}
	else
	{
		//newText = textArea.replace(charToReplace," "); //replaces tab and line breaks
		var arrText = textArea.split(charToReplace);
		
		var trimmed = textArea.slice(0, -2);
		

		if(arrText.length > parseInt(limit))
		{
			
			alert('You have reached the maximum amount of words allowed\n The first ' + limit + ' words will be kept.');
			//alert(textArea.length);
			//loop through textfield until you reach the word which is at the limit
			for(i=0;i < textArea.length ;i++)
			{
				if(textArea.charAt(i).match(charToReplace))
				{
				spaceCounter += 1;//  parseInt(spaceCounter) + 1;
				
				}
				if(spaceCounter < limit)
				{
					strTrimmedText = strTrimmedText + textArea.charAt(i).toString() ;
				
				}
				
			}
			//frm.elements[txtToCount].value = trimmed;
			//alert(strTrimmedText);
			frm.elements[txtToCount].value = strTrimmedText;
			return;
		}
		else
		{
			
			frm.elements[counter].value = parseInt(arrText.length);
			return;
		}
	}
	
}
function compressWhiteSpace(s) {

  // Condense white space.

  s = s.replace(/\s+/g, " ");
  s = s.replace(/^\s(.*)/, "$1");
  s = s.replace(/(.*)\s$/, "$1");

  // Remove uneccessary white space around operators, braces and parentices.
	s = s.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d])/g, "$1");
	s = s.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d])\s/g, "$1");
  
  //s = s.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/g, "$1");
  //s = s.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/g, "$1");
  return s;
}
function countWordsOnKeyPress_NS(form,txtToCount,counter,limit)
{
	
	var frm = document.forms[form];
	var charToReplace = /(\s+)/g;//white space characters
	
	var	spaceCounter = 0;
	var strTrimmedText = '';	
	
	textArea = trim(frm.elements[txtToCount].value);//remove leading and trailing white spaces
	textArea = compressWhiteSpace(textArea);//replace duplicate/triplicate spaces with single space
	
	if(textArea=='')
	{
		frm.elements[counter].value = 0;
	}
	else
	{
		//new code 12-12-05
		//var words  = textArea.match(/\b\w+\b/g);
		var words  = textArea.split(' ');
		var iCount =0;
	//alert(words);
		while(iCount < words.length)
		{
			iCount = iCount +1;
			spaceCounter +=1;
		}
		
		//end of new code
		
		
		///end new code
		//alert(spaceCounter);
		if(spaceCounter > parseInt(limit))
		{
			iCount=0;
			alert('You have reached the maximum amount of words allowed\n The first ' + limit + ' words will be kept.');
			
			//alert(textArea.length);
			//loop through textfield until you reach the word which is at the limit
			for(i=0;i < textArea.length ;i++)
			{
				if(textArea.charAt(i).match(charToReplace))
				{
				iCount =  parseInt(iCount) + 1;
				
				}
				if(iCount < limit)
				{
					strTrimmedText = strTrimmedText + textArea.charAt(i).toString() ;
				
				}
				
			}
			
			frm.elements[txtToCount].value = strTrimmedText;
			return;
		}
		else
		{
			
			frm.elements[counter].value = parseInt(spaceCounter);
			return;
		}
		
	}
	
}

//--This function counts characters in textArea
//field = element being checked
//counter = text box or value to track how many characters can be added
//limit = max number of characters allowed
function textCounter(field,counter,limit) {
if (field.value.length > limit) 
{
	field.value = field.value.substring(0, limit);
	alert('Maximum characters allowed!');
}
else //update 'characters left' counter
{	
	//counter.value = limit - field.value.length;
	}
}
//this function countsCharacters in text boxes
//and displays remaining number as counter
//note same as textCounter function except that it excepts additional parameter
function countCharacters(form,field,counter,limit) 
{

	if (document.forms[form].elements[field].value.length > limit) 
	{
		document.forms[form].elements[field].value = document.forms[form].elements[field].value.substring(0, limit);
		alert('Maximum characters allowed!');
	}
	else //update 'characters left' counter
	{	
		document.forms[form].elements[counter].value = parseInt(limit - document.forms[form].elements[field].value.length);
	}
}

//this function checks for valid currency format!!!
function isValidCurrency(e) 
{
str=e.value; 
if(str=='' || str == null)
{
  e.value = 0;
  return;
}
regExpCur = /(^-?[\£\$\€][0-9\.\,]+$)|(\d+)/;

	if(regExpCur.test( str ) == false)
	{
		alert('Invalid Format');
		e.style.background = 'gold';
		e.value='';
		e.focus();
		}
	else
	{
		e.style.background = 'white';
	}
}  
 

//begin validate us/ca postal code
function validUSCAPostal(e,bRequired) 
{ 

	var okFormats = 'US = 12345, 12345-678 or 12345-6789\nCA = A1B 2C3'
	zip = trim(e.value);
	if(bRequired == null || bRequired =='')
	{
		bRequired = false;
	}
	if(zip == '' && bRequired == false)
	{
		e.style.background = 'white';
		return;
	}

//var exp = /^(\d{5}|\d{9}|(\d{5}-)\d{4}|\d{5}-\d{3}|\d{5}\s\d{3}|\d{5}\s\d{4}|[a-z]\d[a-z]\s*\d[a-z]\d)$/;
var exp = /^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z][ ]\d[A-Z]\d$/i;


var validZip = exp.test(zip);

if(validZip == true)
{
	e.style.background = 'white';
	return;
}
	else
{
	alert('Zip code is invalid. Valid formats are:\n' + okFormats);
	e.value = '';
	e.style.background = 'gold';
}

}
//-->