
function updatePubScript() {
	//nu ska vi se vilken som är selected value, tex ac_1, ff_2, osv
	var value = document.update_publication.publication.value;
	
	var index = document.update_publication.publication.selectedIndex;
	var text = document.update_publication.publication.options[index].text;
	var date = document.update_publication.dates.options[index].text;
	var thesis = document.update_publication.thesises.options[index].text;
	
	//sätt in texten från den selected i pub_text
	document.update_publication.publication_text.value = text;
	document.update_publication.date.value = date;
	if(thesis == 1)
		document.update_publication.thesis.checked = true;
	else
		document.update_publication.thesis.checked = false;
	document.update_publication.submit.disabled=false;
}

function updatePublication() {
	var info = "";
	if(document.update_publication.publication_text.value == "") {
		info += "You cannot submit a publication without a description\n";	
	}
	if(document.update_publication.video_alt.value != "" && document.update_publication.video.value != "") {
		info += "\nYou cannot both upload a video AND put a name in the database, choose one.";	
	}
	if(info != "") {
		alert(info);
		return false;	
	} else {		
		return true;
	}
}

function updateManifest() {
	var info = "";
	if(document.update_manifest.manifest.value == "") {
		info += "You cannot submit an empty manifest\n";	
	}
	if(info != "") {
		alert(info);
		return false;	
	} else {		
		return true;
	}
}


function addPublication() {
	var info = "";
	if(document.add_publication.publication_text.value == "") {
		info += "You cannot submit a publication without a description\n";	
	}
	/* 
	if(document.add_publication.pdf.value == "" && document.add_publication.video.value == "" && document.add_publication.video_alt.value == "") {
		info += "\nThe publication needs to consist of something!\nSubmit a PDF and/or video";	
	}
	*/
	if(document.add_publication.video_alt.value != "" && document.add_publication.video.value != "") {
		info += "\nYou cannot both upload a video AND put a name in the database, choose one.";	
	}
	
	if(info != "") {
		alert(info);
		return false;
	} else {		
		return true;
	}
}

function removeSponsor()
{
  	var answer = confirm ("Are you sure you want to completely remove the sponsor?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removePublication()
{
  	var answer = confirm ("Are you sure you want to completely remove the publication?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removeNews()
{
  	var answer = confirm ("Are you sure you want to completely remove the news?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removeProject()
{
  	var answer = confirm ("Are you sure you want to completely remove the project?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removeCredits()
{
  	var answer = confirm ("Are you sure you want to completely remove the credit?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removeMember()
{
  	var answer = confirm ("Are you sure you want to completely remove the member?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removeLink()
{
  	var answer = confirm ("Are you sure you want to completely remove the link?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removePosition()
{
  	var answer = confirm ("Are you sure you want to completely remove the position?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function removeGroup()
{
  	var answer = confirm ("Are you sure you want to completely remove the group?\nThis operation cannot be undone.")
	if (answer)
		return true;
	else
		return false;
}

function controlLeftPanel()
{
    missingInfo = "";
    var i = document.left_top.page.selectedIndex;
  
    if ( !(document.left_top.update.checked == "true" || document.left_top.update.checked == true) &&
    	(i == 9) )
	  	missingInfo += "\n - You can only update the contact information. ";
    else if ( !(document.left_top.update.checked == "true" || document.left_top.update.checked == true) &&
    	(i == 10) )
	  	missingInfo += "\n - You can only update the manifest. ";

	
	if (i < 1)
	  	missingInfo += "\n - You must choose a page to modify. ";

      if (missingInfo != "") {
            alert(missingInfo);
            return false;
      } else {
      		return true;
      }
}

function updateMember() {
	return addMember();
}

function addMember()
{
      missingInfo = "";
      if (trim(document.member.name_.value) == "")
      	missingInfo += "\n - Name ";
      if (trim(document.member.role.value) == "")
      	missingInfo += "\n - Role ";
      if (trim(document.member.description.value) == "")
      	missingInfo += "\n - Description ";
   	
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updateGroup() {
	missingInfo = "";
      if (trim(document.member.title.value) == "")
      	missingInfo += "\n - Name ";
      
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function addGroup() {
	missingInfo = "";
      if (trim(document.member.title.value) == "")
      	missingInfo += "\n - Name ";
   
   x = 0;
   	for(i = 1; i < 11; i++) {
   		var name = document.getElementById(i+"");
   		if(trim(name.value) != "")
   			x++;
   	}	
   	if(x == 0)
   		missingInfo += "\n - You must have at least one member in the group";
   
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updateMerits(numLinks) {
	missingInfo = "";
      if (trim(document.update_opening.internship_duration.value) == "")
      	missingInfo += "\n - Internship duration or ECTS points ";
      if (trim(document.update_opening.support.value) == "")
      	missingInfo += "\n - Support ";
      if (trim(document.update_opening.financial_support.value) == "")
      	missingInfo += "\n - Financial support ";
      
      for(i = 0; i < numLinks; i++) {
		var title = trim(document.getElementById("title_link_"+i).value);
		var address = trim(document.getElementById("adress_link_"+i).value);
				
		if(title == "" && address == "") {
			continue;
		}	
		else if(title == "" && address != "") {
			missingInfo += "\n - Address"+(i+1)+" is missing its title.";	
		}
		else if(title != "" && address == "") {
			missingInfo += "\n - Title"+(i+1)+" is missing its address.";	
		}
	}
   
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updateRequirements() {
	missingInfo = "";
      if (trim(document.update_opening.fields_of_study.value) == "")
      	missingInfo += "\n - Field of study ";
      if (trim(document.update_opening.study_experience.value) == "")
      	missingInfo += "\n - Study experience ";
      if (trim(document.update_opening.language_skills.value) == "")
      	missingInfo += "\n - Language skills ";
      if (trim(document.update_opening.soft_skills.value) == "")
      	missingInfo += "\n - Soft skills ";
      if (trim(document.update_opening.focus_of_interest.value) == "")
      	missingInfo += "\n - Focus of interest ";
   
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updateCredits(num_names, num_objectives) {
	missingInfo = "";
	
	if (trim(document.credits.place.value) == "")
      	missingInfo += "\n - Place ";	
		
	names = "";
	objs = "";
	for(i = 0; i < num_names; i++) {
		var name = trim(document.getElementById("name_"+i).value);
		names += name;
	}
	for(i = 0; i < num_objectives; i++) {
		var obj = trim(document.getElementById("objective_"+i).value);
		objs += obj;
	}
	
	if (trim(names) == "")
      	missingInfo += "\n - You need at least one name ";
	if (trim(objs) == "")
      	missingInfo += "\n - You need at least one objective";
	
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
            return true;
}

function addCredits() {
	missingInfo = "";
	
	if (trim(document.credits.place.value) == "")
      	missingInfo += "\n - Place ";	
		
	names = "";
	objs = "";
	for(i = 0; i < 7; i++) {
		var name = trim(document.getElementById("name_"+i).value);
		var obj = trim(document.getElementById("objective_"+i).value);
		
		names += name;
		objs += obj;
	}
	
	if (trim(names) == "")
      	missingInfo += "\n - You need at least one name ";
	if (trim(objs) == "")
      	missingInfo += "\n - You need at least one objective";
	
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
            return true;
}

function updateSponsor() {
	return addSponsor();
}

function addSponsor() {
	missingInfo = "";
      if (trim(document.sponsor.name.value) == "")
      	missingInfo += "\n - Name ";
      if (trim(document.sponsor.place.value) == "")
      	missingInfo += "\n - Place ";
   
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updateMediaLinks(numLinks) {
	missingInfo = "";
	
	
	for(i = 0; i < numLinks; i++) {
		var title = trim(document.getElementById("title"+i).value);
		var address = trim(document.getElementById("url"+i).value);
		var date = trim(document.getElementById("date"+i).value);
				
		if( (title == "" && address == "" && date == "") || (title != "" && address != "" && date != "")) {
			continue;
		}	
		else if(title != "") {
			if(date == "" && address == "")
				missingInfo += "\n - Title"+(i+1)+" is missing its date and address.";
			else if(address == "")	
				missingInfo += "\n - Title"+(i+1)+" is missing its address.";
			else if(date == "")	
				missingInfo += "\n - Title"+(i+1)+" is missing its date.";
		}
		else if(date != "") {
			if(title == "" && address == "")
				missingInfo += "\n - Date"+(i+1)+" is missing its title and address.";
			else if(address == "")	
				missingInfo += "\n - Date"+(i+1)+" is missing its address.";
			else if(title == "")	
				missingInfo += "\n - Date"+(i+1)+" is missing its title.";
		}
		else if(address != "") {
			if(date == "" && title == "")
				missingInfo += "\n - Address"+(i+1)+" is missing its date and title.";
			else if(address == "")	
				missingInfo += "\n - Address"+(i+1)+" is missing its title.";
			else if(date == "")	
				missingInfo += "\n - Address"+(i+1)+" is missing its date.";
		}
	}
	      
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updateLinks(numLinks) {
	missingInfo = "";
	
	
	for(i = 0; i < numLinks; i++) {
		var title = trim(document.getElementById("title"+i).value);
		var address = trim(document.getElementById("url"+i).value);
				
		if(title == "" && address == "") {
			continue;
		}	
		else if(title == "" && address != "") {
			missingInfo += "\n - Address"+(i+1)+" is missing its title.";	
		}
		else if(title != "" && address == "") {
			missingInfo += "\n - Title"+(i+1)+" is missing its address.";	
		}
	}
	      
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function addLink() {
	missingInfo = "";
      if (trim(document.link.title.value) == "")
      	missingInfo += "\n - Title ";
      if (trim(document.link.url.value) == "")
      	missingInfo += "\n - Address ";
   
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function addMediaLink() {
	missingInfo = "";
      if (trim(document.link.date.value) == "")
      	missingInfo += "\n - Date ";
      
      if (trim(document.link.title.value) == "")
      	missingInfo += "\n - Title ";
      
      if (trim(document.link.url.value) == "")
      	missingInfo += "\n - Address ";
   
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function updatePosition() {
	return addPosition();
}

function addPosition() {
	missingInfo = "";
  	  	
  	if (trim(document.update_opening.abstract_.value) == "") {
		missingInfo += "\n - Abstract ";
  	} 
  	
  	if (trim(document.update_opening.recommended_skills.value) == "") {
		missingInfo += "\n - Recommended skills ";
  	}
  	if (trim(document.update_opening.project_type.value) == "") {
		missingInfo += "\n - Project type ";
  	}
  	if (trim(document.update_opening.start_time.value) == "") {
		missingInfo += "\n - Start time ";
  	}
	if (trim(document.update_opening.duration.value) == "") {
		missingInfo += "\n - Duration ";
  	}
  		   
	if (missingInfo != "")
  	{
		missingInfo ="You need to fill in the missing fields:\n" +
		missingInfo +
          "\n\nInsert the correct information there before you submit!";

        alert(missingInfo);
        return false;
  	}
  	else
  	{
        return true;
	}
}

function updateProject(numLinks) {
	return checkProject(numLinks);
}

function addProject() {
	return checkProject(10)
}

function checkProject(numLinks) {
	missingInfo = "";
      if (trim(document.project.title.value) == "")
      	missingInfo += "\n - Title ";
      if (trim(document.project.summary_description.value) == "")
      	missingInfo += "\n - Summary";
      if (trim(document.project.description.value) == "")
      	missingInfo += "\n - Description";
      
      for(i = 0; i < numLinks; i++) {
		var title = trim(document.getElementById("industry_title_"+i).value);
		var address = trim(document.getElementById("industry_url_"+i).value);
				
		if(title == "" && address == "") {
			continue;
		}	
		else if(title == "" && address != "") {
			missingInfo += "\n - Address"+(i+1)+" is missing its title.";	
		}
		else if(title != "" && address == "") {
			missingInfo += "\n - Title"+(i+1)+" is missing its address.";	
		}
	}
      
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}



function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function updateNews() {
	return addNews();	
}

function updateContact() {
	missingInfo = "";
      if (trim(document.update_contact.postal.value) == "")
      	missingInfo += "\n - Postal address ";
      if (trim(document.update_contact.lab_head.value) == "")
      	missingInfo += "\n - Lab head";
      if (trim(document.update_contact.title.value) == "")
      	missingInfo += "\n - Web title";
      if (trim(document.update_contact.url.value) == "")
      	missingInfo += "\n - Web address";
      if (trim(document.update_contact.people.value) == "")
      	missingInfo += "\n - Mail title";
  	  if (trim(document.update_contact.address.value) == "")
      	missingInfo += "\n - Mail address";
      if (trim(document.update_contact.suggestion.value) == "")
      	missingInfo += "\n - Suggestion mail address";
     
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}

function addNews() {
	missingInfo = "";
      if (trim(document.add_news.date.value) == "")
      	missingInfo += "\n - Date ";
      else {
	      var date = document.add_news.date.value;
	      var dArray = date.split("-");
	      if(dArray.length == 3) {
	      	var year = dArray[0];
	      	var month = dArray[1];
	      	var day = dArray[2];
	      	if(year < 1900 || year > 2100 || year.length != 4) {
	      		missingInfo += "\n - Date is in invalid format";
	      	} else if (day < 1 || day > 31 || day.length > 2) {
	      		missingInfo += "\n - Date is in invalid format";	
	      	} else if (month < 1 || month > 12 || month.length > 2) {
	      		missingInfo += "\n - Date is in invalid format";
	      	} else {
	      		//CORRECCT
	      	}
	      } else {
	      	missingInfo += "\n - Date is in invalid format";
	      }
      }
      
      if (trim(document.add_news.header.value) == "")
      	missingInfo += "\n - Title";
      if (trim(document.add_news.news.value) == "")
      	missingInfo += "\n - Text";
      
     
      
      
      if (missingInfo != "")
      {
		missingInfo ="You need to fill in the missing fields:\n" +
			missingInfo +
                  "\n\nInsert the correct information there before you submit!";

            alert(missingInfo);
            return false;
      }
      else
      {
            return true;
      }
}
