addLoadEvent(loadPage);

function judgePopUp(id,type)
{
    var leftval=(screen.width)?(screen.width-650)/2:100;
    var topval=(screen.height)?(screen.height-350)/2:100;
    var popwin=window.open("/judge.php?id=" + id + "&table=" + type, "JudgePopup", "width=650,height=300,top=" + topval + ",left=" + leftval + ",toolbar=0,scrollbars=no,directories=no,location=0,statusbar=0,menubar=0,resizable=0");
    popwin.focus();
}

function addEngine(name,ext,cat,pid)
{
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
    window.sidebar.addSearchEngine(
      "http://mycroft.mozdev.org/install.php/" + pid + "/" + name + ".src",
      "http://mycroft.mozdev.org/install.php/" + pid + "/" + name + "."+ ext, name, cat );
  } else {
    alert("You will need a browser which supports Sherlock to install this plugin.");
  }
}

function addOpenSearch(name,ext,cat,pid,meth)
{
  if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
    // See bugs 430058/430067/430070 for Camino
    if (((typeof window.external.AddSearchProvider == "unknown") || (window.navigator.vendor == 'Camino'))&& meth == "p") {
      alert("This plugin uses POST which is not currently supported by your browser's implementation of OpenSearch.");
    } else {
      window.external.AddSearchProvider(
        "http://mycroft.mozdev.org/installos.php/" + pid + "/" + name + ".xml");
    }
  } else {
    alert("You will need a browser which supports OpenSearch to install this plugin.");
  }
}
function addPlugin(){
	if(document.frmSearchPlugin.ff_chatroom.checked || document.frmSearchPlugin.ie_chatroom.checked){
		addOpenSearch('paltalk-groups','gif','','35463','g');
	}
	if(document.frmSearchPlugin.ff_people.checked || document.frmSearchPlugin.ie_people.checked){
		addOpenSearch('paltalk-people','gif','','35462','g');
	}
}
function validateForm(){
	//alert('validating');
	setPluginInstallStatus();

	var browserType = whichBrowser();
	if((browserType!=null && browserType=='Firefox' && !document.frmSearchPlugin.ff_chatroom.checked &&  !document.frmSearchPlugin.ff_people.checked) || 
	   (browserType!=null && browserType=='Internet Explorer' && !document.frmSearchPlugin.ie_chatroom.checked &&  !document.frmSearchPlugin.ie_people.checked) ){
		alert("please select atleast one search plugin to install");
		return false;
	}else{
		addPlugin();
	}
}
function loadPage(){
	//alert('loading');
	var browserType = whichBrowser();
	if(browserType!=null && browserType=='Firefox'){
		document.frmSearchPlugin.ff_chatroom.disabled=false;
		document.frmSearchPlugin.ff_people.disabled=false;
		document.getElementById('ff_plugin').className = 'choose-plugin show';
		document.getElementById('ff_screenshot').className = 'screenshot show';	
		document.getElementById('ff_note').className = 'note show';	
	}
	if(browserType!=null && browserType=='Internet Explorer'){
		document.frmSearchPlugin.ie_chatroom.disabled=false;
		document.frmSearchPlugin.ie_people.disabled=false;
		document.getElementById('ie_plugin').className = 'choose-plugin show';
		document.getElementById('ie_screenshot').className = 'screenshot show';	
		document.getElementById('ie_note').className = 'note show';			
	}
}

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;
    }
}

function setPluginInstallStatus(){
	var expDate = new Date();
	expDate.setDate(expDate.getDate()+7);

	var browser = whichBrowser();
	if(browser!=null && browser=='Firefox'){
		if(document.frmSearchPlugin.ff_chatroom.checked){
			set_cookie_domain('ffGrpsPluginInstalled','true',expDate);
		}

		if(document.frmSearchPlugin.ff_people.checked){
			set_cookie_domain('ffProfPluginInstalled','true',expDate);
		}
	}
	
	if(browser!=null && browser=='Internet Explorer'){
		if(document.frmSearchPlugin.ie_chatroom.checked){
			set_cookie_domain('ieGrpsPluginInstalled','true',expDate);
		}
		
		if(document.frmSearchPlugin.ie_people.checked){
			set_cookie_domain('ieProfPluginInstalled','true',expDate);
		}
	
	}

}

function set_cookie_domain(name, value, expires)
{	
	var domain =".paltalk.com";
	domainString = '; domain='+domain;
	if (!expires)
	{
		expires = new Date();
	}
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/"+domainString;
}
// #############################################################################
// function to set a domain cookie


function fetch_cookie(name)
{
	cookie_name = name + "=";
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (";", value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}
