function isInteger(s)
{   
	var i;
	for (i = 0; i < s.length; i++)
	{   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag)
{   
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{   
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkInternationalPhone(strPhone, validChars, minDigits )
{
	s=stripCharsInBag(strPhone,validChars);
	return (isInteger(s) );
}



function findObj(n, d)
{
  var p,i,x;
  if( !d )
  {
    d = document;
  }
  if( ( p = n.indexOf("?") ) > 0 && parent.frames.length )
  {
    d = parent.frames[n.substring(p+1)].document;
    n = n.substring(0,p);
  }
  if( !( x = d[n] ) && d.all )
  {
    x = d.all[n];
    for( i = 0; !x && i < d.forms.length; i++ )
    {
      x = d.forms[i][n];
    }
    for( i=0; !x && d.layers && i < d.layers.length; i++ )
    {
      x = findObj( n, d.layers[i].document );
    }
  }
  return x;
}


function validateFormFields()
{
 var curr_token, flag;
 var i, errors = '', args=validateFormFields.arguments;
 
 for (i = 0; i < ( args.length - 1 ); i += 3)
 {
  flag = args[i+1];
  if( flag != 'S' )
  {
    curr_token = findObj( args[i] );
  }

  if( flag == 'R' )
  {
    if( curr_token.value == "" || curr_token.value == "-1" )
    {
      errors += '- ' + args[i+2] + ' is required.\n';
    }
  }

  if( flag == 'RC' ) 
  {
    next_token = findObj( args[i+3] );
    // A password is required so check both
    if( curr_token.value != "" && next_token.value != "" )
    {
      if( curr_token.value != next_token.value )
      {
        errors += '- Passwords dont match.\n';
      }
    }
    else
    {
      errors += '- Please fill-in both password fields.\n';
    }
  }
  
  if( flag == 'C' )
  {
    // A password may not be required here
    if( curr_token.value != "" )
    {
      next_token = findObj( args[i+3] );
      if( curr_token.value != next_token.value )
      {
        errors += '- Passwords dont match.\n';
      }
    }
  }

  if( flag == 'C2' )
  {
    // A password may not be required here
    if( curr_token.value != "" )
    {
      next_token = findObj( args[i-3] );
      if( curr_token.value != next_token.value )
      {
        errors += '- Passwords dont match.\n';
      }
    }
  }
  
  if( flag == 'VE' )
  {
		// Validate Email
		if( curr_token.value != "" )
		{
			var str = curr_token.value;
			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			
			if ( str.indexOf(at) == -1 || 
					 str.indexOf(at) == -1 || 
					 str.indexOf(at) == 0 || 
					 str.indexOf(at) == lstr ||
					 str.indexOf(dot) == -1 || 
					 str.indexOf(dot) == 0 || 
					 str.indexOf(dot) == lstr ||
					 str.indexOf(at,(lat+1))!= -1 ||
					 str.substring(lat-1,lat) == dot || 
					 str.substring(lat+1,lat+2) == dot ||
					 str.lastIndexOf(dot)+1 == lstr ||
					 str.indexOf(dot,(lat+2)) == -1 ) 
			{
	      errors += '- Please enter a Valid Email Address\n';
			}
		}
		else
		{
      errors += '- ' + args[i+2] + ' is required.\n';
		}
  }
  
  if( flag == 'VP' )
  {
		// Validate Phone
		if( curr_token.value != "" )
		{
			var digits = "0123456789";
			// non-digit characters which are allowed in phone numbers
			var validPhoneNumberDelimiters = "+()- ";

			if ( !checkInternationalPhone(curr_token.value, validPhoneNumberDelimiters) ) 
			{
	      errors += '- Please enter a Valid Phone Number\n';
			}
		}
		else
		{
      errors += '- ' + args[i+2] + ' is required.\n';
		}
  }
 } // end for
 

 if( errors )
 {
  alert( 'The following error(s) occurred:\n' + errors );
 }
 document.returnValue = ( errors == '' );
}
