// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag	 :defaults
    var key_down=false;
    var curtime=-1;
    var delay=500;
    var tmptext='';
    var timeoutID;
    var timeoutSr;
    var dnum=0;
    var dot_delaytime=200;
    var opened=false;
        
    function showDescription(n)
    {
      if ($("id_descr"+n).style.display=="none" && !opened)
        {
          opened = true;
          new  Effect.Fade('id_descr'+n, {from:0.0, to:1.0, duration:0.3});
          new  Effect.BlindDown("id_descr"+n, {afterFinishInternal:function(){opened=false},duration:0.3});
        }
      if ($("id_descr"+n).style.display!="none" && !opened)
      {
          opened = true;
          new  Effect.Fade('id_descr'+n, {from:1.0, to:0.0, duration:0.3});
          new  Effect.BlindUp("id_descr"+n, {afterFinishInternal:function(){opened=false},duration:0.3});
      }
    }
    
    function keyup(e)
    {
	 if (true) //(e.keyCode!=13)
	 {
      $("ibox").style.color="#000000";
      window.clearTimeout(timeoutID);
      timeoutID=window.setTimeout('load_data()',delay);
      key_down=false;
	 }
	 else{window.clearTimeout(timeoutID)}
    }
  
  function keydown(e)
    {
	 if (true) //(e.keyCode!=13)
	 {
      if ($("ibox").value=="Catalog Search") $("ibox").value="";
      $("ibox").style.color="#000000";
      key_down=true;
	 }
	 else{window.clearTimeout(timeoutID)}
    }
    
  function SearchingDot()
  {
    timeoutSr=window.setTimeout('SearchingDot()',dot_delaytime);
    outs="<h1 style='margin-left:50px;'>Searching";
    if (dnum<5) {dnum=dnum+1;} else {dnum=0;}
    for (i=0; i<dnum; i++)
    {
      outs=outs+".";
    }
    outs=outs+"</h1>";
    $("id_sdot").innerHTML=outs;
  }
    
    function load_data()
    {
      stext=$("ibox").value;
      if (tmptext!=stext && key_down==false && stext.length>2 && stext!="Catalog Search")
      {
        $("showCoursesTable").innerHTML="";
        $("id_sdot").style.display="";
        timeoutSr=window.setTimeout('SearchingDot()',dot_delaytime);    
        $("ibox").style.color="#AAAAAA";
        $("iimg").style.display="";
        //stext = stext.gsub(/[{,},\^,\%,\\,\/,\|,\&]/,'')
	stext = stext.gsub(/[^A-Za-z0-9]/,' ');
        new Ajax.Updater("showCoursesTable", '/browser/byQuery/'+stext, {asynchronous:true, evalScripts:true});
        tmptext=stext;
      }
      window.clearTimeout(timeoutID);
    }
    
    function load_restore_data()
    {
      stext=$("ibox").value;
      if (tmptext!=stext && key_down==false && stext.length>2 && stext!="Catalog Search")
      {
        $("showCoursesTable").innerHTML="";
        $("id_sdot").style.display="";
        timeoutSr=window.setTimeout('SearchingDot()',dot_delaytime);    
        $("ibox").style.color="#AAAAAA";
        $("iimg").style.display="";
        stext = stext.gsub(/[{,},\^,\%,\\,\/,\|,\&]/,'')
        new Ajax.Updater("showCoursesTable", '/browser/byQuery/'+stext+'?remember_page=1', {asynchronous:true, evalScripts:true});
        tmptext=stext;
      }
      window.clearTimeout(timeoutID);
    }

    


  function submitsearch()
  {
  	stext=$("ibox").value;
	//onSubmit="javascript:submitsearch()"
    $("showCoursesTable").innerHTML="";
    $("id_sdot").style.display="";
    timeoutSr=window.setTimeout('SearchingDot()',dot_delaytime);    
    $("ibox").style.color="#AAAAAA";
    $("iimg").style.display="";
    new Ajax.Updater('showCoursesTable', '/main/searchGlobal?query='+stext, {asynchronous:true, evalScripts:true});
    tmptext=stext;
  	alert(stext);
  }

  function putinbox(i)
  {
    $("showCoursesTable").innerHTML="";
    $("id_sdot").style.display="";
    timeoutSr=window.setTimeout('SearchingDot()',dot_delaytime);    
    $("ibox").value=$("exbox"+i).innerHTML;
    $("ibox").style.color="#AAAAAA";
    $("iimg").style.display="";
    new Ajax.Updater('showCoursesTable', '/main/searchGlobal?query='+$("exbox"+i).innerHTML, {asynchronous:true, evalScripts:true});
    tmptext=$("exbox"+i).innerHTML;
  }


    
  function clear_ibox()
  {
    if ($("ibox").value=="Course Search")
    {
      $("ibox").style.color="#AAAAAA";    
      $("ibox").value="";    
    }
  }
  function fill_ibox()
  {
    if ($("ibox").value=="")
    {
      $("ibox").style.color="#53a0e1";    
      $("ibox").value="Course Search";    
    }
  }
  
  function isemail(param)
  {
      if (param.match(/[A-Z,a-z,0-9,_,\.,-]+\@[A-Z,a-z,0-9,_,\.,-]+\..+/)) return true
      else return false
  }
  
  
  function scanfields()
  {
    disable=false;
    if ($("email") && !isemail($("email").value)) disable=true;
    for (i=1; i<20; i++)
    {
      if ($("mustfill"+i) && $("mustfill"+i).value=="") disable=true;
    }
    $("formsubmit").disabled=disable;
    if ($("pass") && $("conf_pass"))
    {
        if ($("pass").value!=$("conf_pass").value) 
        {
          $("conf_pass_txt").innerHTML="<font color='red'>Password dont confirm</font>";
          $("formsubmit").disabled="true";
        }
        if ($("pass").value=="" || $("conf_pass").value=="") $("formsubmit").disabled=true;
        if ($("pass").value==$("conf_pass").value && $("pass").value!="") $("conf_pass_txt").innerHTML="<font color='green'>Password confirmed</font>";     
        if ($("pass").value==$("conf_pass").value && $("pass").value=="") $("formsubmit").disabled=true;
    }
  }

	function onAjaxBrowserBefore(){
		if ($("showCoursesTable")) $("showCoursesTable").innerHTML="";
    if ($("id_sdot")) $("id_sdot").show()
    if ($("ibox")) $("ibox").style.color="#AAAAAA";
		if ($("iimg")) $("iimg").show()  ;
    if ($("id_sdot")) timeoutSr=window.setTimeout('SearchingDot()',dot_delaytime);    
	
	}
	
	function onAjaxBrowserAfter(){
    if ($("id_sdot")) $("id_sdot").hide();
		if ($("iimg")) $("iimg").hide();
		if ($("id_sdot")) window.clearTimeout(timeoutSr)
	}


	function without_java(){
	}


function prepareInputBox() {
  iboxWidth = $("input_box").getWidth();
  iboxHeight = $("input_box").getHeight();
  $('loading_box').show();
  $("input_box").style.left=((scrWidth-iboxWidth)/2)+"px";  
  $("input_box").style.top=((scrHeight-iboxHeight)/2)+"px";
  $('loading_box').innerHTML="<img src='/images/ajax-loader.gif' align='absmiddle' style='margin-top:5px;margin-bottom:5px;'/> loading...";
  $('input_box').style.background="";
  $('input_box').show();
}

function closeInput() {  

  $("control").hide();
  $("input_inside").innerHTML="";
  $("input_inside").hide();
  $("input_box").style.left=((scrWidth-iboxWidth)/2)+"px"
  $("input_box").style.top=((scrHeight-iboxHeight)/2)+"px"
  $("input_box").style.width=iboxWidth+"px";
  $("input_box").style.height=iboxHeight+"px";
  $("input_box").hide();
}

function sendForm() {
    $('edit_form').request({
        onComplete: function(){ alert('Form data saved!'); closeInput(); }
    });
}

function get_ww() {
  var frameWidth=800; 
  if (self.innerWidth) 
      frameWidth = self.innerWidth; 
  else if (document.documentElement && document.documentElement.clientWidth) 
      frameWidth = document.documentElement.clientWidth; 
  else if (document.body) 
      frameWidth = document.body.clientWidth; 
  return frameWidth; 
} 
function get_wh() {
  var frameHeight=640; 
  if (self.innerHeight) 
      frameHeight = self.innerHeight; 
  else if (document.documentElement && document.documentElement.clientHeight) 
      frameHeight = document.documentElement.clientHeight; 
  else if (document.body) 
      frameHeight = document.body.clientHeight; 
  return frameHeight; 
}

