﻿// JavaScript Document

//code to check phone number
//*******************************************************************
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 8;


function stripCharsInBag(s, bag) {
	var i;
   var returnString = "";
   // Search through string's characters one by one.
   // If character is not in bag, append to returnString.
   for (i = 0; i < s.length; i++) {
     // Check that current character isn't whitespace.
     var c = s.charAt(i);
     if (bag.indexOf(c) == -1) returnString += c;
   }
   return returnString;
}

function checkInternationalPhone(strPhone){
   s=stripCharsInBag(strPhone,validWorldPhoneChars);
   return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
//******************************************************************************
//end of check phone



var decimalPointDelimiter = ".";


function isDigit (c) {
   return ((c >= "0") && (c <= "9"))
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isFloat (s) {
	var i;
   var seenDecimalPoint = false;

   if (isEmpty(s))
      if (isFloat.arguments.length == 1) return defaultEmptyOK;
      else return (isFloat.arguments[1] == true);

   if (s == decimalPointDelimiter) return false;

   // Search through string's characters one by one
   // until we find a non-numeric character.
   // When we do,  return false; if we don't,  return true.

   for (i = 0; i < s.length; i++) {
       // Check that current character is number.
       var c = s.charAt(i);

       if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
       else if (!isDigit(c)) return false;
   }

   // All characters are numbers.
   return true;
}



function isInteger (s)  {
	 var i;
    if (isEmpty(s)) {
       return false;
    }

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do,  return false; if we don't,  return true.

    for (i = 0; i < s.length; i++)  {
       // Check that current character is number.
       var c = s.charAt(i);

       if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function isRequired(field) {
  if (field.value==null || field.value=="")  {
  	return false;
  } else {
  	return true;
  }
}


function isEmail(str) {
   l = str.length;
   at=str.indexOf("@");
   lastdot = str.lastIndexOf(".");
   if (at < 1 || (lastdot > l-3) || (lastdot-at < 2))
      return false;
   return true;
}


function addslashes(str) {
   str=str.replace(/\'/g,'\\\'');
   str=str.replace(/\"/g,'\\"');
   str=str.replace(/\\/g,'\\\\');
   str=str.replace(/\0/g,'\\0');
   return str;
}

function stripslashes(str) {
   str=str.replace(/\\'/g,'\'');
   str=str.replace(/\\"/g,'"');
   str=str.replace(/\\\\/g,'\\');
   str=str.replace(/\\0/g,'\0');
   return str;
}


function HTMLEncode(str) {
   var s ="";
   if　(str.length　==　0)　return "";
   s = str.replace(/&/g,"&amp;");
   s = s.replace(/</g,"&lt;");
   s = s.replace(/>/g,"&gt;");
   s = s.replace(/ /g,"&nbsp;");
   s = s.replace(/\'/g,"&#39;");
   s = s.replace(/\"/g,"&quot;");
   //s = s.replace(/\n/g,"<br>");
   return    s;
}