// ====================================================================
// Module : mod_form.js
// Description : Common HTML Form Validation Function// Amendment History:
// 2001/06/13  Edward      Creation
// ====================================================================

/*===========================================================================

  Error Handling Function
  
  Parameter(s) obj      : String -> object name
               err_msg  : String -> Error Message which will be display on Alert

  return false;               
  ===========================================================================
*/
function onErrorAlert(obj, err_msg)
{
 var obj_type ="";
 	if (obj.options)
 		obj_type ="select"
 	else if (obj.length)
 		obj_type = obj[0].type;
 	else  obj_type = obj.type;
 		alert(err_msg);
 	
 	if ((obj_type != "radio")&&(obj_type!="hidden")){
 		
 		obj.focus();
     	
     		if (obj_type != "select"){
     			obj.select();
	 	}
	 }  return false;
}




/*=========================================================================== 

   Checking object has value or not
 
   Parameter(s) obj      :  String -> object name
                obj_type :  String -> object type 
                
   return true or false             
  ===========================================================================
*/
function isEmpty(obj, obj_type)
{
  //Text Box
  if (obj_type == "TEXT" || obj_type == "PASSWORD" || obj_type == "TEXTAREA")
     {
      if (obj.value.length == 0) 
          return true;
      else 
      	  return false;
     }
  //Radio button or checkbox
  if (obj_type == "RADIO" || obj_type == "CHECKBOX")
     {
      if (obj.length) {		  for (i=0; i < obj.length; i++)
		  {
			if (obj[i].checked)
			return false;
		  }
	  } else {			if (obj.checked)
			return false;
	  }      return true;	
     }   
   //Select List
   if (obj_type == "SELECT")
	{
      if (obj.length) {		for (i=0; i < obj.length; i++)
	    	{
		if (obj.options[i].selected)
			return false;
		}
	  } else {			if (obj.selected)
			return false;
	  }		return true;	
	}
}	

/* ===========================================================================
   Email Checking

   Parameter(s) inputVal  :  String -> email string
                
   return true or false             
   ===========================================================================
*/
function isEmail(inputVal){
  if(inputVal.indexOf("@")!=-1 && inputVal.indexOf(".")!=-1 && inputVal !=""){
    return true;
  }else {
    return false;
  }
}

/*
  ===========================================================================
  Integer Checking

  Parameter(s) inputVal  :  Integer 
                
  return true or false             
  ===========================================================================
*/ 
function isInteger(inputVal)
{
  var checkVal;
  checkVal = new String(inputVal);
  if (isNaN(checkVal) || checkVal.indexOf('.') > - 1)
      return false;
  else    
      return true;
}	

function isPosInteger(inputVal)
{
  var checkVal;
  checkVal = new String(inputVal);
  if ((!isInteger(inputVal)) || parseFloat(inputVal) < 0)
      return false;
  else    
      return true;
}	

function isNumberOnly(inputVal)
{
     if (!isInteger(inputVal) || inputVal.length==0)
         return false;
     else
        {
         for (i=0; i<inputVal.length; i++)
             {
              if (inputVal.charAt(i) == "+" || inputVal.charAt(i) == "-")
                 {
                  return false;
                 } 	
             }
        return true;
        }     
}

/* 
   ===========================================================================
   Number Checking

   Parameter(s) inputVal : Numeric 
                
   return true or false             
   ===========================================================================
*/
function isNumber(inputVal)
{
  if (isNaN(inputVal))
      return false;
  else    
      return true;
}	

/* ===========================================================================
   Integer Range Checking

   Parameter(s) inputVal  : Integer
                min_value : Integer -> if value == null value will set to 0
                max_value : Integer -> if value == null will not check
                
   return true or false             
   ===========================================================================
*/
function isIntegerInRange(inputVal, min_value, max_value)
{  if (min_value == null)
       min_value = 0;

   if (isInteger(inputVal) && isInteger(min_value) && inputVal >= min_value)
      {
       if (max_value != null)
          { if (isInteger(max_value) && inputVal <= max_value)
                return true;
            else
                return false;
          }
       else              
          return true;
      } 	 
   else
       return false;    
}


function isNumberInRange(inputVal, min_value, max_value)
{  if (min_value == null)
       min_value = 0;

   if (isNumber(inputVal) && isNumber(min_value) && inputVal > min_value)
      {
       if (max_value != null)
          { if (isNumber(max_value) && inputVal <= max_value)
                return true;
            else
                return false;
          }
       else              
          return true;
      } 	 
   else
       return false;    
}

/*=========================================================================== 
  String Length Checking

  Parameter(s) inputVal  : String
               min_value : Integer -> if value == null value will set to 0
               max_value : Integer -> if value == null will not check
               
  return true or false             
  ===========================================================================
*/
function isValidStringLength(inputVal, min_value, max_value)
{
  if (min_value == null)
       min_value = 1;
  if (!isInteger(min_value) || inputVal.length < min_value)
      return false;
  else
     {  
     if (max_value != null)        
        {
         if (inputVal.length > max_value || !isInteger(max_value))
             return false;
         else
             return true;    
         } 
     else
         return true;
     }    
}

/* ===========================================================================
   Date Checking

   Parameter(s) inyyyy : String
   		inmm   : String
   		indd   : String
                
   return true or false             
   ===========================================================================
*/
function isDate(inyyyy, inmm, indd) 
{var InputDate;		
  InputDate = new Date(inyyyy,inmm-1,indd);	
  if (parseFloat(InputDate.getDate()) >= parseFloat(indd)) 
     { if ((parseFloat(indd) <= 0) || (parseFloat(inmm) <= 0) || (parseFloat(inmm) > 12) || (parseFloat(inyyyy) <= 0))
       	   return false;
      else
           return true;
     }
  else 
    { return false;}
}

/* ===========================================================================
   Date Validation

   Parameter(s) inputVal     : DateTime
   		split_code   : Char    -> Split Code "-" or "/" is value == null then value will set to "/"
   		date_format  : Integer -> Range 1 to 5 if value == null then value will set to 1
                
   return true or false             
   ===========================================================================
*/
function isValidDate(inputVal, split_code, date_format)
{
  if (split_code == null || split_code.length == 0)      
      split_code = "/";
  if (date_format != 5)
     {
      if ((inputVal.length > 10)||(inputVal.length < 6))    
          return false;
      a_date = inputVal.split(split_code);    
      if (a_date.length != 3)
          return false;
      if (date_format == null || date_format.length == 0)
          date_format = 1;
      if (date_format == 1)    //Format : YYYY-MM-DD or YYYY/MM/DD (Default)
         {
          if (isDate(a_date[0],a_date[1],a_date[2])) 	
             return true;
          else
            return false;    
         }
      if (date_format == 2)    //Format : DD-MM-YYYY or DD/MM/YYYY
         {
          if (a_date[1].length==1){
          	a_date[1]="0"+a_date[1];
          }
          if (a_date[0].length==1){
          	a_date[0]="0"+a_date[0];
          }
          if (a_date[2].length==2){
          	if (parseInt(a_date[2]) >= 40){
          		a_date[2]="19"+a_date[2];
          	}else{
          	        a_date[2]="20"+a_date[2];
          	}
          }                    
          if (isDate(a_date[2],a_date[1],a_date[0])) 	
              return true;
          else
             return false;    
        } 
     }
  else
   {       
      if (date_format == 5) //Time -> HH:MM      
         {
          return isValidTime(inputVal)
        }
   }     
}

function lrs_isValidDate(inputVal, split_code, date_format)
{
          if (inputVal.length > 10)
                   return false;
          a_date = inputVal.split(split_code);    
          if (a_date.length != 3)
                   return false;
      //(isNaN(checkVal)
	  if (a_date[1].length <= 2){ 	  	
	  	if (isNaN(a_date[1]))
	  	   return false;          	   
	  }else{
	           return false;
	  }
	  if (a_date[0].length <= 2){	  	
	  	if (isNaN(a_date[0]))
	  	   return false;          	             	
	  }else
	  	   return false;
	  if (a_date[2].length <= 4){	  	
	  	if (isNaN(a_date[2]))
	  	   return false;          	             	
	  }else
	  	   return false;          
	  	   
	  return true;
              
}
/* ===========================================================================
   get Current Date Time

   Parameter(s) date_format : Integer -> Range 1 to 5 if value == null then value will set to 1
   		split_code  : Char    -> Split Code "-" or "/" is value == null then value will set to "/"
   		
                
   return date
   ===========================================================================
*/
function getCurrentDate(date_format, split_code)
{
  var CurrentDate;  
  if (split_code == null)
     split_code = "/";
  if (date_format == null)
     date_format = 1;   
  var d = new Date();
  var year = d.getFullYear();
  var month = String(parseFloat(d.getMonth()+1));
  if (month.length == 1)
      month = "0"+month;
  var day = String(d.getDate());
  if (day.length == 1) 
      day = "0"+day;
  var hr =  String(d.getHours());
  if (hr.length == 1)    
      hr = "0"+hr;
  var min = String(d.getMinutes());
  if (min.length == 1)    
      min = "0"+min;
  if (date_format == 1) //YYYY-MM-DD or YYYY/MM/DD
      return year+split_code+month+split_code+day;
  if (date_format == 2) //DD-MM-YYYY or DD/MM/YYYY
      return day+split_code+month+split_code+year;
  if (date_format == 3) //YYYY-MM-DD HH:MM or YYYY/MM/DD HH:MM 
      return year+split_code+month+split_code+day+' '+hr+':'+min;
  if (date_format == 4) //DD-MM-YYYY HH:MM or DD/MM/YYYY HH:MM
      return day+split_code+month+split_code+year+' '+hr+':'+min;
  if (date_format == 5) //HH:MM
      return hr+':'+min;
}

/*
  ===========================================================================
  Function to Date Compare 
  
  Parameter(s) : inputVal1    : DateTime -> Compare Date 1
  	         inputVal2    : DateTime -> Compare Date 2 if value == null then get NOW to compare
  	         compare_code : Char     -> Compare Symbol e.g >, >= ,==, <, <=
  	         date_format  : int      -> Range 1 to 5 if value == null then value will set to 1
  	         split_code   : Char     -> Split Code "-" or "/" is value == null then value will set to "/"
  
  Return value : true or false  	         
  ===========================================================================  	      
*/
function isValidDateCompare (inputVal1, inputVal2, compare_code, date_format, split_code)
{
 if (!isValidDate(inputVal1, split_code, date_format))
      return false;
 else
    {
     if (inputVal2 == null || inputVal2.length==0)	     
         inputVal2 = getCurrentDate(date_format, split_code) //set date to now
     if (!isValidDate(inputVal2, split_code, date_format))
         return false;
     else
        {
         condition = eval("'"+inputVal1+"'"+compare_code+"'"+inputVal2+"'")
         if (condition) 
             return true;
         else
             return false;    
         }
    }     
}

/* 
  ===========================================================================
  Time Checking 
  
  Parameter(s) : inputVal    : DateTime 
  
  Return value : true or false  	         
  ===========================================================================
*/
function isValidTime(inputVal)
{ 
  if (inputVal.length != 5 || inputVal.indexOf(":") == -1)
      return false;
  a_time = inputVal.split(":")
  if (!isInteger(a_time[0]) || !isInteger(a_time[1]) || 
       a_time[0] < 0 || a_time[0] > 23 || a_time[1] < 0 || a_time[1] > 59 ||
      (a_time[0] == 24 && a_time[1] > 0 ))   
      return false;
  else
      return true;
}

/*
  ===========================================================================
  Card Checking

  Parameter(s) : cardno1 : Integer
   		 cardno2 : Integer
    		 cardno3 : Integer
                 cardno4 : Integer
                 
  Return value : true or false  	         
  ===========================================================================
*/
function isValidCard(cardno1, cardno2, cardno3, cardno4)
{
      if (!isInteger(cardno1) || !isInteger(cardno2) || !isInteger(cardno3) || !isInteger(cardno4))    
          return false;
      else
         {
         if (!isValidStringLength(cardno1,4) || !isValidStringLength(cardno2,4) || !isValidStringLength(cardno3,4) || !isValidStringLength(cardno4,4))
             return false;
         else
             return true;    
         }    
}/*
  ===========================================================================
  setAllCheckBox

  Parameter(s) : obj : Checkbox control		   		 value : boolean                 
  ===========================================================================
*/
function setAllCheckBox(obj, value) {
	if (obj.length) {
			for (i=0; i < obj.length; i++)
{
				obj[i].checked = value;
			}
	}else {
		obj.checked = value;
	}
}

function setAllCheckBoxByName(frm, name, value) {
	var i=0;
	var sname = new String(name);
	for (i=0; i<frm.elements.length; i++) {
		if (frm.elements[i].type=="checkbox") {
			if (frm.elements[i].name.substr(0,sname.length)==sname) {
				var obj = frm.elements[i];
				if (obj.length) {
					for (i=0; i < obj.length; i++)
{
						obj[i].checked = value;
					}
				} else {
					obj.checked = value;
				}
			}
		}
	}
}

