﻿// JScript File

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); 
		return true;
	}
	return false;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789. +-";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z- ]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
function isAlphabet1(elem, helperMsg){
	var alphaExp = /^[a-zA-Z '-]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z '.-]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function emailValidator(elem){
	var emailExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		return false;
	}
 
}


function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function PhonelengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Enter a valid telephone number – between 6 and 16 characters");
		elem.focus();
		return false;
	}
}
function caseurl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}


function Sqlcheck1(elem, helperMsg){
	var alphaExp = /^[<>-]*$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function Sqlcheck(sText)
{
   var ValidChars = "<>";
   var ValidChars1 = ">";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if ((ValidChars.indexOf(Char) == 0) || (ValidChars1.indexOf(Char) == 0)) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
function inputValid(type,e,nam)
{
    var key;
    var ctrlname = nam.id;
    if(document.all)
        key=e.keyCode;
    else if(document.getElementById)    
        key=e.which;
        //alert(key);
    if(key==8 || key==46 || key==13 ||key==32 || key == 16)    
        return true;  
    
    if(type=='message')
    {
        if(isDescription(trim(document.getElementById(ctrlname).value))==false)
        {
             alert(geterrmsg('e1'));
             var question=document.getElementById(ctrlname).value; 
             document.getElementById(ctrlname).value=question.replace(/[^-.,a-zA-Z0-9\s]/gi,'');            
        return false;
        }
    }        
    return true;
}
function isDescription(objValue)
{
       var alphaExp = /[^-_".',a-zA-Z0-9\s]/;
      if(objValue.match(alphaExp))
           return false;
      return true;
}
function trim(str)
{
  return RTrim(LTrim(str));
}

function RTrim(str)
{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    // We have a string with trailing blank(s)...

    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;


    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}

function LTrim(str)
{
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}

function ftrim(strval)
{
  var str2=strval.replace(/\S/g,"|");
  var i=str2.indexOf("|")-1;
  var j=str2.lastIndexOf("|")+1;
  var trimStr=strval.substring(0,i).replace(/\s/g,"") + strval.substring(i+1,j);
      trimStr = trimStr + strval.substring(j).replace(/\s/g,"");
  return trimStr;
}
   
