var counter = 0;
function fetch_id(){
	counter++;
	return counter;
}

function urlencode(D){
	D = escape(D.toString()).replace(/\+/g, "%2B");
	var B = D.match(/(%([0-9A-F]{2}))/gi);
	if(B){
		for(var C = 0; C < B.length; C++){
			var A = B[C].substring(1, 3);
			if(parseInt(A, 16) >= 128){
				D = D.replace(B[C], "%u00" + A);
			}
		}
	}
	D = D.replace("%25", "%u0025");
	return D;
}

function addStyles(el, styles){
	for (var name in styles) {
		if (styles.hasOwnProperty(name)) {
			el.style[name] = styles[name];
		}
	}
}

function fileFromPath(file){
	return file.replace(/.*(\/|\\)/, "");
}

function file_extension(file){
	return (-1 !== file.indexOf('.')) ? file.replace(/.*[.]/, '') : '';
}

function is_image(ext){
	return (ext && /^(jpg|png|jpeg|gif|bmp)$/.test(ext));
}

function stopEvent(e){
	if(!e){
		e = window.event;
	}
	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.returnValue = false;
	}
}

function getTarget(e){
	var targ;
	if(!e){
		e = window.event;
	}
	if(e.target){
		targ = e.target;
	}else if(e.srcElement){
		targ = e.srcElement;
	}
	if(targ.nodeType == 3){// defeat Safari bug
		targ = targ.parentNode;
	}
	return targ;
}

var field_counter = 0;
function add_field(e, name){
	stopEvent(e);
	field_counter++;
	var args = arguments;
	el = YAHOO.util.Dom.get(getTarget(e));
	//
	el = el.parentNode.parentNode;
	//
	var div = document.createElement('div');
	div.className = 'control';
	el.appendChild(div);
	var xdiv = div;
	el = div;
	//
	var label = document.createElement('label');
	label.innerHTML = arguments[3];
	label.className = 'label';
	el.appendChild(label);
	//
	div = document.createElement('div');
	div.className = 'field';
	el.appendChild(div);
	el = div;
	//
	var f = document.createElement('input');
	f.setAttribute('type', 'file');
	f.setAttribute('name', name);
	f.setAttribute('id', 'jv_f_1_'+field_counter);
	f.setAttribute('size', arguments[2]);
	el.appendChild(f);
	//
	f = document.createElement('button');
	f.innerHTML = ' X ';
	f.setAttribute('id', 'jv_f_1_b_'+field_counter);
	f.onclick = delete_field;
	el.appendChild(f);
	//
	var clr = document.createElement('div');
	clr.className = 'clr';
	xdiv.appendChild(clr);
	xdiv = null;
	//
	if(YAHOO.widget.Button){
		f = new YAHOO.widget.Button('jv_f_1_b_'+field_counter);
		f.on("click", delete_field);
	}
}

function delete_field(e){
	var el = getTarget(e);
	var el = el.parentNode.parentNode.parentNode.parentNode;
	el.parentNode.removeChild(el);
}

function prepare_form_for_ajax(frm, funx){
	if(document.forms[frm]){
		frm = document.forms[frm];
	}else{
		frm = YAHOO.util.Dom.get(frm);
	}
	if(frm && funx){
		var oops = arguments;
		frm.onsubmit = function onsubmit(event) {
			stopEvent(event);
			funx(oops, frm);
		}
	}
}

var jbutton_t = null;
var jbutton_c = 0;
var jbutton_a = new Array();
function JButton(el){
	jbutton_a.push(el);
	if(jbutton_t == null){
		jbutton_t = setInterval("ybtn()", 500);
	}
}

function ybtn(){
	if(!YAHOO.widget.Button){
		if(jbutton_c>60){
			clearInterval(jbutton_t);
			jbutton_a = new Array();
		}
		jbutton_c++;
		return false;
	}
	clearInterval(jbutton_t);
	for(var i=0; i<jbutton_a.length;i++){
		new YAHOO.widget.Button(jbutton_a[i]);
	}
	jbutton_a = new Array();
	jbutton_t = null;
}

var jtooltip_t = null;
var jtooltip_c = 0;
var jtooltip_a = new Array();
function JTooltip(el, txt){
	jtooltip_a.push({el:el,txt:txt});
	if(jtooltip_t == null){
		jtooltip_t = setInterval("ytt()", 600);
	}
}

function ytt(){
	if(!YAHOO.widget.Tooltip){
		if(jtooltip_c>90){
			clearInterval(jtooltip_t);
			jtooltip_a = new Array();
		}
		jtooltip_c++;
		return false;
	}
	clearInterval(jtooltip_t);
	for(var i=0; i<jtooltip_a.length;i++){
		new YAHOO.widget.Tooltip("tt"+fetch_id(), {context:jtooltip_a[i].el, text:jtooltip_a[i].txt});
	}
	jtooltip_a = new Array();
	jtooltip_t = null;
}

JFile = function(config){
	this.config = this.fix(config);
	var _self = this;
	this.config.button.style.visibility = 'hidden';
	YAHOO.util.Event.addListener(_self.config.button, "mouseover",function()
	{
		_self.mask(_self.config.button,_self.config.div);
	});
	YAHOO.util.Dom.addClass(_self.config.button, 'jfile-btn');
	YAHOO.util.Event.addListener(_self.config.button, "click", YAHOO.util.Event.preventDefault);
	this.clear();
	this.attach_events();
	YAHOO.util.Dom.setStyle(this.config.button, 'visibility', 'visible');
}

JFile.prototype.attach_events = function(){
	var self = this;
	YAHOO.util.Event.addListener(self.config.file, "mouseover",
		function(){YAHOO.util.Dom.addClass(self.config.button, self.config.hoverClass);}
	);
	YAHOO.util.Event.addListener(self.config.file, "mouseout",
		function(){YAHOO.util.Dom.removeClass(self.config.button, self.config.hoverClass);}
	);
	YAHOO.util.Event.addListener(self.config.file, "change",
		function(){
			var file = fileFromPath(this.value);
			var ext = file_extension(file);
			if(self.config.onChange.call(self, file, ext) === false)
			{
				self.clear();
				return false;
			}
		}
	);
}

JFile.prototype.clear = function(){
	this.config.file.value = '';
	if(this.config.file.value){
		// Fix for IE6
		file = document.createElement('input');
		file.setAttribute('type', 'file');
		file.setAttribute('name', this.config.file.name);
		addStyles(file, {
			fontSize:'480px',
			cursor:'pointer',
			right:0,
			margin:0,
			padding:0,
			position:'absolute'
		});
		var div = this.config.file.parentNode;
		div.removeChild(this.config.file);
		div.appendChild(file);
		this.config.file = file;
		file = null;
		this.attach_events();
	}
}

JFile.prototype.fix = function(o){
	if(!o.file){
		o.file = document.createElement('input');
		o.file.setAttribute('type', 'file');
	}else{
		o.file = YAHOO.util.Dom.get(o.file);
		o.container = o.file.parentNode;
	}
	if(o.list){
		o.list = YAHOO.util.Dom.get(o.list);
	}else{
		o.list = null;
	}
	if(!o.onChange){
		o.onChange = function(file, extension){};
	}
	if(!o.hoverClass){
		o.hoverClass = 'jfile-btn-hover';
	}
	if(!o.container){
		o.container = document.body;
	}else{
		o.container = YAHOO.util.Dom.get(o.container);
	}
	if(!o.button){
		o.button = document.createElement('button');
		o.button.setAttribute('id', 'f_b_' + fetch_id());
		o.button.innerHTML = o.buttonText||'Select File';
	}else{
		o.button = YAHOO.util.Dom.get(o.button);
	}
	addStyles(o.file, {
		fontSize:'480px',
		cursor:'pointer',
		right:0,
		margin:0,
		padding:0,
		position:'absolute'
	});
	o.div = document.createElement('div');
	o.div.appendChild(o.file);
	o.container.appendChild(o.div);
	o.container.appendChild(o.button);
	addStyles(o.div, {
		display:'block',
		position:'absolute',
		overflow:'hidden',
		float:'left',
		cursor:'pointer',
		margin:0,
		padding:0,
		direction:'ltr',
		zIndex:999//2147483583
	});
	YAHOO.util.Dom.setStyle(o.div, 'opacity', 0);
	return o;
}

JFile.prototype.mask = function(from,to){
	var box = YAHOO.util.Dom.getXY(from);
	addStyles(to, {
		position:'absolute',                    
		width:from.offsetWidth + 'px',
		height:from.offsetHeight + 'px'
	});
	YAHOO.util.Dom.setXY(to, box);
}

function change_form_type(el, frm){
	if(el.checked)
	{
		YAHOO.util.Dom.get(frm).setAttribute('enctype', 'multipart/form-data');
	}else{
		if(arguments[2] && !YAHOO.util.Dom.get(arguments[2]).checked){
			return false;
		}
		YAHOO.util.Dom.get(frm).removeAttribute('enctype');
	}
}

function zoom(e, id, el, t, l, type, btn, doo){
	stopEvent(e);
	var me = getTarget(e);
	el = YAHOO.util.Dom.get(el);
	l = YAHOO.util.Dom.get(l);
	l.style.display = 'block';
	image = new Image();
	image.onload = function() {
		l.style.display = 'none';
		el.innerHTML = '';
		el.appendChild(image);
		me.style.display = 'none';
		btn = YAHOO.util.Dom.get(btn);
		btn.style.display = 'block';
		image.onload = function() {};
	}
	image.src = "images.php?mod=products&do="+doo+"&id="+id+"&type="+type+"&t="+t;
}

function xzoom(e, id, el, t, l, type, btn, doo, table){
	stopEvent(e);
	var me = getTarget(e);
	el = YAHOO.util.Dom.get(el);
	l = YAHOO.util.Dom.get(l);
	l.style.display = 'block';
	image = new Image();
	image.onload = function() {
		l.style.display = 'none';
		el.innerHTML = '';
		el.appendChild(image);
		me.style.display = 'none';
		btn = YAHOO.util.Dom.get(btn);
		btn.style.display = 'block';
		image.onload = function() {};
	}
	image.src = "images.php?mod="+table+"&do="+doo+"&id="+id+"&type="+type+"&t="+t;
}

function swapDisplay(a, b){
	a = YAHOO.util.Dom.get(a);
	b = YAHOO.util.Dom.get(b);
	a.style.display = 'none';
	b.style.display = 'block';
}

function InlineEdit(e, el, frm){
	stopEvent(e);
	el = YAHOO.util.Dom.get(el);
	frm = YAHOO.util.Dom.get(frm);
	el.style.display = 'none';
	frm.style.display = 'block';
}

function doOrder(id){
	var frm = 'ofrm' + id;
	var val = document.forms[frm].st.value;
	var handleSuccess = function(A){
		if(A.responseXML){
			if(A.responseXML.getElementsByTagName("status")[0].firstChild.nodeValue == 'ok')
			{
				YAHOO.util.Dom.get('oops' + id).innerHTML = val;
				
				el = YAHOO.util.Dom.get('ordst' + id);
				frm = YAHOO.util.Dom.get('odfrm' + id);
				el.style.display = 'block';
				frm.style.display = 'none';
			}
		}
	}
	var handleFailure = function(o){}
	var callback = {success:handleSuccess,failure:handleFailure};
	var postData = 'do=order&id='+ id +'&s=' + val;
	YAHOO.util.Connect.asyncRequest('POST', 'ajax.php?do=order', callback, postData);
	return false;
}
