// JavaScript Document
var itemID;
var parentItemID;
var selectedCheckBoxes;

function initMenu() {
  $('#menu ul').hide();

  itemID = "#" + $('ul.show').attr('id');
  $(itemID).show();
  //alert(itemID);

  parentItemID = "#" + $(itemID).parent().parent().attr('id');
  $(parentItemID).show();
  //alert(parentItemID);

  $('#menu li a').click(
    function() {
        $(this).next().slideToggle('normal');	
      }
    );  
  }
  
function initDDL() {
  $('#manufacturers').hide();
  $('#manufacturer').click(
    function() {
        $('#manufacturers').slideToggle('normal');
      }
    );
    
  $('#applyManufacturers').click(
    function() {
        selectedCheckBoxes = "";
        $("#manufacturers div ul li input").each(
        	function() {
        		if(this.checked == true)
        		selectedCheckBoxes += this.id + ",";
        	}
        );
        $("#manufacturersIDs").attr('value',selectedCheckBoxes.substring(0,(selectedCheckBoxes.length - 1)));
        //alert($("#manufacturersIDs").attr('value'));
        $('form').submit();
        $('#manufacturers').slideUp('normal');
      }
    );
  }
  
function initRows() {  
  $('#rows .rowDescription').hide();
  
  // SET DEFALT
  $('#' + $('.row .default').attr('id')).click();
  $('#row_' + $('.row .default').attr('id')).show();
  
  $('.rowRadio').click(
    function() {
        $('#rows .rowDescription').hide();
        $('#row_' + this.id).show();
      }
    );
}

function initDelRows() {  
  $('#deliveryOptions div').hide();
    
  // SET DEFALT
  $('#' + $('td .selected').attr('id')).click();
  $('#show_' + $('td .selected').attr('id')).show();  
  
  //alert($('td .selected').attr('id'));
  
  $('.delRadio').click(
    function() {
        $('#deliveryOptions div').hide();
        $('#show_' + this.id).show();
      }
    );
}

function initTabs() {
	$('#relations ul').hide();
	$('#show_tab1').show();
	
	$('#relationTabs span').click(
	  function() {
	  	$('#relationTabs span').each(
	  	function(){
	  		$('#' + this.id).removeClass('activeTab')
	  		}
	  	);
	  	$('#' + this.id).addClass('activeTab');
        $('#relations ul').hide();
        $('#show_' + this.id).show();
      }
    );
}

function initRegistrationTabs() {
	$('#registrationContent div').hide();
	$('#show_info').show();
	
	$('#registrationTabs div').click(
	  function() {
	  	$('#registrationTabs div').each(
	  	function(){
	  		$('#' + this.id).removeClass('activeTab')
	  		}
	  	);
	  	$('#' + this.id).addClass('activeTab');
        $('#registrationContent div').hide();
        $('#show_' + this.id).show();
      }
    );
    
    // Linky pro nechapave lidi
    
    $('#link1').click(
	  function() {
	  	$('#registrationContent div').hide();
        $('#show_privateCustomer').show();
        $('#registrationTabs div').each(
	  	function(){
	  		$('#' + this.id).removeClass('activeTab')
	  		}
	  	);
        $('#privateCustomer').addClass('activeTab');
      }
    );
    
    $('#link2').click(
	  function() {
	  	$('#registrationContent div').hide();
        $('#show_businessCustomer').show();
        $('#registrationTabs div').each(
	  	function(){
	  		$('#' + this.id).removeClass('activeTab')
	  		}
	  	);
        $('#businessCustomer').addClass('activeTab');
      }
    );

}

function initDifferentddress()
{
	$('#differnetAddress').hide();
	$('#differnetAddress2').hide();
	
	//alert($('#cb_differnetAddress').attr("checked"));
	if($('#cb_differnetAddress').attr("checked") == true)
			{
				$("#differnetAddress").find("input").removeAttr("disabled");
			}
			else{
				$("#differnetAddress").find("input").attr("disabled","disabled");
			}
	
	$('#cb_differnetAddress').click(
		function() {
			if(this.checked == true)
			{
			//alert('checked')
				$('#differnetAddress').show();
				$("#differnetAddress").find("input").removeAttr("disabled", !this.checked);
			}
			else{
				$('#differnetAddress').hide();
				$("#differnetAddress").find("input").attr("disabled", !this.checked);
			}
		}
	);
	
	$('#cb_differnetAddress2').click(
		function() {
			if(this.checked == true)
			{
			//alert('checked')
				$('#differnetAddress2').show();
			}
			else{
				$('#differnetAddress2').hide();
			}
		}
	);
}

function initAdvancedSearch()
{	
	$('#advancedSearchContent').hide();
	
	$('#cb_advancedSearch').click(
		function() {
			if(this.checked == true)
			{
			//alert('checked')
				$('#advancedSearchContent').show();
			}
			else{
				$('#advancedSearchContent').hide();
			}
		}
	);
}

function initUserTabs() {
	$('#userContent div').hide();
	
	// Default
	$('#show_userInfo').show();
	$('#userInfo').addClass('activeTab');
	
	$('#userTabs div').click(
	  function() {
	  	$('#userTabs div').each(
	  	function(){
	  		$('#' + this.id).removeClass('activeTab')
	  		}
	  	);
	  	$('#' + this.id).addClass('activeTab');
        $('#userContent div').hide();
        $('#show_' + this.id).show();
      }
    );
}


function askURL(){
	//alert($('form').attr('id'));
	
	$("#askURL").val(self.parent.document.location);
}

function asktitle(){
	//alert($('form').attr('id'));
	
	$("#asktitle").val(self.parent.document.title);
}


function sendMyForm(formId){
	//alert($('form').attr('id'));
	
	//$('form').validate();
	self.parent.location = $('form').submit();


	//self.parent.tb_remove();
}

function sendOrderForm()
{
	//$("#orderFormToBeValidate").validate();
	validateAddressForm();
	//alert($("#orderFormToBeValidate").valid());
}

function validateAddressForm()
{
	$("#orderFormToBeValidate").validate({
		rules: {
			fa_jmeno: 	"required",
			fa_prijmeni: 	"required",
			fa_ulice: 	"required",
			fa_mesto: 	"required",
			fa_psc: 	"required",
			fa_email: 	"required email",
			fa_telefon: 	"required",
			do_jmeno: 	"required",
			do_prijmeni: 	"required",
			do_ulice: 	"required",
			do_mesto: 	"required",
			do_psc: 	"required",
			chb_podminky: "required"
		},
		messages: {
			fa_jmeno: 	"",
			fa_prijmeni: 	"",
			fa_ulice: 	"",
			fa_mesto: 	"",
			fa_psc: 	"",
			fa_email: 	"",
			fa_telefon: 	"",
			do_jmeno: 	"",
			do_prijmeni: 	"",
			do_ulice: 	"",
			do_mesto: 	"",
			do_psc: 	"",
			chb_podminky: ""
			
		}
	});
	
	if($("#orderFormToBeValidate").valid() == false)
	{
// TODO: Tady zadejte nejakou rozumnou hlasku.
		alert("Vyplňte prosím všechna označená pole.");
	}
	else
	{
// TODO: Tohle se stane pokud je vse OK. Tak tady odkomentujte ten alert.
		//alert("Mission has been completed!");
		
		$('form').submit();
	}
}	

function sendAskForm()
{
	//$("#askFormToBeValidate").validate();
	validateAskForm();
	//alert($("#askFormToBeValidate").valid());
}

function validateAskForm()
{
	$("#askFormToBeValidate").validate({
		rules: {
			ask_name: 	"required",
			ask_email: 	"required",
			ask_text: 	"required"
		},
		messages: {
			ask_name: 	"",
			ask_email: 	"",
			ask_text: 	""			
		}
	});

	
	if($("#askFormToBeValidate").valid() == false)
	{
// TODO: Tady zadejte nejakou rozumnou hlasku.
		alert("Vyplňte prosím všechna označená pole.");
	}
	else
	{
// TODO: Tohle se stane pokud je vse OK. Tak tady odkomentujte ten alert.
		//alert("Mission has been completed!");
		
		$('form').submit();
	}
}

function sendOfferForm()
{
	//$("#offerFormToBeValidate").validate();
	validateOfferForm();
	//alert($("#offerFormToBeValidate").valid());
}

function validateOfferForm()
{
	$("#offerFormToBeValidate").validate({
		rules: {
			jmeno: 	"required",
			email2: 	"required",
			tlf: 	"required",
			ks: 	"required"
		},
		messages: {
			jmeno: 	"",
			email2: 	"",
			tlf: 	"",			
			ks: 	""			
		}
	});

	
	if($("#offerFormToBeValidate").valid() == false)
	{
// TODO: Tady zadejte nejakou rozumnou hlasku.
		alert("Vyplňte prosím všechna označená pole.");
	}
	else
	{
// TODO: Tohle se stane pokud je vse OK. Tak tady odkomentujte ten alert.
		//alert("Mission has been completed!");
		
		$('form').submit();
	}
}

$(document).ready(function() 
{
  initMenu();
  initDDL();
  initRows();
  initDelRows();
  initTabs();
  initRegistrationTabs();
  initDifferentddress();
  initAdvancedSearch();
  initUserTabs();
  askURL();
  asktitle();
});
