﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/$/$-1.4.4-vsdoc.js" />

String.prototype.startsWith = function (str)
{ return (this.match("^" + str) == str); };

String.prototype.endsWith = function (str)
{ return (this.match(str + "$") == str); };

String.prototype.trim = function() {
   return (this.replace( /^[\s\xA0]+/ , "").replace( /[\s\xA0]+$/ , ""));
};

function Validazione() {
    $('label.required').children('strong').remove();
    $('label.required').append('<strong>&nbsp;*&nbsp;</strong>');

    $.validator.addMethod("selectRequired", function (value, element) {
       var $element = $(element);
       if (value == 0)
          return false;
       return true;
    }, $.validator.messages.required);

    $.validator.addMethod("alphanumericnospace", function (value, element) {
        if ((value.indexOf(" ") < 0 && value != "")) {
            var test = /^[a-zA-Z0-9._-]+$/i.test(value); // testaccent(value);
            return test;
        } else {
            return false;
        }
    }, "Lettere o numeri no spazi o caratteri accentati");

    $.validator.addMethod("customurl", function (value, element) {
        return this.optional(element) || /^(\/[\w-]+\.)+[a-z]{2,6}$/i.test(value);
    }, "url");

    $.validator.addMethod("emailRequired", function (value, element) {
        validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

        var email = $(element).attr("value");
        if (email.search(validRegExp) == -1)
            return false;
        return true;
    }, $.validator.messages.email);

    $.validator.addMethod("equalRequired", function (value, element) {
        var pass = $("#utentecarrello_password").attr("value");
        var verifica = $(element).attr("value");
        if (verifica == '') return false;
        if (pass != verifica)
            return false;
        return true;
    }, $.validator.messages.equalTo);

    $.validator.addMethod("equalchangeRequired", function (value, element) {
        var pass = $("#nuova").attr("value");
        var verifica = $(element).attr("value");
        if (verifica == '') return false;
        if (pass != verifica)
            return false;
        return true;
    }, $.validator.messages.required);

    $.validator.addMethod("imageRequired", function (value, element) {
        return value > 0;
    }, $.validator.messages.required);
    $.validator.addMethod("PrezzoRequired", function (value, element) {
        return value > 0;
    }, $.validator.messages.required);

    $.validator.addMethod("usernameUnique", function (value, element) {
        var result = $.ajax({
            async: false,
            data: { username: value },
            type: "POST",
            url: '/ECommerceModule/EsisteUtente.ashx',
            dataType: 'text'
        });
        if (result.responseText == 'True')
            return false;
        else
            return true;
    }, $.validator.messages.required);

    jQuery.validator.addMethod("notEqualTo", function (value, element, param) {
        return this.optional(element) || value != $(param).val();
    }, $.validator.messages.required);

    jQuery.validator.addMethod("nonzero", function (value, element) {
        return this.optional(element) || value >0;
    }, $.validator.messages.required);

//    $.validator.addMethod("usernameValido", function (value, element) {
//        var result = $.ajax({
//            async: false,
//            data: { username: value },
//            type: "POST",
//            url: '/ECommerceModule/UtenteValido.ashx',
//            dataType: 'text'
//        });
//        if (result.responseText == 'nonesite')
//            return false;
//        else {
//            $(".bottoni_recuperapassword").hide();
//            $("#formrecupera2").show();
//            $("#domandasegreta").html(result.responseText);
//            $("#risposta").addClass("required");
//            return true;
//        }
//    }, $.validator.messages.required);

    $.validator.addMethod("elementoUnique", function (value, element, params) {
        var result = $.ajax({
            async: false,
            data: { codice: value, field: params },
            type: "POST",
            url: '/admin/Site/IsUnique.ashx',
            dataType: 'text'
        });
        if (result.responseText == 1)
            return false;
        else
            return true;
    }, "Valore già esistente. Deve essere unico");
}

function Popup(action, containerpost,caller,lato) {
    var container = "#container_modifiche";
    var posizionebottone = 0;
    var posizionebottoneleft = 0;
    if (caller != null) {
         posizionebottone = $("#" + caller).position().top;
         posizionebottoneleft = $("#" + caller).position().left;
    }
    var maskHeight = jQuery(document).height();
    var maskWidth = jQuery(window).width();

    var winH = jQuery(window).height();
    var winW = jQuery(window).width();

    var containerHeight = jQuery(container).height();
    var containerWidth = jQuery(container).width();
    // calculate the values for center alignment
    var dialogTop = (winH / 2) - (containerHeight / 2);
    var dialogLeft = (winW / 3) - (containerWidth / 2);
    // assign values to the overlay and dialog box
    $(container).load(action, function (data) {
    });
    jQuery(container).show();
    jQuery('#dialog-overlay').css({ height: maskHeight, width: winW, opacity: 0.5 }).show();
    if (lato == "sinistra") {
        jQuery(container).css({ top: posizionebottone - containerHeight, left: posizionebottoneleft - containerWidth }).animate({ opacity: '+=1' }, 300, function () {
        });
    }
    if (lato == "destra") {
        jQuery(container).css({ top: posizionebottone - containerHeight, left: posizionebottoneleft  }).animate({ opacity: '+=1' }, 300, function () {
        });
    }
    if (lato == "") {
        jQuery(container).css({ top: dialogTop, left: dialogLeft }).animate({ opacity: '+=1' }, 300, function () {
        });
    }

    var topdiv = $(container).position().top;
    $(window).scrollTop(topdiv);
}

function ClosePopup() {
    $('#container_modifiche').html("");
    $('#container_modifiche,#dialog-overlay').animate({ opacity: '-=0.5' }, 300, function () {
        $('#dialog-overlay, #container_modifiche').hide();
    });
}

function InviaEmail(action, container, message) {
    var result = $.ajax({
        async: false,
        type: "POST",
        url: action,
        dataType: 'text'
    });
    $("#" + container).html(result.responseText);
}

$(function () {
    $("#adminpage").click(function () {
        var _page = $(this).attr("rel");
        $(this).attr("target", "_blank");
        $(this).attr("href", "/Admin/index.ashx?returnurl="+_page);
    });
  });

  function PopDialog(message, tipo, form) {
     // css nel file Validazione.css
     $('#dialog-overlay').remove();
     $(".dialogmessage").remove();
     var container = "<div class=\"dialogmessage\"><div class=\"errore\">{errore}</div><div class=\"button\"><input type=\"button\"  value=\"ok\"></div></div>";
     var dialogoverlay = "<div id='dialog-overlay'></div>";
     var container = container.replace(/{errore}/g, message);
     $(container).insertAfter($("body"));
     $(dialogoverlay).insertAfter($("body"));

     var windowWidth = jQuery(window).width();
     var windowHeight = jQuery(window).height();
     var popupHeight = $(".dialogmessage").height();
     var popupWidth = $(".dialogmessage").width();
     var top = windowHeight / 2 - popupHeight / 2;
     var left = windowWidth / 2 - popupWidth / 2;

     $('#dialog-overlay').css({ height: windowHeight, width: windowWidth }).show();
     $('.dialogmessage').css({ top: top, left: left, width: popupWidth }).fadeIn("fast", function () {
     });

     if (tipo == 0) {
        $(".dialogmessage .button input").click(function () {
           $('#dialog-overlay').remove();
           $(".dialogmessage").remove();
        });
     }
     if (tipo == 1) {
        $(".dialogmessage .button input").click(function () {
           $("#formcontattaci .annulla input").click();
           $('#dialog-overlay').remove();
           $(".dialogmessage").remove();
        });
     }
  }
