var $sys;
var $db=0;
var $cal=false;
var $itg=false;
var $ctl=false;
var $cur_dat=false;
var $prev_status;
var $computer=false;
var $ori_cursor;
var $usrn,$lng,p_h;

document.onclick=page_click;
document.onkeypress=page_key;
function product_info(act,v){
	if(!act){
		qjax(0,'sku='+v)
	}else if(act=='rsp'){
		alerT(v,0,0,'OK');
	}		
}
function page_click(){	
  var e=arguments[0] || window.event;
	var targ=e.target || e.srcElement;
	if($ie6 || $ie7){
		if(targ.tagName=='IMG'){
			for(i=0;i<5;i++){
				targ=targ.parentNode;
				if(targ){
					if(targ.tagName=='A'){				
						if(targ.href){
							window.location=targ.href;
							break;
						}
					}
				}
			}
		}
	}
	if(e.altKey && targ){
		var sku=targ.parentNode.innerHTML;
		if(sku.indexOf('images/pdt/')){
			e=false;
			product_info(0,rip(sku,'images/pdt/','.jpg'));
			return false;
		}
	}
	//var c=targ.getAttribute('cgo');
	//if(c){
	//	window.location.href="http://"+$hs+"i.php?pg=shop&c="+c;
	//}
	if(!targ.attributes['ctl']){
		if(targ.attributes['qali']){
			qal('eval',targ,$itg);
		}else{
			ctl_clear(); 
		}
	}	
	var id=targ.id;
	if(id.substring(0,1)=='¢' || id.substring(0,1)=='@'){
		var ix=id.substring(2,5);
		switch(ix){
			case 'cel':js_cell(targ);break;
			case 'qal':break;
			case 'cal':
				$ctl=ge('cal');
				$ctl.style.display='block';
				if(targ.getAttribute('ccu')==1){
					targ=ge("¢_ccu."+id.substring(6,id.length));
				}else{
					targ=ge(id.substring(6,id.length));
				}			
				qal('show',targ);			
				$cur_dat=targ.value;
				$itg=targ;
				break;					
			default:eval('click_'+ix+'(targ)');
		}
	}
}
function ctl_clear(){
	if($ctl){	
		if($ctl.id=='numpad'){if($prev_val!=$itg.value){fire_event($itg,'change');}}
		$ctl.style.display='none';
		$ctl=false;
	}
}
function sel_clear(){
	$sel=false;
}
function frm_dirty(){
	return false;
}
function page_key(e){
	var e=(e) ? e: ((event) ? event : null);
	var node = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
	if(e.keyCode==13){
		if((node.type=='text') && (node.getAttribute('notab')!=1)){ 
			var gof=node.getAttribute('gof');
			if(gof){
				eval(gof);return false;
			}else{	
				//next_focus(node.id,node); return false; 
			}
		}
	}
}

function db_val(val,typ,jsn,targ){
	if(!val){
		val='';
	}else{
		if(typeof(val)=='object'){
			if(typ!='z'){
				typ=val.getAttribute('typ');
				if(typ=='x'){typ=val.getAttribute('val_typ');typ=typ?typ:'x';}
			}
			val=val.value;
		}
	}
	var null_val='Null';
	if(typ.substring(1,2)=='0'){
		typ=typ.substring(0,1);
		null_val=0;
	}else if(typ.substring(1,2)=='!'){
		typ=typ.substring(0,1);
		null_val='';
	}
	if(typ=='n'){ //number
		if(val.length==0){
			val=null_val;
		}else if(val==0){
		}else if(!is_numeric(val)){
			val=null_val;
		}else{
			val=val*1;
		}
	}else if(typ=='d'){ //date
		if(val.length==0){val=null_val;}else{val=date_format(val);}
	}else if(typ=='h'){ //time
		if(val.length==0){val=null_val;}else{val="'" + time_format(val) + "'";}
	}else if(typ=='t'){ //t=text			
		if(val.length==0){
			val=null_val;
		}else{
			val=val.replace(/\r/g, ''); //to get rid of extra return in IE
			val="'" + escape(addslashes(val)) + "'";
			if(jsn){
				val=val.replace(/%5C%27/g, '%5C%5C%27'); //escape escape for json
			}
		}
	}else if(typ=='b'){ //b=binary
		if(val.length==0){val=0;}else{val=val*1;}
	}else if(typ=='l'){ //list
		if(val.length==0){val=null_val;}else{val=val*1;}		
	}else if(typ=='v'){ //vlu numeric
		val=targ.getAttribute('vlu');
		if(val.length==0){val=null_val;}else{val=val;}
	}else if(typ=='z'){ //pass through		
	}else{
		val='!@!';				
	}		
	return val;
}

//DRAGGING
var $drag=false;
var $drag_block=false;
function drag_select(e,id){
	if($drag_block){$drag_block=false;return false;}
	var htype='move';
	if(e==null){e= window.event;htype='move';} 
	if(id){
		var targ=ge(id);
	}else{
		var targ=e.target!=null?e.target:e.srcElement;
	}
	$ori_cursor=targ.style.cursor;
	$drag=targ;       
	drag_ini();
	var p=f_pos($drag);
	$drag.style.cursor=htype;

	drag_x=e.clientX-p[0];
	drag_y=e.clientY-p[1];

	document.onmousemove=drag_move;
	document.onmouseup=drag_end;
	return false;
}

function drag_ini(){
	var ori_cursor=null;
	var drag_x=0;
	var drag_y=0;
}
function drag_move(e){
	if (e == null) { e = window.event } 
	if (e.button<=1 && $drag){
		$drag.style.left=e.clientX-drag_x+'px';
		$drag.style.top =e.clientY-drag_y+'px';
		return false;
	}
}
function drag_end(e) {
	document.onmousemove=null;
	document.onmouseup=null;
	$drag.style.cursor=$ori_cursor;
	$drag=false;
}

function product_u(act,v){
	if(!act){
		qjax(0,array_to_post(ge(['i_pid','i_stock','i_rank'])));
	}else{
		alerT(0);if(v){alerT('Update successful.',0,0,'OK');}else{alerT('Update failed.',0,0,'OK');}
	}
}


//MISC
function computer(){return false;}
function my_account(){window.location.href="http"+$s+"://"+$hs+"i.php?pg=account";}
function my_adm(){window.location.href="http"+$s+"://"+$hs+"i.php?pg=_adm";}
