﻿function makeDate( input_month, input_day, input_year, changing) {
    var month = parseInt(input_month.options[input_month.selectedIndex].value);
    var day = parseInt(input_day.options[input_day.selectedIndex].value);
    var year = parseInt(input_year.options[input_year.selectedIndex].value);
    switch (month) {
	    case 4: case 6: case 9: case 11:
		    if (day == 31) { day = 30; input_day.selectedIndex = 29+1; }
		    break;
	    case 2:
		    if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
			    if (day > 29) {	day = 29; input_day.selectedIndex = 28+1;	}
		    }
		    else {
			    if (day > 28) { day = 28; input_day.selectedIndex = 27+1;	}
		    }
		    break;
    }
    if ((month==0)||(day==0)||(year==0)) {
	    month=0;
	    day=0;
	    year=0;
    }
    if (month < 10) {month = '0' + month; }
    if (day < 10) {day = '0' + day; }
    var nn =	year + '-' + month + '-' + day;
    if (nn=='0-00-00') {nn='';}
    return nn;
}
function makeStartDate(obj) {
    var fm = obj.form;
    obj.form.StartDate.value = makeDate(fm.datefrom_month, fm.datefrom_day, fm.datefrom_year);
}
function makeEndDate(obj) {
    var fm = obj.form;
    obj.form.EndDate.value = makeDate(fm.dateto_month, fm.dateto_day, fm.dateto_year);
}
    
function ClickAdvancedSearch() {
	var link = document.getElementById('aslilnk');
	var div = document.getElementById('advancedSearchContainer');
	if ((link) && (div)) {
		if (div.style.display == 'none') {
			div.style.display = 'block';
			link.className = 'open';
		} else {
			div.style.display = 'none';
			link.className = '';
		} // end if
	} // end if
	return false;
} // end function

function ShowAndCloseProjectItems(id) {
	var link = document.getElementById('cpil' + id);
	var body = document.getElementById('cpid' + id);
	if ((link) && (body)) {
		if (body.style.display == 'none') {
			body.style.display = 'block';
			link.className = 'open';
		} else {
			body.style.display = 'none';
			link.className = '';
		} // end if
	} // end if
	return false;
} // end function

function ShowAndCloseCommentItems(id) {
	var link = document.getElementById('ccil' + id);
	var body = document.getElementById('ccid' + id);
	if ((link) && (body)) {
		if (body.style.display == 'none') {
			body.style.display = 'block';
			link.className = 'open';
		} else {
			body.style.display = 'none';
			link.className = '';
		} // end if
	} // end if
	return false;
} // end function

function ShowBigPassageMap(id) {
	var div = document.getElementById('smic' + id);
	var div2 = document.getElementById('sbic' + id);
	if (div.style.display != 'none') {
		div.style.display = 'none';
		div2.style.display = 'block';
	} // end if
	return false;
} // end function

function CloseBigPassageMap(id) {
	var div = document.getElementById('smic' + id);
	var div2 = document.getElementById('sbic' + id);
	if (div.style.display == 'none') {
		div2.style.display = 'none';
		div.style.display = 'block';
	} // end if
	return false;
} // end function

function PrintPassageMap(id) {
	var container = document.getElementById('container');
	var div = document.getElementById('printImage');
	var from = document.getElementById('spic' + id);
	if ((container) && (div) && (from)) {
		div.innerHTML = from.innerHTML;
		div.className = 'allowPrint';
		container.className = 'noPrint';
		window.onafterprint =
			function () {
				div.innerHTML = '';
				div.className = '';
				container.className = '';
			} // end event
		window.print();
	} // end if
	return false;
} // end function

function ShowHideDiv(divID, a)
{
    var div = document.getElementById(divID);
    if (div.style.display == "none") {
        div.style.display = "block";
        a.className = "open";
    } else {
        div.style.display = "none";
        a.className = "";
    }
    return false;
}

function ShowHideDivWithCheckboxes(divID)
{
    var div = document.getElementById(divID);
    if (div.style.display == "none") {
        div.style.display = "block";
    } else {
        div.style.display = "none";
    }
    return true;
}

function ChangeCheckboxes(form_id, prefix, self)
{
    var form = document.getElementById(form_id);
    var form_l = form.length;
    for (var i=0; i<form_l; i++)
    {
        if(form[i].type == 'checkbox' && form[i].name.substr(0, prefix.length) == prefix)
        {
            form[i].checked = self.checked;
        }
    }
}

function SendLinkToEmail(email, id, type) {    
    switch (type)
    {
        case "News" : file = 'SendNewsToFriend.aspx'; break;
        case "Books" : file = 'SendBookToFriend.aspx'; break;
        case "Actions" : file = 'SendActionToFriend.aspx'; break;
        case "Articles" : file = 'SendArticleToFriend.aspx'; break;                
        case "Vacancies" : file = 'SendVacancyToFriend.aspx'; break;                
    }
    
    var event = function (result) { 
        alert(result); 
        email.value = ''; 
        email.form.visible = true;
        for (var i=0; i<email.form.elements.length; i++)
        {
            e = email.form.elements[i];
            if (e.type.toString() == 'submit') {e.style.display = '';}
        }
    }	
	var data = 'Email=' + email.value + "&ID=" + id;
	HttpRequest(site_path + 'AJAX/' + file + '?' + data, null, null, event);
    
    email.value = '';
    for (var i=0; i<email.form.elements.length; i++)
    {
        e = email.form.elements[i];
        if (e.type.toString() == 'submit') {e.style.display = 'none';}
    }
    email.value = 'Идет отправка...';
    return false;
}

function SendMailToFeedBack(f) {
    
    var data = 'Email=' + f.Email.value + "&FullName=" + f.FullName.value + "&Phone=" + f.Phone.value 
        + "&Icq=" + f.Icq.value + "&Message=" + f.Message.value + "&Recipients=";
    var Recip = new Array();
    for(i = 0; i < f.elements.length; i++) {
        if (f.elements[i].name == 'Recipients' && f.elements[i].checked)
            Recip.push(f.elements[i].value);    
    }
    data += Recip.join(',');
    
    var event = function (result) { alert(result); }
	HttpRequest(site_path + 'AJAX/SendFeedBackMail.aspx', data, null, event);
        
    return false;
}

function SubscribeUserFromPC(f) {
    var data = 'Email=' + f.Email.value + "&Act=" + f.Act.value + "&RegionID=" + f.RegionID.value;    
    var SubscribeTypes = '', Employees = '';
    for(i = 0; i < f.elements.length; i++) {   
        if (f.elements[i].type == 'checkbox' && f.elements[i].checked)
	    {
 		    if (f.elements[i].name.substr(0, 8) == 'Employee')
 		    {
 		        Employees += (Employees != '') ? ',' + encodeURIComponent(f.elements[i].value) : encodeURIComponent(f.elements[i].value);
 		    }
 		    if (f.elements[i].name.substr(0, 13) == 'SubscribeType')
 		    {
 		        SubscribeTypes += (SubscribeTypes != '') ? ',' + encodeURIComponent(f.elements[i].value) : encodeURIComponent(f.elements[i].value);
 		    } 		    
 		    data += "&" + f.elements[i].name + "=on";
	    }
    }
    data += "&SubscribeTypes=" + SubscribeTypes + '&Employees=' + Employees;
    var event = function (result) { alert(result); }
	HttpRequest(site_path + 'AJAX/SubscribeUserFromPC.aspx', data, null, event);
        
    return false;
}

function SelectAll(obj, f) {
    for(i = 0; i < f.elements.length; i++)
        if (f.elements[i].type == 'checkbox')
	        f.elements[i].checked = obj.checked;
}

function DecodeEmail(es)
{
    var res = "";
    for (i=es.length-1; i>=0; i--)
        res += es.charAt(i);
    return res;
}

function makeEmail(es)
{
    document.write(DecodeEmail(es));
}

function mailtoLink(es)
{
    document.location = "mailto:"+DecodeEmail(es);
    return false;
}
