$(function(){
	$('#copy').click(function(){copyText();});
			var copyText = function() {
					var txt = $('#invite_url').val();
					if(txt == ''){
						alert('There is no content to be copied');
						return ;
					}
			    		 if(window.clipboardData) {
			             window.clipboardData.clearData();
			            var judge = window.clipboardData.setData("Text", txt);
			            if(judge === true){
						 	alert("Successfully copied！");
			            }else{
			            	alert(" Copying is not allowed, please type manually");
			            }
				     } else if(navigator.userAgent.indexOf("Opera") != -1) {
				          window.location = txt;
				     } else if (window.netscape) {
			          try {
			               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			          } catch (e) {
						   alert("Denied by the browser! \n Please enter 'about:config' in the address bar, press enter\n Then set 'signed.applets.codebase_principal_support' to 'true'");
			               return ;
			          }
			          var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			          if (!clip)
			               return;
			          var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			          if (!trans)
			               return;
			          trans.addDataFlavor('text/unicode');
			          var str = new Object();
			          var len = new Object();
			          var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			          var copytext = txt;
			          str.data = copytext;
			                    trans.setTransferData("text/unicode",str,copytext.length*2);
			         var clipid = Components.interfaces.nsIClipboard;
			          if (!clip)
			               return false;
			          clip.setData(trans,null,clipid.kGlobalClipboard);
			          alert("Successfully copied！");
			     }
			};

			$('#m').click(function(){
				var mobile = $('#mobile').val();
				var partten = /^1[3,5,8]\d{9}$/;
				if(!partten.test(mobile)){
					alert('Please write correct mobile numbers');
					return false;
				}
				$.ajax({
					url:'/Login/updatemobile',
					type:'POST',
					dataType:'json',
					data:'mobile='+mobile,
					beforeSend:function(){
						$('#m').val('Processing...').attr('disabled','true');
					},
					success:function(data){
						if(data==3){
							alert('Please write correct mobile numbers');
						}else if(data ==1){
							alert('Your mobile number has been saved');
						}else if(data == 2){
							alert("We've encountered problem while saving your number, please try again later");
						}
						$('#m').val('Sending').removeAttr('disabled');
					}
				});
			});
			$('#amount').keyup(function(event){
				var k = event.keyCode;
					if(!(k==8 || k==9 || k==13 || k==16 || k>=33 && k<=40 || k==45 || k==46 || k>=48 && k<=57 || k>=96 && k<=105))
					{
						$(this).val('');
					}else{
						check_amount(this);
					}
			});
			check_amount = function(item){
				var amount = $(item).val();
				if(amount == 0){
					alert('The minimum amount is 1');
					$(item).val('1').select();
					return ;
				}
				var goods_id = $('#deal_id').val();
				var price = parseFloat($('#price').text().replace(/^[^\d.]*/,''));
				var d_f = parseFloat($('#d_f').text().replace(/^[^\d.]*/,''));
				var buy = 'pay now';
				$.ajax({
					url:'/beijing/deal/check_quantity/' + goods_id,
					type:'GET',
					dataType:'json',
					data: {amount:amount},
					beforeSend:function(){$('#grep_ok').val('Processing...').attr('disabled','true');},
					success:function(data){
						var money = '';
						if(data.code == 1){
							money = (amount*price).toFixed(2);
							var money1 = (amount*price+d_f).toFixed(2);
							$('#total_fee').text(money);
							$('#t_f').text(money1);
							$('#grep_ok').removeAttr('disabled').val(buy);
						}else if(data.code == 2){
							alert('Each user can only buy'+data.amount+'units');
							$('#amount').empty().val(data.amount).select();
							$('#grep_ok').removeAttr('disabled').val(buy);
						}else if(data.code == 3){
							alert('There are only '+data.amount+' products available');
							$('#amount').empty().val(data.amount).select();
						    $('#grep_ok').removeAttr('disabled').val(buy);
						}else if(data.code == 6){
							alert(data.message);
							$('#grep_ok').val('Sold out');
							return;
						}else if(data==404){
							window.location.href='http://www.cityweekend.com.cn/signin/?next=http://fb.cityweekend.com.cn/';
							return;
						}
					}
				});
			}
			$('#signup').click(function(){
				var email = $('#sign_up_email').val();
				var sign = $(this).val();
				var partten = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/;
				if(!partten.test(email)){
					alert('Please write the correct email');
					$('#sign_up_email').val('');
				}else{
					$.ajax({
						url:'/subscribe/signup',
						type:'POST',
						dataType:'text',
						data:'email='+email,
						success:function(data){
								alert(data);
								$('#sign_up_email').val('');
						}
					});
				}
				return false;
			});
})

timeformat = function(num){
	return num < 10 ? '0' + num : num;
}
