window.addEvent("domready", function (){ 

	if($('opportunity_experience_html')) {
		updateOpportunity();
	}
		
	$$('.removeLink').addEvent('click', function(e) { 
		e.preventDefault(); 
		$(this).getParent().dispose();
		updateOpportunity();
	});	
	

});


function opportunityDayrate (value) {
	if (value == "Other") {
		$('dayrate_other').removeClass('disappear');
	} else {
		$('dayrate_other').value = '';
		$('dayrate_other').addClass('disappear');
	}
}

function addOpportunityIndustry() {
	var value = $('industry_list').get('value');
	
	if ($('industry_list').get('value') != 0) {
		
		var newExperience = new Element('li', {'html': value});
		var removeLink = new Element('a', {
				'href':'#', 
				'class':'removeLink',
				'html':'&nbsp;',
				'events': {
					'click': function(e) { 
						e.preventDefault(); 
						$(this).getParent().dispose();

						updateOpportunity();
					}
				}	
		});
		
		removeLink.inject(newExperience, 'top');		
		$('experienceList').adopt(newExperience);

		updateOpportunity();
		
		// Reset Industry List
		$('industry_list').set('value','0'); 
	}
	return true;
}

function updateOpportunity() {
	$('opportunity_experience_html').set('value', $('experienceList').get('html'));

	var value = "";
	$$('#experienceList li').each(function(e) {
		value = value + "&bull;&nbsp;" + e.get('text') + "\n";
	});
	$('opportunity_experience').set('value', value);	
}
	

function showHide(divid, icon, base) {
	var msgSlide = '';
	var imagescript = base + "shared/php/icon.php";
		$(divid).set('slide', {mode: 'vertical'});


	if ($(icon).getProperty('src') == imagescript+"?zoom_in" ) {
		$(divid).slide('in');
		$(icon).setProperty('src', imagescript+"?zoom_out");								
	} else {			
		$(divid).slide('out');
		$(icon).setProperty('src', imagescript+"?zoom_in");	
	}
			
	return false;
}



function clearText(id) {
 	document.getElementById(id).value = '';
}


function setFind(value) {
	switch (value) {
		case '0' :
			$('nameFilter').setStyle('display', 'none');
			$('client_name_value').setProperty('value', '');
			break;	
		case '1' :
			$('nameFilter').setStyle('display', '');
			break;		
		default:
			$('nameFilter').setStyle('display', 'none');	
			$('client_name_value').setProperty('value', '');	
		 	break;
	}	
}

function homeEditSector() {
	$('sectorRead').setProperty('class', 'hide');
	$('sectorRead').setStyle('display','none');
	$('sectorEdit').setProperty('class', 'unhide');
	$('sectorEdit').setStyle('display', '');
	$('saveSector').setProperty('class', 'unhide floatR');
	$('saveSector').setStyle('display', '');
}

function homeEditLocate() {
	$('locate').setProperty('class', 'activeTxt');
	$('changeLocate').setStyle('display','none');
	$('saveLocate').setProperty('class', 'unhide');
	$('locateLabel').setStyle('display', 'none');
}

function profileWorkInfoEdit(id) {		
	if ($('editwork'+id).value == 0) {		
		$('editwork'+id).value = 1;	
		$('saveWork'+id).setProperty('class','norm inline unhide');
		$('employer'+id).setProperty('class','text activeTxt');
		$('industry'+id).setProperty('class','text activeTxt');
		$('position'+id).setProperty('class','text activeTxt');
		$('time_year'+id).setProperty('class','tiny activeTxt');
		$('time_month'+id).setProperty('class','tiny activeTxt');
		
		$('descriptionInfo'+id).setStyle('display','none');			
		var descriptionInfo = $('descriptionInfo'+id).getProperty('html');	
				
		var textArea = new Element('textarea', {'class': 'text active', 'name': 'workinfo_description#'+id, 'rows': '6', 'cols': '35', 'html': descriptionInfo});
		textArea.inject('description'+id);
		
		
		$('typeInfo'+id).setStyle('display','none');
		var typeInfo = $('typeInfo'+id).getProperty('html');
		
		var selectArea = new Element('select', {'class': 'active', 'name': 'workinfo_type#'+id}).adopt( new Element('option', {'value': 'EMPLOYED', 'html': 'Employed'})).adopt( new Element('option', {'value': 'CONTRACT', 'html': 'Contract'}));
		
		selectArea.inject('type'+id);
	
	}	
}// end function


function profileEducationEdit(id) {			
	if ($('editEducation'+id).value == 0) {					
		$('editEducation'+id).value = 1;	
		$('saveEducation'+id).setProperty('class','norm inline unhide');	
		$('qualification'+id).setProperty('class','text activeTxt');
	
		$('detailsInfo'+id).setStyle('display','none');			
		var detailsInfo = $('detailsInfo'+id).getProperty('html');	
				
		var textArea = new Element('textarea', {'class': 'large active', 'name': 'education_description#'+id, 'rows': '6', 'cols': '30', 'html': detailsInfo});
		textArea.inject('details'+id);
	}	
}// end function




function feedback(value, err) {
	switch (value)
	{
	case "red":
	  document.getElementById('red').style.display = '';
	  document.getElementById('amber').style.display = 'none';
	  document.getElementById('green').style.display = 'none';
	  document.getElementById('feedback_confirm1').scrollIntoView(true);
	  	  if (err) feedbackFrm(1);
	  break;
	case "amber":
	  document.getElementById('red').style.display = 'none';
	  document.getElementById('amber').style.display = '';
	  document.getElementById('green').style.display = 'none';
	  document.getElementById('feedback_confirm2').scrollIntoView(true);
	  	  if (err) feedbackFrm(2);
	  break;
	case "green":
	  document.getElementById('red').style.display = 'none';
	  document.getElementById('amber').style.display = 'none';
	  document.getElementById('green').style.display = '';
	  document.getElementById('feedback_confirm3').scrollIntoView(true);
	  	  if (err) feedbackFrm(3);
	  break;
	default:
	  document.getElementById('red').style.display = 'none';
	  document.getElementById('amber').style.display = 'none';
	  document.getElementById('green').style.display = 'none';
	  break;
	}// end SWITCH	
}// end function

function feedbackFrm(value) {
	if (value) {
		var dialog = "feedback_confirm" + value;
		document.getElementById(dialog).style.display = 'none';
		document.getElementById('mainFrm').style.display = '';
	}
}// end function (feedbackFrm)




function getSpecialism (value, base, objectid, selectItem) {
	//This code will send a data object via a GET request and alert the retrieved data.
	
	if (value > 0) {
		var callUrl = base+"callbacks/getSpecialism.php?&id=" + value;	
		//alert ("u = " + callUrl);
			
		var jsonRequest = new Request.JSON({ 
			url: callUrl, 
			onComplete: function(data) {
				buildSpecialism(data, objectid, selectItem)
				} 
			});
		
		jsonRequest.send();
	} else {
		$(objectid).empty();
	}
}// end function


//
//  Purely Built because IE and Firefox are being irritating - for MEMBER FIND functionality ONLY
//
function getSpecialismFIND (value, base, objectid, selectItem) {
	//This code will send a data object via a GET request and alert the retrieved data.
	
	if (value > 0) {
	
		var jsonRequest = new Request.JSON({url: base+"callbacks/getSpecialismFIND.php?&id=" + value, onComplete: function(data) {buildSpecialismFIND(data, objectid, selectItem)} });
		
		jsonRequest.send();
	} else {
		$(objectid).empty();
	}
}// end function




function removeSpecialism (value, base) {
	//This code will send a data object via a GET request and alert the retrieved data.
	var jsonRequest = new Request.JSON({url: base+"callbacks/removeSpecialism.php?comp=" + value, onComplete: function(data) {refreshProfile("service_removed")} });
	jsonRequest.send();
}


function removeIndustry(value, base) {
	//This code will send a data object via a GET request and alert the retrieved data.
	var jsonRequest = new Request.JSON({url: base+"callbacks/removeIndustry.php?exp=" + value, onComplete: function(data) {refreshProfile("industry_removed")} });
	jsonRequest.send();
}

function removeEducation (value, base) {
	//This code will send a data object via a GET request and alert the retrieved data.
	
	if (value > 0) {
		var jsonRequest = new Request.JSON({url: base+"callbacks/removeEducation.php?edu=" + value, onComplete: function(data) {refreshProfile("education_removed")} });
		jsonRequest.send();
	
	} else {
		$(objectid).empty();
	}
}

function removeWorkinfo (value, base) {
	//This code will send a data object via a GET request and alert the retrieved data.
	var jsonRequest = new Request.JSON({url: base+"callbacks/removeWorkinfo.php?wi=" + value, onComplete: function(data) {refreshProfile("workinfo_removed")} });
	jsonRequest.send();
}


function removeLocation (value, base) {
	//This code will send a data object via a GET request and alert the retrieved data.
	var jsonRequest = new Request.JSON({url: base+"callbacks/removeLocation.php?loc=" + value, onComplete: function(data) {refreshProfile("location_removed")} });
	jsonRequest.send();
}



function refreshProfile (feedback) {
	switch (feedback){
		case "service_removed":
			window.location = "member_profile_services.php?feedback="+feedback;
			break;
		case "education_removed":
			window.location = "member_profile_education.php?feedback="+feedback;
			break;
		case "industry_removed":
			window.location = "member_profile_industry.php?feedback="+feedback;
			break;
		case "workinfo_removed":
			window.location = "member_profile_work.php?feedback="+feedback;
			break;
		case "resent_invitation":
			alert("Your invitation has been re-sent");
			break;
		default : 
			window.location = "member_profile.php?feedback="+feedback;
			break;
	}
	
}


function buildSpecialism (data, objectid, selectItem) {
	
	$(objectid).empty();
		
	data.each(function(item){
		
		if (selectItem == item.service_id) {
			var option = new Element('option', {value:item.service_id, text:item.service_name, selected: true});
		} else {
			if (item.service_taken == 1) {
				var option = new Element('option', {value:'-1', text:item.service_name, 'class':'taken'}); 		
			} else {
				var option = new Element('option', {value:item.service_id, text:item.service_name});
			}
		}
		
		
		option.inject($(objectid));
	});	
}// end function


//
//  Purely Built because IE and Firefox are being irritating - for MEMBER FIND funcitonality ONLY
//
function buildSpecialismFIND (data, objectid, selectItem) {
	$(objectid).empty();
		
	data.each(function(item){
		
		if (selectItem == item.service_id) {
			var option = new Element('option', {value:item.service_id, text:item.service_name, selected: true});
		} else {
			var option = new Element('option', {value:item.service_id, text:item.service_name});
		}
		
		
		option.inject($(objectid));
	});	
}// end function






function getDirections(){
	var postcode = $('userpostcode').get('value');
	
	Google.getDirections('map', $('directionsPanel'), postcode, $('postcode').get('text'), function(){
		var gmarker = Google.getMarker(marker);
		gmarker.closeInfoWindow();
		$('directionsPanel').morph({'opacity': 1});
	}, 
	function(error){
		if(error == 602) alert('Could not find the postcode specified');
	});
}


function checkTerms(theForm) {	
	with(theForm) {
		if (!member_terms.checked) {
			alert('You need to read and agree to the terms and conditions before you can join the network');	
			return false;	
		} else { 
			return true; 
		}
	}	
}


function resendInvitation (value, base) {
	//This code will send a data object via a GET request and alert the retrieved data.
	var jsonRequest = new Request.JSON({url: base+"callbacks/resendInvitation.php?cli=" + value, onComplete: function(data) {refreshProfile("resent_invitation")} });
	jsonRequest.send();
}

