function getSelectedRadioButton(rButtonGroup) {
// takes in a radiobutton element group as an input
// returns the index of the radiobutton that is checked
// returns -1 if none is checked
	for (var i=0; i < rButtonGroup.length;i++) {
		if (rButtonGroup[i].checked) {
			return i;
		}
	}
	return (-1);
}

// check whether the input year is a leap year
function leapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if (intYear % 4 == 0) { return true; }
	}
	return false;
}


function isEmpty(inputStr) {
//general purpose function to check if an input value has been enterd 
//at all
	pattern = /^[\s]+$/
	if ((inputStr == null) || (inputStr=="")||(inputStr.match(pattern))) return true
	else return false
}

function isString(str) {
// takes in a string and the required number of digits
// returns true IF the lenth is <= req. no of digits AND IF 
// string contains only letters
// OTHERWISE returns false
//
// if you want to check for spaces as well then set 
// pattern = /^[a-zA-Z\s]+$/

	pattern = /^[a-zA-Z\s]+$/
	if (!str.match(pattern)) return false
	else return true;
}

// takes in a string and the required number of digits
// returns true IF the lenth is <= req. no of digits AND IF 
// it checks for validity of the email address interms of its characters
// OTHERWISE returns false
//
// if you want to check for spaces as well then set 
// pattern = /^[a-zA-Z\s]+$/

function isValidChar(str) {
	pattern = /^[0-9a-zA-Z\.\_\-\@\~\s]+$/
	if (!str.match(pattern)) return false
	else return true;
}

// numbers, characters, dashes & underscores only
function isValidChar3(str) {
	pattern = /^[0-9a-zA-Z\_\-]+$/
	if (!str.match(pattern)) return false
	else return true;
}


function isValidChar1(str) {
	pattern = /^[0-9a-zA-Z\.\_\-\@\~\(\)\/\s]+$/
	if (!str.match(pattern)) return false
	else return true;
}

function isValidChar2(str) {
	pattern = /^[0-9a-zA-Z\.\_\-\@\~\;\:\'\"\,\.\/\?\s]+$/
	if (!str.match(pattern)) return false
	else return true;
}

function isNumber(str) {
// takes in a string and the required number of digits
// returns true IF the lenth is <= req. no of digits AND IF string 
// contains only numbers from 0-9
// OTHERWISE returns false
//
	pattern= /^[0-9]+$/
	if (!str.match(pattern)) return false
	else return true;
}


// function isCurrency implemented using regular expressions:

function isCurrency(str)
{
	pattern= /^[0-9]*\.?[0-9]{0,5}$/
	if (!str.match(pattern)) return false
	else return true;
}

// function isTelephone implemented using regular expressions:

function isTelephone(str)
{
	pattern=  /^[0-9a-zA-Z\-\(\)\+\,\/\s]+$/
	if (!str.match(pattern)) return false
	else return true;
}

// this checks for the set of invalid characters !@#$%^*=+[]{}\|<>`~;'",./?
//function isInvalidChar(str) {
//	pattern=  /^[\!\@\#\$\%\^\*\=\+\-\~\`\,\;\'\"\?\<\>\[\]\{\}\|\\\/]+$/
//	if (!str.match(pattern)) return false
//	else return true;
//}

function isValidIPAddress(ipaddr) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) { return false; }
      }
      return true;
   } else {
      return false;
   }
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}



/*
==================================
Validations for normal form fields
==================================
*/


// for passwords - any character is allowed; only spaces are not allowed
function isValidPwd(str) {
	if (str.lastIndexOf(" ") == -1) return true
	else return false;
}

// for usernames - only alpha numeric characters and underscore are allowed
function isValidUsername(str) {
	pattern = /^[0-9a-zA-Z\_]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for names (first, middle, last, full name) - only characters, spaces, (.) and (') are allowed
function isValidName(str) {
	pattern = /^[a-zA-Z\s\.\']+$/
	if (str.match(pattern)) return true
	else return false;
}

// for occupation/designation/profession - only alpha numeric characters, spaces and following characters: ()-/.' are allowed
function isValidProfession(str) {
	pattern = /^[0-9a-zA-Z\s\.\'\(\)\-\/]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for city - only alpha numeric characters, spaces, (-) and (.) are allowed
function isValidCity(str) {
	pattern = /^[0-9a-zA-Z\s\-\.]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for postal code - only numbers are allowed
function isValidPCode(str) {
	pattern = /^[0-9]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for country - only characters, spaces, (-) and (.) are allowed
function isValidCountry(str) {
	pattern = /^[a-zA-Z\s\-\.]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for telephone numbers - only alpha numeric characters, spaces and following characters: ()-+/, are allowed
function isValidTelno(str) {
	pattern = /^[0-9a-zA-Z\s\(\)\-\+\/\,]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for mobile (sms) numbers - only numbers, spaces and following characters: ()-+/ are allowed
function isValidSmsno(str) {
	pattern = /^[0-9\s\(\)\-\+\/]+$/
	if (str.match(pattern)) return true
	else return false;
}

// for email - only alpha numeric characters, (@), (_), (-) and (.) are allowed and it should be in the given pattern
function isValidEmail(str) {
	pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/
	if (str.match(pattern)) return true
	else return false;
}

