function checkAllZips(chk) {
  frm = document.__main;
  for (i = 0; i < frm.length; i++)   {
    if(frm[i].type == "checkbox")     {
      if(chk.checked == true && frm[i].value != 1) {
        frm[i].checked = true;
      }
      else if (chk.checked == false && frm[i].value != 1) {
        frm[i].checked = false;
      }
    }
  }
}

function selectAll(obj, selected) {
	for (i = 0; i < obj.length; i++) {
		obj.options[i].selected = selected;
	}
    document.__main.checkAll
}

function unCheck(obj) {
    if (obj) {
        obj.checked = false;
    }
}

function deleteSelected(obj) {
	for (i = 0; i < obj.length; i++) {
		if (obj.options[i].selected) {	
			obj.options[i] = null;
			i--;
		}
	}
}

function saveSICCode() {
  	window.opener.__main.sicCode.value = document.__main.sicCode.value;
  	window.close();
}

function openSICFinder() {
  	window.open('sicfinder.php', 'sicfinder', 'width=550,height=190,toolbar=no,menubar=no,left=395,top=200,screenX=200,screenY=250');
  	return false;
}

function checkMoveCursor(source, target, maxLength) {
  var noSpace = source.value;
  source.value = noSpace.replace( /\s/g, '' );
  
  if (source.value.length == maxLength) {
    target.focus();
  }
}

function getDocObj(elem,parent) {
    if (document.layers) {
        if (parent) {
            return "document."+parent+".document."+elem;
        }
        else {
            return "document."+elem;
        }
    }
    else if (document.all){
        return "document.all."+ elem;
    }
    else if (document.getElementById) {
        return "document.getElementById('"+elem+"')";
    }
}

function openPricingPage() {
  	window.open('pricing2.php', 'pricing', 'width=755,height=725,toolbar=no,menubar=no,left=295,top=50,screenX=200,screenY=250');
  	return false;
}

function openProductPage() {
  	window.open('products.php', 'products', 'width=755,height=300,toolbar=no,menubar=no,left=295,top=50,screenX=200,screenY=250');
  	return false;
}

function openBMIChart() {
  	window.open('bmichart.php', 'bmichart', 'width=600,height=650,toolbar=no,menubar=no,left=295,top=50,screenX=200,screenY=250');
  	return false;
}

function openAutoCoverageLevels() {
  	window.open('auto_coverage_levels.php', 'auto_coverage_levels', 'width=800,height=375,toolbar=no,menubar=no,left=295,top=50,screenX=200,screenY=250');
  	return false;
}

// Browser Display of Table Row
function bdtr() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("msie") != -1) {
		return 'block';
	} else {
		return 'table-row';
	}
}


// Ajax Routines
function GetXmlHttpObject() {

	var xmlHttp = null;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp = new XMLHttpRequest();
	}
	catch ( e ) {
	  // Internet Explorer
		try {
	    	xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
	  	catch ( e ) {
	    	xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
	    }
	}
	
	if ( xmlHttp == null ) {
	  	var serverScript = "logajaxerror.php";
		var x = new Image();
		x.src = serverScript;
	}
	
	return xmlHttp;
	
}

function checkZipcode(zipcode, selectedCity, blur, onload) {

	if (zipcode.length == 5 && blur == null) {
	
		getCities(zipcode, selectedCity, blur);
	
	} else if (blur || onload) {
		
		if (zipcode.length == 4) {
	
			getCities('0' + zipcode, selectedCity, blur);
		
		} else if (zipcode.length == 3) {
	
			getCities('00' + zipcode, selectedCity, blur);
	
		}
		
	} 

}

function getCities(zipcode, selectedCity, blur) { 
  
	xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp == null) {
	
		alert ("Your browser does not support AJAX!");
	  	return;
	  	
	}
	
	if (blur) { document.getElementById("zip").value = zipcode; }
	
	var url = "getcitiesfromzipcode.php" + "?zipcode=" + zipcode + '&selectedCity=' + selectedCity;
	
	xmlHttp.onreadystatechange = cityChanged;
	xmlHttp.open( "GET", url, true );
	xmlHttp.send( null );
	
}

function cityChanged()  {
 
	if (xmlHttp.readyState == 4) {
		
		var cities 		= eval('(' + xmlHttp.responseText + ')');
		var cityList	= document.getElementById("city");
		
		if (cities.results) {
			
			var selectIndex 			= 0;
			var suggestIndex 			= 0;
			var stateCode 				= document.getElementById("stateCode");
			var hiddenState 			= document.getElementById("hiddenState");
			cityList.options.length 	= 0;
			
			for (var i = 0; i < cities.results.length; i++) {
						
				cityList.options[cityList.options.length] = new Option(cities.results[i].cityName, cities.results[i].cityName);
				stateCode.value 	= cities.results[i].stateCode;
				hiddenState.value 	= cities.results[i].stateCode;
				
				if (cities.results[i].cityName == cities.results[i].selectedCity) {
				
					selectIndex = i;
				
				}  else if (cities.results[i].isPrimary == 1) {
				
					suggestIndex = i;
					
				}
				
			}
			
			cityList.options[cityList.options.length] = new Option('My city is not on this list.', -2);
			
			if (cities.results[0].selectedCity == -2) {
					
				selectIndex = cityList.options.length - 1;
				document.getElementById("customCityLabel").style.display = 'block';
				document.getElementById("customCityText").style.display = 'block';
				
			}
			else if (selectIndex == 0 && cityList.options.length == 2) { selectIndex = 0; }
			else if (selectIndex == 0 && cityList.options.length > 2) {
			
				selectIndex = suggestIndex;
			
			}
			
			cityList.selectedIndex = selectIndex;
			cityList.options[selectIndex].selected=true;
			
			document.getElementById("zipcodeLabel").className = ''
			
		} else {
		
			document.getElementById("zipcodeLabel").className = 'errorText2';
			cityList.options.length 	= 0;
		
		}
				
	}
	
}

function checkCitySelection(citySelection) {

	if (citySelection == -2) {
	
		document.getElementById( "customCityLabel" ).style.display = 'block';
		document.getElementById( "customCityText" ).style.display = 'block';
	
	} else {
	
		document.getElementById( "customCityLabel" ).style.display = 'none';
		document.getElementById( "customCityText" ).style.display = 'none';
	}

}

function switchView(obj, imageID) {

	if (obj != null && imageID != null) {
		
		if (obj.style.display == 'none') {
		 
	        obj.style.visibility='visible';
	        obj.style.display='';
	        
	        if (imageID) {
	        
	        	imageID.src = 'images/tree_open.gif';
	        	
	        }
	        
	    } else {
	    
	        obj.style.display = 'none';
	        
	        if (imageID) {
	        
	        	imageID.src = 'images/tree_closed.gif';
	        	
	        }
	        
	    }
		
	}
	
}

function getRADValue(radioObj) {
	
		if (!radioObj)
			return "";
		
		var radioLength = radioObj.length;
	
		if (radioLength == undefined) {
		
			if (radioObj.checked) {
			
				return radioObj.value;
				
			}
			else {
			
				return "blank";
				
			}
				
		}
	
		for (var i = 0; i < radioLength; i++) {
		
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
			
		}
		
		return "";
	
	}
	
	
	function clearRADValue(radioObj) {
	
		if (radioObj) {
		
			var radioLength = radioObj.length;
	
			if (radioLength == undefined) {
		
				radioObj.checked = false;
				
			}
	
			for (var i = 0; i < radioLength; i++) {
		
				radioObj[i].checked = false;
				
			}
		
		}
	
	}
	
	function switchRow(obj, which) {

		if (obj != null && which != null) {
			
			if (which) {
			
		        obj.style.visibility='visible';
		        obj.style.display='';
		        
		    } else {
		    
		        obj.style.display = 'none';
		        
		    }
			
		}
		
	}
