﻿
function dynamicSelect(id1, id2) {
	// Feature test to see if there is enough W3C DOM support
	if (document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");

		// Onload init: call a generic function to display the related options in the dynamic select box
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		};
	}
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	var ValueSel = sel2.options[sel2.selectedIndex].value;
	
	while (sel2.options.length) {
		sel2.remove(0);
	}
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	var pattern3 = new RegExp("( |^)(" + "all" + ")( |$)");
	
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2) || clonedOptions[i].className.match(pattern3)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			//sel2.appendChild(clonedOptions[i].cloneNode(true));
			var tempOption = document.createElement( "OPTION" );
			sel2.options.add( tempOption );
			tempOption.text = clonedOptions[i].text;
			tempOption.value = clonedOptions[i].value;
			if (clonedOptions[i].value == ValueSel ) {
			tempOption.selected = true;
			}
		}
	}
}

function DoMain( c, cval, sel )
{
	var main = document.getElementById( 'main' );
	for( i=0;i<c.length;i++ )
	{
		main[i] = new Option( c[i], cval[i] );
		if( cval[i] == sel )	main[i].selected = true;
	}
}
function DoRub( m, mval, sel )
{
	var rub = document.getElementById( 'rub' );
	rub.length = 0;
	
	for( i=0; i<m.length; i++ )
	{
		if( !mval[i] )	mval[i]="0";
		rub[i] = new Option( m[i], mval[i] );
		if( mval[i] == sel )	rub[i].selected = true;
	}
}
function DoMark( t, tval, sel )
{
	var mark = document.getElementById( 'mark' );
	mark.length = 0;
	
	for( i=0; i<t.length; i++ )
	{
		if( !tval[i] )	tval[i]="0";
		mark[i] = new Option( t[i], tval[i] );
		if( tval[i] == sel )	mark[i].selected = true;

	}	
}

function Prepni(id)
{
var x = document.getElementById( id );
if( x.style.display == 'none' )
	x.style.display = 'block';
else
	x.style.display = 'none';
}

function HideAll()
{
var x = document.getElementById('left-column').getElementsByTagName('ul')[0];
var y = x.getElementsByTagName('ul');
for( i=0;i<y.length;i++ )
 {
   if( y[i].className != "active" ) y[i].style.display='none';
 }
}

function NewR(link)
{
  open(link, "", "scrollbars=yes, width=750, height=600,status=yes,resizable=yes,directories=yes,toolbar=yes,menubar=yes,location=yes" );
}
function detail(src,id,arr)
{
  var x = document.getElementById('dimg');
  x.style.backgroundImage = "url("+src+")";

  for( i=0; i<arr.length; i++ )
  {
  	var z = document.getElementById('img'+arr[i]);
  	if( z )
  		z.style.display = 'none';
  }
  
  var y = document.getElementById('img'+id);
  y.style.width = '330px';
  y.style.height = '330px';
  y.style.display = 'block';
}

function Show(obj)
{
     var x = document.getElementById(obj);
     if( x )
	    x.style.visibility='visible';
}
function Hide(obj)
{
     var x = document.getElementById(obj);
	if( x )
	    x.style.visibility='hidden';
}
function ActivateMenulink( id )
{
     var obj = document.getElementById('menulink'+id);
     obj.className='active';
}
function DeactivateMenulink( id )
{
     var obj = document.getElementById('menulink'+id);
     obj.className='';
}
function AjaxRegisterNewsletter()
{
     var email = document.getElementById("newsletter-email").value;
     if (email != undefined)
     {
        if (window.ActiveXObject)	{ httpRequest = new ActiveXObject("Microsoft.XMLHTTP");	}
        else					{ httpRequest = new XMLHttpRequest();	}
        	
        httpRequest.open("GET", "http://www.intimia.cz/newsletter.php?email="+email, true);
        httpRequest.onreadystatechange= function () { UpdateContent(); } ;
        httpRequest.send(null);
      }
      else
      {
        document.getElementById("divtyp1").innerHTML = "";
      }
}

function UpdateContent()
{
  if (httpRequest.readyState == 4)
  {
    if(httpRequest.status == 200)
    {
      var output = document.getElementById("newsletter");
      output.innerHTML = httpRequest.responseText;
    }
    else
    {
        alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}
