// put the survey URL here 
var survey = "http://surveyor.imc2.com/ss/wsb.dll/s/5g5e";
var Page_Enter;
var Page_ShowPopOnExit;
var popover = true;
var windowprops = "width=400,height=300,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes";
var phdesign = GetCookie("www-origin");
if(!phdesign)
{
	phdesign = "albert";
}
if (typeof DCSext != "undefined") {

   		DCext.Profile = phdesign;
}
var MyDomain=new Array();
MyDomain[0]="dev.imc2.com";
MyDomain[1]="stage.imc2.com";
MyDomain[2]="pizzahut.com";
MyDomain[3]="quikorder.com";

function IsInDomain(href)
{
	
	if(href.indexOf(".com") == -1)
	{
		return true;
	}
	
	for (var i = 0; i < MyDomain.length; i++) {
	 		if(href.indexOf(MyDomain[i]) != -1){
	 		  return true;
	 		}
	 }
	 return false;        
}


 
 
var count = 0;
var myW;
var expDays = 30; // number of days the cookie should last
var exp = new Date();
exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60 * 1000));
var phexitOverride =false;
function onBlur() {
	phexitOverride =true;
}
function onFocus(){
    if (navigator.appName == 'Microsoft Internet Explorer') {
        phexitOverride =false;	
    }
}
 
if (/*@cc_on!@*/false) { // check for Internet Explorer
	document.onfocusin = onFocus;
	document.onfocusout = onBlur;
} else {
	window.onfocus = onFocus;
	window.onblur = onBlur;
}
 
 
// load up the cookie data
function GetCookie(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) 
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) 
            break;
    }
    return null;
}
 
// Create a cookie
function SetCookie(name, value){
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
    
}
 
// delete a cookie
function DeleteCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
 
// get a paticular variable from the cookie
function getCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) 
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
 
/*******/
function XBrowserAddHandlerPops(target, eventName, handlerName){
    if (target.addEventListener) {
        target.addEventListener(eventName, function(e){
            target[handlerName](e);
        }, false);
    }
    else 
        if (target.attachEvent) {
            target.attachEvent("on" + eventName, function(e){
                target[handlerName](e);
            });
        }
        else {
            var originalHandler = target["on" + eventName];
            if (originalHandler) {
                target["on" + eventName] = function(e){
                    originalHandler(e);
                    target[handlerName](e);
                };
            }
            else {
                target["on" + eventName] = target[handlerName];
            }
        }
}
 
function InternalLink(){
    Page_ShowPopOnExit = false;
}
 
function PageEnter(){
    Page_Enter = new Date();
    Page_ShowPopOnExit = true;
}
 

function SiteExit(){
	//alert("SiteExit- " + Page_ShowPopOnExit);
	
	if (Page_ShowPopOnExit == false)
		return;	
		
  
	DisplayPopup();
}

function ShouldShowPopup()
{
	var show = Page_ShowPopOnExit;
	//alert("ShouldShowPopup= " + 	Page_ShowPopOnExit);
	var phshowsurvey= GetCookie("phshowsurvey");
	if(phshowsurvey== "done")
	  show = false;
	
	//alert("Show= " + show);	
	//alert("stat= " + phshowsurvey);
	return show;
}

function DisplayPopup()
{
  //alert("DisplayPopup");
  if (ShouldShowPopup() == true) {
  		
  		//alert("Displaying popup...");
        var myPage = survey + "?wsb87=" +  phdesign;
        parentWin = window;
        myW = window.open(myPage, "Survey", windowprops);
        
        if(popover)
        {
	        if (window.focus) {
	            myW.focus();
	        }
  
        }else{
            if(parentWin)parentWin.focus();
        }
  		      
        //alert('setting cookie to end');
        SetCookie("phshowsurvey", 'done', exp);
    }

}

function LinkConvert(){
    var href;
    var anchors = document.getElementsByTagName('a');
    
   
     for (var y = 0; y < anchors.length; y++) {
      if(IsInDomain(anchors[y].href.toLowerCase()) == true)
      {
        anchors[y].clickhandler = InternalLink;
       	XBrowserAddHandlerPops(anchors[y], "click", "clickhandler");
      }
   	}

    
}


var phshowsurvey= GetCookie("phshowsurvey");
//alert("Cookie= " + phshowsurvey);
        
if(phshowsurvey!= "done")
{
        XBrowserAddHandlerPops(window, "load", "PageEnter");
        XBrowserAddHandlerPops(window, "load", "LinkConvert");
        XBrowserAddHandlerPops(window, "unload", "SiteExit");
 }   

