// JavaScript Document

function showDiv(divName,lastDiv,moreDiv,lessDiv,hideLess) {
	document.getElementById(divName).className = "visibleDiv";
	document.getElementById(lastDiv).className = "upper_div";
	document.getElementById(moreDiv).className = "hiddenDiv";
	document.getElementById(lessDiv).className = "visibleDiv";
	document.getElementById(hideLess).className = "hiddenDiv";
}
function hideDiv(divName,lastDiv,moreDiv,lessDiv,showLess) {
	document.getElementById(divName).className = "hiddenDiv";
	document.getElementById(lastDiv).className = "visibleDiv";
	document.getElementById(moreDiv).className = "visibleDiv";
	document.getElementById(lessDiv).className = "hiddenDiv";
	document.getElementById(showLess).className = "visibleDiv";
}

function ShowMenu(num, menu, max) 
{ 
	//starting at one, loop through until the number 
	//chosen by the user 
	for(i = 1; i <= num; i++){ 
		//add number onto end of menu 
		var menu2 = menu + i; 
		//change visibility to block, or 'visible' 
		document.getElementById(menu2).style.display = 'block'; 
	} 
	//make a number one more than the number inputed 
	var num2 = num; num2++; 
	//hide it if the viewer selects a number lower 
	//this will hide every number between the selected 
	//number and the maximum 
	//ex. if 3 is selected, hide the <div> cells for 
	//4, 5, and 6 
	//loop until max is reached 
	while(num2 <= max){
		var menu3 = menu + num2; 
		//hide
		document.getElementById(menu3).style.display = 'none'; 
		//add one to loop 
		num2=num2+1; 
	} 
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// 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 = 10;

function isInteger(s)
{   var i;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}
function trim(s)
{   var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not a whitespace, append to returnString.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (c != " ") returnString += c;
	}
	return returnString;
}
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){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkZip(strZip){
	return (isInteger(strZip) && strZip.length == 5);
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}


function checkVals()
{
	//var Phone=document.forms['form1'].Phone;
	//var emailID=document.forms['form1'].Email;
	//var emailID=document.forms['form1'].Email;
	document.forms['form1'].fname.style.background='#ffffff';
	document.forms['form1'].lname.style.background='#ffffff';
	document.forms['form1'].email.style.background='#ffffff';
	document.forms['form1'].phone.style.background='#ffffff';
	document.forms['form1'].zip.style.background='#ffffff';
	var flag=0;
	var alert_focus="";
	var emailID=document.forms['form1'].email;
	var alert_text="Please fill in all of the required information.  We pledge to never sell or share your personal information.  Review our privacy policy for more info.\n\nThe following information is missing...\n\n";
	
	if(document.forms['form1'].fname.value == ""){
		var flag=1;
		var alert_text = alert_text+"Your First and Last Name\n";
		if(alert_focus == '') var alert_focus="fname";
		document.forms['form1'].fname.style.background='#cee8eb';
	}
	
	if(document.forms['form1'].lname.value == ""){
		var flag=1;
		if(document.forms['form1'].fname.value != "") var alert_text = alert_text+"Your First and Last Name\n";
		if(alert_focus == '') var alert_focus="lname";
		document.forms['form1'].lname.style.background='#cee8eb';
	}
	
	if (echeck(document.forms['form1'].email.value)==false && checkInternationalPhone(document.forms['form1'].phone.value)==false){
		var flag=1;
		var alert_text = alert_text+"A Valid Email Address OR A Valid Phone Number\n";
		if(alert_focus == '') var alert_focus="email";
		document.forms['form1'].email.style.background='#cee8eb';
		document.forms['form1'].phone.style.background='#cee8eb';
	}
	
	if (checkZip(document.forms['form1'].zip.value)==false){
		var flag=1;
		var alert_text = alert_text+"A Five Digit Zip Code\n";
		if(alert_focus == '') var alert_focus="zip";
		document.forms['form1'].zip.style.background='#cee8eb';
	}
	
	if(flag==1){
		alert(alert_text);
		document.getElementById(alert_focus).focus();
		return (false);
	}else{
		return (true);
	}

}

function checkQQ()
{
	//var Phone=document.forms['form1'].Phone;
	//var emailID=document.forms['form1'].Email;
	//var emailID=document.forms['form1'].Email;
	document.forms['quick_quote'].qq_fname.style.background='#ffffff';
	document.forms['quick_quote'].qq_lname.style.background='#ffffff';
	document.forms['quick_quote'].qq_email.style.background='#ffffff';
	document.forms['quick_quote'].qq_phone.style.background='#ffffff';
	document.forms['quick_quote'].qq_zip.style.background='#ffffff';
	var flag=0;
	var alert_focus="";
	var emailID=document.forms['quick_quote'].qq_email;
	var alert_text="Please fill in all of the required information.  We pledge to never sell or share your personal information.  Review our privacy policy for more info.\n\nThe following information is missing...\n\n";
	
	if(document.forms['quick_quote'].qq_fname.value == ""){
		var flag=1;
		var alert_text = alert_text+"Your First and Last Name\n";
		if(alert_focus == '') var alert_focus="qq_fname";
		document.forms['quick_quote'].qq_fname.style.background='#cee8eb';
	}
	
	if(document.forms['quick_quote'].qq_lname.value == ""){
		var flag=1;
		if(document.forms['quick_quote'].qq_fname.value != "") var alert_text = alert_text+"Your First and Last Name\n";
		if(alert_focus == '') var alert_focus="qq_lname";
		document.forms['quick_quote'].qq_lname.style.background='#cee8eb';
	}
	
	if (echeck(document.forms['quick_quote'].qq_email.value)==false && checkInternationalPhone(document.forms['quick_quote'].qq_phone.value)==false){
		var flag=1;
		var alert_text = alert_text+"A Valid Email Address OR A Valid Phone Number\n";
		if(alert_focus == '') var alert_focus="qq_email";
		document.forms['quick_quote'].qq_email.style.background='#cee8eb';
		document.forms['quick_quote'].qq_phone.style.background='#cee8eb';
	}
	
	if (checkZip(document.forms['quick_quote'].qq_zip.value)==false){
		var flag=1;
		var alert_text = alert_text+"A Five Digit Zip Code\n";
		if(alert_focus == '') var alert_focus="qq_zip";
		document.forms['quick_quote'].qq_zip.style.background='#cee8eb';
	}
	
	if(flag==1){
		alert(alert_text);
		document.getElementById(alert_focus).focus();
		return (false);
	}else{
		return (true);
	}

}
