function loading(cname){

	if(document.getElementById('charts')) 
	document.getElementById('charts').style.display='none';

	if(document.getElementById('static-chart-1')) 
	document.getElementById('static-chart-1').style.display='none';

	if(document.getElementById('static-chart-2')) 
	document.getElementById('static-chart-2').style.display='none';

	var ldiv = document.getElementById('loading_center_div');
	if (ldiv) {
		ldiv.style.display='';
		if(cname) {
			ldiv.className = cname;
		} else {
			ldiv.className = 'centerabsolute';
		}
	}
}


function createCookie(name,value,days)
{

  var expires = "";
  if (days)
    {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      expires = "; expires="+date.toGMTString();
    }

  document.cookie = name+"="+escape(value)+expires+"; path=/";
}



function readCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++)
    {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name,"",-1);
}

function setCookie(name, value, days) {
  eraseCookie(name);
  createCookie(name, value, days);
}

function checkAll2(listname,tval){

    var elem=document.getElementsByName(listname);

	if(elem){
      for(i=0; i<elem.length; i++){
        elem[i].checked=tval;
      }
    }
}

function checkBox_click(val, cbox){
	document.getElementById(cbox).checked = val;
}
function checkboxall(listname,obj){
	var cboxval=obj.checked;
	if(cboxval) {
		tval = true;
	}else{
		tval = false;
	}
 var elem=document.getElementsByName(listname);
    if(elem){
      for(i=0; i<elem.length; i++){
        elem[i].checked=tval;
      }
    }
}

function checkAll(listnum,allbutton,tval) {
  if(listnum == 1) {
    if(allbutton.form["add_to_wordlist1[]"].length) {
      for (var i = 0; i < allbutton.form["add_to_wordlist1[]"].length; i++) {
        allbutton.form["add_to_wordlist1[]"][i].checked = tval;
      }
    } else {
      allbutton.form["add_to_wordlist1[]"].checked = tval;
    }
  } else {
    if(allbutton.form["add_to_wordlist2[]"].length) {
      for (i = 0; i < allbutton.form["add_to_wordlist2[]"].length; i++) {
        allbutton.form["add_to_wordlist2[]"][i].checked = tval;
      }
    } else {
      allbutton.form["add_to_wordlist2[]"].checked = tval;
    }
  }
}

function tranferSelected() {
	tranferSelected2(document.keyword_form,document.add_to_project_form);
}

function tranferSelected2(source,dest) {
	if(source["add_to_wordlist1[]"]) {
		if(source["add_to_wordlist1[]"].length) {
			for (var i = 0; i < source["add_to_wordlist1[]"].length; i++) {
				if(source["add_to_wordlist1[]"][i].checked) {
					dest["add_to_wordlist1[]"][i].checked = true;
					dest["add_to_wordlist1[]"][i].value = source["add_to_wordlist1[]"][i].value;			
				} else {
					dest["add_to_wordlist1[]"][i].checked = false;
				}
			}
		} else {
			dest["add_to_wordlist1[]"].checked = true;
			dest["add_to_wordlist1[]"].value = source["add_to_wordlist1[]"].value;
		}
	} 
	if(source["add_to_wordlist2[]"]) {
		if(source["add_to_wordlist2[]"].length) {
			for (var i = 0; i < source["add_to_wordlist2[]"].length; i++) {
				if(source["add_to_wordlist2[]"][i].checked) {
					dest["add_to_wordlist2[]"][i].checked = true;
					dest["add_to_wordlist2[]"][i].value = source["add_to_wordlist2[]"][i].value;			
				} else {
					dest["add_to_wordlist2[]"][i].checked = false;
				}
			}
		} else {
			dest["add_to_wordlist2[]"].checked = true;
			dest["add_to_wordlist2[]"].value = source["add_to_wordlist2[]"].value;
		}
	}
}

function hasSelection(listnum,button) {
  var counter = 0;
  if(listnum == 1) {
    if(button.form["add_to_wordlist1[]"])
      if(button.form["add_to_wordlist1[]"].length)
	for (var i = 0; i < button.form["add_to_wordlist1[]"].length; i++) {
	  if(button.form["add_to_wordlist1[]"][i].checked) { counter++; }
	}
      else
	if(button.form["add_to_wordlist1[]"].checked) { counter++; }
  } else {
    if(button.form["add_to_wordlist2[]"]) 
      if(button.form["add_to_wordlist2[]"].length)
	for (i = 0; i < button.form["add_to_wordlist2[]"].length; i++) {
	  if(button.form["add_to_wordlist2[]"][i].checked) { counter++; }
	}
      else
	if(button.form["add_to_wordlist2[]"].checked) { counter++; }
  }
  if(counter > 0) {
    return true;
  } else {
    return false;
  }
}

function hasSelection_general(button) {
	var counter = 0;
	if(button.form["add_to_wordlist1[]"]){
		if(button.form["add_to_wordlist1[]"].length){
			for (var i = 0; i < button.form["add_to_wordlist1[]"].length; i++) {
				if(button.form["add_to_wordlist1[]"][i].checked){
					counter++;
				}
			}
		}
		else{
			if(button.form["add_to_wordlist1[]"].checked){
				counter++;
			}//end if
		}//end else
	}//end if
	
	if(button.form["remove_wordlist_box[]"]){
		if(button.form["remove_wordlist_box[]"].length){
			for (var i = 0; i < button.form["remove_wordlist_box[]"].length; i++) {
				if(button.form["remove_wordlist_box[]"][i].checked){
					counter++;
				}
			}
		}
		else{
			if(button.form["remove_wordlist_box[]"].checked){
				counter++;
			}//end if
		}//end else
	}//end if
	
	if(counter > 0) {
		return true;
	}
	else{
		return false;
	}//end else
}//end function

function hasSelection_general_for_result_2(button) {
        var counter = 0;
        if(button.form["add_to_wordlist2[]"]){
                if(button.form["add_to_wordlist2[]"].length){
                        for (var i = 0; i < button.form["add_to_wordlist2[]"].length; i++) {
                                if(button.form["add_to_wordlist2[]"][i].checked){
                                        counter++;
                                }
                        }
                }
                else{
                        if(button.form["add_to_wordlist2[]"].checked){
                                counter++;
                        }//end if
                }//end else
        }//end if

        if(button.form["remove_wordlist_box2[]"]){
                if(button.form["remove_wordlist_box2[]"].length){
                        for (var i = 0; i < button.form["remove_wordlist_box2[]"].length; i++) {
                                if(button.form["remove_wordlist_box2[]"][i].checked){
                                        counter++;
                                }
                        }
                }
                else{
                        if(button.form["remove_wordlist_box2[]"].checked){
                                counter++;
                        }//end if
                }//end else
        }//end if

        if(counter > 0) {
                return true;
        }
        else{
                return false;
        }//end else
}//end function

function hasSelection_free_users(listnum,button) {
  var counter = 0;
  if(listnum == 1) {
    if(button.form["add_to_wordlist1[]"])
      if(button.form["add_to_wordlist1[]"].length)
	for (var i = 0; i < button.form["add_to_wordlist1[]"].length; i++) {
	  if(button.form["add_to_wordlist1[]"][i].checked) { counter++; }
	}
      else
	if(button.form["add_to_wordlist1[]"].checked) { counter++; }
  } else {
    if(button.form["add_to_wordlist2[]"]) 
      if(button.form["add_to_wordlist2[]"].length)
	for (i = 0; i < button.form["add_to_wordlist2[]"].length; i++) {
	  if(button.form["add_to_wordlist2[]"][i].checked) { counter++; }
	}
      else
	if(button.form["add_to_wordlist2[]"].checked) { counter++; }
  }
  return counter;
}// end function

function hide_analyse(col) {
  show_div('col2');
  if(col == 1) {
    show_div('col3');
  } else {
    show_div('col1');
  }
  show_div('s_analyse_' + col);
  hide_div('h_analyse_' + col);
  var table = document.getElementById('resultstable1');
  if(table) {
    var objs = table.getElementsByTagName('td');
    for (var i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'extradata') { 
	if(objs[i].style)
	  objs[i].style.display='none';
	if(objs[i].display)
	  objs[i].display = 'none';
      };
    }
    objs = table.getElementsByTagName('th');
    for (i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'extradata') { 
	if(objs[i].style)
	  objs[i].style.display='none';
	if(objs[i].display)
	  objs[i].display = 'none';
      };
    }
  }
}

function show_analyse(col) {
  hide_div('col2');
  if(col == 1) {
    hide_div('col3');
  } else {
    hide_div('col1');
  }
  hide_div('s_analyse_' + col);
  show_div('h_analyse_' + col);
  table = document.getElementById('resultstable1');
  if(table) {
    objs = table.getElementsByTagName('td');
    for (var i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'extradata') { 
	if(objs[i].style)
	  objs[i].style.display='';
	if(objs[i].display)
	  objs[i].display = '';
      };
    }
    objs = table.getElementsByTagName('th');
    for (i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'extradata') { 
	if(objs[i].style)
	  objs[i].style.display='';
	if(objs[i].display)
	  objs[i].display = '';
      };
    }
  }
}

function hide_xref(col) {
  show_div('col2');
  if(col == 1) {
    show_div('col3');
  } else {
    show_div('col1');
  }
  show_div('s_xref_' + col);
  hide_div('h_xref_' + col);
  table = document.getElementById('resultstable1');
  if(table) {
    objs = table.getElementsByTagName('td');
    for (var i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'xrefdata') { 
	if(objs[i].style)
	  objs[i].style.display='none';
	if(objs[i].display)
	  objs[i].display = 'none';
      };
    }
    objs = table.getElementsByTagName('th');
    for (i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'xrefdata') { 
	if(objs[i].style)
	  objs[i].style.display='none';
	if(objs[i].display)
	  objs[i].display = 'none';
      };
    }
  }
}

function show_xref(col) {
  hide_div('col2');
  if(col == 1) {
    hide_div('col3');
  } else {
    hide_div('col1');
  }
  hide_div('s_xref_' + col);
  show_div('h_xref_' + col);
  table = document.getElementById('resultstable1');
  if(table) {
    objs = table.getElementsByTagName('td');
    for (var i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'xrefdata') { 
	if(objs[i].style)
	  objs[i].style.display='';
	if(objs[i].display)
	  objs[i].display = '';
      };
    }
    objs = table.getElementsByTagName('th');
    for (i = 0; i < objs.length; i++) { 
      if(objs[i].getAttribute('name') == 'xrefdata') { 
	if(objs[i].style)
	  objs[i].style.display='';
	if(objs[i].display)
	  objs[i].display = '';
      };
    }
  }
}


function show_div(div_name) {
  var my_div;
  if(div_name.match(/_loading/))
  	return;
  my_div = document.getElementById ? document.getElementById(div_name) : null;
  if (my_div) {
    if (my_div.style) my_div.style.display = '';
    else if (my_div.display) my_div.display = '';
  } else {
    //alert('Cant find ' + div_name);
  }
}

function hide_div(div_name) {
	var my_div;
	my_div = document.getElementById ? document.getElementById(div_name) : null;
	if (my_div) {
		if (my_div.style) {
			my_div.style.display = 'none';
		} else if (my_div.display) {
			alert(div_name);
		}
	}
}

function set_graph_src(is_dev) {
  readCookie('PHPSESSID');
  var my_obj;
  var my_name_obj;
  my_obj = document.getElementById ? document.getElementById('keyword_image_frame') : null; 
  my_name_obj = document.getElementById ? document.getElementById('keyword_image_name') : null; 
  if(my_obj) {
    my_obj.src = '/single-term-chart.php?user_id=' + document.keyword_form.user_id.value + '&query=' + document.keyword_form.graph_query.value + '&style=' + readCookie('graph_style') + (readCookie('raw') == '1' ? '&raw=1' :'') + '&PHPSESSID=' + readCookie('PHPSESSID');
    if(my_name_obj) {
      my_name_obj.innerHTML = document.keyword_form.graph_query.value;
    }
  }
}

function show_just_this_div(divname) {
  hide_div('results_div_2');
  hide_div('adverts');
  hide_div('keywords2_list');
  hide_div('keywords2_loading');
  hide_div('paging_2');
  hide_div('keyword_image');
  hide_div('projects_list');
  hide_div('keyword_help');
  hide_analyse(1);
  hide_analyse(2);
  show_div(divname);
}
