/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/  
function ismaxlength(obj){
  var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
  if (obj.getAttribute && obj.value.length>mlength)
     obj.value=obj.value.substring(0,mlength)
}

function HasText(str) {
  if (str + "" == "undefined" || str + "" == "null" ||
             str + "" == "")
    return false;
  return true;
}

function IsInt(numstr) {
  if (numstr + "" == "undefined" || numstr + "" == "null" || numstr + "" == "")
    return false;
  numstr += "";
  for (i = 0; i<numstr.length; i++) {
    if ((numstr.charAt(i) < "0") || (numstr.charAt(i) > "9"))
      return false;
  }
  return true;
}

function confirmDelete (sid, mgtrole, command) {
  if (command == "manageroles") {
    key = "role";
    selectname = 'mgtrole';
  }
  else {
    key = "keyword";
    selectname = 'mgtkeyword';
  }
  var choice = confirm("Are you sure you want to delete the selected "+key+"?");
  if (choice) {
    location.href="main.php?command="+command+"&action=deleterole&sid="+sid+"&"+selectname+"="+mgtrole;
  }
  else
    return false;
}

function confirmDeleteUser (userid) {
  var choice = confirm( "Deleting this user will permanently remove him. Are you sure you want to proceed?" );
  if (choice)
    location.href="admin.php?command=deleteuser&uid="+userid;
  else 
    return false;
}

function confirmReturn (place) {
  var choice = confirm("Are you sure you want to return to the previous page?");
  if (choice) {
    location.href=place;
  }
  else
    return false;
}

function confirmTrash (index) {
  test = 0;
  i = 0;
  while ( test == 0 && i < index ) {
    if ( document.forms[0].elements['del'+i].checked )
      test = 1;
    i++;
  }
  if ( test == 1 ) {
    var c = confirm("Deleting a scenario will permanently remove it from the database. Are you sure you want to continue?");
    if (c) 
      return true;
    else
      return false;
  }
}

function confirmDeleteLink (caseid, bodyid, linkid, editlinks) {
  var test = confirm("Are you sure you want to delete this association?");
  if (test) 
    location.href = "admin.php?command=deletelink&caseid="+caseid+"&bodyid="+bodyid+"&linkid="+linkid+"&editlinks="+editlinks;
  else
    return false;
}

function focusInput() {
  if (document.loginform)
    setTimeout("document.loginform.loginName.focus();",1);
}

function checkClone() {
  var cloneForm = document.getElementsByName('cloneForm')[0];
  if ( HasText(cloneForm.newTitle.value) )
    return true;
  else {
    alert ( "The title for the cloned case cannot be left blank." );
    return false;
  }
}

function doSmartInsert2 (c, count) {
  if ( count == 0 ) 
    suffix = '';
  else
    suffix = '_a';
  var n = document.forms[count].elements[4].value;

  if ( c == 1 ) {
    if ( n != '' ) {
      document.forms[count].elements[5].value = '';
      document.forms[count].elements[6].value = '';
      document.forms[count].elements[5].value = n+" "+"could ...";
      document.forms[count].elements[6].value = n+" "+"could ...";
    }
    else {
      document.forms[count].elements[5].value = '';
      document.forms[count].elements[6].value = '';
    }
  }
  else if ( c == 2 ) {
    document.getElementById("action"+suffix).value = "After due consideration of these options, "+n+" "+"chooses option 1."; 
  }
  else if ( c == 3 ) {
    document.getElementById("action"+suffix).value = "After due consideration of these options, "+n+" "+"chooses option 2.";
  }  
}

// automatically inserts the name of the decision-maker
function doSmartInsert (c, state, command, suffix) {
  if ( state == 1 && command == 1)
    count = 1;
  else if ( state == 2 ) {
    if ( command == 1 )
      count = 0;
    else
      count = 1;
  }
   
  var n = document.forms[count].elements[4].value;

  if ( c == 1 ) {
    if ( n != '' ) {
      document.forms[count].elements[5].value = '';
      document.forms[count].elements[6].value = '';
      document.forms[count].elements[5].value = n+" "+"could ...";
      document.forms[count].elements[6].value = n+" "+"could ...";
    }
    else {
      document.forms[count].elements[5].value = '';
      document.forms[count].elements[6].value = '';
    }
  }
  else if ( c == 2 ) {
    document.getElementById("action"+suffix).value = "After due consideration of these options, "+n+" "+"chooses option 1."; 
  }
  else if ( c == 3 ) {
    document.getElementById("action"+suffix).value = "After due consideration of these options, "+n+" "+"chooses option 2.";
  }
}

function show_tab(tabs_id, index, count) {
  var tab_id = tabs_id+'_'+index;
  document.getElementById(tab_id).style.display = "block";

  for ( i = 0; i < count; i++ ) {
    if ( i != index )
      document.getElementById(tabs_id+'_'+i).style.display="none";
  }
}

function surveyCheck () {
  if ( document.surveyform.feedback_text.value == '' ) {
    alert ( "You need to provide some feedback before proceeding to send it!" );
    return false;
  }
  else if ( document.surveyform.feedback_category[document.surveyform.feedback_category.selectedIndex].value == 0 ) {
    alert ( "You need to select a valid category before sending your feedback!" );
    return false;
  }
  else 
    xajax_sendfeedback(document.surveyform.feedback_text.value, document.surveyform.feedback_category[document.surveyform.feedback_category.selectedIndex].value);
}

function show_part( element_id, box_id ) {
  var el = document.getElementById(element_id).style;
  if ( box_id.checked )
    el.display = 'inline';
  else
    el.display = 'none';
}

function removeDefaultText (el_id) {
  var x = document.getElementById(el_id);
  if ( x.defaultValue == x.value ) {
    if ( x.style )
      x.style.cssText = '';
    x.value = '';
  }
}

function checkComponents (obj) {
  if (!obj.position[0].checked && !obj.position[1].checked) {
    alert( "You need to select Option 1 or Option 2 from item 1!" );
    return false;
  }
  if (obj.startepisode[obj.startepisode.selectedIndex].value == "select") {
    alert ( "You need to select a starting episode!" );
    return false;
  }
  if (obj.endepisode[obj.endepisode.selectedIndex].value == "select") {
    alert ( "You need to select an ending episode!" );
    return false;
  }

  return true;
}

window.onload = focusInput;
