/************************************************ 
Validate that email address is in correct format
*************************************************/

function validateEmailString(strEmail) {
	var strEmail = new String(strEmail);
	if (window.RegExp) {
		// Strict test to match to patters for email addresses without limiting the
		// number and placement of .'s in the field
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"; // RegExp for the @'s and .'s
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";// RegExp for the parts in between the @'s and .'s
		var reg1 = new RegExp(reg1str); // RegExp Object
		var reg2 = new RegExp(reg2str); // RegExp Object

		// Here's the test. Is the email address well formed?
		if (!reg1.test(strEmail) && reg2.test(strEmail)) {
			return true;						// Valid Email
		}
		return false;							// Not Valid Email

	} else {
		// Simple test for older browsers that do not support the Regular Expression object
		// Only need to have an @ in the field
		if(emailAddress.indexOf("@") >= 0) {
			return true;						// Valid Email
		}
		return false;							// Not Valid Email
	}
	return true; // Just say yes
}


/************************************************ 
This will be removed
*************************************************/

function validateInput(strInput) {
  if (window.RegExp) {

    var v1 = "[`|~|!|$|%|\\^|\\*|\\+|\\=|\\\\|\\[|\\]|\\{|\\}|\\||\\|<|>|\\?|\\/|\\\"|;|:]";
    var v2 = new RegExp(v1);
    
    if( v2.test(strInput) )
    {
        return false;
    }    
  }
    return true;
}


/************************************************ 
Trim spaces from the front & back of a string
*************************************************/

function trimSpaces( s )
{
  // Trim spaces off the front of the string
  while( s.substring(0,1) == ' ' ) 
  {
    s = s.substring(1,s.length);
  }

  // Trim spaces off the back of the string
  while( s.substring(s.length-1,s.length) == ' ' ) 
  {
    s = s.substring(0,s.length-1);
  }

  return s;
}

/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can only be a /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy

PARAMETERS:
   strDate - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function validateDateString(strDate) 
{
	var objRegExp = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
 
	//check to see if in correct format
	if(!objRegExp.test(strDate))
		return false; //doesn't match pattern, bad date
	else
	{
		var strSeparator = strDate.substring(2,3) //find date separator
		var arrayDate = strDate.split(strSeparator); //split date into month, day, year
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
		var intDay = parseInt(arrayDate[1],10); 

		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) 
		{
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
				return true; //found in lookup table, good date
		}
    
		var intMonth = parseInt(arrayDate[0],10);
		if(intMonth == 2) 
		{ 
            var intYear = parseInt(arrayDate[2]);
            if(intDay > 0 && intDay < 29)
				return true;
       
			else if(intDay == 29) 
			{
				if((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) 
				{
					// year div by 4 and ((not div by 100) or div by 400) ->ok
					return true;
                }   
			}
		}
	}  
	return false; //any other values, bad date
}

/************************************************
DESCRIPTION: Validates that a string represents a
positive integer

PARAMETERS:
   toTest - String to be tested for validity

RETURNS:
   True if valid, otherwise false.   
*************************************************/
function isPositiveInteger(toTest)
   {
     if( !(toTest == "") && !isNaN(toTest))
     {
       if(toTest < 0)
       {         
         return false;
       }
       if(toTest.indexOf(".") >= 0)
       {         
         return false;
       }
       return true;
     }     
     return false;
   }

/************************************************ 
Based on the form parameter clear form
*************************************************/
function clearForm(form)
{
	// Hide all error messages that may be visible
	var spanElements = document.getElementsByTagName("span");
	for(i = 0; i < spanElements.length; i++)
	{
		if(spanElements[i].className == 'error')
		{
			// Only hide the span tag if it is using the error CSS class
			spanElements[i].style.display = 'none';
		}
	}
	form.reset();
}