

var GLOBAL_TEMP_URL;
var GLOBAL_TEMP_CALLBACK;
var GLOBAL_DIALOG;
var GLOBAL_TEMP_ID;
var GLOBAL_TEMP_VALUE;

//messages to be exported to resource nls bundle 
var msg_classify = emmapp.msg.nlsStrings.CLASSIFY_COMPANY;

function CompanySearchResult_follow(url, id) {

	GLOBAL_TEMP_ID = id;
	
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		console.log("xhrGet OK", response, ioArgs);
		
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		// alert("status=" + response.status);
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.style.visibility = 'hidden';
			var key = emmapp.page.getRowKey(id);
			var e_followers =  dojo.byId(key + "followers");
			/*if ( e_followers ) {
				var c = parseInt(e_followers.innerHTML);
				e_followers.innerHTML = c+1;
			}*/
			updateCount("number_mark_followed_company_cnt",1);
			updateCount("number_mark_liketocompany_follow_cnt",-1);
			updateCount_follow("number_mark_follower_cnt",1,key);
			updateIsFollow_title(key);
		}
		
		return response;
	}
	
//	if ( emmapp.isGuestUser ) {
//		emmapp.ajax.doAjaxPost(url, callback);
//	} else {
		openCompanyClassifyDialog(msg_classify, url,callback);
//	}

}

function CompanySearchResult_classify(url, id) {

	var e = dojo.byId(id);	
	
	GLOBAL_TEMP_ID = id;
	
	var callback = function(response, ioArgs) {
		console.log("xhrGet OK", response, ioArgs);
		
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		//alert("status=" + response.status);
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
		
			// update classify value
			setClassify(GLOBAL_TEMP_VALUE);
		}
		
		return response;
	};
	
	openCompanyClassifyDialog(msg_classify, url,callback);


}

function getClassify() {
	var rowKey = emmapp.page.getRowKey(GLOBAL_TEMP_ID);
	var e = emmapp.page.byId("fol_classify", rowKey);
	if ( e ) {
		return e.innerHTML;
	}
	
	return "";
}

function setClassify(value) {
	var rowKey = emmapp.page.getRowKey(GLOBAL_TEMP_ID);
	var e = emmapp.page.byId("fol_classify", rowKey);
	if ( e ) {
		e.innerHTML = value ;
	}
}

function openCompanyClassifyDialog(title,url,callback){
	GLOBAL_TEMP_URL =url;
	GLOBAL_TEMP_CALLBACK = callback;
	GLOBAL_DIALOG = new dijit.Dialog({
        title: title,
        style: "width: 380px; height: 450px"
    });
	
	GLOBAL_DIALOG.setContent("<div class=\"align_left\">"+"<h2>What is your primary interest in following this company? " +
			"Please select one: </h2>"+"<div style=\"padding-left:10px;\" class=\"deleteBorder\">"+
	"<input type=\"radio\" value=\"pr\" name=\"checked_in\" class=\"pr\"/>&nbsp;&nbsp;It is a prospect<br />"+
	"<input type=\"radio\" value=\"c\" name=\"checked_in\" class=\"c\" />&nbsp;&nbsp;It is a customer<br />"+
	"<input type=\"radio\" value=\"r\" name=\"checked_in\" class=\"r\"/>&nbsp;&nbsp;It is a reseller<br />"+
	"<input type=\"radio\" value=\"d\" name=\"checked_in\" class=\"d\"/>&nbsp;&nbsp;It is a distributor<br />"+
	"<input type=\"radio\" value=\"s\" name=\"checked_in\" class=\"s\"/>&nbsp;&nbsp;It is a supplier<br />"+
	"<input type=\"radio\" value=\"sp\" name=\"checked_in\" class=\"sp\"/>&nbsp;&nbsp;It is a service provider<br />"+
	"<input type=\"radio\" value=\"p\" name=\"checked_in\" class=\"p\" />&nbsp;&nbsp;It is a partner<br />"+
	"<input type=\"radio\" value=\"cp\" name=\"checked_in\" class=\"cp\"/>&nbsp;&nbsp;It is a competitor<br />"+
	"<input type=\"radio\" value=\"po\" name=\"checked_in\" class=\"po\"/>&nbsp;&nbsp;It is a portfolio company<br />"+
	"<input type=\"radio\" value=\"pa\" name=\"checked_in\" class=\"pa\"/>&nbsp;&nbsp;It is a parent company<br />"+
	"<input type=\"radio\" value=\"su\" name=\"checked_in\" class=\"su\"/>&nbsp;&nbsp;It is a subsidiary<br />"+
	"<input type=\"radio\" value=\"si\" name=\"checked_in\" class=\"si\"/>&nbsp;&nbsp;It is a sister company<br />"+
	"<input type=\"radio\" value=\"jv\" name=\"checked_in\" class=\"jv\"/>&nbsp;&nbsp;It is your joint venture<br />"+
	"<input type=\"radio\" value=\"ia\" name=\"checked_in\" class=\"ia\"/>&nbsp;&nbsp;It is your industry association<br />"+
	"<input type=\"radio\" value=\"ii\" name=\"checked_in\" class=\"ii\"/>&nbsp;&nbsp;It is your institutional investor<br />"+
	"<input type=\"radio\" value=\"o\" name=\"checked_in\" class=\"o\"/>&nbsp;&nbsp;Others"+"</div>"+
	"<br /><div style=\"padding-left:240px;\">"+
	"<span class=\"button_imgbg\" >"+
	"<a href=\"javascript:getChecked()\">Submit</a></span>&nbsp;"
	+"<span><a href=\"javascript:GLOBAL_DIALOG.hide()\">Cancel</a></span>"+"</div>"+"</div>"		
	);
	
	var classify = getClassify();
	dojo.query('input[name=\"checked_in\"]').forEach(function(thisObject){
		if(thisObject.value == classify){
			thisObject.checked = "checked";
		} else { 
			// make sure to clear the check flag, the dojo window is reused, GAGI-589: Following Classify bug
			thisObject.checked = "";
		}
	})
	
	GLOBAL_DIALOG.show();
}

function getChecked(){
	var value = "";
	dojo.query('input[name=\"checked_in\"]').forEach(function(thisObject){
		if(thisObject.checked){
			value=thisObject.value;
		}
	})
		
	if ( value == "" ) {
		alert("Please select one option");
	} else {				
			// save the value 
			GLOBAL_TEMP_VALUE = value;
			
			console.debug("classify=" + value);
			var url = emmapp.url.addParameters(GLOBAL_TEMP_URL, "classify", value);
			emmapp.ajax.doAjaxPost(url, function(response, ioArgs) {
				GLOBAL_TEMP_CALLBACK(response, ioArgs);
				
				// update the classcify message
				dojo.query('[class=\"msg_mark_followed_company_classify\"]').forEach(
						function(thisObject){
							console.debug("thisObject.innerHTML=" + thisObject.innerHTML);
							thisObject.innerHTML = response.content;						
							console.debug("response.content=" + response.content);
						});
			});

			GLOBAL_DIALOG.hide();
	}
}

function CompanyProfile_follow(url, id) {
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		console.log("xhrGet OK", response, ioArgs);
		
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		//alert("status=" + response.status);
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.style.visibility = 'hidden';
			/*dojo.byId("e_link_suggest").style.visibility = 'visible';
			
			var e_followers =  dojo.byId("buz_follower_count");
			if ( e_followers != null ) {
				var c = parseInt(e_followers.innerHTML);
				e_followers.innerHTML = c+1;
			}*/
			updateCount("number_mark_followed_company_cnt",1);
			updateCount("number_mark_follower_cnt",1);
			
		}
		
		return response;
	}
//	if ( emmapp.isGuestUser ) {
//		emmapp.ajax.doAjaxPost(url, callback);
//	} else {
		openCompanyClassifyDialog(msg_classify, url,callback);
//	}
}


function rsp_q_following_buz_delete(url, id) {
	
	var key = emmapp.page.getRowKey(id);
	var buz_name = emmapp.page.byId("org_name", key).innerHTML;
	
	var msg = emmapp.msg.unfollow(buz_name);
	if ( !confirm(msg) ) {
		return;
	}
	
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		console.log("xhrGet OK", response, ioArgs);
		
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		//alert("status=" + response.status);
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS) {
			
			// need to hide the whole row
			e.style.visibility = 'hidden';
//			alert("p=" + e.parentNode);
//			alert("pp=" + e.parentNode.parentNode);
			
			var tr = e.parentNode.parentNode;
			if ( tr != null ) {
				tr.style.display = 'none';
				//tr.style.visibility = 'hidden';
			}
/*
			var e_followers =  dojo.byId("mem_following_buz__count");
			if ( e_followers != null ) {
				var c = parseInt(e_followers.innerHTML);
				e_followers.innerHTML = c-1;
			}*/
			
			updateCount("number_mark_followed_company_cnt",-1);
			
		}
		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}

function rsp_q_connections_remove(url, id) {
	
	var key = emmapp.page.getRowKey(id);
	var buz_name = emmapp.page.byId("name", key).innerHTML;
	
	var msg = emmapp.msg.disconnect(buz_name);
	if ( !confirm(msg) ) {
		return;
	}
	
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		console.log("xhrGet OK", response, ioArgs);
		
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		//alert("status=" + response.status);
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS) {
			
			// need to hide the whole row
			e.style.visibility = 'hidden';
//			alert("p=" + e.parentNode);
//			alert("pp=" + e.parentNode.parentNode);
			
			var tr = e.parentNode.parentNode;
			if ( tr != null ) {
				tr.style.display = 'none';
				//tr.style.visibility = 'hidden';
			}

			/*var e_connections =  dojo.byId("mem_colleagues__count");
			if ( e_connections != null ) {
				var c = parseInt(e_connections.innerHTML);
				e_connections.innerHTML = c-1;
			}	*/
			updateCount("number_mark_connection_cnt",-1);
			
		}
		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}


/*
function rsp_q_following_all_members_invite(url, id) {
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.style.visibility = 'hidden';
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}

function rsp_q_colleagues_invite(url, id) {
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.style.visibility = 'hidden';
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}
*/

function rsp_q_home_suggestions_follow(url, id) {

	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			var key = emmapp.page.getRowKey(id);
			var tr = e.parentNode.parentNode;
			if ( tr != null ) {
				tr.style.display = 'none';
				//tr.style.visibility = 'hidden';
			}
			updateCount("number_mark_followed_company_cnt",1);
			updateCount("number_mark_suggestionscompany_follow_cnt",-1);
			updateCount_follow("number_mark_follower_cnt",1,key);
			
		}		
		return response;
	}
	openCompanyClassifyDialog(msg_classify,url,callback);
	/*emmapp.ajax.doAjaxPost(url, callback);*/

}

function rsp_q_home_suggestions_delete_suggestion(url, id) {
	var key = emmapp.page.getRowKey(id);
//	alert("key=" + key);
//	alert(emmapp.page.byId("org_name", key).id);
	
	var buz_name = emmapp.page.byId("org_name", key).innerHTML;
	
	var msg = emmapp.msg.removeSuggestion(buz_name);
	if ( !confirm(msg) ) {
		return;
	}
	
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			var tr = e.parentNode.parentNode;
			if ( tr != null ) {
				tr.style.display = 'none';
				//tr.style.visibility = 'hidden';
			}
			updateCount("number_mark_suggestionscompany_follow_cnt",-1);
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}

function rsp_q_mem_all_invite(url, id) {
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.style.visibility = 'hidden';
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}

function rsp_q_home_inbox_accept(url, id) {
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.style.visibility = 'hidden';
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);

}

function buz_delete_orgcontact(url, id) {
	var e = dojo.byId(id);
//alert(e.parentNode.parentNode.className);

	var msg = emmapp.msg.nlsStrings.DELETE_CONTACT_CONFIRM;
	
	if ( !confirm(msg) ) {
		return;
	}
	
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.parentNode.parentNode.style.visibility = 'hidden';
			e.parentNode.parentNode.style.display = 'none';
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);
}

function buz_delete_newsurl(url, id) {
	var e = dojo.byId(id);
//alert(e.parentNode.parentNode.className);

	var msg = emmapp.msg.nlsStrings.DELETE_URL_CONFIRM; 
	if ( !confirm(msg) ) {
		return;
	}
	
	var callback = function(response, ioArgs) {
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.parentNode.parentNode.style.visibility = 'hidden';
			e.parentNode.parentNode.style.display = 'none';
		}		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);
}

function pg_buz_browse_by_category(url, id) {
	
	var e = dojo.byId(id);
	var callback = function(response, ioArgs) {
		console.log("xhrGet OK", response, ioArgs);
		
		if ( !emmapp.ajax.checkJsonState(response) ) {
			return response;
		}
		
		//alert("status=" + response.status);
		if ( response.status == emmapp.ajax.JSON_STATUS_VALUE_SUCCESS ) {
			e.innerHTML = response.content;
		}
		
		return response;
	}
	
	emmapp.ajax.doAjaxPost(url, callback);
}

function updateCount(/*string*/className, /*int*/adjust) {
	dojo.query('[class="'+className+'"]').forEach(function(thisObject){
				var c = parseInt(thisObject.innerHTML);
				thisObject.innerHTML = c+adjust;
				getUnit(className,c,c+adjust);
	});
	
}
function updateIsFollow_title(/*string*/ rowKey){
	var follow_to_note = '_S_follow_to_note';
	var obj  = dojo.byId(rowKey+follow_to_note);
	obj.innerHTML = "You are following";
}
function updateCount_follow(/*string*/className, /*int*/adjust,rowKey) {
	dojo.query('[class="'+className+'"]').forEach(function (thisObject){
			var previousNodeId = thisObject.parentNode.id;
			if(previousNodeId.indexOf(rowKey) > -1){
				var c = parseInt(thisObject.innerHTML);
				thisObject.innerHTML = c+adjust;
				
				dojo.query('[class="'+className+'_unit"]').forEach(function (thisObject){
					var parent = thisObject.parentNode.id;
					if(parent.indexOf(rowKey) > -1){
						var single = "follower";
						var plural = "followers";
						if((c+adjust)==1){
							var tempNew = single;
							thisObject.innerHTML = tempNew;
						}else if(c==1){
							var tempNew = plural;
							thisObject.innerHTML = tempNew;
						}
					}
				});
			}
	});
	
}

function getUnit(/*string*/className,/*int*/countPre,/*int*/countNext){
	var arraySingle = new Array();
	arraySingle[0]="company";
	arraySingle[1]="connection";
	arraySingle[2]="colleague";
	arraySingle[3]="follower";
	
	var arrayPlural = new Array();
	arrayPlural[0]="companies";
	arrayPlural[1]="connections";
	arrayPlural[2]="colleagues";
	arrayPlural[3]="followers";
	
	dojo.query('[class="'+className+'_unit"]').forEach(function (thisObject){
		var temp = thisObject.innerHTML;
		if(countPre==1){
			for(var i=0;i<arraySingle.length-1;i++){
				if(className.indexOf(arraySingle[i])>-1){
					var tempNew = arrayPlural[i];
					thisObject.innerHTML = tempNew;
				}
			}
			
		}else if(countNext==1){
			for(var i=0;i<arrayPlural.length-1;i++){
				if(className.indexOf(arraySingle[i])>-1){
					var tempNew = arraySingle[i];
					thisObject.innerHTML = tempNew;
				}
			}
		}
	});
	
}
