﻿// JScript File

//Function checks whether the supplied string is empty or not
function IsStringEmpty(argString)
{
    var Empty = false;
    var lString = Trim(argString);
    
    if(lString == "")
        Empty = true;

    return Empty;
}

//Function checks whether the supplied string is empty or not
function IsControlEmpty(argControlID)
{
    var Empty = false;
    var lString="";
    lString = document.getElementById(argControlID).value;
    
    if(Trim(lString) == "")
    {
        document.getElementById(argControlID).value = "";
        Empty = true;
    }
    
    return Empty;
}

//Trim spaces from the right of the string
function RTrim(argString)
{
    var lString = "";
    
    for(i = argString.length - 1; i >= 0; i--)
    {
        if (argString[i] != " ")
        {
            lString = argString.substring(0, i + 1);
            break;
        }
    }
    
    return lString;
}

//Trim spaces from the left of the string
function LTrim(argString)
{
    var lString = "";
    
    for(i = 0; i <= argString.length; i++)
    {
        if (argString[i] != " ")
        {
            lString = argString.substring(i, argString.length);
            break;
        }
    }
    
    return lString;
}

//Trim spaces from the left and right of the string
function Trim(argString)
{
    var lString = argString;

    lString = RTrim(lString);
    lString = LTrim(lString);
    
    
    return lString;
}

//Checks whether the supplied value is numeric
function IsNumeric(argControlID)
{
    return !isNaN(document.getElementById(argControlID).value);
}


//Check Integer Value
function isOnlyIntegerValue(field)
  {  
      var check = true;        
      var value = document.getElementById(field).value; 
      if(value.length>0)
      {
          for(var i=0;i < value.length; ++i)
          {
               var new_key = value.charAt(i); //cycle through characters
               
               if(((new_key < "0") || (new_key > "9")) && 
                    !(new_key == ""))
               {
                    check = false;
                    break;
               }
          }
      }
      else
      check=false;           
      return check;
  }
  
  //Check Decimal Value
   function validateDecimal(ctrlID)//fun 1
    {
       var varValue = document.getElementById(ctrlID).value;
        if(isNaN(varValue))
        {
            alert("Enter numeric value");
            document.getElementById (ctrlID).value='0'; 
            document.getElementById(ctrlID).focus();
            return true;
        }
        else if (varValue == '0.00')
        {
            document.getElementById(ctrlID).value='0'; 
            document.getElementById(ctrlID).focus();
            return true;
        }
        else
        {
            var arrParts = varValue.split('.');            
            if(arrParts.length > 1)
            {
               if(arrParts[1].length < 1)
                {
                   varValue = arrParts[0];
                }
            }
            document.getElementById(ctrlID).value = varValue;
            return true;
        }     
    }


//Limits the string length
function LimitStringLength(argString, argMaxLength)
{
    var lString = "";
    
    if(argString.length > argMaxLength)
        lString = argString.substring(0, argMaxLength);
    else
        lString = argString
    
    return lString;
}

//Compare two strings and return true if both are equal else false
function CompareStrings(argString1, argString2)
{
    var Equal = false;
    
    if(argString1 == argString2)
        Equal = true;
        
    return Equal;
}

//Compare two strings and return true if both are equal else false
function CompareStrings1(argControlID1, argControlID2)
{
    var Equal = false;
    
    if(document.getElementById(argControlID1).value  == document.getElementById(argControlID2).value )
        Equal = true;
        
    return Equal;
}

//Clears the text box value
function ClearTextBox(argTextBoxID)
{
    document.getElementById(argTextBoxID).value = "";
}

//Clears the Label value
function ClearMsg(lblLebal)
{
    var lbl=document.getElementById(lblLebal);    
    document.getElementById(lblLebal).innerHTML = "";
    if(document.getElementById(lblLebal).innerHTML == "")
    lbl.style.visibility = 'hidden';  
    else
    lbl.style.visibility = 'visible';  
    return false;
}


//Sets the focus on the control
function SetFocusOnControl(argControlID)
{
    document.getElementById(argControlID).focus();
}

//Clears the form on the client side
function ClearForm(argFormID)
{
    for(i = 0; i<= document.forms.item(argFormID).elements.length - 1;i++)
    {
        if(document.forms.item(argFormID).elements.item(i).type == "text" || document.forms.item(argFormID).elements.item(i).type == "textarea" || document.forms.item(argFormID).elements.item(i).type == "file")
            document.forms.item(argFormID).elements.item(i).value = "";
        else if(document.forms.item(argFormID).elements.item(i).type == "select-one")
            document.forms.item(argFormID).elements.item(i).selectedIndex = -1;
        else if(document.forms.item(argFormID).elements.item(i).type == "checkbox")
            document.forms.item(argFormID).elements.item(i).checked = false;
        //else if(document.form1.elements.item(i).type == "checkbox")
        //    document.form1.elements.item(i).checked = false;
    }
}

function IsValueSelected(argSelect)
{
    var Selected = true;
    
    if(document.getElementById(argSelect).selectedIndex == 0 || document.getElementById(argSelect).selectedIndex == -1)
        Selected = false;
        
    return Selected;
}

function IsListBoxSelected(argSelect)
{
    var Selected = true;
    
    if(document.getElementById(argSelect).selectedIndex == -1)
        Selected = false;
        
    return Selected;
}

function getStringLength(argControlID)
{
    var len = 0;    
    len = document.getElementById(argControlID).value.length;    
    return len;
}

function IsValidName(argControlID)
{
    var valid = true;
    var username = /^\b\w[a-zA-Z\s]+$/;
	  
	if(document.getElementById(argControlID).value.match(username)==null)
	{
	    valid = false;
	}
	  
    return valid;
}

function IsValidFolderName(argControlID)
{
    var valid = true;
    var username = /^\b\w[_a-zA-Z0-9]+$/;
	  
	if(document.getElementById(argControlID).value.match(username)==null)
	{
	    valid = false;
	}
	  
    return valid;
}


function IsValidNumber(argControlID)
{
    var valid = true;
    var numbervalue = /^\+?[\d]+$/;
    
	if(document.getElementById(argControlID).value.match(numbervalue) == null)
	{
    	valid = false;
	}
	
    return valid;
}

function IsValidDecimal(argControlID)
{
    var valid = true;
    var decimalvalue = /^[0-9-]{1,15}(\.\d{1,2})?$/;
    
	if(document.getElementById(argControlID).value.match(decimalvalue) == null)
	{
	    valid = false;
	}
	
	return valid;
}

function IsNegativeValue(argControlID)
{
    var valid = false;
    
	if(document.getElementById(argControlID).value < 0)
	{
	    valid = true;
	}
	
	return valid;
}

function IsDate1Greater(argControlID1, argControlID2)
{
    var lDate1 = new Date(document.getElementById(argControlID1).value);
    var lDate2 = new Date(document.getElementById(argControlID2).value);

    var DaysDiff = Math.floor((lDate1.getTime() - lDate2.getTime())/(1000*60*60*24));
                
    if(DaysDiff > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function IsValidEmail(argControlID)
{
    var valid = true;
    
    var lEmailInputString = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/; 
    var lMatchArray = document.getElementById(argControlID).value.match(lEmailInputString);
     
    if(lMatchArray == null)
    {
        valid = false;
    }
    
    return valid;
}

function getSubString(argControlID, argStartIndex, argEndIndex)
{
    var lString = document.getElementById(argControlID).value;
    
    lString = lString.substring(argStartIndex, argEndIndex);
    
    return lString;
}
