/**
 * IQ Utils js
 *
 */

function getValue(str, name) {
	var search = name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (str.length > 0) {
		offset = str.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = str.indexOf(";", offset);
			if (end == -1) {
				end = str.length;
			}
			setStr = str.substring(offset, end);
		}
	}
	return(setStr);
}

function urlescape(TEXT) {
	var trans = [];
	for(var i=0x410; i<=0x44F; i++) {
		trans[i] = i - 0x350;
	}
	trans[0x401] = 0xA8;
	trans[0x51] = 0xB8;
	var ret = [];
	for(var i = 0; i<TEXT.length; i++) {
		var n = TEXT.charCodeAt(i);
		if(typeof trans[n] != 'undefined') n = trans[n];
		if(n <= 0xFF) ret.push(n);
	}
	return window.escape(String.fromCharCode.apply(null, ret));
}

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

function trim(str) {
	return str.replace(/^\s+|\s+$/, '');
}

function event_trigger(e) {
	if (!e)
		e = event;
	return e.target || e.srcElement;
}

function isIE() {
	return navigator.appName.indexOf("Microsoft") != -1;
}

function big_double_format(val) {
	if((val / 1000000000) > 1) {
		return (val / 1000000000).toFixed(2) + " млрд.";
	}
	if((val / 1000000) > 1) {
		return (val / 1000000).toFixed(2) + " млн.";
	}
	if((val / 1000) > 1) {
		return (val / 1000).toFixed(2) + " тыс.";
	}
	return val;	
}

function ajaxQ () {

	this.req = null;
	this.type = true;
	this.callback = function() {/*alert("Ajax request ok");*/}
	var __obj = null;

	try {
		this.req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {
		try{
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e) {
			if(window.XMLHttpRequest){
				this.req = new XMLHttpRequest();
			}
		}
	}
	if(!this.req) {
		alert("AjaxQ object init error!");
		return;
	}
	__obj = this;
	this.req.onreadystatechange = function() {recv();}
	
	this.send = function(method, URL, data) {
		var now = new Date();
		var str = "" + now.getHours();
		str += ((now.getMinutes() < 10) ? "0" : "") + now.getMinutes();
		str += ((now.getSeconds() < 10) ? "0" : "") + now.getSeconds();
		this.req.open(method, URL + "&" + str, this.type);
		this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.req.send(data);
	}
	
	var recv = function() {
		if(!__obj) {
			alert("ajax obj false!");
			return;
		}
	
		if(__obj.req.readyState == 4) {
			if(__obj.req.status == 200){
				__obj.callback(__obj.req.responseText);
			}
			else{
				alert("AJAX error: " +__obj.req.status+ "\n"+ __obj.req.statusText);
			}
		}
	}
}

function load_chart_pictures () {
	$("img.picture").each(function() {
			$(this).css("width", "95%");
			var tiker = $(this).attr("tiker");
			var fullname = $(this).attr("fullname");
			var width = this.clientWidth;
			this.src = "/web-backend?module=infi&is_binary=1&wi="+width+"&hi=200&xl=2&yl=3&color=0xCCDDE5&txt_color=0x000001&eng=1&ticker="+tiker+"&fullname="+urlescape(fullname)+"&fill_color=0xccdde5&liwi=1";
			});
}

function clear_chart_cookie() {
	setCookie("symbol", "");
	setCookie("compare", "");
	setCookie("indicator", "");
	setCookie("range", "");
	setCookie("drawings", "");
}
