<!-- Begin
/*Naming method
**I name variables by follow following rules
**1.Variables are form of type and description: type_description eg. int_pageNumber.
**2.Type is consisted of 3 characters only, if the length is not enough, use the first 3 characters.
**3.The first one character of type can't be removed.
**4.The sound charcters would be removed.
**5.If there is consecutive same characters, we would get one only.
**6.Boolean variables are the exception, their prefixes are "is" eg. isinteger.
*/

//Object constructor
function DataValidation()
{
  this.checkInteger = checkInteger;
  this.checkDecimal = checkDecimal;	 
  this.checkString = checkString;
  this.checkDate = checkDate;	
  this.checkEmail = checkEmail;
  this.checkSelectValue = checkSelectValue;
  this.checkCheckBoxValue = checkCheckBoxValue;
  this.checkRadioButtonValue = checkRadioButtonValue;
  this.checkElementsInForm = checkElementsInForm;
}

//Integer data checking
function checkInteger(str_data, int_length)
{
  if(str_data == "")
    return "Integer_error_empty";  	
  if(isNaN(str_data))
    return "Integer_error_type";
  else if(str_data!=parseInt(str_data))
    return "Integer_error_type";  
  else if(int_length!="" && str_data.length!=int_length)
    return "Integer_error_length";
  else
    return true;     
}

//Decimal data checking
function checkDecimal(str_data, int_length)
{	
  if(str_data == "")
    return "Decimal_error_empty";
  else if(isNaN(str_data))
    return "Decimal_error_type";
  else if(int_length!="" && str_data.length!=int_length)
    return "Decimal_error_length";       
  else
    return true;
}

//String data checking
function checkString(str_data, int_length)
{
  if(str_data == "")
    return "String_error_empty";
  else if(int_length!="" && str_data.length!=int_length)
    return "String_error_length";
  else
    return true;
}

//Email checking
function checkEmail(str_data)
{
  
}

//Date checking
function checkDate(intLength, str_data, str_dateSeperator)
{
  var temp = new String();
  var str_year;
  var str_month;
  var str_day;
  var check1;
  var check2;
  var str_functionIndicator = true;
  var int_dayLimit;

  temp = str_data;
  if(temp.length < intLength)
     str_functionIndicator = "Date_error_length";
  else
  {
    check1 = temp.indexOf(str_dateSeperator, 0);
    check2 = temp.indexOf(str_dateSeperator, check1+1);

    if (check1==-1 || check2==-1)
      str_functionIndicator = "Date_error_format";
    else
    {
      str_year = temp.substring(0, check1);
      if(str_year.length <4 || str_year.length>4 || str_year!=parseInt(str_year))
        str_functionIndicator = "Date_error_year";
      str_month = temp.substring(check1 + 1, check2);
      str_month = parseInt(str_month, 10);
      if(str_month<1 || str_month >12 || str_month.length <1 || str_month.length >2 || str_month!=parseInt(str_month))
        str_functionIndicator = "Date_error_month";
      else
      {
        str_day = temp.substring(check2 + 1, temp.length);
        str_day = parseInt(str_day, 10);
        if(str_month==1 || str_month==3 || str_month==5 || str_month==7 || str_month==8 || str_month==10 || str_month==12)
          int_dayLimit = 31;
        else
        {
          if(str_month == 2)
          {
            if(str_year%4 == 0)
              int_dayLimit = 29;
            else
              int_dayLimit = 28;
          }
          else
            int_dayLimit = 30;
        }
        if(str_day<1 || str_day>int_dayLimit || str_day.length>2 || str_day.length<1 || str_day!=parseInt(str_day))
          str_functionIndicator = "Date_error_day";
      }//else
    }//else
  }//else
 
 return str_functionIndicator;
}

//Select value checking
function checkSelectValue(str_data)
{
  if(str_data == "")
    return "select_error_selected";
  else
    return true;
}

//Check box checking
function checkCheckBoxValue(str_data)
{
  if(str_data == "false")
    return "CheckBox_error_checked";
  else
    return true;    
}

//Radion button checking
function checkRadioButtonValue(str_data)
{
  if(str_data == "false")
    return "RadioButton_error_checked";
  else
    return true;  
}

//Check the validation of data in the form by value type and name suffix
function checkElementsInForm(obj_form, nameSuffix, valueType)
{
  var int_length2;
  var str_functionIndicator = "true";
  var int_length = obj_form.elements.length;  
  for(int_i=0; int_i<int_length; int_i++)
  {  
    var str_name = obj_form.elements[int_i].name;    
    var str_type = obj_form.elements[int_i].type;     
    int_length2 = nameSuffix.length;
    for(int_j=0; int_j<int_length2; int_j++)
    {
      var int_position = str_name.lastIndexOf(nameSuffix[int_j]);
      if(int_position != -1)     
      {      	      	
        if(valueType[nameSuffix[int_j]] == "integer")
          str_functionIndicator = checkInteger(obj_form.elements[int_i].value,"");    
        else if(valueType[nameSuffix[int_j]] == "decimal")
          str_functionIndicator = checkDecimal(obj_form.elements[int_i].value,"");            
        else if(valueType[nameSuffix[int_j]] == "string")
          str_functionIndicator = checkString(obj_form.elements[int_i].value,"");            
        else if(valueType[nameSuffix[int_j]] == "email")
          str_functionIndicator = checkEmail(obj_form.elements[int_i].value);            
        else if(valueType[nameSuffix[int_j]] == "date")
          str_functionIndicator = checkDate(obj_form.elements[int_i].value);            
        else if(valueType[nameSuffix[int_j]] == "select")                         
          str_functionIndicator = checkSelectValue(obj_form.elements[int_i].options[0].value);      
        else if(valueType[nameSuffix[int_j]] == "checkbox")                         
          str_functionIndicator = checkCheckBoxValue(obj_form.elements[int_i].checked);   
        else if(valueType[nameSuffix[int_j]] == "radiobutton")                         
          str_functionIndicator = checkRadioButtonValue(obj_form.elements[int_i].checked);                                                                    
        
        if(str_functionIndicator != true)        
        {
          int_j = int_length2; 
          int_i = int_length;
        }        
      }
    }//for
  }//for
  
  return str_functionIndicator;
}
// End -->