var common = function(){

	var urllink = location.toString();

	var caseType = urllink.substring(urllink.lastIndexOf('\/')+1, urllink.lastIndexOf('.'));


	return {

		indexPoint: 0, 
		currentMenu: 0,
		
		
		init: function(){

			$('body').append('<div id="dialog" title="none" style="display:none"><p>empty</p></div>');
			
		},
		

		msg: function(t,p) {
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title',t);
			$('#dialog > p').html(p);
			
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {}
			});
		},
		
		getPageData: function(TID, IPP, NOWP, GOP, Target, Flag) {
			var myTPL = '<div class="switch_item"><a href="#none"><div class="switch_item_box" onclick="location.href=\'productsinfo.php?TypeID=${TypeID}&CateID=${CategoryID}&ID=${ID}\'"><img src="${Img}" width="159" height="125" /></div></a><div class="p_name"><span class="p_name_tag"><a href="productsinfo.php?TypeID=${TypeID}&CateID=${CategoryID}&ID=${ID}">${ProductName}</a></span></div></div>';
			
			$.ajax({
			  dataType: "json",
			  url: 'rtntpl.php',
			  type: 'POST',
			  data: {TopID: TID, ipp:IPP, nowp: NOWP, gop:GOP},
			  success: function(rspn){
				  if(rspn.State!='false') {
					  //alert(rspn.Items);
					  $('#'+Target).fadeOut(250, function(){
						 $('#'+Target).empty();
						 $.tmpl(myTPL, rspn.Items ).appendTo('#'+Target);
					  });
					  $('#'+Target).fadeIn(250);
					  $('#'+Flag).val(rspn.State);
					  
				  }
			  },
			  error: function(rspn) { alert(rspn); }
			});
			
		},
		
		login: function() {
			var param1 = $('#Username').val();
			var param2 = $('#Pswd').val();
			$.ajax({
				url: 'getdata.php?Type=Login',
				dataType: 'json',
				type: 'POST',
				data: {paramID:[param1, param2]},
				success: function(rspn) { 
					if(rspn=='true') {
						location.href = 'member.php';
					} else {
						common.msg('通知','帳號/密碼錯誤或帳號尚未啟動!');
					}
				}
			});
		},
		

		addInquiry: function(uid, name){
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title','Confirm');
			$('#dialog > p').html('Thank, Will be <strong><font color="#f40000">'+name+'</font></strong> into your inquiry list !');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('YES', uid);
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			
		},
		
		updateCart: function(box,uid){
			common.targetBox = {'num': box};
			common.clickBtn('UPD', uid);
			
		}, 

		

		delInquiry: function(box, name, uid){
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title','Confirm');
			$('#dialog > p').html('Do you want delete <strong><font color="#f40000">'+name+'</font></strong> into your inquiry list ?');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('DEL', uid);
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});

			common.targetBox = {'box': box};

		}, 

		linkInquiry: function(target){
			$.ajax({
				url: 'getdata.php?Type=isInquiry',
				type: 'POST',
				dataType: 'json',
				success: function(rspn) { 
					if(rspn=='true') {
						location.href = 'inquiry.php';
					} else {
						common.msg('NOTICE','Sorry, you are not adding products to inquiry list !');
					}
				}			
			});
		}, 
		
		

		clickBtn: function(btn, uid, name){

			switch(btn){

				case 'YES':

					if(!uid) break;
					
					$.ajax({
						url: 'getdata.php?Type=addInquiry',
						type: 'POST',
						data: {paramID: uid}
					});

					break;

					

				case 'DEL':

					if(!uid) break;
					$.ajax({
						url: 'getdata.php?Type=delInquiry',
						type: 'POST',
						data: {paramID: uid},
						success: function(rspn) { 
							var pdt = $(common.targetBox.box).parent().parent();
							
							pdt.hide("Blind", function(){

								pdt.remove();

								$('#inqiryCount').val(parseInt($('#inqiryCount').val())-1);

								if($('#inqiryCount').val() == 0)  {
									//$('#dialog').dialog("destroy");
									$('#dialog').attr('title','Notice');
									$('#dialog > p').html('Sorry, you are not adding products to inquiry list !');
									$('#dialog').dialog({
										resizable: false,
										buttons: {
											OK: function() {
												location.href = 'index.php';
											}
										}
									});
									
								}

							});
						}
					});


					break;
					
				case 'UPD':

					if(!uid) break;
					var nums = $(common.targetBox.num).parent().parent().find("input[name='num']").val();
					
					$.ajax({
						url: 'getdata.php?Type=updateCart',
						type: 'POST',
						data: {paramID: uid, num: nums},
						success: function() {
							location.reload();
						}
					});
	
					break;

				default:

			}


		}, 
	
		
		gotosearch: function(target){

			var keyword = $('#'+target).val();
			
			if (keyword == ''){

				common.msg('NOTICE','Please Enter Keyword!!');

			}else{
				$.ajax({
					url: 'getdata.php?Type=Search',
					type: 'POST',
					dataType: 'json',
					data: {paramID: keyword},
					error: function() {	alert('Ajax Error');	},
					success: function(rspn) { 
						if(rspn=='true') {
							location.href="products.php?act=SRH&Keyword="+keyword;
						} else {
							common.msg('NOTICE','No any match products!');
						}
					}
				});
			}

		},
		
		godrop: function() {
			if($('#cataDrop').val()!='') {
				location.href = 'products.php?TypeID='+$('#cataDrop').val();	
			};
		}
		

	}

}();



$(common.init);
