
function createImageCrop() {

  var cropImage = new ThumbnailSelector("div_crop_image");
  cropImage.bbox_bgimage = 'http://bebac.com/templates/public/images/thumb_selector/bbox1.gif';
  cropImage.bbox_hover_bgimage = 'http://bebac.com/templates/public/images/thumb_selector/bbox1_hover.gif';
  cropImage.bbox_size = 10;
  cropImage.bbox_color = "";
  cropImage.bbox_hover_color = "";
  cropImage.cover_color = ""
  cropImage.cover_bgimage = 'http://bebac.com/templates/public/images/thumb_selector/interlaced.gif';
  cropImage.cover_alpha = 40;
  cropImage.rect_border_width = 2;
  cropImage.rect_border_color = "black";
  cropImage.rect_color = "";
  cropImage.rect_alpha = 0;
  cropImage.left = 0;
  cropImage.top = 0;
  cropImage.width = 550;
  cropImage.height = 550;
  cropImage.Init();

}


function showElement(elementName, show, style) {
  var element = document.getElementById(elementName);
  if (show) {
    if (style != "") {
      if (document.all) {
        element.style.display = 'block';
      } else {
        element.style.display = style;  
      }
    } else {
      element.style.display = 'block';
    }
  } else {
    element.style.display = 'none';
  }
}

function confirmDel(url) {
	if (confirm("Da li ste sigurni da želite da obrišete?")) {
		window.location = url;
	}
}

function showBirthPopup(show) {
  if (show) {
    document.getElementById('AlatIzracunaj').style.display = 'block';
  } else {
    document.getElementById('AlatIzracunaj').style.display = 'none';
  }
}

function calculateBirthDate() {
  if (document.getElementById('register_step_1')) {
    form = document.getElementById('register_step_1');
	  sendRequest('ajax.php?show=member&part=birthdate', form, 'datum_porodjaja', fillBirthdateField);  
  } else if (document.getElementById('calculate_birthdate')) {
    form = document.getElementById('calculate_birthdate');
    sendRequest('ajax.php?show=tool&part=birth', form, '', displayBirthdayResults);  
  }
	
}

function fillBirthdateField() {
  var form = document.getElementById('register_step_1');
  var day = form.elements['str_child_birthdate[Day]'];
  var month = form.elements['str_child_birthdate[Month]'];
  var year = form.elements['str_child_birthdate[Year]'];
  var date = ajaxObj.response;
  dateArray = date.split(".");
  day.value = parseFloat(dateArray[0]);
  month.value = parseFloat(dateArray[1]);
  year.value = parseFloat(dateArray[2]);
}

function displayBirthdayResults() {
  eval(ajaxObj.response);
}

function showContactDetails(show, contactID) {
  sendRequest('ajax.php?show=' + show + '&part=details&int_contactID=' + contactID, false, '', fillContactDetails);
  
  function fillContactDetails() {
    topPos = findPos(document.getElementById('addressbook_list'));
    elemPos = findPos(document.getElementById('contact_'+ contactID));
    //alert('coord[0] = ' + coord[0] + '\n coord[1] = ' + coord[1]);
    document.getElementById('AddressDetails').innerHTML = ajaxObj.response;
    document.getElementById('AddressDetails').style.top = elemPos[1] - topPos[1] - 90 + 'px';
    document.getElementById('AddressDetails').style.display = 'block';
  }
  
}

function showAddEvent(hour) {
  topPos = findPos(document.getElementById('day_events'));
  elemPos = findPos(document.getElementById('day_hour_'+ hour));
  
  document.getElementById('int_time_hour').value = parseFloat(hour);
  document.getElementById('div_add_event').style.top = elemPos[1] - topPos[1] + 30 + 'px';
  document.getElementById('div_add_event').style.display = 'block';
}

function findPos(obj) {
   var curleft = curtop = 0;
   if (obj.offsetParent) {
          while (obj.offsetParent) {

             curleft += obj.offsetLeft;
             curtop += obj.offsetTop;
             obj  = obj.offsetParent;
          }
   }
   return [curleft,curtop];
}

function menuDisplay(element) {
  spanElement = element.parentNode;
  var subMenuElement = spanElement.childNodes[3];
  if (subMenuElement) {
    if (subMenuElement.style.display == 'block') {
      subMenuElement.style.display = 'none';
      spanElement.childNodes[0].src = 'http://bebac.com/templates/public/images/left_menu_arrow.gif';
    } else if (subMenuElement.style.display == 'none') {
      subMenuElement.style.display = 'block';
      spanElement.childNodes[0].src = 'http://bebac.com/templates/public/images/left_menu_arrow_ON.gif';
    }  
  }
}


function multipleDelete(url, formName) {
  if (confirm("Da li ste sigurni da želite da obrišete?")) {
    form = document.getElementById(formName);
  	form.action = url;
  	form.submit();
  }
}

function  selectAllCheckboxes(formName) {
  form = document.getElementById(formName);
  for (var i = 0; i < form.length; i++) {
    if (form[i].name != "") {
      if (form[i].type == "checkbox") {
        form[i].checked = true;
      }
    }
  }
}

function  setAllCheckboxes(formName, checked) {
  form = document.getElementById(formName);
  for (var i = 0; i < form.length; i++) {
    if (form[i].name != "") {
      if (form[i].type == "checkbox") {
        form[i].checked = checked;
      }
    }
  }
}

function setDateRequired() {
  checkedState = document.getElementById('int_pregnancy_trying').checked;
  var form = document.getElementById('register_step_1');
  if (checkedState) {
    form.elements['str_child_birthdate[Day]'].required = 0;
    form.elements['str_child_birthdate[Month]'].required = 0;
    form.elements['str_child_birthdate[Year]'].required = 0;
  } else {
    form.elements['str_child_birthdate[Day]'].required = 1;
    form.elements['str_child_birthdate[Month]'].required = 1;
    form.elements['str_child_birthdate[Year]'].required = 1;
  }
}

function saveLink(type, id) {
  sendRequest('ajax.php?show=save&int_type=' + type + '&itemID=' + id, false, '', linkSaved);
  
  function linkSaved() {
    alert("Vaš link je snimljen u sekciju Moji linkovi.");
  }
  
}

function setContact(formName, contactID) {
  var mailForm = document.getElementById(formName);
  var contactForm = document.getElementById('addressbook_list');
  if (contactForm.elements['int_checked[' + contactID + ']'].checked) {
    var input = document.createElement('input');
    input.type = 'hidden';
    input.id = 'email_' + contactID;
    input.name = 'email[' + contactID+ ']';
    input.value = contactID;
    mailForm.appendChild(input);  
  } else {
    mailForm.removeChild(document.getElementById('email_' + contactID));
  }

}


function  setAllContacts() {
  var contactForm = document.getElementById('addressbook_list');
  for (var i = 0; i < contactForm.length; i++) {
    if (contactForm[i].name != "") {
      if (contactForm[i].type == "checkbox") {
        if (!contactForm[i].checked) {
          contactForm[i].click();  
        }
        
      }
    }
  }
}

function listNames(letter) {
  var form = document.forms['names_list'];
  var href = 'http://bebac.com/index.php?show=names&part=book&str_letter=' + letter;
  if (form.elements['int_male'].checked) {
    href += '&int_male=1'
  }
  if (form.elements['int_female'].checked) {
    href += '&int_female=1'
  }
  location.href = href;
}

function showEventText(e, element) {
  var divs = document.getElementsByTagName('div');
  var list = '';
  for (var i = 0;i < divs.length;i++) {
    if (divs[i].id.substring(0, 10) == 'event_text') {
      showElement(divs[i].id, false);
    }
  }
  
  if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
  
  
//  showElement('div_add_event', false);
  showElement(element, true, 'block');
}

function closeEventText(e, element) {
  showElement(element, false);
  if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function checkRegistration(form) {
  var day = form.elements['str_child_birthdate[Day]'].value;
  var month = form.elements['str_child_birthdate[Month]'].value;
  var year = form.elements['str_child_birthdate[Year]'].value;
  
  var today = new Date();
  
  var birthdateLimit = new Date(today.getFullYear()+1, today.getMonth()-3, today.getDate()+8);
  var birthdate = new Date(year, month-1, day);
  if (birthdate.getTime() > birthdateLimit.getTime()) {
    alert('Uneli ste pogresan datum');
    return false;
  } else {
    return validateCompleteForm(form, 'error');
  }
}

function submitForm(formID, url) {
  if (document.getElementById(formID)) {
    form = document.getElementById(formID);
    if (url != undefined) {
      form.action = url;
    }
    form.submit();
  }
}

function commentVote(module, commentID, helpful) {
    sendRequest('ajax.php?show=comment_vote&str_module='+module+'&int_comment_id='+commentID+'&str_helpful='+helpful, false, '', setElementContent);

    function setElementContent() {
        var data = eval("(" + ajaxObj.response + ")");

        document.getElementById('vote_text_'+commentID).innerHTML = data.message;
        document.getElementById('votes_helpful_'+commentID).innerHTML = data.helpful;
        document.getElementById('votes_total_'+commentID).innerHTML = data.total;
    }
}





