function changeType() {
    showdiv();
    var country = dijit.byId('cbCountry').attr('value');
    var VarUrl = rootUrl + 'countries/json/type/country/' + country;
    var type_store = new dojo.data.ItemFileReadStore( {
        url : VarUrl
    });
    dijit.byId('cbType').store = type_store;
    dijit.byId('cbType').store.fetch();
    dijit.byId('cbType').attr('value', 0);
    dijit.byId('cbType').setDisplayedValue('2. Select an Option', false);
    changeTour();
    hidediv();
}

function changeTour() {
    showdiv();
    var country = dijit.byId('cbCountry').attr('value');
    var type = parseInt(dijit.byId('cbType').attr('value'));
    switch (type) {
        case 2:
            var VarUrl = rootUrl + 'countries/json/bytheme/country/' + country;
            break;
        case 1:
            var VarUrl = rootUrl + 'countries/json/bydestinity/country/'
            + country;
            break;
        case 3:
            var VarUrl = rootUrl + 'countries/json/bypromotion/country/'
            + country;
            break;
        case 4:
            var VarUrl = rootUrl + 'countries/json/byprice/country/'
            + country;
            break;
        case 5:
            var VarUrl = rootUrl + 'countries/json/bytrekking/country/'
            + country;
            break;
        case 6:
            var VarUrl = rootUrl + 'countries/json/byfolklore/country/'
            + country;
            break;
        default :
            var VarUrl = rootUrl + 'countries/json/bydestinity/country/'
            + '0';
    }

    var type_store = new dojo.data.ItemFileReadStore( {
        url : VarUrl
    });
    dijit.byId('cbTour').store = type_store;
    dijit.byId('cbTour').store.fetch();
    dijit.byId('cbTour').attr('value',0);
    //dijit.byId('cbTour').setDisplayedValue('ALL', false);
    hidediv();
}

function SubmitSearch() {
    var type = parseInt(dijit.byId('cbType').attr('value'));
    var destiny = parseInt(dijit.byId('cbTour').attr('value'));
    var country = parseInt(dijit.byId('cbCountry').attr('value'));
    switch (type) {

        case 1:
            url = buildUrl(destiny,country);
            window.location.href = url;
            break;
        case 2:
            url = buildThemesUrl(destiny,country);
            window.location.href = url;
            break;
        case 3:
            url = buildUrl_2(country,destiny);
            window.location.href = url;
            break;
        case 4:
            url = buildUrl_3(country,destiny);
            window.location.href = url;
            break
        case 5:
            url = buildUrl_4(country,destiny);
            window.location.href = url;
            break
        case 6:
            url = buildUrl_5(country,destiny);
            window.location.href = url;
            break
        default :
            url  = rootUrl+getCountry(country);
            window.location.href = url;
            break;
    }
}

function getCountry(country)
{   
    var retorno = null;
    dojo.xhrPost( {
        form: dojo.byId('frmCat'),
        url : rootdomain+'countries/index/getcountry/coui_id/'+country,
        handleAs : 'text',
        preventCache : true,
        sync : true,
        timeout : 50000,
        load : function(response, ioArgs) {
            retorno = response;
        },
        error : function(response, error) {
            console.debug("An error occurred with error: ", error);
        }
    });
    return retorno;
}

function suscribe() {
    if (!dijit.byId('suscribe_mail').isValid()) {
        dijit.byId('suscribe_mail').focus();
        return false;
    }
    /* var correo = dijit.byId('suscribe_mail').attr('value');
    if(!dijit.byId('suscribe_mail').validate())
    {
        return false;
    }else{
        document.frmCat.action = rootUrl + 'suscribe';
        document.frmCat.submit();
    }*/
    document.frmCat.action = rootUrl + 'suscribe';
    document.frmCat.submit();
    return true;

}

function quote(url) {
    document.frmCat.action = url;
    document.frmCat.submit();
}

function SubmitSearchAll() {
    dojo.byId('all').value = 'todos';
    document.frmCat.submit();
}

function navigation(url) {
    document.frmCat.action = url;
    document.frmCat.submit();
}

function hidediv() {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById('hideshow').style.visibility = 'hidden';
    } else {
        if (document.layers) { // Netscape 4
            document.hideshow.visibility = 'hidden';
        } else { // IE 4
            document.all.hideshow.style.visibility = 'hidden';
        }
    }
}

function showdiv() {
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById('hideshow').style.visibility = 'visible';
    } else {
        if (document.layers) { // Netscape 4
            document.hideshow.visibility = 'visible';
        } else { // IE 4
            document.all.hideshow.style.visibility = 'visible';
        }
    }
}


function buildUrl(desi,couid) {
    var retorno = null;
    try {
        x = dojo
        .xhrPost( {
            // The following URL must match that used to test the server.
            url : rootUrl + 'tours/json/getdestinyurl/destiny/' + desi+'/country/'+couid,
            headers : {
                "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8"
            },
            handleAs : 'text',
            preventCache : true,
            sync : true,
            timeout : 50000,
            load : function(responseObject, ioArgs) {
                console.log('Read JSON Data ... do something',
                    responseObject);
                retorno = responseObject;
            },
            error : function(type, error) {
                console.debug(
                    "An error occurred: ",
                    error);
            }
        // More properties for xhrGet...
        });

    } catch (err) {
        console.debug("An error occurred: ", err);
    }
    //window.location.href = rootdomain+retorno;
    return retorno;
}


function buildThemesUrl(desi,couid) {
    var retorno = null;
    try {
        x = dojo
        .xhrPost( {
            // The following URL must match that used to test the server.
            url : rootUrl + 'tours/json/themesurl/promo/' + desi+'/country/'+couid,
            headers : {
                "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8"
            },
            handleAs : 'text',
            preventCache : true,
            sync : true,
            timeout : 50000,
            load : function(responseObject, ioArgs) {
                console.log('Read JSON Data ... do something',
                    responseObject);
                retorno = responseObject;
            },
            error : function(type, error) {
                console.debug(
                    "An error occurred: ",
                    error);
            }
        // More properties for xhrGet...
        });

    } catch (err) {
        console.debug("An error occurred: ", err);
    }
    //window.location.href = rootdomain+retorno;
    return retorno;
}

function buildUrl_2(cou,pro) {
    var retorno = null;
    try {
        x = dojo
        .xhrPost( {
            // The following URL must match that used to test the server.
            url : rootUrl + 'tours/json/getpromourl/country/' + cou+'/promo/'+pro,
            headers : {
                "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8"
            },
            handleAs : 'text',
            preventCache : true,
            sync : true,
            timeout : 50000,
            load : function(responseObject, ioArgs) {
                console.log('Read JSON Data ... do something',
                    responseObject);
                retorno = responseObject;
            },
            error : function(type, error) {
                console.debug(
                    "An error occurred: ",
                    error);
            }
        // More properties for xhrGet...
        });

    } catch (err) {
        console.debug("An error occurred: ", err);
    }
    //window.location.href = retorno;
    return retorno;
}


function buildUrl_3(cou,pro) {
    var retorno = null;
    try {
        x = dojo
        .xhrPost( {
            // The following URL must match that used to test the server.
            url : rootUrl + 'tours/json/getpriceurl/country/' + cou+'/price/'+pro,
            headers : {
                "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8"
            },
            handleAs : 'text',
            preventCache : true,
            sync : true,
            timeout : 50000,
            load : function(responseObject, ioArgs) {
                console.log('Read JSON Data ... do something',
                    responseObject);
                retorno = responseObject;
            },
            error : function(type, error) {
                console.debug(
                    "An error occurred: ",
                    error);
            }
        // More properties for xhrGet...
        });

    } catch (err) {
        console.debug("An error occurred: ", err);
    }
    //window.location.href = retorno;
    return retorno;
}

function buildUrl_4(cou,trek) {
    var retorno = null;
    try {
        x = dojo
        .xhrPost( {
            // The following URL must match that used to test the server.
            url : rootUrl + 'tours/json/gettrekking/country/' + cou+'/trek/'+trek,
            headers : {
                "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8"
            },
            handleAs : 'text',
            preventCache : true,
            sync : true,
            timeout : 50000,
            load : function(responseObject, ioArgs) {
                console.log('Read JSON Data ... do something',
                    responseObject);
                retorno = responseObject;
            },
            error : function(type, error) {
                console.debug(
                    "An error occurred: ",
                    error);
            }
        // More properties for xhrGet...
        });

    } catch (err) {
        console.debug("An error occurred: ", err);
    }
    //window.location.href = retorno;
    return retorno;
}

function buildUrl_5(cou,folk) {
    var retorno = null;
    try {
        x = dojo
        .xhrPost( {
            // The following URL must match that used to test the server.
            url : rootUrl + 'tours/json/getfolklore/country/' + cou+'/folk/'+folk,
            headers : {
                "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8"
            },
            handleAs : 'text',
            preventCache : true,
            sync : true,
            timeout : 50000,
            load : function(responseObject, ioArgs) {
                console.log('Read JSON Data ... do something',
                    responseObject);
                retorno = responseObject;
            },
            error : function(type, error) {
                console.debug(
                    "An error occurred: ",
                    error);
            }
        // More properties for xhrGet...
        });

    } catch (err) {
        console.debug("An error occurred: ", err);
    }
    //window.location.href = retorno;
    return retorno;
}


function sendLinkPartner(fromForm,fragmentURL) {
    dojo.xhrPost( {
        form: dojo.byId(fromForm),
        url: fragmentURL,
        handleAs: "text",
        load: function(response, ioArgs){
            window.location.href = rootdomain+'link-partner/thanks';
            return response;
        },
        error: function(response, ioArgs){
            alert("An error occurred, with response: " + response);
            return response;
        }
    });
}

function loadFriend(url)
{
    dojo.parser.parse(dojo.byId('sendToFriend'));
    dijit.byId('sendToFriend').attr('href',url);
    dijit.byId('sendToFriend').show();
}

function sendFriend() {
    if(!dijit.byId('my_name').isValid()) {
        return false
    }
    if(!dijit.byId('my_email').isValid()) {
        return false
    }
    if(!dijit.byId('your_name').isValid()) {
        return false
    }
    if(!dijit.byId('your_email').isValid()) {
        return false
    }
    dojo.xhrPost( {
        form: dojo.byId('friendForm'),
        url: rootdomain+'tours/index/sendmailfriend',
        handleAs: "text",
        load: function(response, ioArgs){
            window.location.href = rootdomain+'/send-friend/thanks';
            return response;
        },
        error: function(response, ioArgs){
            // alert("An error occurred, with response: " + response);
            return response;
        }
    });
}

function sendContact()
{
    if (!dijit.byId('frmCat').validate()) {
        return false;
    }
    document.frmCat.action = rootUrl + 'contact-us/thanks';
    document.frmCat.submit();
    return true;
}

function showBox(box)
{
    dijit.byId(box).show();
}

function addBlogResponse(url)
{
    document.frmCat.action = url;
    document.frmCat.submit();
}

function sendContest()
{
    dijit.byId('suscribe_mail').attr('dojoType', '');
    dijit.byId('suscribe_mail').attr('required', false);
    
    if (!dijit.byId('apfv_name').isValid()) {
        dijit.byId('apfv_name').focus();
        return false;
    }
    
    var ctxtbox = 5;
    
    for($i=0; $i<=ctxtbox; $i++){
        var idname = 'email'+$i;
        if(!dijit.byId(idname).isValid()){
            dijit.byId(idname).focus();
            return false;
        }else{
            for($x=0; $x<=ctxtbox; $x++){
                var bidname = 'email'+$x;
                if($i != $x){
                    if(dijit.byId(idname).value == dijit.byId(bidname).value){
                        var searchWaitMessageString = 'Sorry, duplicate email entered';
                        dijit.byId(bidname).displayMessage(searchWaitMessageString);
                        dijit.byId(bidname).focus();
                        return false;
                    }
                }
            }
        }
    }
    
    //Validate Email Diferent
    
    dijit.byId('frmCat').attr('action', 'thanks-contest');
    dijit.byId('frmCat').submit();
}

function validateEmailDuplicate()
{
    dijit.byId('suscribe_mail').attr('dojoType', '');
    dijit.byId('suscribe_mail').attr('required', false);
    
    var ctxtbox = 5;
    for($i=0; $i<=ctxtbox; $i++){
        var idname = 'email'+$i;
        if(!dijit.byId(idname).isValid()){
            dijit.byId(idname).focus();
            return false;
        }else{
            for($x=0; $x<=ctxtbox; $x++){
                var bidname = 'email'+$x;
                if($i != $x){
                    if(dijit.byId(idname).value == dijit.byId(bidname).value){
                        var searchWaitMessageString = 'Sorry, duplicate email entered';
                        dijit.byId(bidname).displayMessage(searchWaitMessageString);
                        return false;
                    }
                }
            }
        }
    }
}