/********************
 *
 * Survey trigger for MI-approved surveys
 *
 * Contact: Scott Cale/New York/Contr/IBM
 *          Corporate Webmaster Team
 *
 * $Id: gsttrigger.js,v 1.6 2005/07/25 18:23:51 scale Exp $
 */

function ibmSurveyAllowsCookies(){
	if (typeof ibmCommonCookie == 'undefined') {
		ibmSurveyTest = 'common cookie failed to load';
		return false;           // cookie library did not load
	}

	var c = new ibmCommonCookie();
	var name = "msp";       // cookie name
	var timeout = 60;       // cookie ttl in seconds
	var maxCookies = 17;        // ignore users that have more than this many cookies

	if (c.getCookie(name)) {
		ibmSurveyTest = 'msp found';
		return false;       // cookie already exists
	}

	if (typeof(document.cookie) == 'string' && document.cookie && document.cookie.split) {
		if ((document.cookie.split('; ')).length > maxCookies) {
			ibmSurveyTest =  'too many cookies';
			return false;   // max cookies reached
		}
	}

	// set permantent cookie to verify that permanent cookies are enabled
	c.setCookie(name, 'enabled', timeout, '/', '.ibm.com');
	if (c.getCookie(name) == 'enabled') {
		c.deleteCookie(name, '/', '.ibm.com');
		return true;
	}
	else {
		ibmSurveyTest = 'cookies disabled';
		return false;
	}
}



//returns "ibm" if url is ibm.com domain or "remote" if otherwise valid
//returns false otherwise
function ibmSurveyIsValid(url){
	if( url == null ){
		return false;
	}

	if( url.match(/ibm.com(\.)?$/) ){
		return "ibm";
	} else {  //maybe a valid non-IBM host?
		allowed = new Array(
			"localhost",
			"notibm.com",
			"lotus.com",
			"ihost.com",
			"rational.com",
			"rational.net"
			);
		for (i in allowed) {
			if (url.indexOf(allowed[i]) != -1) {
				return "remote";
			}
		}
	}
	return false;
}//isValid


function ibmSurveyIsEligible(){
	if( navigator.userAgent.indexOf("Lotus-Notes") >= 0 ){
		return false; //See RSC#03902
	}

	if( ibmSurveyAllowsCookies() ){
		return true;
	}
		return false;
}//isEligible



function ibmSurveyTrigger(){  //hide variables and functions from global namespace
	var cgi = '/fcgi-bin/survey/surveyinvite.fcgi';
	//var cgi = '/cgi-bin/surveyinvite.fcgi';

	var remote = '/common/survey/popup-remote.html?';
	var host = window.location.hostname;

	var type = ibmSurveyIsValid(host.toString().toLowerCase());
	var myhref;
	var params;
	var server;

	if( (type == "ibm") || (type == "remote") ){
		//ok
	} else {
		return false;
	}


	if( ibmSurveyIsEligible() ){

		if( (typeof(remotehref) != "undefined") && (remotehref != null) ){
			myhref = remotehref; //if we're coming in from the iframe
		} else {
			myhref = window.location.href;
		}

		if( (typeof(myhref) != "undefined") && (myhref != null) ){
			// From protocol//host:port/pathname#anchor?parameter(s)
			// OR protocol//host:port/pathname?parameter(s)#anchor
			// To host:port/pathname?ibmSurveyOptions=value&parameter(s)
			myhref = unescape(myhref);
			var urlBits = myhref.split("?");

			for( i in urlBits ){
				if( urlBits[i] ){
					var pos = urlBits[i].indexOf("#");
					if( pos >= 0 ){
						urlBits[i] = urlBits[i].substr(0,pos);
					}
				}
			}

			myhref = urlBits[0] + "?";
			if( (typeof(ibmSurveyOptions) != "undefined") && (ibmSurveyOptions != null)){
				myhref += "ibmSurveyOptions=" + ibmSurveyOptions + "&";
			}
			if( urlBits[1] ){
				myhref += urlBits[1];
			}
			myhref = escape(myhref);
		}

		// Build params list (if we have any)
		// often: country language version module rate referer
		// Allows us to override parameters in cgi trigger so we're backwards compatible with old "popup.js" trigger. Also useful if we want to test a survey whose rate is set to zero

		params = '?a=a';  //"a" param not used by the cgis.  I use it here because some browsers
				  //  won't let me do a easch replace to add it later.  Other choise was
				  //  to check that the params list has some values when appending it
				  //  the path.

		if( (typeof(country)  != "undefined") && (country  != null) ){ params += '&cc='   + escape(country); }
		if( (typeof(language) != "undefined") && (language != null) ){ params += '&lc='   + escape(language); }
		if( (typeof(module)   != "undefined") && (module   != null) ){ params += '&mod='  + escape(module); }
		if( (typeof(rate)     != "undefined") && (rate     != null) ){ params += '&rate=' + escape(rate); }
		if( (typeof(referer)  != "undefined") && (referer  != null) ){ params += '&ref='  + escape(referer); }
		if( (typeof(site)     != "undefined") && (site     != null) ){ params += '&site=' + escape(site); }
		if( (typeof(version)  != "undefined") && (version  != null) ){ params += '&ver='  + escape(version); }

		if( (typeof(staging) != "undefined") && (staging == 'yes')){
			server = '//wwwstage.ibm.com';
		} else {
			server = '//www.pescasen.com';
		}

		if( type == "ibm" ){
			if( (typeof(myhref)   != "undefined") && (myhref     != null) ){
				params += '&myhref=' + escape(myhref);
			}

			//params = params.replace( /?&/, "?" ); //Doesn't work in all browsers
			//need to split-print the tags since some browsers don't like to render them.
			cgi = server + cgi + params;
			document.write('<scr', 'ipt src="'
				, cgi
				, '" type="text/javascript" language="JavaScript1.2"></scr', 'ipt>'
				);
			return true;

		} else if( type == "remote" ){
			//
			// Deprecated until we get P3P CP rolled out but we still want have
			//  it ready and working for that glorious day.
			//

			return false;

			//note different name of param
			if( (typeof(myhref)   != "undefined") && (myhref     != null) ){
				params += '&remotehref=' + escape(myhref);
			}
			//params = params.replace( /?&/, "?" ); //Doesn't work in all browsers
			remote = server + remote + params;
			document.write('<if', 'rame name="surveyframe" id="surveyframe" src="' +remote+ '"',
				'remote=yes" width="150" height="20" frameborder="0" scrolling="no" style="overflow: hidden;"></if', 'rame>'
				);
			return false; //since we want this script to stop now
		}
	}//if( isEligible() )
	return false;
}


//    } //namespace block

/** Main **/
if( ibmSurveyTrigger() ){
	// do nothing
} else {
	// do nothing
}
