function whichBrowser() {
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1) return 'Opera';
    if (agt.indexOf("staroffice") != -1) return 'Star Office';
    if (agt.indexOf("webtv") != -1) return 'WebTV';
    if (agt.indexOf("beonex") != -1) return 'Beonex';
    if (agt.indexOf("chimera") != -1) return 'Chimera';
    if (agt.indexOf("netpositive") != -1) return 'NetPositive';
    if (agt.indexOf("phoenix") != -1) return 'Phoenix';
    if (agt.indexOf("firefox") != -1) return 'Firefox';
    if (agt.indexOf("safari") != -1) return 'Safari';
    if (agt.indexOf("skipstone") != -1) return 'SkipStone';
    if (agt.indexOf("msie") != -1) return 'Internet Explorer';
    if (agt.indexOf("netscape") != -1) return 'Netscape';
    if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
    if (agt.indexOf('\/') != -1) {
        if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
            return navigator.userAgent.substr(0,agt.indexOf('\/'));
        } else {
            return 'Netscape';
        }
    } else if (agt.indexOf(' ') != -1) {
        return navigator.userAgent.substr(0,agt.indexOf(' '));
    } else {
        return navigator.userAgent;
    }
} 


// get the current URL
var url = window.location.toString();

//get the parameters
url.match(/\?(.+)$/);
var params = RegExp.$1;

 // split up the query string and store in an
 // associative array
var params = params.split("&");
var queryStringList = {};
var qryval;
for(var i=0;i<params.length;i++)
{
	var tmp = params[i].split("=");
 	queryStringList[i] = unescape(tmp[1]);
}


var mpt_flag = false;
if(queryStringList[0]){
	if(queryStringList[0] == 'mpt'){
		mpt_flag = true;
	}
}
function getMPTurl(){
	return url;
}
function isMPTUrl(){
	return mpt_flag;
}


/* Checking whether lightbox is opened or not. If opened closing the lightbox and reloading the parent window*/
if(self != top){
	//parent.location.reload(1);
	var f1 = parent.document.getElementById('TB_window');
	f1.style.display='none';
	if(parent.isMPTUrl()){
		//alert(parent.getMPTurl());
		//parent.location = parent.getMPTurl();
		parent.location = "https://commerce.paltalk.com/mpt/";
	}
	else{
		if(fetch_cookie("last_url") == "" || fetch_cookie("last_url") == null){
			parent.location.reload(1);
		}
		else{
			parent.location=fetch_cookie("last_url");
			delete_cookie("last_url");
		}
	}
	parent.closeLoginBox();
}


var timer1 = null,timer= null;
var t = null;
/* Getting Login Url required for lightbox */
var loginurl = null;

/* Getting Maximum session idle time from skin.wmt */
function activateSesionTracker(b_flag,i_t,s_url){
	chk_loginStatus = true;
	loginurl = s_url;
	//timer=setInterval("showPopup()",t);
	if(b_flag){
		t = i_t;
		timer=setTimeout("showPopup()",t);
	}
	else{
		clearTimeout(timer);
	}
}
// getMaxSessIdleTime();

/* Timer is set when only if user is logged in. */
/*if(isUserLoggedIn()){
	chk_loginStatus = true;
	//timer=setInterval("showPopup()",t);
	timer=setTimeout("showPopup()",t);

}*/

/*/* Getting Login Url required fro lightbox 
var loginurl = getLoginUrl();*/


/* Functions to trim string */
function ltrim(testStr) { 
   if (testStr == "") 
      return ""; 
   else { 
      var pattern = /[^\s]+.*/; 
      result = testStr.match(pattern); 
      return result[0]; 
   } 
} 
function rtrim(testStr) { 
   if (testStr == "") 
      return ""; 
   else { 
      var pattern = /.*[\S]/; 
      result = testStr.match(pattern); 
      return result[0]; 
   } 
} 

function trim(testStr) { 
   return rtrim(ltrim(testStr)); 
} 


var isClose = false;
function reset_interval()
{
	//clearInterval(timer1);
	//timer1 = null;
	if(isClose){
		resume();
		if(isAjaxCallReq()){
			ajaxFunction(getAjaxurl()+'true&date='+new Date(),'GET');
		}
	}
	timerreset()

}
var isReset = false;
function timerreset(){

	//timer = null;
	//clearInterval(timer);
	clearTimeout(timer);
	//timer=setInterval("showPopup()",t);
	timer=setTimeout("showPopup()",t);
}
var flag1 = true;
var isResume = false;
function resume(){
		isResume = true;
		isReset = true;
		isClose = false;
}
function user_logout(){
	flag1 = false;
	warnlogout_flag = true;
	if(isPopupOpened){
		popup.close();
		isPopupOpened = false;
	}
	createPopup();
	//document.getElementById('TB_window').className="TB_window_logout";	
	var ifr = "IF_"+new Date().getTime();
	document.getElementById('TB_window').innerHTML = "<iframe id='"+ifr+"' src='"+loginurl+"' frameborder='0' class='logout-notice'></iframe>";
	clearInterval(timer1);
	timer1 = setInterval("userLogout()",5000);
}
function createPopup(){

	var newdiv = document.createElement('div');
	var divIdName = "TB_window";
	newdiv.setAttribute('id',divIdName); 
	document.body.appendChild(newdiv); 
	var newdivoverlay = document.createElement('div');
	var divIdName = "TB_overlay";
	newdivoverlay.setAttribute('id',divIdName); 
	document.body.appendChild(newdivoverlay);
	/*var newIframe = document.createElement('iframe');
	newIframe.id = "IF_" +?new Date().getTime();
	newIframe.src = loginurl;
	newdiv.appendChild(newIframe);*/
	//newdiv.innerHTML = 'Test Div'; 
}

function showPopup()
{
	if(chk_loginStatus){
		isClose = true;
		timer = null;
		flag1 = false;
		//document.getElementById('TB_overlay_warn').className="TB_overlayBG";
		//document.getElementById('WarningBox').className="warning_box";
		j.jGrowl('<h4>You will be logged out of Paltalk</h4><p>Due to inactivity you will be logged out soon. To stay logged in please move your mouse or click on the site.</p>',{life:10000,theme:'growl-message',speed: 'slow',  beforeClose: function(e,m) {
		if(!isResume){
			isReset = false;
			inValidate();
			
		}
		isResume = false;
    }});
		//clearTimeout(timer);
		//timer= setTimeout("inValidate()",10000);
	}
}
var sessionExpired = false;
function isSessionExpired(){
	return sessionExpired;
}
function showLoginDiv(){
	return flag1;
}
function inValidate(){

if(!isReset){
	var myDate = new Date();
	myDate.setDate(myDate.getDate()+5);
	set_cookie('postLogoutMessage', 'User logged out due to inactivity.',myDate,'paltalk.com');
	clearInterval(timer);
	isClose=false;
	if(isAjaxCallReq()){
		ajaxFunction(getAjaxurl()+'false&date='+new Date(),'GET');
	}
	//document.getElementById('TB_overlay_warn').className="hide";
	//document.getElementById('WarningBox').className="hide";
	userLogout();
}
else{

	timerreset();
	isReset = false;
	return;
}
}


function ajaxFunction(val,type)
{
	var xmlHttp;
	url=val;
	try
  	{
  	// Firefox, Opera 8.0+, Safari
  	xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  	// Internet Explorer
  	try
    	{
   		 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  	catch (e)
    	{
    		try
      		{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
    		catch (e)
      		{
      			//alert("Your browser does not support AJAX!");
      		return false;
      		}
    	}
  	}
    xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
	 	var isSessionValid =  trim(xmlHttp.responseText);

		if(isSessionValid == 'true'){

			refreshIframe();
		}
		else 
		{
			//document.getElementById('TB_overlay_warn').className="hide";
			//document.getElementById('WarningBox').className="hide";
			userLogout();
		}
		xmlHttp = null;
      }
    }
  xmlHttp.open(type,url,true);
  xmlHttp.send(null);
  }

function userLogout(){
	clearInterval(timer);
	clearInterval(timer1);
	chk_loginStatus = false;
	document.logout.submit();
}
function closepopup(){
	if(isClose){
		reset_interval();
		isClose = false;
	}
}
var popup;
var isPopupOpened = false;
var warnlogout_flag = false;
function warnLogout(){
		flag1 = false;
		warnlogout_flag = true;
		if(isPopupOpened){
			popup.close();
			isPopupOpened = false;
		}
		showLogin();
		clearInterval(timer1);
		timer1 = setInterval("userLogout()",5000);
}
function isWarnLogout(){
	return warnlogout_flag;
}
var target="";
function getTarget(){	
	return target;
}

function offensivePopup(win){
	popup = win;
	isPopupOpened = true;
	//alert(popup);
}

function showLogin(val){
	var username =fetch_cookie('username');
	if ( username == null || username==''){
	if(fetch_cookie('loginrequrl') != null || fetch_cookie('loginrequrl') != ""){
		delete_cookie('loginrequrl','paltalk.com');
	}

	if(val){
	var myDate = new Date();
	myDate.setDate(myDate.getDate()+5);
		set_cookie('last_url',val,myDate);
		target = val;
	}
	else{
		target = "";
	}
	generateLoginBox();
	}
	else{
		var myDate = new Date();
		myDate.setDate(myDate.getDate()+5);
		set_cookie('chkUserLogin', 'true',myDate,'paltalk.com');
		this.location.reload(1);
	}

}

function generateLoginBox(){
	createPopup();
	var browserType = whichBrowser();

	var bg_loginbox =  document.getElementById('TB_overlay');

	bg_loginbox.className = "show"; 
	var loginbox = document.getElementById('TB_window');
	loginbox.className = "show";
	var ifr = "IF_"+new Date().getTime();
	loginbox.innerHTML = "<iframe id='"+ifr+"' src='"+loginurl+"' frameborder='0' class='login-window'></iframe>";	
	if(browserType!=null && browserType=='Internet Explorer'){
			j("body","html").css({height: "100%", width: "100%"});
			j("select","html").css({visibility: "hidden"});
			j("html").css("overflow","hidden");
	}
}
function refreshIframe(){
	var newdiv = document.createElement('div');
	var divIdName = "TB_window";
	newdiv.setAttribute('id',divIdName);
	newdiv.className="hide";  
	document.body.appendChild(newdiv); 
	var ifr = "IF_"+new Date().getTime();
	newdiv.innerHTML = "<iframe id='"+ifr+"' src='"+loginurl+"' frameborder='0' class='login-window'></iframe>";
	//newdiv.parentNode.removeChild(newdiv);
}
function closeLoginBox(){

	j("body","html").css({height: "auto", width: "auto"});
	j("select","html").css({visibility: "visible"});
	j("html").css("overflow","");
	//$("html").css("overflow","hide");

	timer = null;
	timer1 = null;	
	//var bg_loginbox =  document.getElementById('TB_overlay');
	//bg_loginbox.className = "hide";
	var loginbox = document.getElementById('TB_window');
    	loginbox .parentNode.removeChild(loginbox );
	var loginboxoverlay = document.getElementById('TB_overlay');
    	loginboxoverlay.parentNode.removeChild(loginboxoverlay);


	/*var f1 = document.getElementById('iFrame');
	f1.style.display='none';
	f1.src="";

	var loginbox = document.getElementById('TB_window');
	loginbox.className = "hide";
	loginbox.innerHTML = "";*/

}


