//***************************** isValidEmail ****************************
function isValidEmail(emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/;
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]";
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")";
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+';
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")";
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
//	alert("Email address seems incorrect (check @ and .'s)")
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
//    alert("The username doesn't seem to be valid.")
    return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
//	        alert("Destination IP address is invalid!");
		return false;
	    }
    }
    return true;
}

// Domain is symbolic name
var domainArray=domain.match(domainPat);
if (domainArray==null) {
//	alert("The domain name doesn't seem to be valid.");
    return false;
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
//   alert("The address must end in a three-letter domain, or two letter country.")
   return false;
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!";
//   alert(errStr);
   return false;
}

// If we've gotten this far, everything's valid!
return true;
}
//  End -->


//**********************************************************************************

function countCheckboxChecked(fieldName, end, charField, formName)
{
  countChecked = 0;
  
  for (i = 1; i <= end; i++)
  {
    if (charField == 1)
    {
      checkboxField = eval('document.forms["' + formName + '"].'+ fieldName + String.fromCharCode(96 + i));
    }
    else
    {
      checkboxField = eval('document.forms["' + formName + '"].'+ fieldName + i);
    }

    if (checkboxField.checked)
    {
      countChecked += 1;
    }
      
  }// outer for loop
  
  return countChecked;
    
} // end function clearCheckbox

//**********************************************************************************


function isOKAllCheckbox(fieldName, end, maxAnswers, minAnswers, charField, formName)
{
  answeredQtnsCount = 0;
  for (i = 1; i <= end; i++)
  {
    if (charField == 1)
    {
      checkboxField = eval('document.forms["' + formName + '"].'+ fieldName + String.fromCharCode(96 + i));
    }
    else
    {
      checkboxField = eval('document.forms["' + formName + '"].'+ fieldName + i);
    }
    if (checkboxField.checked)
    {
      answeredQtnsCount += 1;
    }
      
  }// outer for loop
  
  
  if (answeredQtnsCount > maxAnswers)
    return 1; // error type. More than MAX answers
  else
     if (answeredQtnsCount < minAnswers)
       return 2; // error type. Less than MIN answers
       else
         return 0; // No error type.
  
    
} // end function isOKAllCheckbox

//**********************************************************************************
// if the field has one group of   buttons then instead of field name like fld_1, fld_2 ...
// the name will be fld (no 1,2,3 ...)

function isOKAllRadio(fieldName, end, charField, formName)
{
 
  for (i = 1; i <= end; i++)
  {
    // if it is a character at the end like q1_a, q1_b .....
    if (charField == 1)
    {
      radioField = eval('document.forms["' + formName + '"].'+ fieldName + String.fromCharCode(96 + i));
    }
    else
    // if it is a number at the end like q1_1, q1_2 .....
    {
      radioField = eval('document.forms["' + formName + '"].'+ fieldName + i);
    }
    
    // if the field is one radio group then no 1,2 etc will appended at end of the name
    if (end == 1)
    {
      radioField = eval('document.forms["' + formName + '"].'+ fieldName);
    }

    currChecked = false;
        
    for (k=0; k < radioField.length; k++) // inner for loop
    {

      if (radioField[k].checked)
      {
        currChecked = true;
      }
    }// inner for loop
    
    if (!currChecked)
    {
      isChecked = false;
      return isChecked;
    }
    
  }// outer for loop
  
  isChecked = true;
  return isChecked;
  
} // end function isOKRadio

//**********************************************************************************

// This function is used to check whether a radio button is selected in the same column 
// in a group of radio buttons
// for ex : r1 r1 r1
//          r2 r2 r2
//          r3 r3 r3

// if r1-1 is selected r2-1 and r3-1 is not to be selected and so on

function isOtherRadioSelected(fieldName, end, currentField, formName)
{  
   
 currentField = eval('document.forms["' + formName + '"].'+ currentField);
 //get the index of the current radio button
 
 optionChecked = false;

 for (x=0; x < currentField.length; x++) // for loop 1
 {
    if (currentField[x].checked)
    {
      currentFieldCheckedIndex = x;
      optionChecked = true;
    }
  } // for loop 1

  if (optionChecked) // if - 1
  {
    for (i = 1; i <= end; i++) // for loop 2
    {
      /*
        if (end == 1)
        {
          radioField = eval('document.forms["' + formName + '"].'+ fieldName);
        }
      */ 
      // if the radio field is in the same position and not the current field
      radioField = eval('document.forms["' + formName + '"].' + fieldName + i);
      if (radioField[currentFieldCheckedIndex].checked && radioField != currentField)
      {
        return true;
      }
      
    }// for loop - 2
  }//   if - 1
  return false;
} // end function isOtherRadioSelected
   
//**********************************************************************************
function isTextEmpty(fieldValue)
{  
   while(''+fieldValue.charAt(0) == ' ')
   {
     fieldValue = fieldValue.substring(1,fieldValue.length);
   }
   
   if (fieldValue == '')
       return true;
   else
       return false;
   
} // end isTextEmpty
   
   
//**********************************************************************************
   
function isDropDownSelected(fieldName, formName)
{  
  dropDownField = eval('document.forms["' + formName + '"].' + fieldName);
  if (dropDownField.options.length == 0) { return false;}
  if ( dropDownField.options[dropDownField.selectedIndex].value == '' ) return false;
  return true;
}  

//**********************************************************************************
   
function isRadioChecked(fieldName, formName)
{  
   radioField = eval('document.forms["' + formName + '"].' + fieldName);
   currChecked = false;
   
   for (k=0; k < radioField.length; k++)
   {
     if (radioField[k].checked)
     {
       currChecked = true;
     }
   }
    
   if (!currChecked)
   {
     return false;
   }
   return true;
   
}

//**********************************************************************************
function isCheckBoxChecked(fieldName, formName)
{  
   checkBoxField = eval('document.forms["' + formName + '"].' + fieldName);
   currChecked = false;
   
   if (checkBoxField.checked)
   {
     currChecked = true;
   }
    
   if (!currChecked)
   {
     return false;
   }
   return true;
   
}

//**********************************************************************************
function checkDropdownsRank( formName, StartObjNumber, EndObjNumber )
{  

  for ( i = StartObjNumber; i <= EndObjNumber; i++ )
  { // for 1 
  
    formObject = eval('document.forms["' + formName + '"].elements[' + i + ']');
//  alert( "formObject name:" + formObject.name );
    for ( j = StartObjNumber; j <= EndObjNumber; j++ )
    { // for 2
      formObject1 = eval('document.forms["' + formName + '"].elements[' + j + ']');
  //  alert( "formObject1 name:" + formObject1.name );
      
      if ( formObject.name != formObject1.name )
      {
        if ( formObject[formObject.selectedIndex].value == formObject1[ formObject1.selectedIndex ].value )
        {
  return false;
        }
      }
    } // for 2
  } // for 1


    return true;


} // end function checkDropdownsRank()

//**********************************************************************************
//**********************************************************************************
function checkUserName (usr_name) {
	  var error = "";
	  var illegalChars = /\W/;

    if (isTextEmpty(usr_name)) {
       error = "Please enter your user name!";
    } 
    
    /*
    else if((usr_name.length < 4)){
       error = "The user name should have minimum 4 characters.";
    }
    else if((usr_name.length > 15)){
       error = "The user name should be less than 15 characters.";
     }   
    // allow only letters, numbers, and underscores
    else if (illegalChars.test(usr_name)) {
       error = "The user name contains illegal characters or white space.";

    } 	
    */
    
    if (error != ""){
    	 alert(error);
       return false;	
    } 
    else {
       return true;	
    }
    
} //checkUsername()

//**********************************************************************************
function checkPassword (pas_word) {
	  var error = "";
	  var illegalChars = /[\W_]/;

    if (isTextEmpty(pas_word)) {
       error = "Please enter your password!";
    } 
    else if((pas_word.length < 3)){
       error = "The password should have minimum 6 characters.";
    }
    else if((pas_word.length > 11)){
       error = "The password should be less than 10 characters.";
     }   
    // allow only letters, numbers, and underscores
    else if (illegalChars.test(pas_word)) {
       error = "The password contains illegal characters.";

    } 	
    
    if (error != ""){
    	 alert(error);
       return false;	
    } 
    else {
       return true;	
    }
    
} //checkPassword()

//**********************************************************************************
function getFormValues(frmObj){
   var str = "";
   
   for(var i = 0;i < frmObj.elements.length;i++)
   {
       var elementType = frmObj.elements[i].type;
       
       if (elementType == "select-one"){
          str += frmObj.elements[i].name + "=" + frmObj.elements[i].options[frmObj.elements[i].selectedIndex].value + "&";
       } else {
          str += frmObj.elements[i].name + "=" + escape(frmObj.elements[i].value) + "&";
       }
   }
   
   str = str.substr(0,(str.length - 1));
   return str;
}
//**********************************************************************************
