//get cookie

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
if(dc.length >0){
   begin = dc.indexOf(cname);
   if(begin != -1 ) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin,end));
      }
   } 
return null;
}   

function setCookie(name, value, expires){
document.cookie = name + "=" + escape(value) + "; path=/" + 
 ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}


function genRandom( maxval, devider){ 

        var generatornum=Math.random();//Number between 0 and 1
        var randomnum=Math.round(generatornum*maxval);//Converts to whole num 
       var ndevider = 9; 
        
        if ( (randomnum % ndevider) == 0){
            showwindow();
        } 
}  

function showwindow(){
if (getCookie(cookieName)== null){ 
launchRemote();
}
}


function launch(newURL, newName, newFeatures, orgName) {
var remote = open(newURL, newName, newFeatures);
if (remote.opener == null)
remote.opener = window;
remote.opener.name = "opener";
return remote;
}

function launchRemote() {
myRemote =launch("/survey/popup.html",
"myRemote",
"height=175,width=250,alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=1,toolbar=0,z-lock=0",
"myWindow");
}

function popup(){
popup =  window.open("","popup","toolbar=no,width=200,height=150,status=no,menubar=no,scrollbars=no,");
popup.location.href = 'popup.html';
if(popup.opener == null) popup.opener = window;
popup.opener.name = "opener";
}

function delCookie(name){
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}

// cookie expires after 30 days
var exp = new Date();
exp.setTime(exp.getTime() + (24 * 60 * 60 * 1000 * 30));
var cookieName = 'StarSurvey0307';
var theMessage = 'test';

function setLongCookie(name, value) {
 var today=new Date();
 var expiry = new Date(today.getTime() + 366*24*3600*1000);
 // the following line is for testing purpose only
 // var expiry = new Date(today.getTime() + 30*60*1000);
  if(value !=null && value != "")
   document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString()+"; path=/";
}

function getDomain(){
	url=document.location+"";
	if(url.indexOf("://")==-1){return "-";}
	rest=url.substring(url.indexOf("://")+3);
	domain=rest.substring(0,rest.indexOf("/"));
	return domain;
}
if(getCookie("gx_session_id_")){
        if(!getCookie("starAccess")){
                setLongCookie("starAccess", "Star-"+getCookie("gx_session_id_")+"||"+getDomain());
//                window.open("/jsp/subscribeq.htm","aaa","width=300,height=200,location=no,resizable=yes");
        }
        else{
        	oldCookie=getCookie("starAccess");
        	if(oldCookie.indexOf("||")==-1){ thisCookie=oldCookie+"||"+getDomain();}
        	else{thisCookie=oldCookie;}
                setLongCookie("starAccess", thisCookie);
        }
}

//currentpop="Oct24-2001";
//currentpage="/survey/asksurvey.htm";
//sizex=300;
//sizey=140;
//if(getCookie("popup")!=currentpop){
//if(true){
        //setLongCookie("popup",currentpop);
        //window.open(currentpage, "aaa", "width="+sizex+",height="+sizey+",location=no,resizable=yes,scrolling=yes");
//}

function goTo( url ){
	document.location= url;
	return 1;
}

var GalleryWindow;
function gallery_open( site, gid ) {
        var pagename= site + '/Render';
        var url = '/NASApp/cs/ContentServer?pagename='+pagename+'&c=Gallery&cid='+gid;

        GalleryWindow = window.open( url, 'gallery_window', 'height=550,width=550,screenX=10,screenY=10,top=10,left=10,resizable=yes,menubar=no,location=no,toolbar=no,scrollbars=yes' );
        if (GalleryWindow.opener == null) {
                GalleryWindow.opener = self;
        }
        GalleryWindow.focus();
//        GalleryWindow.select();
//        return 1;

}

function pageMatch (url) {
    var docurlS = new String(url);
    if (docurlS.match(/c=Page/) != null) {
        return 1;        
    }
    else {
        return 0;
    }
}  

function doSurvey( cookieName, checkPage) {
    var ExpireDate = new Date ();
    var expiredays = 365;
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

    // Check to see if Cookies are enabled.  If not, we make the occurrence
    // of the popup less often.
    var nth;
    var loc = document.domain;
    var docurl = document.location;
           
    // Enable checking to see if it is a page, used to show survey only on main
    // pages and not articles.
    var pageMatched;
//if (1 == 0) {
    // if function requires the check
    if (checkPage == 1) {
        pageMatched = pageMatch(docurl); 
    }     
    // otherwise bypass
    else {
        pageMatched = 1;
    }
 
    if (pageMatched == 1) { 
        if (document.domain == "www.thestar.com" ||
            document.domain == "ottawa.thestar.com" ||
            document.domain == "montreal.thestar.com"
            ) {
            setCookie('cookieEnabled', 'true', ExpireDate);
            var cookieEnabled = getCookie('cookieEnabled');
            if (cookieEnabled == 'true'){
                nth = 5;
            }
            else{
                nth = 150;
            }

            var rnd = Math.floor(Math.random() * nth) + 1;
            //document.write("rnd is: " + rnd);
            if (rnd == nth){
                var surveyCookie = getCookie('StarSurvey03252002');
               // Popup window if it has never been popped up before

               if (surveyCookie == null){
    
                    // Note that setting of cookies is handled in a perl script 
                    var windowHandle = window.open('http://www.thestar.com/survey/popup.html','windowName','width=300,height=150');
                    if (!windowHandle.opener){
                        windowHandle.opener = self;
                    }
               }
            }
        }
    }
//}
}

function doSurvey5() {
    currentpop="20050328";
    currentpage="http://www.thestar.com/survey/asksurvey.htm";
    sizex=400;
    sizey=210;
    var ExpireDate = new Date ();
    var expiredays = 365;
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    var curLoc = location.href;
    if((curLoc.indexOf('http://www.thestar.com/NASApp/cs/ContentServer?pagename=thestar/Render&inifile=futuretense.ini&c=Page&cid=968332188492&pubid=968163964505')==-1) || (curLoc.indexOf("test.html")==-1)) {
        setCookie('cookieEnabled', 'true', ExpireDate);
        var cookieEnabled = getCookie('cookieEnabled');
        if (cookieEnabled == 'true'){
            nth = 5;
        }
        else{
            nth = 150;
        }

        var rnd = Math.floor(Math.random() * nth) + 1;
        if (rnd == nth){
            if(getCookie("popup")!=currentpop){
                setLongCookie("popup",currentpop);
                window.open(currentpage, "aaa", "width="+sizex+",height="+sizey+",location=no,resizable=yes,scrollbars=yes");
            }
        }
    }
}

//doSurvey5();

function doPopup() {

}
function doSurvey2(){
}



