var counter= new Array ();

function getValuesWS (idTextBox, idDiv, wsUrl, baseURL)
{
    //counter[idTextBox] = Math.random ();
    $(idDiv).className="";
    var strSearch = $(idTextBox).getValue();
        //alert (counter[idTextBox]); 
    var strSearch1 = replaceText (strSearch);
    
    $(idDiv).innerHTML = "<img src="+baseURL+"img/loader.gif>";
    if (strSearch1.length > 2)
    {  
      //alert (encodeURI(strSearch));
     // alert(strSearch1);
       
      new Ajax.Request(wsUrl, {
      method: 'get',
      parameters: {'locationName': strSearch1, 'random': counter[idTextBox]},
            
      onSuccess: function(transport) {
      var a = transport.request.parameters['locationName'];
      
      var j = 0;
    
       
         var xml = transport.responseXML;
         var x = xml.documentElement.childNodes;
        
        //alert (counter[idTextBox]); 
        if (a == replaceText ($(idTextBox).getValue()))
        {
            if(x.length>1)
            {    
                $(idDiv).className="FGdestinationDiv";    
                $(idDiv).innerHTML = "<b>Merci de cliquer sur le résultat répondant à votre recherche</b><br />";
            }
            else
            {
                $(idDiv).className="";
                $(idDiv).innerHTML = "";
            }
            for (i=0;i<x.length;i++)
            { 
            if (x[i].nodeType==1)
              {
              
                $(idDiv).innerHTML += "<span class='pointer destinationChoice' onclick=\"$('"+idTextBox+"').value = '"+(x[i].firstChild.nodeValue).replace(/'/g,"\\'")+"';$('"+idDiv+"').innerHTML = '';$('"+idDiv+"').className='';\">"+x[i].firstChild.nodeValue+"</span>";
                $(idDiv).innerHTML += "<br />";
              } 
            }
        }
        else
        {
                $(idDiv).innerHTML += x[1].firstChild.nodeValue.Replace ("random--", "");        
        }
     
         
      }
      
    });

    }
    else
                $(idDiv).innerHTML = "";

}

function replaceText (str)
{
    var reg=new RegExp("é", "g");
    str = str.replace(reg, "e"); 
    
    var reg=new RegExp("è", "g");
    str = str.replace(reg, "e");
        
    var reg=new RegExp("ê", "g");
    str = str.replace(reg, "e");
        
    var reg=new RegExp("ë", "g");
    str = str.replace(reg, "e");
    
    var reg=new RegExp("à", "g");
    str = str.replace(reg, "a");
    
        
    var reg=new RegExp("î", "g");
    str = str.replace(reg, "i");
    
    
        var reg=new RegExp("ï", "g");
    str = str.replace(reg, "i");
    
    
                 
    var reg=new RegExp("ù", "g");
    str = str.replace(reg, "u");
           
    var reg=new RegExp("ü", "g");
    str = str.replace(reg, "u");
    
    
        var reg=new RegExp("û", "g");
    str = str.replace(reg, "u");
    
    
    
    
    var reg=new RegExp("ö", "g");
    str = str.replace(reg, "o");
           
    var reg=new RegExp("ô", "g");
    str = str.replace(reg, "o");
    
    
    return str;
}

function modifPhotoTitle(id, title, baseURL)
{
    $('titre_'+id).className='';
    titre = $('titre_'+id).innerHTML;
    $('titre_'+id).innerHTML="<input style='height:14px;' id='titreModif_"+id+"' value='"+titre+"' type=text /><img style='cursor:pointer;' onclick='GetInputVal("+id+",\""+baseURL+"\");$(\"btnModifTitle"+id+"\").style.visibility=\"visible\";' src='"+baseURL+"img/FR/btnValider.gif'><img onclick='CancelTitre("+id+",\""+titre+"\");$(\"btnModifTitle"+id+"\").style.visibility=\"visible\";' style='cursor:pointer;' src='"+baseURL+"img/FR/btnAnnuler.gif'>";

}

function modifPhotoComment(id, comment, baseURL)
{
    com = $('comment_'+id).innerHTML;
    $('comment_'+id).innerHTML="<input style='height:14px;' id='commentModif_"+id+"' value='"+com+"' type=text /><img style='cursor:pointer;' onclick='GetInputVal2("+id+",\""+baseURL+"\");$(\"btnModifComment"+id+"\").style.visibility=\"visible\";' src='"+baseURL+"img/FR/btnValider.gif'><img onclick='CancelComment("+id+",\""+com+"\");$(\"btnModifComment"+id+"\").style.visibility=\"visible\";' style='cursor:pointer;' src='"+baseURL+"img/FR/btnAnnuler.gif'>";
    $("comment_"+id).addClassName("borderTitrePhotoCarnet");
}

function searchForPlace(placeControlId, placesControlId, baseURL, triptipType, geonameid)
{
    strSearch = $(placeControlId).getValue();
    new Ajax.Updater(placesControlId,baseURL+"webservices/searchForPlace.ws.php?strSearch="+strSearch+"&triptipType="+triptipType+"&geonameid="+geonameid);
}
function searchForVille(villeControlId, villesControlId, baseURL)
{
    strSearch = $(villeControlId).getValue();
    new Ajax.Request(baseURL+"webservices/searchForVille.ws.php?strSearch="+strSearch, 
    { 
        method: 'get', 
        onSuccess: function(transport) 
        {
 //           $(villesControlId).innerHTML=decodeURI(transport.responseText);
            var str=(transport.responseText);
            //str = str.replace(/'/g,"\\'");
alert(str);
            $(villesControlId).innerHTML=str;
        }
    }
   );
}
function GetInputVal(id,baseURL)
{
    var returnVal;
    returnVal = $F("titreModif_"+id);
    $("titre_"+id).innerHTML="";
    if(returnVal=="")
    {
        $(id+'_title_warning').style.visibility='visible';
        $("titre_"+id).className='contentTitrePhotoCarnet';
    }
    else
        $("titre_"+id).className='contentTextPhotoCarnet';
    
    new Ajax.Updater("titre_"+id,baseURL+"webservices/modifTitlePhoto.ws.php?idPhoto="+id+"&title="+returnVal);
}

function GetInputVal2(id,baseURL)
{
    var returnVal;
    returnVal = $F("commentModif_"+id);
    if(returnVal == "")
        $("comment_"+id).removeClassName("borderTitrePhotoCarnet");
    new Ajax.Updater("comment_"+id,baseURL+"webservices/modifTitlePhoto.ws.php?idPhoto="+id+"&comment="+returnVal);
}

function CancelTitre(id,titre)
{
    $('titre_'+id).innerHTML= titre;
    if(titre == "")
    {
        $(id+'_title_warning').style.visibility='visible';
        $('titre_'+id).className='contentTitrePhotoCarnet';
    }
    else
        $('titre_'+id).className='contentTextPhotoCarnet';
}
function checkValidStatus(pref, id)
{
alert($("titreModif_"+id).value);

}
function CancelComment(id,comment)
{
    if(comment == "")
        $("comment_"+id).removeClassName("borderTitrePhotoCarnet");
    $('comment_'+id).innerHTML= comment;
}

function point_it(event, baseURL, type){
    var image="";
    var id_div="";
    var buff_div="";
    var image_value=-1;
    var infobulleId = "";
    
    if(type==0)
    {   
        id_div="id_note";
        image="bonhomme";
        buff_div="buff_note";
        var arrayVal=noteArray;
        infobulleId = "infoNote"; 
    }
    else
    {   
        id_div="id_fouchette";
        image="euro";
        buff_div="buff_fourchette";
        var arrayVal=prixArray;
        infobulleId = "infoPrix";
    }
    pos_x = event.offsetX?(event.offsetX):event.pageX-$(id_div).offsetLeft;
    pos_y = event.offsetY?(event.offsetY):event.pageY-$(id_div).offsetTop;
/*    if(pos_x<15)
        image_value=0;
    else if(pos_x>=15 && pos_x <32)
        image_value=1;
    else if(pos_x>=32 && pos_x <50)
        image_value=2;
    else if(pos_x>=50 && pos_x <67)
        image_value=3;
    else if(pos_x>=67 && pos_x <85)
        image_value=4;
    else if(pos_x>=85)
        image_value=5;*/
    if(pos_x<15)
        image_value=0;
    else if(pos_x>=15 && pos_x <32)
        image_value=1;
    else if(pos_x>=32 && pos_x <50)
        image_value=2;
    else if(pos_x>=50 && pos_x <67)
        image_value=3;
    else if(pos_x>=67 && pos_x <85)
        image_value=4;
    else if(pos_x>=85)
        image_value=5;
/*    document.getElementById("cross").style.left = (pos_x-1) ;
    document.getElementById("cross").style.top = (pos_y-15) ;
    document.getElementById("cross").style.visibility = "visible" ;
    document.pointform.form_x.value = pos_x;
    document.pointform.form_y.value = pos_y;*/
    if(type==1 && image_value==0)
    {
        $(id_div).src=baseURL+"img/"+image+"0_1.gif";
        $(id_div).alt=arrayVal[image_value];
    }
    else
    {
        $(id_div).src=baseURL+"img/"+image+image_value+".gif";
        $(id_div).alt=arrayVal[image_value];
    }
    
    $(infobulleId).innerHTML = arrayVal[image_value];
    $(infobulleId).style.display = "block";
    
    $(buff_div).value=image_value;
}

function updateCalendar(yearInputDep,monthInputDep,dayInputDep,yearInputRet,monthInputRet,dayInputRet,way)
{
	inputYearDep = yearInputDep.options[yearInputDep.selectedIndex].value;
	inputMonthDep = monthInputDep.options[monthInputDep.selectedIndex].value/1;
	inputDayDep = dayInputDep.options[dayInputDep.selectedIndex].value/1;

	inputYearRet = yearInputRet.options[yearInputRet.selectedIndex].value;
	inputMonthRet = monthInputRet.options[monthInputRet.selectedIndex].value/1;
	inputDayRet = dayInputRet.options[dayInputRet.selectedIndex].value/1;
    dateDepart = inputYearDep*10000 + inputMonthDep*100 + inputDayDep;
    dateRetour = inputYearRet*10000 + inputMonthRet*100 + inputDayRet;
    var returnDateToYearModif="";
    var returnDateToMonthModif="";
    var returnDateToDayModif="";

    if(way==0)
    {
	    inputYear = yearInputDep.options[yearInputDep.selectedIndex].value;
	    inputMonth = monthInputDep.options[monthInputDep.selectedIndex].value/1;
	    inputDay = dayInputDep.options[dayInputDep.selectedIndex].value/1;
        if(dateDepart>dateRetour)
        {
            returnDateToYearModif = yearInputRet;
            returnDateToMonthModif = monthInputRet;
            returnDateToDayModif = dayInputRet;
        }

    }
    else
    {
	    inputYear = yearInputRet.options[yearInputRet.selectedIndex].value;
	    inputMonth = monthInputRet.options[monthInputRet.selectedIndex].value/1;
	    inputDay = dayInputRet.options[dayInputRet.selectedIndex].value/1;
        if(dateDepart>dateRetour)
        {
            returnDateToYearModif = yearInputDep;
            returnDateToMonthModif = monthInputDep;
            returnDateToDayModif = dayInputDep;
        }
    }
    if(returnDateToYearModif != "")
    {
	    for(var no=0;no<returnDateToYearModif.options.length;no++){
		    if(returnDateToYearModif.options[no].value==inputYear){
			    returnDateToYearModif.selectedIndex=no;
			    break;
		    }
	    }
	    for(var no=0;no<returnDateToMonthModif.options.length;no++){
		    if(returnDateToMonthModif.options[no].value==inputMonth){
			    returnDateToMonthModif.selectedIndex=no;
			    break;
		    }
	    }
	    for(var no=0;no<returnDateToDayModif.options.length;no++){
		    if(returnDateToDayModif.options[no].value==inputDay){
			    returnDateToDayModif.selectedIndex=no;
			    break;
		    }
	    }
    }

}

function getTripTipWS (idTextBox, idDiv, wsUrl, baseURL, ttType, geonameid)
{
    //counter[idTextBox] = Math.random ();
    $(idDiv).className="";
    var strSearch = $(idTextBox).getValue();
        //alert (counter[idTextBox]); 
    var strSearch1 = replaceText (strSearch);
    $(idDiv).innerHTML = "<img src="+baseURL+"img/loader.gif>";
    if (strSearch1.length > 0)
    {  
      //alert (encodeURI(strSearch));
     // alert(strSearch1);
      new Ajax.Request(wsUrl, {
      method: 'get',
      parameters: {'triptip': strSearch1, 'ttType': ttType, 'geonameid' : geonameid},
            
      onSuccess: function(transport) {
      var a = transport.request.parameters['triptip'];
   
      var j = 0;
         var xml = transport.responseXML;
/*         var x = xml.getElementsByTagName('triptip').item(0);*/
         var x = xml.getElementsByTagName('id');
         //alert(x);
       //alert (counter[idTextBox]); 
        if (a == replaceText ($(idTextBox).getValue()))
        {
            $(idDiv).innerHTML = "";
            if(x.length>1)
            {    
                $(idDiv).className="FGdestinationDivTT";    
            }
            else
            {
                $(idDiv).className=""; 
            }
            //y;
            var text;
            var id;
            for (i=0;i<x.length;i++)
            { 

                    id = x[i].firstChild.nodeValue;
                    text = (xml.getElementsByTagName('name')[i].firstChild.nodeValue).replace(/'/g,"\\'");
                    text = text.replace ("\\", "");
                    $(idDiv).innerHTML += "<span id="+id+" class='pointer destinationChoice' onclick=\"getTripTipInfoWS("+id+", '"+baseURL+"');$('"+idDiv+"').innerHTML = '';$('"+idDiv+"').className='';\">"+text+"</span>";
                    $(idDiv).innerHTML += "<br />";

              } 
        }
        else
        {
                $(idDiv).innerHTML += x[1].firstChild.nodeValue.Replace ("random--", "");        
        }
     
         
      }
      
    });

    }
    else
                $(idDiv).innerHTML = "";

}

function getTripTipInfoWS (idTriptip, baseURL)
{
    //counter[idTextBox] = Math.random ();
       //alert (counter[idTextBox]); 

 
      //alert (encodeURI(strSearch));
     // alert(strSearch1);
    new Ajax.Request(baseURL+"webservices/triptip_info_search.ws.php", {
            method: 'get',
            parameters: {'idTriptip': idTriptip},
            onSuccess: function(transport) {

                var j = 0;
                var xml = transport.responseXML;
                var x = xml.getElementsByTagName('name');
                var text;
                var y;
                for (i=0;i<x.length;i++)
                { 
                    text = (xml.getElementsByTagName('name')[i].firstChild.nodeValue).replace(/'/g,"\\'");
                    text = text.replace ("\\", "");
                
                    $('nom_lieu').value = text;
                    $('adresse_lieu').value = (xml.getElementsByTagName('adresse')[i].firstChild.nodeValue).replace(/'/g,"\\'");
                    $('instruction_lieu').value = (xml.getElementsByTagName('instructions')[i].firstChild.nodeValue).replace(/'/g,"\\'");
                    $('tag_lieu').value = (xml.getElementsByTagName('tags')[i].firstChild.nodeValue).replace(/'/g,"\\'");
                }
            }
        }
    );
}
function setValueDiv(idDiv, value)
{
    alert(idDiv);
    $(idDiv).value=value;
}

function GetValueElem(idElem)
{
    return $(idElem).getValue();
}
function formatHotTag(tag)
{
    alert(tag);
}

function toperCeMembre(baseURL, idFrom, idTo, idPhoto)
{
    if(idPhoto=='')
        idPhoto=0;
    /*alert(idFrom);
alert(idTo);*/
    new Ajax.Request(baseURL+"webservices/topage.ws.php?idFrom="+idFrom+"&idTo="+idTo+"&idPhoto="+idPhoto, 
    { 
        method: 'get', 
        onSuccess: function(transport) 
        {
 //           $(villesControlId).innerHTML=decodeURI(transport.responseText);
            var str=(transport.responseText);
            //str = str.replace(/'/g,"\\'");
            if(str.length>0)
            {    
                /*$(o).value = t.slice(0, begTag) + str + t.slice(endTag, t.length);
                setSelection(o, s, s+str.length-tag.length);*/
                
                $('topage_message').innerHTML=str;
                topagePopup.show();
}
            //o.value=new_o;
//alert(new_o);

            //$(villesControlId).innerHTML=str;
        }
    }
   );
}