/*******************************************************************************
COMMON HANDLING
*******************************************************************************/

function ClustrMaps() 
{
	window.location = 'http://www3.clustrmaps.com/counter/maps.php?url=http://www.forex4you.org'; 
}

function setCommand(f, cmd, id) 
{ 
	if (validateNotEmpty(cmd)) {
		f.command.value = cmd.replace('_cmd', '');
		if (id) {
			f.id.value = id;
		}
		f.submit();
		return true;
	} else {
		return false;
	}
}

function OpenWin(url, framename, width, height, left, top, resizable) 
{
	left = (left) ? left : '50';
	top  = (top) ? top : '50';
	resizable  = (resizable) ? resizable : '1';
	window.open(url,framename, 'width=' + width + ',height=' + height + ',left='+left+',top='+top+',screenX=10,screenY=20,toolbar=0,scrollbars=1,menubar=0,resizable=' + resizable + ',scroll=1,status=1');
}

function check_ways(f, key) 
{
	eval("var the_way = f." + key + ".value;");
	if (validateNotEmpty(the_way)) {
		window.location = the_way; 
	} else { 
		f.reset(); 
	}
}

function change_keyword(f, key, change) 
{
	eval("var the_way = f." + key + ".value;");
	if (validateNotEmpty(the_way)) {
		window.location = "?" + change + "_cmd_" + the_way; 
	} else { 
		f.reset(); 
	}
}

function CheckDel(f, cmd, warning) 
{ 
	if (confirm(warning)) { 
		setCommand(f, cmd);
	} else { 
		return false; 
	} 
}

function getCheckedItems(f, item, type) 
{ 
	checkedItems = 0; 
	if (!type) {
		type = "checkbox";
	}
	for (i=0; i<f.elements.length; i++) { 
		if ((f.elements[i].type.toLowerCase() == type) 
			&& (f.elements[i].checked) 
			&& (f.elements[i].name.toLowerCase() == item)) { 
			checkedItems++ 
		}
	}
	return checkedItems;
}

var check_flag = false;

function checkAll(f, msg_check_all, msg_uncheck_all, item)
{
	check_flag = !check_flag;
	for (i=0; i < f.elements.length; i++) {
		if (f.elements[i].type.toLowerCase()=="checkbox" && f.elements[i].name.toLowerCase()==item) {
			if (!f.elements[i].disabled) {
				f.elements[i].checked = check_flag;
			}
		}
	}
	if (check_flag) {
		f.check_all.value = msg_uncheck_all;
	} else {
		f.check_all.value = msg_check_all;
	}
}   

function checkAll2(f, status, item)
{
	for (i = 0; i < f.length; i++)
	{
		if (f.elements[i].name == item) 
		{ 
			f.elements[i].checked = status;
		}
		
	}
}

function checkRemove(f, cmd, item, lbl_check_all, lbl_delete, lbl_not_exists, lbl_are_you_sure, lbl_one, lbl_five, lbl_more) 
{ 
	checkedItems = getCheckedItems(f, item);

	if (checkedItems > 0) {
		if (checkedItems == 1) {
			getConfirm(f, cmd, lbl_delete + ' ' + checkedItems + ' ' + lbl_one + '. ' + lbl_are_you_sure);
		} else if (checkedItems > 1 && checkedItems <= 4) {
			getConfirm(f, cmd, lbl_delete + ' ' + checkedItems + ' ' + lbl_five + '. ' + lbl_are_you_sure);
		} else if (checkedItems > 4) {
			getConfirm(f, cmd, lbl_delete + ' ' + checkedItems + ' ' + lbl_more + '. ' + lbl_are_you_sure);
		}

		check_flag = true;
		//checkAll(f, lbl_check_all, '', item); // чёта у мя не работало толком всё иначе 8-)

		return false; 
	} else {
		alert(lbl_not_exists);
		return false;
	}
}


function getConfirm(f, cmd, msg) 
{ 
	var ok = confirm(msg);
	if (ok) { 
		setCommand(f, cmd);
	} else { 
		return false; 
	} 
}

/* Функция по указанным параметрам скрывает или показывает фрейм без перегрузки страницы */
var collapsed = false;

function collapseFrame(frame1, frame2, img, field, value1, value2, value, open_frame_lbl, collaps_frame_lbl) 
{
	collapsed =! collapsed;

	collaps_img = document.getElementsByName(img);
	collaps_frm1 = document.getElementsByName(frame1);
	collaps_frm2 = document.getElementsByName(frame2);

	if (collapsed) {
		collaps_img[0].setAttribute('src', images_path + 'icons/open.gif');
		collaps_img[0].setAttribute('alt', open_frame_lbl);
		collaps_frm1[0].setAttribute(field, 0);
		collaps_frm2[0].setAttribute(field, value);
	} else {
		collaps_img[0].setAttribute('src', images_path + 'icons/collaps.gif');
		collaps_img[0].setAttribute('alt', collaps_frame_lbl);
		collaps_frm1[0].setAttribute(field, value1);
		collaps_frm2[0].setAttribute(field, value2);
	}
}

/* Функция управляет чекбоксами use_this_item */
var check_use_this_item_flag = false;	

function checked_similar(f, name)
{
	for (i=0; i < f.elements.length; i++) {
		if (f.elements[i].type.toLowerCase()=="checkbox" && f.elements[i].name.toLowerCase()==name) {
			f.elements[i].checked = !check_use_this_item_flag;
		}
	}
	check_use_this_item_flag = !check_use_this_item_flag;
}   

// Динамический вызов скрипта
function r_sc(href,rnd)
{
if (rnd) href+=(/\?/.test(href)?'&':'?')+"rnd="+(Math.floor((new Date().getTime())/rnd))
var script = document.createElement('SCRIPT')
script.type='text/javascript' 
script.src=href
document.documentElement.firstChild.appendChild(script)
}


function toggleDisplay(id)
{
 document.getElementById(id).style.display=document.getElementById(id).style.display=='none'?'':'none'
}

function objectPosition(o) {
var l=t=0
if(o.offsetParent)
{l=o.offsetLeft;t=o.offsetTop;
while(o=o.offsetParent){l+=o.offsetLeft;t+=o.offsetTop}
}
return [t,l];
}

function showDiv(url,parent,id)
{
 if (document.getElementById(id)) {document.getElementById(id).style.display='';return false;}
 var div=document.createElement('DIV')
 pos=objectPosition(parent)
// alert(pos)
// div.style.width='400px'
 div.id=id

 if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) pos[1]-=300
 div.className='popdiv'
 div.style.top=pos[0]+'px'
 div.style.left=pos[1]+parent.offsetWidth+5+'px'
// div.style.left=pos[1]+'px'
// div.style.left=parent.offsetWidth+5+'px'
 div.innerHTML='<b>...</b>'
// parent.parentNode.appendChild(div)
 document.body.appendChild(div)
 r_sc(url)

 //document.documentElement.firstChild.appendChild(div)
}


// Withdrawal all control 
function withdrawal_toggle_all(o)
{
 var f=o.form;
 if (!o.checked)
 {
  f.amount.value=prev_amount;
  f.amount.disabled=false;

  f.is_block.checked=prev_is_block;
  f.is_block.disabled=false;
 }
 else
 {
  prev_amount=f.amount.value;
  f.amount.value=mt_balance;
  f.amount.disabled=true;

  prev_is_block=f.is_block.checked;
  f.is_block.checked=true;
  f.is_block.disabled=true;
 }
}



// Kayako LiveHelp
var chatwindow = false;

function startChat(lang)
{   
  if (chatwindow && !chatwindow.closed) return false;

  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
	winH = myHeight;
	winW = myWidth;

	docWidth = (winW-500)/2;
	docHeight = (winH-480)/2;

	chatwindow = window.open("https://account.forex4you.org/"+lang+"/livehelp","customerchat", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=500,height=480,left="+docWidth+",top="+docHeight);
}

function set_cookie ( 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 );
  } else {
	var expires = new Date ( );  // current date & time
	expires.setDate ( expires.getDate() + 365 ); 
  }
  
  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 delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


var interval;
var quotes_query = 0;
var quotesStarted = false;
var fx4u_update_delay;

function initQuotes(delayInSec) {
	fx4u_retrieve_quotes();
	fx4u_update_delay=delayInSec;
	if (get_cookie('no_quotes_update')) {
		stopQuotes();
	} else {
		startQuotes();
	}
	
}

function stopQuotes() {
	if (typeof fx4u_update_timer!='undefined') clearInterval(fx4u_update_timer);
	quotesStarted = false;
	var quotesSwitch = document.getElementById('toggle_quotes'); 
	if (quotesSwitch) quotesSwitch.className = 'icon play';
}

function startQuotes() {
	quotesStarted = true;
	var quotesSwitch = document.getElementById('toggle_quotes'); 
	if (quotesSwitch) quotesSwitch.className = 'icon pause';
	if (fx4u_update_delay) fx4u_update_timer=setInterval("fx4u_retrieve_quotes();",fx4u_update_delay*1000);
}

function toggleQuotes() {
	if (quotesStarted) {
		stopQuotes();
		set_cookie('no_quotes_update',1);
	} else {
		startQuotes();
		delete_cookie('no_quotes_update');
	}
}

