var lastimage=null;
var lastdetaillink=null;
var currentImage = Number(1);
var professorId=0;
var userId=0;
//window.resizeTo(780,580);
function showPage(p,resize)
{
	var res = "yes";
	if (resize==null || resize=="undefined")
		res = "no";
	window.open(p,"_blank","titlebar=yes,menubar=yes,toolbar=yes,location=no,resizable="+res);
}


function onresize()
{
  if (solhr!=null)
  {
    solhr.width = document.body.offsetWidth - 48;
    solhr.style.top = firstrow.offsetTop+firstrow.offsetHeight  +88
  }
}

function menu_onreadystatechange() 
{
  if (menu.readyState=="complete")
  {
    var current = getCurrent();
    if (current!=null)
      changeImage(current.nextSibling);
    else
    {
      if (circles.length!=null)
      {
        circles(0).background=getContextPath()+"/images/tun/homepage/selcircle.gif"
        lastimage = circles(0);
      }
      else
      {
        circles.background=getContextPath()+"/images/tun/homepage/selcircle.gif"
        lastimage = circles;
      }
    }
    setImageGalleryLink();
    setFreePageLink()
  }
  
}
/*var circs = document.all.item ("circles");
alert(circs.length)*/
function changeImage(blk)
{
  if (lastimage!=null)
    lastimage.background=getContextPath()+"/images/tun/homepage/emptycirc.gif";
  if(blk.tagName=="IMG")
  {
    lastimage = blk;
    blk.src=getContextPath()+"/images/tun/homepage/selcircle.gif";
  }
  if (blk.tagName=="A")
  {
    try
    {
      var temp = blk.parentElement.previousSibling.previousSibling;
      var tempId=Number(blk.previousSibling.innerText);
      var id1000 = getId("1000");
      if (id1000!=null)
        if (tempId==5 || tempId==6 || tempId == 7)
          id1000.parentElement.previousSibling.previousSibling.background=getContextPath()+"/images/tun/homepage/selcircle.gif";
        else
          id1000.parentElement.previousSibling.previousSibling.background=getContextPath()+"/images/tun/homepage/emptycirc.gif";
      if (tempId==1000)
      {
        var id=getId(5);
        if (id!=null)
          if (id.parentElement.parentElement.style.display == "")
            id.parentElement.parentElement.style.display="none";
          else
            id.parentElement.parentElement.style.display="";
        id=getId(6);
        if (id!=null)
          if (id.parentElement.parentElement.style.display == "")
            id.parentElement.parentElement.style.display="none";
          else
            id.parentElement.parentElement.style.display="";
        id=getId(7);
        if (id!=null)
          if (id.parentElement.parentElement.style.display == "")
            id.parentElement.parentElement.style.display="none";
          else
            id.parentElement.parentElement.style.display="";
      }
      if (temp!=null)
        lastimage = temp;
      lastimage.background=getContextPath()+"/images/tun/homepage/selcircle.gif";
    }
    catch (e)
    {
    }
  }
}

function hilightdetaillink(blk)
{
  if (lastdetaillink!=null)
    lastdetaillink.className="detaillink";
  if (blk!=null)
    blk.className = "detaillinkselect"
  lastdetaillink=blk;
}

function setCurrent(id)
{
  currentImage = id;
  languagelink.href=getContextPath()+"/homepage"+(languageId==1?"/en":"")+"?first=true&menuId="+id+"&professorId="+professorId;
}

function getCurrent()
{
  var current = null;
  var sps = document.all.item("idvalues");
  if (sps.length!=null)
  for (i=0; i<sps.length; i++)
  {
    if (Number(sps[i].innerText) == Number(currentImage))
    {
      current = sps[i];
      break;
    }
  }
  return current;
}


function getId(val)
{
  var current = null;
  var sps = document.all.item("idvalues");
  if (sps.length!=null)
  for (i=0; i<sps.length; i++)
  {
    if (Number(sps[i].innerText) == Number(val))
    {
      current = sps[i];
      break;
    }
  }
  return current;
}

function changeCurrent(id)
{
  var current = null;
  currentImage = id;
  current = getCurrent();
  setCurrent(id);
  if (current!=null)
    changeImage(current.nextSibling);	
}

function searchMe(profId)
{
  if (freetext.value=="" || freetext.value==null || freetext.value=="undefined")
  {
    if (languageId==1)
      show_message("mandatory",SEARCH_ITEM_TITLE);
    else
      show_message("mandatory","Search item");
    return;
  }
  showWaitMessage(40);
  searchfrm.navigate(contextPath+"/homepage"+(languageId==2?"/en":"")+"?professorId="+profId+"&menuId=30&languageId="+languageId+"&searchText="+freetext.value);
}

function showSearchResult()
{
  searchfrm.document_onreadystatechange();
}

function checkEnter()
{
  checkKafYa();
  if (event.keyCode==13)
    searchlink.click();
}

function sendPicture(profId)
{
  var html = "<p align=\"center\"><font face=\"Yagut\"><b>&nbsp;&nbsp;&nbsp; <span lang=\"ar-sa\"> "+SEND_PICTURE_TITLE_1+"\n"
            +"    "+SEND_PICTURE_TITLE_2+"\n"
            +"        "+SEND_PICTURE_TITLE_3+"</span>&nbsp;gif "+OR_TITLE+" jpg<span lang=\"ar-sa\"> "+SEND_PICTURE_TITLE_4+"\n"
            +"        "+SEND_PICTURE_TITLE_5+"</span></b></font></p>\n"
            +"        <form method=\"post\" enctype=\"multipart/form-data\" action=\""+getContextPath()+"/upload.do\" onsubmit=check_file_extention()>\n"
            +"          <p align=\"center\">\n"
            +"          <input type=hidden name=languageId value=\""+languageId+"\">\n"
            +"          <input type=hidden name=id value=\""+profId+"\">\n"
            +"          <input type=hidden name=type value=\"0\">\n"
            +"          <input type=hidden name=menuId value=\""+currentImage+"\">\n"
            +"          <input id=myfile type=\"file\" class=o name=\"myfile\" onchange=check_file_extention() ></p>\n"
            +"          <p align=\"center\">\n"
            +"          <input type=\"submit\" value=\" "+SEND_TITLE+" \" class=button></p>\n"
            +"        </form>\n"
  //showClosableDiv(name, width, height, top, left, myRtl, text , title, url, tempObj, center)
  showClosableDiv("uploadpic", 500, 100, 160, 265, 1,html, SEND_PICTURE_TITLE, null, null,0);
}

function check_file_extention()
{
  var blk=document.all.item ("myfile");
  if (blk.value != "" && blk.value != null && blk.value != "undefined")
  {
    var ext = get_extension (blk.value).toUpperCase();
    if (ext != "JPG" && ext != "GIF")
    {
      alert (PICTURE_SHOULD_JPEG_OR_GIF);
      event.returnValue = false;
    }
  }
  else
  {
    alert (PLEASE_SELECTE_A_FILE);
    event.returnValue = false;
  }
}

function get_extension(val)
{
  var ext, ind;
  if (val != "")
  {
    for (i=val.length-1;i>=0;i--)
    {
      if (val.charAt(i)==".")
      {
        ind = i+1;
        break;
      }
    }
    ext = val.substr(ind);
    return ext;
  }
  else 
    return null;
}

function setProfessorId(id)
{
  professorId = id;
}

function setImageGalleryLink()
{
  var temp = getId(26);
  if (temp!=null)
  {
    temp.nextSibling.href=contextPath+"/explorer?rootid="+userId+"&pid=1";
    temp.nextSibling.target="_blank";
  }
}

function setFreePageLink()
{
  var temp = getId(24);
  if (temp!=null)
  {
    temp.nextSibling.href=contextPath+"/explorer?rootid="+userId+"&pid=2";
    temp.nextSibling.target="_blank";
  }
}
