
// --------------------- Email Verification Function ----------------------
//hemant use Email only field down to this page which includes alert box message
 function validEmail(emailid)
{
 
  var strEmail = emailid ;
  if(!validcharOnly(strEmail,"Email Address") )
  return false ; 
  if(strEmail.charAt(strEmail.length-1) == ".")
	return false ;
  for(i=0;i<strEmail.length-1;i++)
  {	
	if(strEmail.charAt(i) == " ")
		return false ;
  }
  pos = strEmail.search("@");
  if(pos != -1)
  {
	part = strEmail.slice(pos+1) ;
	flag = -1;
	pos = part.search("@") ;
	if(pos != -1)
		return false ;
	for(i=0;i<part.length;i++)
	{
		if(part.charAt(i)=='.')
		{	
			flag = 0 ;
			break;
		}
	}
	if(flag != -1)
		return(true) ;
	else
		return(false) ;
  }
  else
	return(false) ;
}

// --------------------- Number Verification Function ----------------------

function numberVerify(numbervalue)
{
	if(isNaN(numbervalue))
		return(false) ;	
	else
		return(true) ;
}

// --------------------- Phone No. Verification Function ----------------------

function phoneVerify(phonevalue)
{
	valid = "0123456789-()"
	phonevalue = allTrim(phonevalue) ;
	for(i=0;i<phonevalue.length;i++)
	{
		flag = 0 ;
		for(j=0;j<valid.length;j++)
		{
			if(phonevalue.charAt(i) == valid.charAt(j))
			{
				flag = 1 ;
				break ;
			}
		}
		if(flag == 0)
			return(false) ;
	}
	return(true) ; 
}

// --------------------- Date Verification Function ----------------------

function dateVerify(datevalue)
{
	var strDate ;
	var month,day,year ;
	var datearray ;
	strDate = datevalue ;
	if(strDate.search("/") != -1 )
		datearray = strDate.split("/") ;
	else if(strDate.search("-") != -1 )
		datearray = strDate.split("-") ;
	else
		return(false) ;
	if(( datearray.length < 3 ) || ( datearray.length > 3 ) )
		return(false);
	
	m = datearray[0] ;
        d = datearray[1] ;
        y = datearray[2] ;
	if(isNaN(m))
		return(false) ;
	month = parseInt(m,10) ;
	if(isNaN(d))
		return(false) ;
	day = parseInt(d,10) ;
	if(isNaN(y))
		return(false) ;
	year = parseInt(y,10) ;
	if((day >= 1 && day <= 31) && ( month == 1 ) ||
		(day >= 1 && day <= 31) && ( month == 3 ) ||
		(day >= 1 && day <= 31) && ( month == 5 ) ||
		(day >= 1 && day <= 31) && ( month == 7 ) ||
		(day >= 1 && day <= 31) && ( month == 8 ) ||
		(day >= 1 && day <= 31) && ( month == 10 ) ||
		(day >= 1 && day <= 31) && ( month == 12 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((day >= 1 && day <= 30) && ( month == 4 ) ||
		(day >= 1 && day <= 30) && ( month == 6 ) ||
		(day >= 1 && day <= 30) && ( month == 9 ) ||
		(day >= 1 && day <= 30) && ( month == 11 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;	
	}
	else if((month == 2) && (year%4 == 0) && (day >= 1 && day <= 29))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((month == 2) && (day >= 1 && day <= 28))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
		}
	else
		return(false) ;
}

// --------------------- Save message alert Function ----------------------

function saveRecordMsg()
{
	if(confirm("Do you wish to save this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Edit message alert Function ----------------------

function editRecordMsg()
{
	if(confirm("Do you wish to edit this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Delete message alert Function ----------------------

function deleteRecordMsg()
{
	if(confirm("Do you wish to delete this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Exit message alert Function ----------------------

function exitMsg()
{
	if(confirm("Do you wish to exit from this window"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Customized message alert Function ----------------------

function customizedMsg(msg)
{
	if(confirm(msg))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Email Only field Function ----------------------
//emailVerify
function  emailOnly(emailvalue,fieldname)
{
	if(validEmail(emailvalue))
		return(true);
	else
	{
		alert("Please enter valid email address in "+fieldname) ;
		return(false) ;
	}
}

// --------------------- Number Only field Function ----------------------

function  numberOnly(numbervalue,fieldname)
{
	if(numberVerify(numbervalue))
		return(true);
	else
	{
		alert("The "+fieldname+" can contain numeric values only") ;
		return(false) ;
	}
}

// --------------------- Phone Only field Function ----------------------

function  phoneOnly(phonevalue,fieldname)
{
	if(phoneVerify(phonevalue))
		return(true);
	else
	{
		alert("The "+fieldname+" can contain phone number values only") ;
		return(false) ;
	}
}

// --------------------- Date Only field Function ----------------------

function  dateOnly(datevalue,fieldname)
{
	if(dateVerify(datevalue))
		return(true);
	else
	{
		alert("The "+fieldname+" is Invalalid Date. Please Enter Valid Date mm/dd/yyyy.") ;
		return(false) ;
	}
}

// --------------------- Triming string from left sides Function ----------------------

function leftTrim(ltrimvalue)
{
	while(ltrimvalue.charAt(0)==' ')
		ltrimvalue = ltrimvalue.substr(1,ltrimvalue.length-1);
		
	return(ltrimvalue) ;
}

// --------------------- Triming string from right sides Function ----------------------

function rightTrim(rtrimvalue)
{
	while(rtrimvalue.charAt(rtrimvalue.length-1)==' ')
		rtrimvalue = rtrimvalue.substr(0,rtrimvalue.length-1);
	
	return(rtrimvalue) ;
}


 // --------------------- Triming string from both sides Function ----------------------

function allTrim(atrimvalue)
{
	return(rightTrim(leftTrim(atrimvalue))) ;
}


 // --------------------- Character validation Function ----------------------

function validcharOnly(charvalue,fieldname)
{
	invalid = "~/:<>`|^!';$%^&*()+|="
	charvalue = allTrim(charvalue) ;
	for(i=0;i<invalid.length;i++)
	{
		for(j=0;j<invalid.length;j++)
		{
			if(charvalue.charAt(j) ==  invalid.charAt(i))
			{
				alert("Only valid characters are allowed in "+fieldname) ;
				return(false);
			}
		}
	}
	for(i=0;i<charvalue.length;i++)
	{
		if(charvalue.charAt(i) == 66 || charvalue.charAt(i) == 39)
		{
			alert("Only valid characters are allowed in "+fieldname) ;
			return(false);
		}
	}
	return(true);
}


 // --------------------- Not Allowed Null Function ----------------------

function notallowedNull(charvalue,fieldname)
{
	if(allTrim(charvalue) == "")
	{
		alert("Please Enter "+fieldname+"") ;
		return(false);
	}
	return(true) ;
}


 // --------------------- Money Validation Function ----------------------

function moneyOnly(moneyvalue,fieldname)
{  
	valid = "0123456789."
	moneyvalue = allTrim(moneyvalue) ;
	for(i=0;i<moneyvalue.length;i++)
	{
		flag = 0 ;
		for(j=0;j<valid.length;j++)
		{
			if(moneyvalue.charAt(i) == valid.charAt(j))
			{
				flag = 1 ;
				break ;
			}
		}
		if(flag == 0)
		{
			alert("Only Money values are allowed in " + fieldname);
			return(false) ;
		}
	}
	return(true) ; 
}


 // --------------------- Dates Comparison Function ----------------------

function compareDates(datevalue1,datevalue2)
{
	
	var strDate = datevalue1;
	var month1,day1,year1,month2,day2,year2 ;
	pos1 = strDate.search("/") ;
	if(pos1 == 2 || pos1 == 1)
	{
		m = strDate.slice(0,pos1)
		mlater = strDate.slice(pos1+1)
		
		if(isNaN(m))
			return(false) ;
		month1 = parseInt(m,10) ;
		
		pos2 = mlater.search("/") ;
		
		if(pos2 == 2 || pos1 == 1)
		{		
			d = mlater.slice(0,pos2)
			dlater = mlater.slice(pos2+1)
			
			if(isNaN(d))
				return(false) ;
			day1 = parseInt(d,10) ;
			
			y = dlater.slice(0)
			if(isNaN(y))
				return(false) ;
			year1 = parseInt(y,10) ;
		}
	}
	var strDate = datevalue2;
	pos1 = strDate.search("/") ;
	if(pos1 == 2 || pos1 == 1)
	{
		m = strDate.slice(0,pos1)
		mlater = strDate.slice(pos1+1)
		
		if(isNaN(m))
			return(false) ;
		month2 = parseInt(m,10) ;
		
		pos2 = mlater.search("/") ;
		
		if(pos2 == 2 || pos1 == 1)
		{		
			d = mlater.slice(0,pos2)
			dlater = mlater.slice(pos2+1)
			
			if(isNaN(d))
				return(false) ;
			day2 = parseInt(d,10) ;
			
			y = dlater.slice(0)
			if(isNaN(y))
				return(false) ;
			year2 = parseInt(y,10) ;
		}
	}
	if(year1 > year2)
	{
		return(datevalue1) ;
	}
	else if(year2 > year1)
	{
		return(datevalue2) ;
	}
	else if(month1 > month2)
	{
		return(datevalue1) ;
	}
	else if(month2 > month1)
	{
		return(datevalue2) ;
	}
	else if(day1 >day2)
	{
		return(datevalue1) ;
	}
	else if(day2 > day1)
	{
		return(datevalue2) ;
	}
	else
	{
		return(datevalue1) ;
	}
}
 // --------------------- Date Range restriction Function ----------------------
function daterangeVerify(date1,date2,datediff)
{
	sStartDate = date1;
	sEndDate = date2;
        DateRange = datediff; 
	bStartDateSpecified = false;
	bEndDateSpecified = false;
	startDateObj = null;
	endDateObj = null;
	if ( sStartDate.length > 0 )
	{
		bStartDateSpecified = true;
		nStartYear = parseInt(sStartDate.substring(6, 10));
		sStartMonth = sStartDate.substring(0, 2);
		if ( sStartMonth.charAt(0) == '0' )
		{
			nStartMonth = parseInt(sStartMonth.substring(1, 2));
		}
		else
		{
			nStartMonth = parseInt(sStartMonth.substring(0, 2));
		}
		sStartDay = sStartDate.substring(3, 5);
		if ( sStartDay.charAt(0) == '0' )
		{
			nStartDay = parseInt(sStartDay.substring(1, 2));
		}
		else
		{
			nStartDay = parseInt(sStartDay.substring(0, 2));
		}
		startDateObj = new Date(nStartYear, nStartMonth-1, nStartDay);
	}
	if ( sEndDate.length > 0 )
	{
		bEndDateSpecified = true;
		nEndYear = parseInt(sEndDate.substring(6, 10));
		sEndMonth = sEndDate.substring(0, 2);
		if ( sEndMonth.charAt(0) == '0' )
		{
			nEndMonth = parseInt(sEndMonth.substring(1, 2));
		}
		else
		{
			nEndMonth = parseInt(sEndMonth.substring(0, 2));
		}
		sEndDay = sEndDate.substring(3, 5);
		if ( sEndDay.charAt(0) == '0' )
		{
			nEndDay = parseInt(sEndDay.substring(1, 2));
		}
		else
		{
			nEndDay = parseInt(sEndDay.substring(0, 2));
		}
		endDateObj = new Date(nEndYear, nEndMonth-1, nEndDay);
	}
	// Only need to check the date range if both the start and end date are specified
	if ( bStartDateSpecified && bEndDateSpecified )
	{
		nDayDiff = Math.round((endDateObj.getTime() - startDateObj.getTime())/86400000);
		if ( (nDayDiff >= 0) && (nDayDiff <= DateRange) )
		{
			return true;
		}
		else if ( nDayDiff > DateRange )
		{
			alert("An invalid date range was entered.  To Date cannot be more than " + DateRange + " days ahead of From Date.");
			return false;
		}
		
	}
	return true;  
}

//--------------------------- remove all spaces in string --------------
function spacenotallowed(str)
{
	for(i=0;i<str.length;i++)
	{
		if(str.charAt(i)==' ')
			str = str.substr(1,str.length-1);
	}	
	return(str) ;
}
//---Form Validation---------------//
function validate_form ()
{
	valid = true;
	
		 if (!notallowedNull(document.form1.fname.value,'First Name'))
			{
				document.form1.fname.select();
				return false;
			}
			invalid = "~/:<>|^!;$%^&*()+|=1234567890";
			charvalue = document.form1.fname.value ;
			for(i=0;i<invalid.length;i++)
			{
				for(j=0;j<invalid.length;j++)
				{
					if(charvalue.charAt(j) ==  invalid.charAt(i))
					{
						alert("Please Enter Only Alphabets. For eg. John") ;
						document.form1.fname.focus();  
						return(false);
					}
				}
			}
	
	
	
	
	if (document.form1.fname.value ==""){
	alert("Please enter your First Name.")
	document.form1.fname.focus();
	return false;
	}
	
	
		 if (!notallowedNull(document.form1.lname.value,'Last Name'))
			{
				document.form1.lname.select();
				return false;
			}
			invalid = "~/:<>|^!;$%^&*()+|=1234567890";
			charvalue = document.form1.lname.value ;
			for(i=0;i<invalid.length;i++)
			{
				for(j=0;j<invalid.length;j++)
				{
					if(charvalue.charAt(j) ==  invalid.charAt(i))
					{
						alert("Please Enter Only Alphabets. For eg. John") ;
						document.form1.lname.focus();  
						return(false);
					}
				}
			}
	
	
	
	
	if (document.form1.lname.value ==""){
	alert("Please enter your Last Name.")
	document.form1.lname.focus();
	return false;
	}
	
	
	if (EmailCheck(document.form1.email.value)==false)
		{
				alert("Please enter valid Email Address");
				document.form1.email.focus();
				return false;
		
        }
		
	if (document.form1.checkbox.checked==false && document.form1.checkbox2.checked==false && document.form1.checkbox3.checked==false && document.form1.checkbox4.checked==false && document.form1.checkbox5.checked==false && document.form1.checkbox6.checked==false){
	
	alert("Please select Type of Service");
	return false;
	}	
		
		
		
		
	if (document.form1.contact3.checked == true){
		if (document.form1.wpe.value ==""){
		alert("Please enter Work Phone, as you have entered it as a preferred mode of contact.");
		document.form1.wpe.focus();
		return false;		
		}
	}
	
	if (document.form1.contact2.checked == true){
		if (document.form1.cellphone.value ==""){
		alert("Please enter Cell Phone, as you have entered it as a preferred mode of contact.");
		document.form1.cellphone.focus();
		return false;		
		}
	}		
		
		
	
	 return true;
}

function EmailCheck(str)
		{		
	    	var AtTheRate= str.indexOf("@");
	    	var DotSap= str.lastIndexOf(".");
			if (AtTheRate==-1 || DotSap ==-1)
		    	{
		    		return false;
		    	}
	    	else
		    	{
		    		if( AtTheRate > DotSap )
			    		{				    		
				    		return false;
						}
				}
			}		
		

		function IsNotNumber1(textField,msg) {
			
			var digits = "0123456789";
			for (i=0; i<=textField.size; i++) {
				if (digits.indexOf(textField.value.charAt(i))== -1) {
				alert (msg);
				textField.focus();
				textField.select(); 
				return false;
				} 
			}
			return true;
		}
//----Business Enquiry-------//
var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

var contractsymbol='- ' 
var expandsymbol='+ ' //HTML for expand symbol.


if (document.getElementById){
document.write('<style type="text/css">')
document.write('.switchcontent{padding:0 5px;background-color: #F7F7F7;display:none;}')
document.write('</style>')
}

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec){
var thestate=(ec=="expand")? "block" : "none"
var inc=0
while (ccollect[inc]){
ccollect[inc].style.display=thestate
inc++
}
revivestatus()
}


function contractcontent(omit){
var inc=0
while (ccollect[inc]){
if (ccollect[inc].id!=omit)
ccollect[inc].style.display="none"
inc++
}
}

function expandcontent(curobj, cid){
var spantags=curobj.getElementsByTagName("SPAN")
var showstateobj=getElementbyClass(spantags, "showstate")
if (ccollect.length>0){
if (collapseprevious=="yes")
contractcontent(cid)
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
if (showstateobj.length>0){ //if "showstate" span exists in header
if (collapseprevious=="no")
showstateobj[0].innerHTML=(document.getElementById(cid).style.display=="block")? contractsymbol : expandsymbol
else
revivestatus()
}
}
}

function revivecontent(){
contractcontent("omitnothing")
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="block"
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="block")
statecollect[inc].innerHTML=contractsymbol
else
statecollect[inc].innerHTML=expandsymbol
inc++
}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" 
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate