var front_end_javascript_call= false;
var overview_tab_active = false;
var book_tab_active = false;
var news_tab_active = false;
var buzz_tab_active = false;
var discussion_tab_active = true;
var jobs_tab_active = true;
var fin_tab_active = false;
var web_tab_active = false;
var watch_tab_active = false;
var updates_tab_active = false;
var patents_tab_active = false;
var search_tab_active = false;
var related_tab_active = false;
var video_tab_active = false;

var ajax_response_value = "";

var suggest_box_from_regular_page= '';

function activateEcoTab(){
	/*
	alert('overview_tab_active:'+overview_tab_active +
		  '\book_tab_active:'+book_tab_active +
		  '\news_tab_active:'+overview_tab_active +
		  '\buzz_tab_active:'+buzz_tab_active +
		  '\discussion_tab_active:'+discussion_tab_active +
		  '\jobs_tab_active:'+jobs_tab_active +
		  '\fin_tab_active:'+fin_tab_active +
		  '\watch_tab_active:'+watch_tab_active +
		  '\updates_tab_active:'+updates_tab_active +
		  '\patents_tab_active:'+patents_tab_active +
		  '\search_tab_active:'+search_tab_active 		  
		  );
	*/
	if(overview_tab_active){
		$("#overview_tab").trigger("click");
	}
	else if(book_tab_active){
		$("#book_tab").trigger("click");
	}
	else if(fin_tab_active){
		$("#fin_tab").trigger("click");
	}
	else if(news_tab_active){
		$("#news_tab").trigger("click");
	}
	else if(buzz_tab_active){
		$("#buzz_tab").trigger("click");
	}
	else if(discussion_tab_active){
		$("#discussion_tab").trigger("click");
	}
	else if(jobs_tab_active){
		$("#jobs_tab").trigger("click");
	}
	else if(web_tab_active){
		$("#web_tab").trigger("click");
	}
	else if(watch_tab_active){
		$("#watch_tab").trigger("click");
	}
	else if(updates_tab_active){
		$("#updates_tab").trigger("click");
	}
	else if(patents_tab_active){
		$("#patents_tab").trigger("click");
	}
	else if(search_tab_active){
		$("#search_tab").trigger("click");
	}
	else if(related_tab_active){
		$("#related_tab").trigger("click");
	}
	else if(video_tab_active){
		$("#video_tab").trigger("click");
	}
}

function suggestInvestor(fld,type){
	currentVisibleDivLayer['ecosystem']='ecosystemchecklist-company';
	suggest_box_from_regular_page='investor';
	field_id = "investor";
	field_text = "investor_text";
	suggestSearch(fld);
}
function suggestSearch(fld,source_type,source_id){
	//alert(currentVisibleDivLayer['ecosystem']);
	//ecosystem-companies->company-list
	//ecosystem-people->
	if(suggest_box_from_regular_page!=''){
		var list_div = document.getElementById(suggest_box_from_regular_page+"-list"); //company-list
	}
	else{
		var list_div = document.getElementById("company-list");
	}
	//var keyword = document.getElementById("new-search-eco_tag");
	var keyword = fld;//document.getElementById("new-search-eco_tag");
	if(fld.value.length > 0 ){
		//alert('ajax call to populate companies');
		list_div.style.display = "block";
		//var company_name = "Displaying company "+Math.random();
		var company_name = "Displaying company 1";
		makeAjax(keyword.value,source_type,source_id);
		//list_div.innerHTML += "<br><a onclick=\"selectCompany('id','"+company_name+"')\">"+company_name+"</a>";
	}
}

function selectCompany(id,title){

//	var list_div = document.getElementById("company-list");
	if(suggest_box_from_regular_page!=''){
		var list_div = document.getElementById(suggest_box_from_regular_page+"-list"); //company-list
	}
	else{
		var list_div = document.getElementById("company-list");
	}

	var container = document.getElementById("ecosystemchecklist-company");
	var message = document.getElementById("company_list_message");
	var checkbox_name = "link_company[]";
	//alert(currentVisibleDivLayer['ecosystem']);
	if(currentVisibleDivLayer['ecosystem']=='ecosystem-companies'){
		container = document.getElementById("ecosystemchecklist-company");
		checkbox_name = "link_company[]";
	}
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-people'){
		container = document.getElementById("ecosystemchecklist-people");
		checkbox_name = "link_people[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-exits'){
		container = document.getElementById("ecosystemchecklist-exits");
		checkbox_name = "link_exits[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-investments'){
		container = document.getElementById("ecosystemchecklist-investments");
		checkbox_name = "link_investments[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-events'){
		container = document.getElementById("ecosystemchecklist-events");
		checkbox_name = "link_events[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-industries'){
		container = document.getElementById("ecosystemchecklist-industries");
		checkbox_name = "link_industries[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-offices'){
		container = document.getElementById("ecosystemchecklist-offices");
		checkbox_name = "link_offices[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-watchlists'){
		container = document.getElementById("ecosystemchecklist-watchlists");
		checkbox_name = "link_watchlists[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-resources'){
		container = document.getElementById("ecosystemchecklist-resources");
		checkbox_name = "link_resources[]";
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-resource-categories'){
		container = document.getElementById("ecosystemchecklist-resource-categories");
		checkbox_name = "link_resource_categories[]";
	}

	if(suggest_box_from_regular_page !=''){
		//This one is used by regular page if need to show as suggest box. example company-page
		//field_id = "company";
		//field_text = "company_text";
		//alert('field_text:'+field_text);
		document.getElementById(field_id).value=id;
		document.getElementById(field_text).value=title;
	}
	else{
		//following is used for ecosystem plugin(meta box)
		var new_element = document.createElement('li');
		var row_id = currentVisibleDivLayer['ecosystem']+'-'+checkbox_name+'-'+id
		//new_element.setAttribute("id", row_id);
		
		if( (currentVisibleDivLayer['ecosystem']=='ecosystem-events' && (entity_type=='events' || entity_type=='companies' || entity_type=='people')) || 
			(currentVisibleDivLayer['ecosystem']=='ecosystem-companies' && (entity_type=='companies' || entity_type=='people' || entity_type=='events' ) ) || 
			(currentVisibleDivLayer['ecosystem']=='ecosystem-people' && (entity_type=='companies' || entity_type=='people' || entity_type=='events' ) )
		  ){
			//Only for group events
			var html_var =  '<table width="100%" valign="top">';
				html_var += '<tr id="'+id+'"><td width="13px"><div id="delete-link">';
				html_var += '<a href="#" onclick="deleteCurrentRow(\''+id+'\')">&nbsp;&nbsp;</a></div></td>';
				html_var += '<td >';
				html_var += '<input style="display:none" type="checkbox" checked="" value="'+id+'" name="'+checkbox_name+'"/>';
				html_var += '</td><td >'+title+'&nbsp;&nbsp;';
				html_var += '<input  type="hidden" name="ids[]" value="'+id+'" >';
				html_var += '<input  type="hidden" name="entity1_id['+id+']" value="'+entity_id+'" >';
				html_var += '<input  type="hidden" name="entity2_id['+id+']" value="'+id+'" >';
				html_var += '<input  type="hidden" name="entity1_type['+id+']" value="'+entity_type+'" >';
				html_var += '<input  type="hidden" name="new_entity['+id+']" value="NEW" >';
				//alert(currentVisibleDivLayer['ecosystem']);
				//alert(entity_type);
								
				if(currentVisibleDivLayer['ecosystem']=='ecosystem-people' && entity_type !='events'){
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="people" >';
					html_var += '&nbsp;Position:'+'<input type="text" value="" size="32" name="position['+id+']"/>'; 
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-companies' && entity_type=='people'){
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="companies" >';
					html_var += '&nbsp;Position:'+'<input type="text" value="" size="32" name="position['+id+']"/>'; 
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-companies' && entity_type=='companies'){
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="companies" >';
					html_var += ''+getCompanyDropdown(id)+''; //note: populated in ecosystem.php
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-companies' && entity_type=='events'){//added by Ajay on 3-july
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="companies" >';
					html_var += ''+getEventCompanyDropdown(id)+''; //note: populated in ecosystem.php
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-people' && entity_type=='events'){//added by Ajay on 3-july
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="companies" >';
					html_var += ''+getEventPeopleDropdown(id)+''; //note: populated in ecosystem.php
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-events' && entity_type=='companies'){//added by Ajay on 5-july
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="events" >';
					html_var += ''+getEventCompanyDropdown(id)+''; //note: populated in ecosystem.php
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-events' && entity_type=='people'){//added by Ajay on 5-july
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="events" >';
					html_var += ''+getEventPeopleDropdown(id)+''; //note: populated in ecosystem.php
				}
				else if(currentVisibleDivLayer['ecosystem']=='ecosystem-events' && entity_type=='events'){				
					html_var += '<input  type="hidden" name="entity2_type['+id+']" value="events" >';
					html_var += ''+getEventDropdown(id)+''; //note: event_dropdown variable is populated in ecosystem.php
				}

				//html_var += '<input type="button" name="submitPosition" value="Edit" onclick="savePosition()"></td>';
				html_var += '</tr>';

				
		}
		else{
			var html_var =  '<table width="100%" valign="top">';
				html_var += '<tr id="'+row_id+'"><td width="13px"><div id="delete-link">';
				html_var += '<a href="#" onclick="deleteCurrentRow(\''+row_id+'\')">&nbsp;&nbsp;</a></div></td>';
				html_var += '<td >';
				html_var += '<input style="display:none" type="checkbox" checked="" value="'+id+'" name="'+checkbox_name+'"/>';
				html_var += '</td><td >'+title+'</td></tr>';
		}
		//new_element.innerHTML = '<input type="checkbox" name="'+checkbox_name+'" value='+id+' CHECKED >'+title;
		new_element.innerHTML = html_var;

		container.insertBefore(new_element, container.firstChild);
		

		if(message !=null){
			message.style.display="none";
		}
	}

	list_div.style.display = "none";
	return;
}

function makeAjax(keyword,source_type,source_id){

			if ( window.XMLHttpRequest ) {
				x = new XMLHttpRequest();
			} else {
				try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
			}

			if (x) {
				x.onreadystatechange = function() {
					var r, h;
					if ( x.readyState == 4 ) {
						r = x.responseText;//.substr(0, 18);
						h = x.getResponseHeader('Content-Encoding');
						showResults(r, h, keyword);
					}
				}
				//currentVisibleDivLayer['ecosystem'] will havevalues like (ecosystem-companies,ecosystem-people )
				x.open('GET', 'admin-ajax.php?action=ajax-ecosystem-search&search_type='+currentVisibleDivLayer['ecosystem']+'&tax=post_tag&q='+keyword+'&source_type='+source_type+'&source_id='+source_id+'&'+(new Date()).getTime(), true);
				x.send('');
			}
}

function showResults(r, h, keyword,front_end_javascript_call){
	//alert('ajax response:'+r);
	//var list_div = document.getElementById("company-list");
	if(suggest_box_from_regular_page!=''){
		var list_div = document.getElementById(suggest_box_from_regular_page+"-list"); //company-list
	}
	else{
		var list_div = document.getElementById("company-list");
	}
	
	var add_link_text = "Add new company";
	var add_link = "Add new company";

	if(currentVisibleDivLayer['ecosystem']=='ecosystem-companies'){
		add_link_text = "Add new company";

		add_link = "admin.php?page=company-page";
		if(front_end_javascript_call){
			add_link = "/companies/?addnew=1";
		}
	}
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-people'){
		add_link_text = "Add new person";
		add_link = "admin.php?page=person-page";
		if(front_end_javascript_call){
			add_link = "/people/?addnew=1";
		}
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-exits'){
		add_link_text = "Add new company exit";
		add_link = "admin.php?page=exit-page";
		if(front_end_javascript_call){
			add_link = "/exits/?addnew=1";
		}
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-investments'){
		add_link_text = "Add new investment";
		add_link = "admin.php?page=investment-page";
		if(front_end_javascript_call){
			add_link = "/investments/?addnew=1";
		}
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-events'){
		add_link_text = "Add new event";
		add_link = "admin.php?page=event-page";
		if(front_end_javascript_call){
			add_link = "/events/?addnew=1";
		}
	}
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-industries'){
		add_link_text = "Add new industry";
		add_link = "admin.php?page=list-industries";
		if(front_end_javascript_call){
			add_link = "/ecosystem";
		}
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-offices'){
		add_link_text = "Add new Office";
		add_link = "admin.php?page=office-page";
		if(front_end_javascript_call){
			add_link = "/ecosystem";
		}
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-resources'){
		add_link_text = "Add new Resource";
		add_link = "admin.php?page=resource-page";
		if(front_end_javascript_call){
			add_link = "/ecosystem";
		}
	} 
	else if(currentVisibleDivLayer['ecosystem']=='ecosystem-resource-categories'){
		add_link_text = "Add new Resource Category";
		add_link = "admin.php?page=list-categories";
		if(front_end_javascript_call){
			add_link = "/ecosystem";
		}
	} 

	list_div.innerHTML = r;
	list_div.innerHTML += "<br><hr>";
	list_div.innerHTML += "<a target='_blank' href='"+add_link+"'>+ "+add_link_text+"</a> | <a href='javascript:closeAjaxResults()'>[x] Close</a>";
	list_div.innerHTML += "<br><br>";
}


var currentVisibleTabLayer = new Array();
var currentVisibleDivLayer = new Array();
function showhideLayer(sectionName, tabDiv, contentDiv, tabSelectedClass, tabUnSelectedClass){
	if(currentVisibleDivLayer[sectionName] != null && currentVisibleDivLayer[sectionName] != ""){
		document.getElementById(currentVisibleDivLayer[sectionName]).style.display = "none";
		document.getElementById(currentVisibleTabLayer[sectionName]).className =tabUnSelectedClass;
	}
	document.getElementById(contentDiv).style.display = "block";
	document.getElementById(tabDiv).className =tabSelectedClass;
	currentVisibleDivLayer[sectionName] = contentDiv;
	currentVisibleTabLayer[sectionName] = tabDiv;
}

function closeAjaxResults(){
	if(suggest_box_from_regular_page!=''){
		var list_div = document.getElementById(suggest_box_from_regular_page+"-list"); //company-list
	}
	else{
		var list_div = document.getElementById("company-list");
	}
	list_div.style.display = "none";
}


function deleteRelation(row_id,entity1_id,entity2_id,entity1_type,entity2_type){

	var confirmed = confirm('Do you want to delete relationship?');
	if(confirmed){
		var ajax_url= 'admin-ajax.php?action=ajax-ecosystem-delete&entity1_id='+entity1_id+'&entity2_id='+entity2_id+'&entity1_type='+entity1_type+'&entity2_type='+entity2_type+'&'+(new Date()).getTime();
		makeDeleteAjax(row_id,ajax_url);

		return false;
	}
	else{
		return false;
	}
	/*
	var list_div = document.getElementById("company-list");
	var keyword = fld;//document.getElementById("new-search-eco_tag");
	if(fld.value.length > 1 ){
		list_div.style.display = "block";
		var company_name = "Displaying company 1";
		var ajax_url= 'admin-ajax.php?action=ajax-ecosystem-search&search_type='+currentVisibleDivLayer['ecosystem']+'&tax=post_tag&q='+keyword+'&'+(new Date()).getTime();
		makeDeleteAjax(keyword.value);
	}
	*/
}

function makeDeleteAjax(row_id,ajax_url){

			if ( window.XMLHttpRequest ) {
				x = new XMLHttpRequest();
			} else {
				try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
			}

			if (x) {
				x.onreadystatechange = function() {
					var r, h;
					if ( x.readyState == 4 ) {
						r = x.responseText;//.substr(0, 18);
						h = x.getResponseHeader('Content-Encoding');
						actionAfterDelete(row_id,r);
					}
				}
				//currentVisibleDivLayer['ecosystem'] will havevalues like (ecosystem-companies,ecosystem-people )
				x.open('GET', ajax_url, true);
				x.send('');
			}
}

function actionAfterDelete(row_id,r){

	//alert('Delete:'+row_id+'-'+r.replace(/^\s*|\s*$/,""));

	r = r.replace(/^\s*|\s*$/,"");

	if(r=="1"){
		var node = document.getElementById(row_id);
		node.parentNode.removeChild(node);

		alert('Relationship has been deleted');
		//alert(document.getElementById(row_id));
	}
	else{
		alert('Some error occured.');
	}
}

function deleteCurrentRow(row_id){

	var confirmed = confirm('Do you want to delete relationship?');
	if(confirmed){
		var node = document.getElementById(row_id);
		node.parentNode.removeChild(node);
		alert('Relationship has been deleted');
	}
}

function callAjaxURL(ajaxurl,div_id){
			//alert(div_id+'ajax call:'+ajaxurl);
			if ( window.XMLHttpRequest ) {
				x = new XMLHttpRequest();
			} else {
				try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
			}

			if (x) {
				x.onreadystatechange = function() {
					var ajax_response, h;
					if ( x.readyState == 4 ) {
						ajax_response = x.responseText;//.substr(0, 18);
						h = x.getResponseHeader('Content-Encoding');
						displayResults(ajax_response, h, div_id);
					}
				}
				//currentVisibleDivLayer['ecosystem'] will havevalues like (ecosystem-companies,ecosystem-people )
				x.open('GET', ajaxurl+'&'+(new Date()).getTime(), true);
				x.send('');
			}
}

function displayResults(ajax_response, h, div_id){
	//alert('displayResults');
	var div_obj = document.getElementById(div_id);
	div_obj.innerHTML = ajax_response;
	
	//var div_obj = document.getElementById(div_id);
	//div_obj.innerHTML = ajax_response;
	var script_content = div_obj.getElementsByTagName("script");
	if(script_content !=null){
		for (i=0;i<=script_content.length; i++){
			eval(script_content[i].innerHTML);
		}
	}
	
	return;
}

function expand_entity(entity_type, entity_id, div_id){
	//alert('expand_entity');
	var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-ajax.php?action=ajax-ecosystem-expand&search_type='+entity_type+'&search_id='+entity_id+'&tax=post_tag&q=test';
	callAjaxURL(ajax_url,div_id);
	return;
}


//Edit frontend entities
var editlinks_visible = false;
function editAll(){
	if(editlinks_visible){
		$("a.editlink").css({display: 'none'});
		$("br.editlink").css({display: 'none'});
		$("h4.editlink-h4").css({display: 'none'});

		if(overview_tab_active==false) $("li.tab_overview").css({display:'none'})
		if(book_tab_active==false) $("li.tab_bookw").css({display:'none'})
		if(fin_tab_active==false) $("li.tab_financial").css({display:'none'})	
		if(news_tab_active==false) $("li.tab_news").css({display:'none'});
		if(buzz_tab_active==false) $("li.tab_buzz").css({display:'none'});
		if(web_tab_active==false) $("li.tab_webstats").css({display:'none'});
		if(watch_tab_active==false) $("li.tab_watchlists").css({display:'none'});
		if(updates_tab_active==false) $("li.tab_updates").css({display:'none'});
		if(patents_tab_active==false) $("li.tab_patents").css({display:'none'});
		if(search_tab_active==false) $("li.tab_search").css({display:'none'});
		if(related_tab_active==false) $("li.related_search").css({display:'none'});
		if(video_tab_active==false) $("li.tab_video").css({display:'none'});

		editlinks_visible = false;						
	}
	else{
		$("a.editlink").css({display: 'inline'});
		$("br.editlink").css({display: 'block'});
		$("li.editlink").css({display:'block'});
		if(watch_tab_active==false) $("li.tab_watchlists").css({display:'none'}); //there is no need to "edit" watchlists
		if(updates_tab_active==false) $("li.tab_updates").css({display:'none'}); //there is no need to "edit" updates
		$("h4.editlink-h4").css({display: 'block'});
		editlinks_visible = true;
	}
        
}

var currentHiddeneditor = "";
function editEntity(entity_type,entity_id,meta_key,editor,meta_value, formname,messageframeid,editEntity,category){
	//ecosystem-update-ajax.php?entity_type=companies&entity_id=86&meta_key=description&meta_value=newdescription
        //alert(editor);
        if(editor){
		if(editor=="1"){ 
			$("#promptMessage").css({display: 'none'});
			$("#prompt").css({display: 'block'});
			document.saveData.editor_type.value = "1";
			document.saveData.meta_key.value = meta_key;
			hideAlleditors();
			//$("table.mceEditor").css({display: 'none'});
			//alert(1);
			if(currentHiddeneditor!=""){
				//alert(2);
				document.getElementById(currentHiddeneditor).style.display="none";
			}
			//alert(3);
			
			var editor_div = meta_value+"_parent";
			//alert(editor_div);
			document.getElementById(editor_div).style.display="block";
			//alert(4);
			currentHiddeneditor = editor_div;
			$("#editoroverlay").overlay().load();
			$("#editoroverlay").css({left:'center'});
			$("#editoroverlay").css({top:'30%'});
			//$("#editoroverlay").css('margin-left', '142px');
		}
		else if(editor=="2"){
			//multiple entity display
			//alert(formname);
			if(meta_key=='rss' && category!=''){			
				$("#div-"+meta_key+category).css({display: 'block'});
				$("#"+messageframeid+category).css({display: 'none'});
			}
			else{
				$("#div-"+meta_key).css({display: 'block'});
				$("#"+messageframeid).css({display: 'none'});
			}
			//alert(1);
			formname = formname.replace("-","_");
			var frmName = "document."+formname;
			fromObj = eval(frmName);
			//alert(eval("document."+formname+".editor_type"));	
			fromObj.editor_type.value = "2";
			fromObj.meta_key.value = meta_key;
			//hideAlleditors();
			if(currentHiddeneditor!=""){
				document.getElementById(currentHiddeneditor).style.display="none";
			}
			if(meta_key=='rss' && category!=''){			
				var editor_div = meta_value+category;
			}
			else{
				var editor_div = meta_value;
			}
			//alert('editor_div:'+editor_div);
			document.getElementById(editor_div).style.display="block";
			currentHiddeneditor = editor_div;
			//alert("#"+editor_div);
			$("#"+editor_div).overlay().load();
			//$("#editoroverlay").css('margin-left', '142px');
			$("#"+editor_div).css({left:'center'});
			$("#"+editor_div).css({top:'30%'});
		}
	}
	else{
		//show content passed
	}
}

function saveEntity(entity_type,entity_id,meta_key,meta_id){
	var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type='+entity_type+'&entity_id='+entity_id+'&meta_key='+meta_key;
	//alert('ajax url:'+ajax_url);
	document.saveData.action=ajax_url;
	document.saveData.submit();

	$("#prompt").css({display: 'none'});
	$("#promptMessage").css({display: 'block'});

	/*meta_value = $("#tinymce").html();
	var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type='+entity_type+'&entity_id='+entity_id+'&meta_key='+meta_key+'&meta_value='+meta_value+'';
	callAjaxURL(ajax_url,'overlay');
	//$("#prompt").css({display: 'none'});
	*/
}

function saveMultipleEntity(entity_type,entity_id,meta_key,meta_id,formname,messageframeid,category){
	var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type='+entity_type+'&entity_id='+entity_id+'&meta_key='+meta_key;
	//alert('ajax url:'+ajax_url);
	formname = formname.replace("-","_");
	//alert(formname);
	var frmName = "document."+formname;
	fromObj = eval(frmName);

	fromObj.action=ajax_url;
	fromObj.submit();
	if(formname.indexOf('_subsidiary',0)> 1){
		meta_key = meta_key+'_subsidiary';
	}
	if(formname.indexOf('_parent',0) > 1){
		meta_key = meta_key+'_parent';
	}
	
	if(meta_key=='rss' && category!=''){			
		$("#div-"+meta_key+category).css({display: 'none'});		
	}
	else{
		$("#div-"+meta_key).css({display: 'none'});
	}
	
	$("#"+messageframeid).css({display: 'block'});

}

function closepopup(div_id){
	if(div_id==null || div_id=='undefined'){
		$("#editoroverlay").overlay().close();
	}
	else{
		//alert(div_id);
		$("#"+div_id).overlay().close();
		//$("div.modal").overlay().close();
	}
}

function closeandrefreshpopup(div_id){
	if(div_id==null || div_id=='undefined'){
		$("#editoroverlay").overlay().close();
	}
	else{
		//alert(div_id);
		$("#"+div_id).overlay().close();
		//$("div.modal").overlay().close();
	}
	window.location.reload()
}

function openUserAssociation(newuser){
	$("#user_association").overlay().load();
	$("#user_association").css({left:'center'});
	$("#user_association").css({top:'30%'});
	$("#div-user_association").css({display: 'block'});
	$("#promptMessageMultiple-user_association").css({display: 'none'});
	//alert(newuser);
	claimUserProfile(newuser);
}

function openEmailPassword(emailChange){
	$("#change_email_password").overlay().load();
	//$("#editoroverlay").css('margin-left', '142px');
	$("#change_email_password").css({left:'center'});
	$("#change_email_password").css({top:'30%'});
	$("#div-change_email_password").css({display: 'block'});
	$("#promptMessageMultiple-change_email_password").css({display: 'none'});
	//alert(newuser);
	showHideEmailPassword(emailChange);
}

function showHideEmailPassword(val){
	if(val==0){
		document.getElementById("change-email").style.display = "block";
		document.getElementById("change-password").style.display = "none";

		//document.getElementById("subscribername").disabled = true;
		document.getElementById("change_type").value="email";
	}
	else{
		document.getElementById("change-email").style.display = "none";
		document.getElementById("change-password").style.display = "block";
		document.getElementById("change_type").value="password";

		//document.getElementById("subscribername").disabled = false;
	}
	//alert(val);
}

function saveEmailPassword(){
	$("#div-change_email_password").css({display: 'none'});
	$("#promptMessageMultiple-change_email_password").css({display: 'block'});

	document.frmchange_email_password.submit();
}


function linkUser(){
	$("#div-user_association").css({display: 'none'});
	$("#promptMessageMultiple-user_association").css({display: 'block'});
	$("#user_association").css({height: '150px'});
	document.frmuserlink.submit();
}

function saveNewEntity(){
	if( document.frmecosystem.entity_name.value ==''){
		alert('Please enter the name of the entity.');
		document.frmecosystem.entity_name.focus();
		return;
	}
	
	var entity_type = document.frmecosystem.entity_type.value;
	if(entity_type !='people' && entity_type !='investments' && entity_type !='exits'){
		if( document.frmecosystem.entity_website.value ==''){
			alert('Please enter the website.');
			document.frmecosystem.entity_website.focus();
			return;
		}		
	}
	$("#div-ecosystem").css({display: 'none'});
	$("#promptMessageMultiple-ecosystem").css({display: 'block'});

	document.frmecosystem.submit();
}

///new after jquery plugin
function addtoNewWatchList(watchlist_id,entity_id,entity_type){
		//alert('showing new form');
		if(watchlist_id=='new'){
			$("#watchlist_form").overlay().load();
			$("#watchlist_form").css({left:'center'});
			$("#watchlist_form").css({top:'30%'});
		}
		else if(watchlist_id ==''){
			return;
		}
		else{
			savewatchlist(entity_id,entity_type,0,watchlist_id,$("#status").fieldValue());
			//$("#watchlist_message").overlay().load();
		}
}

function addtoNewWatchListOld(entity_id,entity_type){
		//alert('showing new form');
		if($("#watchlist_id").fieldValue()=='new'){
			$("#watchlist_form").overlay().load();
			$("#watchlist_form").css({left:'center'});
			$("#watchlist_form").css({top:'30%'});
		}
		else if($("#watchlist_id").fieldValue() ==''){
			return;
		}
		else{
			savewatchlist(entity_id,entity_type,0,$("#watchlist_id").fieldValue(),$("#status").fieldValue());
			//$("#watchlist_message").overlay().load();
		}
}

function addtoNewWatchListFromResults(fld,entity_id,entity_type){
		//alert('showing new form');
		document.watchlist_frm.entity_id.value=entity_id;
		document.watchlist_frm.entity_type.value=entity_type;


		if(fld.value=='new'){
			$("#watchlist_form").overlay().load();
			$("#watchlist_form").css({left:'center'});
			$("#watchlist_form").css({top:'30%'});
		}
		else if(fld.value ==''){
			return;
		}
		else{
			savewatchlist(entity_id,entity_type,0,fld.value,document.watchlist_frm.status.value);
			//$("#watchlist_message").overlay().load();
		}
}

/*
function addtoNewWatchListFromResults(){
		fld = document.watchlist_select_form.watchlist_id.value;
		//alert(fld);
		if(fld=='new'){
			$("#watchlist_form").overlay().load();
			$("#watchlist_form").css({top:'100px',left:'300px'});
		}
		else if(fld ==''){
			alert('Please select watchlist or Create new option.');
			document.watchlist_select_form.watchlist_id.focus();
			return;
		}
		else{
			savewatchlist(document.watchlist_frm.entity_id.value,document.watchlist_frm.entity_type.value,0,fld,document.watchlist_frm.status.value,document.watchlist_frm.watch_description.value,document.watchlist_frm.watchlist_source.value,document.watchlist_frm.watchlist_date.value);
			//$("#watchlist_message").overlay().load();
		}

}

*/
function addtoNewWatchListOverlay(entity_id,entity_type){
	
		//alert('showing new form');
		document.watchlist_frm.entity_id.value=entity_id;
		document.watchlist_frm.entity_type.value=entity_type;
		$("#watchlist_lists_content").html('<p align="center">Loading Watchlists. Please wait..</p>');

		$("#watchlist_lists").overlay().load();
		$("#watchlist_lists").css({top:'30%',left:'center',height:'250px'});
		var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-ajax.php?action=get-ecosystem-watchlist&entity_type='+entity_type+'&entity_id='+entity_id;
		callAjaxURL(ajax_url,'watchlist_lists_content');

		/*

		if(fld.value=='new'){
			$("#watchlist_form").overlay().load();
			$("#watchlist_form").css({left:'300px'});
		}
		else if(fld.value ==''){
			return;
		}
		else{
			savewatchlist(entity_id,entity_type,0,fld.value,document.watchlist_frm.status.value,document.watchlist_frm.watch_description.value,document.watchlist_frm.watchlist_source.value,document.watchlist_frm.watchlist_date.value);
			//$("#watchlist_message").overlay().load();
		}
		*/

}


function savewatchlistFromResults(is_new,watchlist_value,status,watch_description,watchlist_source,watchlist_date){
	savewatchlist(document.watchlist_frm.entity_id.value,document.watchlist_frm.entity_type.value,is_new,watchlist_value,status,watch_description,watchlist_source,watchlist_date);
}

function savewatchlist(entity_id,entity_type,is_new,watchlist_value,status,watch_description,watchlist_source,watchlist_date){

	var collaborative = '0';
	$("#watchlist_form").overlay().close();
	$("#watchlist_message_div").html('Processing request. Please wait...');
	if(is_new=='1'){
		if (watch_description=='' || watch_description==null){
			watch_description='';
		}
		if (watchlist_source=='' || watchlist_source==null){
			watchlist_source='';
		}
		if (watchlist_date=='' || watchlist_date==null){
			watchlist_date='';
		}
		//watchlist_value = $("#watchlist_name").fieldValue();
		watchlist_value = document.watchlist_frm.watchlist_name.value;
		var status = 'public';
		if(document.watchlist_frm.status[0].checked) status = 'private';
		//if(document.watchlist_frm.status[2].checked) status = 'invisible';

		var watchlist_display = 'public';
		if(document.watchlist_frm.watchlist_display[1].checked) watchlist_display = 'private';
		
		if(document.watchlist_frm.collaborative.checked) collaborative = '1';
	}

	$("#watchlist_message").overlay().load();

	$("#watchlist_message").css({left:'center'});
	$("#watchlist_message").css({top:'30%'});

	var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type=watchlist&ci_entity_type='+entity_type+'&entity_id='+entity_id+'&is_new='+is_new+'&watchlist_value='+watchlist_value+'&status='+status+'&watchlist_display='+watchlist_display+'&watch_description='+watch_description+'&watchlist_source='+watchlist_source+'&watchlist_date='+watchlist_date+'&collaborative='+collaborative;
	//alert(ajax_url);
	callAjaxURL(ajax_url,'watchlist_message_div');

}

function closewatchlistpopup(refresh_page){
	$("#watchlist_form").overlay().close();
	$("#watchlist_message").overlay().close();
	if(refresh_page==1){
		window.location.reload()
	}
}



function deleteWatchlistRow(row_id,watchlist_id,entity_type,entity_id){

	var confirmed = confirm('Do you want to delete relationship?');
	if(confirmed){
		var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type=watchlist-item-delete&watchlist_id='+watchlist_id+'&ci_entity_type='+entity_type+'&entity_id='+entity_id;
		callAjaxURL(ajax_url,'watchlist-message');

		var node = document.getElementById(row_id);
		node.parentNode.removeChild(node);
		alert('Relationship has been deleted');
	}
	return false;
}

function deleteWatchlistSubscriber(row_id,watchlist_id,entity_type,entity_id){

	var confirmed = confirm('Do you want to delete relationship?');
	if(confirmed){
		var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type=watchlist-subscriber-delete&watchlist_id='+watchlist_id+'&ci_entity_type='+entity_type+'&entity_id='+entity_id;
		callAjaxURL(ajax_url,'watchlist-message');

		var node = document.getElementById(row_id);
		node.parentNode.removeChild(node);
		alert('Relationship has been deleted');
	}
	return false;
}

function deleteDateRow(message_div_id,row_id,date_id,entity_type,entity_id){

	var confirmed = confirm('Do you want to delete date?');
	if(confirmed){
		$("#"+message_div_id).html('Please wait processing delete request.');
		var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type=date-item-delete&date_id='+date_id+'&ci_entity_type='+entity_type+'&entity_id='+entity_id;
		callAjaxURL(ajax_url,message_div_id);

		var node = document.getElementById(row_id);
		node.parentNode.removeChild(node);
		//alert('Relationship has been deleted');
		//$("#"+message_div_id).html('Date has been deleted.');
	}
	return false;
}

function loginToSystem(link_url,current_page_redirect){
	//alert('showing new form');
	$("#loginoverlay").overlay().load();
	//$("#loginoverlay").css({left:'142px'});
	$("#editoroverlay").css('left', 'center');
	$("#editoroverlay").css('top', '30%');
	var wrap = $("div.wrap");
	if(current_page_redirect=="1"){
		//alert(window.location.href);
		link_url += "?redirect_to="+window.location.href;
		//alert(link_url);
	}
	//redirect_to=
	var html_content = '<iframe style="border:0" width="100%" height="100%" scrolling="no" src="'+link_url+'"></iframe>';
	$("div.wrap").html(html_content);

	// load the page specified in the trigger
	//wrap.load(link_url);
	//alert('done');	
	//return false;
}

function logoutToSystem(link_url){
		//link_url += "&redirect_to="+window.location.href;
		link_url += "&redirect_to=/";
		window.location.href = link_url;
}

function closeloginpopup(){
	$("#loginoverlay").overlay().close();
}


function suggestPeople(fld,watchlist_id,div_id,newuser){

	var list_div = document.getElementById(div_id);

	var keyword = fld;
	if(fld.value.length > 1 ){
		//alert('ajax call to populate companies');
		list_div.style.display = "block";
		//var company_name = "Displaying company "+Math.random();
		//var company_name = "Displaying company 1";
		var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type=people-subscribers&watchlist_id='+watchlist_id+'&keyword='+fld.value+'&newuser='+newuser;
		callAjaxURL(ajax_url,div_id);

	}
}

function suggestMultiplePeople(fld,watchlist_id,div_id,newuser){

	var list_div = document.getElementById(div_id);

	var keyword = fld;
	if(fld.value.length > 1 ){
		var str_arr = fld.value.split(",");
		
		//alert("Value:"+);
		var search_str = str_arr[str_arr.length-1];
		//alert('ajax call to populate companies');
		list_div.style.display = "block";
		//var company_name = "Displaying company "+Math.random();
		//var company_name = "Displaying company 1";
		var ajax_url = '/wp-content/plugins/ecosystem/ecosystem-update-ajax.php?entity_type=people-subscribers&watchlist_id='+watchlist_id+'&keyword='+search_str+'&newuser='+newuser+'&multipleNames=1';
		callAjaxURL(ajax_url,div_id);

	}
}

function selectPerson(people_id,name,email){
	document.getElementById("people_id").value = people_id;	
	document.getElementById("subscribername").value = name;	
	if(document.getElementById("subscriberemail")!=null){
		document.getElementById("subscriberemail").value = email;	
	}
	$("#people-list").css({display:'none'});
	//document.getElementById("people-list").style.display="none";
}


function selectMultiplePerson(people_id,name,email){
	//document.frmwatchlist.people_id.value = people_id;	
	//document.getElementById("subscribername").value = name;	
	document.frmwatchlist.multipleNames.value="1";
	if(document.frmwatchlist.people_id!=null){
		if(document.frmwatchlist.people_id.value ==''){
			document.frmwatchlist.people_id.value = people_id;	
			//alert('Set Single:'+document.frmwatchlist.people_id.value);
		}
		else{
			document.frmwatchlist.people_id.value = document.frmwatchlist.people_id.value + ","+ people_id;	
			//alert('Multiple List:'+document.frmwatchlist.people_id.value);
		}
		document.frmwatchlist.subscribername.value +=', ';
		document.frmwatchlist.subscribername.focus();
	}
	$("#people-list").css({display:'none'});
	//document.getElementById("people-list").style.display="none";
}

function closeSuggestBox(){
	$("#people-list").css({display:'none'});
	//document.getElementById("people-list").style.display="none";
}

function validateLinkForm(frm){
	if(frm.claimuser[0].checked){
		if(frm.subscribername.value ==''){
			alert('Please enter subscriber name');
			frm.subscribername.focus();
			return false;
		}
		else if(frm.people_id.value ==''){
			alert('Please choose valid person using autosuggest');
			frm.subscribername.focus();
			return false;
		}

	}
	return true;

}

function validateShareForm(frm){
	if(frm.subscribername == null){
		return true;
	}
	else if(frm.subscribername.value ==''){
		alert('Please enter subscriber name');
		frm.subscribername.focus();
		return false;
	}
	else if(frm.people_id.value =='' && frm.subscriberemail.value ==''){
		alert('Please enter subscriber\' email');
		frm.subscriberemail.focus();
		return false;
	}
	else{
		return true;
	}

}


function claimprofile(val){
	if(val==0){
		document.getElementById("subscribername").disabled = true;
	}
	else{
		document.getElementById("subscribername").disabled = false;
	}
}

function claimUserProfile(val){
	if(val==0){
		document.getElementById("enter-user").style.display = "block";
		document.getElementById("suggest-user").style.display = "none";

		//document.getElementById("subscribername").disabled = true;
		document.getElementById("people_id").value="0";
	}
	else{
		document.getElementById("enter-user").style.display = "none";
		document.getElementById("suggest-user").style.display = "block";

		//document.getElementById("subscribername").disabled = false;
	}
	//alert(val);
}

function addTableHeader(tableName,headerArray){
	var container = document.getElementById(tableName);
	var tr = container.insertRow(0);
	for ( var i in headerArray ){
		var th = document.createElement("th");
		var labelHead = document.createElement("p");
		labelHead.setAttribute("style","margin:0px;padding:0px;");		
		labelHead.innerHTML = headerArray[i];
		//optionCat.setAttribute("value",rss_options[i].value);		
		th.appendChild(labelHead);
		tr.appendChild(th);
	}
}

function addmoreEntity(tableName,pName,pValue,aName,aValue,aMeta_Id,cat_type, extra_value){
	var container = document.getElementById(tableName);
	var tr = container.insertRow(0);
	var tdchar = document.createElement("td");
	tdchar.setAttribute("nowrap","true");
	var tdtext = document.createElement("td");

	var inputHidName  = document.createElement("input");
	inputHidName.setAttribute("type","hidden");
	//inputName.setAttribute("name","web_presences_name[]");
	inputHidName.setAttribute("name","hid_"+aName+"[]");
	inputHidName.setAttribute("size","10");
	inputHidName.setAttribute("style","background-color: gray");
	if(tableName.indexOf('multiple-dates',0) !=-1){
		inputHidName.setAttribute("value",pName+"~~"+extra_value+"~~"+pValue+"~~"+cat_type);
	}
	else{
		inputHidName.setAttribute("value",pName+"~~"+pValue+"~~"+cat_type);
	}
	inputHidName.setAttribute("id","hid-entity-id-"+aMeta_Id);

	
	if(tableName.indexOf('multiple-dates',0) !=-1){
		var inputName  = document.createElement("input");
		inputName.setAttribute("type","text");
		inputName.setAttribute("name",aName+"[]");
		inputName.setAttribute("size","10");
		inputName.setAttribute("value",pName);
		inputName.setAttribute("id",aName+"_"+aMeta_Id);		
		inputName.setAttribute("class","date_picker_class");	
		
		//inputName.setAttribute("onfocus","attachDatePicker('"+aName+"_"+aMeta_Id+"')");	
		inputName.setAttribute("onclick","attachDatePicker('"+aName+"_"+aMeta_Id+"')");	
		
		//alert('extra_value'+extra_value);
		var inputNameTime  = document.createElement("input");
		inputNameTime.setAttribute("type","text");
		inputNameTime.setAttribute("name",aName+"Time[]");
		inputNameTime.setAttribute("size","6");
		inputNameTime.setAttribute("value",extra_value);
		inputNameTime.setAttribute("id","name");		
		inputNameTime.setAttribute("id",aName+"_time_"+aMeta_Id);		
		inputNameTime.setAttribute("class","time_picker_class");		
		//inputNameTime.setAttribute("onfocus","attachDatePicker()");	
		inputNameTime.setAttribute("onclick","attachTimePicker('"+aName+"_time_"+aMeta_Id+"')");	
	}
	else{
		var inputName  = document.createElement("input");
		inputName.setAttribute("type","text");
		//inputName.setAttribute("name","web_presences_name[]");
		inputName.setAttribute("name",aName+"[]");
		inputName.setAttribute("size","20");
		inputName.setAttribute("value",pName);
		inputName.setAttribute("id","name");		
	}

	var inputUrl  = document.createElement("input");
	inputUrl.setAttribute("type","text");
	//inputUrl.setAttribute("name","web_presences_url[]");
	inputUrl.setAttribute("name",aValue+"[]");
	inputUrl.setAttribute("size","30");
	inputUrl.setAttribute("value",pValue);
	inputUrl.setAttribute("id","name");


	var inputMeta_key_id  = document.createElement("input");
	inputMeta_key_id.setAttribute("type","hidden");
	//inputMeta_key_id.setAttribute("name","web_presences_url[]");
	inputMeta_key_id.setAttribute("name","meta_key_id[]");
	inputMeta_key_id.setAttribute("size","50");

	if(aMeta_Id !='' && aMeta_Id !='undefined'){
		inputMeta_key_id.setAttribute("value",aMeta_Id);
	}
	else{
		inputMeta_key_id.setAttribute("value","0");
	}
	inputMeta_key_id.setAttribute("id","name");

	tdchar.appendChild(inputHidName);
	tdchar.appendChild(inputName);
	if(tableName.indexOf('multiple-dates',0) !=-1){
		tdchar.appendChild(inputNameTime);
	}
	else{
		tdtext.appendChild(inputUrl); //added by ajay on jan 22 for swapping custom text and select
	}
	tdchar.appendChild(inputMeta_key_id);
	
	tr.appendChild(tdchar);
	//alert(tableName+"->"+tableName.indexOf('keywords',0));
	if(tableName.indexOf('keywords',0) != 0){
		tr.appendChild(tdtext);
	}
	
	//alert(tableName);
	//if(tableName.indexOf('rss',0) !=-1 || tableName.indexOf('web-presences',0) !=-1 || tableName.indexOf('multiple-dates',0) !=-1){
	if(tableName.indexOf('rss',0) !=-1 || tableName.indexOf('multiple-dates',0) !=-1){
		
		var tdselect = document.createElement("td");
		var selectCategory  = document.createElement("select");
		selectCategory.setAttribute("name","entity_category[]");
		selectCategory.setAttribute("id","name_category");
		
		if(tableName.indexOf('rss',0) !=-1){
			for ( var i in rss_options ){
				//alert( web_presences_options[i].value );
				var optionCat  = document.createElement("option");
				optionCat.setAttribute("value",rss_options[i].value);
				optionCat.text = rss_options[i].text;
				if(cat_type == rss_options[i].value){
					optionCat.setAttribute("SELECTED",'true');					
				}
	
				selectCategory.appendChild( optionCat);
			} 		
		}
		else if(tableName.indexOf('web-presences',0) !=-1){
			for ( var i in web_presences_options ){
				//alert( web_presences_options[i].value );
				var optionCat  = document.createElement("option");
				optionCat.setAttribute("value",web_presences_options[i].value);
				optionCat.text = web_presences_options[i].text;
				if(cat_type == web_presences_options[i].value){
					optionCat.setAttribute("SELECTED",'true');					
				}
	
				selectCategory.appendChild( optionCat);
			} 		
		}
		else if(tableName.indexOf('multiple-dates',0) !=-1){
			//alert(event_date_options);
			if(page_type=='funding'){
				//alert(funding_date_options);
				selectCategory.innerHTML = funding_date_options;
				//commented 
				//attachDatePicker(aName+"_"+aMeta_Id);
				//attachTimePicker(aName+"_time_"+aMeta_Id);
				
			}
			else{
				selectCategory.innerHTML = event_date_options;
				//commented by Ajay on 26 jan
				attachDatePicker(aName+"_"+aMeta_Id);
				//inputName.setAttribute("onclick","attachDatePicker('"+aName+"_"+aMeta_Id+"')");	
				attachTimePicker(aName+"_time_"+aMeta_Id);
				
			}
			//alert(selectCategory);			
			for ( var i in selectCategory.options ){
				if(cat_type == selectCategory.options[i].value){
					selectCategory.options[i].selected= true;
					break;
				}
			} 
			
		}

		if(tableName.indexOf('multiple-dates',0) !=-1){
			tdtext.appendChild(selectCategory); //added by ajay on jan 22 for swapping custom text and select
			tdselect.appendChild(inputUrl);
			tr.appendChild(tdselect);	
			//added by Ajay on 26 Jan
			attachDatePicker(aName+"_"+aMeta_Id);
			attachTimePicker(aName+"_time_"+aMeta_Id);

		}
		else{
			tdselect.appendChild(selectCategory);
			tr.appendChild(tdselect);
		}
	}

	if(tableName.indexOf('multiple-dates',0) !=-1){
		//attachDatePicker();
	}

	return;
}

function attachTimePicker(input_id){
	$("#"+input_id).time_input({
	  twelve_hour: false,
	  minute_chunk: 30,
	  start_hour: 1,
	  end_hour: 24
	});	
	//alert($("#"+input_id).position().top);
	var top_pos = $("#"+input_id).position().top + 50;
	//alert(top_pos);
	$(".time_selector").css({top:'90px',left:'175px',zIndex: '19998'});
	//$("#"+input_id).position().top = top_pos;
	//alert($("#"+input_id).position().top);
}

function attachDatePicker(input_id){
	//$("div.date_selector").date_input();
	$("#"+input_id).date_input();
	$.extend(DateInput.DEFAULT_OPTS, {
	  stringToDate: function(string) {
		var matches;
		if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
		  return new Date(matches[1], matches[2] - 1, matches[3]);
		} else {
		  return null;
		};
	  },
	
	  dateToString: function(date) {
		var month = (date.getMonth() + 1).toString();
		var dom = date.getDate().toString();
		if (month.length == 1) month = "0" + month;
		if (dom.length == 1) dom = "0" + dom;
		return date.getFullYear() + "-" + month + "-" + dom;
	  }
	});

	$("div.date_selector").css({top:'90px',left:'115px',zIndex: '19998'});
	//$("#"+input_id).css({top:'100px',left:'120px',zIndex: '19998'});
	//$("table.time_selector").css({top:'250px'});
	//$('input.end_date').dpSetOffset(-100, -100);
	
	
}

function openNewEcosystem(entity_type,overlay_title){

	$("#ecosystem-overlay").overlay().load();
	$("#ecosystem-overlay").css({left:'center'});
	$("#ecosystem-overlay").css({top:'30%'});
	$("#div-ecosystem").css({display: 'block'});
	$("#promptMessageMultiple-ecosystem").css({display: 'none'});

	document.frmecosystem.entity_type.value=entity_type;
	document.getElementById("overlay_title").innerHTML = overlay_title;
}

function savePosition(){
	ifrmPostiionSave.document.write("Saving data. Please wait");
	document.post.target="ifrmPostiionSave";
	document.post.submitPositionForm.value = "Edit";
	document.post.submit();
	document.post.target="";
	document.post.submitPositionForm.value = "";
	
}


function removePhoto(entity_type,entity_id,field_name){

			if ( window.XMLHttpRequest ) {
				x = new XMLHttpRequest();
			} else {
				try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
			}

			if (x) {
				x.onreadystatechange = function() {
					var r, h;
					if ( x.readyState == 4 ) {
						r = x.responseText;//.substr(0, 18);
						h = x.getResponseHeader('Content-Encoding');
						removeLogoFromPage(r,field_name);
					}
				}
				//currentVisibleDivLayer['ecosystem'] will havevalues like (ecosystem-companies,ecosystem-people )
				x.open('GET', 'admin-ajax.php?action=ajax-remove-logo&entity_type='+entity_type+'&entity_id='+entity_id+'&field_name='+field_name+'&'+(new Date()).getTime(), true);
				x.send('');
			}
}


function removeLogoFromPage(r,field_name){
	//alert(r);
	document.getElementById(field_name+"_image").innerHTML = r;
}



function Set_Cookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function get_amazon_data(){
	var asin = document.post.asin.value;
	if(asin ==''){
		alert('Please enter valid ASIN.');
		document.post.asin.focus();
		return;
	}
	//$("#asin").fieldValue()
	var div_id ="asin_data";
	var div_obj = document.getElementById(div_id);
	div_obj.innerHTML = 'Loading data. Please wait...';
	var ajax_url = 'admin-ajax.php?action=ajax-amazon-populate&asin='+asin;
	callAjaxURL(ajax_url,div_id);
	
	/*
	var div_obj = document.getElementById(div_id);
	div_obj.innerHTML = ajax_response;
	var script_content = div_obj.getElementsByTagName("script");
	if(script_content !=null){
		for (i=0;i<=script_content.length; i++){
			eval(script_content[i].innerHTML);
		}
	}
	*/
	return;
}

function scrape_metadata(){
	var url = document.post.website.value;
	
	var keywords = document.post.keywords.value;
	var description = tinyMCE.get('description').getContent();
	var eco_type = document.post.post_type.value;
	var entity_id = 0;
	
	if(url ==''){
		alert('Please enter a valid URL.');
		document.post.website.focus();
		return;
	}
	
	if(eco_type =='resources'){
		entity_id = document.post.resource_id.value;
	}
	else if(eco_type =='companies'){
		entity_id = document.post.company_id.value;
	}
	
	//$("#asin").fieldValue()
	var div_id ="scrape_data";
	var div_obj = document.getElementById(div_id);
	div_obj.innerHTML = 'Loading data. Please wait...';
	var ajax_url = 'admin-ajax.php?action=scrape-metadata&url='+url+'&eco_type='+eco_type+'&entity_id='+entity_id+'&keywords='+keywords+'&description='+description;
	callAjaxURL(ajax_url,div_id);
	
	/*
	var div_obj = document.getElementById(div_id);
	div_obj.innerHTML = ajax_response;
	var script_content = div_obj.getElementsByTagName("script");
	if(script_content !=null){
		for (i=0;i<=script_content.length; i++){
			eval(script_content[i].innerHTML);
		}
	}
	*/
	return;
}


					function getAllWatchlists(){
							var ajax_url= '/wp-content/plugins/ecosystem/sidebar-watchlist-ajax.php?action=watchlists&'+(new Date()).getTime();
							fetchWatchlistAjax(ajax_url,'watchlist-sidebar','all');
					}

					function unSubscribe(watch_id){
							var ajax_url= '/wp-content/plugins/ecosystem/sidebar-watchlist-ajax.php?action=unsubscribe&watchlist_id='+watch_id+'&'+(new Date()).getTime();
							fetchWatchlistAjax(ajax_url,watch_id,'unsubscribe');
					}

					function updateWatchStatus(watch_id,mode, fld){
							//alert(mode);
							val = "";
							if(mode=='visibility'){
								val = fld.value ;
							}
							else if(mode=='display'){
								//val = fld; //used when radio button
								val = fld.value ;
							}

							var ajax_url= '/wp-content/plugins/ecosystem/sidebar-watchlist-ajax.php?action=updatestatus&mode='+mode+'&value='+val+'&watchlist_id='+watch_id+'&'+(new Date()).getTime();
							finalMessage = 'Your watchlist has been updated.';
							
							fetchWatchlistAjax(ajax_url,'watchlist-sidebar','refresh-all',finalMessage,'watchlist-sidebar-message');
					}

					function updateWatchFormSubmit(watch_id,profile_fld, status_fld,div_id){
							var redirect_to = location.href;
							var profile_status_value = eval("document.watchlist_all_frm."+profile_fld).value;
							var status_value = eval("document.watchlist_all_frm."+status_fld).value;

							var ajax_url= '/wp-content/plugins/ecosystem/sidebar-watchlist-ajax.php?action=updatestatus&profile_status='+profile_status_value+'&status='+status_value+'&redirect_to='+redirect_to+'&watchlist_id='+watch_id+'&'+(new Date()).getTime();
							finalMessage = 'Your watchlist has been updated.';
							if(div_id==''){
								div_id = 'watchlist-sidebar';
							}
							fetchWatchlistAjax(ajax_url,div_id,'refresh-all',finalMessage,'watchlist-sidebar-message');
					}

					function deleteWatchFormSubmit(watch_id, div_id){
							var confirmFlag = confirm('Do you want to delete watchlist?');
							if(confirmFlag){
								var redirect_to = location.href;
	
								var ajax_url= '/wp-content/plugins/ecosystem/sidebar-watchlist-ajax.php?action=deletewatchlist&redirect_to='+redirect_to+'&watchlist_id='+watch_id+'&'+(new Date()).getTime();
								finalMessage = 'Watchlist has been deleted.';
								if(div_id==''){
									div_id = 'watchlist-sidebar';
								}
								
								//fetchWatchlistAjax(ajax_url,div_id,'refresh-all',finalMessage,div_id);
								var div_watch_row = document.getElementById(div_id);
								//div_watch_row.style.display="hidden";
								//$("#"+div_id).hide();
								fetchWatchlistAjax(ajax_url,div_id,'DELETE',finalMessage,div_id);
								
								div_watch_row.innerHTML = "Request is in process. Please wait.. ";
								/*
								setTimeout ( "", 1000 );
								
								if(div_id=='message' || div_id=='watchlist-sidebar-message'){
									//var status = ajax_response_value;
									//var status = jQuery.trim(div_watch_row.innerHTML);
									//alert('statusfinal:'+status+'-msgDiv:'+div_id);
									
									if(ajax_response_value=='success'){
										window.location.href = "/manage-watchlists?status=deletesuccess";
									}
									else{
										window.location.href = "/manage-watchlists?status=deletefailed";										
									}
									
									//watch_row = "watchlist_items_"+watch_id;
									//var div_watch_row = document.getElementById(watch_row);
									//$("#"+watch_row).hide();
									
								}
								*/
							}
							else{
								return;
							}
					}

					function fetchWatchlistAjax(ajax_url, div_id, action,finalMessage,msgDiv){

								if ( window.XMLHttpRequest ) {
									x = new XMLHttpRequest();
								} else {
									try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
								}

								if (x) {
									x.onreadystatechange = function() {
										var r, h;
										if ( x.readyState == 4 ) {
											r = x.responseText;//.substr(0, 18);
											h = x.getResponseHeader('Content-Encoding');
											//actionAfterDelete(row_id,r);
											renderSection(div_id,r,action,finalMessage,msgDiv);
											//eval(funName);
										}
									}
									//currentVisibleDivLayer['ecosystem'] will havevalues like (ecosystem-companies,ecosystem-people )
									x.open('GET', ajax_url, true);
									x.send('');
								}
					}

					function renderSection(div_id, ajax_response,action,finalMessage,msgDiv){
						//alert('Here');
						if(msgDiv==''){
							msgDiv="watchlist-sidebar-message"
						}
						var div_message_obj = document.getElementById(msgDiv);

						if(action=='unsubscribe'){
							var status = jQuery.trim(ajax_response);
							if(status=='success'){
								var div_obj_watch	= document.getElementById('dtl-'+div_id);
								var div_obj_dtl		= document.getElementById('watch-'+div_id);
								div_obj_watch.style.display= "none";
								div_obj_dtl.style.display= "none";
								//alert('1-success');
								div_message_obj.innerHTML = finalMessage;
							}
							else{
								alert('Error occured while updating. Please try again later');
							}
							//div_obj.innerHTML = ajax_response;
						}
						else if(action=='refresh-all'){
							var status = jQuery.trim(ajax_response);
							if(status=='success'){
								//alert('2-success');
								div_message_obj.innerHTML = finalMessage;
								getAllWatchlists();
							}
							else{
								alert('Error occured while updating. Please try again later');
							}
						}
						else if(action=='DELETE'){
							var status = jQuery.trim(ajax_response);
							//alert('First:'+status+'-msgDiv:'+msgDiv);
							//div_message_obj.innerHTML = status;
							//ajax_response_value = status;
							if(status=='success'){
								window.location.href = "/manage-watchlists?message=6";
							}
							else{
								window.location.href = "/manage-watchlists?message=deletefailed";										
							}
							
						}
						else{
							var div_obj = document.getElementById(div_id);
							div_obj.innerHTML = ajax_response;
							//alert(div_obj.getElementsByTagName("script"));
							var script_content = div_obj.getElementsByTagName("script");
							if(script_content !=null){
								for (i=0;i<=script_content.length; i++)
								{
									//alert(script_content[i].innerHTML);
									eval(script_content[i].innerHTML);
								}
							}
							
							//alert(ajax_response)
						}
						//alert('Hide');
						//$("div.viewport-bottom").css("{display:none}");
						//tooltip-icon
						//$("img.tooltip-icon").tooltip();
						//$("img.tooltip-icon").css("{display:none}");
						var api_tooltip = $("img.tooltip-icon").tooltip() 
						//alert(api_tooltip);
						api_tooltip.hide();
						//$("#user_association").css({left:'200px'});
						//document.body.style.cursor.moveBy(10,10);
						//$(document)
						//	   .mouseXPos(50)
						//	   .mouseYPos(50);
						
					}

					getAllWatchlists();

					
					function showHideWatchlist(watch_id){
						var div_obj = document.getElementById('dtl-'+watch_id);
						if(div_obj.style.display=='block'){
							div_obj.style.display= "none"; 
							document.getElementById('img-'+watch_id).innerHTML = '<img src="/wp-content/themes/maroon/images/plus.png" border="0">';
						}
						else{
							div_obj.style.display= "block"; 
							document.getElementById('img-'+watch_id).innerHTML = '<img src="/wp-content/themes/maroon/images/minus.png" border="0">';
						}
					}
