

var ajaxLoginURL = '/myfish4/ajax/login';
var ajaxEcomLoginURL = '/myfish4/ajax/ecomLogin';
var ajaxGetDataURL = '/myfish4/ajax/getUserData';
var ajaxDoLoginURL = '/myfish4/ajax/doLogin';
var ajaxDoRegisterURL = '/myfish4/ajax/doRegister';
var ajaxSelectCVs = '/myfish4/ajax/selectcvs';
var checkForApplicationURL = '/applyOnline/checkForApplication';
var getIncompleteApplication = '/applyOnline/getIncompleteApplication';
var isPrefillData = false;
var isApplied = false;

$(document).ready(function() {
    initSecurityJS();
});

function initSecurityJS() {
    $(".requires_login").each(function(){
        $(this).attr('ref', $(this).attr('href'));
    });
    $(".requires_login").attr("href", "javascript:void(0);");
    $(".requires_login").click( function(evt) {
        if($("#AP").length==1 && $("#AP").css("visibility")=="visible") $("#AP").css("visibility", "hidden");
        checkLogin(evt, $(this).attr('ref'), $(this).attr('name'),  $(this).attr('url'));
    });

    $(".check_login").each(function(){
        $(this).attr('ref', $(this).attr('href'));
    });
    $(".check_login").attr("href", "javascript:void(0);");
    $(".check_login").click( function() {checkUserLogin($(this).attr('ref'), $(this).attr('name'));});

    if ($(".checkapplication").length > 0) {
        $(".checkapplication").each(function(){
            $(this).attr('ref', $(this).attr('href'));
        });
        $(".checkapplication").attr("href", "javascript:void(0);");
        $(".checkapplication").click( function() {
            if ($(this).hasClass("preview_mode")) return false;
            checkSearchResultsApplications($(this).attr('name'), $(this).attr('ref'));
        });
    }

   $(document).click(function(e) {
        var $clicked=$(e.target); // get the element clicked
        if( !( $clicked.is('.requires_login')) && $(".login_register_box").is(":visible") && !( $clicked.parents().is(".login_register_box")) && !( $clicked.is(".login_register_box")) && !($clicked.is(".change_jcaptcha_image"))) {
          $(".login_register_box").fadeOut(function(){
            if($("#AP").length==1 && $("#AP").css("visibility")=="hidden") $("#AP").css("visibility", "visible");
          });
        }
        if( !( $clicked.is('.save_alert_box')) && $(".save_alert_box").is(":visible") && !( $clicked.parents().is(".save_alert_box"))) {
            $(".save_alert_box").fadeOut();
        }
    });

    if (isPrefillData) {
        prefillUserData();
    }
    if (isApplied) {        
        checkForApplication();

    }
}

function refreshLoginButtons() {
  var topLinkHtml = '<a href="/myfish4/logout" title="log out">Logout</a>';
  var bottomLinkHtml = '<a href="/myfish4" title="myfish4">myfish4</a>';
  $('.login_button').parents('span').html(topLinkHtml);
  $('.login').html(bottomLinkHtml);
  $('.login').addClass('myfish4');
  $('.login').removeClass('login');
}

function prefillUserData() {
    $.ajax({
        type: 'GET',
        url: ajaxGetDataURL,
        success: function(data) {
            if (data != 'fail') {
                var userData = data.split(',');
                if ($('#firstname').length > 0) {
                    $('#firstname').val(userData[0]);
                }
                if ($('#apply_firstname').length > 0) {
                    $('#apply_firstname').val(userData[0]);
                }
                if ($('#surname').length > 0) {
                    $('#surname').val(userData[1]);
                }
                if ($('#apply_surname').length > 0) {
                    $('#apply_surname').val(userData[1]);
                }
                if ($('#from').length > 0) {
                    $('#from').val(userData[2]);
                }
            }
        }
    });
}



function saveApp(url, linkId) {
    var firstname, surname, adId, from, message;
    $('#' + linkId).hide();
    $('.waiting').show();
    $.ajax({
        type: 'GET',
        url: ajaxGetDataURL,
        success: function(data) {
            if (data != 'fail') {
                var userData = data.split(',');
                if ($('#apply_firstname').length > 0 && $('#apply_firstname').val() != '') {
                    firstname = $('#apply_firstname').val();
                } else {
                    firstname = userData[0];
                }
                if ($('#apply_surname').length > 0 && $('#apply_surname').val() != '') {
                    surname = $('#apply_surname').val();
                } else {
                    surname = userData[1];
                }
                if ($('#from').length > 0 && $('#from').val() != '') {
                    from = $('#from').val();
                } else {
                    from = userData[2];
                }
                adId = $('#adId').val();
                if ($('#message').length > 0) {
                    message = $('#message').val();
                }
                if ($('#apply_firstname').val() == '' || $('#apply_surname').val() == '' || $('#from').val() == '') {
                    prefillUserData();
                }

                $.ajax({
                    type: 'POST',
                    url: url,
                    data: "firstname=" + firstname + "&surname=" + surname + "&adId=" + adId + "&from=" + from + "&message=" + message,
                    success: function(data){
                        if (data != 'success') {
                            alert('There was a problem saving this application to your account. Please try again later.');
                            $('.waiting').hide();
                            $('#' + linkId).show();
                        }
                        else {
                            $('#' + linkId).parent("span").html("<span class=\"saved\">Your application was successfully saved.</span>");
                            $('.waiting').hide();
                            $('#' + linkId).show();
                        }
                    }
                });
            }
            else {
                alert('There was a problem saving this application to your account. Please try again later.');
                $('.waiting').hide();
                $('#' + linkId).show();
            }
        }
    });
}

function checkSearchResultsApplications(adId, url) {
    $.ajax({
        type: 'GET',
        url: checkForApplicationURL,
        data: 'adId='+adId,
        success: function(data) {
            if (data != 'none') {
                if (data == 'complete') {
                  if (confirm('You have already applied for this job. Do you want to continue?')) {
                      window.location.href = url;
                  }
                }
                else {
                  window.location.href = url;
                }
            }
            else {
                window.location.href = url;
            }
        }
    });
}

function checkForApplication() {
    $.ajax({
        type: 'GET',
        url: checkForApplicationURL,
        data: 'adId='+$('#adId').val(),
        success: function(data) {
          if (data != 'none') {
            if (data == 'incomplete') {
                $('.already_applied').html('You have already saved an application for this advert.');
                $.ajax({
                    type: 'GET',
                    url: getIncompleteApplication,
                    data: 'adId='+$('#adId').val(),
                    success: function(data) {
                      var xml = $.xmlDOM(data, function(error) {
                          alert('A parse error occurred! ' + error);
                      });
                      if ($(xml).find("incompleteApp").length > 0) {
                        if ($('#apply_firstname').length > 0 && $(xml).find("firstname").length > 0) {
                            $('#apply_firstname').val($(xml).find("firstname").text());
                        }
                        if ($('#apply_surname').length > 0 && $(xml).find("surname").length > 0) {
                            $('#apply_surname').val($(xml).find("surname").text());
                        }
                        if ($('#from').length > 0 && $(xml).find("from").length > 0) {
                            $('#from').val($(xml).find("from").text());
                        }
                        if ($('#message').length > 0 && $(xml).find("message").length > 0) {
                            $('#message').val($(xml).find("message").text());
                        }
                      }
                    }
                });
            }
            $('.already_applied').show();
          }
        }
    });
}

function submitCheck(evt) {
  if ($('#updateAlert').length > 0) {
      $('#updateAlert').submit();
  } else if ($('#carsupdateAlert').length > 0) {
      $('#carsupdateAlert').submit();
  } else {
      if ($(evt.target).attr('submittype') == 'continue') {
          $('#update').append('<input type=\"hidden\" id=\"continueToPayment\" name=\"continueToPayment\" value=\"true\" />');
      }  
      if($('input[name=\'industrySectorIds\']:checked').size() > 5) {
          document.getElementById('industryHeader').scrollIntoView();
      } else if (typeof searchForLocation=="function") {
          searchForLocation('#update', 'locationJobTown', true);
      } else {
          $('#update').submit();
      }
  }
}

function checkUserLogin(ref, type) {
    $.ajax({
        type: 'GET',
        url: ajaxLoginURL,
        success: function(data) {
            if (data != 'fail') {
                if (type == 'href') {
                    window.location.href = ref + '?authtoken=' + data;
                }
                else if (type == 'submit') {
                    if ($("#applynow").length > 0) {
                       $("#complete").val('true');
                       validateCv("applynow", true);
                    }
                    if ($("#cvbroadcast").length > 0) {
                        if (validateCv("cvbroadcast")) {
                            searchForLocation('#cvbroadcast', 'locationJobTown', true);
                        }
                    }
                }
                else if (type == 'saveapp_submit') {
                    $("#complete").val('false');
                    validateCv("applynow", true);
                }
            }
            else {
                if (type == 'href') {
                    window.location.href = ref
                }
                else if (type == 'submit') {
                    if ($("#cvbroadcast").length > 0) {
                        if (validateCv("cvbroadcast")) {
                            searchForLocation('#cvbroadcast', 'locationJobTown', true);
                        }
                    }
                    if ($("#applynow").length > 0) {
                       $("#complete").val('true');
                       validateCv("applynow", true);
                    }
                }
                else if (type == 'saveapp_submit') {
                   $("#complete").val('false');
                   validateCv("applynow", true);
                }
            }
        }
    });
}

function checkLogin(evt, ref, type, url) {
    var coord = getCoordinates(evt);
    $.ajax({
        type: 'GET',
        url: ajaxLoginURL,
        success: function(data) {
            if (data != 'fail') {
                if (type == 'href') {
                    window.location.href = ref
                }
                else if (type == 'savealert') {
                    var uri = url.substring(0, url.indexOf('?'));
                    var params = url.substring(url.indexOf('?') + 1, url.length);
                    saveAlert(params, uri, coord.x, coord.y);
                }
                else if (type == 'saveadvert') {
                    saveAdvert(ref, url, $(evt.target).attr('id'));
                }
                else if (type == 'saveapp') {
                    saveApp(ref, $(evt.target).attr('id'));
                }
                else if (type == 'chooseCV') {
                    selectCVs();
                }
                else {
                    submitCheck(evt);
                }
            }
            else {
                if (data == 'error_details') {
                    alert('We are experiencing some server problems. Please try again later');
                }
                else {
                    if ($(".login_register_box").length == 0 && accountURL != null && accountURL != '') {
                        createLoginBox(type, ref, url, coord, $(evt.target).attr('id'));
                    }
                    if (type == 'href') {
                        $(".login_register_box").css('left', coord.x - 370);
                        $(".login_register_box").css('top', coord.y);
                    }
                    else if (type == 'submit') {
                        $(".login_register_box").css('left', coord.x);
                        $(".login_register_box").css('top', coord.y - 340);
                    }
                    else if ($('.v7').length > 0 && type == 'chooseCV') {
                        $(".login_register_box").addClass('box_v7');
                    }
                    else {
                        $(".login_register_box").css('left', coord.x);
                        $(".login_register_box").css('top', coord.y);
                    }
                    if ($(evt.target).attr('box') == 'leftbox' || $(evt.target).parents('a').attr('box') == 'leftbox') {
                        $(".login_register_box").css('left', coord.x - 370);
                    }
                    if ($(evt.target).attr('box') == 'rightbox' || $(evt.target).parents('a').attr('box') == 'rightbox') {
                        $(".login_register_box").css('left', coord.x);
                    }
                    if ($(evt.target).attr('box') == 'topbox' || $(evt.target).parents('a').attr('box') == 'topbox') {
                        $(".login_register_box").css('top', (coord.y - 340) + 'px');
                    }

                    $(".login_register_box").fadeIn();
                }
            }
        }
    });
}

function loginUser(type, ref, url, x, y, linkId) {
    if ($('.login_box #username').val() == '' || $('.login_box #password').val() == '' || $('.login_box #imagekey').val() == '') {
        $('.login_box #login_error').html('Please fill all fields');
        $('.login_box #username, .login_box #password, .login_box #imagekey').focus(function() {
            $('.login_box #login_error').html('');
        });
    }
    else {
        $('.btn_login_small').hide();
        $('#forgotten_password').hide();
        $('.login_box_form .waiting').show();
        $.ajax({
            type: 'POST',
            url: ajaxDoLoginURL,
            data: 'username=' + $('.login_box #username').val() + '&password=' + $('.login_box #password').val() + '&imagekey=' + $('.login_box #imagekey').val(),
            success: function(data){
                if (data != 'error' && data != 'error_details' && data != 'error_captcha') {
                    if (type == 'href') {
                        window.location.href = ref + '?token=' + data;
                    }
                    else if (type == 'savealert') {
                        var uri = url.substring(0, url.indexOf('?'));
                        var params = url.substring(url.indexOf('?') + 1, url.length);
                        refreshLoginButtons();
                        if ($(".login_register_box").length > 0) {
                          $(".login_register_box").remove();
                        }
                        saveAlert(params, uri, x, y);
                        closeLoginBox();
                    }
                    else if (type == 'saveadvert') {
                        refreshLoginButtons();
                        if ($(".login_register_box").length > 0) {
                          $(".login_register_box").remove();
                        }
                        saveAdvert(ref, url, linkId);
                        closeLoginBox();
                    }
                    else if (type == 'saveapp') {
                        refreshLoginButtons();
                        if ($(".login_register_box").length > 0) {
                          $(".login_register_box").remove();
                        }
                        saveApp(ref, linkId);
                        closeLoginBox();
                    }
                    else if (type == 'chooseCV') {
                        refreshLoginButtons();
                        if ($(".login_register_box").length > 0) {
                          $(".login_register_box").remove();
                        }
                        selectCVs();
                        prefillUserData();
                    }
                    else {
                        refreshLoginButtons();
                        if ($(".login_register_box").length > 0) {
                          $(".login_register_box").remove();
                        }
                        submitCheck(evt);
                    }
                }
                else {
                    $('.login_box_form .waiting').hide();
                    $('#forgotten_password').show();
                    $('.btn_login_small').show();
                    changeImage();
                    if (data == 'error_details') {
                        $('#login_error').html('Wrong username or password. Please amend.');
                    }
                    else if (data == 'error_captcha') {
                        $('#login_error').html('The letters you typed did not match the letters displayed in the image.');
                        $('#imagekey').val('');
                    }
                    else {
                        $('#login_error').html('There was an error logging you in. Please try again later.');
                    }
                    $('.login_box #username, .login_box #password, .login_box #imagekey').focus(function() {
                        $('.login_box #login_error').html('');
                    });
                }
            }
        });
    }
}

function registerUser(type, ref, url, x, y, linkId) {
    if ($('.register_box #firstname').val() == '' || $('.register_box #surname').val() == '' || $('.register_box #email').val() == '' || $('.register_box #password1').val() == '' || $('.register_box #password2').val() == '') {
        $('#register_error').html('Please fill all fields.');
        $('.register_box #email, .register_box #password1, .register_box #firstname, .register_box #password2, .register_box #surname').focus(function() {
            $('#register_error').html('');
        });
    }
    else {
        if ($('.register_box #password1').val() != $('.register_box #password2').val()) {
            $('#register_error').html('The password and the confirmation password are not the same. Please amend and try again.');
            $('.register_box #email, .register_box #password1, .register_box #firstname, .register_box #password2, .register_box #surname').focus(function() {
                $('#register_error').html('');
            });
        }
        else {
            $('.btn_register_small').hide();
            $('.register_box_form .waiting').show();
            $.ajax({
                type: 'POST',
                url: ajaxDoRegisterURL,
                data: 'title=' + $('.register_box #title').val() + '&firstname=' + $('.register_box #firstname').val() + '&surname=' + $('.register_box #surname').val() + '&email=' + $('.register_box #email').val() + '&password1=' + $('.register_box #password1').val() + '&password2=' + $('.register_box #password2').val(),
                success: function(data){
                    if (data != 'error' && data != 'error_details' && data != 'error_existing_user') {
                        if (type == 'href') {
                            window.location.href = ref
                        }
                        else if (type == 'savealert') {
                            var uri = url.substring(0, url.indexOf('?'));
                            var params = url.substring(url.indexOf('?') + 1, url.length);
                            refreshLoginButtons();
                            saveAlert(params, uri, x, y);
                            closeLoginBox();
                        }
                        else if (type == 'saveadvert') {
                            refreshLoginButtons();
                            saveAdvert(ref, url, linkId);
                            closeLoginBox();
                        }
                        else if (type == 'saveapp') {
                            refreshLoginButtons();
                            saveApp(ref, linkId);
                            closeLoginBox();
                        }
                        else if (type == 'chooseCV') {
                            refreshLoginButtons();
                            if ($(".login_register_box").length > 0) {
                              $(".login_register_box").remove();
                            }
                            selectCVs();
                            prefillUserData();
                        }
                        else {
                            refreshLoginButtons();
                            submitCheck(evt);
                        }
                    }
                    else {
                        $('.register_box_form .waiting').hide();
                        $('.btn_register_small').show();
                        if (data == 'error_existing_user') {
                            $('#register_error').html('There is already user with that email address. Please amend your data.');
                        }
                        else {
                            $('#register_error').html('There was an error registering you. Please try again later.');
                        }
                        $('.register_box #email, .register_box #password1, .register_box #firstname, .register_box #password2, .register_box #surname').focus(function() {
                            $('#register_error').html('');
                        });
                    }
                }
            });
        }
    }
}

function selectCVs() {
    $('.cvlist .choosecv').hide();
    $('.cvlist .waiting').show();
    $.ajax({
        type: 'POST',
        data:'adId='+$('#adId').val(),
        url: ajaxSelectCVs,
        success: function(data){
            $(".waiting").hide();
            $(".choosecv").hide();
            $('.cvlist').append(data);
        }
    });
}

function loginOrRegister(event, btnID) {
    if(event.keyCode==13) {
        $('#' + btnID).click();
    }
}

function createLoginBox(type, reference, url, coord, linkId) {
    var hasNoRegistration = false; // if link has "no_reg" class, registration option doesn't appear
    if (linkId != "" && $("#"+linkId).length > 0 && $("#"+linkId).attr("class").indexOf("no_reg") != -1) {
        hasNoRegistration = true;
    }
var popup_html =
        "<div class=\"login_register_box\">"
            + "<div class=\"user_choice\">"
                + "<input type=\"radio\" id=\"old_user\" name=\"user_choice\" checked onclick=\"showPanel('login_box');\"/>"
                + "<label class=\"existing_user select\" for=\"old_user\">Existing user</label>";

if (!hasNoRegistration) {
     popup_html +="<input type=\"radio\" id=\"new_user\" name=\"user_choice\" onclick=\"showPanel('register_box');\"/>"
                + "<label class=\"new_user\" for=\"new_user\">New user</label>";
}

popup_html += "<a href=\"javascript:void(0);\" onclick=\"closeLoginBox();\" class=\"close_login_box\">X Close</a>"
            + "</div>"
            + "<div class=\"login_box\" onkeydown=\"loginOrRegister(event, \'login_btn\');\">"
                + "<h4>Log-in</h4>"
                + "<p>If you've already registered with fish4, sign in below.</p>"
                + "<form class=\"login_box_form\" name=\"loginform\" id=\"loginform\" action=\"" + accountURL + " + ajax/login\">"
                    + "<fieldset title=\"Login\">"
                        + "<p id=\"login_error\" class=\"error_line\">&nbsp;</p>"
                        + "<p>"
                            + "<label for=\"frmUserName2\">Email</label>"
                            + "<input id=\"username\" tabindex=1 type=\"text\" name=\"username\" />"
                        + "</p>"
                        + "<p>"
                            + "<label for=\"frmPassWord2\">Password</label>"
                            + "<input id=\"password\" tabindex=2 type=\"password\" name=\"password\" />"
                        + "</p>"
                        + "<p class=\"jcaptcha_image\"><label>"
                        + "<img src=\"/jcaptcha/jpeg/image \" />"
                        + "<br/><a href=\"javascript:void(0)\" class=\"change_jcaptcha_image\" onclick=\"changeImage();\">change image</a></label>"
                        + "<input id=\"imagekey\" class=\"imagekey\" tabindex=3 name=\"imagekey\" />"
                        + "<br/><small>Please type what you see from the image</small></p>"
                    + "</fieldset>"
                    + "<fieldset title=\"Submit\">"
                        + "<p>"
                            + "<a href=\"/myfish4/password\" id=\"forgotten_password\" title=\"Forgotten Password\">Forgotten your password?</a>"
                            + "<span class=\"waiting\">Logging in ...</span>"
                            + "<a href=\"javascript:void(0);\" tabindex=4 id=\"login_btn\" class=\"btn_login_small row_right\" onclick=\"loginUser('" + type + "', '" + reference + "', '" + url + "', '" + coord.x + "', '" + coord.y + "', '" + linkId + "');\">&nbsp;</a>"
                        + "</p>"
                    + "</fieldset>"
                + "</form>"
            + "</div>";

if (!hasNoRegistration) {
popup_html += "<div class=\"register_box\" onkeydown=\"loginOrRegister(event, \'register_btn\');\">"
                  + "<h4>Register now</h4>"
                  + "<form name=\"register_box_form\" id=\"register_box_form\" class=\"register_box_form\" action=\"" + accountURL + " + ajax/register\">"
                      + "<fieldset title=\"Register\">"
                          + "<p id=\"register_error\" class=\"error_line\">&nbsp;</p>"
                          + "<p>"
                              + "<label for=\"title\">Title</label>"
                              + "<select id=\"title\" name=\"title\">"
                                  + "<option value=\"Mr\">Mr</option>"
                                  + "<option value=\"Mrs\">Mrs</option>"
                                  + "<option value=\"Miss\">Miss</option>"
                                  + "<option value=\"Ms\">Ms</option>"
                                  + "<option value=\"Dr\">Dr</option>"
                              + "</select>"
                          + "</p>"
                          + "<p>"
                              + "<label for=\"firstname\">First name</label>"
                              + "<input id=\"firstname\" type=\"text\" name=\"firstname\" />"
                          + "</p>"
                          + "<p>"
                              + "<label for=\"surname\">Surname</label>"
                              + "<input id=\"surname\" type=\"text\" name=\"surname\" />"
                          + "</p>"
                          + "<p>"
                              + "<label for=\"email\">Email</label>"
                              + "<input id=\"email\" type=\"text\" name=\"email\" />"
                          + "</p>"
                          + "<p>"
                              + "<label for=\"password1\">Password<br/><small>(3-12 characters)</small></label>"
                              + "<input id=\"password1\" type=\"password\" name=\"password1\" />"
                          + "</p>"
                          + "<p>"
                              + "<label for=\"password2\">Confirm password</label>"
                              + "<input id=\"password2\" type=\"password\" name=\"password2\" />"
                          + "</p>"
                          + "<p>"
                              + "By clicking 'register' you are agreeing to fish4's <a href=\"javascript:void(0)\" onclick=\"window.open('/info/userterms', 'userterms', 'location=0,resizable=yes, statusbar=0,scrollbars=1, menubar=0, width=680, height=500');\" class=\"popup\">user terms</a> and <a href=\"javascript:void(0)\" onclick=\"window.open('/info/privacy', 'privacy_policy', 'location=0,resizable=yes, statusbar=0,scrollbars=1, menubar=0, width=680, height=500');\" class=\"popup\">privacy policy</a>"
                          + "</p>"
                      + "</fieldset>"
                      + "<fieldset>"
                          + "<p>"
                              + "<span class=\"waiting\">Registering ...</span>"
                              + "<a href=\"javascript:void(0);\" id=\"register_btn\" class=\"btn_register_small row_right\" onclick=\"registerUser('" + type + "', '" + reference + "', '" + url + "', '" + coord.x + "', '" + coord.y + "', '" + linkId + "');\">&nbsp;</a>"
                          + "</p>"
                      + "</fieldset>"
                  + "</form>"
            + "</div>";
}

popup_html += "</div>";
    $(document.body).append(popup_html);
}

function showPanel(panelBox) {
    if (panelBox == 'login_box') {
        $('.register_box').hide();
        $('.new_user').removeClass('select');
        $('.existing_user').addClass('select');
    }
    else {
        $('.login_box').hide();
        $('.existing_user').removeClass('select');
        $('.new_user').addClass('select');
    }

    $('.' + panelBox).fadeIn();
}

function closeLoginBox() {
    $(".login_register_box").hide(function(){
        if($("#AP").length==1 && $("#AP").css("visibility")=="hidden") $("#AP").css("visibility", "visible");
    });
}
var captchTimeout = null;
function changeImage() {
    var date = new Date();
    var newSrc = "/jcaptcha/jpeg/image?" + date.getMilliseconds() + date.getSeconds();
    captchTimeout = window.setTimeout( function() {
        window.clearTimeout(captchTimeout);
        $('.jcaptcha_image img').attr("src", newSrc);
      }, 500);
    $('#imagekey').val('');
}

function getCoordinates(evt) {
    var x=0, y=0;
    if (document.all) {//IE
        x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
        y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        x += window.event.clientX;
        y += window.event.clientY;
    }
    else {//Good Browsers
        x = evt.pageX;
        y = evt.pageY;
    }
    return {'x': x, 'y': y};
}


