var enablerequest = true;
var http_request = false;
var div_id = false;
var request_fun = false;
var _requestQueue = new Array();

function makeRequest(url, id, fun, f) {
	if (enablerequest) {
		enablerequest = false;
		http_request = false;
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		}
		else if (window.ActiveXObject) {
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {}
			}
		}
		if (!http_request) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
		div_id = id;
		request_fun = fun;
		http_request.open("POST", url, true);
		http_request.onreadystatechange = loadContent;
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		var data = '';
		if (f && f.elements) {
			var s = '';
			var tmp = '';
			for (c = 0; c < f.elements.length; c++) {
				if (f.elements[c].type != 'checkbox' || f.elements[c].checked) {
					tmp = new String(f.elements[c].name+'='+f.elements[c].value);
					data += s+tmp.replace(/&/g, '%26');
					s = '&';
				}
			}
		}
		http_request.send(data);
	} else
		_requestQueue.push([url,id,fun,f]);
}
function loadContent() {
	if (http_request) {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (div_id != '') {
					var div = document.getElementById(div_id);
					div.innerHTML = http_request.responseText;
				}
				if (request_fun)
					eval(request_fun);
				enablerequest = true;
				if(_requestQueue.length){
					var nextReq = _requestQueue.shift();
					makeRequest(nextReq[0],nextReq[1],nextReq[2],nextReq[3]);
				}
			}
			else
				alert('There was a problem with the request.');
		}
	}
	else
		alert('Http_request error');
}

// bookmark 

function bookmarksite(title, url){
	if (document.all) window.external.AddFavorite(url, title);
	else if (window.sidebar) window.sidebar.addPanel(title, url, "")
}

// cookies functions 

function getCookie(name){ 
	var cname = name + "=";                
	var dc = document.cookie;              
	if (dc.length > 0) {               
    		begin = dc.indexOf(cname);        
		if (begin != -1) {            
            		begin += cname.length;        
                        end = dc.indexOf(";", begin); 
                        if (end == -1) end = dc.length; 
                        return unescape(dc.substring(begin, end)); 
                }  
        } 
        return null; 
}

function setCookie(name, value, expires, path, domain, secure) { 
	document.cookie = name + "=" + escape(value) +  
		((expires == null) ? "" : "; expires=" + expires.toGMTString()) + 
		((path == null) ? "" : "; path=" + path) + 
		((domain == null) ? "" : "; domain=" + domain) + 
		((secure == null) ? "" : "; secure"); 
};

function setCookieYear(name, value, path, domain, secure) {
	var expiration = new Date(); 
	var year; 
	year=expiration.getYear(); 
	if (year<2000) { 
	        year=year+1900; 
	} 
	if (value)
		expiration.setYear(year + 1);
	else
		expiration.setYear(year - 1); // empty value, clear cookie
	setCookie(name,value,expiration,path,domain,secure);
}

function clearCookie(name, path, domain, secure) {
	var expiration = new Date(); 
	var year; 
	year=expiration.getYear(); 
	if (year<2000) { 
	        year=year+1900; 
	} 
	expiration.setYear(year - 1);
	setCookie(name,'_',expiration,path,domain,secure);
}

// clShopInfo

function showCurrency(show){
	var cdiv = document.getElementById("currency");
	if(cdiv) switch(show) {
		case 0:cdiv.style.display="none"; break;
		case 1:cdiv.style.display=""; break;
		case 2:cdiv.style.display=(cdiv.style.display=="none"?"":"none");break;
	}
};

function checkEmail(email){
	var r = /^([\w-]+(\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!r.test(email)) { 
		alert("Email address is not valid."); 
		return false; 
	}
	return true; 
}

// events

function addEvent(element, event, listener, bubble) {
	if(element.addEventListener) {
		if(typeof(bubble) == "undefined")
			bubble = false;
		element.addEventListener(event, listener, bubble);
	} else 
		if(this.attachEvent) {
		    element.attachEvent("on" + event, listener);
		}
}
 /* other */
function str_trim(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

Math.sgn = function(num){
	if(num==0)
		return 0;
	if(num>0)
		return 1;
	return -1;
}

Math.parseInt = function(i){
	var v = parseInt(i);
	if(isNaN(v))
		return 0;
	return v;
}
/* scroll images */
function prepareScrolling(div, cursorSize, cycleScrolling){
	if(div.scrollWidth<=div.offsetWidth){
		div.style.visibility = "visible";
		return;
	}
	if(!div.id){
		do {
			var id = "div_"+Math.round(Math.random()*10000);
		} while (document.getElementById(id))
		div.id = id;
	}
	if(cursorSize){
		var par = div.parentNode;
		div.style.visibility = "hidden";
		div.style.width = (Math.parseInt(div.offsetWidth)-2*cursorSize)+"px";
		div.style.marginLeft = cursorSize+Math.parseInt(div.style.marginLeft)+"px"; 
		div.style.marginRight = cursorSize+Math.parseInt(div.style.marginRight)+"px";
		par.style.position = "relative";
		var d = document.createElement("div");
		d.innerHTML = '<a href="#" onclick="document.getElementById(\''+div.id+'\').scrollImages(-1); return false;">&lt;</a>'; 
		d.className = 'mixSwatchScroller';
		d.style.left = "0px";
		par.appendChild(d);

		var d = document.createElement("div");
		d.innerHTML = '<a href="#" onclick="document.getElementById(\''+div.id+'\').scrollImages(+1); return false;">&gt;</a>'; 
		d.className = 'mixSwatchScroller';
		d.style.left = div.offsetWidth+cursorSize+"px";
		par.appendChild(d);
	}

	div.style.visibility = "visible";
	
	div.scrollImages=function(direction){
		if(this.timer)
			return;
		var tds = this.getElementsByTagName("td");
		if(tds.length==0)
			return;
		this.scroll = 0;
		this.oldScroll = this.scrollLeft;
		if(direction>0){
			for(var i=0; i<tds.length;i++)
				if(tds[i].offsetLeft>this.scrollLeft){
					this.moveImagesTable(tds[i].offsetLeft, direction);
					return i;
				}
			if(cycleScrolling){
				this.moveImagesTable(0, -direction);
				return 0;
			}
		}
		if(direction<0){
			for(var i=tds.length-1; i>=0;i--)
				if(tds[i].offsetLeft<this.scrollLeft){
					this.moveImagesTable(tds[i].offsetLeft, direction);
					return i;
				}
			if(cycleScrolling){
				this.moveImagesTable(tds[tds.length-1].offsetLeft, -direction);
				return tds.length-1;
			}
		}
	};
	
	div.scrollToImage=function(ind){
		var tds = this.getElementsByTagName("td");
		if(ind<0 || ind>=tds.length)
			return;
		this.scroll = 0;
		this.oldScroll = this.scrollLeft;
		var target = tds[ind].offsetLeft;
		this.moveImagesTable(target,Math.sgn(target - this.oldScroll));
	};
	div.moveImagesTable=function(target, direction){
		this.scroll += direction;
		if(Math.sgn(target-this.oldScroll-this.scroll)!=Math.sgn(direction)){
			this.scrollLeft = target;
			this.timer = false;
			return;
		}
		this.scrollLeft = this.oldScroll+this.scroll;
		direction += Math.sgn(direction);
		this.timer = setTimeout("document.getElementById('"+this.id+"').moveImagesTable("+target+","+direction+")", 15);
	};
}

function prepareScrollingVertical(div, cursorSize, cycleScrolling){
	if(div.scrollHeight<=div.offsetHeight){
		div.style.visibility = "visible";
		return;
	}
	if(!div.id){
		do {
			var id = "div_"+Math.round(Math.random()*10000);
		} while (document.getElementById(id))
		div.id = id;
	}
	if(cursorSize){
		var par = div.parentNode;
		div.style.visibility = "hidden";
		div.style.height = (Math.parseInt(div.offsetHeight)-2*cursorSize)+"px";
		div.style.marginTop = cursorSize+Math.parseInt(div.style.marginTop)+"px"; 
		div.style.marginBottom = cursorSize+Math.parseInt(div.style.marginBottom)+"px";
		par.style.position = "relative";
		var d = document.createElement("div");
		d.innerHTML = '<a href="#" onclick="document.getElementById(\''+div.id+'\').scrollImages(-1); return false;">^</a>'; 
		d.className = 'mixSwatchScroller';
		d.style.top = "0px";
		par.appendChild(d);

		var d = document.createElement("div");
		d.innerHTML = '<a href="#" onclick="document.getElementById(\''+div.id+'\').scrollImages(+1); return false;">u</a>'; 
		d.className = 'mixSwatchScroller';
		d.style.top = div.offsetWidth+cursorSize+"px";
		par.appendChild(d);
	}

	div.style.visibility = "visible";
	
	div.scrollImages=function(direction){
		if(this.timer)
			return;
		var tds = this.getElementsByTagName("td");
		if(tds.length==0)
			return;
		this.scroll = 0;
		this.oldScroll = this.scrollTop;
		if(direction>0){
			for(var i=0; i<tds.length;i++)
				if(tds[i].offsetTop>this.scrollTop){
					this.moveImagesTable(tds[i].offsetTop, direction);
					return i;
				}
			if(cycleScrolling){
				this.moveImagesTable(0, -direction);
				return 0;
			}
		}
		if(direction<0){
			for(var i=tds.length-1; i>=0;i--)
				if(tds[i].offsetTop<this.scrollTop){
					this.moveImagesTable(tds[i].offsetTop, direction);
					return i;
				}
			if(cycleScrolling){
				this.moveImagesTable(tds[tds.length-1].offsetTop, -direction);
				return tds.length-1;
			}
		}
	};
	
	div.scrollToImage=function(ind){
		var tds = this.getElementsByTagName("td");
		if(ind<0 || ind>=tds.length)
			return;
		this.scroll = 0;
		this.oldScroll = this.scrollTop;
		var target = tds[ind].offsetTop;
		this.moveImagesTable(target,Math.sgn(target - this.oldScroll));
	};
	div.moveImagesTable=function(target, direction){
		this.scroll += direction;
		if(Math.sgn(target-this.oldScroll-this.scroll)!=Math.sgn(direction)){
			this.scrollTop = target;
			this.timer = false;
			return;
		}
		this.scrollTop = this.oldScroll+this.scroll;
		direction += Math.sgn(direction);
		this.timer = setTimeout("document.getElementById('"+this.id+"').moveImagesTable("+target+","+direction+")", 15);
	};
}

/////////////////////////////////////////////
function preloadImages(images){
	for(var i=0; i<images.length; i++){
		var im = new Image();
		im.src = images[i];
	}
}

