var ie = ( document.all ) ? true : false;
var group;

function init() {	
	var groups= document.myform.help_category.options.length
	group=new Array(groups)
	for (i=0; i<groups; i++) group[i]=new Array()

	group[0][0]=new Option("Invoices","Invoices")
	group[0][1]=new Option("Overdue payments","Overdue payments")
	group[0][2]=new Option("Quickbooks","Quickbooks")
	group[0][3]=new Option("AccountingSubCat4","AccountingSubCat4")

	group[1][0]=new Option("Links","Links")
	group[1][1]=new Option("GT Forum","GT Forum")
	group[1][2]=new Option("Gossamer Mail","Gossamer Mail")
	group[1][3]=new Option("WonderDesk","WonderDesk")

	group[2][0]=new Option("Telnet","Telnet")
	group[2][1]=new Option("FTP","FTP")
	group[2][2]=new Option("SSH","SSH")

	group[3][0]=new Option("ResearchCat1","ResearchCat1")
	group[3][1]=new Option("ResearchCat2","ResearchCat2")
	group[3][2]=new Option("ResearchCat3","ResearchCat3")	
}

function rs(n, u, w, h, l, t, s) {
    args="width=" + w + ",height=" + h + ",top=" + t + ",left=" + l + ",resizable=no,scrollbars=" + s + ",status=0,titlebar=0";
	remote=window.open(u, n, args);
    if (remote != null)  {
	    if (remote.opener == null)
		    remote.opener = self;
    }
}

function selected_check(frm, obj_name, msg_text) {
    var flag = 0;
	var obj  = eval(frm + '.' + obj_name);
	if ( typeof(obj.length) != 'undefined' ) {
		var nrow = obj.length;  
	    for ( i=0; i<nrow; i++) {
	        if ( obj[i].checked ) {
		        flag = 1;
			    break;
	        }
	    }
	}
	else {
		if ( obj.checked )
	        flag = 1;
	}
	if ( flag == 0 ) {
	    alert('Please select the ' + msg_text + ' from the list!');
	    return false;
	}
	if ( !confirm("Do you want to remove selected " + msg_text + " (s)?") )  
	    return false;
}

function form_validate(frm) {
    var obj = frm.field_selected;
    if ( obj.options.length == 1 ) {
        alert("Please select fields.");
        return false;
    }

    for (ii=1; ii< obj.options.length; ii++) {
        obj.options[ii].selected = true;
    }
    return true
}

function additem(objright,objleft,flag){
//-------------------------------------
    if (flag == 1) {// add all
        var src  = eval(objleft);
        var rows = src.options.length
        for (ii=1; ii< rows ; ii++) {
            moveitem(eval(objleft),eval(objright),1);       
        }
    }
    else 
        moveitem(eval(objleft),eval(objright)); 
}

function removeitem(objright,objleft,flag){
//---------------------------------------
    if (flag == 1) {// move all
        var src  = eval(objright);
        var rows = src.options.length
        for (ii=1; ii< rows ; ii++) {
            moveitem(eval(objright),eval(objleft),1);   
        }
    }
    else 
        moveitem(eval(objright),eval(objleft)); 
}

function moveitem (src,tar,index) {
//---------------------------------
    li_rows  = tar.options.length
    if (index > 0)
        li_index = index
    else 
        li_index = src.selectedIndex
    var objs = 0;

    if (li_index <= 0) {
        alert("Please select an item.")
        return 
    }
    // get value & text from objright
    text = src.options[li_index].text
    value = src.options[li_index].value
        
    // add item into lbleft from lbright.
    if (li_rows==0) {
        tar.options[0] = new Option(text,value);
    } 
    else {
        tar.options[li_rows] = new Option(text,value);      
        tar.options[li_rows].selected = true
    }
                            
    for (m=src.options.length-1;m>=0;m--)  {
        if (src.options[m].value==value)  src.options[m]=null
    }
}

function select_item(cb) {    
    if (cb.checked) hL(cb);
    else dL(cb);
}

function select_all(obj_name) {     
    var obj = eval('document.frm_main.' + obj_name);
    if ( typeof(obj) == 'undefined' ) return;
    if ( typeof(obj.length) == 'undefined' ) {
        if ( !obj.disabled ) {
            if ( document.frm_main.c_all.checked ) {
                obj.checked = true; 
                if ( ie ) hL(obj);
            }
            else {
                obj.checked = false;
                if ( ie ) dL(obj);
            }
        }
    }
    else { 
        for (i=0; i< obj.length; i++) {
            if ( !obj[i].disabled ) { 
                if ( document.frm_main.c_all.checked ) {
                    obj[i].checked = true; 
                    if (ie ) hL(obj[i]);
                }
                else {
                    obj[i].checked = false;
                    if ( ie ) dL(obj[i]);
                }
            }
        }
    }        
}
    
function hL(E){
    if (ie) {
        while (E.tagName!="TR") { E=E.parentElement; }
    }
    else {
        while (E.tagName!="TR") { E=E.parentNode; }
    }
    E.className = "highlight";
}

function dL(E){
    if (ie) {
        while (E.tagName!="TR") { E=E.parentElement; }
    }
    else {
        while (E.tagName!="TR") { E=E.parentNode; }
    }
    E.className = 'body';
}

function validate_import(frm) {
   var from = frm.import_from.options[frm.import_from.selectedIndex].value;
   if ( from == '' ) {
       alert("Select the way you want to import from")
       return false;
   }
   else if ( (from != 'text') && ( frm.gt_root_path.value == '' )) {
       alert("Enter the ADMIN ROOT PATH \nThis should be the PATH to the directory where your admin files are. \nNo trailing slash please.")
       return false;
   }
}

function show_title(obj) {
   var value = obj.options[obj.selectedIndex].value;
   document.myform.next_step.value = 'Import';                        
   if ( value ) {
       if ( (value == 'dbsql') || ( value == 'text') ) 
           myform.next_step.value = 'Next Step';
   }
}

function import_select_fields() {
    var frm  = document.myform;        
    var to   = frm.to_selected;
    var from = frm.from_selected;
    for (ii=1; ii< to.options.length; ii++) {
        to.options[ii].selected = true;
    }
    for (ii=1; ii< from.options.length; ii++) {
        from.options[ii].selected = true;
    }
}

function import_load_user() {
    var frm  = document.myform;        
    import_select_fields()
    frm.load.value = 1;
    frm.submit();
}

function import_validate(frm) {
    import_select_fields();
    if ( frm.to_selected.options.length != frm.from_selected.options.length ) {        
        alert("Selected fields of 'from' and 'to' should be equal!")
        return false;
    }
}

function database_confirm_delete(url, col_name) {
   if ( confirm("Do you want to remove " + col_name + " column?") )
       window.location = url + "?do=admin_database_column_delete&c=" + col_name
}

function database_sql_validate(frm) {
    if ( frm.sql_query.value == '' ) {
        alert("Please enter your query into the textarea box!")
        return false;
    }            
}


function resize() {
    var obj  = document.myform.tpl_text;
    var rows = document.myform.nrows.value;
    var cols = document.myform.ncols.value;
    if (isNaN(rows) || isNaN(cols) ||  rows < 0 || cols < 0) {
        alert("Rows and Cols should be greater than 0")
        return;
    }
    obj.cols = cols;
    obj.rows = rows;
}

function switchWrap() {
    if (document.myform.tpl_text.wrap == 'off') {
        document.myform.tpl_text.wrap = 'soft';
        document.myform.bswitch.value = 'Disable wrap';
    }
    else {
        document.myform.tpl_text.wrap = 'off';
        document.myform.bswitch.value = 'Enable wrap';
    }
}

function gotoLine() {
    var obj  = document.myform.tpl_text;
    var line = document.myform.line.value;
    if (obj.innerText) {
        var items = obj.innerText.split("\n");
        if (isNaN(line) || line <= 0 || !line)
            line = 1;
        else if (line > items.length)
            line = items.length;

        var lineContent = items[line - 1];
        var rangeContent = '';
        for (var i = 0; i < line; i++)
            rangeContent += items[i];

        var items    = rangeContent.split(lineContent);
        var numTimes = (items.length > 1) ? (items.length - 1) : 1;

        var txtRange = obj.createTextRange();
        var found    = txtRange.findText(rangeContent);
        if (found) {
            var tmp = 0;
            while (txtRange.findText(lineContent)) {
                tmp++;
                if ( tmp == numTimes ) {
                    txtRange.select();
                    break;
                }
                txtRange.moveStart("character",1);
            }
            txtRange.scrollIntoView();
        }
    }
}

function formHandler(form){
	var URL = document.form.site.options
	[document.form.site.selectedIndex].value;
	window.location.href = URL;
}

function redirect(x){
	if (typeof(document.myform.help_sub_category) == 'undefined'){return;}
	init();
	var temp=document.myform.help_sub_category
	for (m=temp.options.length-1;m>0;m--) temp.options[m]=null
	if (x<0){
		temp.options[0]=new Option('---','')
		return
	}		
	for (i=0;i<group[x].length;i++){
		temp.options[i]=new Option(group[x][i].text,group[x][i].value)
	}
	temp.options[0].selected=true
}

function load_subcat(subcat){
	if (typeof(document.myform.help_sub_category) == 'undefined'){return;}
	init();
	var temp=document.myform.help_sub_category
    var x=document.myform.help_category.options.selectedIndex
	for (m=temp.options.length-1;m>0;m--) temp.options[m]=null
	for (i=0;i<group[x].length;i++){
		temp.options[i]=new Option(group[x][i].text,group[x][i].value)
		if (subcat == group[x][i].value)
		{
			temp.options[i].selected=true
		}
	}
	
}

function go(){
	location=temp.options[temp.selectedIndex].value
}

