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 'npinfo':
				case 'productsinfo':
					$('#addinq').show();
				
				case 'nindex':	
				case 'products':
				
					$('a[href=nindex.php]').animate({backgroundColor: '#fff', color: '#ff0000'}, 1000);
					break;
				
				case 'vpinfo':
				case 'vproductsinfo':
					$('#addinq').show();
				case 'vindex':
				case 'vproducts':
					$('a[href=#check]').animate({backgroundColor: '#fff', color: '#ff0000'}, 1000);
					break;
				case 'contact':
					$('a[href=contact.php]').animate({backgroundColor: '#fff', color: '#ff0000'}, 1000);
					break;
				case 'catalog':
					$('a[href=catalog.php]').animate({backgroundColor: '#fff', color: '#ff0000'}, 1000);
					break;
					
				
			}

		}, 
		
		dropmenu: function(arg) {
			$('.menu_data2').hide('slow');
			$(arg).parent().parent().children(".menu_data2").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: {}
			});
		},
		
		login: function() {
			var param1 = $('#Username').val();
			var param2 = $('#Pswd').val();
			$.ajax({
				url: 'login.php',
				type: 'POST',
				data: {Username: param1, Pswd: param2},
				error: function() {	alert('Ajax Error');	},
				success: function(rspn) { 
					if(rspn=='true') {
						location.reload();
					} else {
						common.msg('Notice','Username or Password error, please input right data!');
					}
				}
			});
		},
		
		

		
		keyPass: function() {
			
			$.ajax({
				url: 'login.php?Type=Browse',
				type: 'POST',
				error: function() {	alert('Ajax Error');	},
				success: function(rspn) { 
					if(rspn=='pass') {
						location.href = 'vindex.php';
					} else {
						$('#dialog').dialog("destroy");
						$('#dialog').attr('title','Confirm');
						$('#dialog > p').html("Welcome to Geowide's website. If you would like to browse our new products, please send in your company information and we will email the  password to you ASAP. Thank you very much.");
						$('#dialog').dialog({
							resizable: false,
							show: 'blind',
							hide: 'fold',
							buttons: {
								'Register Now!': function() {
									location.href='contact.php';
									$(this).dialog('close');
								},
								'No, thanks!': function() {
									$(this).dialog('close');
								}
							}
						});
					}
				}
			});

		},

		addInquiry: function(tbName, 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, tbName);
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
					}
				}
			});
			
		}, 

		

		delInquiry: function(box, name, uid, tbName){
			$('#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, tbName);
						$(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, tbName: name}
					});

					break;

					

				case 'DEL':

					if(!uid) break;
					
					$.ajax({
						url: 'getdata.php?Type=delInquiry',
						type: 'POST',
						data: {paramID: uid, tbName: name},
						success: function(rspn) { 
							var pdt = $(common.targetBox.box).parent().parent().parent().parent();
							
							pdt.hide("slow", 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:

			}


		}, 

		

		
		
		
		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(rspn) {	alert('Ajax Error');	},
					success: function(rspn) { 
						if(rspn=='true') {
							linkPage = $('#vip').attr('class')=='Y' ? "vproducts.php" : 'products.php' ;
							location.href="products.php?act=SRH&Keyword="+keyword;
						} else {
							common.msg('Notice','Did not find any information!');
						}
					}
				});
			}

		}

	}

}();



$(common.init);
