// VARIABLE DECLARATIONS

// BOI, followed by one or more digits, followed by EOI.
var reInteger = /^\d+$/;

// U.S. phone numbers have 10 digits.
// They are formatted as 123 456 7890 or (123) 456-7890.
var digitsInUSPhoneNumber = 10;

var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now."
var iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now."

// whitespace characters as defined by this sample code
var whitespace = " \t\n\r";

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

// characters which are allowed in US phone numbers
var validUSPhoneChars = digits + phoneNumberDelimiters;

// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";

// non-digit characters which are allowed in 
// Social Security Numbers
var SSNDelimiters = "- ";

var digits = "0123456789";

// non-digit characters which are allowed in ZIP Codes
var ZIPCodeDelimiters = "-";

// our preferred delimiter for reformatting ZIP Codes
var ZIPCodeDelimeter = "-";

// characters which are allowed in Social Security Numbers
var validZIPCodeChars = digits + ZIPCodeDelimiters;

// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;

var defaultEmptyOK = false

// reformat (TARGETSTRING, STRING, INTEGER, STRING, INTEGER ... ) 
//
// Handy function for arbitrarily inserting formatting characters
// or delimiters of various kinds within TARGETSTRING.
//
// reformat takes one named argument, a string s, and any number
// of other arguments. The other arguments must be integers or
// strings. These other arguments specify how string s is to be
// reformatted and how and where other strings are to be inserted
// into it.
//
// reformat processes the other arguments in order one by one.
// * If the argument is an integer, reformat appends that number 
// of sequential characters from s to the resultString.
// * If the argument is a string, reformat appends the string
// to the resultString.
//
// NOTE: The first argument after TARGETSTRING must be a string.
// (It can be empty.) The second argument must be an integer.
// Thereafter, integers and strings must alternate. This is to
// provide backward compatibility to Navigator 2.0.2 JavaScript
// by avoiding use of the typeof operator.
//
// It is the caller's responsibility to make sure that we do not
// try to copy more characters from s than s.length.
//
// EXAMPLES:
//
// * To reformat a 10-digit U.S. phone number from "1234567890"
// to "(123) 456-7890" make this function call:
// reformat("1234567890", "(", 3, ") ", 3, "-", 4)
//
// * To reformat a 9-digit U.S. Social Security number from
// "123456789" to "123-45-6789" make this function call:
// reformat("123456789", "", 3, "-", 2, "-", 4)
//
// HINT:
//
// If you have a string which is already delimited in one way
// (example: a phone number delimited with spaces as "123 456 7890")
// and you want to delimit it in another way using function reformat,
// call function stripCharsNotInBag to remove the unwanted 
// characters, THEN call function reformat to delimit as desired.
//
// EXAMPLE:
//
// reformat (stripCharsNotInBag ("123 456 7890", digits),
// "(", 3, ") ", 3, "-", 4)

function reformat (s)
{
var arg;
var sPos = 0;
var resultString = "";

for (var i = 1; i < reformat.arguments.length; i++) {
arg = reformat.arguments[i];
if (i % 2 == 1) resultString += arg;
else {
resultString += s.substring(sPos, sPos + arg);
sPos += arg;
}
}
return resultString;
}

// Check whether string s is empty.

function isEmpty(s)
{ return ((s == null) || (s.length == 0))
}

// isZIPCode (STRING s [, BOOLEAN emptyOK])
// 
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code. Must be 5 or 9 digits only.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function. 
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isZIPCode (s)
{ if (isEmpty(s)) 
if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
else return (isZIPCode.arguments[1] == true);
return (isInteger(s) && 
((s.length == digitsInZIPCode1) ||
(s.length == digitsInZIPCode2)))
}

// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{
theField.focus()
theField.select()
//alert(s)
return s;
return false
}

//Akhilesh 26-9-2006
function warnInvalidNoFocus (s)
{
//theField.focus()
//theField.select()
alert(s)
return false
}

// Removes all characters which appear in string bag from string s.

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;
}

// takes ZIPString, a string of 5 or 9 digits;
// if 9 digits, inserts separator hyphen

function reformatZIPCode (ZIPString)
{ if (ZIPString.length == 5) return ZIPString;
else return (reformat (ZIPString, "", 5, "-", 4));
}

// checkZIPCode (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid ZIP code.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkZIPCode (theField, emptyOK,theFieldToFocus)
{
/*
	alert("function called");
	return false;
	*/
	if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	else
	{
		var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
		if (!isZIPCode(normalizedZIP, false)) 
			return warnInvalid (theFieldToFocus, iZIPCode);
		else 
		{ // if you don't want to insert a hyphen, comment next line out
			//theField.value = reformatZIPCode(normalizedZIP)
			return true;
		}
	}
}

// checkUSPhone (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid US Phone.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkUSPhone (theField, emptyOK, theFieldToFocus)
{ 

	if (checkUSPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
	if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	else
	{ 
		var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
		if (!isUSPhoneNumber(normalizedPhone, false)) 
			  return warnInvalid (theFieldToFocus, iUSPhone);
			//return warnInvalidNoFocus (iUSPhone);
			//return warnInvalid (theField, iUSPhone);
		else 
		{ // if you don't want to reformat as (123) 456-789, comment next line out
			//theField.value = reformatUSPhone(normalizedPhone);
			return true;
		}
	}
}


// isUSPhoneNumber (STRING s [, BOOLEAN emptyOK])
// 
// isUSPhoneNumber returns true if string s is a valid U.S. Phone
// Number. Must be 10 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isUSPhoneNumber (s)
{ if (isEmpty(s)) 
if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
else return (isUSPhoneNumber.arguments[1] == true);
return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

// takes USPhone, a string of 10 digits
// and reformats as (123) 456-789

function reformatUSPhone (USPhone)
{ return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}

// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{ return reLetterOrDigit.test(c)
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
// the default behavior which is specified globally by
// defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
// the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL: RESULT:
// isInteger ("5") true 
// isInteger ("") defaultEmptyOK
// isInteger ("-5") false
// isInteger ("", true) true
// isInteger ("", false) false
// isInteger ("5", false) true

function isInteger (s)

{ var i;

if (isEmpty(s)) 
if (isInteger.arguments.length == 1) return defaultEmptyOK;
else return (isInteger.arguments[1] == true);

return reInteger.test(s)
}


/* Vivek Mehta        02-02-2007			Function for the parsing of a string to check that it contains Valid Anchor Tag */

function parseAnchorTag(myString)
		
		{
		
		/* Regexp to check starts from <a */
		var re = /^<a/i;
		/* Regexp to check ends at ></a> */
		var re1= />[a-zA-Z0-9\/#.\\?\+\-%_&=:;$,'"^()\<\>\*|@\t\r\n\v\f(\s*)]+<\/a>$/gi;
		/* To Check href="#" syntax exists */
		var re2= /href="[a-zA-Z0-9\/#.\\?\+\-%_,&=:;\t\r\n\v\f$^()\*|@']+"/gi;
		/* To Check href='#' syntax exists */
		var re3= /href='[a-zA-Z0-9\/#.\\?\+\-%_,&=:;"\t\r\n\v\f$^()\*|@]+'/gi;
		/* To Check id="#" syntax exists */
		var re4= /id="[a-zA-Z0-9\/#.\\?\+\-%&=_,':;\t\r\n\v\f$^()\*|@]*"/gi;
		/* To Check id='#' syntax exists */
		var re5= /id='[a-zA-Z0-9\/#.\\?\+\-%&=_,;:"\t\r\n\v\f$^()\*|@]*'/gi;
		/* To Check Class="#" syntax exists */
		var re6= /Class="[a-zA-Z0-9\/#.\\?\+\-%_,&=;:\t\r\n\v\f$^()\*|@']*"/gi;
		/* To Check Class='#' syntax exists */
		var re7= /Class='[a-zA-Z0-9\/#.\\?\+\-%_,&=:;\t\r\n\v\f$^()\*|@"]*'/gi;
		
		var re8= /<a/gi;
		var re9= />[a-zA-Z0-9\/#.\\?\+\-%_&=:;$,'"^()\<\>\*|@\t\r\n\v\f ]+<\/a>/gi;
		
		var flag=true;
		
		
		arr= myString.match(re);
		if(arr==null)
		  return false;
		arr= myString.match(re8);
			
		if(arr.length>1)
		 return false;
		arr= myString.match(re1);
		if(arr==null)
		 return false;
		arr=myString.match(re9);
		if(arr.length>1)
		    return false;
		arr=myString.match(re2);
		arr2=myString.match(re3);
		if(arr!=null)
		 {
		   if(arr.length!=1)
		     flag=false;
		 }
		 
		 if(arr2!=null)
		 {
		   if(arr2.length!=1)
		     flag=false;
		 }
		 
		 if(!flag)
		   return false;
		arr=myString.match(re4);
		arr2=myString.match(re5);
		if(arr!=null)
		 {
		   if(arr.length>1)
		     flag=false;
		 }
		 
		 if(arr2!=null)
		 {
		   if(arr2.length>1)
		     flag=false;
		 }
		 
		 if(!flag)
		  return false;
		arr=myString.match(re6);
		arr2=myString.match(re7);
		if(arr!=null)
		 {
		   if(arr.length>1)
		     flag=false;
		 }
		 if(arr2!=null)
		 {
		   if(arr2.length>1)
		     flag=false;
		 }
		 if(!flag)
		   return false;
		return true;
		
		}

