var ajaxShowInvitations = new sack();
var ajaxUpdateInvitation = new sack();
var ajaxCancelInvitations = new sack();
var ajaxAddBan = new sack();
var ajaxStatusUpdate = new sack();
var ajaxInvitationNotification = new sack();
var ajaxMemberBanStatusUpdate = new sack();

var reqSendInvitation;
var urlSendInvitation;

var reqCheckInvitations;
var urlCheckInvitations;

var invitationTimer;
var invitationNotificationTimer;

var invitationLayerOpen = false;

function whenLoading(){}

function fitMessages(){
	var fitMessageButtons = document.getElementsByName("fitMessageButtonTemp");
	var messageButtonsCount = fitMessageButtons.length;
	for (var i=0 ; i<messageButtonsCount ; i++){
		var fitMessageButton = fitMessageButtons.item(0);
		fitMessageButton.onclick();
	}
}

function startAcceptedInvitations(){
	var startButtons = document.getElementsByName("startButton");
	for (var i=0 ; i<startButtons.length ; i++){
		var startButton = startButtons.item(i);
		startButton.onclick();
	}
}

function showInvitations(timer){
	ajaxShowInvitations.reset();
	ajaxShowInvitations.requestFile = "/invitation/checkInvitations.do";
	ajaxShowInvitations.method = 'GET';
	ajaxShowInvitations.onLoading = whenLoading;
	ajaxShowInvitations.onCompletion = completeShowInvitations;
	ajaxShowInvitations.runAJAX();
	
	clearTimeout(invitationNotificationTimer);
	clearTimeout(invitationTimer);
	
	// clear notification response from the cookie
	resetInvitationNotificationResponse();
	
	if (timer)
		invitationTimer = setTimeout("showInvitations(true)",30000);
}

function completeShowInvitations(){
	var response = ajaxShowInvitations.response;
	if (response.trim() == "Error Code:-1"){
		var sURL = unescape(window.location.pathname);
		window.location.href = sURL;
	}
		
	var invitationsDiv = document.getElementById("invitationsDiv");
	if (invitationsDiv) 
		invitationsDiv.innerHTML = response;

	fitMessages();
	setTimeout('startAcceptedInvitations()', 1000);
}

function sendUpdateRequest(actionName,invitationId){
	ajaxUpdateInvitation.reset();
	ajaxUpdateInvitation.encVar("actionName" , actionName);
	ajaxUpdateInvitation.encVar("invitationId" , invitationId);
	ajaxUpdateInvitation.requestFile = "/invitation/updateInvitation.do";
	ajaxUpdateInvitation.method = 'GET';
	ajaxUpdateInvitation.onLoading = whenLoading;
	ajaxUpdateInvitation.onCompletion = completeUpdateInvitations;
	ajaxUpdateInvitation.runAJAX();
}

function completeUpdateInvitations(){
	var response = ajaxUpdateInvitation.response;
	var param = response.split(";");
	var actionType = param[0].trim().substring("actionType=".length);
	var errorCode = param[1].trim().substring("errorCode=".length);
	var channelId = param[2].trim().substring("channelId=".length);
	var flashServerIP = param[3].trim().substring("flashServerIP=".length);
	var language = param[4].trim().substring("language=".length);
	
	if ((actionType == 1 || actionType == 2) && errorCode == 0){
		openOne2One(channelId,language,flashServerIP);
	}
	
	if (errorCode == 0){
		showInvitations(true);
	}
	else if (errorCode == 5){
		//alert("The other user has canceled the Invite.");
		showInvitations(true);
	}
	else{
		//alert("errorCode="+errorCode);
	}
}

function sendCancelRequest(category){
	ajaxCancelInvitations.reset();
	ajaxCancelInvitations.encVar("category" , category);
	ajaxCancelInvitations.requestFile = "/invitation/cancelInvitations.do";
	ajaxCancelInvitations.method = 'GET';
	ajaxCancelInvitations.onLoading = whenLoading;
	ajaxCancelInvitations.onCompletion = completeCancelInvitations;
	ajaxCancelInvitations.runAJAX();
}

function completeCancelInvitations(){
	var response = ajaxCancelInvitations.response;
	var param = response.split(";");
	var actionType = param[0].trim().substring("actionType=".length);
	var errorCode = param[1].trim().substring("errorCode=".length);

	if ((actionType == 6 || actionType == 7) && errorCode == 0){
		showInvitations(true);;
	}
	else if (actionType == 13 && errorCode == 0){
		//it means status set to the off
		showInvitations(false);;
	}
	else{
		//alert("errorCode="+errorCode);
	}
}

function sendAddBanRequest(invitationId,bannedId,bannedName,bannedType){
	ajaxAddBan.reset();
	ajaxAddBan.encVar("invitationId" , invitationId);
	ajaxAddBan.encVar("bannedId" , bannedId);
	ajaxAddBan.encVar("bannedName" , bannedName);
	ajaxAddBan.encVar("bannedType" , bannedType);
	ajaxAddBan.requestFile = "/invitation/addBan.do";
	ajaxAddBan.method = 'GET';
	ajaxAddBan.onLoading = whenLoading;
	ajaxAddBan.onCompletion = completeAddBan;
	ajaxAddBan.runAJAX();
}

function completeAddBan(){
	var response = ajaxAddBan.response;
	var param = response.split(";");
	var actionType = param[0].trim().substring("actionType=".length);
	var errorCode = param[1].trim().substring("errorCode=".length);
	
	if (actionType == 8 && errorCode == 0){
		showInvitations(true);;
	}
	else{
		//alert("errorCode="+errorCode);
	}
}

function getMemberBannedStatus(mediaId){
	ajaxMemberBanStatusUpdate.reset();
	ajaxMemberBanStatusUpdate.encVar("mediaId" , mediaId);
	var url = "/servlet/ajax/getMemberBannedStatus?mediaId="+mediaId;
	ajaxMemberBanStatusUpdate.requestFile = url;
	ajaxMemberBanStatusUpdate.method = 'GET';
	ajaxMemberBanStatusUpdate.onLoading = whenLoading;
	ajaxMemberBanStatusUpdate.onCompletion = completeGetMemberBannedStatus;
	ajaxMemberBanStatusUpdate.runAJAX();
}

function completeGetMemberBannedStatus(){
	var response = ajaxMemberBanStatusUpdate.response;
	var param = response.split(";");
	var isMemberBanned = param[0].trim().substring("isBanned=".length);
	var mediaId = param[1].trim().substring("mediaId=".length);
	if (isMemberBanned == 'true') {
		document.getElementById('addCommentBox1').innerHTML = 
		"<table width=100% border=0 cellspacing=8 cellpadding=0 class=commentTable> <tr><td align=center> This person is not accepting comments at the time.</td></tr></table>";
		document.getElementById('mediaCommentList').innerHTML = "<br>";
	} else {
		showMediaAddCommentForm(mediaId);
	}
}


function sendInvitationRequest(){
	var recieverId = document.sendInvitationForm.recieverId.value;
	var recieverName = document.sendInvitationForm.recieverName.value;
	var invitationType = document.sendInvitationForm.invitationType.value;
	var message = document.sendInvitationForm.message.value;
	message = message.replace(/(\r\n|\r|\n)/g, "");

	reqSendInvitation = false;
	urlSendInvitation = "/invitation/sendInvitation.do?recieverId="+recieverId+"&recieverName="+recieverName+"&invitationType="+invitationType;
	
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			reqSendInvitation = new XMLHttpRequest();
        } catch(e) {
			reqSendInvitation = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	reqSendInvitation = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		reqSendInvitation = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		reqSendInvitation = false;
        	}
		}
    }

  	if (message != null && message != "")
  	{
		reqSendInvitation.open("POST", urlSendInvitation, true);
		reqSendInvitation.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
		reqSendInvitation.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		reqSendInvitation.onreadystatechange = completeSendInvitation;
		reqSendInvitation.send("message=" + encodeURIComponent(message));
  	}
  	else
  	{
   	 //alert("Enter a message");
 	}
}

function completeSendInvitation(){ 
  if (reqSendInvitation.readyState == 4) {
    if (reqSendInvitation.status == 200) {

		overlayclose('invitationSubcontent'); 
		overlay(originalObj, 'errorSubcontent', 'leftbottom');

		var response = reqSendInvitation.responseText;
		
		var param = response.split(";");
		var actionType = param[0].trim().substring("actionType=".length);
		var errorCode = param[1].trim().substring("errorCode=".length);
	
		if(actionType == 0 && errorCode == 0){
			document.getElementById("errorSubcontent").innerHTML = "Invitation has been sent.<br>Please wait for invitee response.";
			setTimeout('closeErrorLayer()', 3000);
		}else if(actionType == 0 && errorCode == 1){
			document.getElementById("errorSubcontent").innerHTML = "Send invitation failed.<br>Please try again.";
			setTimeout('closeErrorLayer()', 3000);
		}else if(actionType == 0 && errorCode == 2){
			document.getElementById("errorSubcontent").innerHTML = "Invitation already exists.<br>Please wait for invitee response.";
			setTimeout('closeErrorLayer()', 3000);
		}else if(actionType == 0 && errorCode == 4){
			document.getElementById("errorSubcontent").innerHTML = "You are ignored by Invitee.<br>Please dont try again.";
			setTimeout('closeErrorLayer()', 3000);			
		}else if(actionType == 0 && errorCode == -1){
			document.getElementById("errorSubcontent").innerHTML = "You are not logged in!<br>Please log in and try again.";
			setTimeout('showLoginPage()', 3000);
		}else if(actionType == 0 && errorCode == -2){
			document.getElementById("errorSubcontent").innerHTML = "You are not allowed to invite this user: Age restriction.";
			setTimeout('closeErrorLayer()', 3000);	
		}else if(actionType == 0 && errorCode == -3){
			document.getElementById("errorSubcontent").innerHTML = "Invitee does not accept invitations.";
			setTimeout('closeErrorLayer()', 3000);		
		}else if(actionType == 0 && errorCode == -4){
			document.getElementById("errorSubcontent").innerHTML = "You can't send invitation to yourself!";
			setTimeout('closeErrorLayer()', 3000);		
		}else if(actionType == 0 && errorCode == -5){
			document.getElementById("errorSubcontent").innerHTML = "You can't send invitation to user of other website!";
			setTimeout('closeErrorLayer()', 3000);	
		}else if(actionType == 0 && errorCode == -6){
			document.getElementById("errorSubcontent").innerHTML = "Service is not available at this moment.<br>Please try later.";
			setTimeout('closeErrorLayer()', 3000);				
		}else{
			document.getElementById("errorSubcontent").innerHTML = "There was an error with your invitation.<br>Please try later.";
			setTimeout('closeErrorLayer()', 3000);
		}
    }
  } 
} 

function sendStatusUpdateRequest(newStatus){
	ajaxStatusUpdate.reset();
	ajaxStatusUpdate.encVar("newStatus" , newStatus);
	ajaxStatusUpdate.requestFile = "/invitation/statusUpdate.do";
	ajaxStatusUpdate.method = 'GET';
	ajaxStatusUpdate.onLoading = whenLoading;
	ajaxStatusUpdate.onCompletion = completeStatusUpdate;
	ajaxStatusUpdate.runAJAX();
}
	
function completeStatusUpdate(){
	var response = ajaxStatusUpdate.response;
	var param = response.split(";");
	var newStatusCode = param[0].trim().substring("newStatusCode=".length);
	var errorCode = param[1].trim().substring("errorCode=".length);
	
	if (newStatusCode == 1 && errorCode == 0){
		showInvitations(true);
	}
	else if (newStatusCode == 0 && errorCode == 0){
		//cancel all invitations
		sendCancelRequest('both');
	}
	else{
		//alert("errorCode="+errorCode);
	}
}

function getInvitationNotification(){
	var invitationNotificationResponse = getInvitationNotificationResponse();
	
	if (!invitationLayerOpen){
		if (invitationNotificationResponse == ""){
			ajaxInvitationNotification.reset();
			ajaxInvitationNotification.requestFile = "/invitation/invitationNotification.do";
			ajaxInvitationNotification.method = 'GET';
			ajaxInvitationNotification.onLoading = whenLoading;
			ajaxInvitationNotification.onCompletion = completeInvitationNotification;
			ajaxInvitationNotification.runAJAX();
		}
		else {
			showInvitationNotification(invitationNotificationResponse);
		}
		invitationNotificationTimer = setTimeout("getInvitationNotification()",30000);
	} else {
		clearTimeout(invitationNotificationTimer);
	}
}
	
function completeInvitationNotification(){
	var response = ajaxInvitationNotification.response;
	setInvitationNotificationResponse(response);
	showInvitationNotification(response);
}

function showInvitationNotification(response){
	var param = response.split(",");
	var errorCode = param[0].trim().substring("errorCode=".length);
	var newInvitationsCount = param[1].trim().substring("newInvitationsCount=".length);
	var acceptedInvitationsCount = param[2].trim().substring("acceptedInvitationsCount=".length);
	var invitationStatus = param[3].trim().substring("invitationStatus=".length);
	var htmlCode = '';

	if (invitationStatus == 1 && errorCode == 0 && acceptedInvitationsCount > 0){
		showNotificationLayer('skres_t1on1Tab','skres_t1on1TabActive','skres_t1on1');
	} else if (invitationStatus == 1 && errorCode == 0 && newInvitationsCount > 0){
		htmlCode = '<a class="skres_aTab" href="javascript:;" onClick="showNotificationLayer(\'skres_t1on1Tab\',\'skres_t1on1TabActive\',\'skres_t1on1\');" onmouseover="showHint(\'1to1Hint\');" onmouseout="hideHint(\'1to1Hint\');"><img width="16" height="16" border="0" src="/images/en/header/1to1_blink.gif"></a><div id="1to1Hint" class="skres_menuHint" style="display:none;">1 to 1</div>';
		var invitationNotification = document.getElementById('skres_t1on1Tab');
		if (invitationNotification)
			invitationNotification.innerHTML = htmlCode;
	} else if (invitationStatus == 1 && errorCode == 0 && newInvitationsCount == 0 && acceptedInvitationsCount == 0){
		htmlCode = '<a class="skres_aTab" href="javascript:;" onClick="showNotificationLayer(\'skres_t1on1Tab\',\'skres_t1on1TabActive\',\'skres_t1on1\');" onmouseover="showHint(\'1to1Hint\');" onmouseout="hideHint(\'1to1Hint\');"><img width="16" height="16" border="0" src="/images/en/header/1to1.gif"></a><div id="1to1Hint" class="skres_menuHint" style="display:none;">1 to 1</div>';
		var invitationNotification = document.getElementById('skres_t1on1Tab');
		if (invitationNotification)
			invitationNotification.innerHTML = htmlCode;
	} else if (invitationStatus == 0){
		//show disabled one2one icon
		clearTimeout(invitationNotificationTimer);
		htmlCode = '<a class="skres_aTab" href="javascript:;" onClick="showNotificationLayer(\'skres_t1on1Tab\',\'skres_t1on1TabActive\',\'skres_t1on1\');" onmouseover="showHint(\'1to1Hint\');" onmouseout="hideHint(\'1to1Hint\');"><img width="16" height="16" border="0" src="/images/en/header/1to1_disable.gif"></a><div id="1to1Hint" class="skres_menuHint" style="display:none;">1 to 1</div>';
		var invitationNotification = document.getElementById('skres_t1on1Tab');
		if (invitationNotification)
			invitationNotification.innerHTML = htmlCode;			
	} else{
		clearTimeout(invitationNotificationTimer);
		//alert("errorCode="+errorCode);
	}
}