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;

	

	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, 

		

		init: function(){

			

			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);

			}

			switch(caseType){

				case 'products':

					if(obj.Type != 'more') break;

					var pic = Ext.query('a[rel=lightbox]');

					if(!pic.length) Ext.get('Brief').removeClass('productsinfo00');

					this.createMsg('inquiryMsg');

					break;

					

				case 'inquiry':

					this.createMsg('deleteMsg', "this.linkInquiry('products.php')");

			}

			var cb = function(rspn, option){this.alertMsg = getTemplate(rspn.responseText)}; 

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

				scope: this, success: cb

			});

		}, 

		

		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();

		},
		
		keyPass: function(x) {
			this.loginNum=x;
			var cb = function(rspn, option){
				if (rspn.responseText=='pass') {
						if(x==1) {
							location.href='newproducts.php';
						} else {
							location.href='catalogs.php';
						}
					} else {
						Ext.Msg.show({
						title: 'Notice',
						msg: "Welcome to Iroda's website. If you would like to view our new products or download our catalogs, 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', params: {'type': x}, success: cb
			});
		},
		
		passLogin: function(btn,text) {
			if(btn=='ok') {
				var cb = function(rspn, option){
					if (rspn.responseText=='true') {
						if(common.loginNum==1) {
							location.href='newproducts.php';
						} else {
							location.href='catalogs.php';
						}
					} 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, 'type': common.loginNum},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^=newproducts00]');

						pdt.frame('df0000', 1, {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 = 'newproducts.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 = 'productsinquiry.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() == 'Please Enter Keyword'){

				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);
