//Validate using a regular Expression
function validateRegEx(regEx,value) {
  var exp = new RegExp(regEx,"g");
  if (exp.test(value)) 
    return true;
  else
    return false;  
}

// Check for null and for empty
function isFilled(elm) {
    if (elm.value == "" ||
        elm.value == null) 
    return false;
    else return true;
}

//Check an email list if the values are valid
function getBadEmailInList(elm) {

	var list = elm.value;	
	var goodEmail = true;
	var emailAddress = "";

	if (list.indexOf(",") != -1) {
	  
		var item = list.split(",");
		var num = 0;			
		while (num < item.length) {	
			emailAddress = item[num];
			if (isEmailString(trimIt(emailAddress)) == false) {				
				goodEmail = false;
				break;
			}			
			num += 1;
		}		
	} else {
		emailAddress = list;
		if (isEmailString(trimIt(emailAddress)) == false) {	
			goodEmail = false;
		}
	}

	if (goodEmail == false){
		return '(' + emailAddress + ')';
	} else {
		return '';
	}
}

// Check for email address: look for [@] and [.] 
function isEmail(elm) {

	if (isEmailString(elm.value) == false){
		return false;
	} else {
		return true;
	}
}

//Check that a string is a valid email
function isEmailString(email) {	
	if (email == "") {	
		return false;
	}
	if (email.indexOf(" ") + "" != "-1"){				
		return false;
	}
	if (isComposedOfChars("0123456789abcdefghijklmnopqrstuvwxyz&@.-_",email.toLowerCase()) == false) {
		return false;
	}
	if (email.indexOf("@") + "" == "-1"){
		return false;
	}
	
	if (email.indexOf("@") !=  email.lastIndexOf("@")){
		return false;
	}

	if (email.indexOf(".") + "" == "-1"){
		return false;
	}
	
	if (email.indexOf("@") + "" == "0"){
		return false;
	}
	
	if (email.indexOf(".")  == email.length - 1){
		return false;
	}
	return true;
}

// Check if this is a po box
function isPOBox(elm) {

	var address = elm.value + "";
	address = address.toLowerCase();
	
    if (address.indexOf("po ") + "" != "-1"){
      return true;
	} else if (address.indexOf("p.o. ") + "" != "-1"){
		return true;
	} else if (address.indexOf("p.o. ") + "" != "-1"){
		return true;
	} else if (address.indexOf("po. ") + "" != "-1"){
		return true;
	} else if (address.indexOf("p. o.") + "" != "-1"){
		return true;	
	} else if (address.indexOf("p. o. ") + "" != "-1"){
		return true;	
	} else if (address.indexOf("p o ") + "" != "-1"){
		return true;	
	} else {
      return false;
	}
}

// Check if the value is alpha numeric
function isAlphaNum(elm) {
  var Chars= '1234567890abcdefghijklmnopqrstuvwxyz';
  var elmstr = elm.value + ""; 
  elmstr = elmstr.toLowerCase();
    
  for (var i = 0; i < elmstr.length; i++) {
    if (Chars.indexOf(elmstr.charAt(i)) == -1)
	  return false;
  }
  return true;
}

// Is this a telephone number?
function isTelephone(number)
{
  number = number + "";
  return ((number.length > 9) && isComposedOfChars("xX0123456789()- ", number));
}

// Is this an image file?
function isImageFile(elm) {
  
  var srcName = elm.value.toLowerCase();

  if (srcName.indexOf(".gif") + "" != "-1"){
      return true;
	} else if (srcName.indexOf(".jpg") + "" != "-1"){
		return true;
	} else if (srcName.indexOf(".png") + "" != "-1"){
		return true;
	} else if (srcName.indexOf(".bmp") + "" != "-1"){
		return true;
	} else if (srcName.indexOf(".tif") + "" != "-1"){
		return true;
	} else if (srcName.indexOf(".swf") + "" != "-1"){
		return true;
	} else {
      return false;
	}
}

// Is this a proper street address?
function containsNumber(elm) {
  var Chars= '1234567890';
  var success = false;  
  var elmstr = elm.value + "";   
    
  for (var i = 0; i < elmstr.length; i++) {
    if (Chars.indexOf(elmstr.charAt(i)) >= 0){	  
	  success = true;
	  break
	}	
  }
  if (success) {
    return true;
  }
  else{
    return false;
  }
}

// Is this a credit card?
function isCreditCard(number)
{
  number = number + "";
  return ((number.length > 0) && isComposedOfChars("0123456789", number));
}

// Is this a zip code?
function isZipCode(number)
{
  number = number + "";
  return ((number.length >= 5) && isComposedOfChars("0123456789-", number));
}

// Is this a date?
function isDate(number)
{
  number = number + "";
  return ((number.length > 0) && isComposedOfChars("0123456789-/", number));
}

// Is this a URL?
function isURL(elm) {
    var urlPrefix = "http" + "://";
    var url = elm.value + "";
    url = url.toLowerCase();

    if (url.indexOf(urlPrefix) + "" != "-1") 
      return true;
    else
      return false;
}

// Is it a positive integer?
function isInt(elm) {
    var elmstr = elm.value + ""; 
    //Allow empty elements
    if (elmstr == "") return true;
    
    for (var i = 0; i < elmstr.length; i++) {
        if (elmstr.charAt(i) != "," && 
           (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9" )){
        return false;
        }
    }
return true;
}

// Is it a positive or negative integer?
function isSignedInt(elm) {

    var elmstr = elm.value + ""; 
	var validChars = "0123456789";
	var isSignedInt=true;
	var curChar;


	for (i = 0; i < elmstr.length && isSignedInt == true; i++) {
	  curChar = elmstr.charAt(i); 
	  if (validChars.indexOf(curChar) == -1) {
		if (i==0 && curChar == '-')
		 isSignedInt = true;
	    else
		 isSignedInt = false;
	  }
	}
	return isSignedInt;
}

function check32BitIntRange(elm) {
   var elmnum = parseInt(elm.value);

	if (elmnum < -2147483648)
     return false;
   else if (elmnum > 2147483647)
     return false;
   return true;
}

function isPosFloat(elm) {
  var Chars= '1234567890,.';
  var elmstr = elm.value + ""; 
  var commaCount =0;
  var periodCount =0;
    
  for (var i = 0; i < elmstr.length; i++) {
    if (Chars.indexOf(elmstr.charAt(i)) == -1)
	  return false;
    else if (elmstr.charAt(i) == '.')
	  periodCount++;
    else if (elmstr.charAt(i) == ',')
	  commaCount++;	  
  }
  if (periodCount > 1 || commaCount > 1)
    return false;
  
  return true;
}

function isPosCurrency(elm) {
  var Chars= '1234567890,.$';
  var elmstr = elm.value + ""; 
  var commaCount =0;
  var periodCount =0;
  var dollarCount =0;
    
  for (var i = 0; i < elmstr.length; i++) {
    if (Chars.indexOf(elmstr.charAt(i)) == -1)
	  return false;
    else if (elmstr.charAt(i) == '.')
	  periodCount++;
    else if (elmstr.charAt(i) == ',')
	  commaCount++;	  
    else if (elmstr.charAt(i) == '$')
	  dollarCount++;	  
  }
  if (periodCount > 1 || dollarCount > 1 )
    return false;
  
  return true;
}

function checkPosCurrencyRange(elm) {
  var Chars= '1234567890.'; 
  var elmstr = elm.value + "";
  var elmstr2 = "";

  //Remove commas and "$"
  if (elmstr != ""){    
    for (var i = 0; i < elmstr.length; i++) {
      if (Chars.indexOf(elmstr.charAt(i)) != -1)
        elmstr2 += elmstr.charAt(i);
    }
  }
  //Convert to float
  var elmfloat = parseFloat(elmstr2);
  if (elmfloat < 0)
    return false;
  else if (elmfloat > 99999999999)
    return false;
  return true;
}

function checkPosCurrencyNonZero(elm) {
  var Chars= '1234567890.'; 
  var elmstr = elm.value + "";
  var elmstr2 = "";

  //Remove commas and "$"
  if (elmstr != ""){    
    for (var i = 0; i < elmstr.length; i++) {
      if (Chars.indexOf(elmstr.charAt(i)) != -1)
        elmstr2 += elmstr.charAt(i);
    }
  }
  //Convert to float
  var elmfloat = parseFloat(elmstr2);
  if (elmfloat <= 0)
    return false;
  else if (elmfloat > 99999999999)
    return false;
  return true;
}

function isPosNonZero(elm) {  
  var number = elm.value + "";  

  if (parseInt(number) > 0) {
		  return true;
	  } else {
		  return false;
	  }

  //if (isComposedOfChars("0123456789", number)) {
//	  if (parseInt(number) > 0) {
//		  return true;
//	  } else {
//		  return false;
//	  }
 // } else {
//	return false;
 // }
}


// Get checked value from radio button.
function getRadioButtonValue (radio)
{   var radioValue =null;

    for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) { 
          radioValue = radio[i].value;    
          break; 
        }
    }
    return radioValue;
}

function isComposedOfChars(validChars, inString)
{
  return (indexOfFirstNotIn(validChars, inString) == -1);
}

function isLetter(aChar)
{
  return ((aChar >= 'a') && (aChar <= 'z')) ||
  ((aChar >= 'A') && (aChar <= 'Z'));
}

function isNumeric(number)
{
  number = number + "";
  return ((number.length > 0) && isComposedOfChars("0123456789", number));
}


function indexOfFirstNotIn(okayChars, inString) 
{
  var i;
  for (i=0; i < inString.length; i++) 
  {
    var charm = inString.charAt(i);
    if (okayChars.indexOf(charm) == -1)
    {
      return i;
    }
  }
    return -1;
}

//Returns a properly formated date string or
//"" if it's invalid.
function isValidDateFormatString(dateStr)
{
var dateLen=dateStr.length;
if ((isNumeric(dateStr)) && (dateLen == 8))
{
dateStr = dateStr.substring(0,2) + "/" +
dateStr.substring(2,4) + "/" +
dateStr.substring(4,8);
dateLen=dateStr.length;
}
if ((isNumeric(dateStr)) && (dateLen == 6))
{
dateStr = dateStr.substring(0,2) + "/" +
dateStr.substring(2,4) + "/" +
dateStr.substring(4,6);
dateLen=dateStr.length;
}
if (isNumeric(dateStr))
{
return "";
}
var spacerIndex = indexOfFirstNotIn("0123456789", dateStr);
var spacerChar = dateStr.charAt(spacerIndex);
if (isLetter(spacerChar))
{
return "";
}
var firstSpacer=dateStr.indexOf(spacerChar);
var secondSpacer=dateStr.lastIndexOf(spacerChar);
var monthStr=dateStr.substring(0, firstSpacer);
var dayStr  =dateStr.substring(firstSpacer + 1, secondSpacer);
var yearStr =dateStr.substring(secondSpacer + 1, dateLen );
if ((!isNumeric(monthStr)) ||
(!isNumeric(dayStr))   ||
(!isNumeric(yearStr)))
{
return "";
}
if(monthStr.length == 1)
{
monthStr = "0" + monthStr;
}
if(dayStr.length == 1)
{
dayStr = "0" + dayStr;
}
var month = parseInt(monthStr,10);
var day = parseInt(dayStr,10);
var year = parseInt(yearStr,10);
if(yearStr.length == 2)
{
if (year > 71)
{
yearStr = "19" + yearStr;
}
else
{
yearStr = "20" + yearStr;
}
}
if ((monthStr.length != 2) ||
(dayStr.length != 2) ||
(yearStr.length != 4))
{
return "";
}
var daysInMonth = new Array(12);
daysInMonth[1] = 31;
daysInMonth[2] = 28;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;
if ((year%4 == 0 && year%100 != 0) || (year%400 == 0))
{
daysInMonth[2]=29;
}
if ((month < 1) || (month > 12))
{
return "";
}
if ((day < 1) || (day > daysInMonth[month]))
{
return "";
}
return (monthStr + "/" + dayStr + "/" + yearStr);
}

function isValidColor(elm, elmName, isRequired) {

	if (isFilled(elm)== false && isRequired==true) {
	  alert(elmName + " not entered.");	
	  return false;
	}
		
	if (isAlphaNum(elm) == false) {
		alert(elmName + " contains invalid characters.");	
		return false;    
	}

	var elmstr = elm.value + "";        

	if (elmstr.length < 6 ) {
		alert(errMsg = elmName + " must be 6 characters long.");	
		return false;    
	}
		return true;
}

function trimIt (str) {
  while (str.charAt(0) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
  return str;
}

function isValidPassword(elm,elmName,elmType) {
	var elmstr = elm.value + "";

	// Passwords cannot be empty
	if (isFilled(elm) == false){
		alert(elmType + " not entered. Please try again.");
		elmName.focus();
		return false
	}

	// Password can only be alphanumeric
	if (isAlphaNum(elm) == false) {
		alert(elmType + " contains invalid characters. Please try again.");	
		elmName.focus();
		return false;    
	}

	// Password must be at least 5 characters long
	if (elmstr.length < 5) {
		alert(elmType + " must be 6 characters long. Please try again.");	
		elmName.focus
		return false;    
	}

	return true;
}

function checkTextAreaLength(oTextArea,maxLength){
  if(oTextArea.value.length > maxLength){
	oTextArea.value = oTextArea.value.substr(0,maxLength);
  }
}
