//======================================================================
// Copyright (c) 1997-1999 Cyber Dialogue Inc.  All rights reserved.
//
// This file is a part of Cyber Dialogue's Profiler module.
// It may not be copied, distributed, or resold except as specified
// in the accompanying license agreement.
//
// Access to this file does not imply rights to reverse
// engineer, modify, distribute, resell, or copy it.
//
// THIS SOFTWARE IS PROVIDED BY CYBER DIALOGUE INC. ``AS IS'' AND ANY
// EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CYBER DIALOGUE INC. OR
// ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
// ======================================================================

// -- Global Variables -- //
var study = 'CNW7P1';
var flip = true;
var s_hostname = 'www.cartoonnetwork.com';
var popup_page = '/survey/survey.html';
var frequency = 2500;
var cookie_name = 'XNoPopup';
var cookie_domain = '.cartoonnetwork.com';
var cookie_date = 'Sun, 31-Dec-01 00:00:00 GMT';
var width = 600;
var height = 440;
var cookie_path = '/';

// -----------------------------------------------------/
// -- quick function to pull out the referer          --/
// -----------------------------------------------------/
function get_referer() {
    var referer = self.location.host+self.location.pathname+self.location.search;
    return referer;
}

// -----------------------------------------------------/
// -- function to append to the query string          --/
// -- checks to see if data has already been appended --/
// -----------------------------------------------------/
function append_url (url, append_name, append_string) {
    var v = parseInt(navigator.appVersion.substring(0,1));
    var append = new String();

    if (( v > 3) && ((append_string == null) || (append_string == 'null')) ){
     append = append_name+'=';
  }
    else {
      append = append_name+'='+escape(append_string);
   }

    if (url.indexOf('?') == -1) {
	url = url+'?'+append;
    }
    else {
	url = url+'&'+append;
    }
    return url;
}

// -----------------------------------------------------/
// -- determines if popup window should go, intercepts
// -----------------------------------------------------/
function popup(url, freq, cname, domain, exp, w, h, path) {
  var index;
  var popup_url;
  
  if ((document.cookie.lastIndexOf(cname)==-1) &&
      (Math.floor(7654321.0*Math.random())%freq==0))
  {
    set_cookie(cname, exp, domain, path);
    var v = parseInt(navigator.appVersion.substring(0,1));

    // -- netscape 3.01 cleanup -- //
    popup_url = append_url(url,'referer',get_referer());

    // -- make sure that cookies get set for both domain
    // -- and host that is the domain, but flipping the user
    // -- to the opposite of where they came from, if it exists.
    if (flip){
      if (is_domain(self.location.hostname)) {
        popup_url = host_to_domain(self.location, popup_url);
      }
      else {
        popup_url = domain_to_host(self.location, popup_url);
      }
    }

    var pw = window.open(popup_url,
   	                 'thePopupWin', 'toolbar=no,location=no,' +
                         'directories=no,status=no,scrollbars=yes,' +
		         'menubar=no,width=' + w + ',height=' + h +
		         ',dependent=yes');
    if ((pw != null) &&
	!((navigator.userAgent.indexOf('MSIE') != -1) && (v < 3)))
      pw.opener = self;

    // -- do the following for:  netscape, IE except for IE4-- /
    if ( (v >= 3) && 
	(((navigator.userAgent.indexOf('MSIE') != -1) && v != 4) || 
	!(navigator.userAgent.indexOf('MSIE') != -1) ) )
      pw.focus();
  }
}

// -----------------------------------------------------//
// -- change popup url from xyz.com to www.xyz.com
// -----------------------------------------------------//
function host_to_domain(url, popup_page) {
   var redirect;
   var newpath;

   if (popup_page.indexOf('/') == 0) {
     redirect = url.protocol+'//'+s_hostname+popup_page; 
   }
   else {
     if (popup_page.indexOf('http') == 0) {
       redirect = popup_page; 
     }
     else {
       if (url.pathname.lastIndexOf('/') != -1){
	 newpath = url.pathname.substring(0, url.pathname.lastIndexOf('/')+1);
       }
       redirect = url.protocol+'//'+s_hostname+newpath+popup_page; 
     }
   }
   return(redirect);
}

// -----------------------------------------------------//
// -- change popup url from www.xyz.com to xyz.com
// -----------------------------------------------------//
function domain_to_host(url, popup_page) { //go from www.a.com to a.com
   var redirect;
   var newpath;

   s_hostname = url.host.substring(url.host.indexOf('.')+1, url.host.length);

   if (popup_page.indexOf('/') == 0) {
     redirect = url.protocol+'//'+s_hostname+popup_page; 
   }
   else {
     if (popup_page.indexOf('http') == 0) {
       redirect = popup_page; 
     }
     else {
       if (url.pathname.lastIndexOf('/') != -1){
	 newpath = url.pathname.substring(0, url.pathname.lastIndexOf('/')+1);
       }
       redirect = url.protocol+'//'+s_hostname+newpath+popup_page; 
     }
   }
   return(redirect);
}

// -----------------------------------------------------//
// -- close popup window
// -----------------------------------------------------//
function go_away() {
  window.close();
}

// -----------------------------------------------------//
// -- open new window with survey, close popup window
// -----------------------------------------------------//
function jump_to_survey(url) {
  var v = parseInt(navigator.appVersion.substring(0,1));
  var pw = window.open(url + self.location.search, 'theSurveyWin',
                       'toolbar=no,location=no,directories=no,' +
                       'status=no,scrollbars=yes,menubar=yes');
  window.close();
}

// -----------------------------------------------------//
// -- get the value of a var from the query string
// -----------------------------------------------------//
function get_search_val(url, name) {
  var value = url.substring(url.indexOf(name)+name.length+1,url.length);
  var v = parseInt(navigator.appVersion.substring(0,1));

  if (url.indexOf(name) == -1){
    return 'null';
  }

  if (v == 3) {
    if (value == null) {
      return 'null';
    }
  }
  else {
    if (value == false) {
      return 'null';
    }
  }

  if (value.indexOf('&') == -1) {
    return unescape(value);
  }
  else {
    value = value.substring(0, value.indexOf('&'));
    return unescape(value);
  }
}

// -----------------------------------------------------//
// -- set a cookie
// -----------------------------------------------------//
function set_cookie (cname, cexpires, cdomain, cpath) {
  document.cookie = cname + '=true; expires=' + cexpires + '; path=/;' +
    ((cdomain != null) ? (' domain=' + cdomain) : '');
}

// -----------------------------------------------------//
// -- show count image, according to study
// -----------------------------------------------------//
function count_image () {
  document.write("<IMG SRC='http://survey.cyberdialogue.com" +
                 "/webqnr/engine/count.gif?client=" + study +
                 "' WIDTH=1 HEIGHT=1>");
}

// -----------------------------------------------------//
// -- determine if hostname is domainmame or not
// -----------------------------------------------------//
function is_domain(url_hostname){
  var value = url_hostname.substring(
    url_hostname.indexOf('.')+1,
    url_hostname.length).indexOf('.');

    if (value == -1) {
	return 1;
    }
    else {
	return 0;
    }
}

