function editMyDinners(iddinner,value,userId,ownerId){
	
	new Ajax.Request('mydinnersajax.php',
		  	{
		  	  method: 'get',
		  	  parameters: {act: 'edit', value: value, iddinner: iddinner,userid: userId, ownerid: ownerId},
		  	  
		  	  onComplete: function(transport){  	  
			  		var response = transport.responseText;
					 var fieldName ="resultMyDinners_"+iddinner+"_"+userId;
					 var val="";
					 var ig="";
					 
					 if(value==1){
						val=0;
						ig="tpl/icons/checkon.gif"
					 }
					 else{
						 val=1;
						 ig="tpl/icons/checkoff.gif"
					 }

			         $(fieldName).innerHTML="<img src='"+ig+"' onclick='editMyDinners("+iddinner+","+val+","+userId+","+ownerId+")' />";
		  	  } 
		  	});
}

function apply(){ 

	new Ajax.Request('apply.php',
		  	{
		  	  method: 'post',
			  parameters: {to: $('to').value, subject: $('subject').value, message: $('message').value, iddinner: $('iddinner').value},reply_to_application: $('reply_to_application'),
			  
		  	  onComplete: function(transport){
		         var response = eval(transport.responseJSON);
		         if(response.result == 0){
			         $('applyerror').innerHTML = response.msg;
			         $('waitmsg').hide();
			         $('applyerror').show();
			     }
		         else if (response.result == 1){
		        	 $('applyerror').hide();		        	 
		        	 $('registerform').hide();
		        	 $('waitmsg').hide();
		        	 $('applyok').innerHTML = response.msg;
		        	 $('applyok').show();
				 }
		       },
		       onLoading:function(transport){
		    	   $('registerform').hide();
		           $('waitmsg').show();
		       }
		  	});
}

function createFacebox(id)
{
	$q.facebox("<div style=\"height:120px; width: 600px;\" id=\"askfacebook\"  align=\"center\"><h1 style=\"font-size: 19px\">You are sucessfully connected to <img style=\"vertical-align: middle;\" src=\"tpl/icons/facebook-logo-small.jpg\" /></h1><p style=\"font-size: 17px\">Do you already have an InnerDinner account?</p><p style=\"font-size: 16px; \"><img src=\"tpl/img/completed.png\" width=\"16\" heigth=\"16\" style=\"vertical-align: middle;\" />&nbsp;<a name=\"linktoface\" id=\"linktoface\" onclick=\"credentialslink("+id+",'');\" href=\"#\">Yes, I do!</a>&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"tpl/img/completed.png\" style=\"vertical-align: middle;\" width=\"16\" heigth=\"16\" />&nbsp;<a href=\"register.php\">No, but I want to register!</a></p></div>");
	}

function createFaceboxLogin(id,nexturl){
	$q.facebox("<div style=\"height:120px; width: 600px;\" id=\"askfacebook\"  align=\"center\"><h1 style=\"font-size: 19px\">You are sucessfully connected to <img style=\"vertical-align: middle;\" src=\"tpl/icons/facebook-logo-small.jpg\" /></h1><p style=\"font-size: 17px\">Do you already have an InnerDinner account?</p><p style=\"font-size: 16px; \"><img src=\"tpl/img/completed.png\" width=\"16\" heigth=\"16\" style=\"vertical-align: middle;\" />&nbsp;<a name=\"linktoface\" id=\"linktoface\" onclick=\"credentialslink("+id+",'"+nexturl+"');\" href=\"#\">Yes, I do!</a>&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"tpl/img/completed.png\" style=\"vertical-align: middle;\" width=\"16\" heigth=\"16\" />&nbsp;<a href=\"register.php\">No, but I want to register!</a></p></div>");
}

function credentialslink(val,nextu){
	if(nextu=="")
	  $q.facebox("<div id=\"facebooklogindiv\"><table  width=\"100%\"><tr align=\"center\"><td><h1>Access InnerDinner using your Facebook account</h1></td></tr><tr align=\"center\"><td><p>If you complete this step, you will be able to access InnerDinner using your Facebook credentials.</p><p>Please enter your InnerDinner username/password to confirm that you want to link the accounts.</p></td></tr><tr><td><p class=\"error\" id=\"logfaceerror\" name=\"logfaceerror\" style=\"text-align: center;background-image: none; display:none;\"></p></div></td></tr><tr><td align=\"center\"><table><tr><td align=\"right\"><label for=\"emailfb\">Email:</label></td><td><input name=\"emailfb\" type=\"text\" id=\"emailfb\" size=\"25\" style=\"width:200px;\"  /></td></tr><tr><td align=\"right\"><label for=\"passwordfb\">Password:</label></td><td ><input name=\"passwordfb\" type=\"password\" id=\"passwordfb\" size=\"25\" maxlength=\"255\" style=\"width:200px;\" /></td></tr><tr><td colspan=\"2\" align=\"center\"><input class=\"submit\" name=\"submitfb\" onclick=\"linktoFacebook();\" type=\"submit\" value=\"Link Account\" /></td></tr><tr><td colspan=\"2\" align=\"center\"><input type=\"hidden\" value="+val+" id=\"facebookID\" name=\"facebookID\" ></td></tr></table></div>");
	else
		$q.facebox("<div id=\"facebooklogindiv\"><table  width=\"100%\"><tr align=\"center\"><td><h1>Access InnerDinner using your Facebook account</h1></td></tr><tr align=\"center\"><td><p>If you complete this step, you will be able to access InnerDinner using your Facebook credentials.</p><p>Please enter your InnerDinner username/password to confirm that you want to link the accounts.</p></td></tr><tr><td><p class=\"error\" id=\"logfaceerror\" name=\"logfaceerror\" style=\"text-align: center;background-image: none; display:none;\"></p></div></td></tr><tr><td align=\"center\"><table><tr><td align=\"right\"><label for=\"emailfb\">Email:</label></td><td><input name=\"emailfb\" type=\"text\" id=\"emailfb\" size=\"25\" style=\"width:200px;\"  /></td></tr><tr><td align=\"right\"><label for=\"passwordfb\">Password:</label></td><td ><input name=\"passwordfb\" type=\"password\" id=\"passwordfb\" size=\"25\" maxlength=\"255\" style=\"width:200px;\" /></td></tr><tr><td colspan=\"2\" align=\"center\"><input class=\"submit\" name=\"submitfb\" onclick=\"linktoFacebookLogin('"+nextu+"');\" type=\"submit\" value=\"Link Account\" /></td></tr><tr><td colspan=\"2\" align=\"center\"><input type=\"hidden\" value="+val+" id=\"facebookID\" name=\"facebookID\" ></td></tr></table></div>");	
  }
    
function fbLogout(val)
{
	jQuery(document).trigger('close.facebox');
	//if user is logged in fb and in id, checks if it is the same account
	if(val==1){
	FB.Connect.logout(function(response) {  
		window.location="login.php?act=logout";
		return false;
		});
	}
	else
		window.location="login.php?act=logout";
		
	
}

function loadIndex(id) {
	// $q(":text:first").focus();
	
	fbConnect("0","load");
}

function loadIndexLogin(id) {
	
	fbConnect("0","load");
}

function fbConnect(id,type) {
	FB.init("825043364c5c565c613f6739833521ee", "xd_receiver.htm",{"ifUserConnected":function(id)
		{
		
		if(id!=null && id!="" && type=="link" && id!="0")
			createFacebox(id);
		/*
		if(type=="load")
			window.location="profile.php";*/
		}
	
		
	
	});
}


function initFB() {
	
	FB.init("825043364c5c565c613f6739833521ee","xd_receiver.htm",{"reloadIfSessionStateChanged":true});
}

function openWindow(url,title,width,height){
	var newwindow=window.open(url,title,'height='+height+', width='+width+', resizable=yes, scrollbars=yes, toolbar=no, status=no');

	if(newwindow){
		newwindow.focus();
	}
	else{
		alert('Your browser must allow popups.');
	}
}

function isNumberKey(evt){    
	try{
		  var charCode = (evt.which) ? evt.which : event.keyCode;
	      if (charCode > 31 && (charCode < 48 || charCode > 57))
	         return false;

	      return true;
	   }catch(err)
	   {}
	   return true;
}

function header(){
  
  if ($('quicksearch').value == 'Quick Find'){
    $('quicksearch').value = '';
  }
}

function getcurrencies(country)
{
       var url = 'getcurrency.php';
       var par = 'country='+country;
       var myAjax = new Ajax.Request( url, { method: 'post', parameters:par,onComplete: result});
       
}


function result(request)
{
       var currency = request.responseText;
       $('currencyLabel').innerHTML=currency;
       $('curVal').value=currency;
       $('amount').value=$('amount').value.replace(/[^0-9]/g, '');
}

function getCur()
{
	if($('price').value == 2 || $('price').value == 3 ){
		$('amount').show();
		$('perPerson').show();
		$('currencyLabel').show();
		getcurrencies($('country').value);
	}
	else{
		$('amount').hide();
		$('perPerson').hide();
		$('currencyLabel').hide();		
	}	
}

function toggleChild(source,target,page){
	if(page=="register"){
		if($(source).value=="00"){
			$(target).hide();
			$(target+"Label").hide();
		}
		else{
			$(target+"Label").show();
			$(target).show();
		}
	}
	if($(source).id=="country")
	{
		getcurrencies($(source).value);
	}
}

function setLabelsText(source,target)
{
	if($(source).id=="dinnerType"){
		
		
	}
}

function toggleFieldsValue(tagname){
	if($(tagname+'Val').value!='used')
		$(tagname).value='';
	
	$(tagname+'Val').value='used';
}


/* function to change reply buttons backgrounds and to enable the send message button*/
function setAnswer(source,repvalue)
{
	if(source=="yes"){
    	$('yes').src="tpl/icons/replybtn/yes_blue.png";
    	$('maybe').src="tpl/icons/replybtn/not_yet_grey.png";
    	$('no').src="tpl/icons/replybtn/no_grey.png";
	}

	if(source=="maybe"){
    	$('maybe').src="tpl/icons/replybtn/not_yet_blue.png";
    	$('yes').src="tpl/icons/replybtn/yes_grey.png";
    	$('no').src="tpl/icons/replybtn/no_grey.png";
	}

	if(source=="no"){
		$('maybe').src="tpl/icons/replybtn/not_yet_grey.png";
    	$('yes').src="tpl/icons/replybtn/yes_grey.png";
		$('no').src="tpl/icons/replybtn/no_blue.png";
	}
    
	$('replyValue').value= repvalue;
	$('submit').disabled="";
	$('submit').style.cursor="pointer";
}
