/************************
*
*  This is my code to control survey on our various webpages.  Only HTML needed is: 
*       <div id="survey_box" class="ui-widget-content" style="display:none" title="How did you hear about DEMAP?">    
*       <form name="survey_form" id="survey_form" action="#" method="get">
*           <p>
*				<div id="error_container"></div>
*				<input type=radio name='survey_option' value='radio' />Radio<br/>
*				<input type=hidden name ='page' id="page" value='demap' />
*           </p>
*			<label>Thank you for your participation!</label>
*
*       </form>
*       </div>
* Then call in the header
*   $(function () {	show_survey() });
*************************/
		//GET COOKIE FUNCTION I FOUND ONLINE!
		function getCookie(c_name)
		{
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++)
		{
		  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		  x=x.replace(/^\s+|\s+$/g,"");
		  if (x==c_name)
			{
			return unescape(y);
			}
		  }
		}


		function set_cookie() //should set a cookie with the pages's value and set to expire in 5 years
		{
			var fiveyears = new Date();
			fiveyears.setTime(fiveyears.getTime() + 157680000000);
			var expire_string = fiveyears.toUTCString();
			document.cookie = $('#page').val() +'=1' +'; path=/; expires=' + expire_string;
			
		}
		
		function send_survey () //send the survey if a radio is checked.  Otherwise fill my error container
		{
			if ($('input:radio[name=survey_option]:checked').val())
			{
				$.ajax(
				{
					url: '/scripts/ajax/ajax_survey.php',
					type: 'POST',
					data: 'survey_option='+$('input:radio[name=survey_option]:checked').val() +'&page='+ $('#page').val()
					//data: 'survey_option=radio&page=demap'
				});
			$('#survey_box').dialog('close');
			}
			else
			{
				$('#error_container').html('Please Select an Option!');
				$("#error_container").addClass("ui-state-error");
			}
		}

		function show_survey()//read teh cookie.  If the page's value is 1 then show the dialog.  
		{
		
			var thispage = $('#page').val();
			//alert(getCookie(thispage));
			if (getCookie(thispage) != 1)
			{
				$('#survey_box').dialog({
					autoOpen: true,
					minHeight: 475,
					width: 375,
					modal: true,
					resizable: false,
					close: function(event, ui) { set_cookie() },
					buttons: 
					[{
						text: 'Submit',
						click: function() { send_survey () }
					},
					{
						text: 'No Thanks',
						click: function() { $(this).dialog('close'); }
					}]
				});
			}
		
		}
