
function doHttpRequest(url,contentId,method,parameters,postFunction){
    if(method==''){
        method = 'GET'
    }
    poststr = '';
    if(typeof(parameters)=='object' && parameters.length>0){
        for(i=0;i<parameters.length;i=i+2){
            if(poststr!='')
                poststr += '&';
            poststr += parameters[i] + "=" + escape(encodeURI( parameters[i+1] ))
        }
    }

    var httpRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    }else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
        catch (e) {
              try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                   }
              catch (e) {}
        }
    }
    if (!httpRequest) {
        alert('Your browser does not support AJAX!');
        return false;
    }

    regExpFunc = /jsFunction:.*/
    regExpCode = /jsCode/
    if(contentId.match(regExpFunc)){

    }else if(contentId.match(regExpCode)){

    }else{
        document.getElementById(contentId).innerHTML = '<img src="../image/ajaxloader.gif" />';
    }

    httpRequest.onreadystatechange = function() { refreshHttpContent(httpRequest,contentId,postFunction); };
    httpRequest.open(method, url, true);
    if(method=='POST'){
        httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        httpRequest.setRequestHeader("Content-length", poststr.length);
        httpRequest.setRequestHeader("Connection", "close");
        httpRequest.send(poststr);
    }else{
        httpRequest.send('');
    }
}

function refreshHttpContent(httpRequest,contentId,postFunction) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            regExpFunc = /jsFunction:.*/
            regExpCode = /jsCode/
            if(contentId.match(regExpFunc)){
                funcName = contentId.replace('jsFunction:','')
                eval(funcName+'("'+escape(httpRequest.responseText)+'")')
            }else if(contentId.match(regExpCode)){
                eval(httpRequest.responseText)
            }else{
                document.getElementById(contentId).innerHTML = httpRequest.responseText;
            }
            if(postFunction!=''){
                eval(postFunction);
            }
        } else {
            alert('There was a problem with ajax: the browser received a response '+httpRequest.status);
        }
    }
}

var win=null;

function OpenNewWindow(mypage,myname,w,h,scroll,pos,resiz)
{
    if(resiz!="yes")
    {
        resiz = 'no';
    }
    if(pos=="random")
    {
        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
    }
    if(pos=="center")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    if(pos=="top")
    {
        LeftPosition=20;
        TopPosition=20;
    }
    else if((pos!="center" && pos!="top" && pos!="random") || pos==null)
    {
        LeftPosition=0;
        TopPosition=20
    }

    settings='width='+w+', height='+h+', top='+TopPosition+', left='+LeftPosition+', scrollbars='+scroll+', location=no, directories=no, status=no, menubar=no, toolbar=no, resizable='+resiz;
    win=window.open(mypage,myname,settings);

    if(win.focus)
    {
        win.focus();
    }
}

var NaviWin;
function NavigaWin(pagina, l, a)
{
    NaviWin=window.open(pagina,'','toolbar=no,left=50,top=80,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=' + l +',height=' + a);
}


function CloseNewWindow()
{
    if(win!=null && win.open)
        win.close()
}

function DeleteRecord(url){
    if(strtodisplay==""){
        strtodisplay = "E' sicuro di voler cancellare il record specificato?";
    }
    if(confirm(strtodisplay)){
        location = url;
    }
    return true;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function GotoRelation(valore, url){
    if(valore != ""){
        location.href = url+valore ;
    }else{
        alert("Nessun elemento correlato");
    }
}

function OpenBp(id){

    w = screen.width-80;
    h = screen.height-90;

    OpenNewWindow('/bp_editor/index.php?BP_id='+id,'bp'+id,w,h,'false','top','yes');
}

function OpenUserBp(id){

    w = screen.width-80;
    h = screen.height-90;

    OpenNewWindow('/bp_editor/create_revision.php?BP_id='+id,'bp'+id,w,h,'false','top','yes');
    for(i=0;i<1000;i++){
        // do nothing just take time
    }
    window.location.reload();
}

function stampa_popup(div_id) {
	var testo = "<html><head><title>"+document.title+"</title><link href=\"../css/global.css\" rel=\"stylesheet\" type=\"text/css\"></head>";
	testo += "<body style=\"background-image:none;background-color:white\">";
	testo += "<table width=\"100%\" border=\"0\"><tr><td>";
	testo += document.getElementById(div_id).innerHTML+"</td></tr></table>";
	testo += "</body></html>";
	var ident_finestra = window.open("","finestra_stampa","height=400,width=400");
	ident_finestra.document.open();
	ident_finestra.document.write(testo);
	ident_finestra.document.close();
	ident_finestra.print();
	ident_finestra.close();
}
