******************************************************************************
*File name:validation.js
*Description:Referenced by the pages to validate the form values
*Author:Justin Wang 214026,Infosys
*Date:06-Oct-2005
******************************************************************************/


/******************************************************************************
*File name:checkLeapYear(nYear)
*Description:Check the input year if it is leap year.
*Input parameters:
* nYear- year to be checked
*Return:true on success ,false on no
******************************************************************************/
function checkLeapYear(nYear){
 if (nYear % 4 == 0)
  if (nYear % 100 == 0)
   if (nYear % 400 == 0)
    return true; 
   else
    return false;
  else
   return true;
 else
  return false;
}

/******************************************************************************
*File name:checkNum(element,value)
*Description:Check the input string only contains number.
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkNum(element,value){
 isOK=checkNull(element,value);
 if (!isOK){       
  return false;
 }
 str="1234567890";
 content=checkContent(value,"@NULL",str);
 if(content!=true){
  alert(element+" should be number only")
  return false;
 }
 return true;
}


/******************************************************************************
*File name:trim(value)
*Description:Delete the space between characters.
*Input parameters:
* value- input string.
*Return:String which contains no spaces
******************************************************************************/
function trim(value){
 var checkStr = value;
 var tempStr = '';
 var countBlank = 0;
 var countBlankEnd = 0;
 for(i = 0 ; i < checkStr.length ; i++){
  if (checkStr.charAt(i)!= ' '){
   break;
  }
  else{
   countBlank ++;
  }
 }

 for(i = checkStr.length - 1 ; i > 0 ; i--){
  if (checkStr.charAt(i) != ' '){
   break;
  }
  else{
   countBlankEnd ++;
  }
 }
 for(i = countBlank ; i<checkStr.length - countBlankEnd ; i++){
  tempStr += checkStr.charAt(i);
 }
 return(tempStr);
}

/******************************************************************************
*File name:checkNull(element,value)
*Description:Check if the value is NULL
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkNull(element,value){
 if(value==''){
  alert(element+" should not be left blank!");
  return false;
 }

 temp=trim(value);
 if(temp==''){
  alert(element+" should not be only spaces!")
  return false;
 }

 return true;
}


/******************************************************************************
*File name:checkContent(value,start,content)
*Description:Check the start character and the body of the string.
*Input parameters:
*   value - The value to be checked
* start - a string which contains the validate character for the first
*  character and "@NULL" on no check on the first character .
* content-  a string which contains the validate character for the remain
*  part of the String.
*Return:true on success ,false on error
******************************************************************************/
function checkContent(value,start,content){ 
    ch1 =value.charAt(0);
    startnull="@NULL";
 if(start!=startnull){
  for(k = 0;k < start.length; k ++){
   if(ch1 == start.charAt(k)){
   break;
   }
  }
  if(k == start.length){
   return false;
  }
    }
 
    for(i = 0;i < value.length;i ++ ){
  ch = value.charAt(i);
  for(j = 0;j < content.length;j ++ ){
   if( ch == content.charAt(j)){
    return true;
   }
  }
  if(j==content.length){
   return false;    
  }
 }
 
 return(true);
}

/******************************************************************************
*File name:checkGeneralInput(element,value)
*Description:Check the general input:should only be space and character and not
* null.
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkGeneralInput(element,value){
 isOk=checkNull(element,value);
 if(!isOk){  
  return false;
 }
 
 str=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 content=checkContent(value,"@NULL",str);
 
 if(content==true){ 
  return true;
 }
 else{
  alert(element+" should only be space and character!" );
  return false;
 }

}

/******************************************************************************
*File name:checkAddr(element,value)
*Description:Should not be left blank and only spaces(Max 50 characters)
*Input parameters:
* element - Name of the form element.
* value- The value of the form.
*Return:flag-relay on the return value of checkNull(element,value)
******************************************************************************/
function checkAddr(element,value){
 result=checkNull(element,value);
 return result;
}

/******************************************************************************
*File name:checkGender(value)
*Description:should select at least one option
*Input parameters:* 
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkGender(value){
 for(i=0;i<value.length;i++){
  if(value[i].checked==true){
  return true;
  }
 }
 alert("Please select your gender")
 return false;
}

/******************************************************************************
*File name:checkName(element,value)
*Description:should meet checkGeneralInput(element,value);
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:flag-relay on the value of checkGeneralInput(element,value)
******************************************************************************/

function checkName(element,value){
 flag=checkGeneralInput(element,value);
 return flag;
}

/******************************************************************************
*File name:checkCity(element,value)
*Description:should meet checkGeneralInput(element,value);
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:flag-relay on the value of checkGeneralInput(element,value)
******************************************************************************/
function checkCity(element,value){
 flag=checkGeneralInput(element,value);
 return flag;
}

/******************************************************************************
*File name:checkState(element,value)
*Description:should meet checkGeneralInput(element,value);
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:flag-relay on the value of checkGeneralInput(element,value)
******************************************************************************/
function checkState(element,value){
 flag=checkGeneralInput(element,value);
 return flag;
}

/******************************************************************************
*File name:checkPin(element,value)
*Description:Should not be blank,only numbers and spaces,only spaces not
* allowed.
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkPin(element,value){ 
 isOk=checkNull(element,value);
 if(!isOk)
  return false;
         str=" 1234567890";
 content=checkContent(value,"@NULL",str);
 if(content==true)
  return true;
 else{
  alert(element+" should be space and number");
  return false;
 }
}

/******************************************************************************
*File name:checkTel(element,value)
*Description:only numbers and spaces allowed
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkTel(element,value){
 str=" 1234567890";
 content=checkContent(value,"@NULL",str);
 if(content==true)
  return true;
 else{
         alert(element+" should be space and number");
  return false;
 }
}

/******************************************************************************
*File name:checkFax(element,value)
*Description:only numbers and spaces allowed
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkFax(element,value){
 str=" 1234567890";
 content=checkContent(value,"@NULL",str);
 if(content==true)
  return true;
 else
  return false;
}


/******************************************************************************
*File name:checkMail(element,value)
*Description:Only spaces not allowed,should have one '@' character and one or
* more '.','@' and '.' should come only after the first 5 characters.
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkEmail(element,value){
 isOk=checkNull(element,value);
 if(!isOk){
  return false;
 }
  
 index=value.indexOf(".");
 indexlast=value.lastIndexOf(".");
 if(index==-1||indexlast==value.length-1){
         alert("Email format invalid,should contain \".\"");
 return false;
 }

 place1=value.indexOf("@");
 place2=value.lastIndexOf("@");
 if(place1!=place2||place1==-1||place1<5){
    alert("Email format invalid,should contain \"@\" following 5 characters");
 return false;
 }

 return true;

}

/******************************************************************************
*File name:checkDate(element,value)
*Description:DD-MON-YYYY format,Should be a valid Date
*Input parameters:
* element - Name of the form element.
* date- date.
*Return:true on success ,false on error
******************************************************************************/

function checkDate(element,date){
       /*validate the content*/
        str="-1234567890ABCDEFGJLMNOPRSTUVYabcdefgjlmnoprstuvy";
 contentOK=checkContent(date,"@NULL",str);
 
 if(!contentOK){  
        alert(element+" contains invalid character! ")
  return false;
 } 
 
 
 /*validate the format*/
 index1=date.indexOf("-");
 index2=date.lastIndexOf("-");
 
 if(index1!=2||index2!=6||date.charAt(2)!="-"||date.charAt(6)!="-"){
  alert(element+" should be \"dd-mmm-yyyy\"");
  return false;
 }
 
 
 /*validate the input of day*/
 var day=date.substring(0,date.indexOf('-'));
 fDay=parseFloat(day); 
 
 
 if(isNaN(fDay)||(!isFinite(fDay))){
  alert("Day should be number please!")
  return false;
                   } 
 if(fDay>31||fDay<1){
         alert("Day"+" should be proper");
  return false;
 } 
 
 /*validate the input of month*/ 
 var month=
 date.substring(date.indexOf('-')+
 1,date.lastIndexOf('-')).toLowerCase();
 
 var arr = new Array('Jan',
 'Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
 
 for(var index = 0;index <= 11;index++){
  if(arr[index].toLowerCase()==month.toLowerCase())
   break;  
 }
 if(index>11){
  alert("Month"+" should be proper like \"Apr/apr \"");
  return false;
 } 
 
 
 /*validate the input of year*/
 var year=date.substring(date.lastIndexOf('-')+1,date.length);
 fYear=parseFloat(year);
 datenow=new Date();
 
 if(isNaN(fYear)||(!isFinite(fYear))){
   alert("Year should be number please!")
   return false;
                  }
 if(fYear>=datenow.getFullYear()){
   alert("Year should be proper");
   return false;
 }   
 
 
 /*validate the relation of month,day*/
 if(fDay>29&&"feb"==month.toLowerCase()){
  alert("Feb has only max 29 days and 28 when leap year");
         return false;   
 }
 if(fDay>28&&"feb"==month.toLowerCase()&&checkLeapYear(fYear)){  
  alert("Feb has only 28 days when leap year");
  return false; 
 }
  
 switch(month.toLowerCase()){
  case "apr":;
  case "june":;
  case "sep":;
  case "nov":if(fDay>30) {
  alert("Please check the relation between day and month!");
  return false;
  }
  default:break;
 }  
 return true;
}


/******************************************************************************
*File name:checkID(element,value)
*Description:Should not be blank,Only numbers
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkID(element,value){
 isOk=checkNum(element,value);
 if(!isOk){
      return false;
  }
        else
 return true;
}

/******************************************************************************
*File name:checkDeposit(element,value)
*Description:Only numbers .Amount should be greater than 500
*  (Maximum of two decimal points are allowed)
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkIniDeposit(element,value){
    isOk=checkNum(element,value);
 if(!isOk){
         return false;
 }
 
 temp=parseFloat(value);
 if(temp<=500){
  alert("Initial deposit should larger than 500Rs")
  return false;
 } 
 return true;
}

/******************************************************************************
*File name:checkAccNum(element,value)
*Description:Should not be blank,only numbers
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkAccNum(element,value){
        isOk=checkNum(element,value);
        if(!isOk){
        return false;
        }
 else
 return true;
}

/******************************************************************************
*File name:checkAmount(element,value)
*Description:Amount should be greater than 0 (Maximum two decimal
* points are allowed),only numbers
*Input parameters:
* element - Name of the form element.
* value- The value of the element.
*Return:true on success ,false on error
******************************************************************************/
function checkAmount(element,value){
 isOK=checkNull(element,value);
 if (!isOK)
 return false;
 
 str=".1234567890";
 content=checkContent(value,"@NULL",str);
 if(content!=true){
               alert(element+" should be number and \".\" only!");
               return false;
 }
 
 index=value.indexOf(".");
 index2=value.lastIndexOf(".");
 
 if(index!=index2){
  alert(element+" format error!");
  return false;
 }
 else if(index!=-1&&index!=value.length-3){
  alert("Two decimal points are allowed");
  return false;
 }
 
 temp=parseFloat(value); 
 if(temp<0){
  alert(element+" should be greater than 0 ");
 return false;
 }  
 return true;
}