﻿
//CHANGE HISTORY
//[001]         MM        14/09/2006    Date validation changed to include future date check
//[002]         HS        21/05/2007    System should also allow you to add wt in packing details if it has less than 1 in wt.(for ex- 0.5 etc)
//[003]         HS        17/10/2007    CR#-12 : Added Deleted Records only option.(Common function)   

var dtCh= "/";
var minYear=1900;
var maxYear=2100;
//var mikExp =  /[$\\@\\\#%\^\,\&\*\(\)\[\]\+\_\{\}\`\~\?\!\;\'\:\"\>\<\***\=\|]/;

function IsNumeric(strToCheck, strToAppend)
{
	if(strToCheck=="") return true;
	patern=/^[[0-9][0-9]*$/;
	var OK = strToCheck.match(patern)
	if (!OK)
	    {
	    strClientErrors=strClientErrors + strToAppend +" value should be numeric. <BR>";
		return false;
		}
	else
		return true;
}

function IsFloat(strToCheck, strToAppend)
{
    var strCheckNumeric = new String();
    strCheckNumeric = strToCheck;
    strCheckNumeric = strCheckNumeric.replace(".","");   
    
	if(strCheckNumeric=="") return true;
	patern=/^[[0-9][0-9]*$/;
	var OK = strCheckNumeric.match(patern)
	if (!OK)
	    {
	    strClientErrors=strClientErrors + strToAppend +" value should be numeric. <BR>";
		return false;
		}
	else
		return true;
}

function isIndex(selectedIndex,strToAppend)
{      
     
    if (  parseInt(selectedIndex) == 0 )
    {        
        strClientErrors=strClientErrors + strToAppend; 
        return false;
    }  
	else
	{
	return true;
	}
}
 function isZeroOrBlank(selectedValue,strToAppend)
{  
    //alert(parseInt(selectedValue));
    if ( selectedValue == 0  || selectedValue== '' )
    {           
        strClientErrors=strClientErrors + strToAppend + ' invalid. Please check. <br>' ; 
        //return  true ;
    } 
        else
	{
	    //return false;
	}
}   
function isZero(selectedValue,strToAppend)
{  
    //alert(parseInt(selectedValue));
    if (  parseInt(selectedValue) == 0   )
    {           
        strClientErrors=strClientErrors + strToAppend + ' invalid. Please check. <br>' ; 
        //return  true ;
    } 
        else
	{
	    //return false;
	}
}      
//Function parameters and functionality changed by PS on Dec 27, 2005
function isBlank(strToCheck, strToAppend)
{
//alert("strToCheck   fjjg "+strToCheck);
     if(strToCheck ==null)
     {    
     strClientErrors=strClientErrors + strToAppend + " cannot be left blank. <BR>";     
     return true;
     }
     
     if(strToCheck =='')
     {  
        strClientErrors=strClientErrors + strToAppend + " cannot be left blank. <BR>";     
        return true;
     }
     
     if(strToCheck.match(/^\s*$/))
     {
        strClientErrors=strClientErrors + strToAppend + " cannot be left blank. <BR>";
        return true
     }
     return false;
}

//Function parameters and functionality changed by PS on Dec 27, 2005
function CheckBlank(strToCheck)
{
     if(strToCheck ==null)
     {
        return true;   
     }
     if(strToCheck =='')
     {     
        return true;
     }
   
     if(strToCheck.match(/^\s*$/))
     {
        return true
     }
     return false;
}

function isComboUnselected(strComboID, strToAppend)
{
    //alert('kkkkk');
    objCombo = document.getElementById(strComboID);
    //alert(objCombo);
    //alert(objCombo[objCombo.selectedIndex].innerText.toUpperCase());
    if (objCombo.selectedIndex>-1)
    {
    if(objCombo[objCombo.selectedIndex].innerText.toUpperCase()=="NONE" )
    {
        strClientErrors=strClientErrors + "Please select a " + strToAppend + " <BR>";
    }
    }
    
}

function isDate(dtStr, strToAppend){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		strClientErrors = strClientErrors + " invalid " + strToAppend + " format [dd/mm/yyyy]<BR>";
		return false
	}
return true
}

//[001]
/****************************************************************************
Method Name		:	fnFutureDate
Parameters		:	String (Date,strToAppend)
Return Type		:	Boolean
Functionality		:	Accepts String and validates it to make 
					sure that the string is a valid date in 
					DD/MM/YYYY format. It also checks for
					LEAP YEAR i.e. in a leap year feb has 29 days
Author			:	
Creation Date		:	
*****************************************************************************/
function fnFutureDate(strDate,strToAppend)
{
    strDate = fnTrim(strDate);
    var nowD = new Date(); 
    var today = new Date(nowD.getYear(),nowD.getMonth(),nowD.getDate()); 
    var l_intDay  = strDate.charAt(0)+strDate.charAt(1);
	var l_intMonth = strDate.charAt(3)+strDate.charAt(4) ;
	var l_intYear = strDate.charAt(6)+strDate.charAt(7) + strDate.charAt(8)+strDate.charAt(9);

    var m_intDay  =nowD.getDate();
	var m_intMonth = nowD.getMonth() ;
	var m_intYear = nowD.getYear();
	m_intMonth = m_intMonth+1;

    if (l_intYear > m_intYear)
	{   
	    strClientErrors=strClientErrors + strToAppend + ' should not be future date. <BR>' ;		
		return false;
	}
	else if((l_intYear == m_intYear ) && (l_intMonth > m_intMonth))
	{
	    strClientErrors=strClientErrors + strToAppend + ' should not be future date. <BR>' ;		
		return false;
	}
	else if((l_intYear == m_intYear ) && (l_intMonth == m_intMonth) && (l_intDay > m_intDay))
	{
	    strClientErrors=strClientErrors + strToAppend + ' should not be future date. <BR>' ;		
		return false;
	}	
	else
	{
		return false;
	}   
	
}

//[001]
	

/****************************************************************************
Method Name		:	fnValidateDate
Parameters		:	String (Date,strToAppend)
Return Type		:	Boolean
Functionality		:	Accepts String and validates it to make 
					sure that the string is a valid date in 
					DD/MM/YYYY format. It also checks for
					LEAP YEAR i.e. in a leap year feb has 29 days
Author			:	
Creation Date		:	
*****************************************************************************/
function fnValidateDate(strDate,strToAppend)
{
	//First part is to remove any Leading and Trailing spaces
	//   
    var strDateFormat = " [ Date Format : DD/MM/YYYY ]";
	strDate = fnTrim(strDate) 

    if (strDate == '') 
	{  
		strClientErrors=strClientErrors+ strToAppend +' cannot be left blank.'+ strDateFormat +' <BR>' ;
		return false
	}

	if (strDate.length > 10)
	{
		strClientErrors=strClientErrors+ strToAppend +' length cannot exceed 10 characters.'+ strDateFormat +' <BR>' ;
		return false
	}

	if ((strDate.charAt(2) != '/') || (strDate.charAt(5) != '/'))
	{
		strClientErrors=strClientErrors+  'Enter ' + strToAppend +' in DD/MM/YYYY format. <BR>' ;
		return false
	}
	
	var l_intDay
	var strMonth
	var l_intMonth
	var l_intYear

	
	l_intDay  = strDate.charAt(0)+strDate.charAt(1)
	strMonth = strDate.charAt(3)+strDate.charAt(4) 
	l_intYear = strDate.charAt(6)+strDate.charAt(7) + strDate.charAt(8)+strDate.charAt(9)

    //alert( ' D ' + l_intDay + ' M ' + strMonth + ' Y ' + l_intYear);
    
	if (l_intDay.length < 2 || strMonth.length < 2 || l_intYear.length < 4)
	{
		strClientErrors=strClientErrors+  'Enter ' + strToAppend +' in DD/MM/YYYY format. <BR>';
		return false
	}

    l_intMonth = strMonth;
	//l_intMonth = fnGetMonth(strMonth);
	//if (l_intMonth == 0)
	//	return false;
	
	
	l_intDay = parseInt(l_intDay,10)
	l_intMonth = parseInt(l_intMonth,10)
	l_intYear = parseInt(l_intYear,10)
	

	if (isNaN(l_intDay) || isNaN(l_intMonth) || isNaN(l_intYear))
	{
		strClientErrors=strClientErrors+  'Invalid ' + strToAppend + strDateFormat +' <BR>';
		return false
	}

	if ((l_intDay < 0) || (l_intMonth < 0) || (l_intYear < 0))
	{
		strClientErrors=strClientErrors+  'Invalid ' + strToAppend + strDateFormat +' <BR>';
		return false
	}

	if(l_intYear<=1752)
	{
		strClientErrors=strClientErrors+  'Year should be greater than 1752.'+ strDateFormat +' <BR>' ;
		return false
	}

	if ((l_intDay == 0) || (l_intMonth == 0) || (l_intYear == 0))
	{
		strClientErrors=strClientErrors+  'Invalid ' + strToAppend + strDateFormat +' <BR>';
		return false
	}

	if (l_intMonth > 12)
	{
		strClientErrors=strClientErrors+  'Month can not be greater than 12.'+ strDateFormat +' <BR>' ;
		return false
	}

	if (l_intDay > 31 )
	{
		strClientErrors=strClientErrors+  'Day can not be greater than 31.'+ strDateFormat +' <BR>';
		return false
	}

	if ((l_intMonth==4)||(l_intMonth==6)||(l_intMonth==9)||(l_intMonth==11))
	{
		if (l_intDay > 30 )
		{
			strClientErrors=strClientErrors+  'Day can not be greater than 30.'+ strDateFormat +' <BR>';
			return false
		}
	}
	else if (l_intMonth==2)
	{
		if  ((l_intYear % 4 == 0) && ( (!(l_intYear % 100 == 0)) || (l_intYear % 400 == 0) ) )
		{
			if (l_intDay > 29)
			{
				strClientErrors=strClientErrors+  'Day can not be greater than 29 for a Leap Year.'+ strDateFormat +' <BR>';
				return false;
			}
		}
		else if (l_intDay > 28)
		{
			strClientErrors=strClientErrors+  'Day cannot be greater than 28 for a Non-Leap Year.'+ strDateFormat +' <BR>' 
			return false;
		}
     }

	return true;
}

function compareStrings(str1, str2, strToAppend)
{
    if(str1==str2)
    {
     return true;   
    }
    else
    {
        strClientErrors = strClientErrors + strToAppend + " are not same.<BR>";
        return false;
    }
}

//function isTime(strTime, strToAppend)
//{
//var regs;
//re=/^(\d{2}):(\d{2})?$/;
//if(strTime!='')
//{
//    regs=re.exec(strTime);//strTime.match(re);
//    if(regs)
//    {
//         if(regs[1] > 23 || regs[1]<0||regs[2] > 59 || regs[2]<0) 
//          {
//            strClientErrors = strClientErrors + "Invalid time in " + strToAppend +"<BR>";
//            return false;
//          }
//           
//    }
//        else
//        {
//        strClientErrors = strClientErrors + "Invalid time in " + strToAppend +"<BR>";
//        return false;
//        }
//}
//     
//else
//{
//    strClientErrors = strClientErrors + strToAppend +" Cannot be blank<BR>";
//    return false
//}
//return true;   
//}
//}
function isTime(field, strToAppend)
  {
    // regular expression to match required time format
    var ErrMsg;
    var regs;
    ErrMsg="";
    re = /^(\d{2}):(\d{2})?$/;
    
    if(field.value != '') 
    {
      
      regs = field.value.match(re);
      
      if(regs) 
      {
            
        if(regs[1] > 23 || regs[1]<0||regs[2] > 59 || regs[2]<0) 
          {
            strClientErrors = strClientErrors + "Invalid time in " + strToAppend +"<BR>";
            return false;
            //ErrMsg="Invalid time in " + strError;
          }
      }
      else
      {
             strClientErrors = strClientErrors + "Invalid time in " + strToAppend +"<BR>";
             return false;
             //ErrMsg="Invalid time in " + strError;
      } 
    }
    else
    {
        strClientErrors = strClientErrors + strToAppend +" Cannot be blank<BR>";
        return false
        //ErrMsg=strError + " Cannot be blank";
    }
    return true;
  }
    function CheckControlLength(obj,strName,strLength)
    {		        
        var intlength = new Number();
        var strValue = new String();
        intlength = parseInt(strLength);		        
        strValue = obj.value;		        
        if(strValue.length > intlength)
        {
            obj.value=strValue.substring(0,strValue.length-1);		            
            return false;
        }
        return true;
    }
    
    function fnCompareTime(strStartTime,strEndTime,strToAppend)
    {
        var intStartHours = new Number();
        var intEndHours = new Number();
        var intStartMins = new Number();
        var intEndMins = new Number();
        
        var arrStartTime;
        var arrEndTime;
        
        arrStartTime = strStartTime.split(":");
        arrEndTime = strEndTime.split(":");
        
        
        intStartHours = (arrStartTime[0]);
        intEndHours = (arrEndTime[0]);
        
        intStartMins = (arrStartTime[1]);
        intEndMins = (arrEndTime[1]);
 

//        alert(intStartHours)
//        alert(intStartMins)
//        alert(intEndHours)
//        alert(intEndMins)
        
        if (intStartHours > intEndHours)
        {
            strClientErrors=strClientErrors + "Start Time is greater then End Time for " + strToAppend + ". <BR>";
        }
        else if (intStartHours == intEndHours)
        {
            if (intStartMins > intEndMins)
            {
                strClientErrors=strClientErrors + "Start Time is greater then End Time for " + strToAppend + ". <BR>";
            }
        }
    }
        
    
function fnCheckDecimalPlaces(strToCheck,strNoOfPlaces,strToAppend)
{
    
    if (strToCheck != '' && strToCheck != null)
    {
        if (strToCheck.indexOf(".") >= 0)
        {
            var strNumber = new String;
            strNumber = strToCheck.substring(strToCheck.indexOf(".")+1,strToCheck.length)
            if (strNumber.length > strNoOfPlaces)
            {
               strClientErrors=strClientErrors + "Only " + strNoOfPlaces + " decimal places are allowed for " + strToAppend + ". <BR>";
            }   
        }
    }
}
//[002] START
function isZeroCheckFloatValue(selectedValue,strToAppend)
{  
    if (  parseFloat(selectedValue) == 0   )
    {           
        strClientErrors=strClientErrors + strToAppend + ' invalid. Please check. <br>' ; 
        //return  true ;
    } 
    else
	{
	    //return false;
	}
}      
//[002] END
//[003] START
function fnCheckDeleteValidation(strToCheck, td_message)
{
    strClientErrors = '';
    if(strToCheck.toUpperCase() != 'TRUE')
    {           
        strClientErrors = strClientErrors + 'Selected Record is already Deleted, cannot be modified. <br>' ; 
    }   
    
    if (strClientErrors != "")
    {                   
        setJavascriptError(td_message, strClientErrors);
        return false;
    } 
    else
    {
        setJavascriptError(td_message, "");
        return true;
    } 
}
function fnCheckEditValidation(strToCheck)
{    
    if(strToCheck.toUpperCase() != 'TRUE')
    {       
        var blnConfirm = confirm("Selected record is Deleted. \nPress OK to view Details, otherwise Press Cancel...");
        if (blnConfirm == false)
            return false;
    }
     return true;   
}

function fnCheckSaveValidation(strToCheck)
{
    if(strToCheck.toUpperCase() != 'TRUE')
    {                        
         var blnConfirm = confirm("Selected record is Deleted. \nPress OK to save the record, otherwise Press Cancel...");
         if (blnConfirm == false)
         return false;
    }
    return true
}
//[003] END