var preload_q = 0;
var preload_r = 0;
var preload_state = 0;

function hide_preload()
{
	$('wait_tooltip').style.display = 'none';
}

function preload(state)
{
	if(state == 1)
	{
		preload_q++;
		$('wait_tooltip').style.display = 'block';
	}
	else
	{
		setTimeout(hide_preload, 100);
		preload_r++;
	}
	preload_state = preload_q - preload_r;
}

function error(err)
{
	//alert(1);
	//alert(lang['js_helper_err'] + err.statusText);
	preload(0);
}

function areyousure(url)
{
	if(confirm('Are you sure?'))
	{	
		document.location.href = url;
	}
}

function toInt(inp)
{
	if(parseInt(inp.value))
	{
		inp.value = parseInt(inp.value);
	}
	else
	{
		inp.value = '0';
	}
}

// Cross-browser addEventListener()/attachEvent() replacement.
function addEvent(elt, name, handler, atEnd) {
  name = name.replace(/^(on)?/, 'on'); 
  var prev = elt[name];
  var tmp = '__tmp';
  elt[name] = function(e) {
    if (!e) e = window.event;
    var result;
    if (!atEnd) {
      elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null; // delete() does not work in IE 5.0 (???!!!)
      if (result === false) return result;
    }
    if (prev) {
      elt[tmp] = prev; result = elt[tmp](e); elt[tmp] = null;
    }
    if (atEnd && result !== false) {
      elt[tmp] = handler; result = elt[tmp](e); elt[tmp] = null;
    }
    return result;
  }
  return handler;
}

var errorStackId = 0;
var errorShows = new Array();
var errorContainerToName = new Array();
function showInlineError(el, er)
{
	//alert(errorShows);
	//alert(errorContainerToName);
	//if(errorShows[el.name] == 'show') return;
	
 	var container = $(document.createElement("div"));
 	container.id = 'error_container_' + errorStackId;
 	container.innerHTML = er;
    container.addClassName("error-block");
    $(el.parentNode).addClassName("field-error");
    $(el.parentNode).appendChild(container);
    errorShows[el.name] = 'show';
    errorContainerToName[container.id] = el.name;
    setTimeout('turnOffInlineError("' + container.id +'")', 3000);
    errorStackId++;
}

function turnOffInlineError(eid)
{
	eid = $(eid);
	eid.remove(true);
    errorShows[errorContainerToName[eid]] = 'none';
}


function is_empty_input(el)
{
	el = $(el);
	$(el.parentNode).removeClassName("field-error");
	if(el.value.length) return false;
	//alert('pick_value');
	showInlineError(el, 'Fill all fields');
	el.focus();
	new Effect.Highlight(el);
	return true;
}
function is_fake_select(el)
{
	el = $(el);
	$(el.parentNode).removeClassName("field-error");
	if(el.value != -1) return false;
	//alert(lang['pick_value']);
	showInlineError(el, 'Pick value');
	el.focus();
	return true;
}
function is_bad_pass(el1, el2)
{
	el1 = $(el1);
	el1.parentNode.removeClassName("field-error");
	el2.parentNode.removeClassName("field-error");
	if(el1.value == el2.value) return false;
//	alert('password_not_the_same');
	showInlineError(el2, 'password_not_the_same');
	el1.focus();
	new Effect.Highlight(el1);
	new Effect.Highlight(el2);
	return true;
}

function is_bad_email(el) {
   el = $(el);	
   $(el.parentNode).removeClassName("field-error");
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = el.value;
   if(reg.test(address)) return false;
//   alert('not_valid_email');
   showInlineError(el, 'not_valid_email');
   new Effect.Highlight(el);
   return true;
}

function is_time(el)
{
	el = $(el);

	var reg = new RegExp('([0-9]|[01][0-9]|2[0123]):[012345][0-9]');
	if (reg.test(el.value) && (el.value.length < 6)) return false; 
	
	showInlineError(el, 'not valid date');
	new Effect.Highlight(el);
	
	return true;
}

function is_bad_int(el) {
   el = $(el);
   $(el.parentNode).removeClassName("field-error");
   var reg = /^([0-9]){1,11}$/;
   var inte = el.value;
   if(reg.test(inte)) return false;
   //alert(lang['not_valid_value']);
   showInlineError(el, lang['not_valid_value']);
   new Effect.Highlight(el);
   return true;
}

function setSelection(select, value)
{
	select.value = value;
}

function info(val)
	{
		var open = document.createElement("div");
		open.id = 'open';
		//open.className = 'open';
		open.setAttribute('style','position:absolute;left:0;top:0;background:red;border:#000 1px solid;width:75px;height:20px;padding:2px 2px 2px 30px;color:white;font-weight:bold;');
		
		var newContainer = document.createElement("div");
		newContainer.id = 'info';
		//newContainer.className = 'info';
		newContainer.setAttribute('style','z-index:10000;position:absolute;left:10px;top:10px;background:red;border:#000 1px solid;color:white;font-weight:bold;width:1000px;');
		
//		newContainer.getElementsByTagName('ul').getElementsByTagName('li').setAttribute('style','list-style:none;');
//		newContainer.getElementsByTagName('ul').setAttribute('style','float:left;');
		
		
		var body = document.getElementsByTagName('body')[0];

		var li = "list-style:none;";
		var ul = "float:left;";
		
		var info = '<ul style="' + ul + '">';
		var i = 0;
		
		for (item in val)
		{
			i++;
			if (i == 15){
				info = info + '</ul><ul style="' + ul + '">s';
				i=0;
			}
			info = info + '<li style="' + li + '" >' + item + '</li>';
		}
		
		
		
		info = info + '</ul>';
		info = info + '<div style="float:right;color:black;font-weight:bold;margin-top:4px;" id="close">close</div>';
		
		newContainer.innerHTML = info;
		
		open.innerHTML = "open";
		body.appendChild(open);
		
		body.appendChild(newContainer);
		newContainer.style.display= 'none';
		
		open.onclick = function(){
			newContainer.style.display= 'block';
			document.getElementById('close').onclick = function(){
				newContainer.style.display= 'none';
			}
		}
	}
	
function ondefault(variable, val)
{
	return typeof(variable) != 'undefined' ? variable : val;
}

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

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

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}