﻿$(document).ready(function() {
    var LoggedIn = $("#LoggedInHolder").html();

    //ajax request to check a users status
    //    if (LoggedIn == "True") {
    //        $.ajax({
    //            url: rootpath + 'login/checkcookie.aspx'
    //        });
    //    }


//    $('.advance_earth').click(function() {
//        LogAction(window.location.href.toString(), null, null, "click on the promo bar");
//    });


    var screenwidth = 0;
    if (document.documentElement) {
        screenwidth = document.documentElement.offsetWidth;
    } else if (window.innerWidth && window.innerHeight) {
        screenwidth = window.innerWidth;
    }
    
    if (screenwidth < 1329) {
        var scroll = (1329 - screenwidth) / 2;
        $('html,body').animate({ scrollLeft: scroll }, 700);
    }

    //-- comment box --//
    $('#comment, a.comment-modal').click(function() {
        $('#comment-submit').show();
        $('#comment-thank').hide();
        $('#comment-box').fadeIn();
        $('html').animate({ scrollTop: 0 }, 200);
        $('#transparent-overlay').show().css('z-index', '3025');
    });
    $('#comment-close').click(function() {
        $('#comment-box').fadeOut();
        $('#transparent-overlay').hide().css('z-index', '3000');
    });
    $('#transparent-overlay').click(function() {
        $('#comment-box').fadeOut();
        $('#transparent-overlay').hide().css('z-index', '3000');
    });

    //-- towersubmit box --//
    $('#towersubmit, a.towersubmit-modal').click(function() {
        $('#towersubmit-submit').show();
        $('#towersubmit-thank').hide();
        $('#towersubmit-box').fadeIn();
        $('html').animate({ scrollTop: 0 }, 200);
        $('#transparent-overlay').show().css('z-index', '3025');
    });
    $('#towersubmit-close').click(function() {
        $('#towersubmit-box').fadeOut();
        $('#transparent-overlay').hide().css('z-index', '3000');
    });
    $('#transparent-overlay').click(function() {
        $('#towersubmit-box').fadeOut();
        $('#transparent-overlay').hide().css('z-index', '3000');
    });

    $(commentjson.email).keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $(commentjson.submitbtn).click();
            return false;
        } else {
            return true;
        }
    });

    $(commentjson.submitbtn).click(function() {
        var $comments = $(commentjson.comments);
        var $email = $(commentjson.email);
        var $relatedto = $(commentjson.relatedto);

        if ($comments.val() == '') {
            $comments.css('border', '1px solid red');
            return false;
        }

        if ($email.val() != 'Your E-mail' && !Page_ClientValidate('CommentValidation')) {
            $(commentjson.validator).show();
        } else {
            $(commentjson.validator).hide();
            $.ajax({
                type: 'POST', contentType: 'application/json; charset=utf-8', dataType: 'json',
                url: rootpath + 'TowerCoService.asmx/SubmitComment',
                data: '{"Comments":"' + $comments.val() + '", "RelatedTo":"' + $relatedto.find(':checked').val() + '", "Email":"' + $email.val() + '"}',
                success: function(msg) {
                    msg = (msg.d == undefined || msg.d == null) ? msg : msg.d;
                    //if (msg)
                    //	do something
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    //alert('error: ' + XMLHttpRequest.statusText);
                }
            });

            $comments.val('');
            $email.val('');
            $relatedto.find('input:first').attr('checked', 'checked');
            $('#comment-submit').hide();
            $('#comment-thank').show();
        }

        return false;
    });
    $(commentjson.email).Watermark('Your E-mail', '#444');

    //-- login box --//
    //  $('ul.loginbox h4.login a').removeAttr('href').click(function(){
    //    $(this).hide();
    //    $('div.login-container').show();
    //  });
    //  $('div.login-container input.uname-cover, div.login-container input.pswd-cover').focus(function(){
    //    $(this).prev().css('display','inline').val('').focus();
    //    $(this).hide();
    //  });
    //  $('div.login-container input.uname:not(.uname-cover), div.login-container input.pswd:not(.pswd-cover)').blur(function(){
    //    if (jQuery.trim($(this).val())==''){
    //      $(this).val('');
    //      $(this).hide();
    //      $(this).next().css('display', 'inline');
    //    }
    //  });

    //-- PCIA Sweeps modal --//
    $('#PCIASweepsEntry').parent().npModal({
        closeClass: 'img.close',
        enableOverlayClose: false,
        enableEscape: false,
        autoPosition: false
    });
    $('#hide-pcia-sweeps').click(function() {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            url: rootpath + 'TowerCoService.asmx/HidePCIASweepsCookie',
            data: '{}'
        });
        $('#PCIASweepsEntry').parent().npModalDestroy();
    });
    $('span.pcia-sweepstakes-cb input').click(function() {
        var id = $(this).attr('id');
        $('span.pcia-sweepstakes-cb input:not(#' + id + ')').removeAttr('checked');
    });
});

function validateSweepsCB() {
  return $('.pcia-sweepstakes-cb input:checkbox').attr('checked') == 'checked';
}

//-- This method is used to parse the returned server JSON to make sure it is safe to use --//
String.parseJSON = (function(s) {
  var m = {
    '\b': '\\b',
    '\t': '\\t',
    '\n': '\\n',
    '\f': '\\f',
    '\r': '\\r',
    '"': '\\"',
    '\\': '\\\\'
  };
  s.parseJSON = function(filter) {
    try {
      if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) {
        var j = eval('(' + this + ')');
        if (typeof filter === 'function') {
          function walk(k, v) {
            if (v && typeof v === 'object') {
              for (var i in v) {
                if (v.hasOwnProperty(i)) {
                  v[i] = walk(i, v[i]);
                }
              }
            }
            return filter(k, v);
          }
          j = walk('', j);
        }
        return j;
      }
    } catch (e) {
    }
    //throw new SyntaxError("parseJSON: filter failed");
    throw new SyntaxError("Trouble loading data please contact website@towerco.com");
  };
}
)(String.prototype);
function SyntaxError(e) {
  alert(e);
}

//function LogAction(Fromlocation, towerid, query, notes) {
//    $.ajax({
//        type: 'POST',
//        url: rootpath + 'TowerCoService.asmx/LogActions',
//        data: ({ "location": Fromlocation, "towerid": towerid, "query": query, "notes": notes }),
//        success: function(msg) {
//            msg = (msg.d == undefined || msg.d == null) ? msg : msg.d;
//            alert("success");
//            //if (msg)
//            //	do something
//        },
//        error: function(XMLHttpRequest, textStatus, errorThrown) {
//            alert(textStatus + ' ' + XMLHttpRequest.status);
//        }
//    });
//}

function LogAction(Fromlocation, towerid, query, notes) {
    $.ajax({
        type: 'POST',
        url: rootpath + 'TowerCoService.asmx/LogActions',
        data: ({ "location": Fromlocation, "towerid": towerid, "query": query, "notes": notes }),
        success: function(msg) {
            msg = (msg.d == undefined || msg.d == null) ? msg : msg.d;
            alert("success");
            //if (msg)
            //	do something
        }

    });
}

