// JavaScript Document

var exp_emailtxt = /^[_0-9a-z\-\.]+$/;
var exp_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var exp_num = /^\d+$/;
var exp_phone = /^[\d]{6,}$/;
var exp_addr = /^(.){6,}$/;
//var exp_pwd = /^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/;
var exp_pwd = /^[_0-9a-zA-Z\-\.@#$%\^&\+=]{6,}$/;

$(document).ready(
	function() {
	    $('div.popup div.foot a.cancel').click(
	        function() {
				var popobj = $(this).parent('div').parent('div');
	            closePopup('#' + popobj[0].id);
	            
	            return false;
	        }
	    );
	    
	    $('#info-templ #ctempl ul.cats a').click(
        function() {
	            $('#ctempl ul.cats a').removeClass('active');
	            $(this).addClass('active');

	            $('#ctempl ul.templs').empty();

	            var phpstring = 'action=get-mtemplates&type=info&id_cat=' + $(this).attr('rel');

	            $.ajax({
	                type: "POST",
	                url: "aj_dialer.php",
	                dataType: 'json',
	                data: phpstring,
	                //Evento di riuscita
	                success: function(ret) {
	                    /*if (ret.msg_type == 'ok') {

	                    } else {

	                    }*/
	                    $('#ctempl ul.templs').append(ret.msg);
	                }
	            });

	            return false;
	        }
	    );
	    $('#info-templ #ctempl a.close').click(
	    	function() {
	            $('#overlay').hide();
	        	$('#ctempl').hide();

	        	return false;
	        }
	    );
	    $('#info-templ a.tmgallery').click(
	        function() {
				$('#overlay').show();
	        	$('#ctempl').show();

	        	return false;
	        }
	    );
    }
);

function getScreenSize() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    /*window.alert( 'Width = ' + myWidth );
    window.alert( 'Height = ' + myHeight );*/

    ret = new Object();
    ret.w = myWidth;
    ret.h = myHeight;

    return ret;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function check_empty(arr_inputs) {
    var notfilled = 0;
    var debug_msg = '';
    for (i=0; i<arr_inputs.length; i++ ) {
        if ($(arr_inputs[i]).val() == '') {
            notfilled++;
            debug_msg += arr_inputs[i].name + ':' + $(arr_inputs[i]).val() + ' ';
        }
    }

    if (debug_msg != '') {
        //alert(debug_msg);
    }

    return notfilled;
}

function callAjax(phpstring, callback) {
    $.ajax({
        type: "POST",
        url: "aj_dialer.php",
        dataType: 'json',
        data: phpstring,
        //Evento di riuscita
        success: callback
    });
}

function status_checkemail() {
    var input_list = $("#askdata :input");
    var rmsg = '';

    var notfilled = check_empty(input_list);
    if (notfilled == 0) {
        if ($("#askdata input[name='privacy']:checked").length == 0) {
            rmsg += 'normativa sulla <b>Privacy</b> non accettata<br />';
		}

		if (rmsg == '') {
	        var qstring = '';
	        for (i=0; i<input_list.length; i++) {
	            qstring += input_list[i].name + '=' + input_list[i].value + '&';
	        }
	        var phpstring = 'action=sendcontmsg&' + qstring;

	        $.ajax({
	            type: "POST",
	            url: "aj_dialer.php",
	            dataType: 'json',
	            data: phpstring,
	            //Evento di riuscita
	            success: function(ret) {
	                eval(ret);
	                if (ret.msg_type == 'ok') {
	                    $('#askdata')[0].reset();
	                    openPopupMsg('','messaggio inviato con successo');
	                } else {
	                    openPopupMsg('Errore',ret.msg);
	                }
	            }
	        });
        } else {
            openPopupMsg('Errore',rmsg);
		}
    } else {
        openPopupMsg('Errore','Tutti i campi sono obbligatori e vanno compilati');
        return false;
    }
}

function openPopup(popid) {
    $('#overlay').show();
    $(popid).css('opacity',0);
    $(popid).show();

/*
    var top_y = $().scrollTop();
    if (topval > 0) {
        top_y = topval;
    }
    if (leftval > 0) {
        $(popid).css('left',leftval + 'px');
    }

    $(popid).css('top',(top_y+10) + 'px');
*/
    var scrsize = getScreenSize();
    var cent_y = scrsize.h /2 + $(window).scrollTop() - $(popid)[0].offsetHeight /2 - $('#center')[0].offsetTop;
    //alert($(popid)[0].offsetHeight);
    var cent_x = $('#col-cr')[0].offsetWidth /2 - $(popid)[0].offsetWidth /2;
    //alert($(popid)[0].offsetWidth);
    $(popid).css({'top':cent_y + 'px','left':cent_x + 'px'});

    $(popid).fadeTo(
    	1000 *0.5,
		1,
     	null,
		jQuery.easing.def = 'easeOutQuint'
	);
}


function closePopup(popid) {
    $('#overlay').hide();
    $(popid).hide();
}

function populateCities(selectid,provid,selval) {
    var phpstring = 'action=getcities&provid=' + provid;
    $.ajax({
        type: "POST",
        url: "aj_dialer.php",
        dataType: 'json',
        data: phpstring,
        //Evento di riuscita
        success: function(ret) {
            var options = '';
    		for (var i = 0; i < ret.length; i++) {
    			options += '<option value="' + ret[i].code + '">' + ret[i].name + '</option>';
    		}
    		$(selectid).html(options);
            if (selval != '') {
                $(selectid).val(selval);
            }
        }
    });
}
