//customs js FunnyDog website r.federici@tnw.it
/*
window.addEvent('domready', function(){
        var elementsToFold = $$('div[id^=sub]');
        elementsToFold.each(function(el) {
           //aggiunge descrizione testuale
           el.getPrevious().setAttribute('title','fai click per espandere/chiudere');
           el.getPrevious().addEvent('click', function(e){
                 var mySlide = new Fx.Slide(el);
                 e = new Event(e);
                 mySlide.toggle();
                 e.stop();
           });
        });
        //fold all elements
        elementsToFold.each(function(el) {
           var mySlide = new Fx.Slide(el);
           mySlide.toggle();
        });
});
*/

//iscrizione singola
function updateSingleIscr(id,table,record,value){
    var dataToEdit = Object.toQueryString({tableRead: table, fieldRead: id, idRead: record, valueRead: value});
    var AjaxReq = new Ajax('iscr_s.php', {
                           data: dataToEdit,
                           method: 'get',
                           onComplete: function(responseTxt, responseXml) {
                                   curResponseText = this.response.text ;
                                   curResponseXml = this.response.xml ;
                                   createRadio( curEditId, curResponseText ) ;
                                   }
                           }).request();
}

//elimina binomio
function delbin(idbin){
 if(idbin.length > 0){
  if( confirm('Vuoi davvero eliminare questo binomio conduttore/cane?') ){
  var req = new Request({
        method: 'get',
        url: 'whoof_delbin.php',
        data: { 'idbin' : idbin },
        onComplete: function(response) {
                                        location.reload(); 
                                        }
        }).send();
  }
 }
}

//elimina conduttore
function delcon(id){
if(id.length > 0){
  if( confirm('Vuoi davvero eliminare questo conduttore?\n(NB:La pagina sarà ricaricata!)')  ){
  var req = new Request({
        method: 'get',
        url: 'whoof_delcon.php',
        data: { 'id' : id },
        onComplete: function(response) {
                                        location.reload(); 
                                        }
        }).send();
  }
 }
}

//elimina conduttore
function delcan(id){
 if(id.length > 0){
  if( confirm('Vuoi davvero eliminare questo cane?\n(NB:La pagina sarà ricaricata!)') && eval(id)>0 ){
  var req = new Request({
        method: 'get',
        url: 'whoof_delcan.php',
        data: { 'id' : id },
        onComplete: function(response) {
                                        location.reload(); 
                                        }
        }).send();
  }
 }
}

//elimina gara
function delgar(id){
 if(id.length > 0){
  if( confirm('Vuoi davvero eliminare questa gara?\n(NB:La pagina sarà ricaricata!)') && eval(id)>0 ){
  var req = new Request({
        method: 'get',
        url: 'whoof_delgar.php',
        data: { 'id' : id },
        onComplete: function(response) {
                                        location.reload(); 
                                        }
        }).send();
  }
 }
}

//aggiungi binomio
function addbin(){
//create overlay
createOverlayField();
//parsa url con mootools
var urlQry = new URI(location.href);
var cClub  = urlQry.getData('club');
  var req = new Request({
        method: 'get',
        url: 'whoof_bin.php',
        data: { 'club' : cClub },
        onComplete: function(response) {
                                        var formDiv = document.createElement('div') ;
                                        formDiv.id  = 'formDiv';
                                        $('holder').appendChild(formDiv);
                                        $('formDiv').innerHTML = response;
                                        setCentered('formDiv');
                                        var myFx = new Fx.Scroll(window).start(0, document.body.offsetTop);
                                        //location.reload(); 
                                        }
        }).send();

$('overlayField').setStyle('display','block');

}

//modifica binomio
function modbin(idbin){
//create overlay
createOverlayField();
//parsa url con mootools
var urlQry = new URI(location.href);
var cClub  = urlQry.getData('club');
  var req = new Request({
        method: 'get',
        url: 'whoof_bin.php',
        data: { 'idbin' : idbin, 'club' : cClub },
        onComplete: function(response) {
                                        var formDiv = document.createElement('div') ;
                                        formDiv.id  = 'formDiv';
                                        $('holder').appendChild(formDiv);
                                        $('formDiv').innerHTML = response;
                                        setCentered('formDiv');
                                        var myFx = new Fx.Scroll(window).start(0, document.body.offsetTop);
                                        //location.reload(); 
                                        }
        }).send();

$('overlayField').setStyle('display','block');

}

//conferma e aggiunge binomio
function confbin(idbin,idcon,idcan){
 if(confirm('Attenzione!\nQuesta operazione conferma tutte le modifiche apportate.\nContinuare?')){
 //controllo dei campi!
 if( 
     $('ana_club').value                             != '' && 
     $('ana_tessera_enci').value                     != '' && 
     $('ana_tessera_enci_rilascio_anno').value       != '' && 
     $('ana_cognome').value                          != '' && 
     $('ana_nome').value                             != '' && 
     $('can_nome').value                             != '' && 
     $('can_nascita').value                          != '' && 
     $('can_sesso').value                            != '' && 
     ( $('can_loi_n').value != '' || $('can_tatuaggio_numero').value != '' ) && 
     $('can_razza').value                            != '' && 
     $('can_proprietario').value                     != '' && 
     $('can_classe').value                           != '' && 
     $('can_categoria').value                        != ''
    ){
    var req = new Request({
          method: 'get',
          url: 'whoof_crbin.php',
          data: { 'idbin' : idbin,
                  'ana_club' : $('ana_club').value,
                  'idcon' : idcon,
                  'ana_tessera_enci' : $('ana_tessera_enci').value,
                  'ana_tessera_enci_rilascio_anno' : $('ana_tessera_enci_rilascio_anno').value,
                  'ana_cognome' : $('ana_cognome').value,
                  'ana_nome' : $('ana_nome').value,
                  'idcan' : idcan,
                  'can_nome' : $('can_nome').value,
                  'can_nascita' : $('can_nascita').value,
                  'can_sesso' : $('can_sesso').value,
                  'can_loi_n' : $('can_loi_n').value,
                  'can_tatuaggio_numero' : $('can_tatuaggio_numero').value,
                  'can_razza' : $('can_razza').value,
                  'can_proprietario' : $('can_proprietario').value,
                  'can_classe' : $('can_classe').value,
                  'can_categoria' : $('can_categoria').value
                  },
          onComplete: function(response) {
                                          location.reload(); 
                                          }
          }).send();
    }else{
          alert('Attenzione!\nUno o più campi non sono stati compilati.\nTutti i campi devono essere compilati per l\'anagrafica coduttore, per il cane è possibile compilare in modo alternativo o il Tatuaggio/chip OPPURE il ROI/RSR');
    }
 }
}

//legge dati conduttore
function read_con(id){
 if(id.length > 0){
  var req = new Request({
        method: 'get',
        url: 'whoof_read_con.php',
        data: { 'id' : id },
        onComplete: function(response) {
                                        var arr_data = response.split("#");
                                        $('ana_tessera_enci').value                 = arr_data[0];
                                        $('ana_tessera_enci_rilascio_anno').value   = arr_data[1];
                                        $('ana_cognome').value                      = arr_data[2];
                                        $('ana_nome').value                         = arr_data[3];
                                        $('ana_club').value                         = arr_data[4];
                                        }
        }).send();
  }else{
                                        $('ana_tessera_enci').value                 = "";
                                        $('ana_tessera_enci_rilascio_anno').value   = "";
                                        $('ana_cognome').value                      = "";
                                        $('ana_nome').value                         = "";
  }
}

//legge dati cane
function read_can(id){
 if(id.length > 0){
  var req = new Request({
        method: 'get',
        url: 'whoof_read_can.php',
        data: { 'id' : id },
        onComplete: function(response) {
                                        var arr_data = response.split("#");
                                        $('can_nome').value                           = arr_data[0];
                                        $('can_nascita').value                        = arr_data[1];
                                        $('can_sesso').value                          = arr_data[2];
                                        $('can_loi_n').value                          = arr_data[3];
                                        $('can_tatuaggio_numero').value               = arr_data[4];
                                        $('can_razza').value                          = arr_data[5];
                                        $('can_proprietario').value                   = arr_data[6];
                                        $('can_classe').value                         = arr_data[7];
                                        $('can_categoria').value                      = arr_data[8];
                                        }
        }).send();
  }else{
                                        $('can_nome').value                           = "";
                                        $('can_nascita').value                        = "";
                                        $('can_sesso').value                          = "";
                                        $('can_loi_n').value                          = "";
                                        $('can_tatuaggio_numero').value               = "";
                                        $('can_razza').value                          = "";
                                        $('can_proprietario').value                   = "";
                                        $('can_classe').value                         = "";
                                        $('can_categoria').value                      = "";
  }
}

//iscrive tutti i selezionati
var totIscr = 0;
function bin_iscr(){
//ritorna solo i binomi flaggati
/*var iscrizioni = $$('.iscrizione').filter(function(item, index){
                                                  return item.checked == true;
                                                  });
*/
var iscrizioni = $$('.iscrizione');
var count      = 1;
totIscr        = iscrizioni.length;
iscrizioni.each(function(iscrizione, i) {
                           //controllo flag iscrizione
                           if(iscrizione.checked == true){
                             var url = 'whoof_crisc.php';
                           }else{
                             var url = 'whoof_drisc.php';
                           }
                           
                           var req = new Request({
                                                  method: 'get',
                                                  url: url,
                                                  data: { 'idgar' : iscrizione.getProperty('idgar'),
                                                          'idbin' : iscrizione.getProperty('idbin'),
                                                          'idcon' : iscrizione.getProperty('idcon'),
                                                          'idcan' : iscrizione.getProperty('idcan')
                                                          },
                                                  onComplete: function(response) {
                                                                                  iscrizione.getParent().getParent().highlight();
                                                                                  count++ ;
                                                                                  if(count==totIscr){
                                                                                    alert('Iscrizione completata.\nLa pagina viene ora ricaricata.');
                                                                                    //05/11/2009 15.40.55 AGGIUNTO INVIO EMAILS
                                                                                    var urlQry  = new URI(location.href);
                                                                                    var host    = urlQry.get('host');
                                                                                    var idgara  = urlQry.getData('idgara');
                                                                                    var cClub   = urlQry.getData('club');
                                                                                    if(cClub!=''){
                                                                                       location.href = 'http://'+host+'/whoof_gare.php?idgara='+idgara+'&club='+cClub+'&sendm=1';
                                                                                    }else{
                                                                                       location.href = 'http://'+host+'/whoof_gare.php?idgara='+idgara+'&sendm=1';
                                                                                    }
                                                                                    
                                                                                  }
                                                                                  }
                                                  }).send();
                });
}


//*************************************
//UTILS
//*************************************

function createOverlayField()
{
  if( $("overlayField") ){
  }else{
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id','overlayField');
    objOverlay.style.display = 'none';
    $("holder").appendChild(objOverlay);
    // stretch overlay to fill page and fade in
    var arrayPageSize = getPageSize();
    objOverlay.style.width = arrayPageSize[0] + 'px';
    objOverlay.style.height = arrayPageSize[1] + 50 + 'px';
  }
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
  
  var xScroll, yScroll;
  
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  
  var windowWidth, windowHeight;
  
//  console.log(self.innerWidth);
//  console.log(document.documentElement.clientWidth);

  if (self.innerHeight) { // all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth; 
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  } 
  
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

//  console.log("xScroll " + xScroll)
//  console.log("windowWidth " + windowWidth)

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){  
    pageWidth = xScroll;    
  } else {
    pageWidth = windowWidth;
  }
//  console.log("pageWidth " + pageWidth)

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}

function setCentered(id){
  var leftAssign = '-' + ($(id).getSize().x / 2) + 'px';
  var topAssign  = '-' + ($(id).getSize().y / 2) + 'px';
  $(id).setStyle('left','50%');
  $(id).setStyle('margin-left',leftAssign);
  $(id).setStyle('top','50%');
  $(id).setStyle('margin-top',topAssign);
}

function createMarkerFunny(point,html) {
  var funnyIcon = new GIcon();
  funnyIcon.image = 'http://www.funnydog.org/e107_themes/silverglow/images/image.png';
  funnyIcon.shadow = 'http://www.funnydog.org/e107_themes/silverglow/images/shadow.png';
  funnyIcon.iconSize = new GSize(30,30);
  funnyIcon.shadowSize = new GSize(45,30);
  funnyIcon.iconAnchor = new GPoint(15,30);
  funnyIcon.infoWindowAnchor = new GPoint(15,0);
  funnyIcon.printImage = 'http://www.funnydog.org/e107_themes/silverglow/images/printImage.gif';
  funnyIcon.mozPrintImage = 'http://www.funnydog.org/e107_themes/silverglow/images/mozPrintImage.gif';
  funnyIcon.printShadow = 'http://www.funnydog.org/e107_themes/silverglow/images/printShadow.gif';
  funnyIcon.transparent = 'http://www.funnydog.org/e107_themes/silverglow/images/transparent.png';
  funnyIcon.imageMap = [29,0,29,1,29,2,29,3,29,4,29,5,29,6,29,7,29,8,29,9,29,10,29,11,29,12,29,13,29,14,29,15,29,16,29,17,29,18,29,19,29,20,29,21,29,22,28,23,26,24,24,25,22,26,20,27,18,28,16,29,13,29,11,28,9,27,7,26,5,25,3,24,1,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,0,0];
  markerOptions = {icon:funnyIcon, draggable: false};
  var marker = new GMarker(point,markerOptions);
  GEvent.addListener(marker, "click", function() {
                                        marker.openInfoWindowHtml(html,{maxWidth:350});
                                        }); 
  return marker;
  }

function createMapFunny(x,y){
  window.addEvent('domready', function() {
  if (GBrowserIsCompatible()){
      var map = new GMap2(document.getElementById("mapfunny")); 
      map.setCenter(new GLatLng(x,y), 15); 
      var point = new GLatLng(x,y); 
      var marker = createMarkerFunny(point,"<div style='width:150px;color:black;'>FUNNY DOG <br />Fraz. Fochat n. 23<br />11010 Sarre (AO)<br />Tel: 0165 257439<br />Cell: 349 1890948<br />www.funnydog.org<br />info@funnydog.org</div>");
      map.addOverlay(marker);
      }
  });
}