ar_tc = [];
ar_graph = [];
arr_num = [];
selected_color = [];
ar_key = [];

var separator = "__"; // ../var1<separator>value1<separator>value2/var2...

function verify(){
	var all_is_ok = true;
	while(verification = document.getElementById('verification')){
		var check = document.getElementById('form_'+verification.value);
		if(!check.value) {
			all_is_ok = false;	
			break;
		}
		verification.parentNode.removeChild(verification);
	}
	if(all_is_ok) return true;
	else {
		alert("Вы не заполнили все обязательные поля!");
		return false;
	}
}

function SetCookie (name, value) {
	var largeExpDate = new Date ();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
	var expires = largeExpDate;
	document.cookie = name + "=" + escape (value) +"; expires=" + expires.toGMTString() +  "; path=/";
}


function getCookie(name) {
	if(document.cookie == "") return false;
	else {
		var cookieStart, cookieEnd;
		var cookieString = document.cookie;
		cookieStart = cookieString.indexOf(name+"=");
		if(cookieStart != -1) {
			cookieStart += name.length+1;
			cookieEnd = cookieString.indexOf(";", cookieStart);
			if(cookieEnd == -1) cookieEnd = cookieString.length;
			return cookieString.substring(cookieStart, cookieEnd);
		} else {
			return false;
		}
	}
}

function getshopingcart() {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	if(shopingcart) {
		rows = shopingcart.split(",");
	}
	return rows.length;
}

function emptyshopingcart() {
	SetCookie("shopingcart", "");
	SetCookie("quantity", ""); //w3c
	return false;
}

function removeposition(id,refresh) {
	var rows = new Array();
	var q_rows = new Array(); //w3c
	
	var shopingcart = unescape(getCookie("shopingcart"));
	var quantity = unescape(getCookie("quantity")); //w3c
	
	var newcartstring = "";
	var newquantitystring = ""; //w3c
	
	if(shopingcart && quantity) {
		rows = shopingcart.split(",");
		q_rows = quantity.split(","); //w3c
		for(i=0; i<rows.length; i++){
			if(rows[i]!=id && rows[i]!='') {
				newcartstring = newcartstring + rows[i]+",";
				newquantitystring = newquantitystring + q_rows[i]+","; //w3c
			}
		}
		
	}
	if(newcartstring.length) newcartstring = newcartstring.substr(0, newcartstring.length-1);
	SetCookie("shopingcart", newcartstring);
	if(newquantitystring.length) newquantitystring = newquantitystring.substr(0, newquantitystring.length-1); //w3c
	SetCookie("quantity", newquantitystring); //w3c
	
	if(refresh==1) window.location = window.location;
}

function toshopingcart(id,n,ref,mes) {
	var rows = new Array();
	var shopingcart = unescape(getCookie("shopingcart"));
	var q_rows = new Array(); //w3c
	var quantity = unescape(getCookie("quantity")); //w3c
	var flag_avaible = false;
	if(shopingcart && quantity) {
		rows = shopingcart.split(",");
		q_rows = quantity.split(","); //w3c
		for(i=0; i<rows.length; i++){
			if(rows[i]==id) {
				flag_avaible = true;
			}
		}
		
	}
	
	if(flag_avaible==false) {
		if(rows && rows[0]!="false") {
			rows[rows.length]=id;
		} else rows[0]=id;
		SetCookie("shopingcart", rows.join(","));
		
		//w3c:
		if(q_rows && q_rows[0]!="false") {
			q_rows[q_rows.length]=n;
		} else q_rows[0]=n;
		SetCookie("quantity", q_rows.join(","));
		//w3c
		
	}
	
	if(mes==1){
		if(flag_avaible==false){
			alert('Бизнес добавлен в ваш список сравнения!');
		}else{
			alert('Бизнес УЖЕ добавлен в ваш список сравнения!');
		}
	}
	
	if(ref==1) window.location = window.location;
}

function uncheck_all(obj){
	obj.innerHTML = obj.innerHTML.replace(new RegExp('checked="checked"', "g"), "");
}
function check_all(obj){
	obj.innerHTML = obj.innerHTML.replace(new RegExp('type="checkbox"', "g"), 'type="checkbox" checked="checked"');
}
function RTrim(str, separator)
{
	var whitespace = new String(separator);
	var s = new String(str);

	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	    var i = s.length - 1;       // Get length of string

	    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) { i--; }
		
		s = s.substring(0, i+1);
	}
	
	return s;
}

function check_select(base_url) {
	var str;
	if(base_url != undefined){
		str = path + RTrim(base_url, '/') + '/';
	} else {
		str = RTrim(document.location, '/') + '/';	
	}
	
	str_tmp = str;
	
	// keyword
	for (id in ar_key){
		keyword = document.getElementById('keyword'+id).value;
		var_key = document.getElementById("var"+id).value;
		if(keyword != ''){
			str += 'keyword' + separator + var_key + separator + keyword + '/';
		}
	}
	
	// select
	/*
	arr = document.getElementsByTagName('select');
	for(var i = 0; i < arr.length; i++) {
		index = arr[i].selectedIndex;
		str = str +''+arr[i].name+'_'+arr[i].options[index].value;
	}
	*/
	
	// pulldown
	for (id in ar){
		var_m_s = document.getElementById("var"+id).value;
		multiselect = document.getElementById('pulldown'+id).value;
		str += var_m_s + separator + multiselect + '/';
	}
	
	// tag cloud
	for (id in ar_tc){
		var_t_c = document.getElementById("var"+id).value;
		tag_cloud = document.getElementById('tag_cloud'+id).value;
		str += var_t_c + separator + tag_cloud + '/';
	}
	
	// graphic
	for (id in ar_graph){
		var_graph = document.getElementById("var"+id).value;
		graph = document.getElementById('graphic'+id).value;
		str += var_graph + separator + graph + '/';
	}
	
	var url_length = str.length;
	if (str.length >= 1900) {
			str = str.substring(0, 1900);
			pos = str.lastIndexOf(separator);
			str = str.substring(0, pos);
	}
	if(str == str_tmp){
		str += 'all/';
	}
	
	var oJS = new JSfunc();
	str = oJS.strNormalize(str);
	document.location = str;
}

// changes tag cloud
function change_tc(obj,id, update){
	ar_tc[id] = [];
	var tag_cloud_val='';
	var count = 0;
	
	if(obj.className == 'active'){
		obj.className = 'inactive';
	} else {
		obj.className = 'active';
	}
	var nodes = document.getElementById("selector"+id).getElementsByTagName("a");
	var tag_cloud='';
	
	for (var i=0; i < nodes.length; i++) {
		if(nodes[i].className == 'active') {
			ar_tc[id][count] = nodes[i].getAttribute('val');
			tag_cloud_val = tag_cloud_val + ar_tc[id][count] + separator;
			count++;
		}
	}
	if(count == 0) delete ar_tc[id];
	
	tag_cloud_val = RTrim(tag_cloud_val, separator);
	
	// генерация подписей под графиком
	generate_count_title(id, count);
	
	document.getElementById('tag_cloud'+id).value = tag_cloud_val;
}

// changes graphic search
function change_graphic(obj, id){
	ar_graph[id] = [];
	var graph_val='';
	var count = 0;
	
	if(obj.className == 'active'){
		obj.className = 'inactive';
	} else {
		obj.className = 'active';
	}
	
	// get selected values
	var nodes = document.getElementById("selector"+id).getElementsByTagName("table");
	for (var i=0; i < nodes.length; i++) {
		if(nodes[i].className == 'active') {
			ar_graph[id][count] = nodes[i].getAttribute('val');
			ar_graph[id][count] = ar_graph[id][count].replace('<','less');
			ar_graph[id][count] = ar_graph[id][count].replace('>','more');
			graph_val = graph_val + ar_graph[id][count] + separator;
			count++;
		}
	}
	if(count == 0) delete ar_graph[id];
	
	graph_val = RTrim(graph_val, separator);
	
	// генерация подписей под графиком
	generate_count_title(id, count);

	document.getElementById('graphic'+id).value = graph_val;
}

function generate_count_title(id, count){
	var title;
	switch(count){
		case 0:
			title = document.getElementById('count_selected'+id).getAttribute('value_0').replace('#count#',count);
			break;
		case 1: case 21: case 31: case 41: case 51: case 61: case 71: case 81: case 91:
			title = document.getElementById('count_selected'+id).getAttribute('value_1').replace('#count#',count);
			break;
		case 2: case 22: case 32: case 42: case 52: case 62: case 72: case 82: case 92:
		case 3: case 23: case 33: case 43: case 53: case 63: case 73: case 83: case 93: 
		case 4: case 24: case 34: case 44: case 54: case 64: case 74: case 84: case 94: 
			title = document.getElementById('count_selected'+id).getAttribute('value_2').replace('#count#',count);
			break;
		case 5: 
		default:
			title = document.getElementById('count_selected'+id).getAttribute('value_5').replace('#count#',count);
	}
	document.getElementById('count_selected'+id).innerHTML = title;
}

function remove_color(e){
	var id = this.getAttribute('el_id');
	var color = this.getAttribute('col_id');
	this.parentNode.removeChild(this);
	
	document.getElementById('result'+id).value = document.getElementById('result'+id).value.replace('_('+color+'|'+document.getElementById('value'+id).value+')_','');
}

function add_color(id) {
	el = document.createElement('div');
	el.style.background = "url("+path+"usr/templates/images/bg/"+ selected_color[id] +".gif"+")";
	el.innerHTML = document.getElementById('value'+id).value + ' (click to delete)';
	el.addEventListener('click', remove_color, false);
	el.setAttribute('el_id', id);
	el.setAttribute('col_id', selected_color[id]);
	el.setAttribute('value', document.getElementById('value'+id).value);
	document.getElementById('display_chosen'+id).appendChild(el);
	
	document.getElementById('result'+id).value += '_(' + selected_color[id] + '|' + document.getElementById('value'+id).value+')_';
}

function display_graphic(id) {
	document.getElementById('graphic'+id).style.display = "block";
}

function hide_graphic(id) {
	document.getElementById('graphic'+id).style.display = "none";
}

function pick_color(obj, id, color) {
	td = obj.parentNode.getElementsByTagName('td');
	for(var i = 0; i < td.length; i++){
		td[i].style.border = "0";;
	}
	obj.style.border = "3px solid black";
	selected_color[id] = color;
}

function check_submit(e, base_url)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE
	{
	keynum = e.keyCode;
	}
else if(e.which) // Netscape/Firefox/Opera
	{
	keynum = e.which;
	}
	if (keynum == 13) {
		check_select(base_url);
	}
}

function JSfunc()
{
	var new_el;
	/* Making transliteration! */
	this.strTranslit = function(el)
	{
		for(var i = 0; i < el.length; i++){
			if(el[i] == " ") el = el.replace(' ','_');
		}
		
		var new_el;
		//new_el = document.getElementById('out');
		A = new Array();
		A["Ё"]="yo";A["Й"]="i";A["Ц"]="c";A["У"]="u";A["К"]="k";A["Е"]="e";A["Н"]="n";A["Г"]="g";A["Ш"]="sh";A["Щ"]="sch";A["З"]="z";A["Х"]="h";A["Ъ"]="''";
		A["ё"]="yo";A["й"]="i";A["ц"]="c";A["у"]="u";A["к"]="k";A["е"]="e";A["н"]="n";A["г"]="g";A["ш"]="sh";A["щ"]="sch";A["з"]="z";A["х"]="h";A["ъ"]="''";
		A["Ф"]="f";A["Ы"]="y";A["В"]="v";A["А"]="a";A["П"]="p";A["Р"]="r";A["О"]="o";A["Л"]="l";A["Д"]="d";A["Ж"]="zh";A["Э"]="ye";
		A["ф"]="f";A["ы"]="y";A["в"]="v";A["а"]="a";A["п"]="p";A["р"]="r";A["о"]="o";A["л"]="l";A["д"]="d";A["ж"]="zh";A["э"]="e";
		A["Я"]="ja";A["Ч"]="ch";A["С"]="s";A["М"]="m";A["И"]="i";A["Т"]="t";A["Ь"]="";A["Б"]="b";A["Ю"]="yu";
		A["я"]="ja";A["ч"]="ch";A["с"]="s";A["м"]="m";A["и"]="i";A["т"]="t";A["ь"]="";A["б"]="b";A["ю"]="yu";
		this.new_el = el.replace(/([\u0410-\u0451])/g,
			function (str,p1,offset,s) {
				if (A[str] != 'undefined'){return A[str];}
			}
		);
		
		return new_el;
	}
	/* Normalizes a string, eю => eyu */
	this.strNormalize = function(el)
	{
		if (!el) { return; }
		this.strTranslit(el);
		return this.new_el;
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;
	var img = document.getElementById(id);
    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function switch_sound() {
	var sound = getCookie('sound');
	//var src = top.document.getElementById('sound').getAttribute('src');
	if (sound != 'off') {
		SetCookie('sound', 'off');
		if (navigator.appName.indexOf ("Microsoft") !=-1) {
			top.iframe.document.getElementById('music_img').setAttribute('src', http_path + '/usr/templates/img/butt_off.png');
			top.document.getElementById('sound').setAttribute('src','');
			top.document.getElementById('sound').setAttribute('src','');
		  } else {
			top.iframe.document.getElementById('music_img').setAttribute('src', http_path + '/usr/templates/img/butt_off.png');
			thisMovie("bg_sound").SetVariable('volume','0');
		  }
		/**/
	} else {
		SetCookie('sound', 'on');
		if (navigator.appName.indexOf ("Microsoft") !=-1) {
			top.iframe.document.getElementById('music_img').setAttribute('src', http_path + '/usr/templates/img/butt_on.png');
			top.document.getElementById('sound').setAttribute('src', http_path + '/usr/templates/sound/sound1.mp3');
			top.document.getElementById('sound').setAttribute('src', http_path + '/usr/templates/sound/sound1.mp3');
		  } else {
			top.iframe.document.getElementById('music_img').setAttribute('src', http_path + '/usr/templates/img/butt_on.png');
			thisMovie("bg_sound").SetVariable('volume','100');
		  }
		/**/
	}
}

var caution;

function thisMovie(movieName) {

  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window[movieName]
  } else {
    return top.document[movieName]
  }
}
///oJS.strNormalize(this)