var State = new Array();
var lastUS = 49;
var cookiesOK = false;


function cookiesEnabled () {
    var TEST_COOKIE = 'test_cookie';
    jQuery.cookie( TEST_COOKIE, true );
    if ( jQuery.cookie ( TEST_COOKIE ) )
    {
      jQuery.cookie( TEST_COOKIE, null );
      cookiesOK = true;
    }
    else
    {
      alert( 'Cookies are not enabled. You will be unable to make appointments online unless you enable cookies. Please see our Privacy Policy for more information.' );
      cookiesOK = false;
    }
  }
	

function humanDate(inDate) {
	var goodDate = "";
var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");
	  if (typeof inDate == "undefined") {
		  return("");
	  }
     var dArray = inDate.split("-");
var curr_date = dArray[2];
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
   {
   sup = "st";
   }
else if (curr_date == 2 || curr_date == 22)
   {
   sup = "nd";
   }
else if (curr_date == 3 || curr_date == 23)
   {
   sup = "rd";
   }
else
   {
   sup = "th";
   }

curr_date = curr_date.replace(/^[0]+/g,"");

goodDate = " "+m_names[dArray[1]-1]+" "+ curr_date  + "<SUP>" + sup + "</SUP> ";
	
	return(goodDate);
}
function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}

function goState(passed) {

       window.location='/content/Hearing_Aids/Store_Locator/Store_List_By_State?state='+passed;
 
	return false;
}

function jumpZip(zipval, day) {
    var op = 'calendar_form_zip='+zipval;

    if (typeof day!= "undefined") {
          var op =op+'&calendar_selected_date='+day;
   }
    document.location.href='/content/Hearing_Aids/Store_Locator/Store_List_By_Zip?'+op;
    return false; 
}


function goZip() {
    var goZipV = $("#find_a_location_zip").val();
	var usRegExpr = /\d{5}(-\d{4})?/;
	var canadaRegExpr = /[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/;
    if ((usRegExpr.test(goZipV)) || (canadaRegExpr.test(goZipV))) {
    	jumpZip(goZipV);
    } else {
	    if (goZipV == "Enter Zip/Postal Code") {
		    eMess = "Please enter a valid U.S. or Canadian zip code";
	    } else {
		    eMess = goZipV +" is not a valid U.S. or Canadian zip code."
	    }
	    	$("#find_a_location_error_message").html(eMess).css("display","block");
    }
    return false;
}

function goZipComponentCalendar() {
    var goZipV = $("#calendar_form_zip").val();
    var goZipDate = $("#calendar_selected_date").val();
	var usRegExpr = /\d{5}(-\d{4})?/;
	var canadaRegExpr = /[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/;
    if ((usRegExpr.test(goZipV)) || (canadaRegExpr.test(goZipV))) {
    	jumpZip(goZipV, goZipDate);
    } else {
	    if (goZipV == "Enter Zip/Postal Code") {
		    eMess = "Please enter a valid U.S. or Canadian zip code";
	    } else {
		    eMess = goZipV +" is not a valid U.S. or Canadian zip code."
	    }
	    	$("#calendar_cc_error_message").html(eMess).css("display","block");
    }
    return false;
}

function goZipInlineCalendar() {
    var goZipV = $("#hidden_calendar_form_zip").val();
    var goZipDate = $("#hidden_calendar_selected_date").val();
	var usRegExpr = /\d{5}(-\d{4})?/;
	var canadaRegExpr = /[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/;
    if ((usRegExpr.test(goZipV)) || (canadaRegExpr.test(goZipV))) {
    	jumpZip(goZipV,goZipDate);
    } else {
	    if (goZipV == "Enter Zip/Postal Code") {
		    eMess = "Please enter a valid U.S. or Canadian zip code";
	    } else {
		    eMess = goZipV +" is not a valid U.S. or Canadian zip code."
	    }
	    	$("#hidden_calendar_ic_error_message").html(eMess).css("display","block");
    }
    return false;
}

function goZipFB() {
    var goZipV = $("#find_a_location_zipFB").val();
	var usRegExpr = /\d{5}(-\d{4})?/;
	var canadaRegExpr = /[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/;
    if ((usRegExpr.test(goZipV)) || (canadaRegExpr.test(goZipV))) {
    	jumpZip(goZipV);
    } else {
	    if (goZipV == "Enter Zip/Postal Code") {
		    eMess = "Please enter a valid U.S. or Canadian zip code";
	    } else {
		    eMess = goZipV +" is not a valid U.S. or Canadian zip code."
	    }
	    	$("#find_a_locationFB_error_message").html(eMess).css("display","block");
    }
    return false;
}

function dateInArray(needle, haystack) {
	for (var i in haystack) {
		if (needle == haystack[i]) {
			return(true);
		}
	}
	return(false);
}


function simpleSearchStates(selectedState) {
	 var matchState = "";
	var op = "";
	 op += '<optgroup label="State">';
	 if (typeof selectedState != "undefined") {
		 matchState = selectedState;
	 }
	 op += '<option value="">Select State</option>';
	for (var i in State) {
		if (State[i].code == matchState) {
			op += '<option value="'+State[i].code+'" SELECTED>'+State[i].name+'</option>';
		} else {
			op += '<option value="'+State[i].code+'">'+State[i].name+'</option>';
		}
	}
	op += '</optgroup>';
	return(op);
}

function getSearchStates (searchID) {
	var op = "";
	 op += '<select name="search'+searchID+'" id="search'+searchID+'" onchange="goState(this.value);">';
	 op += '<option value="#">See stores by state</option>';
	for (var i in State) {
		if (i == 0) {
			op += '<optgroup label="United States">';
		}
		if (State[i].inMenu == 1) {
			op += '<option value="'+State[i].code+'">&nbsp;&nbsp;'+State[i].name+'</option>';
		}
		if (i == lastUS) {
			op += '</optgroup>';
			op += '<optgroup label="Canada">';
		}
	}
	op += '</optgroup>';
	 op += '</select>';
	return(op);
}

function setSearchAgain() {
	var op = '<div style="display: none;"><div id="find_a_location_form"><p>Thank you for your interest in finding a Sonus professional. Please enter a zip code below to search for a nearby Sonus location.</p><p id="find_a_locationFB_error_message" style="color: #dd0000; font-size: 12px; display: none;"></p><form method="post" action="#" id="find_a_location_input_form" name="find_a_location_input_form" onsubmit="goZipFB(); return false;"><table cellspacing="5" cellpadding="0" border="0" summary="" align="center"> <tr> <td>  <input type="text" name="find_a_location_zipFB" id="find_a_location_zipFB" value="Enter Zip/Postal Code" class="input_text" onfocus="clearTextBox(this)" onblur="checkTextBox(this)" /> or  <div id="selectState"> </div></td><td> <input type="image" name="calendar_zip_submit" id="calendar_zip_submit" src="/consumerUS/img/button_submit_86x28.gif" /></td> </tr> </table></form> </div> </div> <a href="#find_a_location_form" class="find_a_location search_again">Search Again</a> <b>or</b> &nbsp;';
	 op+= getSearchStates(3);
	$("#searchAgain").html(op);
	if($(".search_again").length>0){
	    $(".search_again").fancybox({
	      "autoDimensions":false,
	      "overlayColor":"#000",
	      "overlayOpacity":.7,
	      "padding":30,
	      "margin":0,
	      "width":390,
	      "height":120,
	      "onStart":function(){
		$("#fancybox-wrap").removeClass().addClass("fancybox_find_a_location");
	      }
	    });
	  }

	if($("#find_a_location_zipFB").length>0){
	  $("#find_a_location_zipFB").focus(function(){
	      $("#find_a_locationFB_error_message").css("display","none");
	  });
	}
	 $("#selectState").html(getSearchStates(2));
}

function goZipfooter () {
	 var goZipV = $("#zip_code_field").val();
	var usRegExpr = /\d{5}(-\d{4})?/;
	var canadaRegExpr = /[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/;
    if (usRegExpr.test(goZipV) || canadaRegExpr.test(goZipV)) {
	jumpZip(goZipV);
    } else {
	    if (goZipV == "Enter Zip/Postal Code") {
		    eMess = "Please enter a valid U.S. or Canadian zip code";
	    } else {
		    eMess = goZipV +" is not a valid U.S. or Canadian zip code."
	    }
	    	$("#zip_code_form_error_message").html(eMess).css("display","block");
    }
	 
    return false;

}

function setSearchAgainFooter() {
	var op = '<div style="display: none;"><div id="find_a_location_footer"><p>Thank you for your interest in finding a Sonus professional. Please enter a zip code below to search for a nearby Sonus location.</p><p id="zip_code_form_error_message" style="color: #dd0000; font-size: 12px; display: none;"></p><form method="post" action="#" id="find_a_location_input_footer" name="find_a_location_input_footer" onsubmit="goZipfooter(); return false;"><table cellspacing="5" cellpadding="0" border="0" summary="" align="center"> <tr> <td>  <input type="text" name="zip_code_field" id="zip_code_field" value="Enter Zip/Postal Code" class="input_text" onfocus="clearTextBox(this)" onblur="checkTextBox(this)" /> or  <div id="selectStateFooter"> </div></td><td> <input type="image" name="calendar_zip_submit" id="calendar_zip_submit" src="/consumerUS/img/button_submit_86x28.gif" /></td> </tr> </table></form> </div> </div><p><a href="#find_a_location_footer" target="" title="" class="find_a_location footer_again">Click here to find a location</a></p>';
	$("#searchAgainFooter").html(op);
	if($(".footer_again").length>0){
	    $(".footer_again").fancybox({
	      "autoDimensions":false,
	      "overlayColor":"#000",
	      "overlayOpacity":.7,
	      "padding":30,
	      "margin":0,
	      "width":390,
	      "height":120,
	      "onStart":function(){
		$("#fancybox-wrap").removeClass().addClass("fancybox_find_a_location");
	      }
	    });
	  }

	if($("#zip_code_field").length>0){
	  $("#zip_code_field").focus(function(){
	      $("#zip_code_form_error_message").css("display","none");
	  });
	}
	 $("#selectStateFooter").html(getSearchStates(4));
}


function buildStates()
{
  State[0] = new state(0, "Alabama",          "AL",  "32.7990",  "-86.8073",  "US",5);
  State[1] = new state(0, "Alaska",           "AK",  "61.3850",  "-152.2683", "US",5);
  State[2] = new state(0, "Arizona",          "AZ",  "33.7712",  "-111.3877", "US",5);
  State[3] = new state(0, "Arkansas",         "AR",  "34.9513",  "-92.3809", "US",5);
  State[4] = new state(1, "California",       "CA",  "36.1700",  "-119.7462", "US",5);
  State[5] = new state(0, "Colorado",         "CO",  "39.0646",  "-105.3272", "US",5);
  State[6] = new state(0, "Connecticut",      "CT",  "41.5834",  "-72.7622", "US",5);
  State[7] = new state(0, "Delaware",         "DE",  "39.3498",  "-75.5148", "US",5);
  State[8] = new state(0, "Florida",          "FL",  "27.8333",  "-81.7170", "US",5);
  State[9] = new state(0, "Georgia",          "GA",  "32.9866",  "-83.6487", "US",5);
 State[10] = new state(0, "Hawaii",          "HI",  "21.1098",  "-157.5311", "US",5);
 State[11] = new state(0, "Iowa",            "IA",  "42.0046",  "-93.2140", "US",5);
 State[12] = new state(1, "Illinois",        "IL",  "40.3363",  "-89.0022", "US",5);
 State[13] = new state(0, "Indiana",         "IN",  "39.8647",  "-86.2604", "US",5);
 State[14] = new state(0, "Idaho",           "ID",  "44.2394",  "-114.5103", "US",5);
 State[15] = new state(0, "Kansas",          "KS",  "38.5111",  "-96.8005", "US",5);
 State[16] = new state(0, "Kentucky",        "KY",  "37.6690",  "-84.6514", "US",5);
 State[17] = new state(0, "Louisiana",       "LA",  "31.1801",  "-91.8749", "US",5);
 State[18] = new state(0, "Maine",           "ME",  "44.6074",  "-69.3977", "US",5);
 State[19] = new state(1, "Maryland",        "MD",  "39.0724",  "-76.7902", "US",6);
 State[20] = new state(0, "Massachusetts",   "MA",  "42.2373",  "-71.5314", "US",5);
 State[21] = new state(1, "Michigan",        "MI",  "43.3504",  "-84.5603", "US",5);
 State[22] = new state(1, "Minnesota",       "MN",  "45.7326",  "-93.9196", "US",6);
 State[23] = new state(0, "Mississippi",     "MS",  "32.7673",  "-89.6812", "US",5);
 State[24] = new state(0, "Missouri",        "MO",  "38.4623",  "-92.3020", "US",5);
 State[25] = new state(0, "Montana",         "MT",  "46.9048",  "-110.3261", "US",5);
 State[26] = new state(0, "Nebraska",        "NE",  "41.1289",  "-98.2883", "US",5);
 State[27] = new state(0, "Nevada",          "NV",  "38.4199",  "-117.1219", "US",5);
 State[28] = new state(0, "New Hampshire",   "NH",  "43.4108",  "-71.5653", "US",5);
 State[29] = new state(0, "New Jersey",      "NJ",  "40.3140",  "-74.5089", "US",6);
 State[30] = new state(0, "New Mexico",      "NM",  "34.8375",  "-106.2371", "US",5);
 State[31] = new state(1, "New York",        "NY",  "42.1497",  "-74.9384", "US",5);
 State[32] = new state(0, "North Carolina",  "NC",  "35.6411",  "-79.8431", "US",5);
 State[33] = new state(0, "North Dakota",    "ND",  "47.5362",  "-99.7930", "US",5);
 State[34] = new state(0, "Ohio",            "OH",  "40.3736",  "-82.7755", "US",5);
 State[35] = new state(0, "Oklahoma",        "OK",  "35.5376",  "-96.9247", "US",5);
 State[36] = new state(1, "Oregon",          "OR",  "44.5672",  "-122.1269", "US",5);
 State[37] = new state(1, "Pennsylvania",    "PA",  "40.5773",  "-77.2640", "US",6);
 State[38] = new state(0, "Rhode Island",    "RI",  "41.6772",  "-71.5101", "US",5);
 State[39] = new state(0, "South Carolina",  "SC",  "33.8191",  "-80.9066", "US",5);
 State[40] = new state(0, "South Dakota",    "SD",  "44.2853",  "-99.4632", "US",5);
 State[41] = new state(0, "Tennessee",       "TN",  "35.7449",  "-86.7489", "US",5);
 State[42] = new state(0, "Texas",           "TX",  "31.1060",  "-97.6475", "US",5);
 State[43] = new state(1, "Utah",            "UT",  "40.1135",  "-111.8535", "US",5);
 State[44] = new state(0, "Vermont",         "VT",  "44.0407",  "-72.7093", "US",5);
 State[45] = new state(1, "Virginia",        "VA",  "37.7680",  "-78.2057", "US",6);
 State[46] = new state(1, "Washington",      "WA",  "47.3917",  "-121.5708", "US",5);
 State[47] = new state(0, "West Virginia",   "WV",  "38.4680",  "-80.9696", "US",5);
 State[48] = new state(1, "Wisconsin",       "WI",  "44.2563",  "-89.6385", "US",5);
 State[49] = new state(0, "Wyoming",         "WY",  "42.7475",  "-107.2085", "US",5);
 State[50] = new state(1, "Alberta",         "AB",  "54.2400",  "-115.0000", "CA",5);
 State[51] = new state(1, "British Columbia",   "BC",  "55.0000",  "-125.0900", "CA",5);
 State[52] = new state(0, "Ontario",         "ON",  "43.6787",  "-79.5410", "CA",5);
 State[53] = new state(0, "Quebec",          "QC",  "45.5553",  "-73.6523", "CA",5);
 State[54] = new state(0, "Nova Scotia",     "NS",  "44.7183",  "-63.5888", "CA",5);
 State[55] = new state(0, "New Brunswick",   "NB",  "45.3086",  "-66.1157", "CA",5);
 State[56] = new state(0, "Manitoba",        "MB",  "49.9113",  "-97.1850", "CA",5);
 State[57] = new state(0, "Prince Edward Island",   "PE",  "46.2638",  "-63.1467", "CA",5);
 State[58] = new state(0, "Saskatchewan",   "SK",  "52.1347",  "-106.6552", "CA",5);
 State[59] = new state(0, "Newfoundland",   "NL",  "47.5556",  "-52.7454", "CA",5);
 State[60] = new state(0, "Northwest Territories",   "NT",  "62.4422",  "-114.3975", "CA",5);
 State[61] = new state(0, "Yukon",   "YT",  "60.7166",  "-135.05", "CA",5);
 State[62] = new state(0, "Nunavut",   "NU",  "63.7486",  "-68.5197", "CA",5);
 setSearchAgain();
 setSearchAgainFooter();

  $(document).ajaxError( function(e, xhr, settings, exception) {  
     alert('Ajax error in: ' + settings.url + ' \n'+'error:\n' + xhr.responseText + '\n' + exception );
 });
  cookiesEnabled();

}

  function getCodeByState(state) {
  	for (var i in State) {
		if (State[i].name == state) {
			return(State[i].code);
		}
	}
	return("UKN");
  }
  
  function getStateByCode(code) {
  	for (var i in State) {
		if (State[i].code == code) {
			return(State[i].name);
		}
	}
	return("Unknown");
  }
  
  function getStateLatByCode(code) {
  	for (var i in State) {
		if (State[i].code == code) {
			return(State[i].lat);
		}
	}
	return(State[22].lat);
  }
  function getStateLngByCode(code) {
  	for (var i in State) {
		if (State[i].code == code) {
			return(State[i].lng);
		}
	}
	return(State[22].lng);
  }

  function state (inMenu, name, code, lat, lng, country, zoom) {
	  this.inMenu = inMenu;
	  this.name = name;
	  this.code = code;
	  this.lat = lat;
	  this.lng = lng;
	  this.country = country;
	  this.zoom = zoom;
  }


function clearCookies() {
	 var values_to_write=new Array( "title","first_name","last_name","address_line_1","address_line_2","city","state","zip_code","country","email","phone_area_code","phone_first_three","phone_last_four","dob_month","dob_day","dob_year","q_one","q_two","src","calendar_selected_date");

    for(var i=0;i<values_to_write.length;i++){
	      $.cookie(values_to_write[i],null);
	}
}

function reloadImg(id) {
   var obj = document.getElementById(id);
   var src = obj.src;
   var pos = src.indexOf('?');
   if (pos >= 0) {
      src = src.substr(0, pos);
   }
   var date = new Date();
   obj.src = src + '?v=' + date.getTime();
   return false;
}


