
function getArgs(){
  passedArgs=new Array();
  search = self.location.href;
  search = search.split('?');
  if(search.length>1){
    argList = search[1];
    argList = argList.split('&');
    for(var i=0; i<argList.length; i++){
      newArg = argList[i];
      newArg = argList[i].split('=');
      passedArgs[unescape(newArg[0])] = unescape(newArg[1]);
    }
  }
}

function cutResource(aJID) { // removes resource from a given jid
  if (typeof(aJID) == 'undefined' || !aJID)
    return;
  var retval = aJID;
  if (retval.indexOf("/") != -1)
    retval = retval.substring(0,retval.indexOf("/"));
  return retval;
}

function msgEscape(msg) {
  if (typeof(msg) == 'undefined' || !msg || msg == '')
    return;

  msg = msg.replace(/%/g,"%25"); // must be done first

  msg = msg.replace(/\n/g,"%0A");
  msg = msg.replace(/\r/g,"%0D");
  msg = msg.replace(/ /g,"%20");
  msg = msg.replace(/\"/g,"%22");
  msg = msg.replace(/#/g,"%23");
  msg = msg.replace(/\$/g,"%24");
  msg = msg.replace(/&/g,"%26");
  msg = msg.replace(/\(/g,"%28");
  msg = msg.replace(/\)/g,"%29");
  msg = msg.replace(/\+/g,"%2B");
  msg = msg.replace(/,/g,"%2C");
  msg = msg.replace(/\//g,"%2F");
  msg = msg.replace(/\:/g,"%3A");
  msg = msg.replace(/\;/g,"%3B");
  msg = msg.replace(/</g,"%3C");
  msg = msg.replace(/=/g,"%3D");
  msg = msg.replace(/>/g,"%3E");
  msg = msg.replace(/@/g,"%40");

  return msg;
}

// IE is too stupid for window names
function makeWindowName(wName) {
  wName = wName.replace(/@/,"at");
  wName = wName.replace(/\./g,"dot");
  wName = wName.replace(/\//g,"slash");
  wName = wName.replace(/&/g,"amp");
  wName = wName.replace(/\'/g,"tick");
  wName = wName.replace(/=/g,"equals");
  wName = wName.replace(/#/g,"pound");
  wName = wName.replace(/:/g,"colon");	
  wName = wName.replace(/%/g,"percent");
  wName = wName.replace(/-/g,"dash");
  wName = wName.replace(/ /g,"blank");
  wName = wName.replace(/\*/g,"asterix");
  return wName;
}

function htmlEnc(str) {
  if (!str)
    return '';

  str = str.replace(/&/g,"&amp;");
  str = str.replace(/</g,"&lt;");
  str = str.replace(/>/g,"&gt;");
  str = str.replace(/\"/g,"&quot;");
  return str;
}

function msgFormat(msg) { // replaces emoticons and urls in a message
  if (!msg)
    return null;

  msg = htmlEnc(msg);

  if (typeof(emoticons) != 'undefined') {
    for (var i in emoticons) {
      var iq = i.replace(/\\/g, '');
      var emo = new Image();
      emo.src = emoticonpath+emoticons[i];
      if (emo.width > 0 && emo.height > 0)
	msg = msg.replace(eval("/\(\\s\|\^\)"+i+"\(\\s|\$\)/g"),"$1<img src=\""+emo.src+"\" width='"+emo.width+"' height='"+emo.height+"' alt=\""+iq+"\" title=\""+iq+"\">$2");
      else
	msg = msg.replace(eval("/\(\\s\|\^\)"+i+"\(\\s|\$\)/g"),"$1<img src=\""+emo.src+"\" alt=\""+iq+"\" title=\""+iq+"\">$2");
    }
  }
	
  // replace http://<url>
  msg = msg.replace(/(\s|^)(https?:\/\/\S+)/gi,"$1<a href=\"$2\" target=\"_blank\">$2</a>");
  
  // replace ftp://<url>
  msg = msg.replace(/(\s|^)(ftp:\/\/\S+)/gi,"$1<a href=\"$2\" target=\"_blank\">$2</a>");
  
  // replace mail-links
  msg = msg.replace(/(\s|^)(\w+\@\S+\.\S+)/g,"$1<a href=\"mailto:$2\">$2</a>");
  
  // replace *<pattern>*
  msg = msg.replace(/(\s|^)\*([^\*\r\n]+)\*/g,"$1<b>\$2\</b>");
  
  // replace _bla_ 
  msg = msg.replace(/(\s|^)\_([^\*\r\n]+)\_/g,"$1<u>$2</u>");

  msg = msg.replace(/\n/g,"<br>");

  return msg;
}

/* isValidJID
 * checks whether jid is valid
 */
var prohibited = ['"',' ','&','\'','/',':','<','>','@']; // invalid chars
function isValidJID(jid) {
  var nodeprep = jid.substring(0,jid.lastIndexOf('@')); // node name (string before the @)

  for (var i=0; i<prohibited.length; i++) {
    if (nodeprep.indexOf(prohibited[i]) != -1) {
      alert("Invalid JID\n'"+prohibited[i]+"' not allowed in JID.\nChoose another one!");
      return false;
    }
  }
  return true;
}

/* jab2date
 * converts from jabber timestamps to javascript date objects
 */
function jab2date(ts) {
  var date = new Date(Date.UTC(ts.substr(0,4),ts.substr(5,2)-1,ts.substr(8,2),ts.substr(11,2),ts.substr(14,2),ts.substr(17,2)));
  if (ts.substr(ts.length-6,1) != 'Z') { // there's an offset
    var offset = new Date();
    offset.setTime(0);
    offset.setUTCHours(ts.substr(ts.length-5,2));
    offset.setUTCMinutes(ts.substr(ts.length-2,2));
    if (ts.substr(ts.length-6,1) == '+')
      date.setTime(date.getTime() - offset.getTime());
		else if (ts.substr(ts.length-6,1) == '-')
		  date.setTime(date.getTime() + offset.getTime());
  }
  return date;
}

/* hrTime - human readable Time
 * takes a timestamp in the form of 2004-08-13T12:07:04±02:00 as argument
 * and converts it to some sort of humane readable format
 */
function hrTime(ts) {
  return jab2date(ts).toLocaleString();
}

/* jabberDate
 * somewhat opposit to hrTime (see above)
 * expects a javascript Date object as parameter and returns a jabber 
 * date string conforming to JEP-0082
 */
function jabberDate(date) {
  if (!date.getUTCFullYear)
    return;
  
  var jDate = date.getUTCFullYear() + "-";
  jDate += (((date.getUTCMonth()+1) < 10)? "0" : "") + (date.getUTCMonth()+1) + "-";
  jDate += ((date.getUTCDate() < 10)? "0" : "") + date.getUTCDate() + "T";
  
  jDate += ((date.getUTCHours()<10)? "0" : "") + date.getUTCHours() + ":";
  jDate += ((date.getUTCMinutes()<10)? "0" : "") + date.getUTCMinutes() + ":";
  jDate += ((date.getUTCSeconds()<10)? "0" : "") + date.getUTCSeconds() + "Z";
  
  return jDate;
}

function joinConference(baseUrl,episodeId,userName,password,server,port,streamBaseUrl) {
	var chatLoc = baseUrl + '?epid=' + episodeId + 
  				'&uname=' + userName + '&pass=' + password + 
  				'&server=' + server + '&port=' + port + 
  				'&streambaseurl=' + streamBaseUrl;
 	window.open(chatLoc,'TalkShoeChat','width=800,height=600,resizable=yes,status=yes');
}

function joinConferenceSub(baseUrl,episodeId,userName,password,server,port,streamBaseUrl) {
	if(userName != ''){
  		var joinLoc = '/talkshoe/web/go2prot/tscmd/tsl/'+episodeId;
  	}else{
  		var joinLoc = '/talkshoe/web/tscmd/signin/1';
  	}
 	location.href = joinLoc;
}

function joinConferenceAlpha(baseUrl,episodeId,userName,password,server,port,streamBaseUrl) {
	var chatLoc = baseUrl + '?epid=' + episodeId + 
  				'&uname=' + userName + '&pass=' + password + 
  				'&server=' + server + '&port=' + port + 
  				'&streambaseurl=' + streamBaseUrl;
 	window.open(chatLoc,'TalkShoeChat','width=800,height=600,resizable=yes,status=yes');
}

function joinConferenceFromIFrame(p1,p2,p3,p4,p5,p6,p7,p8) {
	var baseUrl = document.getElementById(p1).value;
	var epiId = document.getElementById(p2).value;
	var uN = document.getElementById(p3).value;
	var pw = document.getElementById(p4).value;
	var server = document.getElementById(p5).value;
	var port = document.getElementById(p6).value;
	var streamBaseUrl = document.getElementById(p7).value;
	var bN = document.getElementById(p8).value;
	
	var chatLocIF = baseUrl + '?epid=' + epiId + 
  				'&uname=' + uN + '&pass=' + pw + 
  				'&server=' + server + '&port=' + port + 
  				'&streambaseurl=' + streamBaseUrl + '&bN=' + bN;

	var htmlWrite = '<iframe src="'+chatLocIF+'" name="TalkShoeChatMain" id="TalkShoeChatMain" scrolling="no" allowtransparency="true" ' +
					' width="110%" height="580" frameborder="0" marginheight="0" marginwidth="0" ' +
					' class="TalkShoeChatMain"></iframe>';

 	document.writeln(htmlWrite);
	
}

//START popup checking for Auto Web Client Launch.
var oWCPopObjT = new Object();
var popUpsBlocked = true;

function checkWCOpen(pageId) {
	try {
	   if((!oWCPopObjT[pageId]) || (oWCPopObjT[pageId].closed)){
			var popMsgElem = window.document.getElementById("popupMsgDiv");
			var flashElem = window.document.getElementById("tcForwardProgressInnerDiv");
			flashElem.style.visibility = "hidden";
			flashElem.style.display = "none";
			popMsgElem.style.display = "";
			popMsgElem.style.visibility = "visible";
			popUpsBlocked = true;
		}else{
			var popMsgElem = window.document.getElementById("popupMsgDiv");
			var flashElem = window.document.getElementById("tcForwardProgressInnerDiv");
			flashElem.style.display = "";
			flashElem.style.visibility = "visible";
			popMsgElem.style.visibility = "hidden";
			popMsgElem.style.display = "none";
			popUpsBlocked = false;
		}
     }catch(e){
     	var popMsgElem = window.document.getElementById("popupMsgDiv");
		var flashElem = window.document.getElementById("tcForwardProgressInnerDiv");
		flashElem.style.visibility = "hidden";
		flashElem.style.display = "none";
		popMsgElem.style.display = "";
		popMsgElem.style.visibility = "visible";
		popUpsBlocked = true;
     }
}

function WCToLocCheckPopSuccess(hiddenLocation){
	if(!popUpsBlocked){
		LocationReplaceGoToUrl(hiddenLocation);
	}
}

function WCToLocCheckPop(hiddenLocation,tcStatus){
	/*tcStatus was added to prevent heavy load for live shows autoLaunching 
	the client and redirecting to the Talkcast page at the same time.
	TODO: Remove this check after Talkcast load has been optimized.*/
	
	if(tcStatus == "notLaunched"){ 
		setTimeout("WCToLocCheckPopSuccess(\""+hiddenLocation+"\")",2000);
	}
}

function webClientAuto(chatLoc){
	var popMsgElem = window.document.getElementById("popupMsgDiv");
	var flashElem = window.document.getElementById("tcForwardProgressInnerDiv");
	flashElem.style.display = "";
	flashElem.style.visibility = "visible";
	popMsgElem.style.visibility = "hidden";
	popMsgElem.style.display = "none";
	popUpsBlocked = false;
	var popWidth = "930px";
	var popHeight = "630px";
 
	wcWinPop = window.open(chatLoc, 'TalkShoeChat', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,copyhistory=no, height='+popHeight+', width='+popWidth+', top=20 left=20' );
	
	oWCPopObjT['TalkShoeChat'] = wcWinPop;
	
	//check if the oWCPopObjT opened
	setTimeout("checkWCOpen(\"TalkShoeChat\")",1000);
}

function webClientReLaunch(masterId,goToUrl){
	var chatLoc = '/talkshoe/web/tscmd/wc/' + masterId;
	var popMsgElem = window.document.getElementById("popupMsgDiv");
	var flashElem = window.document.getElementById("tcForwardProgressInnerDiv");
	flashElem.style.display = "";
	flashElem.style.visibility = "visible";
	popMsgElem.style.visibility = "hidden";
	popMsgElem.style.display = "none";
	popUpsBlocked = false;
	
	var popWidth = "930px";
	var popHeight = "630px";
 
	window.open(chatLoc, 'TalkShoeChat', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,copyhistory=no, height='+popHeight+', width='+popWidth+', top=20 left=20' );
	
	WCToLocCheckPop(goToUrl,'launched');
}
//END popup checking for Auto Web Client Launch.

function webClientGoToTC(masterId,customInfo){
	var chatLoc = '/talkshoe/web/tscmd/wc/' + masterId;
	var tcLoc = '/talkshoe/web/tscmd/tc/' + masterId+customInfo;
	
	var popWidth = "930px";
	var popHeight = "630px";
 
	window.open(chatLoc, 'TalkShoeChat', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,copyhistory=no, height='+popHeight+', width='+popWidth+', top=20 left=20' );
	
	//setTimeout("LocationReplaceGoToUrlFull(\""+tcLoc+"\")",1000);
	//TODO: Uncomment as soon as we fix our Talkcast optimization.
}

function webClient(masterId){
	var chatLoc = '/talkshoe/web/tscmd/wc/' + masterId;
	var popWidth = "930px";
	var popHeight = "630px";
 
	window.open(chatLoc, 'TalkShoeChat', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=yes,copyhistory=no, height='+popHeight+', width='+popWidth+', top=20 left=20' );
}

function resizeWCIframe() {
	//http://brondsema.net/blog/index.php/2007/06/06/100_height_iframe
    var height = document.documentElement.clientHeight;
    if(browser=="InternetExplorer"){
    	height -= document.getElementById('TalkShoeChatMain').offsetTop;
    }
    
    // not sure how to get this dynamically
    if(browser!="Opera"){
   		height -= 2; /* whatever you set your body bottom margin/padding to be */
   		document.getElementById('TalkShoeChatMain').style.height = height +"px"
   	}else{
   		//height -= 120;
   	}
    
    //document.getElementById('TalkShoeChatMain').style.height = height +"px";
    
};