﻿// JScript File

function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


//Number only

function isInteger(value)   
{   
var re = /^\-?[0-9]+$/;   
return re.test(value);   
}  

function isString(value)   
{   
var re = /^[a-zA-Z]+$/;
return re.test(value);   
}  

function isAlphanumeric(value)
{
var re =  /^[0-9a-zA-Z]+$/;
return re.test(value);
}



// Email Validation

function isEMail(value)   
{   
var exp_email1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;   
var exp_email2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;   
return !exp_email1.test(value) && exp_email2.test(value);   
}  


//Currency Validation
function isCurrency(value)   
{   
var re = /^[0-9]+(\.[0-9]{2})?$/;   
return re.test(value);   
}  

//URL Validation
function isUrl(s) {   
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/   
return regexp.test(s);   
} 


function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}




//date validation

function checkdate(input)
{ 
    var validformat=/^\d{4}\/\d{2}\/\d{2}$/; //Basic check for format validity 
    var returnval=false;
    if (!validformat.test(input)) 
        alert("Invalid Date Format. Please correct and submit again.") 
    else
    { //Detailed check for valid date ranges 
    var yearfield=input.split("/")[0] 
    var monthfield=input.split("/")[1] 
    var dayfield=input.split("/")[2] 
    var dayobj = new Date(yearfield, monthfield-1, dayfield)
    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))   
            alert("Invalid Day, Month, or Year range detected. Please correct.") 
        else 
        { 
           
            returnval=true;
        }      
        
    }     
   
    return returnval 
} 



//Javascript PopUp

     function popUp()
     {
          var check=document.getElementById('pop_overlay').style.display;
          if(check=='none')
          {
                    document.getElementById('pop_overlay').style.display='';
                    document.getElementById('pop_container').style.display='';
          }
          else
          {
                    document.getElementById('pop_overlay').style.display='none';
                    document.getElementById('pop_container').style.display='none';
          }
     }


function CheckTextRequired(source, arguments)
{
    if (arguments.Value =="")
    {
        arguments.IsValid=false;
    }
    else
    {
        if (isNaN(arguments.Value)) arguments.IsValid=true; else arguments.IsValid=false;
    }
    return arguments.IsValid;    
}

function CheckTextNotRequired(source, arguments)
{
    if (arguments.Value =="")
    {
        arguments.IsValid=true;
    }
    else
    {
        if (isNaN(arguments.Value)) arguments.IsValid=true; else arguments.IsValid=false;
    }
    return arguments.IsValid;    
}

function CheckNumericRequired(source, arguments)
{
    if (arguments.Value =="")
    {
        arguments.IsValid=false;
    }
    else
    {
        if (isNaN(arguments.Value.replace(/,/g, '').replace(/ /g, ''))) arguments.IsValid=false; else arguments.IsValid=true;
    }
    return arguments.IsValid;     
}

function CheckNumericNotRequired(source, arguments)
{
    if (arguments.Value =="")
    {
        arguments.IsValid=true;
    }
    else
    {
        if (isNaN(arguments.Value.replace(/,/g, '').replace(/ /g, ''))) arguments.IsValid=false; else arguments.IsValid=true;
    }
    return arguments.IsValid;    
}

function CheckPinRequired(source, arguments) {
    if (arguments.Value == "") {
        arguments.IsValid = false;
    }
    else {
        if (isNaN(arguments.Value.replace(/,/g, '').replace(/ /g, ''))) {
            arguments.IsValid = false;
        }
        else {
            if (arguments.Value.replace(/,/g, '').replace(/ /g, '').length == 6) {
                arguments.IsValid = true;
            }
            else {
                arguments.IsValid = false;
            }    
        }
    }
    return arguments.IsValid;
}

function CheckPinNotRequired(source, arguments) {
    if (arguments.Value == "") {
        arguments.IsValid = true;
    }
    else {
        if (isNaN(arguments.Value.replace(/,/g, '').replace(/ /g, ''))) {
            arguments.IsValid = false;
        }
        else {
            if (arguments.Value.replace(/,/g, '').replace(/ /g, '').length == 6) {
                arguments.IsValid = true;
            }
            else {
                arguments.IsValid = false;
            }
        }
    }
    return arguments.IsValid;
}


function CheckPhoneRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=false;
    }
    else
    {
        if (isNaN(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, ''))) {
            arguments.IsValid = false;
        } else {
            if (trim(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, '')).length < 11)
                arguments.IsValid = false;
            else
                arguments.IsValid = true;    
         }    
    }
    return arguments.IsValid;
}

function CheckPhoneNotRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=true;
    }
    else
    {
        if (isNaN(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, ''))) {
            arguments.IsValid = false;
        } else {
            if (trim(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, '')).length < 11)
                arguments.IsValid = false;
            else
                arguments.IsValid = true;  
        }    
    }
    return arguments.IsValid;
}


function CheckMobileRequired(source, arguments) {
    if (arguments.Value == "") {
        arguments.IsValid = false;
    }
    else {
        if (isNaN(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, ''))) {
            arguments.IsValid = false;
        } else {
            if (trim(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, '')).length < 10)
                arguments.IsValid = false;
            else
                arguments.IsValid = true;
        }
    }
    return arguments.IsValid;
}

function CheckMobileNotRequired(source, arguments) {
    if (arguments.Value == "") {
        arguments.IsValid = true;
    }
    else {
        if (isNaN(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, ''))) {
            arguments.IsValid = false;
        } else {
            if (trim(arguments.Value.replace(/-/g, '').replace(/,/g, '').replace(/ /g, '')).length < 10)
                arguments.IsValid = false;
            else
                arguments.IsValid = true;
        }
    }
    return arguments.IsValid;
}


function CheckEmailRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=false;
    }
    else
    {
        if (!isNaN(arguments.Value)) 
        {
            arguments.IsValid=false; 
        }
        else 
        {
            if (IsEmail(arguments.Value))
            {
                arguments.IsValid=true; 
            }    
            else 
            {
                arguments.IsValid=false;
            }
        }
    }    
    return arguments.IsValid;
}

function CheckEmailNotRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=true;
    }
    else
    {
        if (!isNaN(arguments.Value)) 
        {
            arguments.IsValid=false; 
        }
        else 
        {
            if (IsEmail(arguments.Value))
            {
                arguments.IsValid=true; 
            }    
            else 
            {
                arguments.IsValid=false;
            }
        }
    }    
    return arguments.IsValid;
}

function CheckDateRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=false;
    }
    else
    {
        if (IsDate(arguments.Value))
        {
            arguments.IsValid=true; 
        }    
        else 
        {
            arguments.IsValid=false;
        }
    }    
    return arguments.IsValid;
}

function CheckDateNotRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=true;
    }
    else
    {
        if (IsDate(arguments.Value))
        {
            arguments.IsValid=true; 
        }    
        else 
        {
            arguments.IsValid=false;
        }
    }    
    return arguments.IsValid;
}

function CheckValidNumberRequired(source, arguments)
{
    if (arguments.Value=="")
    {
        arguments.IsValid=false;
    }
    else if (arguments.Value=="0")
    {
        arguments.IsValid=false;
    }    
    else
    {
        if (!isNaN(arguments.Value)) 
        {
            arguments.IsValid=true; 
        }    
        else 
        {
            arguments.IsValid=false;
        }
    }    
    return arguments.IsValid;
}

function IsDate(objDate)
{
	var strDate = objDate;

  	var myDayStr = strDate.split('/')[2];
	var myMonthStr = strDate.split('/')[1];
	var myYearStr = strDate.split('/')[0];

	var myDate = new Date(myMonthStr+"/"+myDayStr+"/"+myYearStr);

	if ( eval(myDate.getDate()) != eval(myDayStr))
	{
  		return false;
	}
	else
	{
  		return true;
	}
}

function IsEmail(thisEmail)
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(thisEmail)) return (true); else return (false);
}

function isMaxLength(obj) {
    var mlength = obj.getAttribute ? parseInt(obj.getAttribute("MaxLength")) : "";
   
    if (obj.getAttribute && obj.value.length > mlength)
        obj.value = obj.value.substring(0, mlength)
}
