﻿//Function to fix bug in AddThis button, when crash with Prototype
var qsParm = new Array();
function qs() {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos);
            var val = parms[i].substring(pos + 1);
            qsParm[key] = val;
        }
    }
} 

Object.extend(
       Function.prototype, {
           substr: function() {
               var __method = this;
               return function() {
                   return "";
               }
           }
       }
   );

       jQuery(document).ready(function() {


           //alert(document.getElementById("body_main_panel").style.height);


           qs();
           if (qsParm['cl'] == '1') {
               jQuery("#applying").val("save");
               jQuery("#jobInfo").slideUp(750, function() {
                   jQuery("#coveringLetter").slideDown(750);
               });
           }
           jQuery(".saveToBasket").click(function(e) {
             document.getElementById("addthisDiv").style.height = "0px";
               jQuery("#applying").val("save");
               jQuery("#jobInfo").slideUp(750, function() {
                   jQuery("#coveringLetter").slideDown(750);
               });
               return false;
           });

           jQuery(".apply").click(function(e) {
           document.getElementById("addthisDiv").style.height = "0px";
               jQuery("#applying").val("apply");
               jQuery("#jobInfo").slideUp(750, function() {
                   jQuery("#coveringLetter").slideDown(750);
               });
               return false;
           });


           jQuery(".backToDetails").click(function(e) {
           document.getElementById("addthisDiv").style.height = "30px";
               jQuery("#coveringLetter").slideUp(750, function() {
                   jQuery("#jobInfo").slideDown(750, function() {
                       jQuery("#coveringLetter").attr("width", "100%");
                   });
               });
               return false;
           });

           jQuery('a[href*=#]').click(function() {
               if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
 && location.hostname == this.hostname) {
                   var jQuerytarget = jQuery(this.hash);
                   jQuerytarget = jQuerytarget.length && jQuerytarget
   || jQuery('[name=' + this.hash.slice(1) + ']');
                   if (jQuerytarget.length) {
                       var targetOffset = jQuerytarget.offset().top;
                       jQuery('html,body')
  .animate({ scrollTop: targetOffset }, 1000);
                       return false;
                   }
               }
           });


           jQuery(".coverLetterSave").click(function(e) {
               jQuery("#aspnetForm").submit();
               return false;
           });







       });









/*non jquery funtions*/
tinyMCE.init({
    mode: "exact",
    elements: "coveringLetterText",
    theme: "simple"
});

/*
    ;jQuery(function(jQuery) {
    // work out correct scroll element
    var scrollElement = 'html, body';
    jQuery('html, body').each(function() {
        var initScrollTop = jQuery(this).attr('scrollTop');
        jQuery(this).attr('scrollTop', initScrollTop + 1);
        if (jQuery(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            jQuery(this).attr('scrollTop', initScrollTop);
            return false;
        }
    });

  
 jQuery(".selectAll").focus(function(e) {
        if (e.target && e.target.defaultValue == e.target.value) {
            setTimeout(function() {
                e.target.select();
            }, 10);
        }
    });

    jQuery(".eFriend").click(function(e) {
        jQuery("#yourName,#friendName,#friendEmail").attr("disabled", false).each(function() {
            this.value = this.defaultValue;
        });
        jQuery("#eFriendMsg").html("");
        jQuery("#eFriend").slideDown();
        // scroll form into view
        jQuery(scrollElement).animate({ "scrollTop": jQuery("#bodyLHS").offset().top }, 1000, "swing");
        return false;
    });

    jQuery(".saveToBasket").click(function(e) {
        alert("here");
        var loc = jQuery("#redirLoc");
        if (loc.length) {
            loc[0].options[1].selected = true;
        }
        jQuery("#eFriend").slideUp();
        jQuery("#jobInfo,#tabMenu1,#tabMenu2").slideUp(1000, function() {
            jQuery("#coveringLetter,#tabPart2,#tabPart2_2,#aspnetForm").slideDown(1000);
        });
        return false;
    });

    jQuery(".apply").click(function(e) {
        var loc = jQuery("#redirLoc");
        if (loc.length) {
            loc[0].options[0].selected = true;
        }
        jQuery("#eFriend").slideUp();
        jQuery("#jobInfo,#tabMenu1,#tabMenu2").slideUp(1000, function() {
            jQuery("#coveringLetter,#tabPart2,#tabPart2_2,#aspnetForm").slideDown(1000);
        });
        return false;
    });

    jQuery(".applyCaptcha").click(function(e) {
        jQuery("#Details1_btnSubmit").click();
        return false;
    });

    jQuery(".backToDetails").click(function(e) {
        jQuery("#coveringLetter,#tabPart2,#tabPart2_2,#aspnetForm").slideUp(1000, function() {
            jQuery("#jobInfo,#tabMenu1,#tabMenu2").slideDown(1000, function() {
                jQuery("#coveringLetter").attr("width", "100%");
            });
        });
        return false;
    });

    jQuery(".coverLetterSave").click(function(e) {
        jQuery("#applying").val("coverNote");
        jQuery("#detailsForm").submit();
        return false;
    });

    jQuery("#loginBox").submit(function() {
        headerLogin2(false);
        return false;
    });

    jQuery(".logout").click(function() {
        jQuery.get(jQuery(this).attr("href"), {}, function(data, textstatus) {
            urlStr = "" + window.location.href;
            if (urlStr.indexOf('screening.aspx') < 0) {
                window.location.href = window.location.href;
            } else {
                window.location.href = "/";
            }
        });
        return false;
    });

    jQuery(".forgotPass").click(function() {
        jQuery("#loginBox").slideUp(function() {
            jQuery("#sendPassBox").slideDown();
        });
        return false;
    });

    jQuery("#sendPassBox").submit(function() {
        console.log("sendPassBox:submit");
        var email = jQuery("#sendEmail").val();
        jQuery.post("/ajax/spass.aspx", { "e": email }, function(data, textstatus) {
            jQuery("#passMsg").html("<span style=\"color:#ffffff\">If registered the password has been sent.</span>");
            jQuery("#sendPassBox").slideUp(function() {
                jQuery("#loginBox").slideDown();
            });

        });
        return false;
    });
});

tinyMCE.init({
	mode : "exact",
    elements : "coveringLetterText",
	theme : "simple"
});

function ajaxSend(vacID) {
    jQuery.post('/ajax/sendFriend.aspx', {
            friendEmail : jQuery("#friendEmail").val(),
            friendName : jQuery("#friendName").val(),
            yourName : jQuery("#yourName").val(),
            vacID : vacID
        },
        handlerFuncxx, 
        "text"
    );
}

function handlerFuncxx(data, textstatus) {
    var msg = "Thank you your email has been sent";
    if (data != "success") {
        msg = data;
    }
    jQuery("#eFriendMsg").html('<div style="font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size:14px; color:#5a9bf7;">' + msg + '</div>');
    if (data == "success") {
        jQuery("#friendEmail, #friendName, #yourName").attr('disabled', true),
        setTimeout(function() { 
            jQuery("#eFriend").slideUp();
        }, 5000);
    } else {
        jQuery("#friendEmail").focus().select();
    }
}

var employerLogin = false;
function headerLogin2(isEmployer) {
    var jQuery = jQuery;
    employerLogin = isEmployer;
    jQuery.post("/ajax/loginQuick.aspx", jQuery("#loginBox").serialize(),
        function (data, textstatus) {
            jQuery("#xloginEmailID, #xloginPasswordID").attr("disabled", false);
            if (data == "failed") {
                alert("Login incorrect"); 
            } else {
                if (employerLogin) {
                    window.location="http://jobs.search.co.uk/employerHomepage.aspx";
                } else {
                    var urlStr = window.location.href;
                    if (urlStr.indexOf('apply.aspx') < 0) {
                        window.location="/mysearch.aspx";
                    } else {
                        window.location.reload(true);
                    }
                }
            }
        }, 
        "text"
    );
}
*/
