/*
    Author : re/max québec
 */

var targetFavorite = "";
var compareList = [];

function hideMenuItems(){
    document.getElementById("conseilsPlanifLnk").innerHTML = "";
    
}

function showMenuItems(label){
    document.getElementById("conseilsPlanifLnk").innerHTML = label;
}

function myRemaxHover(){
    alert('hover');
}

function myRemaxOut(){
    alert('out');
}

function addToFavorites(idinscr, codeChambre, searchIndex) {
    targetFavorite = codeChambre + "" + idinscr;
    
    var idElementToChange = codeChambre + "" + idinscr;
    
    if (searchIndex != null && searchIndex != "") {
        idElementToChange = idElementToChange + "-" + searchIndex;
    }

    http('GET', contextPath + '/' + lang + '/addToFavorites.rmx?codechambre=' + codeChambre + "&idinscr=" + idinscr + "&statusIndex=" + searchIndex, toggleFavoritesBtn);
    var src = document.getElementById(idElementToChange).style.backgroundImage;
    if(src.indexOf("_fr_gm") != -1){
        document.getElementById(idElementToChange).style.backgroundImage = "url(" + contextPath + "/images/results/cardroundbgselected_fr_gm.jpg)";
    }else if(src.indexOf("_en_gm") != -1){
        document.getElementById(idElementToChange).style.backgroundImage = "url(" + contextPath + "/images/results/cardroundbgselected_en_gm.jpg)";
    }else{
        document.getElementById(idElementToChange).style.backgroundImage = "url(" + contextPath + "/images/results/cardroundbgselected.jpg)";
    }
}

function removeFromFavorites(idinscr, codeChambre, searchIndex) {

    var msg = "";
    if(lang == "en"){
        msg = "Do you want to remove this listing from your list of favorites ?";
    }else{
        msg = "Voulez-vous retirer cette inscription de votre liste de favoris ?";
    }
    if(confirm(msg)){
        targetFavorite = codeChambre + "" + idinscr;
        
        var idElementToChange = codeChambre + "" + idinscr;
    
        if (searchIndex != null && searchIndex != "") {
            idElementToChange = idElementToChange + "-" + searchIndex;
        }
        
        http('GET', contextPath + '/' + lang + '/removeFromFavorites.rmx?codechambre=' + codeChambre + "&idinscr=" + idinscr + "&statusIndex=" + searchIndex, toggleFavoritesBtn);
        var src = document.getElementById(idElementToChange).style.backgroundImage;
        if(src.indexOf("_fr_gm") != -1){
            document.getElementById(idElementToChange).style.backgroundImage = "url(" + contextPath + "/images/results/cardroundbg_fr_gm.jpg)";
        }else if(src.indexOf("_en_gm") != -1){
            document.getElementById(idElementToChange).style.backgroundImage = "url(" + contextPath + "/images/results/cardroundbg_en_gm.jpg)";
        }else{
            document.getElementById(idElementToChange).style.backgroundImage = "url(" + contextPath + "/images/results/cardroundbg.jpg)";
        }

    }
}

function removeFromFavoritesAndDom(idinscr, codeChambre, searchIndex){
    
    if(removeFromFavorites(idinscr, codeChambre, searchIndex)) {        
        
        hideElement(codeChambre + idinscr + "-" + searchIndex);
        if(!(contains(compareList, codeChambre + "" + idinscr))){
            compareList.push(codeChambre + "" + idinscr);
        }
        toggleCompareList(codeChambre, idinscr, null);

        var value = document.getElementById('nbResultats').innerHTML;
        document.getElementById('nbResultats').innerHTML = value - 1;

        if ((value - 1) < 15) {
            document.getElementById('pagerContent').innerHTML = "";
        }
        
    }
    
}

function toggleFavoritesBtn(obj){
    var imgTag = document.getElementById("inscriptionThumb-" + targetFavorite);
    var firstIndex = imgTag.src.lastIndexOf("/") + 1;
    var imgname = imgTag.src.substring(firstIndex);
    document.getElementById("favorites-" + targetFavorite).innerHTML = obj;
}

function toggleCompareList(codeChambre, idinscr, elm){
    if(contains(compareList, codeChambre + "" + idinscr)){
        compareList = remove(compareList, codeChambre + "" + idinscr);
    }else{
        compareList.push(codeChambre + "" + idinscr);
    }
    if(compareList.length > 1){
        showComparaisonLink();
    }else if(compareList.length < 2){
        hideComparaisonLink();
    }
    
    if(elm != null){
        var imgElement = elm.getElementsByTagName("img")[0];
        var src = imgElement.src;
        if(src.indexOf("uncheck.gif") != -1){
            src = src.replace(/uncheck.gif/,"check.gif")
        }else{
            src = src.replace(/check.gif/,"uncheck.gif")
        }
        imgElement.src = src;
    }
    
}

function showComparisonsPage(){
    var location = contextPath + "/" + lang + "/showComparisons.rmx?ids=";
    var ids = "";
    for(var i = 0 ; i < compareList.length ; i++){
        ids += compareList[i] + "-";
    }
    window.location = location + ids.substring(0, ids.length -1);
}

function showComparaisonLink(){
    showElement("comparaisonsLink");
}

function hideComparaisonLink(){
    hideElement("comparaisonsLink");
}

function displayChangeTitleForm(titleId, inputId){
    hideElement(titleId);
    showElement(inputId);
}

function hideChangeTitleForm(titleId, inputId){
    showElement(titleId);
    hideElement(inputId);
}

function saveSearchTitle(titleId, inputId, txtId, id){
    var title = document.getElementById(txtId).value;
    if(title != '' && title.length < 36){
        http('GET', contextPath + '/' + lang + '/saveSearchTitle.rmx?title=' + title + "&id=" + id, noReturn);
        document.getElementById(titleId).innerHTML = title;
        hideChangeTitleForm(titleId, inputId);
    }
}

function noReturn(){}

function displayAutresTypes(){
    try{showElement("autresTypes");}catch(err){}
}

function hideAutresTypes(){
    try{hideElement("autresTypes");}catch(err){}
}

function refreshSortingOrder(sel, currentPage) {
    var page = removeParameterFromURL(currentPage);
    window.location.href = contextPath + page + "?orderby=" + sel[sel['selectedIndex']].value;
}

function refreshPrePage(sel, currentPage){
    var page = removeParameterFromURL(currentPage);
    window.location.href = contextPath + page + "?perpage=" + sel[sel['selectedIndex']].value;
}

function removeParameterFromURL(url) {
    var index = url.indexOf("?");
    var page = "";
    
    if (index == -1) {
        page = url;
    }
    else {
        page =  url.substr(0, index);
    }
    
    if (page.indexOf("fr") != -1) {
        page = page.replace("fr", "en");
    }
    else {
        page = page.replace("en", "fr");
    }

    return page;
}

function showSaveSearchForm(){
    showElement("saveSearchForm");
    place(document.getElementById("saveSearchForm"), xm, ym);
}

var myRemaxBundle;

function showUpdateSearchForm(jsResources, idsearch){
    
    // on doit proposer a l'usager s'il veut ?craser la recherche qu'il a load?
    // auparavant!
    myRemaxBundle = new Bundle(jsResources);
    
    var answer = confirm(unescape(myRemaxBundle.getString("myremax.update.search.question")));
    
    if (answer) {
        updateSearch(idsearch);
    }    
}

function updateSearch(idsearch) {

    http('GET', contextPath + '/' + lang + '/updateSearchBean.rmx?id=' + idsearch, updateSearchConfirmation);

}

function updateSearchConfirmation(obj) {
    if (obj == "200") {
        alert(unescape(myRemaxBundle.getString("myremax.update.search.success")));
        document.getElementById("saveSearchLinkContent").innerHTML = "<span style=\"color: #2E619A; font-size: 10px; font-family: Arial, Helvetica, sans-serif;\">" + unescape(myRemaxBundle.getString("myremax.update.search.saved")) + "</span>";
    }
    else {
        alert(unescape(myRemaxBundle.getString("myremax.update.search.failed")));
    }
}

function saveSearch(){
    var title = document.getElementById("txtSaveSearch").value;
    if(title != "" && title.length < 36){
        http('GET', contextPath + '/' + lang + '/saveSearchBean.rmx?title=' + title, saveSearchConfirmation);
    }else{
        if (lang == "en") {
            alert("The title for saving search criteria can not exceed 36 characters.");
        }else{
            alert("Le titre de la recherche sauvegard\u00e9e ne peut exc\u00e9der 36 caract\u00e8res.");
        }
    }
}

function saveSearchConfirmation(obj){
    if(obj == "200"){
        if (lang == "en") {
            document.getElementById("saveSearchLinkContent").innerHTML = "<span style=\"color: #2E619A; font-size: 10px; font-family: Arial, Helvetica, sans-serif;\">Search criteria saved</span>";
        } else {
            document.getElementById("saveSearchLinkContent").innerHTML = "<span style=\"color: #2E619A; font-size: 10px; font-family: Arial, Helvetica, sans-serif;\">Sauvegard&eacute;e</span>";
        }        
        cancelSaveSearch();
    }else if(obj == "410"){
        
    }
}

function cancelSaveSearch(){
    document.getElementById("txtSaveSearch").value = "";
    hideElement("saveSearchForm");
}

function hideAllStaticAgentContent(){
    for(var i = 1 ; i < 11 ; i ++){
        try{
            hideElement("txtElement-" + i);
        }catch(err){}
    }
}

function showForgotPasswordForm(){
    showElement("forgotPasswordForm");
    place(document.getElementById("forgotPasswordForm"), (xm - 38), (ym + 12));
}

function validateMyRemaxLogin() {
    var username = document.getElementById("myRemaxUsername");
    var password = document.getElementById("myRemaxPassword");
    
    var path = contextPath + '/' + lang + '/login.rmx?user=' + username + '&pass=' + password
    
    http('POST', path, showMyRemaxLoggedInForm);
}

function showMyRemaxLoggedInForm(obj) {
    /*
    var d = document.getElementById("MyRemaxSmall");
    var oldAction = document.getElementById("completeLoginForm");
    d.removeChild(oldAction);
    
    var newdiv = document.createElement('div');
    newdiv.innerHTML = obj;
    d.appendChild(newdiv);
    //document.getElementById('completeLoginForm').innerHTML = "";
    */
    
    
}

function hideForgotPasswordForm(){
    hideElement("forgotPasswordForm");
}

function resetPasswordByEmail(){
    var email = document.getElementById("forgotPasswordInput").value;
    // /*/recoverPassword
    http('GET', contextPath + '/' + lang + '/recoverPassword.rmx?email=' + email, passwordResetConfirmation);
}

function passwordResetConfirmation(obj){
    
    document.getElementById("forgotPasswordInput").value = "";
    hideForgotPasswordForm();
    var msg = obj.split('|');
    
    if (msg[0] == "true" && msg[1] == "fr") {
        alert("Votre mot de passe \340 \351t\351 envoy\351 \340 votre adresse courriel.");
    }
    else if (msg[0] == "true" && msg[1] == "en") {
        alert("Your password has been sent to your email adress."); 
    }
    else if (msg[0] == "false" && msg[1] == "fr") {
        alert("L'adresse de courriel n'est pas pr\351sente dans notre base de donn\351es."); 
    }
    else if (msg[0] == "false" && msg[1] == "en") {
        alert("This email adress is not present in our database.");
    }

}

var hasFocusUsername = false;
var hasFocusPassword = false;
var mouseOverActive = false;

function mouseOverMyRemax() {
    try{hideElement('sorttingOptions')}catch(err){};
    try{showElement("completeLoginForm");}catch(err){};
    try{showElement("loggedInContent");}catch(err){};
    mouseOverActive = true;
    
}

function mouseOutMyRemax() {
    try{showElement('sorttingOptions')}catch(err){};
    try{
        
        if (!hasFocusUsername && !hasFocusPassword) {
            hideElement("completeLoginForm");
        }
            
    }catch(err){};
    
    try{hideElement("loggedInContent");}catch(err){};
    mouseOverActive = false;
}

function setFocusUsername(state) {

    var extendVisible = document.getElementById("extendRemaxProperty").value;
    hasFocusUsername = state;
    
    if (!state) {
        document.getElementById("remaxUsernameInput").style.background = "#FFFFFF";
    }
    else {
        document.getElementById("remaxUsernameInput").style.background = "#eff1ce";
    }
    
    if (!state && !mouseOverActive && !extendVisible) {

        try{showElement('sorttingOptions')}catch(err){};
        try{

            if (!hasFocusUsername && !hasFocusPassword) {
                hideElement("completeLoginForm");
            }

        }catch(err){};

        try{hideElement("loggedInContent");}catch(err){};
    }
}


function setFocusPassword(state) {
    
    var extendVisible = document.getElementById("extendRemaxProperty").value; 
    hasFocusPassword = state;
    
    
    if (!state) {
        document.getElementById("remaxPasswordInput").style.background = "#FFFFFF";
    }
    else {
        document.getElementById("remaxPasswordInput").style.background = "#eff1ce";
    }
    
    if (!state && !mouseOverActive && !extendVisible) {

        try{showElement('sorttingOptions')}catch(err){};
        try{

            if (!hasFocusUsername && !hasFocusPassword) {
                hideElement("completeLoginForm");
            }

        }catch(err){};

        try{hideElement("loggedInContent");}catch(err){};
    }
}




