function $n(tagName, d)
{
  e = document.createElement(tagName);
  if (d) for (prop in d) e[prop]=d[prop];
  return e;
}
function add_field(field_name) {
  
  field = document.getElementById(field_name);
  if(field) {
    field.style.display = 'block';
  }
}

function build_input() {
  if (FILE_FIELDS.length >0) {
        var id_part = FILE_FIELDS.shift();
        document.getElementById(id_part).style.display = 'block';
 }
}

function add_field(field_name) {
  field = document.getElementById(field_name);
  if(field) {
    field.style.display = 'block';
  }
}
function openNewWindow(file,window) {
    msgWindow=open(file,window);
    if (msgWindow.opener == null) msgWindow.opener = self;
    return false
}

function showSubregions() {
	select = document.getElementById('id_regions')
	val = select.value;
	var tablica = subregions[val];
	var target = document.getElementById('subregions');
	if (tablica.length > 0) {
		for(var i=0; i<tablica.length; i++) {
			var s = new Option(tablica[i][1],tablica[i][0]);
			target.options[i] = s;
		}
	if (target.options.length > 0) {
	  target.style.display = 'block';
	}
	} else {
		target.options.length = 0;
	}
}

function ShowCommunicators() {
  var communicators = document.getElementById('id_communicator');
  var cum = document.getElementById('communicator_user_name');
  if (communicators.value != '') {
      cum.style.display = 'block';
  }
  else 
  {
    cum.style.display = 'none';
  }
}

function set_listener() {
  field = document.getElementById('id_communicator');
  field.onclick = ShowCommunicators;
}

function show_temporary_layer() {
/*
   method turn on display temporary layer wich contains indicator, 
  it's used in submiting competition content
*/
   if (document.getElementById('whait')) {
     document.getElementById('whait').style.display = 'block';
   }
}

function show_sections(category_seo) {
/*
function that show all category's section on main page.
Section are loaded by Ajax call.
*/
  var dynamic_list_root = $('dynamic_'+category_seo);

  var url = '/browse/show_sections/' + category_seo + '/';
  var pars ='';
  if (dynamic_list_root.childNodes.length > 3) {
   $('static_'+category_seo).style.display = 'none';
   dynamic_list_root.style.display = 'block';
  return false
}
 //static ul must be set display:none
  $('static_'+category_seo).style.display = 'none';
 //in dynamic ul we set ajax indicator 
  $('indicator_'+category_seo).style.display = 'inline';
  //var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: show_sections_response});
  var myAjax = new Ajax.Updater({success: 'dynamic_'+ category_seo}, url, { method: 'post', parameters: pars, 
    onFailure: reportError, asynchronous:false});
}

function reportError() {
 alert('There were some error, please report that issue');
}

function show_less(category_seo) {
   $('dynamic_'+category_seo).style.display = 'none';
   $('static_'+category_seo).style.display = 'block';
} 

function disable_subregions(number) {
  if (parseInt(number) == 1) {
    document.add_data.regions.disabled = false;
    subregion = $('id_subregion');
    subregion.options.selectedIndex = subregion.options.selectedIndex;
  } else if (parseInt(number) == 2) {
      subregion = $('id_subregion');
      subregion.options.selectedIndex = subregion.options.selectedIndex;
  } else {
    for (var i=0; i< document.add_data.regions.options.length; i++) {
      document.add_data.regions.options[i].selected = false;
    }
    document.add_data.regions.disabled = true;
    
  } 
}

function unmark_metropols_region() {
  subregion = $('id_subregion');
  subregion_checked = 0;
  for (var i=0; i< subregion.options.length; i++) {
    if (subregion.options[i].selected) 
      subregion_checked+= 1;
  }
  if (subregion_checked >1){
    //disable region select, here we deal with 'specific ' case 1
    //we check first radio, that add in one region
    document.add_data.regions_type[0].checked =true;
    // we clean disable region select
    for (var i=0; i< document.add_data.regions.options.length; i++) {
      document.add_data.regions.options[i].selected = false;
    }
    document.add_data.regions.disabled=true;
    return true  
  } else {
    return false;
  }
}  

function go_redirect(link_url) {
  document.location = link_url;
}

function addLoadEvent(func) {  
  var oldonload = window.onload;  
  if (typeof window.onload != 'function') {  
     window.onload = func;  
  } else {  
      window.onload = function() {  
      oldonload();  
      func();  
    }  
  }  
}

function video_uploader_run(id) {
  $('frm_'+id).submit();
}

function video_uploader_cancel(id) {
  $('vup_'+id).hide();
}

function gen_app_uid(v) {
  var chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

  var ret = '';
  for (var i=0; i<80; i++) {
    var c = chars[Math.floor(Math.random()*52)]
    ret = ret+c;
  }
  return ret;
}

function video_uploader_show(id) {
  var vup = $('vup_'+id);
  // TODO: recompute app_uid
  var aui;
  vup.getElementsBySelector('input').each(function(e) {
    if (e.name=='app_uid') aui=e;
    else e.value='';
  });
  aui.value=gen_app_uid();
  vup.show();
}

function iframe_load_complete(ife,ppe,hvi_id)
{
  return function() {
    var d = ife.contentDocument;
    if (d.location.href=='about:blank') return;
    alert("Uplad complete");
    ppe.hide();
    var app_uid;
    ppe.getElementsBySelector('input').each(function(e) {
      if (e.name=='app_uid') app_uid = e.value;
    });
    $(hvi_id).value=app_uid;
    alert("app_uid: "+app_uid);
  }
}

function build_safe_form(elt, action, new_id,hvi_id) {
  var bogus_iframe=$('__bogus_iframe');
  if (!bogus_iframe) {
    bogus_iframe=$n('iframe',{'id':'__bogus_iframe','name':'__bogus_iframe','src':'about:blank'});
    bogus_iframe.style.display='none';
    bogus_iframe.style.width='1350px';
    bogus_iframe.style.height='800px';
    $$('body')[0].appendChild(bogus_iframe);
    bogus_iframe.onload = iframe_load_complete(bogus_iframe, elt, hvi_id);
  }
  var f=$n('form',{'action':action,'id':new_id,'target':'__bogus_iframe','enctype':'multipart/form-data','method':'post'});
  f.appendChild(elt);
  $$('body')[0].appendChild(f);
}
