var ie = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;

if(document.location.hash){
	var _afnh=document.location.hash.replace('#','');
	var d = new Date;
    var cy = d.getFullYear()+1;
    var cm = d.getMonth();
    var cd = d.getDate();
    if(_afnh.substring(0,4)=='_afn'){
		afnSetCookie('_afn',_afnh,cy,cm,cd,'/');
		statsImg= new Image(1,1);
		statsImg.src='http://affiliate.tiesraides.lv/afn/r/'+_afnh;
	}
}

function afnSetCookie(name,value,exp_y,exp_m,exp_d,path,domain,secure){
  	var cookie_string = name + "=" + escape(value);
  	if(exp_y){
    	var expires = new Date(exp_y,exp_m,exp_d);
    	cookie_string += "; expires=" + expires.toGMTString();
  	}
  	if(path) cookie_string += "; path=" + escape(path);
  	if(domain) cookie_string += "; domain=" + escape(domain);
  	if(secure) cookie_string += "; secure";
  	document.cookie = cookie_string;
}

function afnDeleteCookie(cookie_name){
  	var cookie_date = new Date();
  	cookie_date.setTime(cookie_date.getTime()-1);
  	document.cookie = cookie_name+="=; expires=" + cookie_date.toGMTString();
}

function afnGetCookie(cookie_name){
  	var results = document.cookie.match('(^|;)?'+ cookie_name + '=([^;]*)(;|$)');
  	if(results)
    	return (unescape(results[2]));
  	else
    	return null;
}

function afnWriteToLayer(lay,txt){
  	try{
  	if (ie){
    	if(document.all[lay]){
      	document.all[lay].innerHTML = txt;
    	}
  	}
  	if (ns6){
    	over = document.getElementById([lay]);
    	range = document.createRange();
    	range.setStartBefore(over);
    	domfrag = range.createContextualFragment(txt);
    	while (over.hasChildNodes()){
      		over.removeChild(over.lastChild);
    	}
    	over.appendChild(domfrag);
  	}
  	}catch(e){}
}

function afnLoadScript(url, callback){
	var f = arguments.callee;
	if (!("queue" in f))
		f.queue = {};
	var queue =  f.queue;
	if (url in queue) {
		if (callback) {
			if (queue[url])
				queue[url].push(callback);
			else
				callback();
		}
		return;
	}
	queue[url] = callback ? [callback] : [];
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.onload = script.onreadystatechange = function(){
		if (script.readyState && script.readyState != "loaded" && script.readyState != "complete")
			return;
		script.onreadystatechange = script.onload = null;
		while (queue[url].length)
			queue[url].shift()(script.text);
		queue[url] = null;
	};
	script.src = url;
	document.getElementsByTagName("head")[0].appendChild(script);
}

function afnLoadCss(url){
	console.info(url);
	var fileref=document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", url);
	if (typeof fileref!="undefined"){
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}
	