var common = function(){

	var urllink = location.toString();

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


	

	function getTemplate(xtpl){

		var word = ($('#'+xtpl).length>0) ? $('#'+xtpl).html() : xtpl;

		word = word.replace(/\%7B/g, '{').replace(/\%7D/g, '}');

		return word.replace(/TPL/g, 'tpl').replace(/ > /g, ' &gt; ').replace(/ < /g, ' &lt; ');

	}

	

	return {

		indexPoint: 0, 
		currentMenu: 0,
		
		
		init: function(){
			switch(caseType) {
				case 'productsinfo':
					$('#addinq').show();
			}
		}, 
		
		dropmenu: function(arg) {
			$('.dm').hide('slow');
			$(arg).parent().next("div").show('slow');
			
		},
		
		msg: function(t,p) {
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title',t);
			$('#dialog > p').html(p);
			
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {}
			});
		},

		addInquiry: function(uid, name){
			//$('#dialog').dialog("destroy");
			$('#dialog').attr('title','Confirm');
			$('#dialog > p').html('Thank you. '+name+' will be add to your inquiry list.');
			$('#dialog').dialog({
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Yes': function() {
						common.clickBtn('YES', uid);
						$(this).dialog('close');
					},
					'No': function() {
						$(this).dialog('close');
					}
				}
			});
			
		}, 

		

		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',
				error: function() {	alert('Ajax Error');	},
				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().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;

				default:

			}


		}, 

		

		KeyPass: function() {
			$('#dialog').attr('title','Please insert your password');
			$('#dialog > p').html("<input type='password' id='pswd' size='15'>");
			$('#dialog').dialog({
				modal: true,
				resizable: false,
				show: 'blind',
				hide: 'fold',
				buttons: {
					'Submit!': function() {
						var pswd = $('#pswd').val();
						if(pswd!='') {
							$.ajax({
								url: 'getdata.php?Type=Download',
								type: 'POST',
								dataType: 'json',
								data: {paramID: pswd},
								error: function() {	alert('Ajax Error');	},
								success: function(rspn) { 
									if(rspn=='true') {
										location.href="products.php?act=NEW";
										$(this).dialog('close');
									} else {
										common.msg('Sorry','Wrong Password?');
									}
								}
							});
							
						} else {
							common.msg('Notice','Please input Password!');
						}
					},
					'Cancel': function() {
						$(this).dialog('close');
					}
				}
			});
		},
		
		
		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','Did not find any information!');
						}
					}
				});
			}

		}

	}

}();



$(common.init);
