/* dialog functions  */
$(document).ready(function() {
//$(function() {
	//setup the site error dialog
	$("#site-err-dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: false,
		height:140,
		modal: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		open: function(event, ui) {
			if ($(this).data('msg') ) {
	    		$(this).find("#msg").html($(this).data('msg'));
			}
			if ($(this).data('title') ) {
	    		$(this).dialog('option', 'title', $(this).data('title'));
			}
			else {
				$(this).dialog('option', 'title', 'Error!');
			}
		}
	});

	//setup the log in dialog
	$("#site-login-dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Log In': function() {
		    	//get the data to post
		    	var pdata = { txtEmailAddress: $(this).find("#txtEmailAddress").val(), txtPass: $(this).find("#txtPass").val() };

				//call ajax function to login
				jQuery.ajax({
					type: "POST",
					url: "/handlers/ajaxMemberLogin.php",
					data: pdata,
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$("#site-err-dialog").data('msg',textStatus).dialog("open");
					},
					success: function(resp){
						if (resp.indexOf('error:') == -1) {
							//no error so reload page
							//window.location.reload(true);
							window.location.reload();
						}
						else {
							$("#site-err-dialog").data('msg',resp).dialog("open");
						}
			   		}
				});
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});

	//setup the forgot password dialog
	$("#site-forgotpassword-dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		open: function(event, ui) {
			if ($(this).data('email') ) {
	    		$(this).find("#txtEmailAddress").val($(this).data('email'));
			}
		},
		buttons: {
			'Email Password': function() {
		    	//get the data to post
		    	var pdata = { txtEmailAddress: $(this).find("#txtEmailAddress").val() };

				//call ajax function to login
				jQuery.ajax({
					type: "POST",
					url: "/handlers/ajaxForgotPassword.php",
					data: pdata,
					error: function(XMLHttpRequest, textStatus, errorThrown){
						$("#site-err-dialog").data('msg',textStatus).dialog("open");
					},
					success: function(resp){
						if (resp.indexOf('error:') == -1) {
							//no error so reload page
							$(this).dialog('close');
							$("#site-err-dialog").data('title','Email Sent').data('msg',resp).dialog("open");
						}
						else {
							$("#site-err-dialog").data('msg',resp).dialog("open");
						}
			   		}
				});
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});


	//setup the site popup iframe dialog - used to display popup pages
	$("#site-popup-dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		resizable: true,
		height:555,
		width: 690,
		modal: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		open: function(event, ui) {
			if ($(this).data('src') ) {
	    		$(this).find("#popupframe").attr('src',$(this).data('src'));
			}
			else {
				$(this).find("#popupframe").attr('src','');
			}
			if ($(this).data('title') ) {
	    		$(this).dialog('option', 'title', $(this).data('title'));
			}

		}
	});


});

function fblogin(curZip) {
	var pdata = { curZipcode: curZip };
	//call ajax function to login
	jQuery.ajax({
		type: "POST",
		url: "/handlers/ajaxFacebookLogin.php",
		data: pdata,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$("#site-err-dialog").data('msg',textStatus).dialog("open");
		},
		success: function(resp){
			if (resp.indexOf('error:') == -1) {
				//no error so reload page
				//window.location.reload(true);
				window.location.reload();
			}
			else {
				$("#site-err-dialog").data('msg',resp).dialog("open");
			}
   		}
	});
}

function reachlogout() {
	//call ajax function to logout
	jQuery.ajax({
		type: "POST",
		url: "/handlers/ajaxMemberLogout.php",
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$("#site-err-dialog").data('msg',textStatus).dialog("open");
		},
		success: function(resp){
			if (resp.indexOf('error:') == -1) {
				//no error so go to home page
				//document.location = '/';
				//window.location.reload(true);
				window.location.reload();
			}
			else {
				$("#site-err-dialog").data('msg',resp).dialog("open");
			}
   		}
	});
}

function getSubcategories(catid, zip, marketid) {
	//var pdata = "categoryId="+catid;
	var pdata = { 'categoryId': catid, 'zipcode': zip, 'marketId': marketid};

	//call ajax function to pull subcategories
	jQuery.ajax({
		type: "POST",
		url: "/handlers/ajaxGetSubcategories.php",
		data: pdata,
		dataType: "json",
		error: function(XMLHttpRequest, textStatus, errorThrown){
			var errText = "XMLHttpRequest="+XMLHttpRequest.responseText+"<br />textStatus="+textStatus+"<br />errorThrown="+errorThrown
			$("#site-err-dialog").data('msg',errText).dialog("open");
		},
		success: function(resp){
			if (resp.length > -1) {
				var options = "<option value=''>Select Specialty</option>";
				for (var i = 0; i < resp.length; i++) {
					options += "<option value='" + resp[i].txtSpecialtyID + "'>" + resp[i].txtSpecialtyName + "</option>";
				}
				$("#selSubcategorySearch").html(options);
				options = null;
			}
			else {
				$("#site-err-dialog").data('msg',resp).dialog("open");
			}

			resp = null;

   		}
	});
}

function changelocation() {
	$('#change-location').toggle('slow');
}

function zipcodeSearch(zip,curPage) {
	var pdata = { 'srchZip': zip, 'curPage': curPage};

	//call ajax function to pull subcategories
	jQuery.ajax({
		type: "POST",
		url: "/handlers/ajaxChangeLocation.php",
		data: pdata,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			var errText = "XMLHttpRequest="+XMLHttpRequest.responseText+"<br />textStatus="+textStatus+"<br />errorThrown="+errorThrown
			$("#site-err-dialog").data('msg',errText).dialog("open");

			//jQuery.removeData();
		},
		success: function(resp){
			if (resp.indexOf('Sorry,') == -1) {
				//document.location.replace(resp);
				document.location = resp;
				//$("#site-err-dialog").data('msg',resp).dialog("open");
			}
			else {
				$("#site-err-dialog").data('msg',resp).dialog("open");
			}

			resp = null;
			//jQuery.removeData();

   		}
	});

}

function getNeighborhoods(marketid, marketName) {
	var pdata = { 'marketId': marketid};

	//call ajax function to pull subcategories
	jQuery.ajax({
		type: "POST",
		url: "/handlers/ajaxGetNeighborhoods.php",
		data: pdata,
		dataType: "json",
		error: function(XMLHttpRequest, textStatus, errorThrown){
			var errText = "XMLHttpRequest="+XMLHttpRequest.responseText+"<br />textStatus="+textStatus+"<br />errorThrown="+errorThrown
			$("#site-err-dialog").data('msg',errText).dialog("open");

			//jQuery.removeData();
		},
		success: function(resp){
			if (resp.length && resp.length > -1) {
				//var options = "<option value=''>SELECT NEIGHOORHOOD</option>";
				var options = "";
				for (var i = 0; i < resp.length; i++) {
					options += "<option value='" + resp[i].ZipCode + "'>" + resp[i].CityStateName + ", " + resp[i].ZipCode + "</option>";
				}
				$("#selNeighborhood").html(options);

				$("#spMarketName").html(marketName);
				options = null;
			}
			else {
				$("#site-err-dialog").data('msg','There are local savings for the selected area.').dialog("open");
			}

			resp = null;
			//jQuery.removeData();

   		}
	});
}

