//ajax functions

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

var output = new Array();
var last_output = 0;
var xmlHttp = null;
function execute_ajax(method, url, params, onSuccess, onFailure)
{
	output[last_output] = null;

	xmlHttp=GetXmlHttpObject();
	
	xmlHttp.onreadystatechange = function() 
	{
		if(xmlHttp.readyState == 4 && (xmlHttp.status && xmlHttp.status == 200)) 
		{
			output[last_output] = xmlHttp.responseText;
			if(onSuccess instanceof Function)
			{
				onSuccess(xmlHttp);
			}
		}
		else if(xmlHttp.readyState == 4 && (xmlHttp.status && xmlHttp.status != 200))
		{
			if(onFailure instanceof Function)
			{
				onFailure(xmlHttp);
			}
		}
	}

	switch(method)
	{
		case "GET":
			xmlHttp.open(method, url + "?" + params, true);
			xmlHttp.send(null);
			break;
		case "POST":
			xmlHttp.open(method, url, true);

			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");

			xmlHttp.send(params);
			break;
	}
}

function fill_ajax_target(target, onum)
{
	if(output[onum] != null)
	{
		document.getElementById(target).innerHTML = output[onum];
		output[onum] = null;
	}
	else
	{
		setTimeout("fill_ajax_target('" + target + "', " + onum + ")", 100);
	}
}
//end of ajax functions

//events functions

function addEvent(elm, evType, fn, useCapture){
	if (elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} 
	else if (elm.attachEvent) 
	{
		 var r = elm.attachEvent('on' + evType, fn);
		 return r;
	} 
	else 
	{
		elm['on' + evType] = fn;
	}
}
//end of events functions

//input functions

var allowed_chars = Array();
var allowed_keys = Array();

allowed_chars["year"] = ",48,49,50,51,52,53,54,55,56,57,";
allowed_keys["year"] = ",8,9,37,39,";

allowed_chars["double"] = ",44,46,48,49,50,51,52,53,54,55,56,57,";
allowed_keys["double"] = ",8,9,37,39,";

allowed_chars["int"] = ",48,49,50,51,52,53,54,55,56,57,";
allowed_keys["int"] = ",8,9,37,39,";

function check_input(container, event, type)
{
	var all_chars = allowed_chars[type];
	var all_keys = allowed_keys[type];
	var key;

	if(event.keyCode)
	{
		key = event.keyCode;
	}
	else
	{
		key = event.charCode;
	}

	if(all_chars.indexOf("," + key + ",") == -1 && all_keys.indexOf("," + event.keyCode + ",") == -1 )
	{
		if(event.preventDefault)
		{
			event.preventDefault();
		}
		else
		{
			event.keyCode = 0;
		}
	}
}
//end of input functions


//string function

String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}

//end of string functions


//array functions
Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return true;
	}
	return false;
}
//end of array functions

function flybox(title) {

	/// ### Display block
	var browser = navigator.userAgent;
	if(browser.indexOf("MSIE 6") !== -1) {var scroll = document.documentElement.scrollTop; }
	else if(browser.indexOf("MSIE 7") !== -1) { var scroll = document.getElementsByTagName('body').item(0).scrollTop; }
	else { var scroll = document.getElementsByTagName('body').item(0).scrollTop; }

	top.document.getElementById('flydiv1').style.top = scroll;
	top.document.getElementById('flytle_title').innerHTML = title;
	top.document.getElementById('flydiv1').style.display = 'block';
	top.document.getElementById('flydiv').style.display = 'block';

}

function binitFade() {
	top.document.getElementById('flydiv1').style.display = 'none';
return false;
}

function bRep() {
	top.document.getElementById("ttf_ttf_").innerHTML="Доклада е изпратен успешно. Благодарим за вашето съдействие!";
	top.document.getElementById("flyerftr").innerHTML="";
	setTimeout("top.document.getElementById(\'flydiv1\').style.display = \'none\'",2000);
}

function showAjaxProgress(timeout) {
	if(document.getElementById("ajaxProgress")) {
		document.getElementById("ajaxProgress").style.display = "block";
	}
}

function hideAjaxProgress(timeout) {
	t = setTimeout(actualHideAjaxProgress, (timeout || 100));
}

function actualHideAjaxProgress() {
	if(document.getElementById("ajaxProgress")) {
		document.getElementById("ajaxProgress").style.display = "none";
	}
}

var VER_IMG_ID = null;
function activateReloadVerImg(imgContainerId, triggerContainerId) {
    if (! document.getElementById(imgContainerId) || ! document.getElementById(triggerContainerId)) {
        return false;
    }
    
    VER_IMG_ID = imgContainerId;
    var imgCont = document.getElementById(imgContainerId);
    var trigCont = document.getElementById(triggerContainerId);
    
    trigCont.onclick = function () {
        var method = 'GET';
    	var url = '/ajax/genVerImg.php'; 
    	var param = 'i=1';
    	
    	execute_ajax(method, url, param, updateVerImg);
    }
}

function updateVerImg(xmlHttp) {
    if  (xmlHttp && xmlHttp.responseText != '') {
        document.getElementById(VER_IMG_ID).src = xmlHttp.responseText;
    }
}