//.................  Main_function.js  .................
var lang_elem_num = 1;
var last_time;
var is_ok = false;

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function randomNumber (m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}

function setBodyScrollTop()
{	if (document.documentElement && document.documentElement.scrollTop)
		document.documentElement.scrollTop = 0;
	else if (document.body && document.body.scrollTop)
		document.body.scrollTop = 0;
	else
		self.pageYOffset  = 0;
}

function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}
 
function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

function getViewportWidth() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientWidth : document.body.clientWidth : (document.parentWindow || document.defaultView).innerWidth;
}
function  select_p(select_p_obj,val){
   if (val==1){
      select_p_obj.setAttribute('style','background-color:#1589c1; color:#ffffff');
   }
   else{
      select_p_obj.setAttribute('style','background-color:#FFFFFF; color:#2b2b2b');
   }
}

function absPosition(obj) {
      var x = y = 0;
      while(obj) {
            x += obj.offsetLeft;
            y += obj.offsetTop;
            obj = obj.offsetParent;
      }
      return {x:x, y:y};
} 

function callboard_select_ba()
{
   var c_sel_ba = document.getElementById('ba_areas');
   $(c_sel_ba).css("display","");

   var c_sel_obj = document.getElementById('business_area');
   c_sel_objobj.value = 0;

   var c_sel_obj_t = document.getElementById('pba');
   c_sel_obj_t.value = 'none';

   var c_sel_tbl = document.getElementById('tbl_callboard');
   $(c_sel_tbl).css("display", "none");
}

function setValueCallboard(svc_id, svc_title)
{
   var svc_ba = document.getElementById('ba_areas');
   $(svc_ba).css("display", "none");

   var svc_obj = document.getElementById('business_area');
   svc_obj.value = svc_id;

   var svc_obj_t = document.getElementById('pba');
   svc_obj_t.value = svc_title;

   var svc_tbl = document.getElementById('tbl_callboard');
   $(svc_tbl).css("display", "");
}

function ElemSetAtribute(esa_id, esa_atr_name, esa_value)
{
	var esa_obj = document.getElementById(esa_id);
	esa_obj.setAttribute(esa_atr_name,esa_value);
}

var only_id = false;
var id_name = '_id';
function add_iregion(id,ids2,ids,h_name)
{
	var str = '';
	var id_str = '';
	if (ids2 != ''){
	   var add_value2 = document.getElementById(ids2);
	   str = add_value2[add_value2.selectedIndex].text + ', ';
	}

	var ireg_obj = document.getElementById(id);

	var add_value  = document.getElementById(ids);
	if (add_value[add_value.selectedIndex].value > 0)
	{
		if (only_id)
		{
			str = str + add_value[add_value.selectedIndex].text;
			id_str = id_str + add_value[add_value.selectedIndex].value;
		}
		else
			str = str + add_value[add_value.selectedIndex].text;

		var addstr = document.createTextNode(str);

		var delobj = document.getElementById("delobj");
		var cloneDiv = delobj.cloneNode(true);
		cloneDiv.style['display'] = '';
		cloneDiv.setAttribute("id","clone");
		var txtarea = document.getElementById("txtarea");

		var cloneTxtArea = txtarea.cloneNode(true);
		cloneTxtArea.appendChild(addstr);
		cloneTxtArea.setAttribute("id","clone");
		cloneTxtArea.style['display'] = '';

		var oInput = document.createElement("INPUT");
		oInput.setAttribute("type","hidden");
		oInput.setAttribute("name", h_name);
		oInput.setAttribute("id", id_name);
		if (only_id)
			oInput.setAttribute("value", id_str);
		else
			oInput.setAttribute("value", str);

		var oDiv = document.createElement("DIV");
		oDiv.appendChild(cloneTxtArea);
		oDiv.appendChild(cloneDiv);
		oDiv.appendChild(oInput);
		oDiv.appendChild(document.createElement("BR"));

		ireg_obj.appendChild(oDiv);

		cloneTxtArea = null;
		oInput = null;
		clonneDiv = null;
	} else
		alert('Выберите регион');
}

function insertAfter(newElement, targetElement)
{
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement)
	{
		parent.appendChild(newElement);
	}
	else
	{
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

function addClone(obj, blockclass)
{
    var original = obj.parentNode;
    var duplicate = original.cloneNode(true);
    var att = '';
    switch (blockclass){
	case 1:
		break;
	case 2:
		var hw = document.getElementById("h_w");
		var nm = hw.value - 0;
		nm++;

		hw.setAttribute("value", nm);

   		var b = original.getElementsByTagName("input")[3];
   		b.value = nm;

		att = "work_block" + nm;
		break;
	case 3:
		var he = document.getElementById("h_e");
		var nm = he.value - 0;
		nm++;
		he.setAttribute("value", nm);

   		var b = original.getElementsByTagName("input")[4];
   		b.value = nm;

		att = "edu_block" + nm;
		break;
	case 4:
		var hc = document.getElementById("h_c");
		var nm = hc.value - 0;
		nm++;
		hc.setAttribute("value", nm);

   		var b = original.getElementsByTagName("input")[4];
   		b.value = nm;

		att = "course_block" + nm;
		break;
    }
    original.setAttribute("id", att);
    original.style['display'] = '';
    insertAfter(duplicate, original);
}

function removeObj(obj)
{
    $(obj).remove();
}

function removeClone(obj,blockclass)
{
   var del_obj = obj.parentNode;

   switch (blockclass){
	case 1:
		break;
	case 2:
		hw = document.getElementById("h_w");
		nm = hw.value - 0;
		if (nm > 0){
		   nm--;
		   hw.setAttribute("value", nm);
		   b = del_obj.getElementsByTagName("input")[3];
		}
		break;
	case 3:
		he = document.getElementById("h_e");
		nm = he.value - 0;
		if (nm > 0){
		   nm--;
		   he.setAttribute("value", nm);
		   b = del_obj.getElementsByTagName("input")[4];
		}
		break;
	case 4:
		hc = document.getElementById("h_c");
		nm = hc.value - 0;
		if (nm > 0){
		   nm--;
		   hc.setAttribute("value", nm);
		   b = del_obj.getElementsByTagName("input")[4];
		}
		break;
    }
    del_id = document.getElementById(del_obj.id);
    $(del_id).remove();
}

function AddBookmark() {
   window.external.AddFavorite(location.href,'DelovoyMir.biz');
}

function MakeStarting(obj){
   obj.style.behavior='url(#default#homepage)';
   var ohp=obj.isHomePage('http://www.delovoymir.biz/ru/profile/');
   obj.setHomePage('http://www.delovoymir.biz/ru/profile/');
   var nhp=obj.isHomePage('http://www.delovoymir.biz/ru/profile/');
   r(obj, 'shp/o=' + ohp + '/n=' + nhp);
   return false;
}

function refresh_window()
{
	pg_b = document.getElementById("page_body_bg");
	pg_l = document.getElementById("page_left_bg");

	if (pg_b.offsetHeight >= pg_l.offsetHeight)
	{
		pg_l.style['height'] = pg_b.offsetHeight + 'px';
	}
	else
	{
		pg_b.style['height'] = pg_l.offsetHeight + 'px';		
	}
}

function SelectChbx(checked, SC_input, id)
{
	SC_obj = document.getElementById(SC_input);
	if (checked)
	{
		SC_obj.value = id;
	}
	else
	{
		SC_obj.value = '';
	}
}

function removeSpaces(s) 	// removeSpaces - удалить все пробелы в строке
{ 
	var allSpacesRe = /\s+/g;
	return s.replace(allSpacesRe, ""); 
}
function trimLeading(s) 	// trimLeading - удалить пробелы с начала
{ 
	var leadingSpacesRe = /^\s+/;
	return s.replace(leadingSpacesRe, ""); 
}
function trimTrailing(s) 	// trimTrailing - удалить пробелы с конца
{
	var trailingSpacesRe = /\s+$/;
	return s.replace(trailingSpacesRe, ""); 
}


/* ------------example-------------------------
	explode(' ', 'Kevin van Zonneveld');
	{0: 'Kevin', 1: 'van', 2: 'Zonneveld'}	
*/
function explode( delimiter, string ) {    // Split a string by string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: kenneth
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 
    var emptyArray = { 0: '' };
 
    if ( arguments.length != 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
 
    return string.toString().split ( delimiter.toString() );
}

/*
	basename('/www/site/home.htm', '.htm');

	'home'
*/
function basename(path, suffix) {    // Returns filename component of path
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ash Searle (http://hexmen.com/blog/)
    // +   improved by: Lincoln Ramsay
    // +   improved by: djmix
 
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}
function menu_select(obj, bg_color, color)
{
	obj.style['background']= bg_color;
	obj.style['color'] = color;
}
function menu_deselect(obj, bg_color, color)
{
	obj.style['background'] = bg_color;
	obj.style['color'] = color;
}

var choosen_menu_elem = 1;
var choosen_menu_elem_text = 'Весь список';
function menu_choose(obj, selected_border_color, border_color)
{	if (obj.id == 'menu_elem_1')
	{	choosen_menu_elem = 1;
		choosen_menu_elem_text = 'Весь список';
	}else
	if (obj.id == 'menu_elem_2')
	{
		choosen_menu_elem = 2;
		choosen_menu_elem_text = 'Работающие по найму';
	}else
	if (obj.id == 'menu_elem_3')
	{
		choosen_menu_elem = 3;
		choosen_menu_elem_text = 'Владельцы собственного бизнеса';
	}else
	if (obj.id == 'menu_elem_4')
	{
		choosen_menu_elem = 4;
		choosen_menu_elem_text = 'Инвесторы';
	}else
	if (obj.id == 'menu_elem_5')
	{
		choosen_menu_elem = 5;
		choosen_menu_elem_text = 'Организации';
	}
}
//.................  /Main_function.js  .................

//.................  Repair_page.js  .................
function time () {
    // http://kevin.vanzonneveld.net
    // +   original by: GeekFG (http://geekfg.blogspot.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: metjay
    // *     example 1: timeStamp = time();
    // *     results 1: timeStamp > 1000000000 && timeStamp < 2000000000
    
    return Math.round(new Date().getTime()/1000);
}

function popitup(url) {
	newwindow=window.open(url,'name', 'height=150,width=200');
	if (window.focus) {newwindow.focus()}
	return false;
}

$(window).load(function () 
{	if (is_auth != 0)
		window.setTimeout('getSecs()', 1000);

	if (is_auth != 0)
	{	$.post('/res/ajax_server/ajax_group_system.php', {'language' : u_lang, 'action' : 'inv_users', 'gid' : '1', 'user_id': '1'}, getGroupUsers);
		if (showInLeftMenu) getMessageCount();
	}
	/////////////////////////////////////////
});
//.................  /Repair_page.js  .................

function check_email(email) {
	var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
	email = removeSpaces(email);
	if (template.test(email)) {
		return true;
	}
	return false; 
}

function get_html_translation_table (table, quote_style) {
    var entities = {}, hash_map = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';

    useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';

    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');
        // return false;
    }

    entities['38'] = '&amp;';
    if (useTable === 'HTML_ENTITIES') {
        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';
        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';
        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';
        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';
        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';
        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';
        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';
        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';
        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';
        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';
        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';
        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';
        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';
        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';
        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';
        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';
        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';
        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';
        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';
        entities['255'] = '&yuml;';
    }

    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    entities['60'] = '&lt;';
    entities['62'] = '&gt;';


    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        hash_map[symbol] = entities[decimal];
    }
    
    return hash_map;
}

function html_entity_decode (string, quote_style) {
    var hash_map = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }

    // fix &amp; problem
    // http://phpjs.org/functions/get_html_translation_table:416#comment_97660
    delete(hash_map['&']);
    hash_map['&'] = '&amp;';

    for (symbol in hash_map) {
        entity = hash_map[symbol];
        tmp_str = tmp_str.split(entity).join(symbol);
    }
    tmp_str = tmp_str.split('&#039;').join("'");
    
    return tmp_str;
}

function user_profile_mark(obj, str)
{	obj.innerHTML = '<input type="hidden" value="' + str +'" name="uid[]"/><span class="dgray px_12" style="position:relative;left:0px;top:-15px">Отмечено</a>';
}

function alert_mode(u_id, d_id)
{	if(confirm("После подтверждения Вам будет задан этот вопрос. Вы согласны?"))
		location.href= "http://www.delovoymir.biz/ru/consultation/add/?is_question=0&to_user_id="+ u_id +"&did=" + d_id;
}