//-------------------------------------------------------------
// функция проверки обязательных полей
//-------------------------------------------------------------
function CheckReqField(field)
{

	if (typeof(fldreq)!='undefined')
	{

		var k=0;
		for(k=0;k<fldreq.length;k++){
			if(document.getElementById(fldreq[k][0])){
				var block=document.getElementById(fldreq[k][0]);
				switch(fldreq[k][1]){
				case 'input':
					switch(block.type){
						case 'text':
						case 'password':
							if(block.value.length==0){
								//alert('Заполните поле - '+fldreq[k][2]);
								DisabledSubmit(true,fldreq[k][2]);
								return false;
							}
							break;
						case 'radio':
							var arr_r_name = new Array();
							elem = document.getElementsByTagName('input');
							var i, iarr;
							for( i = 0, iarr = 0; i < elem.length; i++) {
							   var att = elem[i].getAttribute("name");
							   if(att == block.name) {
								   arr_r_name[iarr] = elem[i].checked;
								   iarr++;
							   }
							}
							var j=0;
							for(i=0; i<arr_r_name.length; i++){
								if(arr_r_name[i]==true) j=1;
							}
							if(j==0){
								//alert('Выберите - '+ fldreq[k][2]);
								DisabledSubmit(true,fldreq[k][2]);
								return false;
							}
							break;
						case 'checkbox':
							if(block.checked==false){
								//alert('Выберите - '+ fldreq[k][2]);
								DisabledSubmit(true,fldreq[k][2]);
								return false;
							}
							break;
						default:
							break;
					}
				break;
				case 'select':
					if(block.value==''){
						//alert('Выберите значение из списка - '+fldreq[k][2]);
						DisabledSubmit(true,fldreq[k][2]);
						return false;
					}
				break;
				case 'textarea':
					if(block.value.length==0){
						//alert('Заполните поле - '+fldreq[k][2]);
						DisabledSubmit(true,fldreq[k][2]);
						return false;
					}
				break;
				default:
				break;
				}
			}//if end
		}//for end


	}//if (typeof(fldreq)!='undefined')

DisabledSubmit(false);

}//function CheckReqField(field)

//---------------------------------------------------------------------------
// функция включает и выключает кнопку отправки
//---------------------------------------------------------------------------
function DisabledSubmit(mod,text)
{

	if (mod)
	{		alert ("Пожалуйста, заполните поле: «"+text+"»");		return false;
	}
	else {		return true;
	}

	if (typeof(document.getElementById('mainsubmit'))!='undefined')
	{
		if (document.getElementById('mainsubmit')!=null)
		{
			if (mod)
			{
				document.getElementById('mainsubmit').disabled=true;
			}
			else {
				document.getElementById('mainsubmit').disabled=false;
			}
		}//if (document.getElementById('mainsubmit')!=null)
	}//if (typeof(document.getElementById('MainSubmit'))!='undefined')
	else {

		//alert('error');

	}//else {

}//function (mod)

var FileIdArray = 	new Array;
var divFileClass = 	"divFileClass";

function AddFileField(th)
{

	//дабы не пользовалься всякими гетбуид, однозначно определяем элимент куда будет добавлять
	var Eliment = 		th.parentNode.firstChild;

	//определили название базового элимента
	var FirstName = 	th.parentNode.firstChild.firstChild.name;

	var FirstSize = 	th.parentNode.firstChild.firstChild.size;

	//определяем начальное положение id по умолчанию
	var j = 0;

	//выставляем начальное положение id
	for(i=0;i<FileIdArray.length;i++)
	{

		//файловое поле уже есть в массиве, возвращаем его id
		if (FileIdArray[i][0] == FirstName)
		{
			j = 	i;
			break;
		}//if (FileIdArray[i][0] == FirstName)

	}//for(i=0;i<nodei.length;i++)

	//будем добавлять поле, для того чтобы не пересекались надо увеличить счетчик
	if (FileIdArray.length > 0)//чтение элиментов
	{
		FileIdArray[j][1]++;
	}//if (j > 0)
	else {//запись элиментов

		//если такого инпут файла ещё небыло, надо добавить его
		j = 	FileIdArray.length;

		FileIdArray[j] = 		new Array;
		FileIdArray[j][0] = 	FirstName;
		FileIdArray[j][1] = 	0;

	}//else {

	var div = 		document.createElement("div");
	div.id = 		FirstName + FileIdArray[j][1] + 'div';

	//div.class = 	divFileClass;
	// Добавляем HTML-контент с пом. свойства innerHTML
	div.innerHTML = '<input type="file" name="'+ FirstName + FileIdArray[j][1] +'" id="'+ FirstName + FileIdArray[j][1] +'"><input id type="button" name="button" value="Удалить" onClick="DeleteFileField(this)">';
	// Добавляем новый узел в конец списка полей
	Eliment.appendChild(div);

	document.getElementById(FirstName + FileIdArray[j][1]).size = 	FirstSize;

}//function AddFileField()

function ge(id)
{
	if (typeof(document.getElementById(id)) != 'undefined')
	{
		if (document.getElementById(id) != null)
		{
			return document.getElementById(id);
		}
	}
return false;
}

function nomove()
{
}

//-------------------------------------------------------------
// ф-ия для показа инвестиционных проектов
//-------------------------------------------------------------
function showinvest(id)
{
	if (ge('invest_time_'+id).style.display == "none")
	{
		ge('invest_time_'+id).style.display =	"";
		ge('invest_budget_'+id).style.display =	"";
		ge('invest_financ_'+id).style.display =	"";
		ge('invest_descr_'+id).style.display =	"";
		ge('invest_stat_'+id).style.display =	"";
		ge('invest_local_'+id).style.display =	"";
	}
	else {		ge('invest_time_'+id).style.display =	"none";
		ge('invest_budget_'+id).style.display =	"none";
		ge('invest_financ_'+id).style.display =	"none";
		ge('invest_descr_'+id).style.display =	"none";
		ge('invest_stat_'+id).style.display =	"none";
		ge('invest_local_'+id).style.display =	"none";
	}
}//function showinvest(id)


/* getElementByClass
/**********************/
var allHTMLTags = new Array();

function getElementByClass(theClass) {

    //Create Array of All HTML Tags
    var allHTMLTags=document.getElementsByTagName('*');

    //Loop through all tags using a for loop
    for (i=0; i<allHTMLTags.length; i++) {

	    //Get all tags with the specified class name.
	    if (allHTMLTags[i].className==theClass) {

		    //Place any code you want to apply to all
		    //pages with the class specified.
		    //In this example is to “display:none;” them
		    //Making them all dissapear on the page.

		    allHTMLTags[i].style.display='none';

	    }//if (allHTMLTags[i].className==theClass) {

    }//for (i=0; i<allHTMLTags.length; i++) {

}//function getElementByClass(theClass) {


/*	ф-ия показа/скрытия блоков

	@param
		id 	- id блока
		it 	- ссылка на чекбокс, который отвечает за блок
		
*/
function ShowGroup(id,it)
{
	
	if ($(it).attr('checked'))
	{
		
		if (block_start)
		{
			
			if (block_arr[id] === true)
			{
				
				$("#group_id_"+id).show();	
				
			}
			
		}
		else {
			
			$("#group_id_"+id).show();
			
		}
		
	}//if ($(it).attr('checked'))
	else {
		
		$("#group_id_"+id).hide();
		
	}
	
}//function ShowGroup()


/*	ф-ия поиска

	@param
		search_str 	- где ищем
		search_q 	- что ищем
		
	@return
		true/false
*/
function search(search_str,search_q)
{
 
     
 
     //var str     = '1234567890';
     
     var pre_q   = /\?/g
     
     var reg_pre     = search_q.replace(pre_q, ".?")
     
     var pre_q   = /\*/g
     
     reg_pre         = reg_pre.replace(pre_q, ".*");
     
     //alert ("var reg = '/.*"+reg_pre+".*/'");
     
     eval("var reg = /.*"+reg_pre+".*/");
     
     
     var result  = reg.test(search_str) ? true : false
     
     return result;
     
}//function search(search_str,search_q)


function SSsearch()
{	
	
	TimeBreak = timestamp()+0.5;
	
	setTimeout("Ssearch()",1000);	
	
}//function SSsearch()

function Ssearch()
{

	if (timestamp() >= TimeBreak)
	{
		//переставляем счестчик чтобы не было дополнительных запросов
		TimeBreak = timestamp()+0.5;

		var i 		= 0;
		
		var str_s 	= "";
		var str_q 	= $("#search_str").val();
		var result 	= "";
		block_start 	= true;
		block_arr 	= new Array();
		
		$(search_array).each(function(key,val) {
		    i++;
		    //if (i < 5)
		    //{
			str_s 	= val[2];
			result 	= search(str_s,str_q);
			//alert (str_s + '|' + str_q + '|' + result);
			
			$("#group_id_"+val[0]).hide();
			
			if (result)
			{
				
				block_arr[val[0]] 	= true;
				
				$("#number_id_"+val[1]).show();
				
			}//if (result)
			else {
				
				$("#number_id_"+val[1]).hide();
				
			}//else {
			
		    //}//if (i < 20)
		    //return;
		});
		
		
		$(block_arr).each(function(key,val)
		{
			
			if (val === true)
			{
				
				if ($("#group_default_"+key).attr('checked'))
				{
					
					$("#group_id_"+key).show();
					
				}//if ($("#group_default_"+key).attr('checked'))
				
			}//if (val === true)
			
			//alert (key+'|'+val);	
			
		});


	}//if (timestamp() >= TimeBreak)

	
}//function Ssearch()


function timestamp()
{
	return parseInt(new Date().getTime().toString().substring(0, 10))
}
