function redirect(destination)
{	window.location=destination;
}

var a=0;
var b=0;
var pos;
var temp_str="";
var type=0;
var event;

var BrowserDetect =
    {init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//if (window.Event)
//{   event = document.captureEvents(Event.MOUSEUP);
//}
//document.onmouseup = selection_string;
//
//function selection_string(){
//    if (document.selection && document.selection.createRange) {
//        var range = document.selection.createRange();
//        temp_str = range.text;
//    }
//}

function clear_all(id){
    element = document.getElementById(id);
    element.selectionStart=(element.value).length;
    element.selectionEnd = (element.value).length;
    pos[0]=0;
    pos[1]=0;
    temp_str='';
}

function find_special(stringa){
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_";
    for (var i = 0; i < stringa.length; i++) {
        if (iChars.indexOf(stringa.charAt(i)) != -1)
            return true;
    }
    return false;
}

function selection_IE(id){
    element = document.getElementById(id);
//    if(find_special(temp_str)) //alert('Carattere speciale, non è possibile procedere');
//        pos = Array((element.value).length,(element.value).length);
//    else
//        pos=new Array((element.value).search(temp_str),(element.value).search(temp_str) + temp_str.length);
    pos= new Array((element.value).length,(element.value).length);
}

function selection_Other(id){
    element = document.getElementById(id);
    pos=new Array(element.selectionStart,element.selectionEnd);
}

function wysiwyg_link(id){
    temp = prompt('Link da inserire: ','http://');
    element = document.getElementById(id);
    if (BrowserDetect.browser=="Explorer"){
        selection_IE(id);
    }
    else
        selection_Other(id);
    nome = (element.value).substring(pos[0],pos[1]);
    if(nome=="")
        nome = 'TESTO LINK';
    if (temp.substring(0,7)=="http://")
        string = '[link url="'+temp+'"]'+nome+'[/link]';
    else
        string = '[link url="http://'+temp+'"]'+nome+'[/link]';
    a = pos[0];
    b = pos[1];
    clear_all(id);
    type=1;
    wysiwyg(id,string);
}

function wysiwyg_download(id,name,mode){
//se mode=1 vuole la thumb
//se mode=0 non vuole la thumb
    element = document.getElementById(id);
    if (BrowserDetect.browser=="Explorer")
        selection_IE(id);
    else
        selection_Other(id);
    temp_d = (element.value).substring(pos[0],pos[1]);
    switch(mode){
        case 1:{
            if(temp_d=="")
                temp_d = prompt('Nome da usare: ',name);
            string = '[download title="'+temp_d+'" thumb="AUTO"]'+name+'[/download]';
            break;}
        case 0:{
            if(temp_d=="")
                temp_d = prompt('Nome da usare: ',name);
            string = '[download title="'+temp_d+'" thumb="NONE"]'+name+'[/download]';}
    }
    a = pos[0];
    b = pos[1];
    clear_all(id);
    type = 1;
    temp_d='';
    wysiwyg(id,string);
}

function wysiwyg(id,string){
    element = document.getElementById(id);
    temp1="";
    temp2="";
    temp=string;
    if (type==0){
        if (BrowserDetect.browser=="Explorer")
            selection_IE(id);
        else
            selection_Other(id);
    index1 = string.indexOf("]");
    index2 = string.lastIndexOf("[");
    temp1=string.slice(0,index1+1);
    temp2=string.slice(index2-(index2-index1)+1);
    temp= (element.value).substring(pos[0],pos[1]);
    }
    string = temp1+temp+temp2;
    if ((a!=0) && (b!=0)){
        temp1 = (element.value).substring(0,a);
        temp2 =(element.value).substring(b);
        element.value =temp1 + string + temp2;
    }
    else{
        temp1 = (element.value).substring(0,pos[0]);
        temp2 =(element.value).substring(pos[1]);
        element.value =temp1 + string + temp2;
    }
    clear_all(id);
    a=0;
    b=0;
    type = 0;
}


function TreeExec(op,subject, ask)
{	box_op = document.getElementById('tree_editor_op');
	box_op.value = op;
	box_subject = document.getElementById('tree_editor_subject');
	box_subject.value = subject;
	box_ask = document.getElementById('tree_editor_ask');
	box_ask.value = ask;
	form = document.getElementById('tree_editor');
	form.submit();
}

function TreeExecConfirm(op, subject, ask, question)
{	var ret = confirm(question);
	if(ret==true)
	{	TreeExec(op, subject, ask);
	}
}

function TreeExecAsk(op, subject, ask, question)
{	var ask = prompt(question, ask);
	if(ask!=null && ask!="")
	{	TreeExec(op, subject, ask);
	}
}
