$(document).ready(function(){

   $.fn.alignCenter = function() {

      var marginLeft =  - $(this).width()/2 + 'px';
      var marginTop =  - $(this).height()/2 + 'px';
      if($.browser.msie && $.browser.version == '6.0'){
	      var marginLeft =  - $(this).width()/2 + 'px';
				return $(this).css({'zIndex': '200', 'position':'absolute', 'margin-left':marginLeft, 'margin-top':-100});
	    }else{
	      return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
			}
   };

   $.fn.togglePopup = function(id){     if($(id).hasClass('hidden'))
     {
       if($.browser.msie)
       {
         $('#opaco').height($(document).height()).toggleClass('hidden')
                    .click(function(){$(this).togglePopup(id);});
       }
       else
       {
         $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.3)
                    .click(function(){$(this).togglePopup(id);});
       }
			 $(id).alignCenter().toggleClass('hidden');

     }
     else
     {
       if($.browser.msie && $.browser.version == '6.0')
	       $('#opaco').css({'zIndex':0});

       $('#opaco').toggleClass('hidden').removeAttr('style').unbind('click');
       $(id).toggleClass('hidden');
     }
   };
});


function saveFriends(){

	var mailstr = '';
	if($("#mail").attr("checked")){		if(!checkmail($("#f_mail").val())){			alert('Укажите E-mail получателя!');
			return false;
		}
		mailstr = ' ('+$('#f_mail').val().replace("\"","").replace("'","")+')';
	}

	$("#forkogo").html('В подарок для: '+$('#f_komy').val().replace("\"","").replace("'","")+ mailstr);



  if($("#sam").attr("checked")){  	var deliv = 'sam';
  }else{  	var deliv = 'mail';
  }
	$("div.forfr")
	.html('<input name="f_friends" type="hidden" value="OK">'+
	'<input name="f_komy" type="hidden" value="'+ $('#f_komy').val().replace("\"","").replace("'","") +'">'+
	'<input name="f_otkogo" type="hidden" value="'+ $('#f_otkogo').val().replace("\"","").replace("'","") +'">'+
	'<textarea style="display:none;" name="f_text">'+ $('#f_text').val().replace("\"","").replace("'","") +'</textarea>'+
	'<input name="f_deliv" type="hidden" value="'+ deliv.replace("\"","").replace("'","") +'">'+
	'<input name="f_mail" type="hidden" value="'+ $('#f_mail').val().replace("\"","").replace("'","") +'">')
	.find('#popup_bug')
	.togglePopup("#popup");
}
function clearFriends(){	$("#forkogo").text('Купить в подарок');	$('#f_komy').val('');
	$('#f_otkogo').val('');
	$('#f_text').val('');
	$("div.forfr").html();
	$('#popup_bug').togglePopup("#popup");
}
;

function checkmail(value) {
	reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	if (!value.match(reg)){		return false;
	}
	return true;
}
