var IsActiveScriptToClient=1; //1- Active, 0 - InActive

//------------ Starting Cookie ------------------
///////////// Start Appp Setttings ////////////////
	    // cookie info
		var cookieName = "chkSurveyInvite09";
		var cookieDuration = "30"; // in days
 
        var surveyLocation="http://www.iresearchondemand.net/html.pro?ID=31&site=09&QREFURL=[ALPHA]&pcid=ir";
				    		
		
		var IsOnline = "y"; // y / n
		
		var SurveyScriptVersion = "invitesurvey09.js";

 
//--------------------------------------------------------------------------
//  END of User-maintainable parameters
//--------------------------------------------------------------------------

// replace cookie_name with the real cookie name, '' are required
function Get_Cookie(check_name)
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		// return null;
		return "";
	}
}


//------------ Ends Cookie ------------------

// survey url (relative or absolute)     
//var surveyLocation="http://http://www.iresearchondemand.net/html.pro?ID=31&refurl=";

var surveyLocation="http://www.iresearchondemand.net/html.pro?ID=31&site=09&QREFURL=[ALPHA]&pcid=ir";

// Bind the ref url
var CurWeb= document.location.href;
CurWeb = CurWeb.replace("http://","");

surveyLocation = surveyLocation + CurWeb;
surveyLocation = surveyLocation.replace("http://","");
	   
var serviceFlag  = true;

var serviceRPath = location.pathname ? location.pathname : "";
var serviceRHost = location.host ? location.host : "";
var serviceRSea  = location.search ? location.search.substring(1,location.search.length) : "";
var serviceRUri  = document.referrer ? document.referrer.replace('http://','') : "";
var serviceWin   = null;
var serviceProt  = location.protocol == 'https:' ? 'https://secured' : 'http://static';

function RunService()
{

   //surveyLocation  += ';REF=' + serviceRUri + ';DOM=' + serviceRHost + ';PAT=' + serviceRPath + ';SEA=' + serviceRSea;

  // alert("Flag = "+serviceFlag);

   if (serviceFlag && IsActiveScriptToClient==1)
   {
      var l = window.screenX ? (window.screenX+window.outerWidth -200)  : 10000;
      var t = window.screenY ? (window.screenY+window.outerHeight-200)  : 1;

      // serviceWin = open('','hide','top='+t+',left='+l+',width=120,height=130,resizable,scrollbars=auto');
      
      var ileft = 0;
      var itop = 0;
    var iw = 120;
    var ih = 130;


  serviceWin = window.open ('', 'hide', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width='+iw+', height='+ih+', top='+itop+', left='+ileft);


         l = window.screenLeft ? (window.screenLeft + Math.floor( document.body.clientWidth/4 ) ) :
             window.screenX    ? (window.screenX+ Math.floor( window.outerWidth/4  ) )            : 800;
         t = window.screenTop  ? (window.screenTop + Math.floor( document.body.clientHeight/4 ) ) :
             window.screenY    ? (window.screenY+Math.floor( window.outerHeight/4 ) )             : 800;

      self.focus();
      serviceWin.document.open();
      
      
var WindowContent = "";
            
WindowContent += "<html>";

WindowContent += "<title>iResearch Survey Invitation</title>";

// Add in Head save response js

WindowContent += "<head>";

var website = self.location.href;
website = website.replace("http://",""); // Because of url rewtite its not working


WindowContent += "</head>";

WindowContent += '<body style="background-color:#FFFFFF;margin:0;padding:0">';

    var SavePathIrsPath = "";
         
		 if(IsOnline == "n")
		 {
		 	SavePathIrsPath = "irssaveresponse.js";
		 }
		 else
		 {
			// Online 
			SavePathIrsPath = "http://www.iresearch.com/invite/irssaveresponse.js";
		 }

		WindowContent +=' <script language="javascript" src="'+ SavePathIrsPath +'"></script> ';
		
		
		WindowContent +=' 		<table id="divSurveyPop" width="100%" border="0" cellspacing="0" cellpadding="0" style="position:absolute; top:20%;display:none;">';
		WindowContent +='   		<tr>';
		WindowContent +='     	<td align="center">';
		WindowContent +='     <div style="border: 5px solid rgb(14, 129, 196);background-color: rgb(223, 239, 255); height:324px; width:925px;">';
		WindowContent +=' 	<img height="324" border="0" width="925" usemap="#Map" alt="Survey Invitation" src="http://www.iresearch.com/invite/invitesurvey09.jpg">';
		
		
		WindowContent +='<map name="Map" id="Map">';
		
		
		
		WindowContent +='<area shape="rect" coords="569,109,791,139" alt="Yes, I will participate in the survey" onClick="javascript:IRS_SaveResponse(\''+SurveyScriptVersion+'\',\'YES\');" href="javascript://"  />';
WindowContent +='<area shape="rect" coords="803,111,917,140" alt="No thanks" onClick="javascript:IRS_SaveResponse(\''+SurveyScriptVersion+'\',\'NO\');" href="javascript://"   />';
WindowContent +='<area shape="rect" coords="600,145,758,163" href="javascript:openPolicy(\'FR\');" alt="Privacy Policy"/>';
WindowContent +='<area shape="rect" coords="806,146,917,164" href="javascript://" onClick="javascript:IRS_SaveResponse(\''+SurveyScriptVersion+'\',\'MAYBELATER\');" alt="Maybe later"/>';


WindowContent +='<area shape="rect" coords="568,224,789,255" alt="Yes, I will participate in the survey" onClick="javascript:IRS_SaveResponse(\''+SurveyScriptVersion+'\',\'YES\');" href="javascript://"  />';

WindowContent +='<area shape="rect" coords="804,224,917,252" alt="No thanks" onClick="javascript:IRS_SaveResponse(\''+SurveyScriptVersion+'\',\'NO\');" href="javascript://"   />';
WindowContent +='<area shape="rect" coords="637,260,721,277" href="javascript:openPolicy(\'EN\');" alt="Privacy Policy"/>';
WindowContent +='<area shape="rect" coords="829,258,900,278" href="javascript://" onClick="javascript:IRS_SaveResponse(\''+SurveyScriptVersion+'\',\'MAYBELATER\');" alt="Maybe later"/>';

		
		
		
		WindowContent +='</map>';	
		WindowContent +=' 	</div>';
		WindowContent +=' 	</td>';
		WindowContent +='   </tr>';
		WindowContent +=' </table>';
















WindowContent += "<scr"+"ipt>";

WindowContent +='window.opener ? window.opener.focus() : self.blur();';
WindowContent +='var surveyLocation;';
WindowContent +='var serviceWin = null;';
WindowContent +='var w = 1000;';
WindowContent +='var h = 600;';

WindowContent +='function RunService() {';
//WindowContent +='    alert("width = "+w);';
//WindowContent +=' alert("Running service");  ';
WindowContent +='    self.moveTo(0, 0);';
WindowContent +='    self.resizeTo(w,h);';

WindowContent +=' self.focus();';

WindowContent +='  document.getElementById("divSurveyPop").style.display=""; ';
WindowContent +='  document.getElementById("divStartIMG").style.display="none"; ';

WindowContent +='};';

WindowContent +='function parentExists()';
WindowContent +='{ ';

WindowContent +=' if(window.opener && !window.opener.closed) { ';
  WindowContent +=' return true; ';
WindowContent +=' } else { return false; } ';


WindowContent +='}';


WindowContent +='function CheckDomain() {';
WindowContent +='    try {';
WindowContent +='        var oDom = opener.document.domain;';

WindowContent +='        var IsParentCheck = parentExists(); ';

//WindowContent +='        alert("Domain is = "+oDom); ';
//WindowContent +='        alert("IS PARENT = "+IsParentCheck); ';

WindowContent +='  if(IsParentCheck == false) { RunService(); }  ';



WindowContent +='        CloseMe();';
WindowContent +='        ct = setTimeout("CheckDomain()", 500);';
WindowContent +='    } catch (e) {';
//WindowContent +='  alert("WINDOW NOT FOUND");';
WindowContent +='        RunService(); '; 

WindowContent +='    }';
WindowContent +='};';

WindowContent +='function CloseMe() {';
    //WindowContent +='  alert("test timer"); ';
WindowContent +='    if (opener.StopService) opener.StopService(this);';
WindowContent +='    if (opener.ftgSurveySuppress) this.close();';
WindowContent +='};';
WindowContent +='var ct = setTimeout("CheckDomain()", 500);';
WindowContent += "</scr"+"ipt>";
WindowContent += "<div id='divStartIMG'><a href='JavaScript:RunService()'><img src='http://www.iresearch.com/invite/SurveyStart.jpg' border='0'></a>";

var asyckUrl = "";
 if(IsOnline == "n")
 {
	 // Local
	 asyckUrl = "irssavepopup.php?";
 }
 else
 {
	// Online 
	asyckUrl = "http://www.iresearch.com/invite/irssavepopup.php?";
 }
 // Bind the version
 asyckUrl = asyckUrl + 'v='+SurveyScriptVersion+"&w="+website;
		
WindowContent += "<iframe width='0' height='0' scrolling='no' frameborder='0' src='"+asyckUrl+"'></iframe></div>";


WindowContent += '</body>';
WindowContent += '</html>';


serviceWin.document.write(WindowContent);

      serviceWin.document.close();
      serviceWin.surveyLocation = surveyLocation;
      
      /*serviceWin.w = screen.width;
      serviceWin.h = screen.height;*/
	  
	//  serviceWin.w = screen.availWidth;
     //  serviceWin.h = screen.availHeight;
	  
	serviceWin.w = 1000;
      serviceWin.h = 600;

	  
var calcleft = (screen.width/2)-(serviceWin.w/2);
var calctop = (screen.height/2)-(serviceWin.h/2);

l = calcleft;
t = calctop;
 
      serviceWin.l = l;
      serviceWin.t = t;


      serviceWin.moveTo(l,t);
      self.focus();
   }
}

function StopService()
{
   serviceFlag = false;
}

//////////////////////////////////////////--------------- Start CHECK COOKIES AND SET EVENTS HANDLLER --------------------------------------------
function checkStatusQueryString(queryparameter)
{
	var FoundCount = 0;
	
	var qrStr = window.location.search;
	var spQrStr = qrStr.substring(1);
	var arrQrStr = new Array();
	// splits each of pair
	var arr = spQrStr.split('&');
	
	for (var i=0;i<arr.length;i++)
	{
	// splits each of field-value pair
	var index = arr[i].indexOf('=');
	var key = arr[i].substring(0,index);
	var val = arr[i].substring(index+1);
	
	//alert("key = "+key+" Value = "+val);
	
	// saves each of field-value pair in an array variable
	 arrQrStr[key] = val;
	 if(key == queryparameter)
	 {
	 	FoundCount = 1;
		break;
	 }
	
	}

	return FoundCount;
	
}

var LastSurveyPlayCode = Get_Cookie(cookieName);

///// ----- Start to check is user is completed survey or not | Include Code this here only --------------

var statusCheckJSFile = "";
if(IsOnline == "n")
{
	statusCheckJSFile = "saveSurveyStatus.js";
}
else
{
	statusCheckJSFile = "http://www.iresearch.com/invite/saveSurveyStatus.js";
}
document.write('<script type="text/javascript" src="'+ statusCheckJSFile + '"></scr'+'ipt>'); 

///// ----- ENDS to check is user is completed survey or not --------------

//alert("Last Survey Play Code = "+LastSurveyPlayCode);

//alert("Check Code = "+LastSurveyPlayCode);



var TotalFoundStatusParams = checkStatusQueryString("irstatus");

// alert("irstatus is present count = "+TotalFoundStatusParams);

if(TotalFoundStatusParams == 1)
{
		LastSurveyPlayCode = "1";
}

function RunServiceOnClick()
{
   if(LastSurveyPlayCode == "") 
   {
       RunService();
       StopService();
   }
   
}

if(LastSurveyPlayCode == "") 
{
	//	alert('Set click ');
//if (navigator.appName == "Microsoft Internet Explorer" && parseFloat(navigator.appVersion.substring(21,50)) >= 5.5)
document.onclick = RunServiceOnClick;
window.onunload = RunService;
}
else
{
    // alert("Cookie already recorded");
}

//////////////////////////////////////////--------------- Ends CHECK COOKIES AND SET EVENTS HANDLLER --------------------------------------------