﻿cj.evt.add(window, 'load', function(){
	var mrf = document.getElementById('member_regist_form');
	if(mrf){
		mrf.onsubmit = function(){
			if(!cj.form.chk(this, ['email,email', 'passwd,password', 'company,text', 'department,text', 'en_name,text', 'job_title,text', 'phone,text'])){
				return false;
			}
			return true;
		}
	}
	var mlf = document.getElementById('member_login_form');
	if(mlf){
		mlf.onsubmit = function(){
			if(!cj.form.chk(this, ['account,email', 'passwd,text'])){
				return false;
			}
			return true;
		}
	}
	var mff = document.getElementById('member_forget_form');
	if(mff){
		mff.onsubmit = function(){
			if(!cj.form.chk(this, ['account,email'])){
				return false;
			}
			return true;
		}
	}
	var mmf = document.getElementById('member_modify_form');
	if(mmf){
		mmf.chgpass.onclick = cj.fn.chgpass_click;
		mmf.onsubmit = function(){
			if(!cj.form.chk(this, ['email,email', 'passwd,password', 'company,text', 'department,text', 'en_name,text', 'job_title,text', 'phone,text'])){
				return false;
			}
			if(this.chgpass.checked){
				return cj.form.chk(this, ['passwd1,password']);
			}
			return true;
		}
	}
	var cf = document.getElementById('contact_form');
	if(cf){
		cf.onsubmit = function(){
			if(!cj.form.chk(this, ['en_name,text', 'email,email'])){
				return false;
			}
			return true;
		}
	}
	var i, j, n, set, src;
	var imgs = document.getElementsByTagName('img');
	for(var i=0; i<imgs.length; i++){
		n = imgs[i];
		if(!n.className || !/\{.*\}/.test(n.className)) continue;
		try{ set = eval('('+n.className+')')}catch(e){};
		if(typeof set != 'object' || !set.src) continue;
		n.style.cursor = 'pointer';
		n.img = cj.util.createElm('img', {});
		n.img.src = set.src;
		n.onclick = function(){
			cj.popup.popImg(this.img);
		}
	}
	var fgs = document.getElementById('fGoogleSearch');
	if(fgs){
		fgs.onsubmit = function(){
			if(!cj.form.chk(this, ['q,text'])){
				return false;
			}
			if(this.rwww.checked){
				this.cx.value = "004712389762011729978:3yvey-queiq";
			}
			return true;
		}
	}
	//inv_finance_report
	(function(){
		var n = document.getElementById('reportHolder');
		if(!n) return;
		cj.evt.add(n, 'click',function(e){
			var e = e || window.event, tg = e.srcElement || e.target;
			if(tg.className=='download'){
				try{
					tg.set = eval(tg.getAttribute('attrib'));
					if(!cj.ifr) cj.ifr = cj.util.addElm(document.body, {
						tag:'iframe'
						,cssText:'position:absolute;top:-1000px;left:-1000px;display:none'
						//,cssText:'position:absolute;top:100px;left:100px;display:block; width:500px; height:300px'
						,attrib:'frameBorder:0,src:javascript;""'
					});
					cj.ifr.setAttribute('src', '../script/dl.php?a=inv_report&id='+tg.set.id);
				}catch(e){};
			}
		});
		var tab = document.getElementById('tabSeletor');
		tab.lis = tab.getElementsByTagName('li');
		tab.tables = n.getElementsByTagName('table');
		var i=0;
		cj.util.each(tab.getElementsByTagName('a'),function(a){
			a.index = i++;
		});
		cj.evt.add(tab,'click',function(e){
			var e = e || window.event, tg = e.srcElement || e.target;
			if(tg.tagName=='a'||tg.tagName=='A'){
				for(var i=0; i<3; i++){
					tab.lis[i*2].style.display = (tg.index==i) ? 'block' : 'none';
					tab.lis[i*2+1].style.display = (tg.index==i) ? 'none' : 'block';
					tab.tables[i].style.display = (i==tg.index) ? 'block' : 'none';
					tab.tables[i].style.width = 500 + 'px';
				}
			}
		});
	})();

	//inv_download
	(function(){
		cj.evt.add(document,'click',function(e){
			var e = e || window.event, tg = e.srcElement || e.target;
			if(tg.className=='inv_download'){
				try{
					tg.set = eval(tg.getAttribute('attrib'));
					if(!cj.ifr) cj.ifr = cj.util.addElm(document.body, {
						tag:'iframe'
						,cssText:'position:absolute;top:-1000px;left:-1000px;display:none'
						//,cssText:'position:absolute;top:100px;left:100px;display:block; width:500px; height:300px'
						,attrib:'frameBorder:0,src:javascript;""'
					});
					cj.ifr.setAttribute('src', '../script/dl.php?a=inv_download&id='+tg.set.id);
				}catch(e){};
			}
			if(tg.className=='fdl'){
				try{
					tg.set = eval(tg.getAttribute('attrib'));
					if(!cj.ifr) cj.ifr = cj.util.addElm(document.body, {
						tag:'iframe'
						,cssText:'position:absolute;top:-1000px;left:-1000px;display:none'
						//,cssText:'position:absolute;top:100px;left:100px;display:block; width:500px; height:300px'
						,attrib:'frameBorder:0,src:javascript;""'
					});
					cj.ifr.setAttribute('src', '../script/dl.php?a=fdl&fid='+tg.set.fid);
				}catch(e){};
			}
		});
	})();

	//inv_stock_report
	(function(){
		var tab = document.getElementById('stock_report_tabselector');
		if(!tab) return;
		tab.lis = tab.getElementsByTagName('li');
		tab.tables = document.getElementById('stock_report_holder').getElementsByTagName('table');
		var i=0;
		cj.util.each(tab.getElementsByTagName('a'),function(a){
			a.index = i++;
		});
		cj.evt.add(tab,'click',function(e){
			var e = e || window.event, tg = e.srcElement || e.target;
			if(tg.tagName=='a'||tg.tagName=='A'){
				for(var i=0; i<2; i++){
					tab.lis[i*2].style.display = (tg.index==i) ? 'block' : 'none';
					tab.lis[i*2+1].style.display = (tg.index==i) ? 'none' : 'block';
					tab.tables[i].style.display = (i==tg.index) ? 'block' : 'none';
					tab.tables[i].style.width = 500 + 'px';
				}
			}
		});
	})();
});
cj.fn = {
	confirm_logout : function(){
		var c = confirm('Confirm to logout?');
		if (c){
			window.location = 'member.php?act=logout';
			return true;
		}else{
			return false;
		}
	},
	chgpass_click : function(){
		cj.fn.set_visible(this.checked);
	},
	set_visible : function(t){
		var trs = document.getElementsByTagName('tr');
		for(i=0; i<trs.length; i++){
			if(trs[i].getAttribute('attrib') && trs[i].getAttribute('attrib')=='chgpass'){
				if(document.all){
					trs[i].style.display = (t) ? 'block' : 'none';
				}else{
					trs[i].style.display = (t) ? 'table-row' : 'none';
				}
			}
		}
	}
}
