// JavaScript Document
/////////////////////////////////////////////////
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sendReq(display) {
    http.open('get', '../torc/utilities/rpc.php?display='+display);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
		//alert (response)
        var update = new Array();
		//alert(response);
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
			if ( update.length > 0 ){
				var elem = document.getElementById(update[0]);
				if ( elem ){
            		elem.innerHTML = update[1];
				}
			}
			
        }
    }
}
///////////////////////////////////////////////////////////////////////////
// selectedContentType( obj ) called when starting upload in torc
// to determine what kind of content user wants to upload
function selectedContentType( obj ){
	var name = obj.options[obj.selectedIndex].value;
	if  (name != "none" ){
		//alert(name);
		sendReq(name);
		//obj.options[0]=new Option("Please select type", "none", true, true);
	}
}
///////////////////////////////////////////////////////////////////////////
// check if the search box is filled. first step when user attempts to 
// to make a contribution
function checkFirstSteps( id ){
	var elem = document.getElementById(id);
	if ( elem ) {
		if ( !elem.value ){
			alert("Please fill in search box");
		} else {
			//document.first_steps.action.value = "search_previous";
			return true;
		}
		return false;
	}
	return false;
	
}

var toggle_on = 1;

// togles display of the results in niches
function toggleTRDisplay( id, tbl_id, vstart) 
{
  var vstart = new Number(vstart);
  var tbl = document.getElementById(tbl_id);
  var hide_rows = true;
  var len = tbl.rows.length;

  var y = 0;
  //if(vstart == -1){vstart = 10;}
  document.getElementById("collapser_"+id).innerHTML = "";
  for(i=0 ; i < len; i++)
  {
    var trid = "tr_" + id;
    if ( tbl.rows[i].id == trid )
    {
      var vstartnew = vstart + 10;
      if(y >= vstart && y <= vstartnew) 
      {
        tbl.rows[i].style.display  = "";
        hide_rows = false;
        document.getElementById("collapser_"+id).innerHTML = "<img src=\"../torc/images/hide.gif\" alt=\"hide\" border=\"0\" align=\"right\" />close";
      } 
      else 
      {
        tbl.rows[i].style.display  = "none";
        hide_rows = true;
		
      }
      
      y++;
    }
    
  }

  if (hide_rows)
  {
    document.getElementById("tr_sep_"+id).style.display = "";
  } 
  else 
  {
    document.getElementById("tr_sep_"+id).style.display = "none";
  }
}

function toggleTRDisplayBackup( id, tbl_id) {
	if ( toggle_on != 1 ) { return; }
	tbl = document.getElementById(tbl_id);
	var hide_rows = true;
	if ( tbl ){
		
		var len = tbl.rows.length;
		//alert( tbl_id );
		for(i=0 ; i< len; i++){
			if ( tbl.rows[i].id == "tr_"+id ){
				
				if( tbl.rows[i].style.display  == "none" ) {
					tbl.rows[i].style.display  = "";
					hide_rows = false;
					document.getElementById("collapser_"+id).innerHTML = "<img src=\"../torc/images/hide.gif\" alt=\"hide\" border=\"0\" align=\"right\" />hide";
				} else {
					tbl.rows[i].style.display  = "none";
					hide_rows = true;
					document.getElementById("collapser_"+id).innerHTML = "<img src=\"../torc/images/show.gif\" alt=\"show\" border=\"0\" align=\"right\" />show";
				}
			}
			
		 }
		 if (hide_rows){
		 	document.getElementById("tr_sep_"+id).style.display = "";
		} else {
			document.getElementById("tr_sep_"+id).style.display = "none";
		}
	}
}
// togles display of the results in subtopics
function toggleTRSubtopics( id, tbl_id) {
	tbl = document.getElementById(tbl_id);
	var hide_rows = true;
	if ( tbl ){
		var len = tbl.rows.length;
		for(i=0 ; i< len; i++){
			if ( tbl.rows[i].id == "tr_"+id ){
				if( tbl.rows[i].style.display  == "none" ) {
					tbl.rows[i].style.display  = "";
					hide_rows = false;
					document.getElementById("collapser_"+id).innerHTML = "<span class=\"showhide\"><img src=\"../torc/images/hide.gif\" alt=\"hide\" border=\"0\" align=\"right\" />hide</span>";
				} else {
					tbl.rows[i].style.display  = "none";
					hide_rows = true;
					document.getElementById("collapser_"+id).innerHTML = "<span class=\"showhide\"><img src=\"../torc/images/show.gif\" alt=\"show\" border=\"0\" align=\"right\" />show</span>";
				}
			}
			
		 }
		/* if (hide_rows){
		 	document.getElementById("tr_sep_"+id).style.display = "";
		} else {
			document.getElementById("tr_sep_"+id).style.display = "none";
		}*/
	}
}
// togles display of the results in subtopics - titles
function toggleTRSubtopics2( id, tbl_id) {
	tbl = document.getElementById(tbl_id);
	var hide_rows = true;
	if ( tbl ){
		var len = tbl.rows.length;
		for(i=0 ; i< len; i++){
			if ( tbl.rows[i].id == "tr_"+id ){
				if( tbl.rows[i].style.display  == "none" ) {
					tbl.rows[i].style.display  = "";
					hide_rows = false;
					document.getElementById("collapser_"+id).innerHTML = "<span class=\"showhide\"><img src=\"../torc/images/hide.gif\" alt=\"hide\" border=\"0\" align=\"right\" />hide</span>";
				} else {
					tbl.rows[i].style.display  = "none";
					hide_rows = true;
					document.getElementById("collapser_"+id).innerHTML = "<span class=\"showhide\"><img src=\"../torc/images/show.gif\" alt=\"show\" border=\"0\" align=\"right\" />show</span>";
				}
			}
			
		 }
		/* if (hide_rows){
		 	document.getElementById("tr_sep_"+id).style.display = "";
		} else {
			document.getElementById("tr_sep_"+id).style.display = "none";
		}*/
	}
}
// submits basic search form when term is entered
function submitSearchForm(){
	if(document.basic_search.search_term.value){
		location.href='?torc_search&q='+document.basic_search.search_term.value;
	} else {
		return false;
	}
}
function submitSearchForm2(){
	if(document.basic_search2.search_term.value){
		location.href='?torc_search&q='+document.basic_search2.search_term.value;
	} else {
		return false;
	}
}
// display a particuylar form for advanced search since they are all
// diffirent
function showRelevantSearchForm( select_box ) {
	var name = select_box.options[select_box.selectedIndex].value;
	
	switch ( name ){
		case "Download":
			var form_display = document.getElementById("search_downloads");
			var form1_hide = document.getElementById("search_links");
			var form2_hide = document.getElementById("search_bonus_materials");
			if ( form_display ){
				form_display.style.display = "";
				if ( form1_hide ){
					form1_hide.style.display = "none";
				}
				if ( form2_hide ){
					form2_hide.style.display = "none";
				}
				document.advanced_search.action.value= "search_downloads";
			}
		break;
		case "Link":
			var form_display = document.getElementById("search_links");
			var form1_hide = document.getElementById("search_downloads");
			var form2_hide = document.getElementById("search_bonus_materials");
			if ( form_display ){
				form_display.style.display = "";
				if ( form1_hide ){
					form1_hide.style.display = "none";
				}
				if ( form2_hide ){
					form2_hide.style.display = "none";
				}
				document.advanced_search.action.value= "search_links";
			}
		break;
		case "Bonus Material":
			var form_display = document.getElementById("search_bonus_materials");
			var form1_hide = document.getElementById("search_downloads");
			var form2_hide = document.getElementById("search_links");
			if ( form_display ){
				form_display.style.display = "";
				if ( form1_hide ){
					form1_hide.style.display = "none";
				}
				if ( form2_hide ){
					form2_hide.style.display = "none";
				}
				document.advanced_search.action.value= "search_bonus_materials";
			}
		break;
	}
}
//
var popupWindow = null;
function createPopup(path,name,win_width, win_height) 
{
	popup_path = path;
	window_name = name;

    if (popupWindow && !popupWindow.closed) {
        alert('Window already open.  Attempting focus...');
        try {
            popupWindow.focus();
        }
        catch(e) {}
        return false;
    }
	
	if (win_width!=''){
		var cal_width = win_width;
	} else{
    	var cal_width = 415;
	}
	if (win_height!=''){
		var cal_height = win_height;
	} else{
    	var cal_height = 310;
	}

    // IE needs less space to make this thing
    if ((document.all) && (navigator.userAgent.indexOf("Konqueror") == -1)) {
        cal_width = cal_width - 5;
    }

    popupWindow = window.open(path, 'name','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=1,dependent=no,width='+cal_width+',height='+cal_height);

    return false;
}

var win= null;
function NewWindow(mypage,myname,w,h,scroll){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars='+scroll+',';
settings +='resizable=yes';
win=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function MM_openBrWindow(theURL,winName,features) {
window.open(theURL,winName,features);
}


// confirm with user before deletion of the link, download, bonus
function confirmEntityDelete( controlc_tie, mode, rid, wid){
	//var div_to_delete =  document.getElementById("div_"+rid);
	//if ( div_to_delete ){
		//var old_backg = div_to_delete.style.background
		//div_to_delete.style.background = "#FF9933"
		if( confirm("Are you sure you want to delete this item?") ){
			document.write(' ');
			location.replace('?'+controlc_tie+'&mode='+mode+'&rid='+rid+'&wid='+wid);
}
		//div_to_delete.style.background = "#FFFFFF"
	//}
	
	return false;
	
}
// here is number of functions to deal with submissions 
//exclusively for submission toggle special XHR request file
function sendXHRRequest(action) {
    http.open('get', '?torc_select_dir&'+action);
    http.onreadystatechange = handleFormsResponse;
	//alert ('../torc/utilities/rpc_forms.php?'+action)
    http.send(null);
}
function handleFormsResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
		//alert (response)
        var update = new Array();
		//alert(response);
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
			if ( update.length > 0 ){
				try {
					var elem =  document.getElementById(update[0]);
					if ( elem ){
						elem.innerHTML = update[1];
						elem.style.display  = "";
					}
				}
				catch(e) {}
			}
			
        }
    }
}
// expandes/collapses topics for a given wid ( wing id )
function toggleTopics ( wid ){
	try {
		var topics_div =  document.getElementById("wid_"+wid);
		if ( topics_div ){
			//topics_div.style.display = "123";
			if( topics_div.style.display  == "none" && topics_div.innerHTML=="" ) { // then we need to fetch and display results
				var action = "action=get_topics&wid="+wid;
				sendXHRRequest(action);
			} else if ( topics_div.style.display  == "none" && topics_div.innerHTML!="" ) { // need to display previously fetched results
				topics_div.style.display  = "";
			} else { // need to hide the div
				topics_div.style.display  = "none";
			}
		}
	}
	catch(e) {}
	return false;
}
// expandes/collapses subtopics for a given sid ( shelf id )
function toggleSubTopics ( wid, sid ){
	try {
		var topics_div =  document.getElementById("sid_"+sid);
		if ( topics_div ){
			//topics_div.style.display = "123";
			if( topics_div.style.display  == "none" && topics_div.innerHTML=="" ) { // then we need to fetch and display results
				var action = "action=get_subtopics&wid="+wid+"&sid="+sid;
				sendXHRRequest(action);
			} else if ( topics_div.style.display  == "none" && topics_div.innerHTML!="" ) { // need to display previously fetched results
				topics_div.style.display  = "";
			} else { // need to hide the div
				topics_div.style.display  = "none";
			}
		}
	}
	catch(e) {}
	return false;
}

function popup(url) {	
	window.open(url,"PrintBoxPopup","menubar=no,width=430,height=360,toolbar=no");
}