
function submitform(sHost)
{
 
  var sQuery = "";
  var sUri = "";
  var sDelimeter = "";
  var iFl = 0;
  var aWords = document.keyword_search.search_phrase.value.split(" ");
  for(i = 0; i < aWords.length; i++){
	aWords[i] = aWords[i].replace(/\-/g,'_');
    if(typeof(aWords[i]) !="undefined"){ 
		sDelimeter = (iFl >= 1) ? "-" : "";
    	sQuery += sDelimeter + aWords[i];
		iFl++
	}
  }
  sUri = sHost + "/search/" + sQuery;
  document.keyword_search.action = sUri;
  document.keyword_search.submit();
}

function filterResults(sHost,sOriginalQuery)
{
 
  var sQuery = "";
  var sUri = "";
  var sDelimeter = "";
  var iFl = 0;
  // extract search keywords from original query eg /activity/animals
  var aWords = sOriginalQuery.split(" ");
  for(i = 0; i < aWords.length; i++){
	aWords[i] = aWords[i].replace(/\-/g,'_');
    if(typeof(aWords[i]) !="undefined"){ 
		sDelimeter = (iFl >= 1) ? "-" : "";
    	sQuery += sDelimeter + aWords[i];
		iFl++
	}
  }
  // append keywords entered into search within results input
  var iFl = 0;
  var aWords = document.keyword_search.search_phrase.value.split(" ");
  for(i = 0; i < aWords.length; i++){
	aWords[i] = aWords[i].replace(/\-/g,'_');
    if(typeof(aWords[i]) !="undefined"){ 
		sDelimeter = (iFl >= 1) ? "-" : "";
    	sQuery += sDelimeter + aWords[i];
		iFl++
	}
  }

  sUri = sHost + "/search/" + sQuery;
  document.keyword_search.action = sUri;
  document.keyword_search.submit();
}


function sf(){document.keyword_search.search_phrase.focus()}


function doProjectSearchRequest(host) {
	var url = host + '/project_search_ajax.php';
	var aid = document.project_search.s_activity_id.value.toUpperCase();
	var ctn = document.project_search.s_continent_id.value.toUpperCase();
	var cty = document.project_search.s_country_id.value.toUpperCase();
	
	if ((aid == "NULL") && (ctn == "NULL") && (cty == "NULL")) return false;
	
	var pars = 'aid='+aid+'&ctn='+ctn+'&cty='+cty;

	var target = 'company';

	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onComplete: doProjectSearchResponse});
}

function doProjectSearchResponse(originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();	
	$('act_ddlist').innerHTML = data.act;
	$('ctn_ddlist').innerHTML = data.ctn;
	$('cty_ddlist').innerHTML = data.cty;

}


function setCountryDDList(host) {
	var url = host + '/country_list_ajax.php';
	var cid = document.project_search.s_continent_id.value;
	var aid = document.project_search.s_activity_id.value;
	
	var pars = 'cid='+cid+'&aid='+aid;
	var target = 'company';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onComplete: setCountryDDListResponse});
}

function setCountryDDListResponse(originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();	
	$('search_country_ddlist').innerHTML = data.msg;

}


function togglePopupPanel(name,id) {
	lightSwitch('popup_panel_bg');
	lightSwitch(name);
	positionElement(name);
	if (name == 'company') 	{
		var url = 'http://www.oneworld365.org/company.php';
		var pars = 'm=edit&id='+id;
		var target = 'company';	
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
	}
	if (name == 'placement') 	{
		var url = 'http://www.oneworld365.org/placement.php';
		var pars = 'm=edit&id='+id;
		var target = 'placement';	
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
	}
	if (name == 'user') 	{
		var url = 'http://www.oneworld365.org/user.php';
		var target = 'user';
		var pars = 'm=edit&id='+id;
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onLoading: null, onComplete: showResponseAddUser});
	}
}


function doSearch(type,word,offset) {
	$('status_search').innerHTML = '';
	var url = 'http://www.oneworld365.org/search.php';
	if (type == 'tag') {
		var pars = 's=tag&w='+word+'&offset='+offset;
		var pars2 = 'search_phrase='+word+'&type=placement&offset='+offset;
		// company search
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onLoading: showLoad, onComplete: showResponseSearchResult} );
		// placement search
		var myAjax2 = new Ajax.Request( url, {method: 'post', parameters: pars2, onLoading: showPlacementSearch, onComplete: showResSearchPlacement} );

	}
	if (type == 'keyword') {
		var pars = Form.serialize('keyword_search')+'&offset='+offset;
		var pars2 = pars + '&type=placement';
		// company search
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onLoading: showLoad, onComplete: showResponseSearchResult} );
		// placement search
		var myAjax2 = new Ajax.Request( url, {method: 'post', parameters: pars2, onLoading: showPlacementSearch, onComplete: showResSearchPlacement} );

	}
	if (type == 'advanced') {
		var pars = Form.serialize('adv_search')+'&offset='+offset;
		// company search
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onLoading: showLoad, onComplete: showResponseSearchResult} );
		// placement search not currently implemented
	}
	toggleUI('loading');
}

function go(url) {
	window.location = url;
}

function hitandgo(url,gid) {
	pageTracker._trackPageview(gid);
	var load = window.open(url,'','');
}


function doLogin() {
	$('status_login').innerHTML = '';
	var url = './login.php';
	var pars = Form.serialize('login_form');
	var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars,  onLoading: null, onComplete: showResponseLogin });
}

function doLogout(sUrl) {
	
	window.location = sUrl+'/logout.php?&url='+sUrl;
	 
}


function showCompanyProfile(id,referer) {
	$('company_profile').innerHTML = '';
	toggleUI('loading');
	var url = 'http://www.oneworld365.org/company.php';
	var pars = 'm=view&id='+id+'&referer='+referer;
	var target = 'company_profile';	
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponseCompanyProfile});

	// retrieve the placements for this company
	var url = 'http://www.oneworld365.org/search.php';
	var pars2 = 'type=placement&id='+id;
	var myAjax2 = new Ajax.Request( url, {method: 'post', parameters: pars2, onLoading: showPlacementSearch, onComplete: showResSearchPlacement} );

}

function showPlacementProfile(id,referer) {
	$('placement_profile').innerHTML = '';

	if (referer == 'placement_profile_search') {
		toggleUI('placement_profile_search');
		referer = 'search_result';
	} else {
		toggleUI('placement_profile');
	}

	var url = 'http://www.oneworld365.org/placement.php';
	var pars = 'm=view&id='+id+'&referer='+referer;
	var target = 'placement_profile';	
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: hideLoad});
}

function showMyAccount() {
	toggleUI('account_profile');
	var url = 'http://www.oneworld365.org/account.php';
	var pars = ''
	var target = 'account_profile';	
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}

function showPlacementEditList() {
	var url = 'http://www.oneworld365.org/placement.php';
	var id = document.forms.placement_edit_form.p_company.value;
	var pars = 'm=list&id=' + id;
	var target = 'placement_edit_list';	
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onLoading: showLoadPlacementEdit, onComplete: hideLoadPlacementEdit});
}

function showUserEditList() {
	var url = 'http://www.oneworld365.org/user.php';
	var id = document.forms.user_edit_form.p_company.value;
	var pars = 'm=list&id=' + id;
	var target = 'user_edit_list';	
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onLoading: null, onComplete: showEditUserList});
}


function toggleUI(state) {

	var UIState = new Object;

	if (state == 'homepage') {
		UIState.search_panel = 1;
		UIState.account_profile = 0;
		UIState.company_profile = 0;
		UIState.placement_profile = 0;
		UIState.search_result = 0;
		UIState.featured_companies = 1;
		UIState.featured_placements = 1;
		UIState.tag_cloud = 1;
		UIState.search_result_placement = 0;
		UIState.placement_titles = 1;
		UIState.browse_panel = 1;
		UIState.category_panel = 1;
		UIState.partnerships = 1;
		UIState.hp_intro_txt = 1;
	}

	if (state == 'company_profile') {
		UIState.search_panel = 1;
		UIState.account_profile = 0;
		UIState.company_profile = 1;
		UIState.placement_profile = 0;
		UIState.search_result = 0;
		UIState.featured_companies = 0;
		UIState.featured_placements = 0;
		UIState.tag_cloud = 0;
		UIState.search_result_placement = 1;
		UIState.placement_titles = 0;
		UIState.browse_panel = 0;
		UIState.category_panel = 0;
		UIState.partnerships = 0;
		UIState.hp_intro_txt = 0;
	}

	if (state == 'placement_profile') {
		UIState.search_panel = 1;
		UIState.account_profile = 0;
		UIState.company_profile = 0;
		UIState.placement_profile = 1;
		UIState.search_result = 0;
		UIState.featured_companies = 0;
		UIState.featured_placements = 1;
		UIState.tag_cloud = 0;
		UIState.search_result_placement = 0;
		UIState.placement_titles = 0;
		UIState.browse_panel = 0;
		UIState.category_panel = 0;
		UIState.partnerships = 0;
		UIState.hp_intro_txt = 0;
	}

	if (state == 'placement_profile_search') {
		UIState.search_panel = 1;
		UIState.account_profile = 0;
		UIState.company_profile = 0;
		UIState.placement_profile = 1;
		UIState.search_result = 0;
		UIState.featured_companies = 0;
		UIState.featured_placements = 0;
		UIState.tag_cloud = 0;
		UIState.search_result_placement = 1;
		UIState.placement_titles = 0;
		UIState.browse_panel = 0;
		UIState.category_panel = 0;
		UIState.partnerships = 0;
		UIState.hp_intro_txt = 0;
	}


	if (state == 'search_result') {
		UIState.search_panel = 1;
		UIState.account_profile = 0;
		UIState.company_profile = 0;
		UIState.placement_profile = 0;
		UIState.search_result = 1;
		UIState.featured_companies = 0;
		UIState.featured_placements = 0;
		UIState.tag_cloud = 0;
		UIState.search_result_placement = 1;
		UIState.placement_titles = 0;
		UIState.browse_panel = 0;
		UIState.category_panel = 0;
		UIState.partnerships = 0;
		UIState.hp_intro_txt = 0;
	}

	if (state == 'loading') {
		UIState.search_panel = 1;
		UIState.account_profile = 0;
		UIState.company_profile = 0;
		UIState.placement_profile = 0;
		UIState.search_result = 0;
		UIState.featured_companies = 0;
		UIState.featured_placements = 0;
		UIState.tag_cloud = 0;
		UIState.search_result_placement = 0;
		UIState.placement_titles = 0;
		UIState.browse_panel = 0;
		UIState.category_panel = 0;	
		UIState.partnerships = 0;
		UIState.hp_intro_txt = 0;
	}

	if (state == 'account_profile') {
		UIState.search_panel = 0;
		UIState.company_profile = 0;
		UIState.placement_profile = 0;
		UIState.search_result = 0;
		UIState.featured_companies = 0;
		UIState.featured_placements = 0;
		UIState.tag_cloud = 0;
		UIState.search_result_placement = 0;
		UIState.placement_titles = 0;
		UIState.browse_panel = 0;
		UIState.category_panel = 0;
		UIState.partnerships = 0;
		UIState.hp_intro_txt = 0;
		UIState.account_profile = 1;
	}

		

	setLightSwitch('search_result_placement',UIState.search_result_placement);
	setLightSwitch('search_panel',UIState.search_panel);
	setLightSwitch('company_profile',UIState.company_profile);
	setLightSwitch('placement_profile',UIState.placement_profile);
	setLightSwitch('search_result',UIState.search_result);
	setLightSwitch('featured_companies',UIState.featured_companies);
	setLightSwitch('featured_placements',UIState.featured_placements);
	setLightSwitch('tag_cloud',UIState.tag_cloud);
	setLightSwitch('placement_titles',UIState.placement_titles);
	setLightSwitch('browse_panel',UIState.browse_panel);
	setLightSwitch('category_panel',UIState.category_panel);
	setLightSwitch('partnerships',UIState.partnerships);
	setLightSwitch('account_profile',UIState.account_profile);
	setLightSwitch('hp_intro_txt',UIState.hp_intro_txt);
}

function setLightSwitch(e,state) {

	if (document.getElementById) {
		// this is the way the standards work
		if(document.getElementById(e) != null) {
			if(state == 0) {
				document.getElementById(e).style.visibility = "hidden";
				document.getElementById(e).style.display = "none";
			} else {
				document.getElementById(e).style.visibility = "visible";
				document.getElementById(e).style.display = "";
			}
		}
	} else if (document.all) {
		// this is the way old msie versions work
		if(document.all[e] != null ) {
			if(state == 0) {
				document.all[e].style.visibility = "hidden";
				document.all[e].style.display = "none";
			} else {
				document.all[e].style.visibility = "visible";
				document.all[e].style.display = "";
			}
		}
	} else if (document.layers) {
		// this is the way nn4 works
		if(state == 0) {
			if(document.layers[e].visibility != "hidden") {
				document.layers[e].visibility = "hidden";
			} else {
				document.layers[e].visibility = "visible";
			}
		}
	}
}


function handleUpdate(name) {
	if (name == 'COMPANY') {
		var url = 'http://www.oneworld365.org/company_server.php';
		var pars = Form.serialize('edit_company');
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onLoading: showLoadCompany, onComplete: showResponseUpdateCompany} );
	}

	if (name == 'PLACEMENT') {
		var url = 'http://www.oneworld365.org/placement_server.php';
		var pars = Form.serialize('edit_placement');
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onLoading: showLoadPlacement, onComplete: showResponseUpdatePlacement} );
	}
	if (name == 'USER') {
		$('status_add_user').innerHTML = '';
		var url = 'http://www.oneworld365.org/user.php';
		var pars = Form.serialize('add_user_form');
		var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars,onLoading: showLoadUser, onComplete: showResponseAddUser} );
	}
	return false;
}


function deletePlacement(id) {
	var chk = confirm ("Are you sure you want to delete this placement?")
	if (chk) {
		var url = 'http://www.oneworld365.org/placement.php';
		var pars = 'm=delete&id='+id;
		var target = 'placement_profile';	
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
		showPlacementEditList()
	}
}


function deleteUser(id) {
	var chk = confirm ("Are you sure you want to delete this user?")
	if (chk) {
		var url = 'http://www.oneworld365.org/user.php';
		var pars = 'm=delete&id='+id;
		var target = 'user_edit_list';	
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
		showUserEditList()
	}
}

function showLoad () {
	$('load').style.display = 'block';
}
function hideLoad() {
	$('load').style.display = 'none';
}
function showPlacementSearch () {

	lightswitch($('load_placement_search'));
}
function showLoadCompany () {
	$('load_company').style.display = 'block';
}
function showLoadPlacement () {
	$('load_placement').style.display = 'block';
}
function showLoadUser () {
	$('load_add_user').style.display = 'block';
}

function showLoadPlacementEdit () {
	$('load_placement_edit').style.display = 'block';
}

function hideLoadPlacementEdit () {
	$('load_placement_edit').style.display = 'none';
}



function showResponsePlacement (originalRequest) {
	var newData = originalRequest.responseText;
	$('view_placement').innerHTML = newData;
	$('load').style.display = 'none';

}

function showResponseCompanyProfile() {
	toggleUI('company_profile');
	hideLoad();
}

function showEditUserList(originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	$('user_edit_list').innerHTML = data.msg;
}

function showResponseLogin(originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	if (data.retVal == 1) {
		window.location = data.url + "/index.php";
	} else {
		$('status_login').innerHTML = data.msg;
	}
}

function showResponseUpdateCompany (originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	$('load_company').style.display = 'none';
	$('status_company').innerHTML = data.msg;
	document.forms.edit_company.id.value = data.id;
}


function showResponseUpdatePlacement(originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	$('load_placement').style.display = 'none';
	$('status_placement').innerHTML = data.msg;
	document.forms.edit_placement.id.value = data.id;
}

function showResponseAddUser(originalRequest) {
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	$('status_add_user').innerHTML = data.msg;
	$('load_add_user').style.display = 'none';
}

/*
* display search results 
*/

function showResSearchPlacement(originalRequest) {
	$('load_placement_search').style.display = 'none';

	var newData = originalRequest.responseText;
	var data = newData.evalJSON();

	if (data.retVal == 1) {
		$('placement_search_result').innerHTML = data.msg;
	} else {
		$('placement_search_result').innerHTML = "<p>There are 0 matching placements.</p>";
	}

}


function showResponseSearchResult(originalRequest) {
	$('load').style.display = 'none';
	var newData = originalRequest.responseText;
	var data = newData.evalJSON();
	if (data.retVal == 1) {
		$('search_result').innerHTML = data.msg;
		toggleUI('search_result');
		if (data.showPager == 1) {
			setLightSwitch('pager',1);
			$('pager').innerHTML = data.pager;
		}
	} else {
		toggleUI('homepage')
		$('status_search').innerHTML = data.msg;
		$('status_search').style.display = 'bloc';
	}
}

/*
* displays a popup div centered in the viewable portion of the browser window 
*/
function positionElement(e) {


	var availHeight;
	var availWidth;
	var yOffset
	
	if(typeof(window.innerWidth) == 'number'){
		availHeight = window.innerHeight;
		availWidth = window.innerWidth;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		availHeight = document.documentElement.clientHeight;
		availWidth = document.documentElement.clientWidth;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		availHeight = document.body.clientHeight;
		availWidth = document.body.clientWidth;
	}

	if (self.pageYOffset){// all except Explorer
		yOffset = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		yOffset = document.documentElement.scrollTop;
	} else if (document.body){ // all other Explorers
		y = document.body.scrollTop;
	}

	var indicatorWidth = 50;
	var indicatorHeight = 50;
	var left = (availWidth/7) ;
	var top = ((availHeight/7)+yOffset) - (indicatorHeight/2);

	/*
	var d = document.getElementById(e);
	d.style.position="absolute";
	d.style.zIndex="999";
	d.style.top=top+"px";
	d.style.left=left+"px";
	*/


}









function lightSwitch(e) {


	if (document.getElementById) {
		// this is the way the standards work
		if(document.getElementById(e) != null) {
			if(document.getElementById(e).style.visibility != "hidden") {
				document.getElementById(e).style.visibility = "hidden";
				document.getElementById(e).style.display = "none";
			} else {
				document.getElementById(e).style.visibility = "visible";
				document.getElementById(e).style.display = "";
			}
		}
	} else if (document.all) {
		// this is the way old msie versions work
		if(document.all[e] != null ) {
			if(document.all[e].style.visibility != "hidden") {
				document.all[e].style.visibility = "hidden";
				document.all[e].style.display = "none";
			} else {
				document.all[e].style.visibility = "visible";
				document.all[e].style.display = "";
			}
		}
	} else if (document.layers) {
		// this is the way nn4 works
		if(document.layers[e] != null) {
			if(document.layers[e].visibility != "hidden") {
				document.layers[e].visibility = "hidden";
			} else {
				document.layers[e].visibility = "visible";
			}
		}
	}
}



function enter_pressed(e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return false;
	return (keycode == 13);
}


function showHide(e) {
	document.getElementById(e).style.display = (document.getElementById(e).style.display == "none") ? "block" : "none";  
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

