$(document).ready(function(){
	//Basic Links
	$("#logo_left").live("click", function(){
		window.open("/index.php", "_self");
		return false;
	});
	$(".linkappointment").live("click", function(){
		window.open("/schedule", "_self");
		return false;
	});
	$(".linkgetstorenumber").live("click", function(){
		window.open("/find", "_self");
		return false;
	});
	$(".linkvisit").live("click", function(){
		window.open("/find", "_self");
		return false;
	});
	
	//Form Processing
	$("#sendform").live("click", function(){
		if(validateSchedule()){
			//Check some date constraints
			var submitteddate = $("#date").val();
			var submittedtime = $("#hour").val()+':'+$("#min").val();
			$.get("/ajax_services.php?do=checkcalendar&date="+submitteddate+"&time="+submittedtime, function(datedata){
				if(datedata == 1){
					//If we are good to go, submit it
					$.post("/ajax_services.php", $("#scheduleform").serialize(), function(){
						$("#schedulebox").hide();
						$("#scheduleboxfinal").show();
					});
				} else {
					//Something went wrong. Alert user based on problem.
					switch(datedata){
						case "SUN":
						alert("You have chosen a Sunday. Please select a different day of the week.");
						$("#date").focus();
						break;
						case "PAST":
						alert("You have chosen a date that is too close to today's date. Please select a different day.");
						$("#date").focus();
						break;
						case "BLOCKED":
						$.get("/ajax_services.php?do=blocked&date="+submitteddate, function(data){
							alert(data);
						});
						$("#date").focus();
						break;
						case "CLOSINGEARLY":
						//Store is closing early. Lets see if they picked a good time.
						$.get("/ajax_services.php?do=closingearly&date="+submitteddate+"&time="+submittedtime, function(data){
							if(data == 1){
								//Date is good, submit the form
								$.post("ajax_services.php", $("#scheduleform").serialize(), function(){
									$("#schedulebox").hide();
									$("#scheduleboxfinal").show();
								});
							} else {
								alert(data);
							}
						});
						$("#date").focus();
						break;
					} 
				}
			});
		}
		return false;
	});
	
	//FIND - Store Selection
	$(".newstore").live("click", function(){
		//Find our ID
		var DATAS = $(this).attr("id").split("_");
		//ID out button
		var BUTTONCLICK = $(this);
		$(this).html("SELECTING...");
		$.get("/ajax_services.php?do=setlocation&locationid="+DATAS[1], function(data){
			//Once the AJAX data comes back, clean it up.
			$(".newstore").html("Make This My Store").removeClass('store_selected').css('background-position', '0px 0px').css('color', '');
			BUTTONCLICK.addClass('store_selected').css('background-position', '0px -30px').css('color', '#fff');
			BUTTONCLICK.html("STORE SELECTED");
			//Show the user we are ready to roll.
			BUTTONCLICK.fadeTo(200, 0.3).delay(5).fadeTo(200, 1).delay(5).fadeTo(200, 0.3).delay(5).fadeTo(200, 1).delay(200).html('Your Store');
			$("#popup_storename2").html(data);
			if(preventLocationPopup != 'on'){
				setTimeout(function(){ popupSchedule_2011(); }, 3000);
			} else {
				preventLocationPopup = '';
			}
		});
		return false;
	});
	
	//TAB - Stay On
	if(tabon != ''){
		$("#"+tabon).css('background-position', 'bottom left');
	}
	
	
	//Font Sizes
	textSIZE = $.cookie('fontsize');
	$("#s").fontscale(".css_event_text, .css_medicare_text","down",{unit:"px",increment:2, useCookie:false,  adjustLeading:true});
	$("#m").fontscale(".css_event_text, .css_medicare_text","reset");
	$("#l").fontscale(".css_event_text, .css_medicare_text","up",{unit:"px",increment:2, useCookie:false, adjustLeading:true});
	//Rollover Sprite Changes
	$("#s").hover(function(){
		$(this).css('background-position', '0px -19px');
	}, function(){
		$(this).css('background-position', '0px 0px');
	});
	$("#m").hover(function(){
		$(this).css('background-position', '-19px -19px');
	}, function(){
		$(this).css('background-position', '-19px 0px');
	});
	$("#l").hover(function(){
		$(this).css('background-position', '-40px -19px');
	}, function(){
		$(this).css('background-position', '-40px 0px');
	});
	//Functions to remember a set size.
	$("#s").click(function(){
		if(textSIZE > -5){
			textSIZE--;
			$.cookie('fontsize', textSIZE);
		}
	});
	$("#m").click(function(){
		textSIZE = 0;
		$.cookie('fontsize', 0);
	});
	$("#l").click(function(){
		if(textSIZE < 3){
			textSIZE++;
			$.cookie('fontsize', textSIZE);
		}
	});
	
	//Check to see if the size is different than 0. If so, lets simulate some clicks.
	if(textSIZE != 0){
		if(textSIZE > 0){
			initalSIZE = textSIZE;
			textSIZEcounter = textSIZE;
			while(textSIZEcounter > 0){
				$("#l").trigger("click");
				textSIZEcounter--;
			}
			textSIZE = initalSIZE;
			$.cookie('fontsize', textSIZE);
		}
		if(textSIZE < 0){
			initalSIZE = textSIZE;
			textSIZEcounter = textSIZE;
			while(textSIZEcounter < 0){
				$("#s").trigger("click");
				textSIZEcounter++;
			}
			textSIZE = initalSIZE;
			$.cookie('fontsize', textSIZE);
		}
	}
	
	
	
	//LANGUAGE
	$("#languagelist").hover(function(){
		$(this).height(145);
	}, function(){
		$(this).height(25);
	});
	$("#Spanish").hover(function(){
		$(this).css('background-position', '0px -20px');
	}, function(){
		$(this).css('background-position', '0px 0px');
	}).click(function(){
		popUpWindow('/lang/espanol.html',600,465);
	});
	$("#English").hover(function(){
		$(this).css('background-position', '0px -60px');
	}, function(){
		$(this).css('background-position', '0px -40px');
	}).click(function(){
		popUpWindow('/lang/others.html',600,465);
	});
	$("#Italian").hover(function(){
		$(this).css('background-position', '0px -100px');
	}, function(){
		$(this).css('background-position', '0px -80px');
	}).click(function(){
		popUpWindow('/lang/italian.html',600,465);
	});
	$("#Russian").hover(function(){
		$(this).css('background-position', '0px -140px');
	}, function(){
		$(this).css('background-position', '0px -120px');
	}).click(function(){
		popUpWindow('/lang/russian.html',600,465);
	});
	$("#Vietnamese").hover(function(){
		$(this).css('background-position', '0px -180px');
	}, function(){
		$(this).css('background-position', '0px -160px');
	}).click(function(){
		popUpWindow('/lang/vietnamese.html',600,465);
	});
	$("#Chinese").hover(function(){
		$(this).css('background-position', '0px -220px');
	}, function(){
		$(this).css('background-position', '0px -200px');
	}).click(function(){
		popUpWindow('/lang/chinese.html',600,465);
	});
	
	//ZIPCODE
	$("#zipcodelink").live('click', function(){
		if(region == ''){
			popupZip_2011();
		} else {
			$.get('/ajax_services.php?do=generatemedicarelink', function(linkdata){
				if(linkdata == 0){
					//No link
				} else {
					$("#link_medicaresolutions").attr('href', linkdata);
				}
			});
			if(region == 'WPA'){
				$("#logo_left").css('background-position', 'top left');
			}
		}
	});
	
	//DIRECTIONS
	$(".directionslink").live("click", function(){
		MAPID = $(this).attr('href').replace('#', '').replace('http://www.highmarkdirect.com/find', '');
		$("#dataoutput").html(MAPID);
		$("#dialog-confirm-directions2").dialog({
			resizable: false,
			height:200,
			width:500,
			modal: true
		});
		return false;
	});
	$("#dialog-confirm-directions-submit").live('click', function(){
		var thisbox = $("#dialog-confirm-directions2");
		if($("#userinputaddress2").val() != ''){
			$.post('/ajax_services.php', {"do":"getaddress", "id": MAPID}, function(data){
				var startadd = $("#userinputaddress2").val();
				var endadd = data;
				window.open('http://maps.google.com/maps?saddr='+startadd+'&daddr='+endadd, '_blank');
				thisbox.dialog("close");
			});
			$(this).dialog("close");
		} else {
			alert('Please enter your current address to continue.');
			$("#userinputaddress2").focus();
		}
		return false;
	});
	$("#dialog-confirm-directions-map").live('click', function(){
		$.post('/ajax_services.php', {"do":"getmaplink", "id": MAPID}, function(data){
			window.open(data, '_blank');
			$("#dialog-confirm-directions2").dialog("close");
		});
		return false;
	});
	$("#dialog-confirm-directions-cancel").live('click', function(){
		$("#dialog-confirm-directions2").dialog("close");
		return false;
	});
	
	$('.ui-widget-overlay').live('click', function() {
		$('#dialog-confirm').dialog("close");
	});
	
	
	//TEXT ONLY
	$("#action_textonly").click(function(){
		updateTextState();
		return false;
	});
	if($.cookie('textonly')){ updateTextState(); }
	if(region != ''){
		$.get('/ajax_services.php?do=generatemedicarelink&rand='+randomNumber, function(linkdata){
			if(linkdata == 0){
				//No link
			} else {
				$(".link_medicaresolutions").attr('href', linkdata);
			}
		});
		if(region == 'WPA'){
			regionSwap();
		}
		if(region == 'CPA'){
			regionSwapCPA();
		}
	} else {
		regionSwapCPA();
	}
	
	//Link Click ZipCode Prompt
	$(".link_region").live('click', function(){
		//Lets grab the link
		targetedLink = $(this).attr('href');
		//User wants to click a link, do we have the region set?
		if(region == ''){
			//We do not have a region, lets prompt for the zip code.
			popupZipLink_2011();
		} else {
			//Good to go, send them along their way.
			window.open(targetedLink, '_blank');	
		}
		return false;
	});
});

var preventLocationPopup = '';
var textSIZE;
var randomNumber = Math.floor(Math.random()*10001);

//FUNCTIONS
function validateSchedule(){
	//var emailregex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
	var emailregex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var phoneregex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	if($("#firstname").val() == ''){
		alert('Please enter your first name.');
		$("#firstname").focus();
		return false;
	}
	if($("#lastname").val() == ''){
		alert('Please enter your last name');
		$("#lastname").focus();
		return false;
	}
	if($("#phone").val() == ''){
		alert('Please enter your phone number');
		$("#phone").focus();
		return false;
	}
	if(!phoneregex.test($("#phone").val())){
		alert('Please enter a valid phone number');
		$("#phone").focus();
		return false;
	}
	if($("#email").val() == ''){
		alert('Please enter an email address');
		$("#email").focus();
		return false;
	}
	if(!emailregex.test($("#email").val())){
		alert('Please enter a valid email address');
		$("#email").focus();
		return false;
	}
	if($("#date").val() == ''){
		alert('Please enter a date');
		$("#date").focus();
		return false;
	}
	if($("#location").val() == 0){
		alert('Please select a location');
		$("#location").focus();
		return false;
	}
	return true;
}

////////////////////////////////////////////////
////////// TEXT ONLY FLIP AND RESTYLE //////////
////////////////////////////////////////////////
function updateTextState() {
	//if (textOnlyState) { 
		$('img').css('visibility', 'hidden');	
		$("div").css('background', 'none').css('background-color', '#FFF');
		$("a").css('background', 'none').css('background-color', '#FFF');
		$("#header #menu_inner").css('background-color', '#ecedee');
		$("#interior_focus div, #interior_focus").css('background-color', '#ecedee');
		//$(".css_title_interior_white").css('color', '#000');
		//$("#bluebox #bluebox_content #bluebox_top .bluebox_item .bluebox_item_title .bluebox_item_text").css('background-color', '#1892c8');
		$("#bluebox").css('background-color', '#1892c8');
		$("#bluebox div").css('background-color', '#1892c8');
		$(".bluebox_line").css('background-color', '#4facd5');
		$("#home_focus").css('background-color', '#ecedee');
		$("#menu_left > a").css('background-color', '#cccccc');
		$("#menu_right > a").css('background-color', '#1892c8');
		$(".tout").css('border', '1px solid #dbdbdb').css('width', '230px');
		$(".tout_header").css('width', '210px');
		$("#footer, .css_menu_footer, #footer a").css('background-color', '#e5e5e5');
		$(".dottedline").css('background-color', '#9a9a9a');
		$("#s").html('S').unbind('mouseenter mouseleave');
		$("#m").html('M').unbind('mouseenter mouseleave');
		$("#l").html('L').unbind('mouseenter mouseleave');
		$("#Spanish").html('Espa&ntilde;ol');
		$("#English").html('Others');
		$("#Italian").html('Italino');
		$("#Russian").html('Русский язык');
		$("#Vietnamese").html('Tiếng Việt');
		$("#Chinese").html('中文');
		$("#action_textonly").html('Images On').unbind().click(function(){ revertTextState(); });
		$(".css_title_interior_white").removeAttr('style').attr('style', 'height:67px; line-height:67px; color:#000;');
		$.cookie('textonly', 'on');
	//}
}
function revertTextState(){
	$("#action_textonly").html('Reloading...');
	$.cookie('textonly', '');	
	location.reload();
}
/* Lang Additions */
// function to display the popup window	
function popUpWindow(URL,w,h) {
	var popUpX = (screen.width/2) - w/2;
	var popUpY = (screen.height/2)-h/2;
	var pos = "left="+popUpX+",top="+popUpY;
	window.open(URL,'lep','width='+w+',height='+h+',resizable=yes,menubar=no,toolbar=no,directoriesbar=no,locationbar=no,scrollbar=yes,statusbar=no,'+pos); 
	//prvWin.focus()
}

//OTHER FUNCTIONS

function regionSwap(){
	//Swaps for WPA
	$(".medicare_CPA").hide();
	$(".medicare_WPA").show();
	$("#logo_left").css('background-position', 'top left');
	$("#home_focus").css('background-image', 'url(/media/images/site_library/47_bg_homepage2.jpg)');
	$(".link_region").each(function(){
		$(this).attr('href', $(this).attr('href').replace('www.highmarkblueshield.com', 'www.highmarkbcbs.com'));
	});		
}
function regionSwapCPA(){
	//Swaps for WPA
	$(".medicare_WPA").hide();
	$(".medicare_CPA").show();
	$("#logo_left").css('background-position', 'bottom left');
	$("#home_focus").css('background-image', 'url(/media/images/site_library/48_bg_homepage_west.jpg)');
	$(".link_region").each(function(){
		$(this).attr('href', $(this).attr('href').replace('www.highmarkbcbs.com', 'www.highmarkblueshield.com'));
	});	
}

function popupSchedule(){
	$("#dialog-confirm-schedule").dialog({
		resizable: false,
		height:170,
		width:500,
		modal: true,
		buttons: {
			"Yes, Schedule an Appointment": function() {
				window.open('/schedule', '_self');
			},
			"No, Thank You": function() {
				schedulepop = 'yes';
				$(this).dialog("close");
			}
		}
	});	
}
function popupZip(){
	$("#dialog-confirm-zip").dialog({
		resizable: false,
		height:200,
		width:500,
		modal: true,
		closeOnEscape: false,
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
		buttons: {
			"Submit": function() {
				var thisbox = $(this);
				var zipcode = $("#userinputzip").val();
				if(zipcode.length === 5){
					$.post('/ajax_services.php', {"do":"submitzipcode", "zip": zipcode}, function(data){
						if(data != 0){
							//create an array and split our return variable.
							var returneddata = new Array();
							returneddata = data.split('||');
							//Show content based on region.
							region = returneddata[0];
							if(region == 'WPA'){
								regionSwap();
							}
							if(region == 'CPA'){
								$(".medicare_CPA").show();
							}
							//Now, check to see if we need to adjust this box to ask for zip code.
							if(returneddata[1].length > 1){
								//Looks like we have zip code data. Lets throw that selection into the box and change some actions.
								$("#countyform").html(returneddata[1]);
								thisbox.dialog("close");
								popupCounty();
							} else {
								$.get('/ajax_services.php?do=generatemedicarelink&rand='+randomNumber, function(linkdata){
									if(linkdata == 0){
										//No link
									} else {
										$("#link_medicaresolutions").attr('href', linkdata);
									}
								});
								thisbox.dialog("close");
							}
						} else {
							thisbox.dialog("close");
						}
					});
				} else {
					alert('Zip code must be 5 numbers');
					$("#userinputzip").focus();
				}
			},
			Cancel: function() {
				var thisbox = $(this);
				if(window.location.href == 'http://www.highmarkdirect.com/medicare'){
					window.open('/index.php', '_top');
				} else {
					thisbox.dialog("close");
				}
			}
		}
	});	
}
function popupZipLink(){
	$("#dialog-confirm-zip").dialog({
		resizable: false,
		height:200,
		width:500,
		modal: true,
		closeOnEscape: false,
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
		buttons: {
			"Submit": function() {
				var thisbox2 = $(this);
				var zipcode = $("#userinputzip").val();
				if(zipcode.length === 5){
					$.post('/ajax_services.php', {"do":"submitzipcode", "zip": zipcode}, function(data){
						if(data != 0){
							//create an array and split our return variable.
							var returneddata = new Array();
							returneddata = data.split('||');
							//Show content based on region.
							region = returneddata[0];
							if(region == 'WPA'){
								regionSwap();
								targetedLink = targetedLink.replace('www.highmarkblueshield.com', 'www.highmarkbcbs.com');
								thisbox2.dialog("close");
								window.open(targetedLink, '_blank');
							} else if(region == 'CPA'){
								regionSwapCPA();
								targetedLink = targetedLink.replace('www.highmarkbcbs.com', 'www.highmarkblueshield.com');
								thisbox2.dialog("close");
								window.open(targetedLink, '_blank');
							}
							return false;
						} else {
							thisbox2.dialog("close");
						}
					});
				} else {
					alert('Zip code must be 5 numbers');
					$("#userinputzip").focus();
				}
			},
			Cancel: function() {
				var thisbox2 = $(this);
				if(window.location.href == 'http://www.highmarkdirect.com/medicare'){
					window.open('/index.php', '_top');
				} else {
					thisbox2.dialog("close");
				}
			}
		}
	});	
}


function popupCounty(){
	$("#dialog-confirm-county").dialog({
		resizable: false,
		height:220,
		width:500,
		modal: true,
		closeOnEscape: false,
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
		buttons: {
			"Submit": function() {
				var thisbox = $(this);
				var county = $("#county").val();
				$.post('/ajax_services.php', {"do":"submitcounty", "county": county}, function(data){
					$.get('/ajax_services.php?do=generatemedicarelink&rand='+randomNumber, function(linkdata){
						if(linkdata == 0){
							//No link
						} else {
							$("#link_medicaresolutions").attr('href', linkdata);
						}
						thisbox.dialog("close");
					});
				});
			},
			Cancel: function() {
				window.open('/index.php', '_top');
			}
		}
	});	
}
















function popupZipLink_2011(){
	$("#dialog-confirm-zip3").dialog({
		resizable: false,
		height:200,
		width:500,
		modal: true,
		closeOnEscape: false,
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
	});	
}
$("#dialog-confirm-zip3-submit").live("click", function(){
	var thisbox2 = $("#dialog-confirm-zip3");
	var zipcode = $("#userinputzip3").val();
	if(zipcode.length === 5){
		$.post('/ajax_services.php', {"do":"submitzipcode", "zip": zipcode}, function(data){
			if(data != 0){
				//create an array and split our return variable.
				var returneddata = new Array();
				returneddata = data.split('||');
				//Show content based on region.
				region = returneddata[0];
				if(region == 'WPA'){
					regionSwap();
					targetedLink = targetedLink.replace('www.highmarkblueshield.com', 'www.highmarkbcbs.com');
					thisbox2.dialog("close");
					window.open(targetedLink, '_blank');
				} else if(region == 'CPA'){
					regionSwapCPA();
					targetedLink = targetedLink.replace('www.highmarkbcbs.com', 'www.highmarkblueshield.com');
					thisbox2.dialog("close");
					window.open(targetedLink, '_blank');
				}
				return false;
			} else {
				thisbox2.dialog("close");
			}
		});
	} else {
		alert('Zip code must be 5 numbers');
		$("#userinputzip").focus();
	}
});
$("#dialog-confirm-zip3-cancel").live("click", function(){
	var thisbox2 = $("#dialog-confirm-zip3");
	if(window.location.href == 'http://www.highmarkdirect.com/medicare'){
		window.open('/index.php', '_top');
	} else {
		thisbox2.dialog("close");
	}
});

function popupCounty_2011(){
	$("#dialog-confirm-county2").dialog({
		resizable: false,
		height:190,
		width:500,
		modal: true,
		closeOnEscape: false,
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); $("#county").attr('style', 'font-size:20px; line-height:30px;'); }
	});	
}
$("#dialog-confirm-county-submit").live("click", function(){
	var thisbox = $("#dialog-confirm-county2");
	var county = $("#county").val();
	$.post('/ajax_services.php', {"do":"submitcounty", "county": county}, function(data){
		$.get('/ajax_services.php?do=generatemedicarelink&rand='+randomNumber, function(linkdata){
			if(linkdata == 0){
				//No link
			} else {
				$("#link_medicaresolutions").attr('href', linkdata);
			}
			thisbox.dialog("close");
		});
	});
	return false;
});
$("#dialog-confirm-county-cancel").live("click", function(){
	window.open('/index.php', '_top');
	return false;
});
	







function popupZip_2011(){
	$("#dialog-confirm-zip2").dialog({
		resizable: false,
		height:186,
		width:500,
		modal: true,
		closeOnEscape: false,
		open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
		beforeClose: function(){ $("#dialog-confirm-zip-submit").unbind(); $("#dialog-confirm-zip-cancel").unbind(); }
	});
	
	$("#dialog-confirm-zip-submit").live('click', function(){
		var thisbox = $("#dialog-confirm-zip2");
		var zipcode = $("#userinputzip2").val();
		if(zipcode.length === 5){
			$.post('/ajax_services.php', {"do":"submitzipcode", "zip": zipcode}, function(data){
				if(data != 0){
					//create an array and split our return variable.
					var returneddata = new Array();
					returneddata = data.split('||');
					//Show content based on region.
					region = returneddata[0];
					if(region == 'WPA'){
						regionSwap();
					}
					if(region == 'CPA'){
						$(".medicare_CPA").show();
					}
					//Now, check to see if we need to adjust this box to ask for zip code.
					if(returneddata[1].length > 1){
						//Looks like we have zip code data. Lets throw that selection into the box and change some actions.
						$("#countyform2").html(returneddata[1]);
						thisbox.dialog("close");
						popupCounty_2011();
					} else {
						$.get('/ajax_services.php?do=generatemedicarelink&rand='+randomNumber, function(linkdata){
							if(linkdata == 0){
								//No link
							} else {
								$("#link_medicaresolutions").attr('href', linkdata);
							}
						});
						thisbox.dialog("close");
					}
				} else {
					thisbox.dialog("close");
				}
			});
		} else {
			alert('Zip code must be 5 numbers');
			$("#userinputzip2").focus();
		}
		return false;
	});
	
	$("#dialog-confirm-zip-cancel").live('click', function(){
		var thisbox = $("#dialog-confirm-zip2");
		if(window.location.href == 'http://www.highmarkdirect.com/medicare'){
			window.open('/index.php', '_top');
		} else {
			thisbox.dialog("close");
		}
		return false;
	});
}

function popupSchedule_2011(){
	$("#dialog-confirm-schedule2").dialog({
		resizable: false,
		height:170,
		width:500,
		modal: true
	});	
}
$("#dialog-confirm-schedule-submit").live("click", function(){
	window.open('/schedule', '_self');
});
$("#dialog-confirm-schedule-cancel").live("click", function(){
	var thisbox = $("#dialog-confirm-schedule2");
	schedulepop = 'yes';
	thisbox.dialog("close");
	return false;
});
