// JavaScript Document

function docReady()
{
	var person = $('#check').val();
	var username = $('#username').val();
	var page = $('#countryPage').val();
	var url = rootPath + "ajax/getCountryMember.php?username="+username+"&page="+page+"&person="+person;
	if($('#check').val()== "you")
	{
		$.post(url, function(data) {
			if($(data).text()=="")
			{
				$('.button2_left').hide();
				$('.button2_right').hide();
				$('#countryButton').hide();
			}else
			{
				$('#country_members_you').html(data);
			}
		  
		});
	}else
	{
		$.post(url, function(data) {
			if($(data).text()=="")
			{
				$('.button2_left').hide();
				$('.button2_right').hide();
				$('#countryButton').hide();
			}else
			{
				$('#country_members_other').html(data);
			}		  
		});
	}
	
	$('#countryButton').click(function() {
		page++;
		$('#countryPage').val(page);
		
		var person = $('#check').val();
		var username = $('#username').val();
		var url = rootPath + "ajax/getCountryMember.php?username="+username+"&page="+page+"&person="+person;
		
		if($('#check').val()== "you")
		{
			$.post(url, function(data) {
			if($(data).text()=="")
			{
				$('.button2_left').hide();
				$('.button2_right').hide();
				$('#countryButton').hide();
			}else
			{
				$('#country_members_you').html(data);
			}
			});
		}else
		{
			$.post(url, function(data) {
			if($(data).text()=="")
			{
				$('.button2_left').hide();
				$('.button2_right').hide();
				$('#countryButton').hide();
			}else
			{
				$('#country_members_other').html(data);
			}
			});
		}
	});
	
	// Check whether 'worked' is expanded
	if (document.getElementById('workedExpanded').value == '1')
	{
		// Load editors for 'worked'
		var count = document.getElementById('workedCount').value;
		
		for (i = 1; i <= count; i++)
		{
			loadEditor(i);
		}
	}
}

function profileEdit(question, count)
{
	
	var div = document.getElementById(question);
	var valueField = document.getElementById(question + "Value");
	var visible = (div.style.display == "none" ? false : true);

	toggle(question, 'slide');
	
	if (valueField.value == 1)
	{
		questionClicked(question, 1);
	}
	
	var expandedField = document.getElementById(question + "Expanded");
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
	if (question == "worked")
	{
		
		for (i=1;i<=count;i++)
		{
			loadEditor(i);
		}

	}
}

function statusEdit()
{
	var div = document.getElementById('status');
	var visible = (div.style.display == "none" ? false : true);
	
	toggle('status', 'slide');
	
	var expandedField = document.getElementById("statusExpanded");
	
	if (visible)
	{
		expandedField.value = "0";
	}
	else
	{
		expandedField.value = "1";
	}
}

function questionClicked(question)
{
	var max = 4;
	var count = document.getElementById(question + 'Count').value;
	var id = document.getElementById(question + 'Value').value;
	//alert(question);
	if (count <= max)
	{
		if (count == max)
		{
			document.getElementById(question + "HREF").style.display = 'none';
		}
		
		var url = "../../ajax/register.php?id=" + id + "&question=" + question + "&showdelete=1";
		
		updateAllDOMFields(document.profileForm);
		loadXMLDoc(url, gotData, true);
	}
	
}

function gotData(xmlDoc)
{
	var questionInfo = new Array();
	var html = "";
	
	if (xmlDoc != null)
	{
		var id = getXmlValue(xmlDoc, 'id', 0);
		
		// Build up array of event info
		var question = xmlDoc.getElementsByTagName('response');
		
		if (question.length > 0)
		{
			// Loop through each event
			for (var e = 0; e < question.length; e++)
			{
				var info = question[e];
				
				// Loop through event info nodes
				// and build up an array of event details
				var details = new Array();
				
				if (info.childNodes.length > 0)
				{
					for (var n = 0; n < info.childNodes.length; n++)
					{
						// firstChild of the node is simply the text of the element
						var value = info.childNodes[n].firstChild.nodeValue;
						
						// Store in array
						// (e.g. details["id"] = 1)
						details[info.childNodes[n].nodeName] = value;
					}
				}
				
				// Add event details to main array
				questionInfo[questionInfo.length] = details;
			}
		}
		
		// Build HTML
		var found = false;
		
		if (questionInfo.length > 0)
		{
			for (var e = 0; e < questionInfo.length; e++)
			{
				var details = questionInfo[e];
				var html = details["html"];
				var question = details["info"];
				found = true;
			}
		}
		
		if (found == false)
		{
			return false;
		}
		
		var oldValue = parseInt(document.getElementById(question + "Value").value);
		var newValue = oldValue + 1;
		document.getElementById(question + "Value").value  = newValue;
		document.getElementById(question + "Count").value  = parseInt(document.getElementById(question + "Count").value) + 1;
		
		if (question == 'worked')
		{
			var deletedEditors = new Array();
			
			for (i = 1; i < oldValue; i++)
			{
				// Has section been deleted?
				var del = document.getElementById(question + "Deleted_" + i).value;
				
				if (del == "")
				{
					// Section has not been deleted
					// ... destroy the CK-Editor
					eval('CKEDITOR.instances.workbody_' + i + '.destroy();');
					CKEDITOR.remove('workbody_' + i)
					
					// Remember that this editor has been deleted
					deletedEditors.push(i);
				}
			}
			
			// Show the new HTML
			document.getElementById(question + "Div").innerHTML += html;
			
			// Reload the deleted CK-Editors
			if (deletedEditors.length > 0)
			{
				for (i = 0; i < deletedEditors.length; i++)
				{
					var j = deletedEditors[i];
					loadEditor(j);
				}
			}
			
			loadEditor(id);
		}
		else
		{
			// Show the new HTML
			document.getElementById(question + "Div").innerHTML += html;
		}
	}
}

function deleteClicked(element, id)
{
	var containerId = element + 'Container_' + id;
	var container = document.getElementById(containerId);
	var valueId = document.getElementById(element + 'Value');
	var countId = document.getElementById(element + 'Count');
	var deletedId = document.getElementById(element + 'Deleted_' + id);
	
	//alert (element);
	//alert(id);
	
	hide(containerId, 'slide');
	
	var condition  = clearFields(container, true, true);
	deletedId.value = "1";
	if (element == 'worked')
	{
		document.getElementById('studiedGradYear_'+id).value = "";
		document.getElementById('studiedFromYear_'+id).value = "";
		document.getElementById('studiedDegree_'+id).value = "";
		document.getElementById('studiedEduLevel_'+id).value = "";
		document.getElementById('studiedInstitutionFreehand_'+id).value = "";
		document.getElementById('studiedInstitution_'+id).value = "";
	}
	
	
	countId.value = parseInt(countId.value) - 1;
	
	if (countId.value <= 5)
		document.getElementById(element + 'HREF').style.display = '';
	
	// Delete CK-Editor
	if (element == 'worked')
	{
		eval('CKEDITOR.instances.workbody_' + id + '.destroy();');
		CKEDITOR.remove('workbody_' + id)
	}
}

function workedCurrentlyClicked(id)
{
	var dateTo = document.getElementById('workedDateEnd_' + id);
	var dateDiv = document.getElementById('workedDate_' + id);
	
	dateTo.value = 'DD/MM/YYYY';
	if (document.getElementById('workedCurrently_' + id).checked == true)
	{
		dateTo.style.display='none';
		dateDiv.style.display='none';
	}
	else
	{
		dateTo.style.display='';
		dateDiv.style.display='';
	}
}
// Send Connection to the person whose profile page is up
function addFriendProfile(user)
{
	document.addFriendForm.action.value = "addFriend";
	document.addFriendForm.submit();
}
// Send connnection to the friends of the current profile page
function addFriend(username, prefix)
{
	var url = rootPath + "ajax/addfriend.php?username=" + username + "&extra=" + prefix;
	document.getElementById(prefix + 'link_' + username).style.display = "none";
	document.getElementById(prefix + 'loader_' + username).style.display = "";
	loadXMLDoc(url, addFriendComplete, true);
}

function currentlyDropdownChanged(obj)
{
	var index = obj.selectedIndex;
	var option = obj.options[index];
	var parent = option.parentNode;
	
	// Hide Company/school field for "Job Hunting" or "On a Break"
	if (option.value == 'JH' || option.value == 'OB')
	{
		hide("institution_container", "slide");
		document.profileForm.institutionExpanded.value = "no";
	}
	else
	{
		show("institution_container", "slide");
		document.profileForm.institutionExpanded.value = "yes";
	}
	
	if (parent.label == "Working")
	{
		show("planningBusinessSchoolContainer", "slide");
	}
	else
	{
		hide("planningBusinessSchoolContainer", "slide");
		document.getElementById("planningBusinessSchool").checked = false;
	}
}

function oiSiteChanged()
{
	var index = document.openInviterForm.oiService.selectedIndex;
	var value = document.openInviterForm.oiService.options[index].value;
	
	if (value != "")
	{
		var provLogoPathDiv = document.getElementById("oi_prov_logo_path_" + value);
		var provLogoImg = document.getElementById("oi_prov_logo");
		var logoPath = provLogoPathDiv.innerHTML;
		
		// Load the image
		if (logoPath != "")
		{
			provLogoImg.src = logoPath;
		}
		
		document.openInviterForm.oiLogoPath.value = logoPath;
		show("oi_prov", 'slide', null);
	}
	else
	{
		document.openInviterForm.oiLogoPath.value = "";
		hide("oi_prov", 'slide', null);
	}
}

function acceptFriend(username)
{
	document.notificationsForm.action.value = "acceptFriend";
	document.notificationsForm.invite.value = username;
	document.notificationsForm.submit();
}

function rejectFriend(username)
{
	document.notificationsForm.action.value = "rejectFriend";
	document.notificationsForm.invite.value = username;
	document.notificationsForm.submit();
}

function schoolDropdownChanged(obj, id)
{
	var index = obj.selectedIndex;
	var option = obj.options[index];
	var freehandDiv = "studied_ins_freehand_" + id;
	
	if (option.value == "0")
	{
		show(freehandDiv, "slide");
	}
	else
	{
		hide(freehandDiv, "slide");
	}
}

function loadEditor(id)
{
	//questionClicked('worked');	
	var i = 1; 
	
	//alert(id);
	var st = 'workbody_' + id;
	var editor = CKEDITOR.replace(st,
	{
		width: 440,
		height: 260,
		resize_maxWidth: 400,
		resize_minWidth: 400,
		resize_minHeight: 250,
		toolbar : 'Profile',
		filebrowserBrowseUrl : '/templates/js/ckfinder/ckfinder.html',
		filebrowserImageBrowseUrl : '/templates/js/ckfinder/ckfinder.html',
		filebrowserFlashBrowseUrl : '/templates/js/ckfinder/ckfinder.html?type=Flash',
		filebrowserUploadUrl : '/templates/js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
		filebrowserImageUploadUrl : '/templates/js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
		filebrowserFlashUploadUrl : '/templates/js/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
	});
}