﻿/*!
* Copyright 2010, EIDOS Srl
* http://www.gruppoeidos.it
*
* Date: 2010-10-25
*/

/* READY */
$(document).ready(function () {

 });

/*### UTILITY #####################################*/

function SetEnter(id_input, id_cmd) {
    $('#' + id_input).bind('keypress', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) { //Enter keycode
            $('#' + id_cmd).click();
            return false;
        }
    });
}

function SetEnterSrc() {
    $('#srcbox').bind('keypress', function (e) {
        var notxt = 'Inserire una chiave di ricerca...';
        var shorttxt = 'Digitare almeno 2 caratteri...';
        if ($('#srcbox').val() == notxt || $('#srcbox').val() == shorttxt) $('#srcbox').val('');
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) { //Enter keycode
            $('#srccmd').click();
            return false;
        }
    });
}


function getFilters() {
    var myLocation = document.location.toString();
    if (myLocation.match('#')) { // the URL contains an anchor           
        if (myLocation.indexOf('?') > 0) myLocation = myLocation.replace('#', '&');
        else myLocation = myLocation.replace('#', '?');
    }
    var myParams = myLocation.slice(myLocation.indexOf('?'));
    myParams = myParams.replace('?', '&');
    return myParams;
}

function getParam(paramName, myParams) {
    // var myDebug = 'getParam(' + paramName + ' , ' + myParams + '\n';        
    var myResult = '';
    var myIdx = myParams.indexOf('&' + paramName + '=');
    // myDebug += 'myIdx: ' + myIdx + '\n';
    if (myIdx > -1) {
        var myLastParam = myParams.substring(myIdx + 1);
        //   myDebug += 'myLastParam: ' + myLastParam + '\n';
        var myIdxEnd = myLastParam.indexOf('&');
        //   myDebug += 'myIdxEnd: ' + myIdxEnd + '\n';
        if (myIdxEnd > -1) myLastParam = myLastParam.substring(0, myIdxEnd);

        myResult = myLastParam.replace(paramName + '=', '');
    }
    // myDebug += 'myResult: ' + myResult + '\n';
    // alert(myDebug);   
    return myResult;
}


function Loading(IsToOpen) {
    if (IsToOpen == 1)
        $("#loading").fadeIn(500);
    else
        $("#loading").fadeOut(1000);

} //end Loading

/*### END  UTILITY #####################################*/

function lo() {

    if ($('#panelLogin').is(':hidden') == true) {
        $('#panelLogin').slideDown('slow');
    }
    else { $('#panelLogin').hide(); }
}

function l(ook) {
    var usr = $('#usr').val();
    var pwd = $('#pwd').val();
    var pers = $('#pers').val();
    $('#loginResult').html('');
    Loading(1);

    var xhr = $.ajax({
        type: 'POST',
        url: 'post.aspx',
        data: { f: 'l', u: usr, p: pwd, c: pers },
        success: function (data, status) {
            if (data == '') {
                window.location.href = 'home.aspx';
                return;
            }
            $('#loginResult').html(data);
            Loading(0);
        },
        error: function (data, status) {
            $('#loginResult').html(data);
            Loading(0);
        }
    });
}


/*##############################*/
/*###    SEARCH FUNCTIONS    ###*/
/*##############################*/

function SetSearch() {
    var myParams = getFilters();
    var t = getParam('t', myParams);
    //$("select[name=foo] option[text=bar]").attr("selected", true);
    $('#src_tp option[value=' + t + ']').attr('selected', true);
    $('#src_tp').change(function () {
        Loading(1);
        var xhr = $.ajax({
            type: 'POST',
            url: 'post.aspx',
            data: { f: 'f', t: $('#src_tp option:selected').val() },
            success: function (data, status) {
                $('#srcfilter').html(data);
                Loading(0);
            },
            error: function (data, status) {
                $('#srcfilter').html(data);
                Loading(0);
            }
        });
    })
    .trigger('change');

}  //end SetSearch.


function DoSearch() {
    Loading(1);

    var xhr = $.ajax({
        type: 'POST',
        url: 'post.aspx',
        data: { f: 's',
            t: $('#src_tp option:selected').val(),
            mar: $('#src_mar option:selected').val(),
            mod: $('#src_mod option:selected').val(),
            col: $('#src_col option:selected').val(),
            prz: $('#src_prz option:selected').val(),
            km: $('#src_km option:selected').val(),
            anno: $('#src_anno option:selected').val(),
            dep: $('#src_dep option:selected').val(),
            stato: $('#src_stato option:selected').val(),
            casa: $('#src_casa option:selected').val(),
            targa: $('#src_targa').val()
        },
        success: function (data, status) {
            $('#srcresult').html(data);
            popupinit();
            Loading(0);
        },
        error: function (data, status) {
            $('#srcresult').html(data);
            Loading(0);
        }
    });
}


function imp(ti, id) {

    if (ti == 'p') $('#lbltipo').html('PRENOTAZIONE');
    $('#txtnominativo').val('');
    $("#dialogconfirmpost").dialog({
        width: 500,
        resizable: false,
        modal: true,
        buttons: {
            "OK": function () {
                // Publish Comment
                Loading(1);
                var xhr = $.ajax({
                    type: 'POST',
                    url: 'post.aspx',
                    data: { f: 'im', ti: ti, id: id, n: $('#txtnominativo').val() },
                    success: function (data, status) {
                        Loading(0);
                        $('#cmdSrc').click();
                    },
                    error: function (data, status) {
                        $('#BoxResult').html(data);
                        Loading(0);
                    }
                });
                $('#txtnominativo').val('');
                $(this).dialog("close");
                // end.

            },
            Cancel: function () {
                confirm_Ok = false;
                $(this).dialog("close");
            }
        }
    });
}


/*##############################*/



/*!
* EIDOS PopUpBox JavaScript Library
* http://lab.gruppoeidos.it/
*
* Copyright 2010, EIDOS Srl
* http://www.gruppoeidos.it
*
* v1.0.2 Date: 2010-11-26
*/

$(document).ready(function () {
    popupinit();
});

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars(myURL) {
    var vars = [], hash;
    var hashes = myURL.slice(myURL.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function popupinit() {

    /* Hide div popup on click and/or keypress */
    $('.ui-widget-overlay').live('click', function () { $('#popupbox').dialog('close'); });

    /* Change click on dialog "popupboxclose" class object */
    $('.popupboxclose').click(function (e) {
        e.preventDefault();
        parent.top.$("#popupbox").dialog("close");
    });

    /* Change click on a class "popupbox" */
    $('.popupbox').click(function (e) {
        e.preventDefault();
        var iframeSource = $(this).attr("href");
        setPopUp(iframeSource, $(this).attr("rel"));
    });
}

function popupbox(iframeSource) {
    setPopUp(iframeSource);
}

function setPopUp(iframeSource, frameTitle) {
    var wdim = 50;
    var hdim = 60;
    var UrlVarS = getUrlVars(iframeSource);

    var popw = UrlVarS['popw'];
    if (isNaN(popw)) popw = 700;

    var poph = UrlVarS['poph'];
    if (isNaN(poph)) poph = 530;

    var w = Number(popw);
    var h = Number(poph);

    $('#popupbox').append('<div id="popupboxwait" style="display:none; margin-top:100px;"><img alt="loading..." src="images/popup_loading.gif"/></div>'
        + '<iframe id="popupboxfrm" frameborder="0" style="display:none; border:none;"></iframe>');

    //var pophpad = Math.Floor(poph / 2)
    //$('#popupboxwait').attr('margin-top', pophpad);
    $('#popupboxwait').show();

    $('#popupboxfrm').hide();
    $('#popupboxfrm').attr('src', iframeSource).width(w - wdim).height(h - hdim);

    $('#popupboxfrm').load(function () {
        $('#popupboxfrm').show();
        $('#popupboxwait').hide();
    });
    if ($('#popupboxfrm').complete) $('#popupboxfrm').trigger("load");

    $('#popupbox').css('display', 'none');
    $('#popupbox').dialog({
        width: w,
        height: h,
        resizable: false,
        modal: true,
        title: frameTitle
        //resizeStop: function (event, ui) {
        //    $('#popupbox iframe').width($('#popupbox').width() - wdim)
        //    .height($('#popupbox').width() - hdim);
        //}
    });
}


		function ChkMail() {
		
		    var nome = $('#MainContentPlace_Nome').val();
		    var email = $('#MainContentPlace_Email').val();
		    var richiesta = $('#MainContentPlace_Richiesta').val();
		    var reg_exp = /^([a-zA-Z0-9_.,àòè éùì])+$/;
		    var reg_num = /^([0-9_ -])+$/;
		    var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		    var IsToSend = 1;
		
		    $('#MainContentPlace_Nome').removeClass('nocompiled');
		    $('#MainContentPlace_Email').removeClass('nocompiled');
		    $('#MainContentPlace_Richiesta').removeClass('nocompiled');
		    $('#MainContentPlace_Privacy').removeClass('nocompiled2');
		
		
		    //Effettua il controllo sul campi    		
		
		    if ((nome == "") || (nome == "undefined")) { IsToSend = 0; $('#MainContentPlace_Nome').addClass('nocompiled'); }
		    else if (!reg_exp.test(nome)) { IsToSend = 0; $('#MainContentPlace_Nome').addClass('nocompiled'); }
		
		    if ((email == "") || (email == "undefined")) { IsToSend = 0; $('#MainContentPlace_Email').addClass('nocompiled'); }
		    else if (!email_reg_exp.test(email)) { IsToSend = 0; $('#MainContentPlace_Email').addClass('nocompiled'); }
		
		    if ((richiesta == "") || (richiesta == "undefined")) { IsToSend = 0; $('#MainContentPlace_Richiesta').addClass('nocompiled'); }

		    if ($('#MainContentPlace_Privacy').attr('checked') == false) { IsToSend = 0; $('#MainContentPlace_Privacy').addClass('nocompiled2'); }

		    if (IsToSend == 1) return true;
		    else return false;
		    
		}


