// msg contains the message in different cases
var msg;
//Request to subscribe a thread
function subscribe_request(num){
	// To get email in case when member registration is off
	var email;
	var url_subscribe;
	// Check thread id
	if(id) {
		// num indicates the case when member registration is off
		if(num) {
			email = document.getElementById('email').value;
			if(!(/^([\w\d\-\.])+\@([\w\d\-]+\.)+([\w]{2,4})$/i.test(email))) {
				alert("You entered an invalid email address.");
				return false;
			}
		}
		msg="You have subscribed this thread successfully.";
		// url used to subscribe a thread
		url_subscribe = "/subscribe/ajax_subscription?id="+id+"&forum="+forum+"&name="+name+"&email="+email;
	} else {
		// num indicates the notification type
		// (num == 1) ==> Daily updates by email
		// (num == 20) ==> Weekly updates by email
		var url;
		if(num) {
			url="&notificationtype="+num;
		}
		msg="You have subscribed this forum successfully.";
		// url used to subscribe a forum
		url_subscribe = "/subscribe/subscribeforum?forum="+forum+url;
	}

	// create & set a response handler, global variable 
	httpRequest = CreateXmlHttpReq(_subscribeThreadHandler);
	
	// if response handler created successfully send the url 
 	if (httpRequest) {
		httpRequest.open("GET", url_subscribe, true);
		httpRequest.onreadystatechange = _subscribeThreadHandler;
	}
	httpRequest.send(null);
}

//Request to unsubscribe a thread
function unsubscribe_request() {
	var url_subscribe;
	//check thread id
	if(id) {
		msg="You have unsubscribed from this thread successfully.";
		// url used to unsubscribeforum a thread
		url_subscribe = "/subscribe/ajax_unsubscription?id="+id+"&forum="+forum;
	} else {
		msg="You have unsubscribed from this forum successfully.";
		// url used to unsubscribeforum a forum
		url_subscribe = "/subscribe/unsubscribeforum?forum="+forum;
	}
	// create & set a response handler, global variable 
	httpRequest = CreateXmlHttpReq(_subscribeThreadHandler);
	
	// if response handler created successfully send the url 
 	if (httpRequest) {
		httpRequest.open("GET", url_subscribe, true);
		httpRequest.onreadystatechange = _subscribeThreadHandler;
	}
	httpRequest.send(null);
}

function _subscribeThreadHandler() {
	// if server is ready to process the request
	if (httpRequest.readyState == 4) {
		// if server response code is ok
		 if (httpRequest.status == 200) {
			var response = httpRequest.responseText;
			// Check whether response caonatins an "Error"
			var re = /^Error:/;
			// If response contains an 'Error message' then it show 'Error message' through alert.
			if(re.test(response)) {
				alert(response);
			} else {				
				// Put the content of responce at 'divToolsId' id.
				document.getElementById(divToolsId).innerHTML = response;
				// Show the successful message of  "thread's / forum's" "subscription / unsubscription".
				document.getElementById('showmessage').innerHTML = '<br><span class=errorMsg><b>'+msg+'</b></span>';
				// Thread Subscription :: It hides 'email address' window that comes when member registration is off.
				// Forum Subscription :: It hides 'email notification type' window.
				// It hides Tooltip in case of 'Thread/Forum' unsubscription.
				hideTooltip();
				// It hides parent Tooltip in case when child Tooltip is available.
				document.getElementById(divToolsId).style.display =  'none';
			}
		} else {
			// give alert that there is some problem occurred during ajax request process.
			alert('There was a problem with the request.');
			return false;
		}
	}
}

