/*
*JavaScript
*author: Marko Marusic
*/




/*
*Provjera da li je korisnik odabrao stranu drzavu
*/
function countryCheck(el){
    
    var userCountry = el.get('value');
    
   
 	// 191 -- Hrvatska
    if (userCountry && userCountry != '191' ){
    
  
	// call function for hidding elements
	show();
      
     $('do_rhname').addClass("validate['required']");
     formcheck.register($('do_rhname'));
          
    $('do_rhaddress').addClass("validate['required','alphanum']");
     formcheck.register($('do_rhaddress'));
     
     $('do_rhcity').addClass("validate['required','alpha']");
     formcheck.register($('do_rhcity'));
     
     $('do_rhpostnbr').addClass("validate['required','alphanum']");
     formcheck.register($('do_rhpostnbr'));
     
     
            
        return true;
    } else {
    	hide();
      // alert(el.value);
       //remove class ako vrati na hrvatsku
       $('do_rhname').removeClass("validate['required']");
       $('do_rhname').value='';
       formcheck.dispose($('do_rhname'));
       
       $('do_rhaddress').removeClass("validate['required','alphanum']");
       $('do_rhaddress').value='';
       formcheck.dispose($('do_rhaddress'));
     
       $('do_rhcity').removeClass("validate['required','alpha']");
       $('do_rhcity').value='';
       formcheck.dispose($('do_rhcity'));
     
       $('do_rhpostnbr').removeClass("validate['required','alphanum']");
       $('do_rhpostnbr').value='';
       formcheck.dispose($('do_rhpostnbr'));
       
       
        return true;
    }
	
}

/*********************************************
**			Mootools Funkcije				**
**											**	
**											**	
**											**	
**											**	
*********************************************/	



// ovako se definira funkcija u frameworku Mootools
//hide element

var hide = function(element) {
		
		$('boxShow').setStyle('visibility', 'hidden');
		$('boxShow').setStyle('padding', '0px');
		$('boxShow').setStyle('bottom', '0px');
		$('boxShow').set('tween', {}).tween('height', '0px');

};

var show = function() {
				// Always sets the duration of the tween to 1000 ms and a bouncing transition
				// And then tweens the height of the element
				$('boxShow').setStyle('visibility', 'visible');
				$('boxShow').setStyle('padding-bottom', '10px');
				$('boxShow').setStyle('margin-bottom', '10px');
				
				$('boxShow').set('tween', {
					duration: 2000,
					transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
					
				}).tween('height', '312px');
};



		
/*********************************************
**			Mootools akcije					**
**											**	
**											**	
**											**	
**											**	
*********************************************/		
	
window.addEvent('domready', function(){	


 if ($('domena')) 
 {
	$('domena').focus();
 }

 if ($('username')) 
 {
 	if( $('username').get('value') ){
 		$('password').focus();
 	}else{
		$('username').focus();
	}
 }
 
  if ($('oldPassword')) 
 {
	$('oldPassword').focus();
 }


/*
* ako je oznaceno slanje nove lozinke, 
* napravi disable na polja za unos nove lozinke
*/
 if ($('sendCurrentlyPassword')) 
 {
 
 	$('sendCurrentlyPassword').addEvent('click', function(event){
		
		if(this.checked ){
			$('password').disabled = true;
			$('password2').disabled = true;
		}else{
			$('password').disabled = false;
			$('password2').disabled = false;
		}
									
	});	
	
 	
 }


/*
*
* Highlihter
*/
 if ($('formular')) 
 {
 	/*
 	*Ako  korisnik ima ukljucen JavaScript onda mu prikazi formu
 	*/
 	$('showUserForm').setStyle('visibility', 'visible');
 
	var myInputs = $$('input.text-input-sifra','input.text-input','input.text-input-name','input.text-input-postnbr','input.text-input-grad', 'textarea');
	
	var myHilighter = new SL_Hilight({
		inputs: myInputs, 
		activeClass: 'active_field',
		initialFocus: true
	});
}
/*
* ispitaj ako posotoji id= formular, inace u IE javlja Page error
*/
 if ($('formular')) 
 {
 
    formcheck = new FormCheck('formular',
     {
         display: {
                errorsLocation : 1,
                indicateErrors : 1,
                showErrors : 1,
		//addClassErrorToField : 1,
		flashTips : true,
		fadeDuration : 1000

         }
     }
    );

if ($('do_usercountry'))
{
	$('do_usercountry').addEvent('change', function(event){
		
		countryCheck(this);
			
	});
	
	
	// inicijalno prikazi formu b.) ako je != od Hrvatska
	// u slucaju refresha
	if ($('do_usercountry').value != '191' )
	{
		countryCheck($('do_usercountry'));
		show();
	}
}	
	
if ($('do_usercity'))
{
	//alert (getBaseURL());
	new Autocompleter.Ajax.Json('do_usercity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
}

if ($('do_rhcity'))
{
	new Autocompleter.Ajax.Json('do_rhcity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
}

if ($('do_admincity'))
{	
	new Autocompleter.Ajax.Json('do_admincity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
}

if ($('do_tehcity'))
{	
	new Autocompleter.Ajax.Json('do_tehcity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
}
	
if ($('do_uplaticity'))
{	
	new Autocompleter.Ajax.Json('do_uplaticity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
	
}

	
	
}	


/*
* korisnik mijenja podatke o domeni
* 
*/
 if ($('userData')) 
 {
 
     formcheck = new FormCheck('userData',
     {
         display: {
                errorsLocation : 1,
                indicateErrors : 1,
                showErrors : 1,
		//addClassErrorToField : 1,
		flashTips : true,
		fadeDuration : 1000

         }
     }
    );
 
 
 	var myInputs = $$('input.text-input-sifra','input.text-input','input.text-input-name','input.text-input-postnbr','input.text-input-grad','input.text-input-oib','textarea');
	
	var myHilighter = new SL_Hilight({
		inputs: myInputs, 
		activeClass: 'active_field',
		initialFocus: true
	});
 
 if ($('do_admincity'))
{	
	new Autocompleter.Ajax.Json('do_admincity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
}

if ($('do_tehcity'))
{	
	new Autocompleter.Ajax.Json('do_tehcity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
}
 
 if ($('do_uplaticity'))
{	
	new Autocompleter.Ajax.Json('do_uplaticity', '/index.php/domain/displayCities', {
		//name the element containing the search term something suitable
		//otherwise defaults to 'value'
		'postVar': 'q'
	});
	
}
 
 }


/*
* Postavi calendar
*
*/
if ($('do_date'))
{	
	//alert($('do_date').value);
	//new CalendarEightysix('do_date', 	 { 'offsetY': -4 });
	new CalendarEightysix('do_date', { 'theme': 'default blue','startMonday': true, 'format': '%d.%m.%Y', 'slideTransition': Fx.Transitions.Back.easeOut, 'draggable': true, 'offsetY': -4 });
	//new CalendarEightysix('exampleV',	 { 'theme': 'default red', 'defaultDate': 'next Wednesday', 'minDate': 'tomorrow', 'offsetY': -4 });
}




/*
* Postavi funkciju za check all checkbox in form
*
*/

if ($('checkAll'))
{
	$('checkAll').addEvent('click', function() {
							
		
		var urlBase = getBaseURL(location.href);
		
		
		if($('checkAll').get('rel') == 'yes')
		{
			do_check = false;
			$('checkAll').set('src',urlBase+'/img/uncheck.jpg').set('rel','no');
		}
		else
		{
			do_check = true;
			$('checkAll').set('src',urlBase+'/img/check.jpg').set('rel','yes');
		}
		
		$$('.check-me').each(function(el) { el.checked = do_check; });
	});
}


});
