// inhibit JS errors
window.onerror = function(){
    return true;
}
// initiate search from quick search form
function search_selected(){
    var searchVal = document.getElementById("search_text").value;
    
    if (trim(searchVal)!="" && (searchVal.toLowerCase()!="or") && (searchVal.toLowerCase()!="and") && (searchVal.toLowerCase()!="not") && (searchVal.toLowerCase()!="the"))
    {
        var searchAction = basepath;
        var searchOption = document.getElementById('select_option').selectedIndex;
        
        var encodedSearch = encodeURIComponent( searchVal.replace( /\//g, " " ).replace( /&/g, " " ).replace( /-/g, " " ) );
        switch( searchOption )
        {
          case 0:
            searchAction += 'search_results/title|' + encodedSearch +'?rentit=Y&m_search=yes';
            break;
          case 1:
            searchAction += 'disambiguation/'+encodedSearch+'?m_search=yes';
            break;
          case 2:
            searchAction += 'search_results/keyword|' + encodedSearch +'?rentit=Y&m_search=yes';
            break;
          case 3:
            searchAction += 'search_results/series|' + encodedSearch +'?rentit=Y&m_search=yes';
            break;
        }
        document.simple_search.method = "POST";
        document.simple_search.action = searchAction;
     }
     else
     {
        alert("Please enter text to search");
        return false;
     }
     return true;
}
function Go_home(e){
    var browserName=navigator.appName;
    var unicode=e.charCode? e.charCode : e.keyCode;
        if (unicode==13)
        {
           search_selected();
        }
        if (browserName=="Microsoft Internet Explorer")
        {
            if(unicode==37)
            return false;
        }
        else
        {
            if((unicode==37 && e.keyCode == 0))
            return false;
        }
}
// Removes leading whitespaces
function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value){
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}
function trim(value) {
    return LTrim(RTrim(value));
}



// fill in the <option> values for a <select> control
function populateDropdown( list, selectId )
{
    var opts = "";
    for (var n=0; n < list.length; n++)
    {
        var opt = list[n];
        if (typeof(opt) == "object")
          opts += "<option value='" + opt['ID'] + "'>" + opt['title'] + "</option>\n";
        else
          opts += "<option>" + opt + "</option>\n";
    }
    document.getElementById( selectId ).innerHTML = opts;
    // This is needed to fix an IE6 bug
    var ie = (document.all) ? 1 : 0;
    if (ie)
      $("#"+selectId).parent().get(0).innerHTML += '';
}

//Opens a popup for the give n URL with the given params
function popup(url,width,height) 
{
	// var width  = 300;
	// var height = 200;
	 var left   = (screen.width  - width)/2;
	 var top    = (screen.height - height)/2;
	 var params = 'width='+width+', height='+height;
	 params += ', top='+top+', left='+left;
	 params += ', directories=no';
	 params += ', location=no';
	 params += ', menubar=no';
	 params += ', resizable=no';
	 params += ', scrollbars=no';
	 params += ', status=no';
	 params += ', toolbar=no';
	 newwin=window.open(url,'windowname5', params);
	 if (window.focus) {newwin.focus();}
}

