var soc_object_couter = 0;

var theForm = document.forms['person_form'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function doPostBack(eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.action.value = eventArgument;
        theForm.submit();
    }
}


function ax_show_form(id) {
    eval("document.getElementById('ax_caption_" + id + "').style.display='none'");
    eval("document.getElementById('ax_form_" + id + "').style.display='block'");
    eval("document.getElementById('ax_input_" + id + "').select()");
}
function ax_hide_form(id) {
    eval("document.getElementById('ax_caption_" + id + "').style.display='block'");
    eval("document.getElementById('ax_form_" + id + "').style.display='none'");
}

function ax_create_form(id) {
    var text = eval("document.getElementById('ax_caption_" + id + "').innerHTML");
    document.write("<div id='ax_form_name' style='display:none;'>");
    document.write("<input id='ax_input_" + id + "' type=text name='" + id + "' value='" + text + "' maxlength=255  class='input_implicit' style='width:100%;' >");
    document.write("<input type=submit value='Сохранить'>&nbsp;");
    document.write("<input type=reset value='Отменить' onClick='ax_hide_form(\"" + id + "\")'>");
    document.write("</div>");
}

function ax_switch(id) {
    var current_display = eval("document.getElementById('" + id + "_0').style.display");
    if (current_display == 'none') {
        eval("document.getElementById('" + id + "_0').style.display='block'");
        eval("document.getElementById('" + id + "_1').style.display='none'");
    }
    else {
        eval("document.getElementById('" + id + "_0').style.display='none'");
        eval("document.getElementById('" + id + "_1').style.display='block'");
    }

}


function ax_submenu_switch(current) {
    var submenu = new Array('plans', 'stages', 'curtasks', 'tasks');

    for (var i = 0; i < submenu.length; i++) {
        if (submenu[i] == current) {
            eval("document.getElementById('submenu_" + submenu[i] + "_lug').className='inset_on'");
            eval("document.getElementById('submenu_" + submenu[i] + "').style.display='block'");
        }
        else {
            eval("document.getElementById('submenu_" + submenu[i] + "_lug').className='inset_off'");
            eval("document.getElementById('submenu_" + submenu[i] + "').style.display='none'");
        }
    }
}

function ax_combobox_switch(id) {
    if (eval("document.getElementById('" + id + "_select').selectedIndex") == 1) // Второй пункт в списке!
    {
        eval("document.getElementById('" + id + "_0').style.display='none'");
        eval("document.getElementById('" + id + "_1').style.display='block'");
        eval("document.getElementById('" + id + "_text').focus()");
    }
}
function ax_combobox_hide(id) {
    eval("document.getElementById('" + id + "_1').style.display='none'");
    eval("document.getElementById('" + id + "_0').style.display='block'");
    ax_hide('tr_' + id);
}

function ax_show(id) {
    eval("document.getElementById('" + id + "').style.display='block'");
}
function ax_hide(id) {
    eval("document.getElementById('" + id + "').style.display='none'");
}

function ax_focus(id) {
    eval("document.getElementById('" + id + "').focus()");
}

function ax_preview(id) {

    eval("document.getElementById('" + id + "_preview').src = document.getElementById('" + id + "').value");
    eval("document.getElementById('" + id + "_delete').checked = false");
    eval("document.getElementById('" + id + "_preview_span').style.display='inline'");
}

function ax_preview_hide(id) {
    if (eval("document.getElementById('" + id + "_delete').checked"))
        eval("document.getElementById('" + id + "_preview_span').style.display='none'");
}

function ax_add_fields(readrootname, writerootname, insertAtTop) {
    var newFields = document.getElementById(readrootname).cloneNode(true);
    //newFields.id = "";
    //newFields.id = i++;
    newFields.style.display = "block";

    if (readrootname == 'container_ward') {
        soc_object_couter++;
        //        alert(soc_object_couter);
        var children = newFields.childNodes;
        for (var i = 0; i < children.length; i++) {
            var child = children[i];
            if (child.name == 'ward[]')
                child.name = 'ward[' + soc_object_couter + ']';

            if (child.name == 'ward_address[]')
                child.name = 'ward_address[' + soc_object_couter + ']';

            if (child.name == 'ward_address[]')
                child.name = 'ward_address[' + soc_object_couter + ']';

            if (child.name == 'ward_phone1[]')
                child.name = 'ward_phone1[' + soc_object_couter + ']';

            if (child.name == 'ward_boss[]')
                child.name = 'ward_boss[' + soc_object_couter + ']';

            if (child.name == 'help[]')
                child.name = 'help[' + soc_object_couter + '][]';
        }
    }

    var insertHere = document.getElementById(writerootname);
    if (insertAtTop) {
        var insertBefore = insertHere.parentNode.firstChild;
    }
    else {
        var insertBefore = insertHere;
    }

    insertHere.parentNode.insertBefore(newFields, insertBefore);
    return false;
}

function ax_del_fields(obj) {
    if (confirm("Уверены, что хотите удалить этот блок?")) {
        obj.parentNode.removeChild(obj);
    }
    return false;
}

function ax_radio_set(obj, newValue) {
    if (!obj)
        return;
    var radioLength = obj.length;
    if (radioLength == undefined) {
        obj.checked = (obj.value == newValue.toString());
        return;
    }
    for (var i = 0; i < radioLength; i++) {
        obj[i].checked = false;
        if (obj[i].value == newValue.toString()) {
            obj[i].checked = true;
        }
    }
}

function ax_select_set(obj, newValue) {
    for (var i = 0; i < obj.length; i++) {
        if (obj[i].value == newValue.toString())
            obj.selectedIndex = i;
    }
}


function ajax_load(url, callback, post, bAsync) {
    var ajaxObj;
    if (window.XMLHttpRequest) {
        ajaxObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return;
    }
    if (post && ajaxObj.setRequestHeader) {
        ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251;");
    }
	if (bAsync) {
		ajaxObj.onreadystatechange = function(){
			if (ajaxObj.readyState == 4) {
				if (ajaxObj.status == 200) 
					if (callback) 
						callback(ajaxObj);
				
			}
		}
	}
    ajaxObj.open((post ? 'POST' : 'GET'), url, bAsync);
	ajaxObj.send(null);
	if (!bAsync && callback)
        callback(ajaxObj);

}



function set_select_country() {
	 var combo;
	 var updateCombo = function(ajaxObj) {
	        if (!combo) return;
	        if (combo.innerHTML) {
	            combo.innerHTML = null;
	            var items = ajaxObj.responseText.split(";");
	            var count = items.length;
	            for (var i = 0; i < count; i++) {
	                var options = items[i].split("=");
	                combo.options[i] = new Option(options[0], options[1]);
	            }
	        }

	    }
	  var country_id;
	    combo = document.getElementsByName('country_id')[0];
	    if (combo != null) {
	    	country_id = combo.value;
        if(country_id != '')
        {
          if(document.forms['search_select_form'].elements['eparchy_id'])
          {
              ax_select_set(document.forms['search_select_form'].elements['eparchy_id'], '');
              document.forms['search_select_form'].elements['eparchy_id'].disabled = true;
          }
  	    }
  	    else
        {
          if(document.forms['search_select_form'].elements['eparchy_id'])
              document.forms['search_select_form'].elements['eparchy_id'].disabled = false;
        }
	    }
	    combo = document.getElementsByName('fo_id')[0];
	    if (combo != null) {
	        ajax_load("/social/index_ax.php?fo=1&country_id=" + country_id, updateCombo, null, false);
	    }
	    combo = document.getElementsByName('eparchy_id')[0];
	    if (combo != null) {
	        ajax_load("/social/index_ax.php?eparchy=1&country_id=" + country_id, updateCombo, null, false);
	    }
	    combo = document.getElementsByName('region_id')[0];
	    if (combo != null) {
	        ajax_load("/social/index_ax.php?region=1&country_id=" + country_id, updateCombo, null, false);
	    }
	
}



function set_select_geo() {
    var combo;
    var updateCombo = function(ajaxObj) {
        if (!combo) return;
        if (combo.innerHTML) {
            combo.innerHTML = null;
            var items = ajaxObj.responseText.split(";");
            var count = items.length;
            for (var i = 0; i < count; i++) {
                var options = items[i].split("=");
                combo.options[i] = new Option(options[0], options[1]);
            }
        }

    }
    var fo_id;
    combo = document.getElementsByName('fo_id' )[0];
	
    if (combo != null) {
        fo_id = combo.value;
        if(fo_id != '')
        {
          if(document.forms['search_select_form'].elements['eparchy_id'])
          {
              ax_select_set(document.forms['search_select_form'].elements['eparchy_id'], '');
              document.forms['search_select_form'].elements['eparchy_id'].disabled = true;
          }
  	    }
  	    else
        {
          if(document.forms['search_select_form'].elements['eparchy_id'])
              document.forms['search_select_form'].elements['eparchy_id'].disabled = false;
        }
    }
    combo = document.getElementsByName('eparchy_id')[0];
    if (combo != null) {
        ajax_load("/social/index_ax.php?eparchy=1&fo_id=" + fo_id, updateCombo, null, false);
    }
    combo = document.getElementsByName('region_id')[0];
    if (combo != null) {
        ajax_load("/social/index_ax.php?region=1&fo_id=" + fo_id, updateCombo, null, false);
    }
} 


function set_select_region() {
    var combo;
    var region_id;
    combo = document.getElementsByName('region_id' )[0];
	
    if (combo != null) {
        region_id = combo.value;
        if(region_id != '')
        {
          if(document.forms['search_select_form'].elements['eparchy_id'])
          {
              ax_select_set(document.forms['search_select_form'].elements['eparchy_id'], '');
              document.forms['search_select_form'].elements['eparchy_id'].disabled = true;
          }
  	    }
  	    else
        {
          if(document.forms['search_select_form'].elements['eparchy_id'])
              document.forms['search_select_form'].elements['eparchy_id'].disabled = false;
        }
    }
}//set_select_geo




