// Start cookie handling functions

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

function setCookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+"="+escape( value ) +
    ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// End cookie handling


// A little Web 2.0 for ya...
function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		ro = new XMLHttpRequest();
	}
	return ro;
}

// Functions to handle pulling in cobranded headers if applicable
var cobrand = createRequestObject();
var cobrand_firstpageonly = false;

function sendCobrandRequest() {
	var ref = document.referrer;
	if(cobrand_firstpageonly != true) {
		var refcookie = getCookie('HSWPartner');
		if(refcookie != "" && refcookie != null && refcookie != undefined) {
			ref = refcookie;
		}
	}
	// Be sure we have a valid partner so we don't make unnecessary HTTP calls
	var partner = getPartner(ref);
	if(partner == "" || partner == undefined || partner == null) {
		return;
	}
	if(cobrand_firstpageonly != true) {
		if(refcookie == "" || refcookie == null || refcookie == undefined) {
			setCookie('HSWPartner', ref, '', '/', '.howstuffworks.com');
		}
	}
	var cobrand_url = 'get-cobrand.php?partner='+partner;
	cobrand.open('get', cobrand_url);
	cobrand.onreadystatechange = handleResponse;
	cobrand.send(null);
}

function getPartner(ref) {
	if(ref.indexOf('www.forbestraveler.com') >= 0) {
		return 'forbestravel';
	} else {
		return '';
	}
}

function handleResponse() {
	switch(cobrand.readyState) {
		case 1:
		case 2:
		case 3:
			break;
		case 4:
			if(cobrand.responseText != '' && cobrand.responseText != null) {
				document.getElementById('coBrand').style.display='';
				document.getElementById('coBrandContents').innerHTML=cobrand.responseText;
			}
			break;
		default:
			break;
	}
}

// End cobrand code

// Function for in-page video player
function embedVideo(videofile) {
        var msg = '<iframe src="http://videos.howstuffworks.com/vid_inpage.php?pageID='+videofile+'" name="vidFrame" frameborder=0 width=420 hscroll=no vscroll=no height=410>Your Browser Does Not Support iFrames</iframe>';
        document.write(msg);
}
function embedVideoSmall(videofile) {
        var msg = '<iframe src="http://videos.howstuffworks.com/vid_inpage_s.php?pageID='+videofile+'" name="vidFrame" frameborder=0 width=225 hscroll=no vscroll=no height=195>Your Browser Does Not Support iFrames</iframe>';
        document.write(msg);
}

function inject_code(str) {
	document.write(str);
}

function browserAcceptsCookies() {
	var HSW_acceptsCookies = false;
	if (document.cookie == '') {
		document.cookie = 'HSW_acceptsCookies=yes';
		if (document.cookie.indexOf('HSW_acceptsCookies=yes') != -1) {
			HSW_acceptsCookies = true;
		}
	}
	else {
		HSW_acceptsCookies = true;
	}
	return (HSW_acceptsCookies);
}

function recordStats(webpage,referer) {
}

function tsStats(parms) {
  var _bs = '<img height=1 width=1 border=0 src="http://www.howstuffworks.com/tsform.php?'+parms+'">';
  document.write(_bs);
}

function OpenWindow(url) {
	newwindow = window.open ("/" + url, 'HowStuffWorks', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,width=485,height=675');
}

function OpenWindow2(url,h,w) {
	newwindow = window.open ("/" + url, 'HowStuffWorks', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=no,resizable=no,width=' + w + ',height=' + h);
}

var RN = new String (Math.random());
var RNS = RN.substring(2,11);

function DisplayAds (sitepage, position, width, height)
{
  var oas = 'http://ad.howstuffworks.com/RealMedia/ads/';
  var oaspage = sitepage + '/1' + RNS + '@' + position;
  if (haveSpider) browser_ok = 0;
  else            browser_ok = 1;

  if (browser_ok == 1) {
     if (_version < 11) {
       document.write ('<A HREF="' + oas + 'click_nx.ads/' + oaspage + '" TARGET="_top"><IMG SRC="' + oas + 'adstream_nx.ads/' + oaspage + '" BORDER="0" WIDTH="' + width + '" HEIGHT="' + height + '"></a>');
     } else {
       document.write ('<SCRIPT LANGUAGE="JavaScript1.1" SRC="' + oas + 'adstream_jx.ads/' + oaspage + '">');
       document.write ('\<\!-- --\>');
       document.write ('\<\/SCRIPT\>');
       document.write ('\<\!-- --\>');
     }
    }
}

function showImage(html)
{
  windowOps = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes," +
              "width=500,height=500,left=250,top=50";
  ImageWindow = window.open("","ImageWindow",windowOps);
  windowHTML = '<html><body><base href="http://www.howstuffworks.com/"><center><font face="arial,helvetica">' + html +
               '<p><font face="arial,helvetica">' +
               '<a href="javascript:window.close();">Click here</a> to close this window.</font></center>' +
               '</body></html>';
	self.ImageWindow.document.clear();
  self.ImageWindow.document.write(windowHTML);
	self.ImageWindow.focus();
	self.ImageWindow.document.close();
}

function tryIt(HTML)
{
  windowOps = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes," +
              "width=500,height=500,left=250,top=50";
  ImageWindow = window.open("","ImageWindow",windowOps);
  windowHTML = '<html><body>' + HTML +
               '<p><font face="arial,helvetica">' +
               '<A HREF="javascript:window.close();">Click here</a> to close this window.</font>' +
               '</body></html>';
  self.ImageWindow.document.clear();
  self.ImageWindow.document.write(windowHTML);
  self.ImageWindow.focus();
  self.ImageWindow.document.close();
}

function shopTI(url,lid) {
   if (url.indexOf("LINKIN_ID") == -1) {
      url="http://"+escape(url.substring(7))+"/LINKIN_ID-"+lid;
      }
   return url;
   }

function videoWin(id) {
    window.open('mediacenter/mcPlayerFull.php?vidID='+id,'mcPlayer','width=740,height=440,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes,resizable=yes');
}

