
// code to break framesets
// if mistakenly embedded
//breakFrameset();


// --------- BEGIN DM_SEGMENT code ------------------

// segment params
var mv = new Array(
   "firstSeg", "secondSeg", "thirdSeg", 
   "fourthSeg", "fifthSeg", "sixthSeg", "lastSeg"
   );

// initialize and globalize segment params
for( i=0; i<mv.length; i++ ) {
    eval( "var "+mv[i]+" = ''" );
}

// parse cookie and set segment params
parseDmSegment();

function parseDmSegment() {
	var crumbs = (getDMCookie("DMSEG")) ? "".concat(getDMCookie("DMSEG")).split("&") : "";
	var howMany = (crumbs[4]) ? crumbs[4] : "";
	var segments = (crumbs[5]) ? crumbs[5] : "";
	var seg = (segments) ? segments.split(",") : "";
	for( i=0; i<mv.length; i++ ) {
		eval( mv[i]+" = (seg["+i+"]) ? seg["+i+"] : ''");
	}
	lastSeg = (seg[seg.length - 1]) ? seg[seg.length - 1] : "";
}
	
// Supporting function to extract cookie value
function getDMCookie(n) {
   var co = document.cookie;
   var pos = co.indexOf(n+"=");
   return pos != -1 ? co.substring(pos+n.length+1,(co.indexOf("; ",pos)!= -1 ? co.indexOf("; ",pos):co.length)):null;
}

// --------- END DM_SEGMENT code ------------------


function getCookieVal(offset) { 
   var endstr = document.cookie.indexOf(";", offset); 
   if (endstr == -1) {
      endstr = document.cookie.length; 
   }
   return unescape(document.cookie.substring(offset, endstr)); 
} 

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; 
} 

function getUBID(ubid) { 
	var str = ubid;
	if ( str == null ) {
      return "";	
   }
   var end=(str.indexOf("!"));
	
	if (end == "0" || end == "") {
		return "";
	} else {
		return escape(str.substring(0,end));
	}
} 

// ----------------------------
// BEGIN doubleclick-related vars

var globalRand = Math.round( Math.random() * 9000000000000000 ) + 1000000000000000;
var billingSite = "startup.wsj.com";
var ubidID = getCookie("UBID");
var segID = getCookie("segment");

if ( !ubidID ) {
   ubidID = "";
}
if ( !segID ) {
   segID = "";
}

//////////////////////////////////////////////////
// BEGIN handling code for query-string referrers

// The 'adZone' param is used dblclick ads to set the 'zone'
// part of the request string.  We'll override it if 
// if certain conditions are met
var adZone;

// 'source' parameter in query-string
if ( getParamVal( 'source' ) == 'google' ) {
        adZone = 'nopop';
}

// basic query-string parser.  does not escape vals
function getParamVal( param ) {
   var qsParam = new Array();
   var qs = location.search.substr(location.search.indexOf("?")+1);
   qs = qs.split("&");
   // split param and value into individual pieces
   for (var i=0; i<qs.length; i++) {
       tmp = qs[i].split("=");
       qsParam[tmp[0]] = tmp[1];
   }
   // note that we did not escape the query string
   if ( typeof qsParam[ param ] != 'undefined' ) {
       return qsParam[ param ];
   }
   return "";
}

// END handling code for query-string referrers
//////////////////////////////////////////////////


// for layers to be exposed at end of page before body
var dcLayerExpose = new Array();

// 2004-07-30 temp support for old variables 
var UBIDCookie = ubidID;
var segmentCookie = segID;

// END doubleclick-related vars
// ----------------------------


// BEGIN Date Functions 

var tDays = new Array();
tDays[0] = "Sunday";
tDays[1] = "Monday";
tDays[2] = "Tuesday";
tDays[3] = "Wednesday";
tDays[4] = "Thursday";
tDays[5] = "Friday";
tDays[6] = "Saturday";

var tMons = new Array();
tMons[0] = "Jan";
tMons[1] = "Feb";
tMons[2] = "Mar";
tMons[3] = "Apr";
tMons[4] = "May";
tMons[5] = "Jun";
tMons[6] = "Jul";
tMons[7] = "Aug";
tMons[8] = "Sep";
tMons[9] = "Oct";
tMons[10] = "Nov";
tMons[11] = "Dec";


function writeDate( whatToReturn ) {
	var dDate = new Date();
	var today;
	
	// append day
	today = tDays[dDate.getDay()];
	// append short month
	today += ", " + tMons[dDate.getMonth()];
	// append date
	today += " " + dDate.getDate() + ",";
	// append long year
	
	today += (dDate.getYear()<2000) ? " " + (dDate.getYear()+1900) : " " + dDate.getYear();
		
	if ( whatToReturn == "year" ) {
	  document.write( (dDate.getYear()<2000) ? " " + (dDate.getYear()+1900) : " " + dDate.getYear() );
	}
	// yiu 2005-03-21
	// date for front page without "day of week"
   else if( whatToReturn == "onlyDate") {
      var onlyDate = "";
      // append short month
      onlyDate += tMons[dDate.getMonth()];
      // append date
      onlyDate += " " + dDate.getDate() + ",";
      // append long year
      onlyDate += (dDate.getYear()<2000) ? " " + (dDate.getYear()+1900) : " " + dDate.getYear();
      document.write( onlyDate );
   }
   else {	
	  document.write( today );
	}
}

// END Date Functions

// random number for doubleclick ord=[random]
function writeRand( maxNum, minNum ) {
   if ( !maxNum )
      maxNum = 900000;
   if ( !minNum )
      minNum = 100000;
   
   maxNum -= minNum;
   
   var rndOrd = Math.round( Math.random() * maxNum ) + minNum;
   document.write(rndOrd);
}

// random number for doubleclick ord=[random]
function getRand() {
   var rndOrd = globalRand;
   return rndOrd;
}

// quick-search directing  
function submitSearch() {
   if( document.forms['sj_search'].stype[1].checked ) {
      var target = "http://bizbuysell.com/cgi-bin/adsearch?ir=1&tab=eb&i=XX&s=XX&t=9&A=1&J=J";
      target += "&k=" + escape(document.sj_search.keywords.value);
      top.location.href=target;
   } else {
      document.forms['sj_search'].submit();
   }
}

// sidebar pop-up windows
function popSidebar(url) {
	var target = url;
	var winName = 'sjSidebar';
	var winParams = 'WIDTH=410,HEIGHT=430,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
	//var winParams = 'WIDTH=410,HEIGHT=430,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,NORESIZE';
   
   window.open( target, winName, winParams);
}

function popBigSidebar(url) {
	var target = url;
	var winName = 'sjBigSidebar';
	var winParams = 'WIDTH=750,HEIGHT=750,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
	//var winParams = 'WIDTH=750,HEIGHT=750,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,NORESIZE';
   
   window.open( target, winName, winParams);
}

// franchise pop-up windows
function popFranchise(url) {
	var target = '/franchise/companies/'+url;
	var winName = 'sjFranchise';
	var winParams = 'WIDTH=380,HEIGHT=360,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
	//var winParams = 'WIDTH=410,HEIGHT=430,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,NORESIZE';
   
   window.open( target, winName, winParams);
}

// franchise pop-up windows
function popFranchiseSpecial(url) {
	var target = '/franchisespecialreport/companies/'+url;
	var winName = 'sjFranchise';
	var winParams = 'WIDTH=380,HEIGHT=360,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
	//var winParams = 'WIDTH=410,HEIGHT=430,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,NORESIZE';
   
   window.open( target, winName, winParams);
}

// email pop-up window
function popEmail() {
	var target = '/forms/emailForm.asp?url='+escape(document.location);
	var winName = 'print';
	var winParams = 'WIDTH=440,HEIGHT=354,SCROLLBARS=no,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,NORESIZE';
	
	window.open( target, winName, winParams);
}

// print pop-up window
function popPrint() {
	var target = '/forms/printContent.asp?url='+escape(document.location);
	var winName = 'print';
	var winParams = 'WIDTH=660,SCROLLBARS=yes,TOOLBAR=1,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
	
	window.open( target, winName, winParams);
}

// content pop-up windows
function popContent(url,width,height) {
	var target = url;
   var w;
   var h;

   w = ( width ) ? width : 410;
   h = ( height ) ? height : 430;

	var winName = 'sjContent';
	var winParams = 'WIDTH='+w+',HEIGHT='+h+',SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
   
   window.open( target, winName, winParams);
}

function breakFrameset() {
   // ditch out if partner is including

   if ( location.hostname != "www.startupjournal.com" 
   &&  location.hostname != "startupjournal.com") {
      return;
   }

   if ( location.href.indexOf("mastercard_startup") != -1) {
      return;
   }

   if ( top.location.href != self.location.href ) {
      top.location.href = self.location.href;
   }
   return;
}

// redirect for discussions to fix caching
function openDiscussion(suffix) {
	var randNum;
   var url;
    
   randNum = Math.random()+"";
	randNum = randNum * 10000;
	url = "http://discussions.startupjournal.com/WebX?14@"+randNum+"@."+suffix+"/0";
	
   location.href = url;
}


// interactive guide pop-up windows
function popIguide(url) {
	var target = url;
	var winName = 'sjIguide';
	var winParams = 'WIDTH=800,HEIGHT=500,SCROLLBARS=yes,TOOLBAR=0,PERSONALBAR=0,STATUSBAR=1,RESIZABLE';
   
   window.open( target, winName, winParams);
}


// --------------------------------
// BEGIN exit popup code

var winCount = 0;
//var subdomain = "sbkverpub";
var subdomain = ".startupjournal.com";
var exitURL = "/exit.html";

// Execute event capture if we're in subdomain
// This will prevent outside partners from executing
/*if ( location.host.indexOf(subdomain) != -1 ) {
**   // for NS - onclick events only
**   if ( document.captureEvents ) {
**      document.captureEvents(Event.ONCLICK);
**      document.onclick = popExitLink;
**   }
**   // for IE - onclick and onunload
**   if ( document.all ) {
**      document.onclick = popExitLink;
**      window.onbeforeunload = popExitBrowser;
**   }
**}
*/

function popExitLink(evt){
   var e = (document.all) ? window.event.srcElement : evt.target;
   var hrefL = "";
   
   // see if we're handling an href
   if ( e.parentNode && e.parentNode.href ) {
      hrefL = e.parentNode.href;
   } else if ( e.href ) {
      hrefL = e.href;
   } else {
      if (document.all) {
         window.event.returnValue = true;
      }
      return true;
   }
		
	// if we're clicking external link then pop-up
   // but omit javascript funtions
   if ( hrefL.indexOf(subdomain) == -1 
   && hrefL.indexOf("javascript:") == -1 ) {
      exitPopup();
   }
   
   // IE needs to have returnval set to true
   if (document.all) {
      window.event.returnValue = true;
   }
   
   return true;
}

function popExitBrowser(evt){
   var e = (document.all) ? window.event : evt;
	
   // test to see if window is closing by 
   // measuring dimensions of active window
   var iX = window.document.body.offsetWidth - e.clientX;
   var iY = e.clientY;

   if ( iX <= 30 && iY <= 0 ) {
      //alert("Closing Window");
      exitPopup();
   }
	
   return;
}

function exitPopup() {
	var newWin;
	var Height   = 475;
	var Width    = 371;
	var Top      = 0;
	var Left     = 0;
	
	if ( winCount == 0) {
		var winOpts = 'toolbar=no,location=no,directories=no,' 
			+ 'status=no,scrollbars=auto,menubar=no,' 
			+ 'width=' + Width + ',height=' + Height 
			+ ',top=' + Top + ',left=' + Left;

		newWin = window.open(exitURL,'Advertisement',winOpts);
		winCount++;
	}
	
	return newWin;
}

// END exit popup code
// --------------------------------

