//=============================================================
//=== __CGlobal
//=============================================================
var g_global = new __CGlobal();
function __CGlobal()
{
	this.guid_DialogWindow = "G_CustomDialogWindow";
}
//............................
//... EnableDisableButton
//............................
__CGlobal.prototype.EnableDisableButton=function(_button,_isEnabled)
{
	_button.disabled = _isEnabled!=true;
	try
	{
		var a = _button.getElementsByTagName("IMG");//.childNodes;
		for(var j=0; j<a.length; j++)
		{
			var o=a[j];
			if (o.getAttribute("c_initial_src")==null)
				o.setAttribute("c_initial_src", o.src);
			o.src = _isEnabled ? o.getAttribute("c_initial_src") : "/images/16x16/UncheckDisabled.gif";
			//o.style.filter = _isEnabled ? "" : "alpha(opacity=20)";
		}
	}catch(e){}
};
//=============================================================
//=== __CCookies
//=============================================================
var g_cookies = new __CCookies();
function __CCookies()
{
}
__CCookies.prototype.Set=function(sName,sValue)
{
  date = new Date(2100,1,1);
  document.cookie = sName + "=" + escape(sValue) + "; path=/; expires=" + date.toString();
};
__CCookies.prototype.Get=function(sName)
{
	//...cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		//...a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
			return unescape(aCrumb[1]);
	}
	//...a cookie with the requested name does not exist
	return null;
};
__CCookies.prototype.Delete=function(sName)
{
	document.cookie = sName + "=; path=/; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
};


//=============================================================
//=== __CRWTA_Survey - main object for handling 'unload' event and providing evaluation survey
//=============================================================
var g_rwta;// = new __CRWTA_Survey();
//var g_rwta_resetStatus = false;
var g_const_cookie_LastVisitDate	= "rwtaLastVisitDate";
var g_const_cookie_LastVisitPage	= "rwtaLastVisitPage";
var g_const_cookie_TotalPagesSeen	= "rwtaPagesSeenDuringSession";
var g_const_cookie_ResponseStatus	= "rwtaResponseStatus";
var g_const_cookie_ResponseDate		= "rwtaResponseDate";
function __CRWTA_Survey()
{
	this.beforeUnloadOccured = false;
	this.isUserEligibleForSurvey = false;
	
	var _respStatus = this.GetResponseStatus();
	if (_respStatus==null) _respStatus="1"; 
		
	var _minsMilli	= 1000*60;			//...miliseconds in minute
	var _dayMilli	= 1000*60*60*24;	//...miliseconds in day
	var dtNow = new Date();
	var dtLast = dtNow;
	var alreadyVisited = true;
	try
	{ 
		dtLast = new Date(this.GetLastVisitDate());
	}
	catch(e)
	{
		alreadyVisited = false;
		_respStatus="1"; 
	}
	var mins = Math.round( (dtNow.getTime() - dtLast.getTime()) /_minsMilli);
	var days = Math.round( (dtNow.getTime() - dtLast.getTime()) /_dayMilli);
	var num = 1;
	if (mins <= 20 && alreadyVisited)
	{
		try{ num = 1*this.GetTotalPagesSeen() + 1;}catch(e){}
	}
	
	//...check rules and see if user is eligible for survey
	var r = g_survey_rules;
	var ok = true;
	if (_respStatus==1)
	{
		ok = r.SurveyWasNeverOffered_Offer;
	}
	if (_respStatus==2)			//...responded "Yes" and submitted more than N days ago
	{
		if (r.RespondedYes == g_const_survey_DoNotOffer)
			ok = false;
		else
		{
			try
			{
				var dtLast2 = new Date(this.GetResponseDate());
				var days2 = Math.round( (dtNow.getTime() - dtLast2.getTime()) /_dayMilli);
				ok = days2 >= r.RespondedYes_OfferIn;
			}
			catch(e)
			{
				ok = false;
			}
		}
	}
	else if (_respStatus==3)	//...responded "No, remind me later" N day(s) or more ago
	{
		ok = r.RespondedNo_remind;
		if (ok)
			ok = days >= r.RespondedNo_remind_in;
	}
	else if (_respStatus==4)	//...responded "No, and don't ask me again" N days or more ago
	{
		if (r.RespondedNo_dontask == g_const_survey_DoNotOffer)
			ok = false;
		else
			ok = days >= r.RespondedNo_dontask_in;
	}
	else if (_respStatus==5)	//...started survey but then clicked Cancel button
	{
		ok = true;
	}
	this.isUserEligibleForSurvey = ok;

	this.SetLastVisitDate(dtNow.toUTCString());
	this.SetLastVisitPage(window.location.href);
	this.SetTotalPagesSeen(num);
	this.SetResponseStatus(_respStatus);
		
	//g_cookies.Set(this.cookieLastVisitDate,	dt.toUTCString());
};
//...Get's
__CRWTA_Survey.prototype.GetLastVisitDate	= function(){	return g_cookies.Get(g_const_cookie_LastVisitDate); };
__CRWTA_Survey.prototype.GetLastVisitPage	= function(){	return g_cookies.Get(g_const_cookie_LastVisitPage);};
__CRWTA_Survey.prototype.GetTotalPagesSeen	= function(){	return g_cookies.Get(g_const_cookie_TotalPagesSeen);};
__CRWTA_Survey.prototype.GetResponseStatus	= function(){	return g_cookies.Get(g_const_cookie_ResponseStatus);};
__CRWTA_Survey.prototype.GetResponseDate	= function(){	return g_cookies.Get(g_const_cookie_ResponseDate);};
//...Set's
__CRWTA_Survey.prototype.SetLastVisitDate	=function(_val){g_cookies.Set(g_const_cookie_LastVisitDate, _val);};
__CRWTA_Survey.prototype.SetLastVisitPage	=function(_val){g_cookies.Set(g_const_cookie_LastVisitPage, _val);};
__CRWTA_Survey.prototype.SetTotalPagesSeen	=function(_val){g_cookies.Set(g_const_cookie_TotalPagesSeen,_val);};
__CRWTA_Survey.prototype.SetResponseStatus	=function(_val){g_cookies.Set(g_const_cookie_ResponseStatus,_val);};
__CRWTA_Survey.prototype.SetResponseDate	=function(_val){g_cookies.Set(g_const_cookie_ResponseDate,	_val);};
__CRWTA_Survey.prototype.CheckToAskForSurvey=function(){
//alert(g_rwta.isUserEligibleForSurvey);
	var ok = g_rwta.isUserEligibleForSurvey;
	if (ok)
	{
		var r = g_survey_rules;
		var num = 1*this.GetTotalPagesSeen();
//alert(num);
		//if (r.OfferSurveyOn
		ok = num >= r.OfferSurveyAfter;
	}
	
	if (ok)
	{	
		if (confirm(g_survey_messages.MainQuestion))
		{
			var isIE = false;
			try
			{
				//var agent = window.navigator.userAgent.toUpperCase();
				isIE = window.clientInformation.userAgent.toUpperCase().indexOf( "MSIE " ) > 0;
				//var app = window.navigator.appName.toUpperCase();
				//isIE = app.indexOf("MICROSOFT")!=-1 && app.indexOf("EXPLORER")!=-1; //agent.indexOf("MSIE")!=-1
			}catch(e){}
			
			if (isIE)
			{
				var url = "/Surveys/TakeSurvey.htm?caller=" + window.location.href;
				window.showModalDialog(url, null, "dialogWidth:800px; dialogHeight:640px; center:yes; edge:raised; scroll:auto; status:yes; resizable:no;");
			}
			else //...if (agent.indexOf("FIREFOX")!=-1)
			{
				var url = "/Surveys/TakeSurvey.aspx?caller=" + window.location.href;
				var w = window.open(url, "_blank", "width=800, height=640, location=no, menubar=no, directories=no, toolbar=no, status=no");
				try{w.focus();}catch(e){}
				try{w.click();}catch(e){}
			}
		}
		else
		{
			var status;
			if (confirm(g_survey_messages.QstOnCancelMainQuestion))
				status = 3;
			else
				status = 4;

			g_rwta.SetResponseStatus(status);
			try
			{
				var url = "/Surveys/SetStatus.aspx?status="+status+"&caller="+window.location.href;
				var ifr = document.createElement("IFRAME");
				document.body.insertBefore(ifr,document.body.childNodes[0]);
				ifr.style.width = "1";
				ifr.style.height = "1";
				ifr.style.display = "none";
				ifr.src = url;
			}
			catch(e)
			{
alert(e.message);
			}

			//window.open(url, "_blank", "width=50, height=50, location=no, menubar=no, directories=no, toolbar=no, status=no");
		}
	}
}

//-------------------------------------------------------------
//--- Window handlers - onload, onunload
//-------------------------------------------------------------
function rwta_window_onload()
{
	var ss = window.location.href;
	var j = ss.indexOf("?");
	if (j != -1)
	{
		ss = ss.substr(j+1);
//alert(ss);
		var a=ss.split("&");
		for(var j=0; j<a.length; j++)
		{
			var a2=a[j].split("=");
			if (a2[0].toUpperCase() == "resetResponseStatus".toUpperCase())
			{
				if (a2[1].toLowerCase() == "true")
				{
					g_cookies.Set(g_const_cookie_ResponseStatus,1);
					//g_rwta_resetStatus = true;
					alert("Survey Response Status has been reseted to 'Survey was Never Offered' state.");
					break;
				}
			}
		}
	}


	g_rwta = new __CRWTA_Survey();


//g_rwta.SetResponseStatus(1);
//var agent = window.navigator.userAgent.toUpperCase();
//alert(agent);
	
//alert(g_rwta.GetResponseStatus());

/*
var app = window.navigator.appName.toUpperCase();
var isIE = app.indexOf("MICROSOFT")!=-1 && app.indexOf("EXPLORER")!=-1;
alert(g_rwta.GetResponseStatus() + 
"\n isIE = " + isIE + 
"\n appCodeName = " + window.navigator.appCodeName +
"\n appName     = " + window.navigator.appName +
"\n appVersion  = " + window.navigator.appVersion +
"\n userAgent   = " + window.navigator.userAgent
);
*/

	if (window.navigator.cookieEnabled)
	{
		window.onunload	= rwta_window_onunload;
		try{window.onbeforeunload = rwta_window_beforeunload;}catch(e){}
	}
}
function rwta_window_onunload()
{
	if (g_rwta.beforeUnloadOccured == false) g_rwta.CheckToAskForSurvey();
}
function rwta_window_beforeunload()
{
	g_rwta.beforeUnloadOccured = true;
	g_rwta.CheckToAskForSurvey();
}

