/*
 filename   : utilsrc.js
 location   : utilsrc.js
 Created by : Quaid
 Date       : 27/09/2002
 Description: functions to validate form data
 */
function isvaliddate(idd, imm, iyr){
  if (isNaN(idd) || isNaN(imm) || isNaN(iyr)){
    return false;
  }

  if (idd < 1 || idd > 31 || imm < 1 || imm > 12 ||  iyr < 1988 || iyr > 3000){
    return false;
  }
  if (((imm == 4) || (imm == 6) || (imm == 9) || (imm == 11)) && (idd == 31)) {
     return false;
  }
  if ((imm == 2)   && 
      (idd  == 29) && 
      (((parseInt(iyr % 4) == 0) && (parseInt(iyr % 100) != 0)) || (parseInt(iyr % 400) == 0)) 
     ) {
  } else { 
    if ((imm == 2) && ((idd == 29) || (idd == 30) || (idd == 31))) {
         return false;
    }
  }
  return true;
}

function removeleadzeros(istr)
{
  var i, ostr, rmspc;
  i=0;
  ostr='';
  rmspc = true;
  for (i=0;i < istr.length;i++){
    if (!((istr.charAt(i) == '0') && (rmspc == true)))
    {
      rmspc = false;
      ostr += istr.charAt(i);
    }
  }
  return ostr;
}

function removeleadspaces(istr)
{
  var i, ostr, rmspc;
  i=0;
  ostr='';
  rmspc = true;
  for (i=0;i < istr.length;i++){
    if (!((istr.charAt(i) == ' ') && (rmspc == true)))
    {
      rmspc = false;
      ostr += istr.charAt(i);
    }
  }
  return ostr;
}
function removetrailspaces(istr)
{
  var i, ostr, rmspc;
  i=0;
  ostr='';
  rmspc = true;
  for (i=(istr.length - 1);i >= 0; i--){
    if (!((istr.charAt(i) == ' ') && (rmspc == true)))
    {
      rmspc = false;
      ostr  = istr.charAt(i) + ostr;
     }
  }
  return ostr;
}
/* returns string containing only alphabets and numbers, other characters are rejected */
function compressstr(str)                                                                
{                                                                                       
  var i = str.length;                                                                   
  var j=0;                                                                              
  var ostr='';                                                                          
  for (j=0;j<i;j++)                                                                    
  {                                                                                     
    if ((str.charAt(j) >= 'A' && str.charAt(j) <= 'Z')||                                 
        (str.charAt(j) >= 'a' && str.charAt(j) <= 'z')||                                 
        (str.charAt(j) >= '0' && str.charAt(j) <= '9'))                                  
      ostr += str.charAt(j);                                                            
  }                                                                                     
  return(ostr);                                                                         
}                                                                                       
function isvalidint(istr)
{
   var i=0, ostr='', noec=0;
   if (istr.length == 0){
   return false;
   }
   if (isNaN(parseInt(istr)))
   { 
     return false;
   }
   /* remove leading and trailing spaces */
   istr = removeleadspaces(istr);
   istr = removetrailspaces(istr);
   /* now check if any error chars are there */
   ostr = '';
   noec = 0;
   for (i=0;i < istr.length;i++){
     if (i==0 && (istr.charAt(i) == '-' || istr.charAt(i) == '+'))
     {
        ostr += istr.charAt(i);
     }
     else
     if (istr.charAt(i) >= '0' && istr.charAt(i) <= '9')
     {
        ostr += istr.charAt(i);
     }
     else
     {
       noec ++;
     }
   }
   if (noec > 0)
     return false;
   else
     return true;
}
function isvaliddec(istr)
{
   var i=0, ostr='', noec=0, noofdot, rmspc;
   if (istr.length == 0){
   return false;
   }
   /* commented by dilip 21/12/2003 start */ 
   /*
   if (isNaN(parseInt(istr)))
   { 
     return false;
   } 
   */

    if (isNaN(parseFloat(istr)))
   { 
     return false;
   }
   /* remove leading and trailing spaces */
   istr = removeleadspaces(istr);
   istr = removetrailspaces(istr);
   /* now check if any error chars are there */
   ostr = '';
   noec = 0;
   noofdot = 0;
   for (i=0;i<istr.length;i++){
     if (i==0 && (istr.charAt(i) == '-' || istr.charAt(i) == '+'))
     {
        ostr += istr.charAt(i);
     }
     else
     if (istr.charAt(i) >= '0' && istr.charAt(i) <= '9')
     {
        ostr += istr.charAt(i);
     }
     else
     if (noofdot == 0 && istr.charAt(i) == '.')
     {
        ostr += istr.charAt(i);
        noofdot = 1;
     }
     else
     {
       noec ++;
     }
   }
   if (noec > 0)
     return false;
   else
     return true;
}
function isvalidalpha(istr){
  var i;
  var ostr='';
  for (i=0;i<istr.length;i++){
    if ((istr.charAt(i) >= 'A' && istr.charAt(i) <= 'Z')||
        (istr.charAt(i) >= 'a' && istr.charAt(i) <= 'z'))
      ostr += istr.charAt(i);
  }
  if (istr.length == ostr.length)
    return true;
  else
    return false;
}
function isvalidalphanum(istr){
  var i;
  var ostr='';
  for (i=0;i<istr.length;i++){
    if ((istr.charAt(i) >= 'A' && istr.charAt(i) <= 'Z') ||
        (istr.charAt(i) >= 'a' && istr.charAt(i) <= 'z') ||
        (istr.charAt(i) >= '0' && istr.charAt(i) <= '9'))
      ostr += istr.charAt(i);
  }
  if (istr.length == ostr.length)
    return true;
  else
    return false;
}
 function text1(inputval,fieldname)                     
 {                                                     
  var j = 0;                                           
  var j = 0;                                           
 var s = 'Not Proper text in' + fieldname;             
 msg='Empty text box in' + fieldname;                  
 var inputstrlen;                                      
 inputstr=inputval.toString();                         
 inputstrlen = inputstr.length;                        
 if (inputstrlen== 0) {                               
      alert(msg);                                      
      return false;                                    
   }                                                   
 for (var i=0; i <= inputstr.length; i++) {           
 var onechar = inputstr.charAt(i);                     
 if ((onechar >= 'A' && onechar <= 'Z') || ( onechar >= 'a' && onechar <= 'z') || onechar == '-' || onechar == '/' 
    || onechar == '0' || (onechar >'0' && onechar < '9') || onechar == ' ' || onechar == '9' ) { 
   if(j==0 && onechar==' ') {   
      alert('Blank in the first place in' +fieldname);  
      break;                                            
   }                                                    
    j = j + 1;                                          
   if( j == inputstr.length ) {                        
    return true;                                        
  }                                                     
   }                                                    
 else                                                   
  {                                                    
   alert(s);                                            
   break;                                               
   }                                                    
 } /* for loop*/                                        
}                                                       
function isempty(istr){
  var i;
  var ostr='';
  if (istr.length == 0)
    return true;
  for (i=0;i<istr.length;i++){
    if ((istr.charAt(i) == ' '))
      ostr += istr.charAt(i);
  }
  if (istr.length == ostr.length)
    return true;
  else
    return false;
}

function comparedates(odd1, omm1, oyy1, odd2, omm2, oyy2){ 
  var oflag, odate1, odate2; 

  /*
  odd1  = removeleadzeros(odd1);
  omm1  = removeleadzeros(omm1);
  oyy1  = removeleadzeros(oyy1);
  odd2  = removeleadzeros(odd2);
  omm2  = removeleadzeros(omm2);
  oyy2  = removeleadzeros(oyy2);
  */

  if(!isvaliddate(odd1, omm1, oyy1) || !isvaliddate(odd2, omm2, oyy2)) { 
     oflag = -1;
  }
  else
  {
     /* now make dates */ 
     odate1 = (oyy1 * 10000) + (omm1 * 100) + (odd1 * 1);
     odate2 = (oyy2 * 10000) + (omm2 * 100) + (odd2 * 1);

     /* now compare */ 
     if (odate2 == odate1){ 
        oflag = 0; 
     } 
     else 
     if (odate1 > odate2){ 
        oflag = 1; 
     } 
     else 
     if (odate2 > odate1){ 
        oflag = 2; 
     } 
  } 
  return oflag; 
} 
 function isAmount(fieldName , integerPlaces , decimalPlaces  )                      
 {                                                                                  
  var str;                                                                           
  var separator = ".";                                                                    
  str = fieldName.value;                                                             
  if( isNaN(str) )                                                                   
  {                                                                                 
    alert( "The amount is not a number.");                                           
        return false;                                                                
  }                                                                                 
  var ar = str.split(separator);                                                     
     if (ar[0])                                                                        
    {                                                                               
     if (ar[0].length > integerPlaces )                                             
     {                                                                             
             alert(" The Amount exceeds the maximum alowed amount !");                   
             return false;                                                                     
     }                                                                             
    }                                                                               
     if (ar[1])                                                                        
    {                                                                               
     if (ar[1].length > decimalPlaces )                                             
     {                                                                             
               alert(" The Amount exceeds the maximum alowed number of decimal points !");  
                       return false;                                                             
     }                                                                             
    }                                                                               
    if (parseFloat(str) <= 0 )                                                       
    {                                                                               
               alert(" The Amount can not be zero or negative value !");                    
                       return false;                                                             
    }                                                                               
   return true;                                                                         
 }
function isAmountAllowZero(fieldName , integerPlaces , decimalPlaces  )                      
 {                                                                                  
  var str;                                                                           
  var separator = ".";                                                                    
  str = fieldName.value;                                                             
  if( isNaN(str) )                                                                   
  {                                                                                 
    alert( "The amount is not a number.");                                           
        return false;                                                                
  }                                                                                 
  var ar = str.split(separator);                                                     
     if (ar[0])                                                                        
    {                                                                               
     if (ar[0].length > integerPlaces )                                             
     {                                                                             
             alert(" The Amount exceeds the maximum alowed amount !");                   
             return false;                                                                     
     }                                                                             
    }                                                                               
     if (ar[1])                                                                        
    {                                                                               
     if (ar[1].length > decimalPlaces )                                             
     {                                                                             
               alert(" The Amount exceeds the maximum alowed number of decimal points !");  
                       return false;                                                             
     }                                                                             
    }                                                                               
    if (parseFloat(str) < 0 )                                                       
    {                                                                               
               alert(" The Amount can not be  negative !");                    
                       return false;                                                             
    }                                                                               
   return true;                                                                         
 }

function getwagonno(istr)                                                               
{                                                                                      
  var awgn='';                                                                          
  var nwgn='';                                                                          
  var cwgn='';                                                                          
  var i=0;                                                                              
  var j=0;           
  /* compress and make uppercase */                                                     
  istr=compressstr(istr);                                                               
  if (istr==null || istr=='' || istr.length > 12)                                       
  {                                                                                    
     return '';                                                                         
  }                                                                                    
  istr=istr.toUpperCase();                                                              
  /* now check whether the wagon no is correct -- NO further check for truck no*/       
  /* DONT MESS WITH i variable -- in this block */                                      
  /* begin */                                                                           
  while(istr.charAt(i) >= 'A' && istr.charAt(i) <= 'Z')  /* seperate out alpha part */  
  {                                                                                    
    awgn += istr.charAt(i);                                                             
    i++;  
  }       
  while(istr.charAt(i) >= '0' && istr.charAt(i) <= '9' )  /* seperate out num part */    
  {                                                                                    
    nwgn += istr.charAt(i);                                                             
    i++;                                                                                
  }        
  
  while ((istr.charAt(i) >= 'A' && istr.charAt(i) <= 'Z') ||
         (istr.charAt(i) >= '0' && istr.charAt(i) <= '9'))  /* seperate out alpha - num part */  
  {                                                                                    
    cwgn += istr.charAt(i);                                                             
    i++;                                                                                
  }                                                                                    
  /* extra characters in wagon no --- NOT ALLOWED */                                    
  if (i < istr.length) {                                                               
     return '';                                                                         
  }                                                                                    
  /* end */                                                                             
  /**/                                                                                  
  /*
  if ((awgn.length < 1) || (awgn.length > 3) ||                                         
      isNaN(nwgn) || (nwgn.length < 1) ||                                               
      (nwgn.length > 7) || parseFloat(nwgn)==0 ||                                         
      (cwgn.length > 2))
     return '';                                                                         
  */
  if ((awgn.length < 1) || (awgn.length > 3) ||                                         
      isNaN(nwgn) || (nwgn.length < 1) ||                                               
      parseFloat(nwgn)==0 || (cwgn.length > 2))
     return '';                                                                         
  /*pad with spaces the alpha part and zeros the num part*/                             
  j = 3 - awgn.length;                                                                  
  for (i=0;i<j;i++)                                                                     
    awgn += ' ';                                                                        
  j = 7 - nwgn.length;                                                                  
  for(i=0;i<j;i++)                                                                      
    nwgn = '0' + nwgn;                                                                  
  j = 2 - nwgn.length;                                                                  
  for(i=0;i<j;i++)                                                                      
    cwgn += ' ';      
  
  /* now combine alpha and num parts */                                                 
  istr= awgn + nwgn + cwgn;                                                             
  /* in the end return the value */                                                     
  return istr;                                                                          
}

/* return truck no */                                                                    
function gettruckno(istr)                                                                
{                                                                                       
  /* compress and make uppercase */                                                      
  istr=compressstr(istr);                         

/*dkp 09082005 start */                                      
/* if (istr==null || istr=='' || istr.length > 10) */
   if (istr==null || istr=='' || istr.length > 12)
/*dkp 09082005 end */                                     
  { 
     return '';                                                                         
  }                                                                                     
  istr=istr.toUpperCase();                                                              
                                                                                         
  /* in the end return the value */                                                      
  return istr;                                                                          
}                                                                                       

/* function added for rounding off the decimal
31/12/2003 start */

/* function getRound(inum,pos)
{
  if ( inum == '0.00' || inum == '' || inum == '0' ) {
       inum = 0;
    }
 
  var i = 0;
 
  var num = new String(inum);
  if (num == '0') 
     {
        return '0.00'; 
     } 
  if (num.indexOf('.') == -1) 
     {
       num = num + ".";
       for ( i = 1; i <= pos; i++ ) {
          num = num + "0";
       }
      return num; 
     }
 
 var intnum = num.substring(0,num.indexOf('.'));
 var decnum = num.substring(num.indexOf('.') + 1);
 var decnum1 = decnum.substring(0,pos);
 var decnum2 = decnum.substring(pos);
 
  decnum = decnum1 + '.' + decnum2;
  decnum = Math.round(decnum);
 
  decnum = new String(decnum);
  if ( decnum.length < 2 ) {
    decnum = decnum + '0';
  }

   return intnum + '.' + decnum;  

} */
function getRound(number,X) {
// rounds number to X decimal places, defaults to 2
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

/* 31/12/2003 end */
/* Rama Krishna 31/12/2003 Begin */
 function isPercentage(fieldName)                      
 {                                                                                  
   var str;                                                                           
   var separator = ".";
   var integerPlaces = 2;
   var decimalPlaces = 2;                                                                    
   str = fieldName.value;  
   if( isNaN(str) )                                                                   
     {                                                                                 
        alert( "The value is not valid percentage.");                                           
        return false;                                                                
     }                                                                                 
    
   var ar = str.split(separator);                                                     
     if (ar[0])                                                                        
        {                                                                               
          if (ar[0].length > integerPlaces )                                             
             {                                                                             
              alert(" The percentage value exceeds the maximum allowed value !");                   
              return false;                                                                     
             }                                                                             
        }                                                                               

     if (ar[1])                                                                        
        {                                                                               
          if (ar[1].length > decimalPlaces )                                             
             {                                                                             
               alert(" The Percentage exceeds the maximum alowed number of decimal points !");  
               return false;                                                             
             }                                                                             
        }                     
    if (parseFloat(str) < 0 )                                                       
       {                                                                               
          alert(" The Percentage can not be negative value !");                    
          return false;                                                             
       }                                                                               
   return true;                                                                         
 } 
/* Rama Krishna End  */                                                                                 

 function addDaystoDate(mydd, mymm, myyr, myndays) {
   if (isvaliddate(mydd, mymm, myyr))
   {
      fdt = new Date(parseFloat(myyr),(parseFloat(mymm) - 1),parseFloat(mydd));  
      var fdtinMS = fdt.getTime();
 
      var tdtinMS = fdtinMS + 60*60*24*1000*parseFloat(myndays);
      var tdt = new Date();
      tdt.setTime(tdtinMS);
      return tdt ;
   }
   else
   {
      return -1;
   }
   /*
 frm.tdd.value = tdt.getDate();
 frm.tmm.value = tdt.getMonth() + 1;
 frm.tyy.value = tdt.getYear();
 */
 }

 function getDateDiff(fdtyy, fdtmm, fdtdd, tdtyy, tdtmm, tdtdd)
 {
    fdt = new Date(parseFloat(fdtyy),(parseFloat(fdtmm) - 1),parseFloat(fdtdd));
    tdt = new Date(parseFloat(tdtyy),(parseFloat(tdtmm) - 1),parseFloat(tdtdd));
      var fdtinMS = fdt.getTime();
      var tdtinMS = tdt.getTime();
      var noofdays = (tdtinMS - fdtinMS)/(60*60*24*1000);
      return noofdays;
 }

/* Anand */
function getlastdateofmonth(mm, yyyy){
  tdt = new Date(parseFloat(yyyy),(parseFloat(mm) - 1),28) ; 
  tdt.setTime(tdt.getTime() + 4*1000*24*60*60);
  sub = tdt.getDate();
  tdt.setTime(tdt.getTime() - parseFloat(sub)*1000*24*60*60);
  mydt = tdt.getDate() + "/"  + (tdt.getMonth() + 1 ) + "/" + tdt.getFullYear();
  return mydt;
}

/* end */






