// --
function getRadioValue(elements)
{
//	var elements = obj.elements;

	for (var i = 0; i < elements.length; i++)
	{
		if (elements[i].checked)
		{
			return elements[i].value;
		}
	}

	return null;
}

function selectAll ( form_name, checked)
{
	var elements = document.forms[form_name].elements;

	for (var i = 0; i < elements.length; i++)
	{
		if ( elements[i].tagName == "INPUT" && elements[i].type == "checkbox" )
		{
//			debugger;
			elements[i].checked = checked;
		}
	}
}
function selectAllLink(name, selAllTitle, deselAllTitle)
{
	var ch		= document.getElementById('chall_' + name);

	try
	{
		var ch_div	= document.getElementsByName('chall_' + name + '_div');
		
		if (ch.checked) 
		{
			ch.checked = ''; 
			try {ch_div[0].innerHTML = selAllTitle;} catch(e) {}
			try {ch_div[1].innerHTML = selAllTitle;} catch(e) {}
		}
		else 
		{
			ch.checked = 'checked';  
			try {ch_div[0].innerHTML = deselAllTitle;} catch(e) {}
			try {ch_div[1].innerHTML = deselAllTitle;} catch(e) {}
		}
	} catch (e) 
	{
		var ch_div	= document.getElementsById('chall_' + name + '_div');
		
		if (ch.checked) 
		{
			ch.checked = ''; 
			try {ch_div.innerHTML = selAllTitle;} catch(e) {}
		}
		else 
		{
			ch.checked = 'checked';  
			try {ch_div.innerHTML = deselAllTitle;} catch(e) {}
		}
	}
	
	selectAll(name, ch.checked);	
}

function formToUrl(obj)
{
	var elem = obj.elements;
	var url = '';

	for (var i=0; i<elem.length; i++)
	{
		if (elem[i] && elem[i].name && elem[i].name != '_dont_save_')
		{
			if (elem[i].type == 'radio' || elem[i].type == 'checkbox')
			{
				if (elem[i].checked)
				{
					url = url + elem[i].name + '=' + encodeURIComponent(elem[i].value) + '&';
				}
			}
			else
			{
				url = url + elem[i].name + '=' + encodeURIComponent(elem[i].value) + '&';
			}
		
		/*	if ((navigator.appName.indexOf('Internet Explorer') != -1))
			{
			//alert(elem[i].name);
				if (obj.name == 'login' && elem[i].name == 'login')
				{
						dat = new Date(2020, 01, 01);
						setCookie('login', elem[i].value, dat);
				}
				
				if (obj.name == 'login' && elem[i].name == 'psw')
				{
						dat = new Date(2020, 01, 01); //alert(elem[i].value);
						setCookie('password', encode64(elem[i].value), dat);
				}
			}*/
		}
	}
	
	return url;
}
function formClear(obj)
{
	var elem = obj.elements;
	for (var i=0; i<elem.length; i++)
	{
		if (elem[i] && elem[i].name && elem[i].name != 'SELECT' && elem[i].type != 'button' && elem[i].type != 'hidden' && elem[i].type != 'select-one' && elem[i].type != 'radio')
		{
			if (elem[i].type == 'checkbox')
			{
				elem[i].checked = false;
			}
			else
			{
				elem[i].value = '';
			}
		}
	}
}

function callCalendar(name)
{
	blockById('blocking_gray');
	var calendarContainer = document.getElementById('callendar_container_'+name);
	calendarContainer.className = 'wyswyg_show';
}

function closeCalendar(name)
{
	var calendarContainer = document.getElementById('callendar_container_'+name);
	calendarContainer.className = 'wyswyg_hidden';
	unblockById('blocking_gray', 'no');
}

function callEditor(container)
{
	blockById('blocking_gray');
	wc = document.getElementById('wyswyg_container');
	bg = document.getElementById('blocking_gray');
	
	bg.style.height = document.body.scrollHeight + 'px';
	
	wc.className = 'wyswyg_show';
	wc.style.top = document.body.scrollTop + 100;

	if(!isIE)
	{
		var wdth = document.documentElement.clientWidth;
	}
	else
	{
		var wdth = document.body.clientWidth;
	}
	var oEditor = FCKeditorAPI.GetInstance('wyswyg_gate') ;

	document.getElementById('wyswyg_container').style.left = (wdth - 800) / 2;
	oEditor.SetHTML(container.innerHTML);
	curEditedElement = container.id.replace('div_', '');
}

function saveEditorChanges(act)
{
	var oEditor = top.FCKeditorAPI.GetInstance('wyswyg_gate') ;
	var container = document.getElementById('div_'+curEditedElement);
	var valueContainer = document.getElementById('hd_'+curEditedElement);

	if(act == 'yes')
	{
		valueContainer.value = oEditor.GetXHTML();
		container.innerHTML = valueContainer.value;
		oEditor.SetHTML('');
	}
	else
	{
		oEditor.SetHTML('');
	}

	curEditedElement = null;

	document.getElementById('wyswyg_container').className = 'wyswyg_hidden';

	unblockById('blocking_gray', 'no');
}

function selectCustomType(selWCustom, customVal)
{
	if(selWCustom.options[selWCustom.selectedIndex].value != customVal)
	{
		document.getElementById('cstm_inp_'+selWCustom.id).style.display = 'none';
	}
	else
	{
		document.getElementById('cstm_inp_'+selWCustom.id).style.display = 'block';
	}
}

function insertInSelect(selectElement, optionValue, optionText)
{
	var newOption = document.createElement('option');
	newOption.appendChild(document.createTextNode(optionText));
	newOption.setAttribute('value', optionValue);

	selectElement.appendChild(newOption);
}

function addSelElToList(dest)
{
	var cntr = document.getElementById('cntr_'+dest);
	var cntr_val = cntr.options[cntr.selectedIndex];

	if(cntr.value != 0)
	{
		var container = document.getElementById('div_cntr_'+dest);
		var hidden = document.forms['form_edit_classifier'].elements['h_cntr_'+dest];

		hidden.value = (hidden.value == '' ? cntr_val.value : hidden.value+','+cntr_val.value);

		count = (dest == 'from' ? ++countFrom: ++countTo);

		var div = document.createElement('div');
		var sdiv = document.createElement('span');
		div.setAttribute('id', 'div_'+dest+'_'+count);
		sdiv.setAttribute('id', 'sdiv_'+dest+'_'+count);
		sdiv.appendChild(document.createTextNode(cntr_val.text));
		var a = document.createElement('a');
		var img = document.createElement('img');
		img.setAttribute('src', delImg.src);
		img.setAttribute('style', 'border: 0px');
		a.setAttribute('href', 'javascript:void(0)');
		a.setAttribute('style', 'border: 0px;text-decoration: none;');
		a.setAttribute('onclick', "deleteCntr('"+dest+"', '"+cntr_val.value+"', '"+count+"')");
		a.appendChild(img);
		div.appendChild(sdiv);
		div.appendChild(a);

		cntr.remove(cntr.selectedIndex);

		container.appendChild(div);
	}
}

function deleteSelElFromList(dest, idVal, div_num)
{
	var hidden = document.forms['form_edit_classifier'].elements['h_cntr_'+dest];
	var idLine = hidden.value.toString();
	var idList = idLine.split(",");
	var container = document.getElementById('div_cntr_'+dest);
	var cntr = document.forms['form_edit_classifier'].elements['cntr_'+dest];

	hidden.value = '';

	for(var i = 0; i < idList.length; ++i)
	{
		if(idList[i] != idVal)
		{
			hidden.value = (hidden.value == '' ? idList[i] : hidden.value+','+idList[i]);
		}
		else
		{
			var delSubItem = document.getElementById('sdiv_'+dest+'_'+div_num);

			insertInSelect(cntr, idVal, delSubItem.innerHTML);
		}
	}

	var remElem = document.getElementById('div_'+dest+'_'+div_num);
    
    container.removeChild(remElem);
}

function hideElement(controlEl, id, val)
{
	var el = document.getElementById(id);
//	debugger;
	if(controlEl.tagName == 'INPUT')
	{
		if(controlEl.type == 'checkbox')
		{
			if(controlEl.checked == true)
			{
				el.style.display = 'block';
				return true;
			}
			else
			{
				el.style.display = 'none';
				return false;
			}
		}
	}
	else if(controlEl.tagName == 'SELECT')
	{
		if(controlEl.options[controlEl.selectedIndex].value == val)
		{
			el.style.display = 'block';
			return true;
		}
		else
		{
			el.style.display = 'none';
			return false;
		}
	}
}

function setDo(id, operation)
{
	var doEl = document.getElementById(id);
	
	doEl.value += operation;
}

var jsFormCallendar = {
	day: 		new Array(),
	month: 		new Array(),
	year: 		new Array(),
	weekFormat: null,
	monthNames:	new Array(),
	monthNamesI:	new Array(),
	weekDays:	new Array(),
	links:		new Array(),
	onclicks:	new Array(),
	forArchive:	null,
	dateDump:	new Array(),
	currDate:	new Array(),

	isLeapYear: function (year)
	{
		if (year % 4 == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	},

	getDays: function (month, year)
	{
		 var ar = new Array(13);
		 ar[0] = 31 ;
		 ar[1] = (this.isLeapYear(year)) ? 29 : 28;
		 ar[2] = 31 ;
		 ar[3] = 30 ;
		 ar[4] = 31 ;
		 ar[5] = 30 ;
		 ar[6] = 31;
		 ar[7] = 31 ;
		 ar[8] = 30 ;
		 ar[9] = 31 ;
		 ar[10] = 30 ;
		 ar[11] = 31 ;

		 return ar[month];
	},

	Print: function(callId)
	{

		var container = document.getElementById('callendar_container_'+callId);
		container.innerHTML = '';
		var displDate = new Date();

		if(this.year[callId])
		{
			displDate.setFullYear(this.year[callId]);
		}
		else
		{
			if (isIE)
			{
				this.year[callId] = displDate.getYear();
			}
			else
			{
				this.year[callId] = 1900 + displDate.getYear();
			}
		}
		
		if(this.month[callId])
		{
			displDate.setMonth(this.month[callId]);
		}
		else
		{
			this.month[callId] = displDate.getMonth();
		}
		
		displDate.setDate(1);
		
		this.setCurDate(callId);

		var dayInWeek = displDate.getDay();

		if(this.weekFormat == 'ru')
		{
			var dayOffset = dayInWeek == 0 ? 6 : dayInWeek - 1;
		}
		else
		{
			var dayOffset = dayInWeek;
		}

		var dayInMonth = this.getDays(this.month[callId], this.year[callId]);

		var monYear = document.createElement('div');
		var table = document.createElement('table');
		var body = document.createElement('tbody');
		var icont =  document.createElement('div');

		icont.id = 'callendar_container';

		this.generateHead(monYear, body, callId);
		this.generateBody(body, dayInMonth, dayOffset, callId);

		icont.appendChild(monYear);
		table.appendChild(body);
		icont.appendChild(table);
		
		var closeButtonContainer = document.createElement('div');
		//var closeButton = document.createElement('img');
//		closeButton.src = '';
//		closeButton.onclick = function(){closeCalendar(callId)};
//		closeButton.alt = "Close";
		closeButtonContainer.innerHTML = "Закрыть";
		closeButtonContainer.className = "pointer";
		closeButtonContainer.onclick = function(){closeCalendar(callId)};
		//closeButtonContainer.style = "cursor:pointer;";
		//closeButtonContainer.appendChild(closeButton);
		
		icont.appendChild(closeButtonContainer);
		container.appendChild(icont);
	},

	setCurDate: function(callId)
	{
		if(typeof(this.dateDump[callId]) == 'undefined')
		{
			this.dateDump[callId] = new Array();

			this.dateDump[callId]['day'] = this.day[callId];
			this.dateDump[callId]['month'] = this.month[callId];
			this.dateDump[callId]['year'] = this.year[callId];
		}
		
		if(typeof(this.currDate[callId]) == 'undefined')
		{
			this.currDate[callId] = new Array();
			var dt = new Date();

			this.currDate[callId]['day'] = dt.getDate();
			this.currDate[callId]['month'] = dt.getMonth();
			this.currDate[callId]['year'] = isIE ? parseInt(dt.getYear()) : 1900 + parseInt(dt.getYear());
		}
	},

	generateBody: function(body, dayInMonth, dayOffset, callId)
	{
		var link = this.links[callId];
		var onclick = this.onclicks[callId];

		var dayCounter = 1;
		var tr = document.createElement('tr');
		tr.className = 'row';

		for(var i = -dayOffset; i < dayInMonth + 7; i++)
		{

			if(dayCounter % 8 == 0)
			{
				body.appendChild(tr);
			}

			if(dayCounter % 8 == 0)
			{
				var tr = document.createElement('tr');
				tr.className = 'row';
				dayCounter = 1;
			}

			var td = document.createElement('td');

			if(i >= 0 && i < dayInMonth)
			{
				var d = i + 1;
				var m = this.month[callId];
				var y = this.year[callId];

				if(d == this.dateDump[callId]['day'] && m == this.dateDump[callId]['month'] && y == this.dateDump[callId]['year'])
				{
					td.className = "selected";
				}
				
				if(d == this.currDate[callId]['day'] && m == this.currDate[callId]['month'] && y == this.currDate[callId]['year'])
				{
					td.className = td.className == '' ? "current" : td.className+" current";
				}
					
				var subTd = document.createElement('a');
				this.setDayClass(subTd, dayCounter, 'we_day_p', 'smpl_day_p');
				
//				subTd.onclick = function(){jsFormCallendar.Set(callId, d, m, y)};
			//	subTd.onclick = "jsFormCallendar.Set('"+callId+"', "+d+", "+m+", "+y+")";
				subTd.setAttribute("href", "javascript:jsFormCallendar.Set('"+callId+"', "+d+", "+m+", "+y+")");
			//	subTd.setAttribute("onclick", "jsFormCallendar.Set('"+callId+"', "+d+", "+m+", "+y+")");
				subTd.appendChild(document.createTextNode(d));

			}
			else
			{
				var subTd = document.createTextNode('');
			}

			td.appendChild(subTd);
			tr.appendChild(td);
			dayCounter++;
		}
	},

	setDayClass: function(element, dnum, wks, sds)
	{
		if(this.weekFormat == 'ru')
		{
			if(dnum % 7 == 0 || (dnum + 1) % 7 == 0)
			{
				element.className = wks;
			}
			else
			{
				element.className = sds;
			}
		}
		else
		{
			if(dnum == 1 || dnum == 7 )
			{
				element.className = wks;
			}
			else
			{
				element.className = sds;
			}
		}
	},

	generateHead: function(monYear, body, callId)
	{
		monYear.className = 'top-cal';
		var aPrev = document.createElement('a');
		aPrev.className = 'arch_prev_mon';
		
		if(isIE)
		{
			aPrev.onclick = function(){jsFormCallendar.Prev(callId)};
		}
		else
		{
			aPrev.setAttribute('onclick', 'jsFormCallendar.Prev(\''+callId+'\'); return false;');
		}
		
		aPrev.appendChild(document.createTextNode('«'));
		var aNext = document.createElement('a');
		aNext.className = 'arch_next_mon';
		
		if(isIE)
		{
			aNext.onclick = function(){jsFormCallendar.Next(callId)};
		}
		else
		{
			aNext.setAttribute('onclick', 'jsFormCallendar.Next(\''+callId+'\'); return false;');
		}
		
		aNext.appendChild(document.createTextNode('»'));

		monYear.appendChild(aPrev);
		monYear.appendChild(document.createTextNode(' ' + this.monthNames[this.month[callId]] + ' ' + this.year[callId] + ' '));
		monYear.appendChild(aNext);

		var row = document.createElement('tr');
		row.className = 'arch_head';

		for(var i = 0; i < this.weekDays.length; i++)
		{
			var th = document.createElement('th');
			this.setDayClass(th, i + 1, 'h_we_day', 'h_smpl_day');
			th.appendChild(document.createTextNode(this.weekDays[i]));
			row.appendChild(th);
		}

		body.appendChild(row);
	},

	Prev: function(callId)
	{

		if (this.month[callId] == 0)
		{
			this.month[callId] = 11
			this.year[callId]--;
		}
		else
		{
			this.month[callId]--;
		}

		this.Print(callId);
	},

	Next: function(callId)
	{
		if (this.month[callId] == 11)
		{
			this.month[callId] = 0;
			this.year[callId]++;
		}
		else
		{
			this.month[callId]++;
		}

		this.Print(callId);
	},

	Reset: function(callId)
	{
		this.day[callId]		= document.getElementById('callendar_c_day_'+callId).value;
		this.month[callId]		= document.getElementById('callendar_c_month_'+callId).value;
		this.year[callId]		= document.getElementById('callendar_c_year_'+callId).value;
		this.month_name[callId] 	= this._month[this.month[callId]];

		this.Print(callId);
	},

	Set: function(callId, day, month, year)
	{
		document.getElementById('callendar_day_'+callId).value = day;
		document.getElementById('callendar_month_'+callId).value = month;
		document.getElementById('callendar_year_'+callId).value = year;
		
		var el = document.getElementById('callendar_date_div_'+callId);
		
		var divs = el.getElementsByTagName('div');
		if (divs.length > 0) el.removeChild(divs[0]);
		
		newdiv = document.createElement('div');
		newdiv.innerHTML = day + " " + this.monthNamesI[month] + " " + year;
		el.appendChild(newdiv);
		
		closeCalendar(callId);
//		SetClasses();
	}
}
