var firs=0;


jQuery(document).ready(function(){
     setTimeout('$("#ActionNote").children(\'p\').fadeOut(3000)',5000);
     $('#aInvitationForm').click(function () {
       $('#expand').slideToggle('normal'); 
     });
	}); 
	
function sortBy(category)
{
  var url = '/index.php/contacts/list?';
  if ($('user').value != 0)
    url += '&user='+$('user').value;
  if ($('talentpoolchoice').value != 0)
    url += '&talentpoolchoice='+$('talentpoolchoice').value;
  if ($('category').value != 0)
    url += '&category='+$('category').value;
  if ($('industry').value != 0)
    url += '&industry='+$('industry').value;
  if ($('tags').value != '')
    url += '&tags='+$('tags').value;
  switch (category)
  {
  	case '1': //most recommended
        url += '&sortby=1';
  		break;
  	default: //join date
        break;
  }
  url += '&online='+$('online').value;
  document.location = url;
}

function activitycomment(x)


{
   url='/activities/addComment';
   tformname='#comm_' + x;
   hname='#hcomm_' + x;
   ffields=$(tformname).serializeArray();
     jQuery.post(url, ffields, function(data){
                
          $(hname).after(data);

        }); 
     
   
   
     
     
     
}













/*function changeByTalentPool(talentpoolchoice)
{
  if (talentpoolchoice == 2 || talentpoolchoice == 3 || talentpoolchoice == 4 ||
        talentpoolchoice == 5 || talentpoolchoice == 6)
    document.location = '/contacts/list?talentpoolchoice='+talentpoolchoice+'&online='+$('online').value;
  else document.location = '/contacts/list?online='+$('online').value;
}*/

function changeIndustriesInColumn(catId)
{
    $('industry').options.length=0;
    $('industry').options[0]=new Option('-- please select a industry --', 0);

    if ((catId != '') && (catId != 0))
    {
        longitud = eval('category_'+catId+'_ids.length');
        for(var q=0;q<longitud;q++)
        {
            $('industry').options[q+1] = new Option(eval('category_'+catId+'_values['+q+']'), eval('category_'+catId+'_ids['+q+']'));
        }

    }
    else
    {
        $('industry').options[0]=new Option('-- please select a category --', 0);
    }
}

function _Contacts() {
	
	this.Invite = new _ContactsInvite();
	this.Import = new _ContactsImport();
	this.Add = new _ContactsAdd();
	this.onLoad = function() {
		if (document.InvitationForm) {
			this.Invite.onLoad();
		}
	}
	
}

function _ContactsInvite() {
	

	
	this.addRow = function() {
          	if (firs==0) {
		this.minRowID = (typeof minRowID != 'undefined') ? minRowID : 0;
		this.maxRowID = (typeof maxRowID != 'undefined') ? maxRowID : 0;
		this.rowCounter = (typeof rowCounter != 'undefined') ? rowCounter : 1;
		this.templateRow =  $('#row_'+this.minRowID)[0].cloneNode(true);
                firs=1;
	}
          
		if (this.rowCounter >= 10) return false;
		this.rowCounter++;
		this.maxRowID++;
		var newRow = this.templateRow.cloneNode(true);
		Common.changeNameIDs4Node(newRow,this.minRowID,this.maxRowID);
		$('#ContactRows')[0].appendChild(newRow);
	}
	
	this.removeRow = function(removeID) {
		if (this.rowCounter <= 1) return false;
		var rowID = removeID.replace('remove_','');
		this.rowCounter--;
		//$('ContactRows').removeChild($('row_'+rowID));
		$('#ContactRows')[0].removeChild($('row_'+rowID));
		
	}
	
}

function _ContactsImport() {
	
	this.sourceOnChange = function(newValue) {
		var fileUpload = (newValue == 'outlook');
		$('WebService').style.display = fileUpload ? 'none' : '';
		$('FileUpload').style.display = fileUpload ? '' : 'none';
		document.ImportForm._command.value = fileUpload ? 'FileUpload' : 'WebService';
	}
	
	this.markSelected = function(checkbox) {
		if (checkbox.checked) {
			if (checkbox.parentNode.parentNode.className.indexOf(' checked') == -1) {
				checkbox.parentNode.parentNode.className += ' checked';
			}
		} else {
			if (checkbox.parentNode.parentNode.className.indexOf(' checked') > -1) {
				checkbox.parentNode.parentNode.className = checkbox.parentNode.parentNode.className.replace(' checked','');
			}
			document.FetchedContactsForm.select_all.checked = false;
		}
	}
	
	this.selectAll = function(checked) {
		for (var i=0; i<document.FetchedContactsForm.elements.length; i++) {
			var element = document.FetchedContactsForm.elements[i];
			if (element.type == 'checkbox' && element.name.indexOf('selected') == 0) {
				if (checked) {
					if (element.parentNode.parentNode.style.display == '') {
						element.checked = checked;
						this.markSelected(element);
					}
				} else {
					element.checked = checked;
					this.markSelected(element);
				}

			}
		}
	}
	
	this.showH2UL = function(ul) {
		var visible = false;
		var ndList = ul.childNodes;
		for (var i=0; i<ndList.length; i++) {
			if (ndList[i].tagName == 'LI') {
				if (ndList[i].style && ndList[i].style.display == '') {
					visible = true;
					break;
				}
			}
		}
		ul.style.display = visible ? '' : 'none';
		$(ul.id.replace('ul','h2')).style.display = visible ? '' : 'none';
	}
	
	this.showJustSkipsoMembers = function(element,justMembers) {
		var ndList = element.childNodes;
		for (var i=0; i<ndList.length; i++) this.showJustSkipsoMembers(ndList[i],justMembers);
		if (element.tagName == 'LI') {
			if (justMembers) {
				if (element.className.indexOf('member') > -1) {
					element.style.display = '';
				} else {
					element.style.display = 'none';
				}
			} else {
				element.style.display = '';
			}
		} else if (element.tagName == 'UL') {
			this.showH2UL(element);
		}
	}
	
	this.submitFetchedContactsForm = function() {
		var nothingSelected = true;
		for (var i=0; i<document.FetchedContactsForm.elements.length; i++) {
			var element = document.FetchedContactsForm.elements[i];
			if (element.type == 'checkbox' && element.name.indexOf('selected') == 0) {
				if (element.checked) {
					nothingSelected = false;
				}
			}
		}
		if (nothingSelected) {
			alert('No contact is selected!');
			return false;
		}
		document.FetchedContactsForm.submit();
	}
	
	this.backToSelect = function() {
		document.InvitationForm.action = document.InvitationForm.BackToSelectAction.value;
		document.InvitationForm.submit();
	}

}


function _ContactsAdd() {
		
	this.relationshipTypeChanged = function(label) {
		var AdditionalInfos = $('relationship_types').getElementsByTagName('DIV');
		for (var i=0; i<AdditionalInfos.length; i++) {
			if (AdditionalInfos[i].className.indexOf('additional-info') == -1) continue;
			AdditionalInfos[i].style.display = 'none';
		}
		var AdditionalInfos = label.parentNode.getElementsByTagName('DIV');
		for (var i=0; i<AdditionalInfos.length; i++) {
			if (AdditionalInfos[i].className.indexOf('additional-info') == -1) continue;
			AdditionalInfos[i].style.display = '';
		}
	}

	this.UpdateWorkMenu = function(serValues) {
		if (document.WorkForm.Work_Id) {
			document.ContactAddForm.elements['employer[1]'][document.ContactAddForm.elements['employer[1]'].length] = new Option(document.WorkForm.Work_Employer.value, document.WorkForm.Work_Id.value, true);
			document.ContactAddForm.elements['employer[3]'][document.ContactAddForm.elements['employer[3]'].length] = new Option(document.WorkForm.Work_Employer.value, document.WorkForm.Work_Id.value, true);
		}
	}

    this.UpdateEducationMenu = function(serValues) {
		if (document.EducationForm.Edu_Id) {
			document.ContactAddForm.elements['profile_education_id[2]'][document.ContactAddForm.elements['profile_education_id[2]'].length] = new Option(document.EducationForm.Edu_Name.value, document.EducationForm.Edu_Id.value, true);
		}
	}
}


var Contacts = new _Contacts();
