/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

var idelemento = new function(){
    
    this.id_elemento = null;
    this.album = 0;
    
    this.risultato = function(){
        return document.getElementById("ciccio_").innerHTML;
    }
    
    this.cambiaValore = function(url){        
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
            searchReq.open("GET", window.location.href, true);
            searchReq.onreadystatechange = function(){
            if (searchReq.readyState == 4) {
                document.getElementById("ciccio_").innerHTML = "testo modificato";
            }};
            searchReq.send(null);
	}
        //this.id_elemento = url.substring( url.lastIndexOf("/")+1, url.lastIndexOf(".")); 
        document.getElementById("ciccio_").innerHTML = "testo modificato";  
        console.log(document.getElementById("ciccio_").innerHTML);
        console.log(window.location.href);
        
    }      
    this.getDiv = function(url){
        //console.log(url);
        this.id_elemento = url.substring( url.lastIndexOf("/")+1, url.lastIndexOf("."))
        //console.log(this.id_elemento);
        return this.id_elemento;
    }    
    this.getAlbum = function(url){
        console.log(url);
        this.album = url.substring( url.lastIndexOf("contenuti/")+10, url.lastIndexOf("/"))
        console.log(this.album);
        return this.album;
    }    
}

function addComment(content_el, id_contenuto, id_foto, testo, nickname) {
    
    var areaval = content_el.find('textarea:eq(0)').val();
    var textval = content_el.find('input:eq(0)').val();
    if( areaval == '' || areaval == 'Messaggio' || textval == '' || textval == 'NickName'){        
        jQuery.noticeAdd1({
            text: 'NickName e Testo sono Necessari'
        });
        return false;
    }  
    
    var url = ('include/addComments.php?id_contenuto='+id_contenuto+'&id_foto='+id_foto+'&testo='+testo.replace("'", "\\'")+'&nickname='+nickname.replace("'", "\\'"));
    //console.log(url);
    if (searchReq.readyState == 4 || searchReq.readyState == 0) {
        searchReq.open("GET", url, true);
        searchReq.onreadystatechange = function() {
            if (searchReq.readyState == 4) {
                var str = searchReq.responseText.split("\n");
                //console.log(str[0]);
                if(str[0]=="success"){
                    //$('#box').load('include/loadComments_1.php?id_foto='+str[1]+'');
                    $(content_el).find('ul:eq(0)').load('include/loadComments.php?id_foto='+str[1]+'', function(response, status, xhr) {
                      if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        alert(msg + xhr.status + " " + xhr.statusText);
                        console.log(msg + xhr.status + " " + xhr.statusText);
                      }
                    });
                    //$(content_el).find('textarea:eq(0)');
                }else
                    jQuery.noticeAdd1({
                        text: 'Commento non caricato! Riprova'
                    });
            }
        };        
        searchReq.send(null);
    }
}

function checkForm(el){
    var areaval = el.find('textarea:eq(0)').val();
    var textval = el.find('input:eq(0)').val();
    //console.log(areaval+textval);
    
    if( areaval == '' || areaval == 'Messaggio' || textval == '' || textval == 'NickName'){        
        jQuery.noticeAdd1({
            text: 'NickName e Testo sono Necessari'
        });
        return false;
    }else{
        return true;
    }
    return false;
    
}
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return null;
  else
    return qs[1];
}
