// JavaScript Document


// function for validationg security image code entered by visitor
function validateCaptcha(formName, inputName, code) {

  var input = document.getElementById(inputName);
  var code  = document.getElementById(code);
  var form  = document.getElementById(formName);

  if (input.value == hex_md5(code.value.toUpperCase())) {
    form.submit(); 
    return true;
  }
  else {
    alert("Wrong image code entered!\nTry again!");
    return false;
  }

}

// insert tag into textarea (using this for adding youtube tags in admin area)
function insertTag(tag, area, code) {
  var client = navigator.userAgent.toLowerCase();
  var is_ie = ((client.indexOf('msie') != -1) && (client.indexOf('opera') == -1));
  var textarea = document.getElementById(area);

  if (!is_ie) {
     var text     = textarea.value;
     var selBegin = textarea.selectionStart;
     var selEnd   = textarea.selectionEnd;
     var preText  = text.substring(0, selBegin);
     var inText   = text.substring(selBegin, selEnd);
     var postText = text.substring(selEnd, text.length);
     //textarea.value = preText + '[' + tag + ']' + inText + '[/' + tag + ']' + postText;
     textarea.value = preText + '[' + tag + ']' + code + '[/' + tag + ']' + inText + postText;
     textarea.selectionStart = selBegin + 2 + tag.length;
     textarea.selectionEnd = selEnd + 2 + tag.length;
     textarea.focus();
  } else {
     theSelection = document.selection.createRange().text; // Get text selection
     if (theSelection) {
       // Add tags around selection
       //document.selection.createRange().text = '[' + tag + ']' + theSelection + '[/' + tag + ']';
       document.selection.createRange().text = theSelection + '[' + tag + ']' + code + '[/' + tag + ']';
       txtarea.focus();
       theSelection = '';
       return;
     } else {
       textarea.value = textarea.value + '[' + tag + '][/' + tag + ']';
     }
  }
}

// function which opens/closes category navigation
function displayNavigation(catContainer) {

  var area = document.getElementById(catContainer);

  if (area.style.display == "none") {
    area.style.display  = "";
    area.style.overflow = "visible";
  } else {
    area.style.display = "none";
  }
}

// displays large image
function showLarge(imgArea, imgSrc, width, height) {

  // first preload image to avoid IE6 bug
  var tempImg = new Image();
  tempImg.src = "share/images/large/" + imgSrc;

  var size = "";

  if (width >= height)
      size = "width=\"595px\"";

  var img       = document.getElementById(imgArea);
  img.innerHTML = "<img src=\""+ tempImg.src +"\" border=\"0\" "+ size +" />";
}

// checks/unchecks all category checkboxes
function toggleAll(formName) {
  var form     = document.getElementById(formName);
  var chkBoxes = form.elements;
  var arrSize  = form.elements.length;

  for (var i = 0; i < arrSize; i++) {
    if (chkBoxes[i].type == "checkbox")
      chkBoxes[i].checked = form.elementCheck.checked;
  }
}

// first check if only one checkbox/category is selected
function editElement(varName, formName, action) {
  
  var form     = document.getElementById(formName);
  var chkBoxes = form.elements;
  var arrSize  = form.elements.length;
  var selected = 0;
  var elID    = -1;

  for (var i = 1; i < arrSize; i++) {
    if (chkBoxes[i].type == "checkbox" && chkBoxes[i].checked) {
      selected++;
      elID = chkBoxes[i].value;
    }
  }

  if (selected > 1) {
    alert("Please select only one element for editing!");
    return ;
  } else if (selected == 0) {
    alert("You didn't select any element for editing!");
    return ;
  }

  location.replace(action + "&elementID=" + elID);
  return ;
}

// remove selected categories
function removeCat(formName, action) {

  var form     = document.getElementById(formName);
  var chkBoxes = form.elements;
  var arrSize  = form.elements.length;
  var selected = 0;
  var catID    = -1;

  for (var i = 1; i < arrSize; i++) {
    if (chkBoxes[i].type == "checkbox" && chkBoxes[i].checked)
      selected++;
  }

  if (selected == 0) {
    alert("You didn't select any element!");
    return ;
  } else {
    var msg = document.getElementById("message");
    document.getElementById("yesNoBox").style.visibility = "visible";
    msg.innerHTML = "Da li ste sigurni da zelite da potvrdite izmene?";
    form.action = action;// "?moduleAction=catRemove";
    return ;
  }
}

// create PDF for selected orders
function createPDF(areaName, formName, action) {

  var form     = document.getElementById(formName);
  var chkBoxes = form.elements;
  var arrSize  = form.elements.length;
  var selected = 0;

  for (var i = 1; i < arrSize; i++) {
    if (chkBoxes[i].type == "checkbox" && chkBoxes[i].checked)
      selected++;
  }

  if (selected == 0) {
    alert("Niste izabrali nijedan element!");
    return ;
  }

  processPostXMLRequest(areaName, action, createPostParameters(formName));
}

/* submit form if yes is clicked in confirmation box */
function yesAnswer(formName) {

//  var form   = document.getElementById(formName);
//  var action = form.action;
  //processPostXMLRequest(areaName, action, createPostParameters(formName));
  
  document.elementsForm.submit();
}

/* hide yesNoBox if no is clicked */
function hideBox() {
  document.getElementById("message").innerHTML = "";
  document.getElementById("yesNoBox").style.visibility = "hidden";
}



/* checks if value is correctly filled into input field */
function checkValue(value) {

  if (isNaN(parseInt(value))) {
    alert("Error: You must enter a number in quantity field!");
    return -1;
  } else if (parseInt(value) == 0) {
    alert("Error: You must enter a number that is greater than 0!");
    return -1;
  }
  
  return 1;
}


function submitForm(formName, action) {

  var form    = document.getElementById(formName);
  form.action = action;
  form.submit();
}

/* function and variables for displaying/hiding submenu */

var submenuVisible = false;

function displayMenu(menu) {

  var subArea = document.getElementById(menu);
  
  if (!submenuVisible) {
  
    // show hidden menu
    subArea.style.display = "block";
    submenuVisible = true;
  } else {
  
    // show hidden menu
    subArea.style.display = "none";
    submenuVisible = false;
  }
}


/* function to display/hide selected project */
function displayProject(containerName, containerID, totalCount) {

    // set selected container visible, while hiding all others
    var selectedContainer;

    // first, we are hiding all others
    for (var i = 0; i < totalCount; i++) {

        var projectContainer = document.getElementById(containerName + i);

        if (i == containerID)
            selectedContainer = projectContainer;
        else {
            
            // hide all others
            projectContainer.style.display = "none";
        }
    }

    selectedContainer.style.display = "block";
}


var current = 0; // currently selected container

/* function to display/hide project navigation container */
function next(totalCount) {

    // set selected container visible, while hiding all others
    var selectedContainer;

    if (current < totalCount-1)
        current++;

    // first, we are hiding all others
    for (var i = 0; i < totalCount; i++) {

        var projectContainer = document.getElementById('projectNavigationContainer_' + i);

        if (i == current)
            selectedContainer = projectContainer;
        else {

            // hide all others
            projectContainer.style.display = "none";
        }
    }

    selectedContainer.style.display = "block";

    // if there are no more containers, hide next button
    if (current >= totalCount-1) {
        var nextContainer = document.getElementById('nextNavigation');
        nextContainer.innerHTML = "";
    }

    // display previous button if necessary
    if (current > 0) {

        var prevContainer = document.getElementById('previousNavigation');
        prevContainer.innerHTML = "<img src=\"images/leftArrows.gif\" border=\"0\" />&nbsp; <a href=\"javascript:void(0);\" onClick=\"javascript:previous(" + totalCount + ");\">previous</a>";
    }

}


/* function to display/hide project navigation container */
function previous(totalCount) {

    // set selected container visible, while hiding all others
    var selectedContainer;

    if (current > 0)
        current--;

    // first, we are hiding all others
    for (var i = 0; i < totalCount; i++) {

        var projectContainer = document.getElementById('projectNavigationContainer_' + i);

        if (i == current)
            selectedContainer = projectContainer;
        else {

            // hide all others
            projectContainer.style.display = "none";
        }
    }

    selectedContainer.style.display = "block";

    // if there are no more containers, hide previous button
    if (current == 0) {
        var prevContainer = document.getElementById('previousNavigation');
        prevContainer.innerHTML = "";
    }

    // display next button if necessary
    if (current < totalCount-1) {
        var nextContainer = document.getElementById('nextNavigation');
        nextContainer.innerHTML = "<a href=\"javascript:void(0);\" onClick=\"javascript:next(" + totalCount + ");\">next</a> &nbsp;<img src=\"images/rightArrows.gif\" border=\"0\" />";
    }

}

