$().ready( function () {

	$(function(){  
			$('#survey').dialog({
				bgiframe: true,
				autoOpen: false,
				modal: true,
				width: 640,
				resizable: false,
				buttons: {
					Cauta: function(){
						if($("#buget").val() !== '...'){
							setCookie('POPsurvey','POPsurvey',30);
							
							$.post("ajax.php", $("#popup_survey").serialize(),
							function(data){
								if(data.db_check == 'fail'){
									$("#error_message").html("<p>Database not available. Please try again.</p>");
								} else {
									var pa = 0;
									var pb = 0;
									var valuta = $("#valutaSondaj").val();
									if($("#buget").val()=='< 100'){
										var pa = 0;
										var pb = 100;
									}
									if($("#buget").val()=='100-300'){
										var pa = 100;
										var pb = 300;
									}
									if($("#buget").val()=='300-500'){
										var pa = 300;
										var pb = 500;
									}
									if($("#buget").val()=='500-1000'){
										var pa = 500;
										var pb = 1000;
									}
									if($("#buget").val()=='1000-2000'){
										var pa = 1000;
										var pb = 2000;
									}
									if($("#buget").val()=='> 2000'){
										var pa = 2000;
										var pb = 100000;
									}
									$('#survey').dialog('close');
									document.location = 'index.php?q=modal&sectiune=listare&catid=modal&pa='+pa+'&pb='+pb+'&moneda='+valuta+'#afterCaut';
									//$('#survey_thanks').dialog('open');
								}
								}, "json");
						}else{
							$("#error_message").html("<p>Va rugam selectati bugetul din lista</p>");
						}
					}
				}
			});
		});
		
	$(function(){  
			$('#survey_thanks').dialog({
				bgiframe: true,
				autoOpen: false,
				modal: true,
				width: 640,
				resizable: false,
				buttons: {
					Inchide: function(){
						$(this).dialog('close');
						}
					}
			});
		});

	$('.surveyCookieDelete').click(function() {		
		deleteCookie('POPsurvey');
		alert('Survey cookie cleared');
	});
	
	$('p#surveyDenied a').click(function() {
		setCookie('POPsurvey','POPsurvey',30);
		$('#survey').dialog('close');
	});
	
	$('#thanksClose').click(function() {
		$('#survey_thanks').dialog('close');
	});
	
	checkCookie('POPsurvey');	

});

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
} 
function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}

function checkCookie(c_name)
{
	cookie_value=getCookie(c_name);
	if (cookie_value=="") {
		$('#survey').dialog('open');
	}
	  
}

function deleteCookie(c_name) {
	document.cookie = c_name +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 


