/**
 * kontrola formulare Poptavka
 *
 * @return boolean    true pokud je formular validovan, false jinak
 */
function checkDemand() {
  if (element("demand").value == "") {
    alert("Musíte zadat text poptávky.");
    element("demand").focus();
    
    return false;
  }
  
  if (!element("demand_phone").value && !element("demand_email").value) {
    alert("Musíte zadat kontakt ve formě telefonního čísla nebo e-mailu.");
    element("demand_phone").focus();
    
    return false;
  }
  
  if (!element("demand_phone").value && element("demand_callback").checked) {  
    alert("Pokud chcete, abychom Vám zavolali zpět, zadejte telefonní číslo.");
    element("demand_phone").focus();
    
    return false;
  }
    
  return true;
}

/**
 * kontrola formulare Kontaktni formular
 *
 * @return boolean    true pokud je formular validovan, false jinak
 */
function checkContact() {
  if (element("message").value == "") {
    alert("Musíte zadat text zprávy.");
    element("message").focus();
    
    return false;
  }
  
  if (element("contact").value == "" && element("callback").checked) {  
    alert("Pokud chcete, abychom Vám zavolali zpět, zadejte telefonní číslo.");
    element("contact").focus();
    
    return false;
  }  
    
  return true;
}

/**
 * kontrola formulare Objednavka
 *
 * @return boolean    true pokud je formular validovan, false jinak
 */
function checkOrder() {
  if (element("order").value == "") {
    alert("Musíte zadat text objednávky.");
    element("order").focus();
    
    return false;
  }
  
  if (element("name").value == "") {
    alert("Musíte zadat svoje jméno.");
    element("name").focus();
    
    return false;
  }  
  
  if (!element("phone").value && !element("email").value) {  
    alert("Musíte zadat kontakt ve formě telefonního čísla nebo e-mailu.");
    element("phone").focus();
    
    return false;
  }
    
  return true;
}

/**
 * kontrola preference v ramci formulare Objednavka
 *
 * @return void
 */
function checkOrderPreference(obj) {
  var other_id = (obj.id == "prefer_email" ? "prefer_phone" : "prefer_email");
  if (obj.checked) {
    element(other_id).checked = false;
  }
}

/**
 * prepinani viditelnosti detailu na strance Reference
 *
 * @return void
 */
function switchReference(ref_id) {
  for (var i=1;i<5;i++) {
    switchClass("ref_" + ref_id + "_" + i, "dis_none", "dis_row");
  }
}


/*
 * HTML object manipulation functions / START
 */
 
/**
 * get HTML element defined by id
 *
 * @return obj    HTML element
 */
function element(id) {
  var obj = document.getElementById(id);
  
  return obj;
}
 
/**
 * hide HTML element
 *    
 * @param mixed obj    object or object's id
 * @return void
 */
function elementHide(obj) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  obj.style.display = "none";
}

/**
 * show HTML element
 *    
 * @param mixed obj            object or object's id
 * @param boolean is_inline    [optional] if element is inline, pass true as second parameter
 * @return void
 */
function elementShow(obj, is_inline) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }

  obj.style.display = (is_inline ? "inline" : "block");
}

/**
 * switch element class name - if element has className class_1, replace it with class_2 and vice versa
 *    
 * @param mixed obj         object or object's id
 * @param string class_1    name of class 1
 * @param string class_2    name of class 2
 * @return void
 */
function switchClass(obj, class_1, class_2) {
  if (element(obj)) {    // object identified by its id
    obj = element(obj);
  }
  
  if (obj.className == "") {    // class name empty -> set class name from class_1
    obj.className = class_1;
  } else {
    if (obj.className.indexOf(class_1) != -1) {    // class_1 exists -> replace with class_2
      obj.className = obj.className.replace(class_1, class_2);
    } else {
      if ((obj.className.indexOf(class_2) != -1) && (class_2 != "")) {    // class_2 exists -> replace with class_1
        obj.className = obj.className.replace(class_2, class_1);
      } else {    // if not found, add class_1 to existing
        obj.className = obj.className + " " + class_1;
      }
    }
  }
}

/*
 * HTML object manipulation functions / END
 */
 

/* 
 * tab functions / START
 */
 
var active_tab = 1;

/**
 * hide tab
 *    
 * @param string tab_name    name of tab to hide
 * @return void
 */
function tabHide(tab_name) {
  var tab = element("tabname_" + tab_name);
  if (tab) {
    elementHide(tab);
  }
}

/**
 * show tab
 *    
 * @param string tab_name    name of tab to show
 * @return void
 */
function tabShow(tab_name) {
  var tab = element("tabname_" + tab_name);
  if (tab) {
    elementShow(tab, true);
  }
}

/**
 * change active tab to tab_name
 *    
 * @param string tab_name    name of tab which should be set as active
 * @return void
 */
function tabChange(tab_name) {
	elementHide("tab_" + active_tab);
	element("tabname_" + active_tab).className = "";
	
	elementShow("tab_" + tab_name);
	element("tabname_" + tab_name).className = "active";
	active_tab = tab_name;
	
	// try to put focus on the first field in active tab, when tab includes a form
	var child = element("tab_" + active_tab).childNodes[0];
  var type;
  var field;

	while (child) {
	  if (child.tagName == "FORM") {
	    for (i=0;i<child.elements.length;i++) {
	      field = child.elements[i];
	    
	      try {
          type = field.type;
        } catch (e) {}
	    
	      if (type && type != "hidden") {
		      if (formGetFieldParentTabName(field) == active_tab) {
		        if (type == "text" || type == "textarea") {
	  	        try {
	              field.focus();
	            } catch (e) {}
		        }
		            
		        return;
		      }
	      }
	    }
	  }
	  
	  try {
      child = child.childNodes[0];
    } catch (e) {}
	}
}

/*
 * tab functions / END
 */
