function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function disablePopup(divName){  
	//disables popup only if it is enabled   
	$("#backgroundPopup").fadeOut("slow");  
	$("#" + divName).fadeOut("slow");
}

function isValidEmailAddress(emailAddress) {
 var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 return pattern.test(emailAddress);
}

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function jsguid() {
   return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
}

function convertUnixToTime(timestamp)
{	
	var dateObj = new Date(timestamp * 1000); 	
	var newTime = dateObj.toLocaleTimeString();
	
	newTime = newTime.substring(0,5);
	return newTime;
}

function checkForSmiley(data)
{
	var smileyArray = newArray();
	smileyArray[0] = ":)";
	smileyArray[1] = ":-)";
	smileyArray[2] = ":d";
	smileyArray[3] = ":-d";
	smileyArray[4] = ":p";
	smileyArray[5] = ":-p";
	smileyArray[6] = ":(";
	smileyArray[7] = ":-(";
	smileyArray[8] = ";)";
	smileyArray[9] = ";-)";
	
}

function convertCommand(data)
{
	var regext = new RegExp(/^![\S]*/);
	var term;
	var command;
	var text = "";
	
	if(data.match(regext))
	{
		command = data.match(regext);
		term = data.split(regext);
		term = term[1].substr(1);
		text = convertText(command, term, data);
	} else {
		text = data;
	}
	return text;
}

function convertText(command, term, text)
{
	var newText = "";
	term = term.replace(" ", "%20");
	
	if(command == "!search")
	{
		newText = text + " - http://www.google.com/search?q=" + term;
	} else if (command == "!image")
	{
		newText = text + " - http://images.google.com/images?q=" + term;
	}
	else if (command == "!map")
	{
		newText = text + " - http://maps.google.com/maps?q=" + term;
	}
	else if (command == "!video")
	{
		newText = text + " - http://uk.youtube.com/results?search_query=" + term;
	}
	else
	{
		newText = text;
	}
	
	return newText;
}

function fetch_unix_timestamp()
{
	return parseInt(new Date().getTime().toString().substring(0, 10))
}

function convertSmileys(data)
{

	data = data.replace(/:\)/g, "<img src='cbicons/smileys/smile_esk.png' class='smiley' border=0>");
	data = data.replace(/:-\)/g, "<img src='cbicons/smileys/smile_esk.png' class='smiley' border=0>");
	data = data.replace(/:0\)/g, "<img src='cbicons/smileys/smile_esk.png' class='smiley' border=0>");
	data = data.replace(/:o\)/g, "<img src='cbicons/smileys/smile_esk.png' class='smiley' border=0>");
	data = data.replace(/:d/ig, "<img src='cbicons/smileys/grin_esk.png' class='smiley' border=0>");
	data = data.replace(/:-d/ig, "<img src='cbicons/smileys/grin_esk.png' class='smiley' border=0>");
	data = data.replace(/:0d/ig, "<img src='cbicons/smileys/grin_esk.png' class='smiley' border=0>");
	data = data.replace(/:od/ig, "<img src='cbicons/smileys/grin_esk.png' class='smiley' border=0>");
	data = data.replace(/;\)/g, "<img src='cbicons/smileys/wink_esk.png' class='smiley' border=0>");
	data = data.replace(/;-\)/g, "<img src='cbicons/smileys/wink_esk.png' class='smiley' border=0>");
	data = data.replace(/;0\)/g, "<img src='cbicons/smileys/wink_esk.png' class='smiley' border=0>");
	data = data.replace(/;o\)/g, "<img src='cbicons/smileys/wink_esk.png' class='smiley' border=0>");
	data = data.replace(/:\(/g, "<img src='cbicons/smileys/sad_esk.png' class='smiley' border=0>");
	data = data.replace(/:-\(/g, "<img src='cbicons/smileys/sad_esk.png' class='smiley' border=0>");
	data = data.replace(/:0\(/g, "<img src='cbicons/smileys/sad_esk.png' class='smiley' border=0>");
	data = data.replace(/:o\(/g, "<img src='cbicons/smileys/sad_esk.png' class='smiley' border=0>");
	data = data.replace(/:p/ig, "<img src='cbicons/smileys/tongue_esk.png' class='smiley' border=0>");
	data = data.replace(/:-p/ig, "<img src='cbicons/smileys/tongue_esk.png' class='smiley' border=0>");
	data = data.replace(/:0p/ig, "<img src='cbicons/smileys/tongue_esk.png' class='smiley' border=0>");
	data = data.replace(/:op/ig, "<img src='cbicons/smileys/tongue_esk.png' class='smiley' border=0>");
	data = data.replace(/;p/ig, "<img src='cbicons/smileys/winkTongue_esk.png' class='smiley' border=0>");
	data = data.replace(/;-p/ig, "<img src='cbicons/smileys/winkTongue_esk.png' class='smiley' border=0>");
	data = data.replace(/;0p/ig, "<img src='cbicons/smileys/winkTongue_esk.png' class='smiley' border=0>");
	data = data.replace(/;op/ig, "<img src='cbicons/smileys/winkTongue_esk.png' class='smiley' border=0>");
	data = data.replace(/:o/ig, "<img src='cbicons/smileys/shocked_esk.png' class='smiley' border=0>");
	data = data.replace(/:-o/ig, "<img src='cbicons/smileys/shocked_esk.png' class='smiley' border=0>");
	data = data.replace(/:-0/ig, "<img src='cbicons/smileys/shocked_esk.png' class='smiley' border=0>");

				
	return data;

}

function createClick(windowType)
{
	newguid = jsguid();
	$.post('create.php', {joinGUID: newguid}, function(){
		if(windowType == "SAME")
		{
			window.location="chatroom.php?GUID=" + newguid;
		} else if (windowType == "NEW")
		{
			var blocker = window.open("chatroom.php?GUID=" + newguid);
			if(blocker)
			{
				// not blocked, ok!
			}
			else
			{
				alert("You have popups blocked - you need to disable this in your browser to create a room from this page!");
			}
		}
	});	
}

function loadPopup(divName){  
	//loads popup only if it is disabled  

	$("#backgroundPopup").css({  
		"opacity": "0.7"  
	});  
	$("#backgroundPopup").fadeIn("slow");  
	$(divName).fadeIn("slow");  

}

function centerPopup(divName)
{
  
	//request data for centering
	var windowWidth; 
	var windowHeight;
	var popupHeight = $(divName).height();
	var popupWidth = $(divName).width();
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
			  windowWidth = window.innerWidth,
			  windowHeight = window.innerHeight
	}
 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		windowWidth = document.documentElement.clientWidth,
		windowHeight = document.documentElement.clientHeight
	}
 
	 // older versions of IE 
	 else
	 {
			   windowWidth = document.getElementsByTagName('body')[0].clientWidth,
			   windowHeight = document.getElementsByTagName('body')[0].clientHeight
	 }
  
	//centering  
	$(divName).css({     
	"position": "absolute",  
	"top": windowHeight/2-popupHeight/2,     
	"left": windowWidth/2-popupWidth/2
	});

	//ie
	
	var bgHeight = $("body").height();
		
	$("#backgroundPopup").css({
		"height": bgHeight,
		"width" : windowWidth  
	});
}

$(document).ready(function() {

	$("#tsandcsButton").click(function(){
		var divName = "#popupTsandcs";
		$("#tsandcsArea").load("tsandcs.html");
		centerPopup(divName);
		loadPopup(divName);
	});
	
	$("#privacyButton").click(function(){
		var divName = "#popupPrivacy";
		$("#privacyArea").load("privacy.html");
		centerPopup(divName);
		loadPopup(divName);	
	});
	
	$("#tsandcsClose").click(function(){  
		var parentDiv = $(this).parent().attr("id");
		disablePopup(parentDiv);
	});
	
	$("#privacyClose").click(function(){  
		var parentDiv = $(this).parent().attr("id");
		disablePopup(parentDiv);
	});	
	
});