﻿function GetApplyClickCount(applyURL, url, type, parameters, isCenter, widhtW, heightW, domain, srcimg, trackingparam)
{

    if ("" != trackingparam)
    {
        pageTracker._trackPageview(trackingparam);
    }

    if(isCenter = "true" && widhtW != "0" && heightW != "0")
    {
        if(parameters != "") 
            parameters += ", ";
        
        parameters += "top=" + (Math.round(document.body.offsetHeight/2) - Math.round(heightW/2)) + ", left=" + (Math.round(document.body.offsetWidth/2) - Math.round(widhtW/2));
    }   

    if(srcimg == "")
    {
        if(type == "NewWindow")    
            window.open(applyURL, GetGUID(), parameters);
        else if (type == "SameWindow")
            openUrlInSameWindowPassingReferrer(applyURL);
        else if (type == "Lightbox")
        {
            createLightBox(document.body.offsetHeight, document.body.offsetWidth, applyURL, "");
            createBlockSite(document.body.offsetHeight, document.body.offsetWidth);        
        }
    }
    else
    {
        if(type == "NewWindow")    
            window.open("http://"+domain+"/ApplyWindow.aspx?srcimgheader=" + encodeURIComponent(srcimg) + "&srciframe=" + encodeURIComponent(applyURL), GetGUID(), parameters);
        else if (type == "SameWindow")
            window.location.href = "http://"+domain+"/ApplyWindow.aspx?srcimgheader=" + encodeURIComponent(srcimg) + "&srciframe=" + encodeURIComponent(applyURL);
        else if (type == "Lightbox")
        {
            createLightBox(document.body.offsetHeight, document.body.offsetWidth, applyURL, srcimg);
            createBlockSite(document.body.offsetHeight, document.body.offsetWidth);        
        }
    }
    
    scroll(0,0);
    
    var newURL = (url.indexOf("?") != -1) ? url + "&clickok=true" : url + "?clickok=true";
    var xmlHttp, e;
    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) {}
    }
    xmlHttp.open("POST", newURL, false);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}

// hack for IE to pass referrer info when opening URL
function openUrlInSameWindowPassingReferrer(url)
{
    var a = document.createElement("a");
    if(!a.click) { //only IE has this (at the moment);
        window.location = url;
        return;
    }
    a.setAttribute("href", url);
    a.style.display = "none";
    document.body.insertBefore(a);
    a.click();
}

function GetGUID()
{
    var guid = "";
    for (var i = 0; i < 32; i++)
      guid += parseInt(Math.random() * 16).toString(16);
      
    return guid;
}

function createBlockSite(heightS, widthS)
{
    var blockSite = document.createElement("div");
         
    with(blockSite.style)
    {
        position = "absolute";
        zIndex = "100";
        top = "0px";
        left = "0px";
        width = widthS+"px";
        height = heightS+"px";    
        //width = "100%";
        //height = "100%";
        filter = "alpha(opacity=" + 50 + ")";
        opacity = 0.5;
        background = "black";    
    }

    blockSite.id = "blockSite";           
    document.getElementsByTagName("body")[0].appendChild(blockSite);
} 

function createLightBox(heightS, widthS, applyURL, srcheader)
{
    var lightBox = document.createElement("div");         
    with(lightBox.style)
    {
        position = "absolute";
        zIndex = "2000";
        top = "50%";
        left = "50%";           
    }
    lightBox.id = "lightBox"; 
    lightBox.className = "lightBox";
    
    var header = document.createElement("div");   
    header.id = "headerLightBox";
    header.className = "headerLightBox";
    
    var closeL = document.createElement("a");   
    closeL.innerHTML = "Close";
    closeL.href = "javascript:CloseLB();";
    closeL.id = "closeLightBox";
    closeL.className = "closeLightBox";
    header.appendChild(closeL);
    
    lightBox.appendChild(header);
    
    var content = document.createElement("div");   
    with(content.style)
    {       
        overflow = "auto";   
    }
    content.id = "contentLightBox";
    content.className = "contentLightBox";
    lightBox.appendChild(content);
    
    if(srcheader != "")
    {
        var img = document.createElement("img");
        img.src = srcheader;
        content.appendChild(img);
    }
    
    var imgL = document.createElement("img");
    imgL.id = "imgLoadingLightBox";
    imgL.src = "http://seostatic.tmp.com/job-images/throbber2.gif";
    imgL.style.position = "absolute";
    imgL.style.zIndex = "2001";
    imgL.style.top = "52%"; //iframeL.style.top + Math.round(widthIframe/2); //150
    imgL.style.left = "49%"; //iframeL.style.left + Math.round(widthIframe/2); //300
    content.appendChild(imgL);
    
    var iframeL = document.createElement("iframe");
    iframeL.id = "iframeLightBox";
    iframeL.scrolling = "auto";
    iframeL.src = applyURL;
    iframeL.className = "iframeLightBox";
    eventPush(iframeL,'load',function () {CloseImg();});
    content.appendChild(iframeL);
    
    var widthIframe = (iframeL.width == "") ? 300 : iframeL.width;
    var heightIframe = (iframeL.height == "") ? 150 : iframeL.height;
//    alert(iframeL.style.top);
//    alert(iframeL.style.left);
    
    document.getElementsByTagName("body")[0].appendChild(lightBox);
} 

function CloseLB()
{
    var blockSite = document.getElementById("blockSite");
    blockSite.parentNode.removeChild(blockSite);
    var lightBox = document.getElementById("lightBox");
    lightBox.parentNode.removeChild(lightBox);
}

function CloseImg()
{
    var imgLoad = document.getElementById("imgLoadingLightBox");
    imgLoad.parentNode.removeChild(imgLoad);
}
  
function eventPush(obj, event, handler) { 
  if (obj.addEventListener) { 
    obj.addEventListener(event, handler, false); 
  } else if (obj.attachEvent) { 
    obj.attachEvent('on'+event, handler); 
  } 
} 
