////**********************************************************************************************************
////* Author		 :	S.M.P.Gokulnath
////* Start Date	 :	September ,9, 2004
////* Last Modified  :	 
////* Project Name	 :	BBN
////* File Name	     :	validation.js
////* LOC			 :	
////* Version No.	 :	1.0.0	
////* Developed By   :	S.M.P.Gokulnath
////* purpose / Functionality       : This File contains general common functions 
////    1. TimedPopup 		- To show a popup window for specified number of seconds
////    2. IsNumeric(sText) - Checks whether passed value is Numeric - returns true if all are numeric and false if any character is found in 					 
////						  passed parameter

var TimeOut = 1;    // Initial time to Close window after __ number of seconds?
var TimeRemain = 0;  // Remaining time to Close window after __ number of seconds?
var RefreshRate = 0; // Check to close window every __ number of seconds?
var ChildWin = null;
// function  TimedPopup takes 4 parameter
/// 1. url = physical files to be displayed in the popup window /  constructed html string content can also be passed that is to be displayed 
/// 2. w   = width of popup window
/// 3. h   = height of popup window 
/// 4.intsec = number of secs the window needs to be displayed 
//// Note : Though the window is displayed excatly for number of secs specified , the processing time for the page which shows results is
////        dynamic hence in body on unload of the calling page the opened popup window is made to close 
//Description     : displays popup window for specified number of secs
//Author          : S.M.P.Gokulnath
//Created Date    :  Sep -09- 2004   

function TimedPopup(url,w,h,intsec)
{ 
TimeOut = intsec
//var winl = (screen.width)/4;
//var wint = (screen.height)/4;
var winl = 200;
var wint = 200;
var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+winl+',';
      settings +='left='+wint+',';
      settings +='scrollbars=No,resizable=No,toolbar=no, location=no, directories=no';

 windowprops = settings; // May be adjusted according to your needs
 if (ChildWin)
     return false;
 ChildWin = window.open(url, "ChildWin", windowprops);
  if(parseInt(navigator.appVersion) >= 4){ChildWin.window.focus();}
 ResetTimer();
 if (TimeOut && RefreshRate)
    setTimeout("CheckClose();",RefreshRate * 1);
}
function ResetTimer()
{
 TimeRemain = TimeOut;
}
function CheckClose()
{
 TimeRemain -= (RefreshRate);
 if (TimeRemain > 0)
    {
    if (ChildWin && ChildWin.closed)
       {
 
	ChildWin = null;
       }
    else
    if (ChildWin)
       { 
	setTimeout("CheckClose();",RefreshRate * 1);
       }
    }
 else
 if (ChildWin)
    {
 
     if (ChildWin.closed)
	 ChildWin = null;
     else
	{
         ChildWin.close();
	 ChildWin = null;
        }
    }
}

// function  IsNumeric takes 1 parameter
/// 1. sText = value that is to be checked whether it contains all numeriuc characters
//Description     : returns true if valid numeric values are entered and false if wrong values are entered
//Author          : S.M.P.Gokulnath
//Created Date    :  Sep -09- 2004   
 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 isValidNumber(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 IsValidPhoneNo(sText)
   {
   		if (sText.length != 10)
		{
			return false
		}
   }
   // function  IsAlphabets takes 1 parameter
/// 1. sText = value that is to be checked whether it contains only Characters
//Description     : returns true if valid Characters are entered and false if wrong values are entered
//Author          : S.M.P.Gokulnath
//Created Date    :  Jan-11-2005 
 function IsAlphabets(sText)
{
   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
   var IsAlpha=true;
   var Char;
   sText=sText.replace(/^\s*(.*)/, "$1");
   sText=sText.replace(/(.*?)\s*$/, "$1");
	if (sText.length==0 )
	{
	IsAlpha = false;
	}
	else
	{
  	 for (i = 0; i < sText.length && IsAlpha == true; i++) 
      { 
    	  Char = sText.charAt(i); 
	      if (ValidChars.indexOf(Char) == -1) 
    	     {
        	 IsAlpha = false;
	         }
      }
	}
   return IsAlpha;   
  }
  
 // Function Name  : checkNumbersonly
// Description    : This function checks whether numbers alone is entered into service and contact phone numbers
//Author          : S.M.P.Gokulnath
//Created Date    :  Jan-11-2004
function checkNumbersonly(thob)
{
	if (IsNumeric(thob.value)==false)
	{
	alert('Please enter numeric value');
	thob.value="";
	thob.focus();
	}
}
 // allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
function isValidName(str)
{	
   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_ ";
   var Isok=true;
   var Char;
	str=str.replace(/^\s*(.*)/, "$1");
	str=str.replace(/(.*?)\s*$/, "$1");
	if (str.length==0 )
	{
	Isok = false;
	}
	else
	{
	   for (i = 0; i < str.length && Isok == true; i++) 
    	  { 
	      Char = str.charAt(i); 
    	  if (ValidChars.indexOf(Char) == -1) 
        	 {
	         Isok = false;
    	     }
	      }
	}
   return Isok;   
   }

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
function isValidEmailChar(str)
{	
   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@_.- ";
   var Isok=true;
   var Char;
	str=str.replace(/^\s*(.*)/, "$1");
	str=str.replace(/(.*?)\s*$/, "$1");
	if (str.length==0 )
	{
	Isok = false;
	}
	else
	{
	   for (i = 0; i < str.length && Isok == true; i++) 
    	  { 
	      Char = str.charAt(i); 
    	  if (ValidChars.indexOf(Char) == -1) 
        	 {
	         Isok = false;
    	     }
	      }
	}
   return Isok;   
   }

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
function isValidServiceAddress(str)
{	
   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/_., ";
   var Isok=true;
   var Char;
	str=str.replace(/^\s*(.*)/, "$1");
	str=str.replace(/(.*?)\s*$/, "$1");
	if (str.length==0 )
	{
	Isok = false;
	}
	else
	{
	   for (i = 0; i < str.length && Isok == true; i++) 
    	  { 
	      Char = str.charAt(i); 
    	  if (ValidChars.indexOf(Char) == -1) 
        	 {
	         Isok = false;
    	     }
	      }
	}
   return Isok;   
   }
// function  isValidEmail takes 1 parameter
/// 1. str = value that is to be checked for email address
//Description     : returns true if valid email values is enteres and false if wrong values are entered
function isValidEmail(emailAddress)
{
   /* Check for empty address or invalid characters */
   if (hasInvalidChar(emailAddress))//emailAddress == "" ||
   {
      return false;
   }
   /* Check for invalid characters */
   if (isValidEmailChar(emailAddress)==false)//emailAddress == "" ||
   {
      return false;
   }

   /* check for presence of the @ character */
   var atPos = emailAddress.indexOf("@", 1)
   if (atPos == -1)
   {
      return false;
   }

   /* Check that there are no more @ characters */
   if (emailAddress.indexOf("@", atPos + 1) > -1)
   {
      return false;
   }

   /* Check for the presence of a dot somewhere after @ */
   var dotPos = emailAddress.indexOf(".", atPos + 1);
   if (dotPos == -1)
   {
      return false;
   }

   /* Check for presence of two or more characters after last dot */
   var lastDotPos = emailAddress.lastIndexOf(".");
   if (lastDotPos + 3 >  emailAddress.length)
   {
      return false;
   }

   return true;
}

// function  isValidEmailAddress takes 1 parameter
/// 1. str = value that is to be checked for email address
//Description     : returns true if valid email values is enteres and false if wrong values are entered
function isValidEmailAddress(emailAddress)
{
   /* Check for empty address or invalid characters */
   if (hasInvalidChar(emailAddress))//emailAddress == "" ||
   {
      return false;
   }
   /* Check for invalid characters */
   if (isValidEmailChar(emailAddress)==false)//emailAddress == "" ||
   {
      return false;
   }

   /* check for presence of the @ character */
   var atPos = emailAddress.indexOf("@", 1)
   if (atPos == -1)
   {
      return false;
   }

   /* Check that there are no more @ characters */
   if (emailAddress.indexOf("@", atPos + 1) > -1)
   {
      return false;
   }

   /* Check for the presence of a dot somewhere after @ */
   var dotPos = emailAddress.indexOf(".", atPos + 1);
   if (dotPos == -1)
   {
      return false;
   }

      
    /* Anupam 14th July 2006 */
   /* Check for the presence of blank space in the email address */
	var  char_array_emailaddr = emailAddress.split('');
   for (i=0;i<char_array_emailaddr.length-1;i++)
   {
   		if (char_array_emailaddr[i] ==' ')
   		{
		
      return false;
   }
	}
   
   /* Anupam 14th July 2006 */
   /* Check for presence of sequence of dot */
   var strBtwDot = emailAddress.split('.');
   for (i=0;i<strBtwDot.length-1;i++)
   {
   		if (strBtwDot[i] =='')
   		{
      		return false;
   		}
	}
	
	/* Anupam 14th July 2006 */
   /* Check for the presence of blank space in the email address */
	var domainpart =emailAddress.split('@');
	var splitdomain = domainpart[1].split('.');
	if (splitdomain.length==3)
	{
		lastsegment=new Array("ac","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","as","at","au","aw","az","ax","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","er","es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tp","tr","tt","tv","tw","tz","ua","ug","uk","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","yu","za","zm","zw");
		var Flag = true;
		for(i=0;i<lastsegment.length;i++)
		{
			//alert(lastsegment[i]);
			if (lastsegment[i]==splitdomain[2])
			{
				Flag=true;
			}
		}
		return Flag;
	}
	else if(splitdomain.length==2)
	{
		lastsegment=new Array("com","net","org","edu","int","mil","gov","arpa","biz","aero","name","coop","info","pro","museum");
		var Flag = true;
		for(i=0;i<lastsegment.length;i++)
		{
			//alert(lastsegment[i]);
			if (lastsegment[i]==splitdomain[1])
			{
				Flag=true;
			}
		}
		return Flag;
	}
	else
	{
		return true;
	}
   return true;
}

/*
   Return true if the given email address has an invalid character
   in it, else return false.
*/
function hasInvalidChar(emailAddress)
{
   var invalidChars = "/;:,"; // this list is not complete
   for (var k = 0; k < invalidChars.length; k++)
   {
      var ch = invalidChars.charAt(k);
      if (emailAddress.indexOf(ch) > -1)
      {
         return true;
      }
   }
   return false;
}

	



// function  checkHomePageInputValues  
///  Note : This Function is called from two pages
///           1. homepage.asp
///           2. input.asp
///      since the control name and validation are same
/// this is done to avoid code repetation and for ease of maintanence      
// Description    : This function checks whether the form values entered are proper 
//					and gives appropriate error message if any value is found invalid or left blank
//                  The function returns true if all values are found correct and false if any value is found invalid or left blank
//Author          : S.M.P.Gokulnath
//Created Date    :  Sep -09- 2004   
function checkHomePageInputValues(strSec)
{
 
	/////// ///   Name   ////////////////////////
	if((strSec.toLowerCase()=="home"))
	{
		if(document.IndexForm.name.value!='')
		{
				if(isValidName(document.IndexForm.name.value)==false)
				{
					alert('Please do not enter special characters with your name');
					document.IndexForm.name.focus();
					return false;
				}
		}
			
	}
	else
	{
		if(document.IndexForm.name.value=='')
		{
			alert('Please enter your Full Name ');
			document.IndexForm.name.focus();
			return false;
		}
				  
		if(isValidName(document.IndexForm.name.value)==false)
		{
			alert('Please do not enter special characters with your name');
			document.IndexForm.name.focus();
			return false;
		}	
	} 

	/// ///   Service Phone    ////////////////////////
	if (document.IndexForm.DSLPhoneAreaCode.value=='')  
	{
		alert('Please enter your Service telephone number');
		document.IndexForm.DSLPhoneAreaCode.focus();
		return false;
	}
	var num1,newTerm1
	if (parseInt(document.IndexForm.DSLPhoneAreaCode.value) != document.IndexForm.DSLPhoneAreaCode.value) 
	{
            num1 = document.IndexForm.DSLPhoneAreaCode.value;
            while (num1.charAt(0) == "0") 
	    {
	    	newTerm1 = num1.substring(1, num1.length);
		    num1 = newTerm1;
	    }
	    if (parseInt(num1) != num1)
	    {
		alert('Please enter a Service telephone number, numbers only');
		document.IndexForm.DSLPhoneAreaCode.focus();
		return false;
            }
	}

	if (document.IndexForm.DSLPhoneAreaCode.value.length < 3 || document.IndexForm.DSLPhoneAreaCode.value.length > 3 )  
	{
		alert('Please enter your Service telephone Area Code  3 digits number Eg(123) ');
		document.IndexForm.DSLPhoneAreaCode.focus();
		return false;
	}
	
	if  (document.IndexForm.DSLPhoneFirst.value=='')  
	{
		alert('Please enter your Service telephone number');
		document.IndexForm.DSLPhoneFirst.focus();
		return false;
	}
	var num2,newTerm2
	if (parseInt(document.IndexForm.DSLPhoneFirst.value) != document.IndexForm.DSLPhoneFirst.value)
	{
            num2 = document.IndexForm.DSLPhoneFirst.value;
            while (num2.charAt(0) == "0") 
	    {
	    	newTerm2 = num2.substring(1, num2.length);
		num2 = newTerm2;
	    }
	    if (parseInt(num2) != num2) 
	    {
		alert('Please enter a Service telephone number, numbers only');
		document.IndexForm.DSLPhoneFirst.focus();
		return false;
	    }
	}

	if (document.IndexForm.DSLPhoneFirst.value.length < 3 || document.IndexForm.DSLPhoneFirst.value.length > 3 )
	{
		alert('Please enter your Service telephone first 3 digits number Eg(123) ');
		document.IndexForm.DSLPhoneFirst.focus();
		return false;
	}
	
	if (document.IndexForm.DSLPhoneLast.value=='') 
	{
		alert('Please enter your Service telephone number');
		document.IndexForm.DSLPhoneLast.focus();
		return false;
	}
 
 	var num3,newTerm3
	if (parseInt(document.IndexForm.DSLPhoneLast.value) != document.IndexForm.DSLPhoneLast.value) 
	{
            num3 = document.IndexForm.DSLPhoneLast.value;
            while (num3.charAt(0) == "0") 
	    {
	    	newTerm3 = num3.substring(1, num3.length);
 	    	num3 = newTerm3;
	    }
	    if (parseInt(num3) != num3) 
	    {
		alert('Please enter a Service telephone number, numbers only');
		document.IndexForm.DSLPhoneLast.focus();
		return false;
	    }									
	}
			
	if (document.IndexForm.DSLPhoneLast.value.length < 4 || document.IndexForm.DSLPhoneLast.value.length > 4 )
	{
		alert('Please enter your Service telephone last 4 digits number Eg(1234) ');
		document.IndexForm.DSLPhoneLast.focus();
		return false;
	}
 
	/// ///   Mail Check Code ////////////////////////
	if (document.IndexForm.shopper_email.value=='' ||  (! isValidEmailAddress(document.IndexForm.shopper_email.value) ) ) {
		alert('Please enter a valid e-mail address so you will receive confirmation of your registration.  E-Mail addresses must be in a valid e-mail format (e.g. yourname@domain.com)');
		document.IndexForm.shopper_email.focus();
		return false;
	}
	 
	if((strSec.toLowerCase()=="home"))
	{
	}
	else
	{
		/// ///   Contact Phone  ////////////////////////
		if(document.IndexForm.DSLPhoneAreaCode2.value=='') 
		{
			alert('Please enter your Contact telephone number');
			document.IndexForm.DSLPhoneAreaCode2.focus();
			return false;
		}
		
		var num1,newTerm1
		if (parseInt(document.IndexForm.DSLPhoneAreaCode2.value) != document.IndexForm.DSLPhoneAreaCode2.value) 
		{
            num1 = document.IndexForm.DSLPhoneAreaCode2.value;
            while (num1.charAt(0) == "0") 
		    {
		    	newTerm1 = num1.substring(1, num1.length);
			    num1 = newTerm1;
	    	}
		    if (parseInt(num1) != num1)
		    {
				alert('Please enter a Contact telephone number, numbers only');
				document.IndexForm.DSLPhoneAreaCode2.focus();
				return false;
	       }
		}

		if(document.IndexForm.DSLPhoneAreaCode2.value.length < 3 || document.IndexForm.DSLPhoneAreaCode2.value.length > 3 ) 
		{
		alert('Please enter your Contact telephone Area Code  3 digits number Eg(123) ');
			document.IndexForm.DSLPhoneAreaCode2.focus();
			return false;
		}


		if (document.IndexForm.DSLPhoneFirst2.value=='') 
		{
			alert('Please enter your Contact telephone number');
			document.IndexForm.DSLPhoneFirst2.focus();
			return false;
		}

		var num2,newTerm2
		if (parseInt(document.IndexForm.DSLPhoneFirst2.value) != document.IndexForm.DSLPhoneFirst2.value)
		{
            num2 = document.IndexForm.DSLPhoneFirst2.value;
            while (num2.charAt(0) == "0") 
		    {
		    	newTerm2 = num2.substring(1, num2.length);
				num2 = newTerm2;
		    }
		    if (parseInt(num2) != num2) 
		    {
			alert('Please enter a Contact  telephone number, numbers only');
			document.IndexForm.DSLPhoneFirst2.focus();
			return false;
		    }
		}

		if (document.IndexForm.DSLPhoneFirst2.value.length < 3 || document.IndexForm.DSLPhoneFirst2.value.length > 3 ) 
		{
		alert('Please enter your Contact telephone First 3 digits number Eg(123) ');
			document.IndexForm.DSLPhoneFirst2.focus();
			return false;
		}
		
		if (document.IndexForm.DSLPhoneLast2.value=='')  
		{
			alert('Please enter your Contact telephone number');
			document.IndexForm.DSLPhoneLast2.focus();
			return false;
		}

		var num3,newTerm3
		if (parseInt(document.IndexForm.DSLPhoneLast2.value) != document.IndexForm.DSLPhoneLast2.value) 
		{
	        num3 = document.IndexForm.DSLPhoneLast2.value;
	        while (num3.charAt(0) == "0") 
		    {
		    	newTerm3 = num3.substring(1, num3.length);
	 	    	num3 = newTerm3;
		    }
		    if (parseInt(num3) != num3) 
		    {
			alert('Please enter a Contact telephone number, numbers only');
			document.IndexForm.DSLPhoneLast2.focus();
			return false;
		    }									
		}
		if (document.IndexForm.DSLPhoneLast2.value.length < 4 || document.IndexForm.DSLPhoneLast2.value.length > 4 )  
		{
		alert('Please enter your Contact telephone Last 4 digits number Eg(1234) ');
			document.IndexForm.DSLPhoneLast2.focus();
			return false;
		}
		
		
	}
	return true;
}
// This is part of code for auto tab in phone number fields
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}

//----------------------------- SMPG BBN LONG --------------------------------

function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function



 function IsNumbers(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;   
}
