////**********************************************************************************************************
////* 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  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;
   }

   /* Check for presence of two or more characters after last dot */
   var lastDotPos = emailAddress.lastIndexOf(".");
   if (lastDotPos + 3 >  emailAddress.length)
   {
      return false;
   }

   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()
{

myOption = -1;
for (i=0; i<document.IndexForm.rbintention.length; i++) {
if (document.IndexForm.rbintention[i].checked) {
myOption = i;
}
}
if (myOption == -1) {
alert("Please select a service type (Home or Business)");
return false;
}

strSec = document.IndexForm.rbintention[myOption].value;

/////// ///   Name   ////////////////////////
	if((strSec.toLowerCase()=="either_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;  It is required for Business Service');
			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;
		}
		
		
	}
	
		 var sFullName="";
		var sFirstName="";
		var sLastName="";
		var ss="";
		sFullName= document.IndexForm.name.value
		if(sFullName.length>0)
		{
			sFullName=sFullName.replace(/^\s*(.*)/, "$1");
			sFullName=sFullName.replace(/(.*?)\s*$/, "$1");
			arr1=sFullName.split(" ");
			count=1;
			sFirstName=arr1[0];	 
			while(count < arr1.length)
			{
				ss=ss + " " + arr1[count];
				count = count+1;
			}
			 sLastName=ss;
		}
		 	strRedirURL="targus.asp";
	 		strRedirURL=strRedirURL + "&yourname=" + document.IndexForm.name.value;
			strRedirURL=strRedirURL + "&ServicePhone=" + document.IndexForm.DSLPhoneAreaCode.value + document.IndexForm.DSLPhoneFirst.value +document.IndexForm.DSLPhoneLast.value;
			strRedirURL=strRedirURL + "&Email=" + document.IndexForm.shopper_email.value;
			strRedirURL=strRedirURL + "&ContactPhone=" + document.IndexForm.DSLPhoneAreaCode2.value + document.IndexForm.DSLPhoneFirst2.value +document.IndexForm.DSLPhoneLast2.value;
			strRedirURL=strRedirURL + "&RecentMove=";
			strRedirURL=strRedirURL + "&Type=" + strSec;
			strRedirURL=strRedirURL + "&fname=" +sFirstName;
	 		strRedirURL=strRedirURL + "&lname=" + sLastName;
			strRedirURL=strRedirURL + "&referring_partner=" +"dslbroker";
			strRedirURL=strRedirURL + "&targusflag=yes";
			strRedirURL=strRedirURL + "&autophoneflag=no";	
			strRedirURL=strRedirURL + "&Optin="+ document.IndexForm.OptIn.checked;


	var url1;
	url1 = "http://www.broadbandnational.com/winpopup.asp?Redirfile="+strRedirURL;
	location.href = url1;
	return false;
}
// 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;
}