var common = function(){

	var urllink = location.toString();

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

	var obj = Ext.urlDecode(urllink.substring(urllink.lastIndexOf('?')+1, urllink.length));

	var tf = ['Keyword'];

	var winBox = null;
	
	var carousel, shelter;
	
	var papaH = Array();
	

	function changeToExtField(fields){

		var text = (Ext.isIE) ? fields.bvalue : getAbrite(fields, 'bvalue');

		var type = (fields.type == 'text') ? 'TextField' : 'TextArea';

		var indexField = new Ext.form[type]({

			emptyClass: '', 

			emptyText: text, 

			fieldClass: '',

			focusClass: '',

			applyTo: fields.id

		});

		indexField.getEl().removeClass('x-form-text');

		indexField.getEl().removeClass('x-form-empty-field');

	}

	

	function getTemplate(xtpl){

		var word = (Ext.getDom(xtpl)) ? Ext.getDom(xtpl).innerHTML : 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(){
			
			
			var papa = Ext.query('div[ID^=menuPaPa]');
				for(i=0;i<papa.length;i++){
					papaH[i+1] = Ext.get(papa[i]).getHeight();
				}
			this.hidePaPa();
			var cateTest = urllink.indexOf('CategoryID=');
			if(cateTest!=-1) {
				var nowCate = urllink.substring(urllink.indexOf('CategoryID=')+12,urllink.indexOf('&KindID')-1);
				this.dropMenu(nowCate);
			}
			

			for(x in tf){

				if(!Ext.get(tf[x])) continue;

				var f = new Ext.form.TextField({emptyText: Ext.getDom(tf[x]).value, 

					emptyClass: '', fieldClass: '', focusClass: '', applyTo: tf[x] });

				f.getEl().removeClass('x-form-text').removeClass('x-form-empty-field');

				var btn = Ext.get(tf[x]).parent('div').child('div[class^=searchgo_PNG]', true);

				if(btn) Ext.get(tf[x]).addKeyListener({key:13}, btn.onclick);

			}

		}, 
		
		hidePaPa: function() {
			var display = Ext.query('div[ID^=menulist]');
			for(i=0;i<display.length;i++){
				Ext.get(display[i]).hide();
			}
			var papa = Ext.query('div[ID^=menuPaPa]');
			for(i=0;i<papa.length;i++){
				
				Ext.getDom(papa[i]).style.height = 0;
			}
		},
		
		dropMenu: function(x) {
			
			if(x==this.currentMenu) return;
			this.currentMenu = x;
			this.hidePaPa();
			if(Ext.get('menuPaPa'+x)){
				var list = Ext.get('menuPaPa'+x);
				i = papaH[x]/10;
				LH = 0;
				task = new Ext.util.DelayedTask(function(){
						  if(LH < papaH[x]) {
							  LH = LH+i;
							  list.dom.style.height = LH+'px';
							  task.delay(10);
						  } else {
							  var papaSon = list.query('div[ID^=menulist]');
							  for(i=0;i<papaSon.length;i++){
								  Ext.get(papaSon[i]).show();
							  }
						  }
					  });
				task.delay(10);
			}
		},


		verifyCode: function(){

//			var vnum = Ext.getDom('Verify').value;
//
//			var cb = function(rspn, option){
//
//				if(!eval(rspn.responseText)){
//
//					alert('Verify Code Error');
//
//					Ext.getDom('Verify').select();
//
//					return;
//
//				}
//
				Ext.getDom('request').submit();
//
//			}
//
//			var cn = new Ext.data.Connection({url:'getdata.php?Type=Verify'}).request({
//
//				scope: this, success: cb, params: {'paramID': vnum}
//
//			});

		},

		

		createMsg: function(type, todo){

			var cb = function(rspn, option){

				this.backupMsg = getTemplate(rspn.responseText);

				if(todo) eval(todo);

			}; 

			var cn = new Ext.data.Connection({url:'getdata.php?Type='+type}).request({

				scope: this, success: cb

			});

		}, 

		

		createWinMsg: function(html){

			winBox = new Ext.Window({

    			modal: true, baseCls: '', headerAsText: false, html: html, closeAction: 'hide', closable: false, 

    			resizable: false, shim: false, shadow: false, layout:'fit', closeAction:'hide', animate: true

			});

			winBox.on('show', function(p){winBox.center()});

		}, 

		

		addInquiry: function(uid, name){

			Ext.MessageBox.confirm('Confirm','Thank, Will be <strong><font color="#f40000">'+name+'</font></strong> into your inquiry list !',YNbtn)

			function YNbtn(btn) {

				(btn=='yes') ? common.clickBtn('YES', uid) : common.clickBtn('NO');

			}

			//var tpl = new Ext.XTemplate(this.backupMsg);

			//var word = tpl.apply({'Name': name, 'YES': "common.clickBtn('YES', "+uid+")", 'NO': "common.clickBtn('NO')"});

			//(!winBox) ? this.createWinMsg(word) : winBox.body.update(word);

			//winBox.show();

		}, 

		

		delInquiry: function(box, uid){

			var name = Ext.get(box).parent('div').parent('div').child('span', true).innerHTML;

			Ext.MessageBox.confirm('Confirm','Do you want delete <strong><font color="#f40000">'+name+'</font></strong> into your inquiry list ?',YNbtn)

			function YNbtn(btn) {

				(btn=='yes') ? common.clickBtn('DEL', uid) : common.clickBtn('NO');

			}

			

			//var tpl = new Ext.XTemplate(this.backupMsg);

			//var word = tpl.apply({'Name': name, 'YES': "common.clickBtn('DEL', "+uid+")", 'NO': "common.clickBtn('NO')"});

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

			//if(!winBox) this.createWinMsg(word); else winBox.body.update(word);

			//winBox.show();

		}, 

		

		showAlert: function(name, target){

			var loc = (target ? "location.href = '"+target+"'" : "common.clickBtn('NO')");

			var tpl = new Ext.XTemplate(this.alertMsg);

			var word = tpl.apply({'Name': name, 'YES': loc});

			if(!winBox) this.createWinMsg(word); else winBox.body.update(word);

			winBox.show();

		},
		
		addShelter: function(img){
			shelter = Ext.DomHelper.append(Ext.getBody(), {tag: 'img', src: img}, true);
			shelter.setStyle({'z-index': '1000', 'cursor': 'pointer'});
			shelter.anchorTo('bar1', 'c-c');
		},
		
		moveTo: function(area){
			if(carousel.moving) return;
			var num = parseInt(area.id.replace('bar', ''));
			//shelter.anchorTo(area.id, 'c-c', [0, 0], true);
			carousel.moveTo(num);
		},

		
		keyPass: function() {
			var cb = function(rspn, option){
				if (rspn.responseText=='pass') {
					
						location.href='products.php?type=NP';
						
					} else {
						Ext.Msg.show({
						title: 'Notice',
						msg: "Welcome to Mightyjaw'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.",
						width: 400,
						buttons: {ok:'Login', cancel:'Cancel'},
						prompt: true,
						fn: this.passLogin,
						icon: Ext.MessageBox.INFO
						});
					}
				}  
			var cn = new Ext.data.Connection({url:'login.php'}).request({
				scope: this, method: 'POST', success: cb
			});
		},
		
		passLogin: function(btn,text) {
			if(btn=='ok') {
				var cb = function(rspn, option){
					if (rspn.responseText=='true') {
						
						location.href='products.php?type=NP';
						
					} else {
						
						Ext.Msg.show({title:'Error',msg:'Password incorrect!', buttons: {ok:'OK'}, fn:function(btn){ location.href='index.php'; } });
					}
				}  
				var cn = new Ext.data.Connection({url:'login.php'}).request({
					scope: this, method: 'POST', params: {'paramID': text},success: cb
				});
			} else {
				location.href='index.php';
			}
		},

		clickBtn: function(btn, uid, name){

			switch(btn){

				case 'YES':

					if(!uid) break;

//					var cb = function(rspn, option){alert(rspn.responseText);}  

					var cn = new Ext.data.Connection({url:'getdata.php?Type=addInquiry'}).request({

						scope: this, method: 'POST', params: {'paramID': uid}

//						success: cb

					});

					break;

					

				case 'DEL':

					if(!uid) break;

					var cb = function(rspn, option){

						var pdt = Ext.get(this.targetBox.box).parent('div').parent('div[class^=inquirylist01]');

						pdt.slideOut("b", {remove: true, callback: function(){

							pdt.remove();

							Ext.getDom('inqiryCount').value = parseInt(Ext.getDom('inqiryCount').value)-1;

							if(Ext.getDom('inqiryCount').value == 0) 

								Ext.MessageBox.alert('Notice','Sorry, you are not adding products to inquiry list !',YNbtn)

								function YNbtn(btn) {

									(btn=='ok') ? location.href = 'products.php' : common.clickBtn('NO');

								}

						}, scope: this, block: true});

					}

					var cn = new Ext.data.Connection({url:'getdata.php?Type=delInquiry'}).request({

						scope: this, method: 'POST', params: {'paramID': uid}, success: cb

					});

					break;

					

				default:

			}

			//winBox.hide();

		}, 

		

		linkInquiry: function(target){
			var cb = function(rspn, option){
				if(eval(rspn.responseText) == 'true'){
					if(!target) location.href = 'inquiry.php';

				}else{
					Ext.MessageBox.alert('Notice','Sorry, you are not adding products to inquiry list !')

				}

			}

			var cn = new Ext.data.Connection({url:'getdata.php?Type=isInquiry'}).request({scope: this, success: cb});

		}, 
		
		
		gotosearch: function(target){

			var keyword = Ext.getDom(target).value.trim();

			if (Ext.getDom(target).value.trim() == ''){

				Ext.MessageBox.alert('Notice','Please Enter Keyword!!');

			}else{

				var cb = function(rspn, option){

					if(eval(rspn.responseText) == 'true'){

						var strURL="products.php?type=SRH&Keyword="+keyword.trim();

						location.href=strURL;

					}else{

						Ext.MessageBox.alert('Notice','Did not find any information');

					}

				}

				var cn = new Ext.data.Connection({url:'getdata.php?Type=Search'}).request({

					scope: this, method: 'POST', params: {'paramID': keyword}, success: cb

				});

			}

		}

	}

}();



Ext.onReady(common.init, common);
