// JavaScript Document

// Clear value text
function clearDefaultValue(element) {
    if (element.value == element.defaultValue) element.value = '';
}

// Show value text
function showDefaultValue(element) {
    if (element.value == '') element.value = element.defaultValue;
}

// Stars
function aHover(a, b) {
	if(a == 1) {
		document.getElementById('star1').style.background = "url(/img/icon/16_star"+b+".png)";
	}
	if(a == 2) {
		document.getElementById('star1').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star2').style.background = "url(/img/icon/16_star"+b+".png)";
	}
	if(a == 3) {
		document.getElementById('star1').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star2').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star3').style.background = "url(/img/icon/16_star"+b+".png)";
	}
	if(a == 4) {
		document.getElementById('star1').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star2').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star3').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star4').style.background = "url(/img/icon/16_star"+b+".png)";
	}
	if(a == 5) {
		document.getElementById('star1').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star2').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star3').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star4').style.background = "url(/img/icon/16_star"+b+".png)";
		document.getElementById('star5').style.background = "url(/img/icon/16_star"+b+".png)";
	}
	if(b == 2) { 
		document.getElementById('rate'+a).style.display = "none";
		document.getElementById('rate').style.display = "block";
	} else {
		document.getElementById('rate'+a).style.display = "block";
		document.getElementById('rate').style.display = "none";
	}
}

// Toggle recorded by sessions
function recordToggle(a) {
    var e = document.getElementById(a);
    var f = document.getElementById(a+'_arrow');
     if(!e) return true;
      if(e.style.display == "none")
      {
           e.style.display = "block";
           f.style.background = "url(/img/icon/16_open.png)";
           // setCookie(a, '',365);
           ajaxAdd('session', a, 2);
     }
      else
      {
           e.style.display = "none";
           f.style.background = "url(/img/icon/16_closed.png)";
           // setCookie(a, 1,365);
           ajaxAdd('session', a, 1);
      }
      return true;
}

function switchHideAll(a){
    var elements = document.getElementById(a);
    var i;
    for(i=0; i<elements.length; i++){
        document.getElementById(a+i).style.display="block";
    }
}

// Toggle
function toggle(a)
{
    var e = document.getElementById(a);
    var f = document.getElementById(a+'_arrow');
     if(!e) return true;
      if(e.style.display == "none")
      {
           e.style.display = "block";
           f.style.background = "url(/img/icon/16_open.png)";
           // setCookie(a, '',365);
           ajaxAdd('session', a, 2);
     }
      else
      {
           e.style.display = "none";
           f.style.background = "url(/img/icon/16_closed.png)";
           // setCookie(a, 1,365);
           ajaxAdd('session', a, 1);
      }
      return true;
}
// Set cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

// Show
function switchShow(a)
{
	document.getElementById(a).style.display="block";
}
// Hide
function switchHide(a)
{
	document.getElementById(a).style.display="none";
}
// Switch show and hide
function switchToggle(a)
{
	if ( (document.getElementById(a).style.display)==("block") )
	{
		document.getElementById(a).style.display="none";
	}
	else
	{
		document.getElementById(a).style.display="block";
	}
}

// Swidth input checked
function switchChecked(id)
{
    if(document.getElementById(id).checked == true)
        document.getElementById(id).checked = false;
    else document.getElementById(id).checked = true;
}
// Input Check
function check(id)
{
    document.getElementById(id).checked = true;
}
// Input unCheck
function uncheck(id)
{
    document.getElementById(id).checked = false;
}

// URL redirection
function goToUrl(url)
{
	document.location.href=url;
}

// Textarea expand
function countLines(strtocount, cols) {
    var hard_lines = 1;
    var last = 0;
    while ( true ) {
        last = strtocount.indexOf("\n", last+1);
        hard_lines ++;
        if ( last == -1 ) break;
    }
    var soft_lines = Math.round(strtocount.length / (cols-1));
    var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
    if ( hard ) soft_lines = hard_lines;
    return soft_lines;
}
function cleanForm() {
    var the_form = document.forms[1];
    for ( var x in the_form ) {
        if ( ! the_form[x] ) continue;
        if( typeof the_form[x].rows != "number" ) continue;
        the_form[x].rows = countLines(the_form[x].value,the_form[x].cols) +1;
    }
    setTimeout("cleanForm();", 300);
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(function() {
  cleanForm();
});