/**
  * This source is part of the Times of Money Financial Portal Software System and is 
 * copyrighted by i-flex Solutions Limited.

 * All rights reserved.  No part of this work may be reproduced, stored in a retrieval system, 
 * adopted or transmitted in any form or by any means, electronic, mechanical, photographic, 
 * graphic, optic recording or otherwise, translated in any language or computer language, 
 * without the prior written permission of i-flex Solutions Limited.

 * i-flex Solutions Limited.
 * 10-11, SDF I, SEEPZ, Andheri (East),
 * Mumbai - 400 096.
 * India

 * Copyright © 2000 i-flex Solutions Limited.
*/

/*
*  jsvalidation.js  
*  Date 09/16/2000
*  Html Form Validation Script
*  Jitendra Mehta
*/

var error = new Array();
var e=0;

//adding error msg
function add_error_msg(msg)
{
    error[e] = msg; 
    e++;
    return false;
}

//initialize error array          
function initialize_error_msgs()
{
 /*
 for(n=0;n<=error.length-1;n++)
 {
  error[n]=" ";
 }
 */
 error = new Array();
 e=0;
}

//display error msg
function display_error_msgs(form_name)
{
  var errDes="";
  //errDes = "Error Processing   " + form_name.name + "\n\n"; 

 for(n=0;n<=error.length-1;n++)
 {
  errDes=errDes+error[n]+"\n";
 }
 alert(errDes);
}

//check error array for displaying error msg
function check_display_errors(form_name)
{

  if(e>0)
  {
  	display_error_msgs(form_name);
 	return false;
  }
  else
  {
	 return true;
  }
}

//function check for empty field value
function is_empty_tb(textbox)
{
  if (textbox.value == '')
  {
	return true;
  }
  else
  {
   return false;
  }
}


function mod_is_empty_tb(textbox)
{
  chkString = escape(textbox.value)
  if( (chkString.substring(0,3)=="%20") && (textbox.value.length == 1))
  {	
        textbox.value="";
	return true;
  }
  else
  {
   return false;
  }
}


function printMsg(msg)
{
  return add_error_msg(msg) ;
}

function validate_maxlength_allowed(textbox,msg)
{
	var size;
	size=textbox.value.length;
	if(size>9 || size<9)
	{
		return add_error_msg(msg);
	}
}

function validate_maxlength_allowed(textbox,msg,maxsize)
{
	var size;
	var maxallowed;
	maxallowed = maxsize;
	size=textbox.value.length;
	if(size>maxallowed || size<maxallowed)
	{
		return add_error_msg(msg);
	}
}

//function checks for mandatory field's value
function validate_mandatory_tb(textbox)
{
  if(textbox.type=="text" || textbox.type=="textarea")
  var f_value = textbox.value;
  else
  if(textbox.type=="select-one")
  var f_value = textbox.options[textbox.selectedIndex].value;
     
  if (f_value == "") 
  {
    if(textbox.type=="text" || textbox.type=="textarea")
  	return add_error_msg(textbox.name + " must be entered");
	else
	if(textbox.type=="select-one")
	return add_error_msg("select " + textbox.name + " from SelectBox");
  }
}

//function checks for mandatory field's value and throws customised message as in argument
function validate_mandatory_tb_with_message(textbox, message)
{
  if(textbox.type=="text" || textbox.type=="textarea" || textbox.type=="hidden")
  var f_value = textbox.value;
  else
  if(textbox.type=="select-one")
  var f_value = textbox.options[textbox.selectedIndex].value;
  else
  {
   if(textbox[0].type=="radio")
   {
    var f_value = "";
    var radiolen = textbox.length;
    for(i=0;i<=radiolen-1;i++) 
    {
     if(textbox[i].checked)
     var f_value = "1";
	 
    }
   }
  } 
     
  if (f_value == "") 
  {
    if(textbox.type=="text" || textbox.type=="textarea")
  	return add_error_msg(message);
	else
	if(textbox.type=="select-one")
	return add_error_msg(message);
	else
	if(textbox[0].type=="radio")
	return add_error_msg(message);
  }
}


//function compares two textbox value
function validate_compare_tb(textbox1,textbox2)
{
 if(textbox1.value!=textbox2.value)
 return true;
 else
 return false;
}

//function checks for Numeric Value Entry
function validate_numeric_value(textbox)
{
 if(isNaN(textbox.value))
 {
  return add_error_msg(textbox.name + " must be a Numeric Value");
 }
}

//function checks for Numeric Value Entry with Apropriate Field Msg
function validate_numeric_value_with_msg(textbox,msg)
{
 if(isNaN(textbox.value))
 {
  return add_error_msg(msg + " must be a Numeric Value");
 }
}

//function checks for String Value
function validate_string_value(textbox)
{
 var str = textbox.value;
 for(i=0;i<=str.length-1;i++)
 {
  if(!(isNaN(str.charAt(i))))
  return add_error_msg(textbox.name + " must be String Value");
 }
}

//function checks for String Value Entry with Apropriate Field Msg
function validate_string_value_with_msg(textbox,msg)
{
 var str = textbox.value; 
 for(i=0;i<=str.length-1;i++)
 {
  if(!(isNaN(str.charAt(i))))
  return add_error_msg(msg + " must be String Value");
 }
}

//function checks for Alpha-Numeric Value
function validate_alpha_numeric_value(textbox)
{
  if(!(isNaN(textbox.value)))
  {
   return add_error_msg(textbox.name + " must be Alpha-Numeric OR String Value");
  }
}

//function checks for Alpha-Numeric Value Entry with Apropriate Field Msg
function validate_alpha_numeric_value_with_msg(textbox,msg)
{
  if(!(isNaN(textbox.value)))
  {
   return add_error_msg(msg + " must be Alpha-Numeric OR String Value");
  }
}

//function checks for spaces[begening and end of the string] in the value of textbox input field
function validate_textbox_space(textbox)
  {
    chkString = escape(textbox.value)
    if(chkString.substring(0,3)=="%20")
    return add_error_msg("Space not allowed at the begening of "+textbox.name+" = "+ textbox.value);
    if(chkString.substring(chkString.length-3,chkString.length+1)=="%20")
    return add_error_msg("Space not allowed at the end of "+textbox.name+" = "+ textbox.value);
  }
  
//function checks for spaces[begening and end of the string] in the value of textbox input field with Msg  
function validate_textbox_space_with_msg(textbox,msg)
  {
    chkString = escape(textbox.value)
    if(chkString.substring(0,3)=="%20")
    return add_error_msg("Space not allowed at the beginning of "+msg);
    if(chkString.substring(chkString.length-3,chkString.length+1)=="%20")
    return add_error_msg("Space not allowed at the end of "+msg);
  }  

//function checks specified decimal points in numeric value
function validate_decimal_value(textbox,dVal)
{
  var fldLen = textbox.value.length;
  for(i=0;i<=fldLen-1;i++)
  {
   if(textbox.value.charAt(i)==".")
   {
    if((parseInt(fldLen-1)-i)>parseInt(dVal))
	add_error_msg(textbox.value + " Only "+dVal+" decimal points allowed");
   }
  }
}

//function checks specified decimal points in numeric value with Msg
function validate_decimal_value_with_msg(textbox,dVal,msg)
{
  var fldLen = textbox.value.length;
  for(i=0;i<=fldLen-1;i++)
  {
   if(textbox.value.charAt(i)==".")
   {
    if((parseInt(fldLen-1)-i)>parseInt(dVal))
	add_error_msg(" Only "+dVal+" digits allowed after decimal point in "+ msg);
   }
  }
}

//function checks specified decimal points in numeric value and digits before decimal points
function validate_decimal_init_value(textbox,dVal,iVal,textbox_name)
{
  var fldLen = textbox.value.length;
  for(i=0;i<=fldLen-1;i++)
  {
   if(textbox.value.charAt(i)==".")
   {
    if((parseInt(fldLen-1)-i)>parseInt(dVal))
	add_error_msg("For "+textbox_name+" Only "+dVal+" digits allowed after decimal point");
	
	if((parseInt(i))>parseInt(iVal))
	add_error_msg("For "+textbox_name+" Only "+iVal+" digits allowed before decimal point");
   }
  }
}

//function checks specified decimal points in numeric value and digits before decimal points with Msg
function validate_decimal_init_value_with_msg(textbox,dVal,iVal,textbox_name,msg)
{
  var fldLen = textbox.value.length;
  for(i=0;i<=fldLen-1;i++)
  {
   if(textbox.value.charAt(i)==".")
   {
    if((parseInt(fldLen-1)-i)>parseInt(dVal))
	add_error_msg("For "+msg+" Only "+dVal+" digits allowed after decimal point");
	
	if((parseInt(i))>parseInt(iVal))
	add_error_msg("For "+msg+" Only "+iVal+" digits allowed before decimal point");
   }
  }
}

//function validates email values  
function validate_email_value(textbox)
{
 var email = textbox.value;
 var flag = new Boolean();
 
 if(email.charAt(0)!="@")
 { 
  for(i=0;i<=email.length-1;i++)
  {
   if(email.charAt(i)=="@")
   {
    for(j=i;j<=email.length-1;j++)
    {
     if(email.charAt(j)==".")
	 {
	  if(email.length-1>j+1)
	  return true;
	 } 
    }
    return add_error_msg("Please enter a valid email address");
   }
  }
  return add_error_msg("Please enter a valid email address");
 }
 else
 return add_error_msg("Please enter a valid email address");
}  


//function checks for integer value
function validate_integer_value(textbox)
{
 var fldlen = textbox.value.length;
 for(i=0;i<=fldlen-1;i++)
 {
  if(textbox.value.charAt(i)==".")
  add_error_msg(textbox.name + " must be Integer Value"); 
 }
}

//function checks for an integer value (no decimals allowed).
function validate_integer_value_msg(textbox,msg)
{
 var fldlen = textbox.value.length;
 for(i=0;i<=fldlen-1;i++)
 {
  if(textbox.value.charAt(i)==".")
  add_error_msg("Decimal value not allowed in "+msg); 
 }
}

function validate_ispositive(textbox)
{
 if(parseFloat(textbox.value)<=parseFloat("0")) 
 add_error_msg(textbox.name + " Value must be > 0"); 

}

function validate_ispositive_msg(textbox,msg)
{
 if(parseFloat(textbox.value)<=parseFloat("0")) 
 add_error_msg(msg+" should be a positive value"); 

}

//function checks for special char entry
function validate_special_char(textbox,msg)
{
 var amtinv = textbox.value;
 var validStr = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789/-?(),\"+\n\r";
	  
 for(i=0;i<=textbox.value.length-1;i++)
 {
  //alert(validStr.indexOf(amtinv.charAt(i)));
  if(validStr.indexOf(amtinv.charAt(i))==-1)
  {
   add_error_msg("Invalid character    "+amtinv.charAt(i)+"    entered for "+msg);
   break;
  }
 }
}

//Added by Subhendu
function clear_error_msg()
{
	e=0;
	return false;
}

