// psearch-local.js

// JavaScript Document

function searchObj() {
	
	this.networkG = '';
	this.networkState = '';
	this.fromStreet = '';
	this.fromCity = '';
	this.fromState = '';
	this.county = '';
	this.fromZipcode = '';
	this.proximity = '';
	this.selectedSpecialtyTypeId = '';
	this.selectedSpecialtyTypeIdSaved = '';
	this.specialty = '';
	this.selectedNetworkId = '';
	this.providerName = '';
	this.selectedLanguageId = '';
	this.selectedGender = ''; 
	this.priorState = '';
	//this.priorNetwork = '';
}

function QueryString() {
	this.product = '';
	this.stcd = '';
	this.nwk = '';
}

var pSearch = new searchObj;
var qString = new QueryString;

function criteriaDisplay(criteria) {
	var htmlStr = '';
	switch (criteria) {
		case "location" :
			if (pSearch.fromZipcode.length > 0) {
				htmlStr += "<li>"+pSearch.fromZipcode+"</li>";
				htmlStr += (pSearch.proximity.length > 0) ? "<li>"+pSearch.proximity+"</li>" : "";
			} else if (pSearch.fromState.length > 0) {
				if (pSearch.fromCity.length > 0) {
					htmlStr += (pSearch.fromStreet.length > 0) ? "<li>"+pSearch.fromStreet+"</li>\n" : "";
					htmlStr += "<li>"+pSearch.fromCity+"</li>\n";
				} else if (pSearch.county.length > 0) {
					htmlStr += "<li>"+pSearch.county+"</li>\n";
				}
				htmlStr += "<li>"+pSearch.fromState+"</li>";
				htmlStr += (pSearch.proximity.length > 0) ? "\n<li>"+pSearch.proximity+"</li>" : "";
			}
			$(".left #search-criteria #location ul").html(htmlStr);
			break;
		case "type" :
		    var string = '';
			if (isNumeric(pSearch.selectedSpecialtyTypeId)) htmlStr = "<li>"+pSearch.selectedSpecialtyTypeIdSaved+"</li>";
			else htmlStr = "<li>"+pSearch.selectedSpecialtyTypeId+"</li>";
 			if(pSearch.selectedSpecialtyId != null){
 				string = "\n<li>"+pSearch.selectedSpecialtyId+"</li>";
 			}
 			if(pSearch.specialty.length > 0){
 				string = "\n<li>"+pSearch.specialty+"</li>";
 			}
 			htmlStr += string;
			$(".left #search-criteria #type ul").html(htmlStr);
			break;
		case "options" :
			htmlStr = (pSearch.selectedNetworkId) ? "<li>"+pSearch.selectedNetworkId+"</li>" : "";
//			htmlStr = (pSearch.networkG) ? "<li>"+pSearch.networkG+"</li>" : "";
			htmlStr += (pSearch.providerName) ? "<li>"+pSearch.providerName+"</li>" : "";
			htmlStr += (pSearch.selectedLanguageId) ? "<li>"+pSearch.selectedLanguageId+"</li>" : "";
			htmlStr += (pSearch.selectedGender) ? "<li>"+pSearch.selectedGender+"</li>" : "";
			$(".left #search-criteria #options ul").html(htmlStr);
			break;
	}
}

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 parseCriteria(val) {
	val = (val != "No Preference" && val.match(/^(?:select)/i) == null && val.match(/^(?:other providers)/i) == null) ? val : '';
	return val;
}

function criteriaUpdate(type, val) {

	if (type != null) {
	
		switch(type) {
		
			case "provider" :
				pSearch.selectedSpecialtyTypeId = pSearch.selectedSpecialtyTypeIdSaved = parseCriteria(val);
				break;
			case "specialty" :
				pSearch.specialty = parseCriteria(val);
				break;
		}
	
	} else {
		$(":input").each(function(){
							
				$(":checked").each(function(){
					pSearch[this.name] = this.value;
				});
				
				$("select").each(function(){
					if (this.value.match(/\D/g) == null && this.value.length > 0) {
						pSearch[this.name] = parseCriteria(this.options[this.selectedIndex].text);
					} else {
						pSearch[this.name] = parseCriteria(this.value);
					}
				});
		
				$(":text").each(function(){
					pSearch[this.name] = this.value;
				});
		});
	}
}

function setState(myHash) {
	
	switch (myHash) {
		
		case "advanced":
			$("#search").addClass("hidden");
			$("#guided-location").removeClass("hidden");
			$("#help-search").addClass("hidden");
			$("#help-location").removeClass("hidden");
			$(".left h3").html("Search By");
			$(".left #upsell").addClass("hidden");
			$(".left #search-criteria").removeClass("hidden");
			$(".left #search-criteria #location").addClass("active");
			break;
		case "providerType":
			
				criteriaUpdate();
				$("#search").addClass("hidden");
				$(".left h3").html("Search By");
				$(".left #upsell").addClass("hidden");
				$(".left #search-criteria").removeClass("hidden");
				
				$("#guided-location").addClass("hidden");
				
				$("#guided-poptions").removeClass("hidden");
				$("#help-location").addClass("hidden");
				$("#help-search").addClass("hidden");
				$("#help-ptype").removeClass("hidden");
				if ($("#guided-poptions .col2").hasClass("hidden") == false){
					$("#help-poptions").removeClass("hidden");
				}
				$(".left #search-criteria #location").addClass("visited").removeClass("active");
				$(".left #search-criteria #type").addClass("active");
				criteriaDisplay("location");
				
			break;
		case "location":
			criteriaUpdate();
			$("#guided-location").removeClass("hidden");
			$("#guided-poptions").addClass("hidden");
			$("#help-location").removeClass("hidden");
			$("#help-poptions").addClass("hidden");
			$("#help-ptype").addClass("hidden");
			$(".left #search-criteria #type").addClass("visited").removeClass("active");
			//if 
			$(".left #search-criteria #options").addClass("visited").removeClass("active");
			$(".left #search-criteria #location").addClass("active").removeClass("visited");
			criteriaDisplay("options");
			break;
		default :
		/*
			$("#search").removeClass("hidden");
			$("#guided-location").addClass("hidden");
			$("#guided-poptions").addClass("hidden");
			$("#help-location").removeClass("hidden");
			$("#help-poptions").addClass("hidden");
			$("#help-ptype").addClass("hidden");
			$("#help-search").removeClass("hidden");
			$("#help-location").addClass("hidden");
			$(".left h3").html("Other Options");
			$(".left #upsell").removeClass("hidden");
			$(".left #search-criteria").addClass("hidden");
			$(".left #search-criteria #location").removeClass("active");
			*/
			break;
	}
	//currentState = myHash;
}

function parseHash(myAnchor) {
	
	myAnchor = (myAnchor.match('#')) ? myAnchor.split('#')[1] : myAnchor;
		
	//alert("parseHash: "+currentState+","+myAnchor);
	if (myAnchor == browserState) {
		return;
	}
	
	if (myAnchor.length > 0) {
		browserState = myAnchor;
		setState(myAnchor);
	} else {
		browserState = '';
		setState("default")
	}
	
}

function setStateDropdown() {
	var el = document.getElementById("fromState");
	if (el.selectedIndex == 0 || pSearch.fromState != el.options[el.selectedIndex].text)	// If a state is selected, don't override it
	{
		for (var i=0;i<el.options.length;i++) {
			if (el.options[i].value == pSearch.fromState) {
				el.options[i].selected = true;
				searchLocation.getCounties();
			}
		}
	}
}
/*
function setSelectedNetwork(state,network) {
	var today = new Date();
	if(network == '' || network == 'Select a Network') {
		htmlStr = "<strong>Default network:</strong> all networks selected</p> ";
		setCookie("defaultNetwork","",today,"/",this.domainName);
	} else {
		htmlStr = "<strong>Default network:</strong> "+network+"</p> ";
		stateAffiliate = "";
		var el = document.getElementById("selectedNetworkId");
		for (var i=0;i<el.options.length;i++) {
			if (el.options[i].text.substring(" - ID")) stateAffiliate = "ID";
			if (el.options[i].text.substring(" - OR")) stateAffiliate = "OR";
			if (el.options[i].text.substring(" - UT")) stateAffiliate = "UT";
			if (el.options[i].text.substring(" - WA")) stateAffiliate = "WA";
			if (el.options[i].text.substring(" - AS")) stateAffiliate = "AS";
			if (el.options[i].value == network &&
			    (stateAffiliate == state && el.options[i].text == network)) {
				el.options[i].selected = true;
			}
		}
		today.setFullYear(today.getFullYear() + 10);
		setCookie("defaultNetwork",state + "|" + network,today,"/",this.domainName);
	}
	$(".global-network p").each(function(){
		$(this).html(htmlStr);
	});
}
*/

function networkDisplayLocal() {
	var thISSelect = document.getElementById("selectedNetworkId");
	var stateAffiliate = "";
	for (var i=0;i<thISSelect.options.length;i++) {
			networkText = thISSelect.options[i].text;
			networkValue = thISSelect.options[i].value;
			if (networkValue.search(/ - ID/) > -1) {stateAffiliate = "ID";}
			if (networkValue.search(/ - OR/) > -1) {stateAffiliate = "OR";}
			if (networkValue.search(/ - UT/) > -1) {stateAffiliate = "UT";}
			if (networkValue.search(/ - WA/) > -1) {stateAffiliate = "WA";}
			if (networkValue.search(/ - AS/) > -1) {stateAffiliate = "AS";}
			if (stateAffiliate == pSearch.networkState && networkText == pSearch.selectedNetworkId) {
				document.getElementById("selectedNetworkId").options[i].selected = true;
			}
		}
}


function networkDisplay() {
//	setSelectedNetwork(pSearch.fromState,pSearch.networkG);
	var today = new Date();
	var htmlStr = "";
	var htmlChange = "&nbsp;<a title=\"Select a network\" class=\"thickbox\" href=\"/public/psearch/provider/scripts/networkselect.jsp?KeepThis=true&TB_iframe=true&height=348&width=658\">Change</a>"
	//pSearch.networkState = pSearch.fromState;
	if(pSearch.networkG == '' || pSearch.networkG == 'Select a Network' || pSearch.networkG == 'None') {
		htmlStr = "<strong>Default network:</strong> all networks selected</p> ";
		if(pSearch.networkG == 'None') setCookie("defaultNetwork","",today,"/",this.domainName);
	} else {
		htmlStr = "<strong>Default network:</strong> "+pSearch.networkG+"</p> ";
		var stateAffiliate = "";
		var thISSelect = document.getElementById("selectedNetworkId");
//		if (thISSelect.options[thISSelect.selectedIndex].value == "Any Network")
//		{
		for (var i=0;i<thISSelect.options.length;i++) {
			networkText = thISSelect.options[i].text;
			networkValue = thISSelect.options[i].value;
			if (networkValue.search(/ - ID/) > -1) {stateAffiliate = "ID";}
			if (networkValue.search(/ - OR/) > -1) {stateAffiliate = "OR";}
			if (networkValue.search(/ - UT/) > -1) {stateAffiliate = "UT";}
			if (networkValue.search(/ - WA/) > -1) {stateAffiliate = "WA";}
			if (networkValue.search(/ - AS/) > -1) {stateAffiliate = "AS";}
			if (stateAffiliate == pSearch.networkState && networkText == pSearch.networkG) {
				document.getElementById("selectedNetworkId").options[i].selected = true;
			}
		}
//		}
//		today.setFullYear(today.getFullYear() + 10);
//		setCookie("defaultNetwork",pSearch.networkState + "|" + pSearch.networkG,today,"/",this.domainName);
		setList("selectedNetworkId",pSearch.networkG);
//		pSearch.selectedNetworkId = pSearch.networkG;
		criteriaDisplay("options");
	}
//	if(qString.stcd == 'as' || getCookie('PLANCODE') == 'AS') {
//		htmlStr = "";
//		htmlChange = "";
//	}
	$(".global-network p").each(function(){
//		$(this).html(htmlStr + htmlChange);
		$(this).html(htmlStr);
	});
	pSearch.fromState = pSearch.fromState.substring(0,2).toUpperCase();
//	updateNetworksList(pSearch.fromState,pSearch.networkG);

}

function setBranding(myRegion) {
	myRegion = (myRegion == 'WA') ? 'WARBS' : myRegion;
	$("#logo").attr("class","");
	$("#logo").attr("class",myRegion);
	var date = new Date();
	var htmlStr = '<p>&copy; '+date.getFullYear()+' ';
	switch (myRegion) {
		
		case "AS" :
			htmlStr += 'Asuris Northwest Health. All rights reserved.</p>'
			$("#hero").hide();
			break;
		case "ID" :
			htmlStr += 'Regence BlueShield of Idaho. All rights reserved. Regence BlueShield of Idaho is an Independent Licensee of the Blue Cross and Blue Shield Association.</p>'
			break;
		case "OR" :
			htmlStr += 'Regence BlueCross BlueShield of Oregon. All rights reserved. Regence BlueCross BlueShield of Oregon is an Independent Licensee of the Blue Cross and Blue Shield Association.</p>'
			break;
		case "UT" :
			htmlStr += 'Regence BlueCross BlueShield of Utah. All rights reserved. Regence BlueCross BlueShield of Utah is an Independent Licensee of the Blue Cross and Blue Shield Association.</p>'
			break;
		case "WARBS" :
			htmlStr += 'Regence BlueShield. All rights reserved. Regence BlueShield serves much of the state of Washington and is an Independent Licensee of the Blue Cross and Blue Shield Association.</p>'
			break;
		default:
			htmlStr += 'The Regence Group.</p>'
			break;
	}
	
	$("#footer").html(htmlStr);
}


function networkUpdate(el) {
	$("#network-select select[name='network']").each(function(){
		$(this).addClass("hidden");
	});
	if (el.id != "network_all") {
		var tempState = el.id.split("_")
		$("#select_"+tempState[1]).removeClass("hidden");
	}
}

function parseQueryString(myQueryString) {
	
	var vars = myQueryString.replace("?","").split("&");
	var pairs = Array();
	for (var i=0;i<vars.length;i++) {
		pairs[i] = vars[i].split("=");
	}
	for (i in pairs) {
		var key = pairs[i][0]
		qString[key] = pairs[i][1].replace(/\+/g," ");
	}
}

function setStateAndNetwork(){

//	self.parent.pSearch.priorState = self.parent.pSearch.fromState;
//	self.parent.pSearch.priorNetwork = self.parent.pSearch.networkG;
	if ($("#network-select .radio:checked").attr("id") == "network_all"){
		self.parent.pSearch.networkG = "None";
//		self.parent.pSearch.fromState = "";
		self.parent.pSearch.networkState = "";
	} else {
		var suppliedNetwork = $("#network-select select:visible option:selected").text();
		if (suppliedNetwork != null) self.parent.pSearch.networkG = suppliedNetwork;
		if (self.parent.pSearch.networkG != "") self.parent.pSearch.selectedNetworkId = self.parent.pSearch.networkG;
		var tempState = $("#network-select :checked").attr("id");
		if (tempState != null)
		{
			tempState = tempState.toUpperCase().split("_");
			self.parent.pSearch.networkState = tempState[1];
		}
		if (self.parent.pSearch.fromState == "") self.parent.pSearch.fromState = self.parent.pSearch.networkState;
		var cookieValue = getCookie("defaultNetwork");
		if (cookieValue != null)
		{
			var cookieVals = cookieValue.split("|");
			if (self.parent.pSearch.fromState == "") self.parent.pSearch.fromState = cookieVals[0];
			if (self.parent.pSearch.networkG == "") self.parent.pSearch.networkG = cookieVals[1];
		}
	}
//	self.parent.setStateDropdown();
	self.parent.networkDisplay();
	//self.parent.tb_remove();
}
	
/* OLD CODE */

function updateOptions(el) {
	$("#guided-poptions .onlycol").removeClass("onlycol");
	$("#guided-poptions .col2").removeClass("hidden");
	$("#help-poptions").removeClass("hidden");
	//$("#"+el.id).next("label").after($("#specialty"));
	//$("#specialty").removeClass("hidden");
	//updateCriteria(el,"type");
}

$(document).ready(function(){
	
	// Add .text class (hack)
	$("input[type='text']").addClass("text");
	// Remove hero thickbox class (hack)\
	$("#hero a").each(function(){
		if ($(this).hasClass("thickbox")) {
			$(this).unbind("click");
			$(this).removeClass("thickbox");
		}
	});
	
	/*
	if( getCookie('lastsearch') != null ) {
		if( getCookie('lastsearch').substring(0,2) == "A:") {
			currentState='advanced';
		}
	}
	*/
	//Parse Hash onLoad, after anchor link clicks, and at interval to check for browser button
	browserState='';
	parseHash('advanced');
	hashInt = setInterval("parseHash(window.location.hash.toString())", 500);
	/*$("a[href^='#']").click(function(){
		clearInterval(hashInt);
		parseHash($(this).attr("href"));
		hashInt = setInterval("parseHash(window.location.hash.toString())", 500);
	});
	*/
	
	if (window.location.search.toString().length > 0 && window.top == window.self) {
		parseQueryString(window.location.search.toString())
		tb_show(qString.product,"provider/productpage.html?TB_iframe=true&height=218&width=568",null);
		setBranding(qString.stcd.toUpperCase());
//		setStateAndNetwork();
//		pSearch.selectedNetworkId = pSearch.networkG; // stcd/nwk passed in, so set network
		pSearch.networkState = qString.stcd.toUpperCase();
		pSearch.selectedNetworkId = pSearch.networkG = qString.nwk;
		setStateAndNetwork();
		networkDisplayLocal();
		if (qString.stcd.toUpperCase() == 'AS') {
			pSearch.fromState = 'WA';
			setCookie('PLANCODE','AS');
		} else {
			pSearch.fromState = qString.stcd.toUpperCase();
		}
		setStateDropdown();
		criteriaUpdate();
		criteriaDisplay("location");
		criteriaDisplay("options");
	} else if (window.top == window.self) {
		var passRegion
		passRegion = (getCookie('PLANCODE') != null) ? getCookie('PLANCODE') : 'default';
		setBranding(passRegion);
		setStateAndNetwork();
		setStateDropdown();
		if (pSearch.networkG != "") pSearch.selectedNetworkId = pSearch.networkG;
	}
	
	// Network Pop-up
	$("#network-select .radio").click(function(){
		networkUpdate(this);
	});
	
	// Network Popup Close
	$("#network_cancel").click(function(){
		self.parent.tb_remove();
	});
	$("#network_update").click(function(){
		
	setStateAndNetwork();
	self.parent.pSearch.selectedNetworkId = self.parent.pSearch.networkG; // network selector update, so set network
//	if ($("#network-select .radio:checked").attr("id") == "network_all"){
//			self.parent.pSearch.networkG = "None";
////			self.parent.pSearch.fromState = "";
//			self.parent.pSearch.networkState = "";
//		} else {
//			self.parent.pSearch.networkG = $("#network-select select:visible option:selected").text();
//			if (self.parent.pSearch.networkG == "") self.parent.pSearch.networkG = self.parent.pSearch.networkState;
//			var tempState = $("#network-select :checked").attr("id");
//			tempState = tempState.toUpperCase().split("_");
//			self.parent.pSearch.networkState = tempState[1];
//			if (self.parent.pSearch.fromState == "") self.parent.pSearch.fromState = self.parent.pSearch.networkState;
//		}
//		self.parent.setStateDropdown();
//		self.parent.networkDisplay();
		var today = new Date();
		if(self.parent.pSearch.networkG == 'None') setCookie("defaultNetwork","",today,"/",this.domainName);
		else
		{
			today.setFullYear(today.getFullYear() + 10);
			if (self.parent.pSearch.networkState == "" || self.parent.pSearch.networkG == "")
				alert("Value problem setting cookie: networkState = " + self.parent.pSearch.networkState + "; networkG = " + self.parent.pSearch.networkG);
			setCookie("defaultNetwork",self.parent.pSearch.networkState + "|" + self.parent.pSearch.networkG,today,"/",this.domainName);
		}
		self.parent.tb_remove();
	});
	
	/*
	// Provider Type
	$("#guided-poptions [name='selectedSpecialtyTypeId']").click(function(){
		updateOptions(this);
		poptionsVisible = true;
	});
	*/
	
	// Quick Search Remove/Replace
	$("#csearch").focus(function(){
		if (this.value=='example: Portland Clinic, Pediatrics, 97209'){
			this.value='';
		}
	}).blur(function(){
		if (this.value==''){
			this.value='example: Portland Clinic, Pediatrics, 97209';
		}
	});

	//pSearch.priorNetwork = pSearch.networkG;
	//setStateAndNetwork();

	// If Provider Type is populated from a previous search, make the
	// options, options help, and the two elements at the bottom of the
	// panel appear.
	if (pSearch.selectedSpecialtyTypeId)
	{
		$("#guided-poptions .col2").removeClass("hidden");
		$("#guided-poptions .global-network").removeClass("hidden");
		$("#guided-poptions .panel-nav").removeClass("hidden");
		poptionsVisible = true;
		pSearch.selectedSpecialtyTypeIdSaved = pSearch.selectedSpecialtyTypeId;
	}
	
	// Provider Type
	$("#guided-poptions .providerTypeRadio").click(function(){
		$("#guided-poptions .onlycol").removeClass("onlycol");
		$("#guided-poptions .col2").removeClass("hidden");
		$("#help-poptions").removeClass("hidden");
		$("#guided-poptions .global-network").removeClass("hidden");
		$("#guided-poptions .panel-nav").removeClass("hidden");
		//$("#"+this.id).next("label").after($("#specialty"));
		//$("#specialty").removeClass("hidden");
		$(".left #search-criteria #type").removeClass("active");
		$(".left #search-criteria #type").addClass("visited");
		$(".left #search-criteria #options").addClass("active");
		//document.getElementById("specialty").options[0].selected = true;
		var val = $("#"+this.id).next("label").html();
		criteriaUpdate("provider",val);
		criteriaUpdate("specialty","");
		//criteriaUpdate();
		criteriaDisplay("type");
	});
	
	if(qString.stcd == 'as' || getCookie('PLANCODE') == 'AS') {
		$("#guided-location .default-network").addClass("hidden");
		$("#guided-poptions .default-network").addClass("hidden");
	}
	
	// Populate Criteria
	$("#guided-poptions .col1 select").change(function(){
		var val = this.options[this.selectedIndex].text;
		criteriaUpdate("specialty", val);
		criteriaDisplay("type");
	});
	$("input[name='Search']").click(function(){
		criteriaUpdate();
		criteriaDisplay("options");
	});
	
	// Product Page Popup
	$("input#product_ok").click(function(){
		self.parent.tb_remove();
	});
	
	// Col 2 select changes
	$("#guided-poptions .col2 select").change(function() {
		criteriaUpdate();
		criteriaDisplay("options");
	});

	// WARBS Branding hotfix
	$('#network_wa').next().html('Regence BlueShield <span>(select counties of Washington)</span>');


 });

/*
 	// Advanced Search
	$(".advancedSearch").click(function(){
		$("#search").addClass("hidden");
		$("#guided-location").removeClass("hidden");
		$("#help-search").addClass("hidden");
		$("#help-location").removeClass("hidden");
		/*$(".left h3").html("Search By");
		$(".left #upsell").addClass("hidden");
		$(".left #search-criteria").removeClass("hidden");*//*
		return false;
	});
	$("#guided-next").click(function(){
		$("#guided-location").addClass("hidden");
		$("#guided-poptions").removeClass("hidden");
		$("#help-location").addClass("hidden");
		$("#help-ptype").removeClass("hidden");
		if (poptionsVisible) {
			$("help-poptions").removeClass("hidden");
		}
	});
	$("#provider-back").click(function(){
		$("#guided-location").removeClass("hidden");
		$("#guided-poptions").addClass("hidden");
		$("#help-location").removeClass("hidden");
		$("#help-ptype").addClass("hidden");
		$("#help-poptions").addClass("hidden");
	});
 
 	// Populate Criteria
	/*
	$("#guided-location input").blur(function(){
		updateCriteria(this,"location");
	});
	$("#guided-location select").blur(function(){
		updateCriteria(this,"location");
	});
	$("#guided-poptions .col1 select").blur(function(){
		updateCriteria(this,"specialty");
	});
	$("#guided-poptions .col2 input").blur(function(){
		updateCriteria(this,"options");
	});
	$("#guided-poptions .col2 select").blur(function(){
		updateCriteria(this,"options");
	});
	
 });*/
