//json
var JSON;if(!JSON){JSON={}}(function(){function str(a,b){var c,d,e,f,g=gap,h,i=b[a];if(i&&typeof i==="object"&&typeof i.toJSON==="function"){i=i.toJSON(a)}if(typeof rep==="function"){i=rep.call(b,a,i)}switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i){return"null"}gap+=indent;h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1){h[c]=str(c,i)||"null"}e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]";gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1){if(typeof rep[c]==="string"){d=rep[c];e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}else{for(d in i){if(Object.prototype.hasOwnProperty.call(i,d)){e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}";gap=g;return e}}function quote(a){escapable.lastIndex=0;return escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function f(a){return a<10?"0"+a:a}"use strict";if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;if(typeof JSON.stringify!=="function"){JSON.stringify=function(a,b,c){var d;gap="";indent="";if(typeof c==="number"){for(d=0;d<c;d+=1){indent+=" "}}else if(typeof c==="string"){indent=c}rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":a})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object"){for(c in e){if(Object.prototype.hasOwnProperty.call(e,c)){d=walk(e,c);if(d!==undefined){e[c]=d}else{delete e[c]}}}}return reviver.call(a,b,e)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})()

window.schepnet = {
  webmethodUrl: '/CMSPages/Custom/ClientAjaxMethods.aspx/',
  webmethod: function webmethod(method, data, callback) {
    //var for converting json to string
    var DTO = {
      requestJson: data
    };
    //options for ajax post
    var options = {
      error: function (msg) {
        alert('webmethod failed, response:\n' + msg.d);
      },
      type: "POST",
      url: window.schepnet.webmethodUrl + method,
      data: JSON.stringify(DTO),
      // array to string fixes it *
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (response) {
        if (typeof response == "object") {
          var data = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
          if (callback) callback(data);
        }
      }
    };
    return jQuery.ajax(options);
  },
  validateEmail: function validateEmail(email) {
    //var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
    return email.match("/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/")
  }
}

jQuery(document).ready(function ($) {
  if ($('.LoginButton').length) {
    $('.uitloggen').addClass('HighLighted');
  }

  if (!$('#breadcrumb span').length) {
    $('#breadcrumb a').html("vodafone points");
  }

  // block close
  $('.block_close').click(function (e) {
    e.preventDefault();
    $(this).parent().slideUp();
  });

  // news small click
  $('.news_small').click(function (e) {
    var url = $(this).find('a').attr('href');
    window.location = url;
  });

  $('.news_small').hover(function (e) {
    $(this).find('a').addClass("hover");
  }, function (e) {
    $(this).find('a').removeClass("hover");
  });

  // trainingen popup


  function openTraining() {
    var trainingElm = $(this).parents('.training'),
      SWFUrl = trainingElm.data('url'),
      SWFWidth = trainingElm.data('width'),
      SWFHeight = trainingElm.data('height'),
      TrainingTitle = trainingElm.data('title');
    $.colorbox({
      href: SWFUrl,
      innerWidth: SWFWidth,
      innerHeight: SWFHeight,
      title: TrainingTitle,
      iframe: true
    });
  }

  if ($('.training').length) {
    $('.training a').click(openTraining);
  }


  if ($('.register').length) {

    function updateForm(disabled) {
      if (disabled) {
        $('.RegisterButton').attr('disabled', 'disabled');
        $('.RegisterButton').css('opacity', .4);
        $('#codeValid').hide();
        $('.register_user').hide()
        $('#channelCode').css({
          width: 196
        });
      } else {
        $('.RegisterButton').removeAttr('disabled');
        $('.RegisterButton').css('opacity', 1);
        $('.register_user').show()
        $('#codeValid').css('display', 'inline-block');
        $('#channelCode').css({
          width: 170
        });
      }
    }

    function checkCode() {
      var code = $('#channelCode input').val();
      var channelID = $('#channels select').val();
      if (code.length == 4 && channelID > 0) {
        schepnet.webmethod('CheckChannelCode', {
          code: code,
          channelid: parseInt(channelID)
        }, function (response) {

          if (response.match) {
            updateForm(false);
          } else {
            updateForm(true);
          }
        })
      } else {
        updateForm(true);
      }
    }

    function populateShops() {

      var defaultValue = $('#winkelRow select option:eq(0)').detach();
      $('#winkelRow select').empty();
      schepnet.webmethod('GetShopsByChannel', {
        channelid: parseInt($('#channels select').val())
      }, function (response) {

        if (response.shops && response.shops.length) $('#winkelRow').show();
        else $('#winkelRow').hide();

        $(response.shops).each(function () {
          $('<option>', {
            value: this.shopid,
            text: this.name
          }).appendTo($('#winkelRow select'));
        })

      })

      defaultValue.appendTo($('#winkelRow select'));
    }


    schepnet.webmethodUrl = '/cmspages/VodafonePoints/ClientAjaxMethods.aspx/';

    var code = $('#channelCode input').val();
    var channelID = $('#channels select').val();
    if (code.length == 4 && channelID > 0) {
      checkCode();
    } else {
      updateForm(true);
    }

    if (channelID > 0) {
      populateShops();
    }

    $('#channels select').change(function () {

      updateForm(true);
      $('#channelCode input').val('');
      populateShops();
    })

    $('#channelCode input').keyup(checkCode);
  }
});
